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 MEM_VEC_FUNCTIONS(qc_program, const char*, function_stack)
55 static void loaderror(const char *fmt, ...)
62 printf(": %s\n", strerror(err));
65 static void qcvmerror(qc_program *prog, const char *fmt, ...)
77 qc_program* prog_load(const char *filename)
84 file = util_fopen(filename, "rb");
88 if (fread(&header, sizeof(header), 1, file) != 1) {
89 loaderror("failed to read header from '%s'", filename);
94 if (header.version != 6) {
95 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
100 prog = (qc_program*)mem_a(sizeof(qc_program));
103 printf("failed to allocate program data\n");
106 memset(prog, 0, sizeof(*prog));
108 prog->entityfields = header.entfield;
109 prog->crc16 = header.crc16;
111 prog->filename = util_strdup(filename);
112 if (!prog->filename) {
113 loaderror("failed to store program name");
117 #define read_data(hdrvar, progvar, type) \
118 if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
119 loaderror("seek failed"); \
122 prog->progvar##_alloc = header.hdrvar.length; \
123 prog->progvar##_count = header.hdrvar.length; \
124 prog->progvar = (type*)mem_a(header.hdrvar.length * sizeof(*prog->progvar)); \
125 if (!prog->progvar) \
127 if (fread(prog->progvar, sizeof(*prog->progvar), header.hdrvar.length, file) \
128 != header.hdrvar.length) { \
129 loaderror("read failed"); \
132 #define read_data1(x, y) read_data(x, x, y)
134 read_data (statements, code, prog_section_statement);
135 read_data1(defs, prog_section_def);
136 read_data1(fields, prog_section_def);
137 read_data1(functions, prog_section_function);
138 read_data1(strings, char);
139 read_data1(globals, qcint);
143 /* profile counters */
144 if (!qc_program_profile_resize(prog, prog->code_count))
147 /* Add tempstring area */
148 prog->tempstring_start = prog->strings_count;
149 prog->tempstring_at = prog->strings_count;
150 if (!qc_program_strings_resize(prog, prog->strings_count + 16*1024))
153 /* spawn the world entity */
154 if (!qc_program_entitypool_add(prog, true)) {
155 loaderror("failed to allocate world entity\n");
158 for (i = 0; i < prog->entityfields; ++i) {
159 if (!qc_program_entitydata_add(prog, 0)) {
160 loaderror("failed to allocate world data\n");
169 if (prog->filename) mem_d(prog->filename);
170 if (prog->code) mem_d(prog->code);
171 if (prog->defs) mem_d(prog->defs);
172 if (prog->fields) mem_d(prog->fields);
173 if (prog->functions) mem_d(prog->functions);
174 if (prog->strings) mem_d(prog->strings);
175 if (prog->globals) mem_d(prog->globals);
176 if (prog->entitydata) mem_d(prog->entitydata);
177 if (prog->entitypool) mem_d(prog->entitypool);
182 void prog_delete(qc_program *prog)
184 if (prog->filename) mem_d(prog->filename);
185 MEM_VECTOR_CLEAR(prog, code);
186 MEM_VECTOR_CLEAR(prog, defs);
187 MEM_VECTOR_CLEAR(prog, fields);
188 MEM_VECTOR_CLEAR(prog, functions);
189 MEM_VECTOR_CLEAR(prog, strings);
190 MEM_VECTOR_CLEAR(prog, globals);
191 MEM_VECTOR_CLEAR(prog, entitydata);
192 MEM_VECTOR_CLEAR(prog, entitypool);
193 MEM_VECTOR_CLEAR(prog, localstack);
194 MEM_VECTOR_CLEAR(prog, stack);
195 MEM_VECTOR_CLEAR(prog, profile);
197 if (prog->builtins_alloc) {
198 MEM_VECTOR_CLEAR(prog, builtins);
200 /* otherwise the builtins were statically allocated */
204 /***********************************************************************
208 char* prog_getstring(qc_program *prog, qcint str)
210 if (str < 0 || str >= prog->strings_count)
211 return "<<<invalid string>>>";
212 return prog->strings + str;
215 prog_section_def* prog_entfield(qc_program *prog, qcint off)
218 for (i = 0; i < prog->fields_count; ++i) {
219 if (prog->fields[i].offset == off)
220 return (prog->fields + i);
225 prog_section_def* prog_getdef(qc_program *prog, qcint off)
228 for (i = 0; i < prog->defs_count; ++i) {
229 if (prog->defs[i].offset == off)
230 return (prog->defs + i);
235 qcany* prog_getedict(qc_program *prog, qcint e)
237 if (e >= prog->entitypool_count) {
239 printf("Accessing out of bounds edict %i\n", (int)e);
242 return (qcany*)(prog->entitydata + (prog->entityfields * e));
245 qcint prog_spawn_entity(qc_program *prog)
250 for (e = 0; e < (qcint)prog->entitypool_count; ++e) {
251 if (!prog->entitypool[e]) {
252 data = (char*)(prog->entitydata + (prog->entityfields * e));
253 memset(data, 0, prog->entityfields * sizeof(qcint));
257 if (!qc_program_entitypool_add(prog, true)) {
259 printf("Failed to allocate entity\n");
263 for (i = 0; i < prog->entityfields; ++i) {
264 if (!qc_program_entitydata_add(prog, 0)) {
265 printf("Failed to allocate entity\n");
269 data = (char*)(prog->entitydata + (prog->entityfields * e));
270 memset(data, 0, prog->entityfields * sizeof(qcint));
274 void prog_free_entity(qc_program *prog, qcint e)
278 printf("Trying to free world entity\n");
281 if (e >= prog->entitypool_count) {
283 printf("Trying to free out of bounds entity\n");
286 if (!prog->entitypool[e]) {
288 printf("Double free on entity\n");
291 prog->entitypool[e] = false;
294 qcint prog_tempstring(qc_program *prog, const char *_str)
296 /* we don't access it, but the macro-generated functions don't use
299 char *str = (char*)_str;
301 size_t len = strlen(str);
302 size_t at = prog->tempstring_at;
304 /* when we reach the end we start over */
305 if (at + len >= prog->strings_count)
306 at = prog->tempstring_start;
308 /* when it doesn't fit, reallocate */
309 if (at + len >= prog->strings_count)
311 prog->strings_count = at;
312 if (!qc_program_strings_append(prog, str, len+1)) {
313 prog->vmerror = VMERR_TEMPSTRING_ALLOC;
319 /* when it fits, just copy */
320 memcpy(prog->strings + at, str, len+1);
321 prog->tempstring_at += len+1;
325 static int print_escaped_string(const char *str, size_t maxlen)
329 --maxlen; /* because we're lazy and have escape sequences */
339 case '\a': len += 2; putchar('\\'); putchar('a'); break;
340 case '\b': len += 2; putchar('\\'); putchar('b'); break;
341 case '\r': len += 2; putchar('\\'); putchar('r'); break;
342 case '\n': len += 2; putchar('\\'); putchar('n'); break;
343 case '\t': len += 2; putchar('\\'); putchar('t'); break;
344 case '\f': len += 2; putchar('\\'); putchar('f'); break;
345 case '\v': len += 2; putchar('\\'); putchar('v'); break;
346 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
347 case '"': len += 2; putchar('\\'); putchar('"'); break;
359 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
361 static char spaces[28+1] = " ";
362 prog_section_def *def;
367 len = printf("<null>,");
371 def = prog_getdef(prog, glob);
372 value = (qcany*)(&prog->globals[glob]);
375 const char *name = prog_getstring(prog, def->name);
379 len = printf("%s ", name);
380 vtype = def->type & DEF_TYPEMASK;
383 len = printf("[@%u] ", glob);
391 len += printf("(%i),", value->_int);
394 len += printf("'%g %g %g',", value->vector[0],
399 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
401 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
405 len += printf("%g,", value->_float);
409 if (len < sizeof(spaces)-1) {
410 spaces[sizeof(spaces)-1-len] = 0;
412 spaces[sizeof(spaces)-1-len] = ' ';
416 static void prog_print_statement(qc_program *prog, prog_section_statement *st)
418 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
419 printf("<illegal instruction %d>\n", st->opcode);
422 if ((prog->xflags & VMXF_TRACE) && prog->function_stack_count) {
424 for (i = 0; i < prog->function_stack_count; ++i)
426 printf("%s:", prog->function_stack[prog->function_stack_count-1]);
428 printf(" <> %-12s", asm_instr[st->opcode].m);
429 if (st->opcode >= INSTR_IF &&
430 st->opcode <= INSTR_IFNOT)
432 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
433 printf("%d\n", st->o2.s1);
435 else if (st->opcode >= INSTR_CALL0 &&
436 st->opcode <= INSTR_CALL8)
438 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
441 else if (st->opcode == INSTR_GOTO)
443 printf("%i\n", st->o1.s1);
447 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
451 t[1] = t[2] = TYPE_VECTOR;
454 t[0] = t[2] = TYPE_VECTOR;
457 t[0] = t[1] = TYPE_VECTOR;
463 t[0] = t[1] = t[2] = TYPE_VECTOR;
467 t[0] = t[1] = TYPE_STRING;
474 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
477 t[0] = t[1] = TYPE_STRING; t[2] = -1;
479 case INSTR_STORE_ENT:
480 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
482 case INSTR_STORE_FLD:
483 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
485 case INSTR_STORE_FNC:
486 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
489 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
492 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
494 case INSTR_STOREP_ENT:
495 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
497 case INSTR_STOREP_FLD:
498 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
500 case INSTR_STOREP_FNC:
501 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
504 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
505 else printf("(none), ");
506 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
507 else printf("(none), ");
508 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
509 else printf("(none)");
515 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
521 st.localsp = prog->localstack_count;
522 st.stmt = prog->statement;
525 if (prog->xflags & VMXF_TRACE) {
526 (void)!qc_program_function_stack_add(prog, prog_getstring(prog, func->name));
529 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
530 if (prog->stack_count)
532 prog_section_function *cur;
533 cur = prog->stack[prog->stack_count-1].function;
536 qcint *globals = prog->globals + cur->firstlocal;
537 if (!qc_program_localstack_append(prog, globals, cur->locals))
539 printf("out of memory\n");
546 qcint *globals = prog->globals + func->firstlocal;
547 if (!qc_program_localstack_append(prog, globals, func->locals))
549 printf("out of memory\n");
555 /* copy parameters */
556 parampos = func->firstlocal;
557 for (p = 0; p < func->nargs; ++p)
560 for (s = 0; s < func->argsize[p]; ++s) {
561 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
566 if (!qc_program_stack_add(prog, st)) {
567 printf("out of memory\n");
574 static qcint prog_leavefunction(qc_program *prog)
576 prog_section_function *prev = NULL;
579 qc_exec_stack st = prog->stack[prog->stack_count-1];
581 if (prog->xflags & VMXF_TRACE) {
582 if (prog->function_stack_count)
583 prog->function_stack_count--;
586 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
587 if (prog->stack_count > 1) {
588 prev = prog->stack[prog->stack_count-2].function;
589 oldsp = prog->stack[prog->stack_count-2].localsp;
592 prev = prog->stack[prog->stack_count-1].function;
593 oldsp = prog->stack[prog->stack_count-1].localsp;
596 qcint *globals = prog->globals + prev->firstlocal;
597 memcpy(globals, prog->localstack + oldsp, prev->locals);
598 if (!qc_program_localstack_resize(prog, oldsp)) {
599 printf("out of memory\n");
604 if (!qc_program_stack_remove(prog, prog->stack_count-1)) {
605 printf("out of memory\n");
609 return st.stmt - 1; /* offset the ++st */
612 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
615 size_t oldxflags = prog->xflags;
616 prog_section_statement *st;
619 prog->xflags = flags;
621 st = prog->code + prog_enterfunction(prog, func);
629 #define QCVM_PROFILE 0
636 #define QCVM_PROFILE 0
643 #define QCVM_PROFILE 1
648 case (VMXF_TRACE|VMXF_PROFILE):
650 #define QCVM_PROFILE 1
658 prog->xflags = oldxflags;
659 prog->localstack_count = 0;
660 prog->stack_count = 0;
666 /***********************************************************************
667 * main for when building the standalone executor
670 #if defined(QCVM_EXECUTOR)
673 const char *type_name[TYPE_COUNT] = {
688 bool opts_debug = false;
689 bool opts_memchk = false;
696 VECTOR_MAKE(qcvm_parameter, main_params);
698 #define CheckArgs(num) do { \
699 if (prog->argc != (num)) { \
701 printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
702 __FUNCTION__, prog->argc, (num)); \
707 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
708 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
709 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
711 static int qc_print(qc_program *prog)
714 const char *laststr = NULL;
715 for (i = 0; i < prog->argc; ++i) {
716 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
717 printf("%s", (laststr = prog_getstring(prog, str->string)));
719 if (laststr && (prog->xflags & VMXF_TRACE)) {
720 size_t len = strlen(laststr);
721 if (!len || laststr[len-1] != '\n')
727 static int qc_error(qc_program *prog)
729 printf("*** VM raised an error:\n");
735 static int qc_ftos(qc_program *prog)
742 snprintf(buffer, sizeof(buffer), "%g", num->_float);
743 str.string = prog_tempstring(prog, buffer);
748 static int qc_vtos(qc_program *prog)
755 snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
756 str.string = prog_tempstring(prog, buffer);
761 static int qc_etos(qc_program *prog)
768 snprintf(buffer, sizeof(buffer), "%i", num->_int);
769 str.string = prog_tempstring(prog, buffer);
774 static int qc_spawn(qc_program *prog)
778 ent.edict = prog_spawn_entity(prog);
780 return (ent.edict ? 0 : -1);
783 static int qc_kill(qc_program *prog)
788 prog_free_entity(prog, ent->edict);
792 static int qc_vlen(qc_program *prog)
797 len._float = sqrt(vec->vector[0] * vec->vector[0] +
798 vec->vector[1] * vec->vector[1] +
799 vec->vector[2] * vec->vector[2]);
804 static prog_builtin qc_builtins[] = {
815 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
817 static const char *arg0 = NULL;
821 printf("usage: [-debug] %s file\n", arg0);
825 static void prog_main_setparams(qc_program *prog)
830 for (i = 0; i < main_params_elements; ++i) {
831 arg = GetGlobal(OFS_PARM0 + 3*i);
835 switch (main_params_data[i].vtype) {
838 (void)sscanf_s(main_params_data[i].value, " %f %f %f ",
843 (void)sscanf(main_params_data[i].value, " %f %f %f ",
850 arg->_float = atof(main_params_data[i].value);
853 arg->string = prog_tempstring(prog, main_params_data[i].value);
856 printf("error: unhandled parameter type: %i\n", main_params_data[i].vtype);
862 void prog_disasm_function(qc_program *prog, size_t id);
863 int main(int argc, char **argv)
868 size_t xflags = VMXF_DEFAULT;
869 bool opts_printfields = false;
870 bool opts_printdefs = false;
871 bool opts_disasm = false;
872 bool opts_info = false;
880 if (!strcmp(argv[1], "-trace")) {
883 xflags |= VMXF_TRACE;
885 else if (!strcmp(argv[1], "-profile")) {
888 xflags |= VMXF_PROFILE;
890 else if (!strcmp(argv[1], "-info")) {
895 else if (!strcmp(argv[1], "-disasm")) {
900 else if (!strcmp(argv[1], "-printdefs")) {
903 opts_printdefs = true;
905 else if (!strcmp(argv[1], "-printfields")) {
908 opts_printfields = true;
910 else if (!strcmp(argv[1], "-vector") ||
911 !strcmp(argv[1], "-string") ||
912 !strcmp(argv[1], "-float") )
915 if (argv[1][1] == 'f')
916 p.vtype = TYPE_FLOAT;
917 else if (argv[1][1] == 's')
918 p.vtype = TYPE_STRING;
919 else if (argv[1][1] == 'v')
920 p.vtype = TYPE_VECTOR;
928 if (main_params_add(p) < 0) {
929 if (main_params_data)
930 mem_d(main_params_data);
931 printf("cannot add parameter\n");
942 prog = prog_load(argv[1]);
944 printf("failed to load program '%s'\n", argv[1]);
948 prog->builtins = qc_builtins;
949 prog->builtins_count = qc_builtins_count;
950 prog->builtins_alloc = 0;
953 printf("Program's system-checksum = 0x%04x\n", (int)prog->crc16);
954 printf("Entity field space: %i\n", (int)prog->entityfields);
957 for (i = 1; i < prog->functions_count; ++i) {
958 const char *name = prog_getstring(prog, prog->functions[i].name);
959 /* printf("Found function: %s\n", name); */
960 if (!strcmp(name, "main"))
968 for (i = 1; i < prog->functions_count; ++i)
969 prog_disasm_function(prog, i);
972 if (opts_printdefs) {
973 for (i = 0; i < prog->defs_count; ++i) {
974 printf("Global: %8s %-16s at %u\n",
975 type_name[prog->defs[i].type & DEF_TYPEMASK],
976 prog_getstring(prog, prog->defs[i].name),
977 (unsigned int)prog->defs[i].offset);
980 else if (opts_printfields) {
981 for (i = 0; i < prog->fields_count; ++i) {
982 printf("Field: %8s %-16s at %u\n",
983 type_name[prog->fields[i].type],
984 prog_getstring(prog, prog->fields[i].name),
985 (unsigned int)prog->fields[i].offset);
992 prog_main_setparams(prog);
993 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
996 printf("No main function found\n");
1003 void prog_disasm_function(qc_program *prog, size_t id)
1005 prog_section_function *fdef = prog->functions + id;
1006 prog_section_statement *st;
1008 if (fdef->entry < 0) {
1009 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1013 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1015 st = prog->code + fdef->entry;
1016 while (st->opcode != AINSTR_END) {
1017 prog_print_statement(prog, st);
1022 #else /* !QCVM_LOOP */
1024 * Everything from here on is not including into the compilation of the
1025 * executor. This is simply code that is #included via #include __FILE__
1026 * see when QCVM_LOOP is defined, the rest of the code above do not get
1027 * re-included. So this really just acts like one large macro, but it
1028 * sort of isn't, which makes it nicer looking.
1031 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1032 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1033 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1035 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1037 /* to be consistent with current darkplaces behaviour */
1038 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1039 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1043 prog_section_function *newf;
1050 prog->profile[st - prog->code]++;
1054 prog_print_statement(prog, st);
1060 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1065 /* TODO: add instruction count to function profile count */
1066 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1067 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1068 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1070 st = prog->code + prog_leavefunction(prog);
1071 if (!prog->stack_count)
1077 OPC->_float = OPA->_float * OPB->_float;
1080 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1081 OPA->vector[1]*OPB->vector[1] +
1082 OPA->vector[2]*OPB->vector[2];
1085 OPC->vector[0] = OPA->_float * OPB->vector[0];
1086 OPC->vector[1] = OPA->_float * OPB->vector[1];
1087 OPC->vector[2] = OPA->_float * OPB->vector[2];
1090 OPC->vector[0] = OPB->_float * OPA->vector[0];
1091 OPC->vector[1] = OPB->_float * OPA->vector[1];
1092 OPC->vector[2] = OPB->_float * OPA->vector[2];
1095 if (OPB->_float != 0.0f)
1096 OPC->_float = OPA->_float / OPB->_float;
1102 OPC->_float = OPA->_float + OPB->_float;
1105 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1106 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1107 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1110 OPC->_float = OPA->_float - OPB->_float;
1113 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1114 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1115 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1119 OPC->_float = (OPA->_float == OPB->_float);
1122 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1123 (OPA->vector[1] == OPB->vector[1]) &&
1124 (OPA->vector[2] == OPB->vector[2]) );
1127 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1128 prog_getstring(prog, OPB->string));
1131 OPC->_float = (OPA->_int == OPB->_int);
1134 OPC->_float = (OPA->function == OPB->function);
1137 OPC->_float = (OPA->_float != OPB->_float);
1140 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1141 (OPA->vector[1] != OPB->vector[1]) ||
1142 (OPA->vector[2] != OPB->vector[2]) );
1145 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1146 prog_getstring(prog, OPB->string));
1149 OPC->_float = (OPA->_int != OPB->_int);
1152 OPC->_float = (OPA->function != OPB->function);
1156 OPC->_float = (OPA->_float <= OPB->_float);
1159 OPC->_float = (OPA->_float >= OPB->_float);
1162 OPC->_float = (OPA->_float < OPB->_float);
1165 OPC->_float = (OPA->_float > OPB->_float);
1170 case INSTR_LOAD_FLD:
1171 case INSTR_LOAD_ENT:
1172 case INSTR_LOAD_FNC:
1173 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1174 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1177 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1178 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1183 ed = prog_getedict(prog, OPA->edict);
1184 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1187 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1188 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1191 if (OPB->_int < 0 || OPB->_int + 3 > prog->entityfields)
1193 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1198 ed = prog_getedict(prog, OPA->edict);
1199 OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
1200 OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
1201 OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
1205 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1206 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1209 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1211 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1217 ed = prog_getedict(prog, OPA->edict);
1218 OPC->_int = ((qcint*)ed) - prog->entitydata;
1219 OPC->_int += OPB->_int;
1224 case INSTR_STORE_ENT:
1225 case INSTR_STORE_FLD:
1226 case INSTR_STORE_FNC:
1227 OPB->_int = OPA->_int;
1230 OPB->ivector[0] = OPA->ivector[0];
1231 OPB->ivector[1] = OPA->ivector[1];
1232 OPB->ivector[2] = OPA->ivector[2];
1235 case INSTR_STOREP_F:
1236 case INSTR_STOREP_S:
1237 case INSTR_STOREP_ENT:
1238 case INSTR_STOREP_FLD:
1239 case INSTR_STOREP_FNC:
1240 if (OPB->_int < 0 || OPB->_int >= prog->entitydata_count) {
1241 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1244 if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
1245 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1247 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1249 ptr = (qcany*)(prog->entitydata + OPB->_int);
1250 ptr->_int = OPA->_int;
1252 case INSTR_STOREP_V:
1253 if (OPB->_int < 0 || OPB->_int + 2 >= prog->entitydata_count) {
1254 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1257 if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
1258 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1260 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1262 ptr = (qcany*)(prog->entitydata + OPB->_int);
1263 ptr->ivector[0] = OPA->ivector[0];
1264 ptr->ivector[1] = OPA->ivector[1];
1265 ptr->ivector[2] = OPA->ivector[2];
1269 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1272 OPC->_float = !OPA->vector[0] &&
1277 OPC->_float = !OPA->string ||
1278 !*prog_getstring(prog, OPA->string);
1281 OPC->_float = (OPA->edict == 0);
1284 OPC->_float = !OPA->function;
1288 /* this is consistent with darkplaces' behaviour */
1289 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1291 st += st->o2.s1 - 1; /* offset the s++ */
1292 if (++jumpcount >= maxjumps)
1293 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1297 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1299 st += st->o2.s1 - 1; /* offset the s++ */
1300 if (++jumpcount >= maxjumps)
1301 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1314 prog->argc = st->opcode - INSTR_CALL0;
1316 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1318 if(!OPA->function || OPA->function >= (unsigned int)prog->functions_count)
1320 qcvmerror(prog, "CALL outside the program in `%s` (%i)", prog->filename, (int)OPA->function);
1324 newf = &prog->functions[OPA->function];
1327 prog->statement = (st - prog->code) + 1;
1329 if (newf->entry < 0)
1331 /* negative statements are built in functions */
1332 int builtinnumber = -newf->entry;
1333 if (builtinnumber < prog->builtins_count && prog->builtins[builtinnumber])
1334 prog->builtins[builtinnumber](prog);
1336 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1337 builtinnumber, prog->filename);
1340 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1346 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1350 st += st->o1.s1 - 1; /* offset the s++ */
1351 if (++jumpcount == 10000000)
1352 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1356 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1357 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1360 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1361 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1365 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1368 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1375 #endif /* !QCVM_LOOP */