1 extern cvar_t prvm_garbagecollection_enable;
3 // NEED to reset startst after calling this! startst may or may not be clobbered!
4 #define ADVANCE_PROFILE_BEFORE_JUMP() \
5 prog->xfunction->profile += (st - startst); \
6 if (prvm_statementprofiling.integer || (prvm_coverage.integer & 4)) { \
7 /* All statements from startst+1 to st have been hit. */ \
8 while (++startst <= st) { \
9 if (prog->statement_profile[startst - cached_statements]++ == 0 && (prvm_coverage.integer & 4)) \
10 PRVM_StatementCoverageEvent(prog, prog->xfunction, startst - cached_statements); \
12 /* Observe: startst now is clobbered (now at st+1)! */ \
15 #ifdef PRVMTIMEPROFILING
17 ADVANCE_PROFILE_BEFORE_JUMP(); \
18 prog->xstatement = st - cached_statements; \
19 tm = Sys_DirtyTime(); \
20 prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0; \
25 ADVANCE_PROFILE_BEFORE_JUMP(); \
26 prog->xstatement = st - cached_statements; \
30 // This code isn't #ifdef/#define protectable, don't try.
32 #if HAVE_COMPUTED_GOTOS && !(PRVMSLOWINTERPRETER || PRVMTIMEPROFILING)
33 // NOTE: Due to otherwise duplicate labels, only ONE interpreter path may
35 # define USE_COMPUTED_GOTOS 1
38 #if USE_COMPUTED_GOTOS
39 // Must exactly match opcode_e enum in pr_comp.h
40 const static void *dispatchtable[] = {
81 &&handle_OP_STORE_ENT,
82 &&handle_OP_STORE_FLD,
83 &&handle_OP_STORE_FNC,
88 &&handle_OP_STOREP_ENT,
89 &&handle_OP_STOREP_FLD,
90 &&handle_OP_STOREP_FNC,
184 &&handle_OP_STOREP_I,
189 &&handle_OP_BITAND_I,
259 &&handle_OP_BITAND_IF,
260 &&handle_OP_BITOR_IF,
261 &&handle_OP_BITAND_FI,
262 &&handle_OP_BITOR_FI,
272 &&handle_OP_GSTOREP_I,
273 &&handle_OP_GSTOREP_F,
274 &&handle_OP_GSTOREP_ENT,
275 &&handle_OP_GSTOREP_FLD,
276 &&handle_OP_GSTOREP_S,
277 &&handle_OP_GSTOREP_FNC,
278 &&handle_OP_GSTOREP_V,
279 &&handle_OP_GADDRESS,
282 &&handle_OP_GLOAD_FLD,
283 &&handle_OP_GLOAD_ENT,
285 &&handle_OP_GLOAD_FNC,
286 &&handle_OP_BOUNDCHECK,
293 #define DISPATCH_OPCODE() \
294 goto *dispatchtable[(++st)->op]
295 #define HANDLE_OPCODE(opcode) handle_##opcode
297 DISPATCH_OPCODE(); // jump to first opcode
298 #else // USE_COMPUTED_GOTOS
299 #define DISPATCH_OPCODE() break
300 #define HANDLE_OPCODE(opcode) case opcode
302 #if PRVMSLOWINTERPRETER
304 if (prog->watch_global_type != ev_void)
306 prvm_eval_t *g = PRVM_GLOBALFIELDVALUE(prog->watch_global);
307 prog->xstatement = st + 1 - cached_statements;
308 PRVM_Watchpoint(prog, 1, "Global watchpoint hit by engine", prog->watch_global_type, &prog->watch_global_value, g);
310 if (prog->watch_field_type != ev_void && prog->watch_edict < prog->max_edicts)
312 prvm_eval_t *g = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
313 prog->xstatement = st + 1 - cached_statements;
314 PRVM_Watchpoint(prog, 1, "Entityfield watchpoint hit by engine", prog->watch_field_type, &prog->watch_edictfield_value, g);
322 #endif // USE_COMPUTED_GOTOS
324 #if !USE_COMPUTED_GOTOS
326 #if PRVMSLOWINTERPRETER
328 PRVM_PrintStatement(prog, st);
329 if (prog->break_statement >= 0)
330 if ((st - cached_statements) == prog->break_statement)
332 prog->xstatement = st - cached_statements;
333 PRVM_Breakpoint(prog, prog->break_stack_index, "Breakpoint hit");
339 HANDLE_OPCODE(OP_ADD_F):
340 OPC->_float = OPA->_float + OPB->_float;
342 HANDLE_OPCODE(OP_ADD_V):
343 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
344 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
345 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
347 HANDLE_OPCODE(OP_SUB_F):
348 OPC->_float = OPA->_float - OPB->_float;
350 HANDLE_OPCODE(OP_SUB_V):
351 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
352 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
353 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
355 HANDLE_OPCODE(OP_MUL_F):
356 OPC->_float = OPA->_float * OPB->_float;
358 HANDLE_OPCODE(OP_MUL_V):
359 OPC->_float = OPA->vector[0]*OPB->vector[0] + OPA->vector[1]*OPB->vector[1] + OPA->vector[2]*OPB->vector[2];
361 HANDLE_OPCODE(OP_MUL_FV):
362 tempfloat = OPA->_float;
363 OPC->vector[0] = tempfloat * OPB->vector[0];
364 OPC->vector[1] = tempfloat * OPB->vector[1];
365 OPC->vector[2] = tempfloat * OPB->vector[2];
367 HANDLE_OPCODE(OP_MUL_VF):
368 tempfloat = OPB->_float;
369 OPC->vector[0] = tempfloat * OPA->vector[0];
370 OPC->vector[1] = tempfloat * OPA->vector[1];
371 OPC->vector[2] = tempfloat * OPA->vector[2];
373 HANDLE_OPCODE(OP_DIV_F):
374 if( OPB->_float != 0.0f )
376 OPC->_float = OPA->_float / OPB->_float;
380 if (developer.integer)
383 VM_Warning(prog, "Attempted division by zero in %s\n", prog->name );
388 HANDLE_OPCODE(OP_BITAND):
389 OPC->_float = (prvm_int_t)OPA->_float & (prvm_int_t)OPB->_float;
391 HANDLE_OPCODE(OP_BITOR):
392 OPC->_float = (prvm_int_t)OPA->_float | (prvm_int_t)OPB->_float;
394 HANDLE_OPCODE(OP_GE):
395 OPC->_float = OPA->_float >= OPB->_float;
397 HANDLE_OPCODE(OP_LE):
398 OPC->_float = OPA->_float <= OPB->_float;
400 HANDLE_OPCODE(OP_GT):
401 OPC->_float = OPA->_float > OPB->_float;
403 HANDLE_OPCODE(OP_LT):
404 OPC->_float = OPA->_float < OPB->_float;
406 HANDLE_OPCODE(OP_AND):
407 OPC->_float = PRVM_FLOAT_IS_TRUE_FOR_INT(OPA->_int) && PRVM_FLOAT_IS_TRUE_FOR_INT(OPB->_int); // TODO change this back to float, and add AND_I to be used by fteqcc for anything not a float
409 HANDLE_OPCODE(OP_OR):
410 OPC->_float = PRVM_FLOAT_IS_TRUE_FOR_INT(OPA->_int) || PRVM_FLOAT_IS_TRUE_FOR_INT(OPB->_int); // TODO change this back to float, and add OR_I to be used by fteqcc for anything not a float
412 HANDLE_OPCODE(OP_NOT_F):
413 OPC->_float = !PRVM_FLOAT_IS_TRUE_FOR_INT(OPA->_int);
415 HANDLE_OPCODE(OP_NOT_V):
416 OPC->_float = !OPA->vector[0] && !OPA->vector[1] && !OPA->vector[2];
418 HANDLE_OPCODE(OP_NOT_S):
419 OPC->_float = !OPA->string || !*PRVM_GetString(prog, OPA->string);
421 HANDLE_OPCODE(OP_NOT_FNC):
422 OPC->_float = !OPA->function;
424 HANDLE_OPCODE(OP_NOT_ENT):
425 OPC->_float = (OPA->edict == 0);
427 HANDLE_OPCODE(OP_EQ_F):
428 OPC->_float = OPA->_float == OPB->_float;
430 HANDLE_OPCODE(OP_EQ_V):
431 OPC->_float = (OPA->vector[0] == OPB->vector[0]) && (OPA->vector[1] == OPB->vector[1]) && (OPA->vector[2] == OPB->vector[2]);
433 HANDLE_OPCODE(OP_EQ_S):
434 OPC->_float = !strcmp(PRVM_GetString(prog, OPA->string),PRVM_GetString(prog, OPB->string));
436 HANDLE_OPCODE(OP_EQ_E):
437 OPC->_float = OPA->_int == OPB->_int;
439 HANDLE_OPCODE(OP_EQ_FNC):
440 OPC->_float = OPA->function == OPB->function;
442 HANDLE_OPCODE(OP_NE_F):
443 OPC->_float = OPA->_float != OPB->_float;
445 HANDLE_OPCODE(OP_NE_V):
446 OPC->_float = (OPA->vector[0] != OPB->vector[0]) || (OPA->vector[1] != OPB->vector[1]) || (OPA->vector[2] != OPB->vector[2]);
448 HANDLE_OPCODE(OP_NE_S):
449 OPC->_float = strcmp(PRVM_GetString(prog, OPA->string),PRVM_GetString(prog, OPB->string));
451 HANDLE_OPCODE(OP_NE_E):
452 OPC->_float = OPA->_int != OPB->_int;
454 HANDLE_OPCODE(OP_NE_FNC):
455 OPC->_float = OPA->function != OPB->function;
459 HANDLE_OPCODE(OP_STORE_F):
460 HANDLE_OPCODE(OP_STORE_ENT):
461 HANDLE_OPCODE(OP_STORE_FLD): // integers
462 HANDLE_OPCODE(OP_STORE_FNC): // pointers
463 OPB->_int = OPA->_int;
465 HANDLE_OPCODE(OP_STORE_S):
466 // refresh the garbage collection on the string - this guards
467 // against a certain sort of repeated migration to earlier
468 // points in the scan that could otherwise result in the string
469 // being freed for being unused
470 if(prvm_garbagecollection_enable.integer)
471 PRVM_GetString(prog, OPA->_int);
472 OPB->_int = OPA->_int;
474 HANDLE_OPCODE(OP_STORE_V):
475 OPB->ivector[0] = OPA->ivector[0];
476 OPB->ivector[1] = OPA->ivector[1];
477 OPB->ivector[2] = OPA->ivector[2];
480 HANDLE_OPCODE(OP_STOREP_F):
481 HANDLE_OPCODE(OP_STOREP_ENT):
482 HANDLE_OPCODE(OP_STOREP_FLD): // integers
483 HANDLE_OPCODE(OP_STOREP_FNC): // pointers
484 if ((prvm_uint_t)OPB->_int - cached_entityfields >= cached_entityfieldsarea_entityfields)
486 if ((prvm_uint_t)OPB->_int >= cached_entityfieldsarea)
489 prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
492 if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
495 VM_Warning(prog, "Attempted assignment to NULL entity field .%s (%i) in %s\n", PRVM_GetString(prog, PRVM_ED_FieldAtOfs(prog, OPB->_int)->s_name), (int)OPB->_int, prog->name);
498 ptr = (prvm_eval_t *)(cached_edictsfields + OPB->_int);
499 ptr->_int = OPA->_int;
501 HANDLE_OPCODE(OP_STOREP_S):
502 if ((prvm_uint_t)OPB->_int - cached_entityfields >= cached_entityfieldsarea_entityfields)
504 if ((prvm_uint_t)OPB->_int >= cached_entityfieldsarea)
507 prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
510 if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
513 VM_Warning(prog, "Attempted assignment to NULL entity field .%s (%i) in %s\n", PRVM_GetString(prog, PRVM_ED_FieldAtOfs(prog, OPB->_int)->s_name), (int)OPB->_int, prog->name);
516 // refresh the garbage collection on the string - this guards
517 // against a certain sort of repeated migration to earlier
518 // points in the scan that could otherwise result in the string
519 // being freed for being unused
520 if(prvm_garbagecollection_enable.integer)
521 PRVM_GetString(prog, OPA->_int);
522 ptr = (prvm_eval_t *)(cached_edictsfields + OPB->_int);
523 ptr->_int = OPA->_int;
525 HANDLE_OPCODE(OP_STOREP_V):
526 if ((prvm_uint_t)OPB->_int - cached_entityfields > (prvm_uint_t)cached_entityfieldsarea_entityfields_3)
528 if ((prvm_uint_t)OPB->_int > cached_entityfieldsarea_3)
531 prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
534 if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
537 VM_Warning(prog, "Attempted assignment to NULL entity field .%s (%i) in %s\n", PRVM_GetString(prog, PRVM_ED_FieldAtOfs(prog, OPB->_int)->s_name), (int)OPB->_int, prog->name);
540 ptr = (prvm_eval_t *)(cached_edictsfields + OPB->_int);
541 ptr->ivector[0] = OPA->ivector[0];
542 ptr->ivector[1] = OPA->ivector[1];
543 ptr->ivector[2] = OPA->ivector[2];
546 HANDLE_OPCODE(OP_ADDRESS):
547 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
550 prog->error_cmd("%s attempted to address an out of bounds edict number", prog->name);
553 if ((prvm_uint_t)OPB->_int >= cached_entityfields)
556 prog->error_cmd("%s attempted to address an invalid field (%i) in an edict", prog->name, (int)OPB->_int);
560 if (OPA->edict == 0 && !cached_allowworldwrites)
563 prog->error_cmd("Forbidden assignment to NULL entity in %s", prog->name);
567 OPC->_int = OPA->edict * cached_entityfields + OPB->_int;
570 HANDLE_OPCODE(OP_LOAD_F):
571 HANDLE_OPCODE(OP_LOAD_FLD):
572 HANDLE_OPCODE(OP_LOAD_ENT):
573 HANDLE_OPCODE(OP_LOAD_FNC):
574 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
577 prog->error_cmd("%s attempted to read an out of bounds edict number", prog->name);
580 if ((prvm_uint_t)OPB->_int >= cached_entityfields)
583 prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
586 ed = PRVM_PROG_TO_EDICT(OPA->edict);
587 OPC->_int = ((prvm_eval_t *)(ed->fields.ip + OPB->_int))->_int;
589 HANDLE_OPCODE(OP_LOAD_S):
590 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
593 prog->error_cmd("%s attempted to read an out of bounds edict number", prog->name);
596 if ((prvm_uint_t)OPB->_int >= cached_entityfields)
599 prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
602 ed = PRVM_PROG_TO_EDICT(OPA->edict);
603 OPC->_int = ((prvm_eval_t *)(ed->fields.ip + OPB->_int))->_int;
604 // refresh the garbage collection on the string - this guards
605 // against a certain sort of repeated migration to earlier
606 // points in the scan that could otherwise result in the string
607 // being freed for being unused
608 if(prvm_garbagecollection_enable.integer)
609 PRVM_GetString(prog, OPC->_int);
612 HANDLE_OPCODE(OP_LOAD_V):
613 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
616 prog->error_cmd("%s attempted to read an out of bounds edict number", prog->name);
619 if ((prvm_uint_t)OPB->_int > cached_entityfields_3)
622 prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
625 ed = PRVM_PROG_TO_EDICT(OPA->edict);
626 ptr = (prvm_eval_t *)(ed->fields.ip + OPB->_int);
627 OPC->ivector[0] = ptr->ivector[0];
628 OPC->ivector[1] = ptr->ivector[1];
629 OPC->ivector[2] = ptr->ivector[2];
634 HANDLE_OPCODE(OP_IFNOT):
635 //spike FIXME -- dp redefined IFNOT[_I] as IFNOT_F, which breaks if(0x80000000)
636 //spike FIXME -- you should add separate IFNOT_I/IFNOT_F opcodes and remap IFNOT_I to ITNOT_F in v6 progs for compat.
637 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
638 // TODO add an "int-if", and change this one to OPA->_float
639 // although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
640 // and entity, string, field values can never have that value
642 ADVANCE_PROFILE_BEFORE_JUMP();
643 st = cached_statements + st->jumpabsolute - 1; // offset the st++
645 // no bounds check needed, it is done when loading progs
646 if (++jumpcount == 10000000 && prvm_runawaycheck)
648 prog->xstatement = st - cached_statements;
649 PRVM_Profile(prog, 1<<30, 1000000, 0);
650 prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
655 HANDLE_OPCODE(OP_IF):
656 //spike FIXME -- dp redefined IF[_I] as IF_F
657 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
658 // TODO add an "int-if", and change this one, as well as the FLOAT_IS_TRUE_FOR_INT usages, to OPA->_float
659 // although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
660 // and entity, string, field values can never have that value
662 ADVANCE_PROFILE_BEFORE_JUMP();
663 st = cached_statements + st->jumpabsolute - 1; // offset the st++
665 // no bounds check needed, it is done when loading progs
666 if (++jumpcount == 10000000 && prvm_runawaycheck)
668 prog->xstatement = st - cached_statements;
669 PRVM_Profile(prog, 1<<30, 0.01, 0);
670 prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
675 HANDLE_OPCODE(OP_GOTO):
676 ADVANCE_PROFILE_BEFORE_JUMP();
677 st = cached_statements + st->jumpabsolute - 1; // offset the st++
679 // no bounds check needed, it is done when loading progs
680 if (++jumpcount == 10000000 && prvm_runawaycheck)
682 prog->xstatement = st - cached_statements;
683 PRVM_Profile(prog, 1<<30, 0.01, 0);
684 prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
688 HANDLE_OPCODE(OP_CALL0):
689 HANDLE_OPCODE(OP_CALL1):
690 HANDLE_OPCODE(OP_CALL2):
691 HANDLE_OPCODE(OP_CALL3):
692 HANDLE_OPCODE(OP_CALL4):
693 HANDLE_OPCODE(OP_CALL5):
694 HANDLE_OPCODE(OP_CALL6):
695 HANDLE_OPCODE(OP_CALL7):
696 HANDLE_OPCODE(OP_CALL8):
697 #ifdef PRVMTIMEPROFILING
698 tm = Sys_DirtyTime();
699 prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
702 ADVANCE_PROFILE_BEFORE_JUMP();
704 prog->xstatement = st - cached_statements;
705 prog->argc = st->op - OP_CALL0;
708 prog->error_cmd("NULL function in %s", prog->name);
711 if(!OPA->function || OPA->function < 0 || OPA->function >= prog->numfunctions)
714 prog->error_cmd("%s attempted CALL outside the program", prog->name);
718 enterfunc = &prog->functions[OPA->function];
719 if (enterfunc->callcount++ == 0 && (prvm_coverage.integer & 1))
720 PRVM_FunctionCoverageEvent(prog, enterfunc);
722 if (enterfunc->first_statement < 0)
724 // negative first_statement values are built in functions
725 int builtinnumber = -enterfunc->first_statement;
726 prog->xfunction->builtinsprofile++;
727 if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
729 prog->builtins[builtinnumber](prog);
730 #ifdef PRVMTIMEPROFILING
731 tm = Sys_DirtyTime();
732 enterfunc->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
733 prog->xfunction->tbprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
736 // builtins may cause ED_Alloc() to be called, update cached variables
737 cached_edictsfields = prog->edictsfields.fp;
738 cached_entityfields = prog->entityfields;
739 cached_entityfields_3 = prog->entityfields - 3;
740 cached_entityfieldsarea = prog->entityfieldsarea;
741 cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
742 cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
743 cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
744 cached_max_edicts = prog->max_edicts;
745 // these do not change
746 //cached_statements = prog->statements;
747 //cached_allowworldwrites = prog->allowworldwrites;
748 //cached_flag = prog->flag;
749 // if prog->trace changed we need to change interpreter path
750 if (prog->trace != cachedpr_trace)
751 goto chooseexecprogram;
754 prog->error_cmd("No such builtin #%i in %s. This program is corrupt or incompatible with DarkPlaces (or this version of it)", builtinnumber, prog->name);
757 st = cached_statements + PRVM_EnterFunction(prog, enterfunc);
761 HANDLE_OPCODE(OP_DONE):
762 HANDLE_OPCODE(OP_RETURN):
763 #ifdef PRVMTIMEPROFILING
764 tm = Sys_DirtyTime();
765 prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
768 ADVANCE_PROFILE_BEFORE_JUMP();
769 prog->xstatement = st - cached_statements;
771 prog->globals.ip[OFS_RETURN ] = prog->globals.ip[st->operand[0] ];
772 prog->globals.ip[OFS_RETURN+1] = prog->globals.ip[st->operand[0]+1];
773 prog->globals.ip[OFS_RETURN+2] = prog->globals.ip[st->operand[0]+2];
775 st = cached_statements + PRVM_LeaveFunction(prog);
777 if (prog->depth <= exitdepth)
778 goto cleanup; // all done
781 HANDLE_OPCODE(OP_STATE):
782 if(cached_flag & PRVM_OP_STATE)
784 ed = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
785 PRVM_gameedictfloat(ed,nextthink) = PRVM_gameglobalfloat(time) + 0.1;
786 PRVM_gameedictfloat(ed,frame) = OPA->_float;
787 PRVM_gameedictfunction(ed,think) = OPB->function;
792 prog->xstatement = st - cached_statements;
793 prog->error_cmd("OP_STATE not supported by %s", prog->name);
797 HANDLE_OPCODE(OP_ADD_I):
798 OPC->_int = OPA->_int + OPB->_int;
800 HANDLE_OPCODE(OP_ADD_IF):
801 OPC->_float = OPA->_int + (prvm_int_t) OPB->_float;
803 HANDLE_OPCODE(OP_ADD_FI):
804 OPC->_float = OPA->_float + (prvm_vec_t) OPB->_int;
806 HANDLE_OPCODE(OP_SUB_I):
807 OPC->_int = OPA->_int - OPB->_int;
809 HANDLE_OPCODE(OP_SUB_IF):
810 OPC->_float = OPA->_int - (prvm_int_t) OPB->_float;
812 HANDLE_OPCODE(OP_SUB_FI):
813 OPC->_float = OPA->_float - (prvm_vec_t) OPB->_int;
815 HANDLE_OPCODE(OP_MUL_I):
816 OPC->_int = OPA->_int * OPB->_int;
818 HANDLE_OPCODE(OP_MUL_IF):
819 OPC->_float = OPA->_int * (prvm_int_t) OPB->_float;
821 HANDLE_OPCODE(OP_MUL_FI):
822 OPC->_float = OPA->_float * (prvm_vec_t) OPB->_int;
824 HANDLE_OPCODE(OP_MUL_VI):
825 OPC->vector[0] = (prvm_vec_t) OPB->_int * OPA->vector[0];
826 OPC->vector[1] = (prvm_vec_t) OPB->_int * OPA->vector[1];
827 OPC->vector[2] = (prvm_vec_t) OPB->_int * OPA->vector[2];
829 HANDLE_OPCODE(OP_DIV_VF):
831 float temp = 1.0f / OPB->_float;
832 OPC->vector[0] = temp * OPA->vector[0];
833 OPC->vector[1] = temp * OPA->vector[1];
834 OPC->vector[2] = temp * OPA->vector[2];
837 HANDLE_OPCODE(OP_DIV_I):
838 OPC->_int = OPA->_int / OPB->_int;
840 HANDLE_OPCODE(OP_DIV_IF):
841 OPC->_float = OPA->_int / (prvm_int_t) OPB->_float;
843 HANDLE_OPCODE(OP_DIV_FI):
844 OPC->_float = OPA->_float / (prvm_vec_t) OPB->_int;
846 HANDLE_OPCODE(OP_CONV_IF):
847 OPC->_float = OPA->_int;
849 HANDLE_OPCODE(OP_CONV_FI):
850 OPC->_int = OPA->_float;
852 HANDLE_OPCODE(OP_BITAND_I):
853 OPC->_int = OPA->_int & OPB->_int;
855 HANDLE_OPCODE(OP_BITOR_I):
856 OPC->_int = OPA->_int | OPB->_int;
858 HANDLE_OPCODE(OP_BITAND_IF):
859 OPC->_int = OPA->_int & (prvm_int_t)OPB->_float;
861 HANDLE_OPCODE(OP_BITOR_IF):
862 OPC->_int = OPA->_int | (prvm_int_t)OPB->_float;
864 HANDLE_OPCODE(OP_BITAND_FI):
865 OPC->_int = (prvm_int_t)OPA->_float & OPB->_int;
867 HANDLE_OPCODE(OP_BITOR_FI):
868 OPC->_int = (prvm_int_t)OPA->_float | OPB->_int;
870 HANDLE_OPCODE(OP_GE_I):
871 OPC->_int = OPA->_int >= OPB->_int;
873 HANDLE_OPCODE(OP_LE_I):
874 OPC->_int = OPA->_int <= OPB->_int;
876 HANDLE_OPCODE(OP_GT_I):
877 OPC->_int = OPA->_int > OPB->_int;
879 HANDLE_OPCODE(OP_LT_I):
880 OPC->_int = OPA->_int < OPB->_int;
882 HANDLE_OPCODE(OP_AND_I):
883 OPC->_int = OPA->_int && OPB->_int;
885 HANDLE_OPCODE(OP_OR_I):
886 OPC->_int = OPA->_int || OPB->_int;
888 HANDLE_OPCODE(OP_GE_IF):
889 OPC->_int = (prvm_vec_t)OPA->_int >= OPB->_float;
891 HANDLE_OPCODE(OP_LE_IF):
892 OPC->_int = (prvm_vec_t)OPA->_int <= OPB->_float;
894 HANDLE_OPCODE(OP_GT_IF):
895 OPC->_int = (prvm_vec_t)OPA->_int > OPB->_float;
897 HANDLE_OPCODE(OP_LT_IF):
898 OPC->_int = (prvm_vec_t)OPA->_int < OPB->_float;
900 HANDLE_OPCODE(OP_AND_IF):
901 OPC->_int = (prvm_vec_t)OPA->_int && OPB->_float;
903 HANDLE_OPCODE(OP_OR_IF):
904 OPC->_int = (prvm_vec_t)OPA->_int || OPB->_float;
906 HANDLE_OPCODE(OP_GE_FI):
907 OPC->_int = OPA->_float >= (prvm_vec_t)OPB->_int;
909 HANDLE_OPCODE(OP_LE_FI):
910 OPC->_int = OPA->_float <= (prvm_vec_t)OPB->_int;
912 HANDLE_OPCODE(OP_GT_FI):
913 OPC->_int = OPA->_float > (prvm_vec_t)OPB->_int;
915 HANDLE_OPCODE(OP_LT_FI):
916 OPC->_int = OPA->_float < (prvm_vec_t)OPB->_int;
918 HANDLE_OPCODE(OP_AND_FI):
919 OPC->_int = OPA->_float && (prvm_vec_t)OPB->_int;
921 HANDLE_OPCODE(OP_OR_FI):
922 OPC->_int = OPA->_float || (prvm_vec_t)OPB->_int;
924 HANDLE_OPCODE(OP_NOT_I):
925 OPC->_int = !OPA->_int;
927 HANDLE_OPCODE(OP_EQ_I):
928 OPC->_int = OPA->_int == OPB->_int;
930 HANDLE_OPCODE(OP_EQ_IF):
931 OPC->_int = (prvm_vec_t)OPA->_int == OPB->_float;
933 HANDLE_OPCODE(OP_EQ_FI):
934 OPC->_int = OPA->_float == (prvm_vec_t)OPB->_int;
936 HANDLE_OPCODE(OP_NE_I):
937 OPC->_int = OPA->_int != OPB->_int;
939 HANDLE_OPCODE(OP_NE_IF):
940 OPC->_int = (prvm_vec_t)OPA->_int != OPB->_float;
942 HANDLE_OPCODE(OP_NE_FI):
943 OPC->_int = OPA->_float != (prvm_vec_t)OPB->_int;
945 HANDLE_OPCODE(OP_STORE_I):
946 HANDLE_OPCODE(OP_STORE_P):
947 OPB->_int = OPA->_int;
949 HANDLE_OPCODE(OP_STOREP_I):
951 if (OPB->_int < 0 || OPB->_int + 4 > pr_edictareasize)
954 prog->error_cmd("%s attempted to write to an out of bounds edict", prog->name);
958 ptr = (prvm_eval_t *)(prog->edictsfields.ip + OPB->_int);
959 ptr->_int = OPA->_int;
961 HANDLE_OPCODE(OP_LOAD_I):
963 if (OPA->edict < 0 || OPA->edict >= prog->max_edicts)
966 prog->error_cmd("%s attempted to read an out of bounds edict number", prog->name);
969 if (OPB->_int < 0 || OPB->_int >= progs->entityfields.ip)
972 prog->error_cmd("%s attempted to read an invalid field in an edict", prog->name);
976 ed = PRVM_PROG_TO_EDICT(OPA->edict);
977 OPC->_int = ((prvm_eval_t *)((int *)ed->fields.ip + OPB->_int))->_int;
980 HANDLE_OPCODE(OP_GSTOREP_I):
981 HANDLE_OPCODE(OP_GSTOREP_F):
982 HANDLE_OPCODE(OP_GSTOREP_ENT):
983 HANDLE_OPCODE(OP_GSTOREP_FLD): // integers
984 HANDLE_OPCODE(OP_GSTOREP_S):
985 HANDLE_OPCODE(OP_GSTOREP_FNC): // pointers
986 if (OPB->_int < 0 || OPB->_int >= prog->numglobals)
989 prog->error_cmd("%s attempted to write to an invalid indexed global", prog->name);
992 prog->globals.ip[OPB->_int] = OPA->_int;
994 HANDLE_OPCODE(OP_GSTOREP_V):
995 if (OPB->_int < 0 || OPB->_int + 2 >= prog->numglobals)
998 prog->error_cmd("%s attempted to write to an invalid indexed global", prog->name);
1001 prog->globals.ip[OPB->_int ] = OPA->ivector[0];
1002 prog->globals.ip[OPB->_int+1] = OPA->ivector[1];
1003 prog->globals.ip[OPB->_int+2] = OPA->ivector[2];
1006 HANDLE_OPCODE(OP_GADDRESS):
1007 i = OPA->_int + (prvm_int_t) OPB->_float;
1008 if (i < 0 || i >= prog->numglobaldefs)
1011 prog->error_cmd("%s attempted to address an out of bounds global", prog->name);
1014 OPC->_int = prog->globals.ip[i];
1017 HANDLE_OPCODE(OP_GLOAD_I):
1018 HANDLE_OPCODE(OP_GLOAD_F):
1019 HANDLE_OPCODE(OP_GLOAD_FLD):
1020 HANDLE_OPCODE(OP_GLOAD_ENT):
1021 HANDLE_OPCODE(OP_GLOAD_S):
1022 HANDLE_OPCODE(OP_GLOAD_FNC):
1023 if (OPA->_int < 0 || OPA->_int >= prog->numglobals)
1026 prog->error_cmd("%s attempted to read an invalid indexed global", prog->name);
1029 OPC->_int = prog->globals.ip[OPA->_int];
1032 HANDLE_OPCODE(OP_GLOAD_V):
1033 if (OPA->_int < 0 || OPA->_int + 2 >= prog->numglobals)
1036 prog->error_cmd("%s attempted to read an invalid indexed global", prog->name);
1039 OPC->ivector[0] = prog->globals.ip[OPA->_int ];
1040 OPC->ivector[1] = prog->globals.ip[OPA->_int+1];
1041 OPC->ivector[2] = prog->globals.ip[OPA->_int+2];
1044 HANDLE_OPCODE(OP_BOUNDCHECK):
1045 if ((unsigned int)OPA->_int < (unsigned int)st->operand[2] || (unsigned int)OPA->_int >= (unsigned int)st->operand[1])
1048 prog->error_cmd("Progs boundcheck failed in %s, value is < %" PRVM_PRIi " or >= %" PRVM_PRIi, prog->name, OPC->_int, OPB->_int);
1053 #if !USE_COMPUTED_GOTOS
1056 prog->error_cmd("Bad opcode %i in %s. This program is corrupt or incompatible with DarkPlaces (or this version of it)", st->op, prog->name);
1059 #if PRVMSLOWINTERPRETER
1061 if (prog->watch_global_type != ev_void)
1063 prvm_eval_t *g = PRVM_GLOBALFIELDVALUE(prog->watch_global);
1064 prog->xstatement = st - cached_statements;
1065 PRVM_Watchpoint(prog, 0, "Global watchpoint hit", prog->watch_global_type, &prog->watch_global_value, g);
1067 if (prog->watch_field_type != ev_void && prog->watch_edict < prog->max_edicts)
1069 prvm_eval_t *g = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
1070 prog->xstatement = st - cached_statements;
1071 PRVM_Watchpoint(prog, 0, "Entityfield watchpoint hit", prog->watch_field_type, &prog->watch_edictfield_value, g);
1076 #endif // !USE_COMPUTED_GOTOS
1078 #undef DISPATCH_OPCODE
1079 #undef HANDLE_OPCODE
1080 #undef USE_COMPUTED_GOTOS
1082 #undef ADVANCE_PROFILE_BEFORE_JUMP