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
33 (prog_section_statement, code)
34 (prog_section_def, defs)
35 (prog_section_def, fields)
36 (prog_section_function, functions)
42 (qc_exec_stack, stack)
44 (prog_builtin, builtins)
45 (const char*, function_stack)
48 static void loaderror(const char *fmt, ...)
55 printf(": %s\n", strerror(err));
58 static void qcvmerror(qc_program *prog, const char *fmt, ...)
70 qc_program* prog_load(const char *filename)
76 file = util_fopen(filename, "rb");
80 if (fread(&header, sizeof(header), 1, file) != 1) {
81 loaderror("failed to read header from '%s'", filename);
86 if (header.version != 6) {
87 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
92 prog = (qc_program*)mem_a(sizeof(qc_program));
95 printf("failed to allocate program data\n");
98 memset(prog, 0, sizeof(*prog));
100 prog->entityfields = header.entfield;
101 prog->crc16 = header.crc16;
103 prog->filename = util_strdup(filename);
104 if (!prog->filename) {
105 loaderror("failed to store program name");
109 #define read_data(hdrvar, progvar, reserved) \
110 if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
111 loaderror("seek failed"); \
114 if (fread(vec_add(prog->progvar, header.hdrvar.length + reserved), \
115 sizeof(*prog->progvar), \
116 header.hdrvar.length, file) \
117 != header.hdrvar.length) \
119 loaderror("read failed"); \
122 #define read_data1(x) read_data(x, x, 0)
123 #define read_data2(x, y) read_data(x, x, y)
125 read_data (statements, code, 0);
128 read_data1(functions);
130 read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
134 /* profile counters */
135 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
137 /* Add tempstring area */
138 prog->tempstring_start = vec_size(prog->strings);
139 prog->tempstring_at = vec_size(prog->strings);
140 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
142 /* spawn the world entity */
143 vec_push(prog->entitypool, true);
144 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
151 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);
164 void prog_delete(qc_program *prog)
166 if (prog->filename) mem_d(prog->filename);
167 vec_free(prog->code);
168 vec_free(prog->defs);
169 vec_free(prog->fields);
170 vec_free(prog->functions);
171 vec_free(prog->strings);
172 vec_free(prog->globals);
173 vec_free(prog->entitydata);
174 vec_free(prog->entitypool);
175 vec_free(prog->localstack);
176 vec_free(prog->stack);
177 vec_free(prog->profile);
181 /***********************************************************************
185 char* prog_getstring(qc_program *prog, qcint str)
187 if (str < 0 || str >= (qcint)vec_size(prog->strings))
188 return "<<<invalid string>>>";
189 return prog->strings + str;
192 prog_section_def* prog_entfield(qc_program *prog, qcint off)
195 for (i = 0; i < vec_size(prog->fields); ++i) {
196 if (prog->fields[i].offset == off)
197 return (prog->fields + i);
202 prog_section_def* prog_getdef(qc_program *prog, qcint off)
205 for (i = 0; i < vec_size(prog->defs); ++i) {
206 if (prog->defs[i].offset == off)
207 return (prog->defs + i);
212 qcany* prog_getedict(qc_program *prog, qcint e)
214 if (e >= (qcint)vec_size(prog->entitypool)) {
216 printf("Accessing out of bounds edict %i\n", (int)e);
219 return (qcany*)(prog->entitydata + (prog->entityfields * e));
222 qcint prog_spawn_entity(qc_program *prog)
226 for (e = 0; e < (qcint)vec_size(prog->entitypool); ++e) {
227 if (!prog->entitypool[e]) {
228 data = (char*)(prog->entitydata + (prog->entityfields * e));
229 memset(data, 0, prog->entityfields * sizeof(qcint));
233 vec_push(prog->entitypool, true);
235 data = (char*)vec_add(prog->entitydata, prog->entityfields);
236 memset(data, 0, prog->entityfields * sizeof(qcint));
240 void prog_free_entity(qc_program *prog, qcint e)
244 printf("Trying to free world entity\n");
247 if (e >= (qcint)vec_size(prog->entitypool)) {
249 printf("Trying to free out of bounds entity\n");
252 if (!prog->entitypool[e]) {
254 printf("Double free on entity\n");
257 prog->entitypool[e] = false;
260 qcint prog_tempstring(qc_program *prog, const char *_str)
262 /* we don't access it, but the macro-generated functions don't use
265 char *str = (char*)_str;
267 size_t len = strlen(str);
268 size_t at = prog->tempstring_at;
270 /* when we reach the end we start over */
271 if (at + len >= vec_size(prog->strings))
272 at = prog->tempstring_start;
274 /* when it doesn't fit, reallocate */
275 if (at + len >= vec_size(prog->strings))
277 (void)vec_add(prog->strings, len+1);
278 memcpy(prog->strings + at, str, len+1);
282 /* when it fits, just copy */
283 memcpy(prog->strings + at, str, len+1);
284 prog->tempstring_at += len+1;
288 static size_t print_escaped_string(const char *str, size_t maxlen)
292 --maxlen; /* because we're lazy and have escape sequences */
302 case '\a': len += 2; putchar('\\'); putchar('a'); break;
303 case '\b': len += 2; putchar('\\'); putchar('b'); break;
304 case '\r': len += 2; putchar('\\'); putchar('r'); break;
305 case '\n': len += 2; putchar('\\'); putchar('n'); break;
306 case '\t': len += 2; putchar('\\'); putchar('t'); break;
307 case '\f': len += 2; putchar('\\'); putchar('f'); break;
308 case '\v': len += 2; putchar('\\'); putchar('v'); break;
309 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
310 case '"': len += 2; putchar('\\'); putchar('"'); break;
322 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
324 static char spaces[28+1] = " ";
325 prog_section_def *def;
330 len = printf("<null>,");
334 def = prog_getdef(prog, glob);
335 value = (qcany*)(&prog->globals[glob]);
337 len = printf("[@%u] ", glob);
339 const char *name = prog_getstring(prog, def->name);
343 len += printf("%s ", name);
344 vtype = def->type & DEF_TYPEMASK;
353 len += printf("(%i),", value->_int);
356 len += printf("'%g %g %g',", value->vector[0],
361 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
363 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
367 len += printf("%g,", value->_float);
371 if (len < (int)sizeof(spaces)-1) {
372 spaces[sizeof(spaces)-1-len] = 0;
374 spaces[sizeof(spaces)-1-len] = ' ';
378 static void prog_print_statement(qc_program *prog, prog_section_statement *st)
380 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
381 printf("<illegal instruction %d>\n", st->opcode);
384 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
386 for (i = 0; i < vec_size(prog->function_stack); ++i)
388 printf("%s:", vec_last(prog->function_stack));
390 printf(" <> %-12s", asm_instr[st->opcode].m);
391 if (st->opcode >= INSTR_IF &&
392 st->opcode <= INSTR_IFNOT)
394 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
395 printf("%d\n", st->o2.s1);
397 else if (st->opcode >= INSTR_CALL0 &&
398 st->opcode <= INSTR_CALL8)
400 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
403 else if (st->opcode == INSTR_GOTO)
405 printf("%i\n", st->o1.s1);
409 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
413 t[1] = t[2] = TYPE_VECTOR;
416 t[0] = t[2] = TYPE_VECTOR;
419 t[0] = t[1] = TYPE_VECTOR;
425 t[0] = t[1] = t[2] = TYPE_VECTOR;
429 t[0] = t[1] = TYPE_STRING;
436 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
439 t[0] = t[1] = TYPE_STRING; t[2] = -1;
441 case INSTR_STORE_ENT:
442 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
444 case INSTR_STORE_FLD:
445 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
447 case INSTR_STORE_FNC:
448 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
451 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
454 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
456 case INSTR_STOREP_ENT:
457 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
459 case INSTR_STOREP_FLD:
460 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
462 case INSTR_STOREP_FNC:
463 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
466 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
467 else printf("(none), ");
468 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
469 else printf("(none), ");
470 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
471 else printf("(none)");
477 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
484 st.localsp = vec_size(prog->localstack);
485 st.stmt = prog->statement;
488 if (prog->xflags & VMXF_TRACE) {
489 vec_push(prog->function_stack, prog_getstring(prog, func->name));
492 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
493 if (vec_size(prog->stack))
495 prog_section_function *cur;
496 cur = prog->stack[vec_size(prog->stack)-1].function;
499 qcint *globals = prog->globals + cur->firstlocal;
500 vec_append(prog->localstack, cur->locals, globals);
505 qcint *globals = prog->globals + func->firstlocal;
506 vec_append(prog->localstack, func->locals, globals);
510 /* copy parameters */
511 parampos = func->firstlocal;
512 for (p = 0; p < func->nargs; ++p)
515 for (s = 0; s < func->argsize[p]; ++s) {
516 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
521 vec_push(prog->stack, st);
526 static qcint prog_leavefunction(qc_program *prog)
528 prog_section_function *prev = NULL;
531 qc_exec_stack st = vec_last(prog->stack);
533 if (prog->xflags & VMXF_TRACE) {
534 if (vec_size(prog->function_stack))
535 vec_pop(prog->function_stack);
538 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
539 if (vec_size(prog->stack) > 1) {
540 prev = prog->stack[vec_size(prog->stack)-2].function;
541 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
544 prev = prog->stack[vec_size(prog->stack)-1].function;
545 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
548 qcint *globals = prog->globals + prev->firstlocal;
549 memcpy(globals, prog->localstack + oldsp, prev->locals);
550 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
551 vec_shrinkto(prog->localstack, oldsp);
554 vec_pop(prog->stack);
556 return st.stmt - 1; /* offset the ++st */
559 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
562 size_t oldxflags = prog->xflags;
563 prog_section_statement *st;
566 prog->xflags = flags;
568 st = prog->code + prog_enterfunction(prog, func);
576 #define QCVM_PROFILE 0
583 #define QCVM_PROFILE 0
590 #define QCVM_PROFILE 1
595 case (VMXF_TRACE|VMXF_PROFILE):
597 #define QCVM_PROFILE 1
605 prog->xflags = oldxflags;
606 vec_free(prog->localstack);
607 vec_free(prog->stack);
613 /***********************************************************************
614 * main for when building the standalone executor
617 #if defined(QCVM_EXECUTOR)
620 const char *type_name[TYPE_COUNT] = {
635 bool opts_debug = false;
636 bool opts_memchk = false;
643 qcvm_parameter *main_params = NULL;
645 #define CheckArgs(num) do { \
646 if (prog->argc != (num)) { \
648 printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
649 __FUNCTION__, prog->argc, (num)); \
654 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
655 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
656 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
658 static int qc_print(qc_program *prog)
661 const char *laststr = NULL;
662 for (i = 0; i < (size_t)prog->argc; ++i) {
663 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
664 printf("%s", (laststr = prog_getstring(prog, str->string)));
666 if (laststr && (prog->xflags & VMXF_TRACE)) {
667 size_t len = strlen(laststr);
668 if (!len || laststr[len-1] != '\n')
674 static int qc_error(qc_program *prog)
676 printf("*** VM raised an error:\n");
682 static int qc_ftos(qc_program *prog)
689 snprintf(buffer, sizeof(buffer), "%g", num->_float);
690 str.string = prog_tempstring(prog, buffer);
695 static int qc_stof(qc_program *prog)
701 num._float = strtof(prog_getstring(prog, str->string), NULL);
706 static int qc_vtos(qc_program *prog)
713 snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
714 str.string = prog_tempstring(prog, buffer);
719 static int qc_etos(qc_program *prog)
726 snprintf(buffer, sizeof(buffer), "%i", num->_int);
727 str.string = prog_tempstring(prog, buffer);
732 static int qc_spawn(qc_program *prog)
736 ent.edict = prog_spawn_entity(prog);
738 return (ent.edict ? 0 : -1);
741 static int qc_kill(qc_program *prog)
746 prog_free_entity(prog, ent->edict);
750 static int qc_vlen(qc_program *prog)
755 len._float = sqrt(vec->vector[0] * vec->vector[0] +
756 vec->vector[1] * vec->vector[1] +
757 vec->vector[2] * vec->vector[2]);
762 static prog_builtin qc_builtins[] = {
774 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
776 static const char *arg0 = NULL;
780 printf("usage: [-debug] %s file\n", arg0);
784 static void prog_main_setparams(qc_program *prog)
789 for (i = 0; i < vec_size(main_params); ++i) {
790 arg = GetGlobal(OFS_PARM0 + 3*i);
794 switch (main_params[i].vtype) {
797 (void)sscanf_s(main_params[i].value, " %f %f %f ",
802 (void)sscanf(main_params[i].value, " %f %f %f ",
809 arg->_float = atof(main_params[i].value);
812 arg->string = prog_tempstring(prog, main_params[i].value);
815 printf("error: unhandled parameter type: %i\n", main_params[i].vtype);
821 void prog_disasm_function(qc_program *prog, size_t id);
822 int main(int argc, char **argv)
827 size_t xflags = VMXF_DEFAULT;
828 bool opts_printfields = false;
829 bool opts_printdefs = false;
830 bool opts_printfuns = false;
831 bool opts_disasm = false;
832 bool opts_info = false;
840 if (!strcmp(argv[1], "-trace")) {
843 xflags |= VMXF_TRACE;
845 else if (!strcmp(argv[1], "-profile")) {
848 xflags |= VMXF_PROFILE;
850 else if (!strcmp(argv[1], "-info")) {
855 else if (!strcmp(argv[1], "-disasm")) {
860 else if (!strcmp(argv[1], "-printdefs")) {
863 opts_printdefs = true;
865 else if (!strcmp(argv[1], "-printfuns")) {
868 opts_printfuns = true;
870 else if (!strcmp(argv[1], "-printfields")) {
873 opts_printfields = true;
875 else if (!strcmp(argv[1], "-vector") ||
876 !strcmp(argv[1], "-string") ||
877 !strcmp(argv[1], "-float") )
880 if (argv[1][1] == 'f')
881 p.vtype = TYPE_FLOAT;
882 else if (argv[1][1] == 's')
883 p.vtype = TYPE_STRING;
884 else if (argv[1][1] == 'v')
885 p.vtype = TYPE_VECTOR;
893 vec_push(main_params, p);
902 prog = prog_load(argv[1]);
904 printf("failed to load program '%s'\n", argv[1]);
908 prog->builtins = qc_builtins;
909 prog->builtins_count = qc_builtins_count;
912 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
913 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
914 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
917 for (i = 1; i < vec_size(prog->functions); ++i) {
918 const char *name = prog_getstring(prog, prog->functions[i].name);
919 /* printf("Found function: %s\n", name); */
920 if (!strcmp(name, "main"))
928 for (i = 1; i < vec_size(prog->functions); ++i)
929 prog_disasm_function(prog, i);
932 if (opts_printdefs) {
933 for (i = 0; i < vec_size(prog->defs); ++i) {
934 printf("Global: %8s %-16s at %u\n",
935 type_name[prog->defs[i].type & DEF_TYPEMASK],
936 prog_getstring(prog, prog->defs[i].name),
937 (unsigned int)prog->defs[i].offset);
940 else if (opts_printfields) {
941 for (i = 0; i < vec_size(prog->fields); ++i) {
942 printf("Field: %8s %-16s at %u\n",
943 type_name[prog->fields[i].type],
944 prog_getstring(prog, prog->fields[i].name),
945 (unsigned int)prog->fields[i].offset);
948 else if (opts_printfuns) {
949 for (i = 0; i < vec_size(prog->functions); ++i) {
951 printf("Function: %-16s taking %i parameters:(",
952 prog_getstring(prog, prog->functions[i].name),
953 (unsigned int)prog->functions[i].nargs);
954 for (a = 0; a < prog->functions[i].nargs; ++a) {
955 printf(" %i", prog->functions[i].argsize[a]);
957 printf(") locals: %i + %i\n",
958 prog->functions[i].firstlocal,
959 prog->functions[i].locals);
966 prog_main_setparams(prog);
967 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
970 printf("No main function found\n");
977 void prog_disasm_function(qc_program *prog, size_t id)
979 prog_section_function *fdef = prog->functions + id;
980 prog_section_statement *st;
982 if (fdef->entry < 0) {
983 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
987 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
989 st = prog->code + fdef->entry;
990 while (st->opcode != AINSTR_END) {
991 prog_print_statement(prog, st);
996 #else /* !QCVM_LOOP */
998 * Everything from here on is not including into the compilation of the
999 * executor. This is simply code that is #included via #include __FILE__
1000 * see when QCVM_LOOP is defined, the rest of the code above do not get
1001 * re-included. So this really just acts like one large macro, but it
1002 * sort of isn't, which makes it nicer looking.
1005 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1006 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1007 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1009 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1011 /* to be consistent with current darkplaces behaviour */
1012 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1013 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1017 prog_section_function *newf;
1024 prog->profile[st - prog->code]++;
1028 prog_print_statement(prog, st);
1034 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1039 /* TODO: add instruction count to function profile count */
1040 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1041 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1042 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1044 st = prog->code + prog_leavefunction(prog);
1045 if (!vec_size(prog->stack))
1051 OPC->_float = OPA->_float * OPB->_float;
1054 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1055 OPA->vector[1]*OPB->vector[1] +
1056 OPA->vector[2]*OPB->vector[2];
1059 OPC->vector[0] = OPA->_float * OPB->vector[0];
1060 OPC->vector[1] = OPA->_float * OPB->vector[1];
1061 OPC->vector[2] = OPA->_float * OPB->vector[2];
1064 OPC->vector[0] = OPB->_float * OPA->vector[0];
1065 OPC->vector[1] = OPB->_float * OPA->vector[1];
1066 OPC->vector[2] = OPB->_float * OPA->vector[2];
1069 if (OPB->_float != 0.0f)
1070 OPC->_float = OPA->_float / OPB->_float;
1076 OPC->_float = OPA->_float + OPB->_float;
1079 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1080 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1081 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
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];
1093 OPC->_float = (OPA->_float == OPB->_float);
1096 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1097 (OPA->vector[1] == OPB->vector[1]) &&
1098 (OPA->vector[2] == OPB->vector[2]) );
1101 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1102 prog_getstring(prog, OPB->string));
1105 OPC->_float = (OPA->_int == OPB->_int);
1108 OPC->_float = (OPA->function == OPB->function);
1111 OPC->_float = (OPA->_float != OPB->_float);
1114 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1115 (OPA->vector[1] != OPB->vector[1]) ||
1116 (OPA->vector[2] != OPB->vector[2]) );
1119 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1120 prog_getstring(prog, OPB->string));
1123 OPC->_float = (OPA->_int != OPB->_int);
1126 OPC->_float = (OPA->function != OPB->function);
1130 OPC->_float = (OPA->_float <= OPB->_float);
1133 OPC->_float = (OPA->_float >= OPB->_float);
1136 OPC->_float = (OPA->_float < OPB->_float);
1139 OPC->_float = (OPA->_float > OPB->_float);
1144 case INSTR_LOAD_FLD:
1145 case INSTR_LOAD_ENT:
1146 case INSTR_LOAD_FNC:
1147 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1148 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1151 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1152 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1157 ed = prog_getedict(prog, OPA->edict);
1158 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1161 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1162 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1165 if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1167 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1172 ed = prog_getedict(prog, OPA->edict);
1173 OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
1174 OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
1175 OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
1179 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1180 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1183 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1185 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1191 ed = prog_getedict(prog, OPA->edict);
1192 OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1197 case INSTR_STORE_ENT:
1198 case INSTR_STORE_FLD:
1199 case INSTR_STORE_FNC:
1200 OPB->_int = OPA->_int;
1203 OPB->ivector[0] = OPA->ivector[0];
1204 OPB->ivector[1] = OPA->ivector[1];
1205 OPB->ivector[2] = OPA->ivector[2];
1208 case INSTR_STOREP_F:
1209 case INSTR_STOREP_S:
1210 case INSTR_STOREP_ENT:
1211 case INSTR_STOREP_FLD:
1212 case INSTR_STOREP_FNC:
1213 if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1214 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1217 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1218 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1220 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1222 ptr = (qcany*)(prog->entitydata + OPB->_int);
1223 ptr->_int = OPA->_int;
1225 case INSTR_STOREP_V:
1226 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1227 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1230 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1231 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1233 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1235 ptr = (qcany*)(prog->entitydata + OPB->_int);
1236 ptr->ivector[0] = OPA->ivector[0];
1237 ptr->ivector[1] = OPA->ivector[1];
1238 ptr->ivector[2] = OPA->ivector[2];
1242 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1245 OPC->_float = !OPA->vector[0] &&
1250 OPC->_float = !OPA->string ||
1251 !*prog_getstring(prog, OPA->string);
1254 OPC->_float = (OPA->edict == 0);
1257 OPC->_float = !OPA->function;
1261 /* this is consistent with darkplaces' behaviour */
1262 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1264 st += st->o2.s1 - 1; /* offset the s++ */
1265 if (++jumpcount >= maxjumps)
1266 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1270 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1272 st += st->o2.s1 - 1; /* offset the s++ */
1273 if (++jumpcount >= maxjumps)
1274 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1287 prog->argc = st->opcode - INSTR_CALL0;
1289 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1291 if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1293 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1297 newf = &prog->functions[OPA->function];
1300 prog->statement = (st - prog->code) + 1;
1302 if (newf->entry < 0)
1304 /* negative statements are built in functions */
1305 qcint builtinnumber = -newf->entry;
1306 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1307 prog->builtins[builtinnumber](prog);
1309 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1310 builtinnumber, prog->filename);
1313 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1319 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1323 st += st->o1.s1 - 1; /* offset the s++ */
1324 if (++jumpcount == 10000000)
1325 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1329 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1330 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1333 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1334 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1338 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1341 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1348 #endif /* !QCVM_LOOP */