6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 static void loaderror(const char *fmt, ...)
39 printf(": %s\n", strerror(err));
42 static void qcvmerror(qc_program *prog, const char *fmt, ...)
54 qc_program* prog_load(const char *filename)
60 file = util_fopen(filename, "rb");
64 if (fread(&header, sizeof(header), 1, file) != 1) {
65 loaderror("failed to read header from '%s'", filename);
70 if (header.version != 6) {
71 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
76 prog = (qc_program*)mem_a(sizeof(qc_program));
79 printf("failed to allocate program data\n");
82 memset(prog, 0, sizeof(*prog));
84 prog->entityfields = header.entfield;
85 prog->crc16 = header.crc16;
87 prog->filename = util_strdup(filename);
88 if (!prog->filename) {
89 loaderror("failed to store program name");
93 #define read_data(hdrvar, progvar, reserved) \
94 if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
95 loaderror("seek failed"); \
98 if (fread(vec_add(prog->progvar, header.hdrvar.length + reserved), \
99 sizeof(*prog->progvar), \
100 header.hdrvar.length, file) \
101 != header.hdrvar.length) \
103 loaderror("read failed"); \
106 #define read_data1(x) read_data(x, x, 0)
107 #define read_data2(x, y) read_data(x, x, y)
109 read_data (statements, code, 0);
112 read_data1(functions);
114 read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
118 /* profile counters */
119 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
121 /* Add tempstring area */
122 prog->tempstring_start = vec_size(prog->strings);
123 prog->tempstring_at = vec_size(prog->strings);
124 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
126 /* spawn the world entity */
127 vec_push(prog->entitypool, true);
128 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
135 mem_d(prog->filename);
136 vec_free(prog->code);
137 vec_free(prog->defs);
138 vec_free(prog->fields);
139 vec_free(prog->functions);
140 vec_free(prog->strings);
141 vec_free(prog->globals);
142 vec_free(prog->entitydata);
143 vec_free(prog->entitypool);
148 void prog_delete(qc_program *prog)
150 if (prog->filename) mem_d(prog->filename);
151 vec_free(prog->code);
152 vec_free(prog->defs);
153 vec_free(prog->fields);
154 vec_free(prog->functions);
155 vec_free(prog->strings);
156 vec_free(prog->globals);
157 vec_free(prog->entitydata);
158 vec_free(prog->entitypool);
159 vec_free(prog->localstack);
160 vec_free(prog->stack);
161 vec_free(prog->profile);
165 /***********************************************************************
169 char* prog_getstring(qc_program *prog, qcint str)
171 if (str < 0 || str >= (qcint)vec_size(prog->strings))
172 return "<<<invalid string>>>";
173 return prog->strings + str;
176 prog_section_def* prog_entfield(qc_program *prog, qcint off)
179 for (i = 0; i < vec_size(prog->fields); ++i) {
180 if (prog->fields[i].offset == off)
181 return (prog->fields + i);
186 prog_section_def* prog_getdef(qc_program *prog, qcint off)
189 for (i = 0; i < vec_size(prog->defs); ++i) {
190 if (prog->defs[i].offset == off)
191 return (prog->defs + i);
196 qcany* prog_getedict(qc_program *prog, qcint e)
198 if (e >= (qcint)vec_size(prog->entitypool)) {
200 printf("Accessing out of bounds edict %i\n", (int)e);
203 return (qcany*)(prog->entitydata + (prog->entityfields * e));
206 qcint prog_spawn_entity(qc_program *prog)
210 for (e = 0; e < (qcint)vec_size(prog->entitypool); ++e) {
211 if (!prog->entitypool[e]) {
212 data = (char*)(prog->entitydata + (prog->entityfields * e));
213 memset(data, 0, prog->entityfields * sizeof(qcint));
217 vec_push(prog->entitypool, true);
219 data = (char*)vec_add(prog->entitydata, prog->entityfields);
220 memset(data, 0, prog->entityfields * sizeof(qcint));
224 void prog_free_entity(qc_program *prog, qcint e)
228 printf("Trying to free world entity\n");
231 if (e >= (qcint)vec_size(prog->entitypool)) {
233 printf("Trying to free out of bounds entity\n");
236 if (!prog->entitypool[e]) {
238 printf("Double free on entity\n");
241 prog->entitypool[e] = false;
244 qcint prog_tempstring(qc_program *prog, const char *_str)
246 /* we don't access it, but the macro-generated functions don't use
249 char *str = (char*)_str;
251 size_t len = strlen(str);
252 size_t at = prog->tempstring_at;
254 /* when we reach the end we start over */
255 if (at + len >= vec_size(prog->strings))
256 at = prog->tempstring_start;
258 /* when it doesn't fit, reallocate */
259 if (at + len >= vec_size(prog->strings))
261 (void)vec_add(prog->strings, len+1);
262 memcpy(prog->strings + at, str, len+1);
266 /* when it fits, just copy */
267 memcpy(prog->strings + at, str, len+1);
268 prog->tempstring_at += len+1;
272 static size_t print_escaped_string(const char *str, size_t maxlen)
276 --maxlen; /* because we're lazy and have escape sequences */
286 case '\a': len += 2; putchar('\\'); putchar('a'); break;
287 case '\b': len += 2; putchar('\\'); putchar('b'); break;
288 case '\r': len += 2; putchar('\\'); putchar('r'); break;
289 case '\n': len += 2; putchar('\\'); putchar('n'); break;
290 case '\t': len += 2; putchar('\\'); putchar('t'); break;
291 case '\f': len += 2; putchar('\\'); putchar('f'); break;
292 case '\v': len += 2; putchar('\\'); putchar('v'); break;
293 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
294 case '"': len += 2; putchar('\\'); putchar('"'); break;
306 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
308 static char spaces[28+1] = " ";
309 prog_section_def *def;
314 len = printf("<null>,");
318 def = prog_getdef(prog, glob);
319 value = (qcany*)(&prog->globals[glob]);
321 len = printf("[@%u] ", glob);
323 const char *name = prog_getstring(prog, def->name);
327 len += printf("%s ", name);
328 vtype = def->type & DEF_TYPEMASK;
337 len += printf("(%i),", value->_int);
340 len += printf("'%g %g %g',", value->vector[0],
345 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
347 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
351 len += printf("%g,", value->_float);
355 if (len < (int)sizeof(spaces)-1) {
356 spaces[sizeof(spaces)-1-len] = 0;
358 spaces[sizeof(spaces)-1-len] = ' ';
362 static void prog_print_statement(qc_program *prog, prog_section_statement *st)
364 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
365 printf("<illegal instruction %d>\n", st->opcode);
368 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
370 for (i = 0; i < vec_size(prog->function_stack); ++i)
372 printf("%s:", vec_last(prog->function_stack));
374 printf(" <> %-12s", asm_instr[st->opcode].m);
375 if (st->opcode >= INSTR_IF &&
376 st->opcode <= INSTR_IFNOT)
378 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
379 printf("%d\n", st->o2.s1);
381 else if (st->opcode >= INSTR_CALL0 &&
382 st->opcode <= INSTR_CALL8)
384 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
387 else if (st->opcode == INSTR_GOTO)
389 printf("%i\n", st->o1.s1);
393 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
397 t[1] = t[2] = TYPE_VECTOR;
400 t[0] = t[2] = TYPE_VECTOR;
403 t[0] = t[1] = TYPE_VECTOR;
409 t[0] = t[1] = t[2] = TYPE_VECTOR;
413 t[0] = t[1] = TYPE_STRING;
420 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
423 t[0] = t[1] = TYPE_STRING; t[2] = -1;
425 case INSTR_STORE_ENT:
426 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
428 case INSTR_STORE_FLD:
429 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
431 case INSTR_STORE_FNC:
432 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
435 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
438 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
440 case INSTR_STOREP_ENT:
441 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
443 case INSTR_STOREP_FLD:
444 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
446 case INSTR_STOREP_FNC:
447 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
450 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
451 else printf("(none), ");
452 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
453 else printf("(none), ");
454 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
455 else printf("(none)");
461 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
468 st.localsp = vec_size(prog->localstack);
469 st.stmt = prog->statement;
472 if (prog->xflags & VMXF_TRACE) {
473 vec_push(prog->function_stack, prog_getstring(prog, func->name));
476 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
477 if (vec_size(prog->stack))
479 prog_section_function *cur;
480 cur = prog->stack[vec_size(prog->stack)-1].function;
483 qcint *globals = prog->globals + cur->firstlocal;
484 vec_append(prog->localstack, cur->locals, globals);
489 qcint *globals = prog->globals + func->firstlocal;
490 vec_append(prog->localstack, func->locals, globals);
494 /* copy parameters */
495 parampos = func->firstlocal;
496 for (p = 0; p < func->nargs; ++p)
499 for (s = 0; s < func->argsize[p]; ++s) {
500 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
505 vec_push(prog->stack, st);
510 static qcint prog_leavefunction(qc_program *prog)
512 prog_section_function *prev = NULL;
515 qc_exec_stack st = vec_last(prog->stack);
517 if (prog->xflags & VMXF_TRACE) {
518 if (vec_size(prog->function_stack))
519 vec_pop(prog->function_stack);
522 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
523 if (vec_size(prog->stack) > 1) {
524 prev = prog->stack[vec_size(prog->stack)-2].function;
525 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
528 prev = prog->stack[vec_size(prog->stack)-1].function;
529 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
532 qcint *globals = prog->globals + prev->firstlocal;
533 memcpy(globals, prog->localstack + oldsp, prev->locals);
534 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
535 vec_shrinkto(prog->localstack, oldsp);
538 vec_pop(prog->stack);
540 return st.stmt - 1; /* offset the ++st */
543 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
546 size_t oldxflags = prog->xflags;
547 prog_section_statement *st;
550 prog->xflags = flags;
552 st = prog->code + prog_enterfunction(prog, func);
560 #define QCVM_PROFILE 0
566 #define QCVM_PROFILE 0
572 #define QCVM_PROFILE 1
576 case (VMXF_TRACE|VMXF_PROFILE):
578 #define QCVM_PROFILE 1
585 prog->xflags = oldxflags;
586 vec_free(prog->localstack);
587 vec_free(prog->stack);
593 /***********************************************************************
594 * main for when building the standalone executor
597 #if defined(QCVM_EXECUTOR)
602 const char *type_name[TYPE_COUNT] = {
622 qcvm_parameter *main_params = NULL;
624 #define CheckArgs(num) do { \
625 if (prog->argc != (num)) { \
627 printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
628 __FUNCTION__, prog->argc, (num)); \
633 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
634 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
635 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
637 static int qc_print(qc_program *prog)
640 const char *laststr = NULL;
641 for (i = 0; i < (size_t)prog->argc; ++i) {
642 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
643 laststr = prog_getstring(prog, str->string);
644 printf("%s", laststr);
646 if (laststr && (prog->xflags & VMXF_TRACE)) {
647 size_t len = strlen(laststr);
648 if (!len || laststr[len-1] != '\n')
654 static int qc_error(qc_program *prog)
656 printf("*** VM raised an error:\n");
662 static int qc_ftos(qc_program *prog)
669 snprintf(buffer, sizeof(buffer), "%g", num->_float);
670 str.string = prog_tempstring(prog, buffer);
675 static int qc_stof(qc_program *prog)
681 num._float = strtof(prog_getstring(prog, str->string), NULL);
686 static int qc_vtos(qc_program *prog)
693 snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
694 str.string = prog_tempstring(prog, buffer);
699 static int qc_etos(qc_program *prog)
706 snprintf(buffer, sizeof(buffer), "%i", num->_int);
707 str.string = prog_tempstring(prog, buffer);
712 static int qc_spawn(qc_program *prog)
716 ent.edict = prog_spawn_entity(prog);
718 return (ent.edict ? 0 : -1);
721 static int qc_kill(qc_program *prog)
726 prog_free_entity(prog, ent->edict);
730 static int qc_vlen(qc_program *prog)
735 len._float = sqrt(vec->vector[0] * vec->vector[0] +
736 vec->vector[1] * vec->vector[1] +
737 vec->vector[2] * vec->vector[2]);
742 static prog_builtin qc_builtins[] = {
754 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
756 static const char *arg0 = NULL;
758 static void version() {
759 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
770 printf("usage: %s [options] [parameters] file\n", arg0);
771 printf("options:\n");
772 printf(" -h, --help print this message\n"
773 " -trace trace the execution\n"
774 " -profile perform profiling during execution\n"
775 " -info print information from the prog's header\n"
776 " -disasm disassemble and exit\n"
777 " -printdefs list the defs section\n"
778 " -printfields list the field section\n"
779 " -printfuns list functions information\n");
780 printf("parameters:\n");
781 printf(" -vector <V> pass a vector parameter to main()\n"
782 " -float <f> pass a float parameter to main()\n"
783 " -string <s> pass a string parameter to main() \n");
786 static void prog_main_setparams(qc_program *prog)
791 for (i = 0; i < vec_size(main_params); ++i) {
792 arg = GetGlobal(OFS_PARM0 + 3*i);
796 switch (main_params[i].vtype) {
799 (void)sscanf_s(main_params[i].value, " %f %f %f ",
804 (void)sscanf(main_params[i].value, " %f %f %f ",
811 arg->_float = atof(main_params[i].value);
814 arg->string = prog_tempstring(prog, main_params[i].value);
817 printf("error: unhandled parameter type: %i\n", main_params[i].vtype);
823 void prog_disasm_function(qc_program *prog, size_t id);
824 int main(int argc, char **argv)
829 size_t xflags = VMXF_DEFAULT;
830 bool opts_printfields = false;
831 bool opts_printdefs = false;
832 bool opts_printfuns = false;
833 bool opts_disasm = false;
834 bool opts_info = false;
836 const char *progsfile = NULL;
846 if (!strcmp(argv[1], "-h") ||
847 !strcmp(argv[1], "-help") ||
848 !strcmp(argv[1], "--help"))
853 else if (!strcmp(argv[1], "-v") ||
854 !strcmp(argv[1], "-version") ||
855 !strcmp(argv[1], "--version"))
860 else if (!strcmp(argv[1], "-trace")) {
863 xflags |= VMXF_TRACE;
865 else if (!strcmp(argv[1], "-profile")) {
868 xflags |= VMXF_PROFILE;
870 else if (!strcmp(argv[1], "-info")) {
876 else if (!strcmp(argv[1], "-disasm")) {
882 else if (!strcmp(argv[1], "-printdefs")) {
885 opts_printdefs = true;
888 else if (!strcmp(argv[1], "-printfuns")) {
891 opts_printfuns = true;
894 else if (!strcmp(argv[1], "-printfields")) {
897 opts_printfields = true;
900 else if (!strcmp(argv[1], "-vector") ||
901 !strcmp(argv[1], "-string") ||
902 !strcmp(argv[1], "-float") )
905 if (argv[1][1] == 'f')
906 p.vtype = TYPE_FLOAT;
907 else if (argv[1][1] == 's')
908 p.vtype = TYPE_STRING;
909 else if (argv[1][1] == 'v')
910 p.vtype = TYPE_VECTOR;
920 vec_push(main_params, p);
924 else if (!strcmp(argv[1], "--")) {
929 else if (argv[1][0] != '-') {
931 printf("only 1 program file may be specified\n");
952 printf("only 1 program file may be specified\n");
966 prog = prog_load(progsfile);
968 printf("failed to load program '%s'\n", progsfile);
972 prog->builtins = qc_builtins;
973 prog->builtins_count = qc_builtins_count;
976 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
977 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
978 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
986 for (i = 1; i < vec_size(prog->functions); ++i)
987 prog_disasm_function(prog, i);
990 if (opts_printdefs) {
991 for (i = 0; i < vec_size(prog->defs); ++i) {
992 printf("Global: %8s %-16s at %u%s\n",
993 type_name[prog->defs[i].type & DEF_TYPEMASK],
994 prog_getstring(prog, prog->defs[i].name),
995 (unsigned int)prog->defs[i].offset,
996 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
999 if (opts_printfields) {
1000 for (i = 0; i < vec_size(prog->fields); ++i) {
1001 printf("Field: %8s %-16s at %u%s\n",
1002 type_name[prog->fields[i].type],
1003 prog_getstring(prog, prog->fields[i].name),
1004 (unsigned int)prog->fields[i].offset,
1005 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1008 if (opts_printfuns) {
1009 for (i = 0; i < vec_size(prog->functions); ++i) {
1011 printf("Function: %-16s taking %i parameters:(",
1012 prog_getstring(prog, prog->functions[i].name),
1013 (unsigned int)prog->functions[i].nargs);
1014 for (a = 0; a < prog->functions[i].nargs; ++a) {
1015 printf(" %i", prog->functions[i].argsize[a]);
1017 printf(") locals: %i + %i\n",
1018 prog->functions[i].firstlocal,
1019 prog->functions[i].locals);
1023 for (i = 1; i < vec_size(prog->functions); ++i) {
1024 const char *name = prog_getstring(prog, prog->functions[i].name);
1025 if (!strcmp(name, "main"))
1030 prog_main_setparams(prog);
1031 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1034 printf("No main function found\n");
1041 void prog_disasm_function(qc_program *prog, size_t id)
1043 prog_section_function *fdef = prog->functions + id;
1044 prog_section_statement *st;
1046 if (fdef->entry < 0) {
1047 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1051 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1053 st = prog->code + fdef->entry;
1054 while (st->opcode != INSTR_DONE) {
1055 prog_print_statement(prog, st);
1060 #else /* !QCVM_LOOP */
1062 * Everything from here on is not including into the compilation of the
1063 * executor. This is simply code that is #included via #include __FILE__
1064 * see when QCVM_LOOP is defined, the rest of the code above do not get
1065 * re-included. So this really just acts like one large macro, but it
1066 * sort of isn't, which makes it nicer looking.
1069 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1070 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1071 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1073 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1075 /* to be consistent with current darkplaces behaviour */
1076 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1077 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1081 prog_section_function *newf;
1088 prog->profile[st - prog->code]++;
1092 prog_print_statement(prog, st);
1098 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1103 /* TODO: add instruction count to function profile count */
1104 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1105 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1106 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1108 st = prog->code + prog_leavefunction(prog);
1109 if (!vec_size(prog->stack))
1115 OPC->_float = OPA->_float * OPB->_float;
1118 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1119 OPA->vector[1]*OPB->vector[1] +
1120 OPA->vector[2]*OPB->vector[2];
1123 OPC->vector[0] = OPA->_float * OPB->vector[0];
1124 OPC->vector[1] = OPA->_float * OPB->vector[1];
1125 OPC->vector[2] = OPA->_float * OPB->vector[2];
1128 OPC->vector[0] = OPB->_float * OPA->vector[0];
1129 OPC->vector[1] = OPB->_float * OPA->vector[1];
1130 OPC->vector[2] = OPB->_float * OPA->vector[2];
1133 if (OPB->_float != 0.0f)
1134 OPC->_float = OPA->_float / OPB->_float;
1140 OPC->_float = OPA->_float + OPB->_float;
1143 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1144 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1145 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1148 OPC->_float = OPA->_float - OPB->_float;
1151 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1152 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1153 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1157 OPC->_float = (OPA->_float == OPB->_float);
1160 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1161 (OPA->vector[1] == OPB->vector[1]) &&
1162 (OPA->vector[2] == OPB->vector[2]) );
1165 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1166 prog_getstring(prog, OPB->string));
1169 OPC->_float = (OPA->_int == OPB->_int);
1172 OPC->_float = (OPA->function == OPB->function);
1175 OPC->_float = (OPA->_float != OPB->_float);
1178 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1179 (OPA->vector[1] != OPB->vector[1]) ||
1180 (OPA->vector[2] != OPB->vector[2]) );
1183 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1184 prog_getstring(prog, OPB->string));
1187 OPC->_float = (OPA->_int != OPB->_int);
1190 OPC->_float = (OPA->function != OPB->function);
1194 OPC->_float = (OPA->_float <= OPB->_float);
1197 OPC->_float = (OPA->_float >= OPB->_float);
1200 OPC->_float = (OPA->_float < OPB->_float);
1203 OPC->_float = (OPA->_float > OPB->_float);
1208 case INSTR_LOAD_FLD:
1209 case INSTR_LOAD_ENT:
1210 case INSTR_LOAD_FNC:
1211 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1212 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1215 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1216 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1221 ed = prog_getedict(prog, OPA->edict);
1222 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1225 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1226 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1229 if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1231 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1236 ed = prog_getedict(prog, OPA->edict);
1237 OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
1238 OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
1239 OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
1243 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1244 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1247 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1249 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1255 ed = prog_getedict(prog, OPA->edict);
1256 OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1261 case INSTR_STORE_ENT:
1262 case INSTR_STORE_FLD:
1263 case INSTR_STORE_FNC:
1264 OPB->_int = OPA->_int;
1267 OPB->ivector[0] = OPA->ivector[0];
1268 OPB->ivector[1] = OPA->ivector[1];
1269 OPB->ivector[2] = OPA->ivector[2];
1272 case INSTR_STOREP_F:
1273 case INSTR_STOREP_S:
1274 case INSTR_STOREP_ENT:
1275 case INSTR_STOREP_FLD:
1276 case INSTR_STOREP_FNC:
1277 if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1278 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1281 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1282 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1284 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1286 ptr = (qcany*)(prog->entitydata + OPB->_int);
1287 ptr->_int = OPA->_int;
1289 case INSTR_STOREP_V:
1290 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1291 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1294 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1295 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1297 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1299 ptr = (qcany*)(prog->entitydata + OPB->_int);
1300 ptr->ivector[0] = OPA->ivector[0];
1301 ptr->ivector[1] = OPA->ivector[1];
1302 ptr->ivector[2] = OPA->ivector[2];
1306 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1309 OPC->_float = !OPA->vector[0] &&
1314 OPC->_float = !OPA->string ||
1315 !*prog_getstring(prog, OPA->string);
1318 OPC->_float = (OPA->edict == 0);
1321 OPC->_float = !OPA->function;
1325 /* this is consistent with darkplaces' behaviour */
1326 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1328 st += st->o2.s1 - 1; /* offset the s++ */
1329 if (++jumpcount >= maxjumps)
1330 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1334 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1336 st += st->o2.s1 - 1; /* offset the s++ */
1337 if (++jumpcount >= maxjumps)
1338 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1351 prog->argc = st->opcode - INSTR_CALL0;
1353 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1355 if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1357 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1361 newf = &prog->functions[OPA->function];
1364 prog->statement = (st - prog->code) + 1;
1366 if (newf->entry < 0)
1368 /* negative statements are built in functions */
1369 qcint builtinnumber = -newf->entry;
1370 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1371 prog->builtins[builtinnumber](prog);
1373 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1374 builtinnumber, prog->filename);
1377 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1383 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1387 st += st->o1.s1 - 1; /* offset the s++ */
1388 if (++jumpcount == 10000000)
1389 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1393 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1394 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1397 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1398 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1402 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1405 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1412 #endif /* !QCVM_LOOP */