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);
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)
432 else if (st->opcode == INSTR_GOTO)
434 printf("%i\n", st->o1.s1);
438 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
442 t[1] = t[2] = TYPE_VECTOR;
445 t[0] = t[2] = TYPE_VECTOR;
448 t[0] = t[1] = TYPE_VECTOR;
454 t[0] = t[1] = t[2] = TYPE_VECTOR;
458 t[0] = t[1] = TYPE_STRING;
465 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
468 t[0] = t[1] = TYPE_STRING; t[2] = -1;
470 case INSTR_STORE_ENT:
471 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
473 case INSTR_STORE_FLD:
474 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
476 case INSTR_STORE_FNC:
477 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
480 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
483 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
485 case INSTR_STOREP_ENT:
486 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
488 case INSTR_STOREP_FLD:
489 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
491 case INSTR_STOREP_FNC:
492 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
495 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
496 else printf("(none), ");
497 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
498 else printf("(none), ");
499 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
500 else printf("(none)");
506 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
512 st.localsp = prog->localstack_count;
513 st.stmt = prog->statement;
516 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
517 if (prog->stack_count)
519 prog_section_function *cur;
520 cur = prog->stack[prog->stack_count-1].function;
523 qcint *globals = prog->globals + cur->firstlocal;
524 if (!qc_program_localstack_append(prog, globals, cur->locals))
526 printf("out of memory\n");
533 qcint *globals = prog->globals + func->firstlocal;
534 if (!qc_program_localstack_append(prog, globals, func->locals))
536 printf("out of memory\n");
542 /* copy parameters */
543 parampos = func->firstlocal;
544 for (p = 0; p < func->nargs; ++p)
547 for (s = 0; s < func->argsize[p]; ++s) {
548 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
553 if (!qc_program_stack_add(prog, st)) {
554 printf("out of memory\n");
561 static qcint prog_leavefunction(qc_program *prog)
563 prog_section_function *prev = NULL;
566 qc_exec_stack st = prog->stack[prog->stack_count-1];
568 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
569 if (prog->stack_count > 1) {
570 prev = prog->stack[prog->stack_count-2].function;
571 oldsp = prog->stack[prog->stack_count-2].localsp;
574 prev = prog->stack[prog->stack_count-1].function;
575 oldsp = prog->stack[prog->stack_count-1].localsp;
578 qcint *globals = prog->globals + prev->firstlocal;
579 memcpy(globals, prog->localstack + oldsp, prev->locals);
580 if (!qc_program_localstack_resize(prog, oldsp)) {
581 printf("out of memory\n");
586 if (!qc_program_stack_remove(prog, prog->stack_count-1)) {
587 printf("out of memory\n");
591 return st.stmt - 1; /* offset the ++st */
594 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
597 size_t oldxflags = prog->xflags;
598 prog_section_statement *st;
601 prog->xflags = flags;
603 st = prog->code + prog_enterfunction(prog, func);
611 #define QCVM_PROFILE 0
618 #define QCVM_PROFILE 0
625 #define QCVM_PROFILE 1
630 case (VMXF_TRACE|VMXF_PROFILE):
632 #define QCVM_PROFILE 1
640 prog->xflags = oldxflags;
641 prog->localstack_count = 0;
642 prog->stack_count = 0;
648 /***********************************************************************
649 * main for when building the standalone executor
652 #if defined(QCVM_EXECUTOR)
655 const char *type_name[TYPE_COUNT] = {
670 bool opts_debug = false;
671 bool opts_memchk = false;
678 VECTOR_MAKE(qcvm_parameter, main_params);
680 #define CheckArgs(num) do { \
681 if (prog->argc != (num)) { \
683 printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
684 __FUNCTION__, prog->argc, (num)); \
689 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
690 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
691 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
693 static int qc_print(qc_program *prog)
696 const char *laststr = NULL;
697 for (i = 0; i < prog->argc; ++i) {
698 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
699 printf("%s", (laststr = prog_getstring(prog, str->string)));
701 if (laststr && (prog->xflags & VMXF_TRACE)) {
702 size_t len = strlen(laststr);
703 if (!len || laststr[len-1] != '\n')
709 static int qc_error(qc_program *prog)
711 printf("*** VM raised an error:\n");
717 static int qc_ftos(qc_program *prog)
724 snprintf(buffer, sizeof(buffer), "%g", num->_float);
725 str.string = prog_tempstring(prog, buffer);
730 static int qc_vtos(qc_program *prog)
737 snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
738 str.string = prog_tempstring(prog, buffer);
743 static int qc_etos(qc_program *prog)
750 snprintf(buffer, sizeof(buffer), "%i", num->_int);
751 str.string = prog_tempstring(prog, buffer);
756 static int qc_spawn(qc_program *prog)
760 ent.edict = prog_spawn_entity(prog);
762 return (ent.edict ? 0 : -1);
765 static int qc_kill(qc_program *prog)
770 prog_free_entity(prog, ent->edict);
774 static int qc_vlen(qc_program *prog)
779 len._float = sqrt(vec->vector[0] * vec->vector[0] +
780 vec->vector[1] * vec->vector[1] +
781 vec->vector[2] * vec->vector[2]);
786 static prog_builtin qc_builtins[] = {
797 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
799 static const char *arg0 = NULL;
803 printf("usage: [-debug] %s file\n", arg0);
807 static void prog_main_setparams(qc_program *prog)
812 for (i = 0; i < main_params_elements; ++i) {
813 arg = GetGlobal(OFS_PARM0 + 3*i);
817 switch (main_params_data[i].vtype) {
820 (void)sscanf_s(main_params_data[i].value, " %f %f %f ",
825 (void)sscanf(main_params_data[i].value, " %f %f %f ",
832 arg->_float = atof(main_params_data[i].value);
835 arg->string = prog_tempstring(prog, main_params_data[i].value);
838 printf("error: unhandled parameter type: %i\n", main_params_data[i].vtype);
844 void prog_disasm_function(qc_program *prog, size_t id);
845 int main(int argc, char **argv)
850 size_t xflags = VMXF_DEFAULT;
851 bool opts_printfields = false;
852 bool opts_printdefs = false;
853 bool opts_disasm = false;
854 bool opts_info = false;
862 if (!strcmp(argv[1], "-trace")) {
865 xflags |= VMXF_TRACE;
867 else if (!strcmp(argv[1], "-profile")) {
870 xflags |= VMXF_PROFILE;
872 else if (!strcmp(argv[1], "-info")) {
877 else if (!strcmp(argv[1], "-disasm")) {
882 else if (!strcmp(argv[1], "-printdefs")) {
885 opts_printdefs = true;
887 else if (!strcmp(argv[1], "-printfields")) {
890 opts_printfields = true;
892 else if (!strcmp(argv[1], "-vector") ||
893 !strcmp(argv[1], "-string") ||
894 !strcmp(argv[1], "-float") )
897 if (argv[1][1] == 'f')
898 p.vtype = TYPE_FLOAT;
899 else if (argv[1][1] == 's')
900 p.vtype = TYPE_STRING;
901 else if (argv[1][1] == 'v')
902 p.vtype = TYPE_VECTOR;
910 if (main_params_add(p) < 0) {
911 if (main_params_data)
912 mem_d(main_params_data);
913 printf("cannot add parameter\n");
924 prog = prog_load(argv[1]);
926 printf("failed to load program '%s'\n", argv[1]);
930 prog->builtins = qc_builtins;
931 prog->builtins_count = qc_builtins_count;
932 prog->builtins_alloc = 0;
935 printf("Program's system-checksum = 0x%04x\n", (int)prog->crc16);
936 printf("Entity field space: %i\n", (int)prog->entityfields);
939 for (i = 1; i < prog->functions_count; ++i) {
940 const char *name = prog_getstring(prog, prog->functions[i].name);
941 /* printf("Found function: %s\n", name); */
942 if (!strcmp(name, "main"))
950 for (i = 1; i < prog->functions_count; ++i)
951 prog_disasm_function(prog, i);
954 if (opts_printdefs) {
955 for (i = 0; i < prog->defs_count; ++i) {
956 printf("Global: %8s %-16s at %u\n",
957 type_name[prog->defs[i].type & DEF_TYPEMASK],
958 prog_getstring(prog, prog->defs[i].name),
959 (unsigned int)prog->defs[i].offset);
962 else if (opts_printfields) {
963 for (i = 0; i < prog->fields_count; ++i) {
964 printf("Field: %8s %-16s at %u\n",
965 type_name[prog->fields[i].type],
966 prog_getstring(prog, prog->fields[i].name),
967 (unsigned int)prog->fields[i].offset);
974 prog_main_setparams(prog);
975 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
978 printf("No main function found\n");
985 void prog_disasm_function(qc_program *prog, size_t id)
987 prog_section_function *fdef = prog->functions + id;
988 prog_section_statement *st;
990 if (fdef->entry < 0) {
991 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
995 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
997 st = prog->code + fdef->entry;
998 while (st->opcode != AINSTR_END) {
999 prog_print_statement(prog, st);
1004 #else /* !QCVM_LOOP */
1006 * Everything from here on is not including into the compilation of the
1007 * executor. This is simply code that is #included via #include __FILE__
1008 * see when QCVM_LOOP is defined, the rest of the code above do not get
1009 * re-included. So this really just acts like one large macro, but it
1010 * sort of isn't, which makes it nicer looking.
1013 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1014 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1015 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1017 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1019 /* to be consistent with current darkplaces behaviour */
1020 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1021 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1025 prog_section_function *newf;
1032 prog->profile[st - prog->code]++;
1036 prog_print_statement(prog, st);
1042 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1047 /* TODO: add instruction count to function profile count */
1048 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1049 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1050 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1052 st = prog->code + prog_leavefunction(prog);
1053 if (!prog->stack_count)
1059 OPC->_float = OPA->_float * OPB->_float;
1062 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1063 OPA->vector[1]*OPB->vector[1] +
1064 OPA->vector[2]*OPB->vector[2];
1067 OPC->vector[0] = OPA->_float * OPB->vector[0];
1068 OPC->vector[1] = OPA->_float * OPB->vector[1];
1069 OPC->vector[2] = OPA->_float * OPB->vector[2];
1072 OPC->vector[0] = OPB->_float * OPA->vector[0];
1073 OPC->vector[1] = OPB->_float * OPA->vector[1];
1074 OPC->vector[2] = OPB->_float * OPA->vector[2];
1077 if (OPB->_float != 0.0f)
1078 OPC->_float = OPA->_float / OPB->_float;
1084 OPC->_float = OPA->_float + OPB->_float;
1087 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1088 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1089 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1092 OPC->_float = OPA->_float - OPB->_float;
1095 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1096 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1097 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1101 OPC->_float = (OPA->_float == OPB->_float);
1104 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1105 (OPA->vector[1] == OPB->vector[1]) &&
1106 (OPA->vector[2] == OPB->vector[2]) );
1109 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1110 prog_getstring(prog, OPB->string));
1113 OPC->_float = (OPA->_int == OPB->_int);
1116 OPC->_float = (OPA->function == OPB->function);
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);
1138 OPC->_float = (OPA->_float <= OPB->_float);
1141 OPC->_float = (OPA->_float >= OPB->_float);
1144 OPC->_float = (OPA->_float < OPB->_float);
1147 OPC->_float = (OPA->_float > OPB->_float);
1152 case INSTR_LOAD_FLD:
1153 case INSTR_LOAD_ENT:
1154 case INSTR_LOAD_FNC:
1155 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1156 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1159 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1160 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1165 ed = prog_getedict(prog, OPA->edict);
1166 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1169 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1170 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1173 if (OPB->_int < 0 || OPB->_int + 3 > prog->entityfields)
1175 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1180 ed = prog_getedict(prog, OPA->edict);
1181 OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
1182 OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
1183 OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
1187 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1188 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1191 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1193 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1199 ed = prog_getedict(prog, OPA->edict);
1200 OPC->_int = ((qcint*)ed) - prog->entitydata;
1201 OPC->_int += OPB->_int;
1206 case INSTR_STORE_ENT:
1207 case INSTR_STORE_FLD:
1208 case INSTR_STORE_FNC:
1209 OPB->_int = OPA->_int;
1212 OPB->ivector[0] = OPA->ivector[0];
1213 OPB->ivector[1] = OPA->ivector[1];
1214 OPB->ivector[2] = OPA->ivector[2];
1217 case INSTR_STOREP_F:
1218 case INSTR_STOREP_S:
1219 case INSTR_STOREP_ENT:
1220 case INSTR_STOREP_FLD:
1221 case INSTR_STOREP_FNC:
1222 if (OPB->_int < 0 || OPB->_int >= prog->entitydata_count) {
1223 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1226 if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
1227 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1229 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1231 ptr = (qcany*)(prog->entitydata + OPB->_int);
1232 ptr->_int = OPA->_int;
1234 case INSTR_STOREP_V:
1235 if (OPB->_int < 0 || OPB->_int + 2 >= prog->entitydata_count) {
1236 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1239 if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
1240 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1242 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1244 ptr = (qcany*)(prog->entitydata + OPB->_int);
1245 ptr->ivector[0] = OPA->ivector[0];
1246 ptr->ivector[1] = OPA->ivector[1];
1247 ptr->ivector[2] = OPA->ivector[2];
1251 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1254 OPC->_float = !OPA->vector[0] &&
1259 OPC->_float = !OPA->string ||
1260 !*prog_getstring(prog, OPA->string);
1263 OPC->_float = (OPA->edict == 0);
1266 OPC->_float = !OPA->function;
1270 /* this is consistent with darkplaces' behaviour */
1271 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1273 st += st->o2.s1 - 1; /* offset the s++ */
1274 if (++jumpcount >= maxjumps)
1275 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1279 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1281 st += st->o2.s1 - 1; /* offset the s++ */
1282 if (++jumpcount >= maxjumps)
1283 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1296 prog->argc = st->opcode - INSTR_CALL0;
1298 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1300 if(!OPA->function || OPA->function >= (unsigned int)prog->functions_count)
1302 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1306 newf = &prog->functions[OPA->function];
1309 prog->statement = (st - prog->code) + 1;
1311 if (newf->entry < 0)
1313 /* negative statements are built in functions */
1314 int builtinnumber = -newf->entry;
1315 if (builtinnumber < prog->builtins_count && prog->builtins[builtinnumber])
1316 prog->builtins[builtinnumber](prog);
1318 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1319 builtinnumber, prog->filename);
1322 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1328 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1332 st += st->o1.s1 - 1; /* offset the s++ */
1333 if (++jumpcount == 10000000)
1334 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1338 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1339 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1342 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1343 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1347 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1350 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1357 #endif /* !QCVM_LOOP */