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 MEM_VEC_FUNCTIONS(qc_program, prog_section_statement, code)
33 MEM_VEC_FUNCTIONS(qc_program, prog_section_def, defs)
34 MEM_VEC_FUNCTIONS(qc_program, prog_section_def, fields)
35 MEM_VEC_FUNCTIONS(qc_program, prog_section_function, functions)
36 MEM_VEC_FUNCTIONS(qc_program, char, strings)
37 MEM_VEC_FUN_APPEND(qc_program, char, strings)
38 MEM_VEC_FUN_RESIZE(qc_program, char, strings)
39 MEM_VEC_FUNCTIONS(qc_program, qcint, globals)
40 MEM_VEC_FUNCTIONS(qc_program, qcint, entitydata)
41 MEM_VEC_FUNCTIONS(qc_program, bool, entitypool)
43 MEM_VEC_FUNCTIONS(qc_program, qcint, localstack)
44 MEM_VEC_FUN_APPEND(qc_program, qcint, localstack)
45 MEM_VEC_FUN_RESIZE(qc_program, qcint, localstack)
46 MEM_VEC_FUNCTIONS(qc_program, qc_exec_stack, stack)
48 MEM_VEC_FUNCTIONS(qc_program, size_t, profile)
49 MEM_VEC_FUN_RESIZE(qc_program, size_t, profile)
51 MEM_VEC_FUNCTIONS(qc_program, prog_builtin, builtins)
53 static void loaderror(const char *fmt, ...)
60 printf(": %s\n", strerror(err));
63 static void qcvmerror(qc_program *prog, const char *fmt, ...)
75 qc_program* prog_load(const char *filename)
82 file = util_fopen(filename, "rb");
86 if (fread(&header, sizeof(header), 1, file) != 1) {
87 loaderror("failed to read header from '%s'", filename);
92 if (header.version != 6) {
93 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
98 prog = (qc_program*)mem_a(sizeof(qc_program));
101 printf("failed to allocate program data\n");
104 memset(prog, 0, sizeof(*prog));
106 prog->entityfields = header.entfield;
107 prog->crc16 = header.crc16;
109 prog->filename = util_strdup(filename);
110 if (!prog->filename) {
111 loaderror("failed to store program name");
115 #define read_data(hdrvar, progvar, type) \
116 if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
117 loaderror("seek failed"); \
120 prog->progvar##_alloc = header.hdrvar.length; \
121 prog->progvar##_count = header.hdrvar.length; \
122 prog->progvar = (type*)mem_a(header.hdrvar.length * sizeof(*prog->progvar)); \
123 if (!prog->progvar) \
125 if (fread(prog->progvar, sizeof(*prog->progvar), header.hdrvar.length, file) \
126 != header.hdrvar.length) { \
127 loaderror("read failed"); \
130 #define read_data1(x, y) read_data(x, x, y)
132 read_data (statements, code, prog_section_statement);
133 read_data1(defs, prog_section_def);
134 read_data1(fields, prog_section_def);
135 read_data1(functions, prog_section_function);
136 read_data1(strings, char);
137 read_data1(globals, qcint);
141 /* profile counters */
142 if (!qc_program_profile_resize(prog, prog->code_count))
145 /* Add tempstring area */
146 prog->tempstring_start = prog->strings_count;
147 prog->tempstring_at = prog->strings_count;
148 if (!qc_program_strings_resize(prog, prog->strings_count + 16*1024))
151 /* spawn the world entity */
152 if (!qc_program_entitypool_add(prog, true)) {
153 loaderror("failed to allocate world entity\n");
156 for (i = 0; i < prog->entityfields; ++i) {
157 if (!qc_program_entitydata_add(prog, 0)) {
158 loaderror("failed to allocate world data\n");
167 if (prog->filename) mem_d(prog->filename);
168 if (prog->code) mem_d(prog->code);
169 if (prog->defs) mem_d(prog->defs);
170 if (prog->fields) mem_d(prog->fields);
171 if (prog->functions) mem_d(prog->functions);
172 if (prog->strings) mem_d(prog->strings);
173 if (prog->globals) mem_d(prog->globals);
174 if (prog->entitydata) mem_d(prog->entitydata);
175 if (prog->entitypool) mem_d(prog->entitypool);
180 void prog_delete(qc_program *prog)
182 if (prog->filename) mem_d(prog->filename);
183 MEM_VECTOR_CLEAR(prog, code);
184 MEM_VECTOR_CLEAR(prog, defs);
185 MEM_VECTOR_CLEAR(prog, fields);
186 MEM_VECTOR_CLEAR(prog, functions);
187 MEM_VECTOR_CLEAR(prog, strings);
188 MEM_VECTOR_CLEAR(prog, globals);
189 MEM_VECTOR_CLEAR(prog, entitydata);
190 MEM_VECTOR_CLEAR(prog, entitypool);
191 MEM_VECTOR_CLEAR(prog, localstack);
192 MEM_VECTOR_CLEAR(prog, stack);
193 MEM_VECTOR_CLEAR(prog, profile);
195 if (prog->builtins_alloc) {
196 MEM_VECTOR_CLEAR(prog, builtins);
198 /* otherwise the builtins were statically allocated */
202 /***********************************************************************
206 char* prog_getstring(qc_program *prog, qcint str)
208 if (str < 0 || str >= prog->strings_count)
209 return "<<<invalid string>>>";
210 return prog->strings + str;
213 prog_section_def* prog_entfield(qc_program *prog, qcint off)
216 for (i = 0; i < prog->fields_count; ++i) {
217 if (prog->fields[i].offset == off)
218 return (prog->fields + i);
223 prog_section_def* prog_getdef(qc_program *prog, qcint off)
226 for (i = 0; i < prog->defs_count; ++i) {
227 if (prog->defs[i].offset == off)
228 return (prog->defs + i);
233 qcany* prog_getedict(qc_program *prog, qcint e)
235 if (e >= prog->entitypool_count) {
237 printf("Accessing out of bounds edict %i\n", (int)e);
240 return (qcany*)(prog->entitydata + (prog->entityfields * e));
243 qcint prog_spawn_entity(qc_program *prog)
248 for (e = 0; e < (qcint)prog->entitypool_count; ++e) {
249 if (!prog->entitypool[e]) {
250 data = (char*)(prog->entitydata + (prog->entityfields * e));
251 memset(data, 0, prog->entityfields * sizeof(qcint));
255 if (!qc_program_entitypool_add(prog, true)) {
257 printf("Failed to allocate entity\n");
261 for (i = 0; i < prog->entityfields; ++i) {
262 if (!qc_program_entitydata_add(prog, 0)) {
263 printf("Failed to allocate entity\n");
267 data = (char*)(prog->entitydata + (prog->entityfields * e));
268 memset(data, 0, prog->entityfields * sizeof(qcint));
272 void prog_free_entity(qc_program *prog, qcint e)
276 printf("Trying to free world entity\n");
279 if (e >= prog->entitypool_count) {
281 printf("Trying to free out of bounds entity\n");
284 if (!prog->entitypool[e]) {
286 printf("Double free on entity\n");
289 prog->entitypool[e] = false;
292 qcint prog_tempstring(qc_program *prog, const char *_str)
294 /* we don't access it, but the macro-generated functions don't use
297 char *str = (char*)_str;
299 size_t len = strlen(str);
300 size_t at = prog->tempstring_at;
302 /* when we reach the end we start over */
303 if (at + len >= prog->strings_count)
304 at = prog->tempstring_start;
306 /* when it doesn't fit, reallocate */
307 if (at + len >= prog->strings_count)
309 prog->strings_count = at;
310 if (!qc_program_strings_append(prog, str, len+1)) {
311 prog->vmerror = VMERR_TEMPSTRING_ALLOC;
317 /* when it fits, just copy */
318 memcpy(prog->strings + at, str, len+1);
319 prog->tempstring_at += len+1;
323 static int print_escaped_string(const char *str, size_t maxlen)
327 --maxlen; /* because we're lazy and have escape sequences */
337 case '\a': len += 2; putchar('\\'); putchar('a'); break;
338 case '\b': len += 2; putchar('\\'); putchar('b'); break;
339 case '\r': len += 2; putchar('\\'); putchar('r'); break;
340 case '\n': len += 2; putchar('\\'); putchar('n'); break;
341 case '\t': len += 2; putchar('\\'); putchar('t'); break;
342 case '\f': len += 2; putchar('\\'); putchar('f'); break;
343 case '\v': len += 2; putchar('\\'); putchar('v'); break;
344 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
345 case '"': len += 2; putchar('\\'); putchar('"'); break;
357 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
359 static char spaces[28+1] = " ";
360 prog_section_def *def;
365 len = printf("<null>,");
369 def = prog_getdef(prog, glob);
370 value = (qcany*)(&prog->globals[glob]);
373 const char *name = prog_getstring(prog, def->name);
377 len = printf("%s ", name);
378 vtype = def->type & DEF_TYPEMASK;
381 len = printf("[@%u] ", glob);
389 len += printf("(%i),", value->_int);
392 len += printf("'%g %g %g',", value->vector[0],
397 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
399 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
403 len += printf("%g,", value->_float);
407 if (len < sizeof(spaces)-1) {
408 spaces[sizeof(spaces)-1-len] = 0;
410 spaces[sizeof(spaces)-1-len] = ' ';
414 static void prog_print_statement(qc_program *prog, prog_section_statement *st)
416 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
417 printf("<illegal instruction %d>\n", st->opcode);
420 printf(" <> %-12s", asm_instr[st->opcode].m);
421 if (st->opcode >= INSTR_IF &&
422 st->opcode <= INSTR_IFNOT)
424 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
425 printf("%d\n", st->o2.s1);
427 else if (st->opcode >= INSTR_CALL0 &&
428 st->opcode <= INSTR_CALL8)
430 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
433 else if (st->opcode == INSTR_GOTO)
435 printf("%i\n", st->o1.s1);
439 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
443 t[1] = t[2] = TYPE_VECTOR;
446 t[0] = t[2] = TYPE_VECTOR;
449 t[0] = t[1] = TYPE_VECTOR;
455 t[0] = t[1] = t[2] = TYPE_VECTOR;
459 t[0] = t[1] = TYPE_STRING;
466 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
469 t[0] = t[1] = TYPE_STRING; t[2] = -1;
471 case INSTR_STORE_ENT:
472 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
474 case INSTR_STORE_FLD:
475 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
477 case INSTR_STORE_FNC:
478 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
481 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
484 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
486 case INSTR_STOREP_ENT:
487 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
489 case INSTR_STOREP_FLD:
490 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
492 case INSTR_STOREP_FNC:
493 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
496 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
497 else printf("(none), ");
498 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
499 else printf("(none), ");
500 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
501 else printf("(none)");
507 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
513 st.localsp = prog->localstack_count;
514 st.stmt = prog->statement;
517 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
518 if (prog->stack_count)
520 prog_section_function *cur;
521 cur = prog->stack[prog->stack_count-1].function;
524 qcint *globals = prog->globals + cur->firstlocal;
525 if (!qc_program_localstack_append(prog, globals, cur->locals))
527 printf("out of memory\n");
534 qcint *globals = prog->globals + func->firstlocal;
535 if (!qc_program_localstack_append(prog, globals, func->locals))
537 printf("out of memory\n");
543 /* copy parameters */
544 parampos = func->firstlocal;
545 for (p = 0; p < func->nargs; ++p)
548 for (s = 0; s < func->argsize[p]; ++s) {
549 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
554 if (!qc_program_stack_add(prog, st)) {
555 printf("out of memory\n");
562 static qcint prog_leavefunction(qc_program *prog)
564 prog_section_function *prev = NULL;
567 qc_exec_stack st = prog->stack[prog->stack_count-1];
569 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
570 if (prog->stack_count > 1) {
571 prev = prog->stack[prog->stack_count-2].function;
572 oldsp = prog->stack[prog->stack_count-2].localsp;
575 prev = prog->stack[prog->stack_count-1].function;
576 oldsp = prog->stack[prog->stack_count-1].localsp;
579 qcint *globals = prog->globals + prev->firstlocal;
580 memcpy(globals, prog->localstack + oldsp, prev->locals);
581 if (!qc_program_localstack_resize(prog, oldsp)) {
582 printf("out of memory\n");
587 if (!qc_program_stack_remove(prog, prog->stack_count-1)) {
588 printf("out of memory\n");
592 return st.stmt - 1; /* offset the ++st */
595 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
598 size_t oldxflags = prog->xflags;
599 prog_section_statement *st;
602 prog->xflags = flags;
604 st = prog->code + prog_enterfunction(prog, func);
612 #define QCVM_PROFILE 0
619 #define QCVM_PROFILE 0
626 #define QCVM_PROFILE 1
631 case (VMXF_TRACE|VMXF_PROFILE):
633 #define QCVM_PROFILE 1
641 prog->xflags = oldxflags;
642 prog->localstack_count = 0;
643 prog->stack_count = 0;
649 /***********************************************************************
650 * main for when building the standalone executor
653 #if defined(QCVM_EXECUTOR)
656 const char *type_name[TYPE_COUNT] = {
671 bool opts_debug = false;
672 bool opts_memchk = false;
679 VECTOR_MAKE(qcvm_parameter, main_params);
681 #define CheckArgs(num) do { \
682 if (prog->argc != (num)) { \
684 printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
685 __FUNCTION__, prog->argc, (num)); \
690 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
691 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
692 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
694 static int qc_print(qc_program *prog)
697 const char *laststr = NULL;
698 for (i = 0; i < prog->argc; ++i) {
699 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
700 printf("%s", (laststr = prog_getstring(prog, str->string)));
702 if (laststr && (prog->xflags & VMXF_TRACE)) {
703 size_t len = strlen(laststr);
704 if (!len || laststr[len-1] != '\n')
710 static int qc_error(qc_program *prog)
712 printf("*** VM raised an error:\n");
718 static int qc_ftos(qc_program *prog)
725 snprintf(buffer, sizeof(buffer), "%g", num->_float);
726 str.string = prog_tempstring(prog, buffer);
731 static int qc_vtos(qc_program *prog)
738 snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
739 str.string = prog_tempstring(prog, buffer);
744 static int qc_etos(qc_program *prog)
751 snprintf(buffer, sizeof(buffer), "%i", num->_int);
752 str.string = prog_tempstring(prog, buffer);
757 static int qc_spawn(qc_program *prog)
761 ent.edict = prog_spawn_entity(prog);
763 return (ent.edict ? 0 : -1);
766 static int qc_kill(qc_program *prog)
771 prog_free_entity(prog, ent->edict);
775 static int qc_vlen(qc_program *prog)
780 len._float = sqrt(vec->vector[0] * vec->vector[0] +
781 vec->vector[1] * vec->vector[1] +
782 vec->vector[2] * vec->vector[2]);
787 static prog_builtin qc_builtins[] = {
798 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
800 static const char *arg0 = NULL;
804 printf("usage: [-debug] %s file\n", arg0);
808 static void prog_main_setparams(qc_program *prog)
813 for (i = 0; i < main_params_elements; ++i) {
814 arg = GetGlobal(OFS_PARM0 + 3*i);
818 switch (main_params_data[i].vtype) {
821 (void)sscanf_s(main_params_data[i].value, " %f %f %f ",
826 (void)sscanf(main_params_data[i].value, " %f %f %f ",
833 arg->_float = atof(main_params_data[i].value);
836 arg->string = prog_tempstring(prog, main_params_data[i].value);
839 printf("error: unhandled parameter type: %i\n", main_params_data[i].vtype);
845 void prog_disasm_function(qc_program *prog, size_t id);
846 int main(int argc, char **argv)
851 size_t xflags = VMXF_DEFAULT;
852 bool opts_printfields = false;
853 bool opts_printdefs = false;
854 bool opts_disasm = false;
855 bool opts_info = false;
863 if (!strcmp(argv[1], "-trace")) {
866 xflags |= VMXF_TRACE;
868 else if (!strcmp(argv[1], "-profile")) {
871 xflags |= VMXF_PROFILE;
873 else if (!strcmp(argv[1], "-info")) {
878 else if (!strcmp(argv[1], "-disasm")) {
883 else if (!strcmp(argv[1], "-printdefs")) {
886 opts_printdefs = true;
888 else if (!strcmp(argv[1], "-printfields")) {
891 opts_printfields = true;
893 else if (!strcmp(argv[1], "-vector") ||
894 !strcmp(argv[1], "-string") ||
895 !strcmp(argv[1], "-float") )
898 if (argv[1][1] == 'f')
899 p.vtype = TYPE_FLOAT;
900 else if (argv[1][1] == 's')
901 p.vtype = TYPE_STRING;
902 else if (argv[1][1] == 'v')
903 p.vtype = TYPE_VECTOR;
911 if (main_params_add(p) < 0) {
912 if (main_params_data)
913 mem_d(main_params_data);
914 printf("cannot add parameter\n");
925 prog = prog_load(argv[1]);
927 printf("failed to load program '%s'\n", argv[1]);
931 prog->builtins = qc_builtins;
932 prog->builtins_count = qc_builtins_count;
933 prog->builtins_alloc = 0;
936 printf("Program's system-checksum = 0x%04x\n", (int)prog->crc16);
937 printf("Entity field space: %i\n", (int)prog->entityfields);
940 for (i = 1; i < prog->functions_count; ++i) {
941 const char *name = prog_getstring(prog, prog->functions[i].name);
942 /* printf("Found function: %s\n", name); */
943 if (!strcmp(name, "main"))
951 for (i = 1; i < prog->functions_count; ++i)
952 prog_disasm_function(prog, i);
955 if (opts_printdefs) {
956 for (i = 0; i < prog->defs_count; ++i) {
957 printf("Global: %8s %-16s at %u\n",
958 type_name[prog->defs[i].type & DEF_TYPEMASK],
959 prog_getstring(prog, prog->defs[i].name),
960 (unsigned int)prog->defs[i].offset);
963 else if (opts_printfields) {
964 for (i = 0; i < prog->fields_count; ++i) {
965 printf("Field: %8s %-16s at %u\n",
966 type_name[prog->fields[i].type],
967 prog_getstring(prog, prog->fields[i].name),
968 (unsigned int)prog->fields[i].offset);
975 prog_main_setparams(prog);
976 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
979 printf("No main function found\n");
986 void prog_disasm_function(qc_program *prog, size_t id)
988 prog_section_function *fdef = prog->functions + id;
989 prog_section_statement *st;
991 if (fdef->entry < 0) {
992 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
996 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
998 st = prog->code + fdef->entry;
999 while (st->opcode != AINSTR_END) {
1000 prog_print_statement(prog, st);
1005 #else /* !QCVM_LOOP */
1007 * Everything from here on is not including into the compilation of the
1008 * executor. This is simply code that is #included via #include __FILE__
1009 * see when QCVM_LOOP is defined, the rest of the code above do not get
1010 * re-included. So this really just acts like one large macro, but it
1011 * sort of isn't, which makes it nicer looking.
1014 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1015 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1016 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1018 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1020 /* to be consistent with current darkplaces behaviour */
1021 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1022 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1026 prog_section_function *newf;
1033 prog->profile[st - prog->code]++;
1037 prog_print_statement(prog, st);
1043 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1048 /* TODO: add instruction count to function profile count */
1049 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1050 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1051 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1053 st = prog->code + prog_leavefunction(prog);
1054 if (!prog->stack_count)
1060 OPC->_float = OPA->_float * OPB->_float;
1063 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1064 OPA->vector[1]*OPB->vector[1] +
1065 OPA->vector[2]*OPB->vector[2];
1068 OPC->vector[0] = OPA->_float * OPB->vector[0];
1069 OPC->vector[1] = OPA->_float * OPB->vector[1];
1070 OPC->vector[2] = OPA->_float * OPB->vector[2];
1073 OPC->vector[0] = OPB->_float * OPA->vector[0];
1074 OPC->vector[1] = OPB->_float * OPA->vector[1];
1075 OPC->vector[2] = OPB->_float * OPA->vector[2];
1078 if (OPB->_float != 0.0f)
1079 OPC->_float = OPA->_float / OPB->_float;
1085 OPC->_float = OPA->_float + OPB->_float;
1088 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1089 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1090 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1093 OPC->_float = OPA->_float - OPB->_float;
1096 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1097 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1098 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1102 OPC->_float = (OPA->_float == OPB->_float);
1105 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1106 (OPA->vector[1] == OPB->vector[1]) &&
1107 (OPA->vector[2] == OPB->vector[2]) );
1110 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1111 prog_getstring(prog, OPB->string));
1114 OPC->_float = (OPA->_int == OPB->_int);
1117 OPC->_float = (OPA->function == OPB->function);
1120 OPC->_float = (OPA->_float != OPB->_float);
1123 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1124 (OPA->vector[1] != OPB->vector[1]) ||
1125 (OPA->vector[2] != OPB->vector[2]) );
1128 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1129 prog_getstring(prog, OPB->string));
1132 OPC->_float = (OPA->_int != OPB->_int);
1135 OPC->_float = (OPA->function != OPB->function);
1139 OPC->_float = (OPA->_float <= OPB->_float);
1142 OPC->_float = (OPA->_float >= OPB->_float);
1145 OPC->_float = (OPA->_float < OPB->_float);
1148 OPC->_float = (OPA->_float > OPB->_float);
1153 case INSTR_LOAD_FLD:
1154 case INSTR_LOAD_ENT:
1155 case INSTR_LOAD_FNC:
1156 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1157 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1160 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1161 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1166 ed = prog_getedict(prog, OPA->edict);
1167 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1170 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1171 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1174 if (OPB->_int < 0 || OPB->_int + 3 > prog->entityfields)
1176 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1181 ed = prog_getedict(prog, OPA->edict);
1182 OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
1183 OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
1184 OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
1188 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1189 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1192 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1194 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1200 ed = prog_getedict(prog, OPA->edict);
1201 OPC->_int = ((qcint*)ed) - prog->entitydata;
1202 OPC->_int += OPB->_int;
1207 case INSTR_STORE_ENT:
1208 case INSTR_STORE_FLD:
1209 case INSTR_STORE_FNC:
1210 OPB->_int = OPA->_int;
1213 OPB->ivector[0] = OPA->ivector[0];
1214 OPB->ivector[1] = OPA->ivector[1];
1215 OPB->ivector[2] = OPA->ivector[2];
1218 case INSTR_STOREP_F:
1219 case INSTR_STOREP_S:
1220 case INSTR_STOREP_ENT:
1221 case INSTR_STOREP_FLD:
1222 case INSTR_STOREP_FNC:
1223 if (OPB->_int < 0 || OPB->_int >= prog->entitydata_count) {
1224 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1227 if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
1228 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1230 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1232 ptr = (qcany*)(prog->entitydata + OPB->_int);
1233 ptr->_int = OPA->_int;
1235 case INSTR_STOREP_V:
1236 if (OPB->_int < 0 || OPB->_int + 2 >= prog->entitydata_count) {
1237 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1240 if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
1241 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1243 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1245 ptr = (qcany*)(prog->entitydata + OPB->_int);
1246 ptr->ivector[0] = OPA->ivector[0];
1247 ptr->ivector[1] = OPA->ivector[1];
1248 ptr->ivector[2] = OPA->ivector[2];
1252 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1255 OPC->_float = !OPA->vector[0] &&
1260 OPC->_float = !OPA->string ||
1261 !*prog_getstring(prog, OPA->string);
1264 OPC->_float = (OPA->edict == 0);
1267 OPC->_float = !OPA->function;
1271 /* this is consistent with darkplaces' behaviour */
1272 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1274 st += st->o2.s1 - 1; /* offset the s++ */
1275 if (++jumpcount >= maxjumps)
1276 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1280 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1282 st += st->o2.s1 - 1; /* offset the s++ */
1283 if (++jumpcount >= maxjumps)
1284 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1297 prog->argc = st->opcode - INSTR_CALL0;
1299 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1301 if(!OPA->function || OPA->function >= (unsigned int)prog->functions_count)
1303 qcvmerror(prog, "CALL outside the program in `%s` (%i)", prog->filename, (int)OPA->function);
1307 newf = &prog->functions[OPA->function];
1310 prog->statement = (st - prog->code) + 1;
1312 if (newf->entry < 0)
1314 /* negative statements are built in functions */
1315 int builtinnumber = -newf->entry;
1316 if (builtinnumber < prog->builtins_count && prog->builtins[builtinnumber])
1317 prog->builtins[builtinnumber](prog);
1319 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1320 builtinnumber, prog->filename);
1323 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1329 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1333 st += st->o1.s1 - 1; /* offset the s++ */
1334 if (++jumpcount == 10000000)
1335 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1339 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1340 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1343 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1344 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1348 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1351 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1358 #endif /* !QCVM_LOOP */