Debian Codegraph @

code/qcommon/vm_interpreted.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 #include "vm_local.h"
23
24 //#define DEBUG_VM
25 #ifdef DEBUG_VM
26 static char *opnames[256] = {
27 "OP_UNDEF",
28
29 "OP_IGNORE",
30
31 "OP_BREAK",
32
33 "OP_ENTER",
34 "OP_LEAVE",
35 "OP_CALL",
36 "OP_PUSH",
37 "OP_POP",
38
39 "OP_CONST",
40
41 "OP_LOCAL",
42
43 "OP_JUMP",
44
45 //-------------------
46
47 "OP_EQ",
48 "OP_NE",
49
50 "OP_LTI",
51 "OP_LEI",
52 "OP_GTI",
53 "OP_GEI",
54
55 "OP_LTU",
56 "OP_LEU",
57 "OP_GTU",
58 "OP_GEU",
59
60 "OP_EQF",
61 "OP_NEF",
62
63 "OP_LTF",
64 "OP_LEF",
65 "OP_GTF",
66 "OP_GEF",
67
68 //-------------------
69
70 "OP_LOAD1",
71 "OP_LOAD2",
72 "OP_LOAD4",
73 "OP_STORE1",
74 "OP_STORE2",
75 "OP_STORE4",
76 "OP_ARG",
77
78 "OP_BLOCK_COPY",
79
80 //-------------------
81
82 "OP_SEX8",
83 "OP_SEX16",
84
85 "OP_NEGI",
86 "OP_ADD",
87 "OP_SUB",
88 "OP_DIVI",
89 "OP_DIVU",
90 "OP_MODI",
91 "OP_MODU",
92 "OP_MULI",
93 "OP_MULU",
94
95 "OP_BAND",
96 "OP_BOR",
97 "OP_BXOR",
98 "OP_BCOM",
99
100 "OP_LSH",
101 "OP_RSHI",
102 "OP_RSHU",
103
104 "OP_NEGF",
105 "OP_ADDF",
106 "OP_SUBF",
107 "OP_DIVF",
108 "OP_MULF",
109
110 "OP_CVIF",
111 "OP_CVFI"
112 };
113 #endif
114
115 #if idppc
116
117 //FIXME: these, um... look the same to me
118 #if defined(__GNUC__)
119 static ID_INLINE unsigned int loadWord(void *addr) {
120 unsigned int word;
121
122 asm("lwbrx %0,0,%1" : "=r" (word) : "r" (addr));
123 return word;
124 }
125 #else
126 static ID_INLINE unsigned int __lwbrx(register void *addr,
127 register int offset) {
128 register unsigned int word;
129
130 asm("lwbrx %0,%2,%1" : "=r" (word) : "r" (addr), "b" (offset));
131 return word;
132 }
133 #define loadWord(addr) __lwbrx(addr,0)
134 #endif
135
136 #else
137 static ID_INLINE int loadWord(void *addr) {
138 int word;
139 memcpy(&word, addr, 4);
140 return LittleLong(word);
141 }
142 #endif
143
144 char *VM_Indent( vm_t *vm ) {
145 static char *string = " ";
146 if ( vm->callLevel > 20 ) {
147 return string;
148 }
149 return string + 2 * ( 20 - vm->callLevel );
150 }
151
152 void VM_StackTrace( vm_t *vm, int programCounter, int programStack ) {
153 int count;
154
155 count = 0;
156 do {
157 Com_Printf( "%s\n", VM_ValueToSymbol( vm, programCounter ) );
158 programStack = *(int *)&vm->dataBase[programStack+4];
159 programCounter = *(int *)&vm->dataBase[programStack];
160 } while ( programCounter != -1 && ++count < 32 );
161
162 }
163
164
165 /*
166 ====================
167 VM_PrepareInterpreter
168 ====================
169 */
170 void VM_PrepareInterpreter( vm_t *vm, vmHeader_t *header ) {
171 int op;
172 int byte_pc;
173 int int_pc;
174 byte *code;
175 int instruction;
176 int *codeBase;
177
178 vm->codeBase = Hunk_Alloc( vm->codeLength*4, h_high ); // we're now int aligned
179 // memcpy( vm->codeBase, (byte *)header + header->codeOffset, vm->codeLength );
180
181 // we don't need to translate the instructions, but we still need
182 // to find each instructions starting point for jumps
183 int_pc = byte_pc = 0;
184 instruction = 0;
185 code = (byte *)header + header->codeOffset;
186 codeBase = (int *)vm->codeBase;
187
188 // Copy and expand instructions to words while building instruction table
189 while ( instruction < header->instructionCount ) {
190 vm->instructionPointers[ instruction ] = int_pc;
191 instruction++;
192
193 op = (int)code[ byte_pc ];
194 codeBase[int_pc] = op;
195 if(byte_pc > header->codeLength)
196 Com_Error(ERR_DROP, "VM_PrepareInterpreter: pc > header->codeLength");
197
198 byte_pc++;
199 int_pc++;
200
201 // these are the only opcodes that aren't a single byte
202 switch ( op ) {
203 case OP_ENTER:
204 case OP_CONST:
205 case OP_LOCAL:
206 case OP_LEAVE:
207 case OP_EQ:
208 case OP_NE:
209 case OP_LTI:
210 case OP_LEI:
211 case OP_GTI:
212 case OP_GEI:
213 case OP_LTU:
214 case OP_LEU:
215 case OP_GTU:
216 case OP_GEU:
217 case OP_EQF:
218 case OP_NEF:
219 case OP_LTF:
220 case OP_LEF:
221 case OP_GTF:
222 case OP_GEF:
223 case OP_BLOCK_COPY:
224 codeBase[int_pc] = loadWord(&code[byte_pc]);
225 byte_pc += 4;
226 int_pc++;
227 break;
228 case OP_ARG:
229 codeBase[int_pc] = (int)code[byte_pc];
230 byte_pc++;
231 int_pc++;
232 break;
233 default:
234 break;
235 }
236
237 }
238 int_pc = 0;
239 instruction = 0;
240
241 // Now that the code has been expanded to int-sized opcodes, we'll translate instruction index
242 //into an index into codeBase[], which contains opcodes and operands.
243 while ( instruction < header->instructionCount ) {
244 op = codeBase[ int_pc ];
245 instruction++;
246 int_pc++;
247
248 switch ( op ) {
249 // These ops need to translate addresses in jumps from instruction index to int index
250 case OP_EQ:
251 case OP_NE:
252 case OP_LTI:
253 case OP_LEI:
254 case OP_GTI:
255 case OP_GEI:
256 case OP_LTU:
257 case OP_LEU:
258 case OP_GTU:
259 case OP_GEU:
260 case OP_EQF:
261 case OP_NEF:
262 case OP_LTF:
263 case OP_LEF:
264 case OP_GTF:
265 case OP_GEF:
266 if(codeBase[int_pc] < 0 || codeBase[int_pc] > vm->instructionCount)
267 Com_Error(ERR_DROP, "VM_PrepareInterpreter: Jump to invalid instruction number");
268
269 // codeBase[pc] is the instruction index. Convert that into an offset into
270 //the int-aligned codeBase[] by the lookup table.
271 codeBase[int_pc] = vm->instructionPointers[codeBase[int_pc]];
272 int_pc++;
273 break;
274
275 // These opcodes have an operand that isn't an instruction index
276 case OP_ENTER:
277 case OP_CONST:
278 case OP_LOCAL:
279 case OP_LEAVE:
280 case OP_BLOCK_COPY:
281 case OP_ARG:
282 int_pc++;
283 break;
284
285 default:
286 break;
287 }
288
289 }
290 }
291
292 /*
293 ==============
294 VM_Call
295
296
297 Upon a system call, the stack will look like:
298
299 sp+32 parm1
300 sp+28 parm0
301 sp+24 return stack
302 sp+20 return address
303 sp+16 local1
304 sp+14 local0
305 sp+12 arg1
306 sp+8 arg0
307 sp+4 return stack
308 sp return address
309
310 An interpreted function will immediately execute
311 an OP_ENTER instruction, which will subtract space for
312 locals from sp
313 ==============
314 */
315
316 #define DEBUGSTR va("%s%i", VM_Indent(vm), opStackOfs)
317
318 int VM_CallInterpreted( vm_t *vm, int *args ) {
319 byte stack[OPSTACK_SIZE + 15];
320 int *opStack;
321 uint8_t opStackOfs;
322 int programCounter;
323 int programStack;
324 int stackOnEntry;
325 byte *image;
326 int *codeImage;
327 int v1;
328 int dataMask;
329 int arg;
330 #ifdef DEBUG_VM
331 vmSymbol_t *profileSymbol;
332 #endif
333
334 // interpret the code
335 vm->currentlyInterpreting = qtrue;
336
337 // we might be called recursively, so this might not be the very top
338 programStack = stackOnEntry = vm->programStack;
339
340 #ifdef DEBUG_VM
341 profileSymbol = VM_ValueToFunctionSymbol( vm, 0 );
342 // uncomment this for debugging breakpoints
343 vm->breakFunction = 0;
344 #endif
345 // set up the stack frame
346
347 image = vm->dataBase;
348 codeImage = (int *)vm->codeBase;
349 dataMask = vm->dataMask;
350
351 programCounter = 0;
352
353 programStack -= ( 8 + 4 * MAX_VMMAIN_ARGS );
354
355 for ( arg = 0; arg < MAX_VMMAIN_ARGS; arg++ )
356 *(int *)&image[ programStack + 8 + arg * 4 ] = args[ arg ];
357
358 *(int *)&image[ programStack + 4 ] = 0; // return stack
359 *(int *)&image[ programStack ] = -1; // will terminate the loop on return
360
361 VM_Debug(0);
362
363 // leave a free spot at start of stack so
364 // that as long as opStack is valid, opStack-1 will
365 // not corrupt anything
366 opStack = PADP(stack, 16);
367 *opStack = 0xDEADBEEF;
368 opStackOfs = 0;
369
370 // vm_debugLevel=2;
371 // main interpreter loop, will exit when a LEAVE instruction
372 // grabs the -1 program counter
373
374 #define r2 codeImage[programCounter]
375
376 while ( 1 ) {
377 int opcode, r0, r1;
378 // unsigned int r2;
379
380 nextInstruction:
381 r0 = opStack[opStackOfs];
382 r1 = opStack[(uint8_t) (opStackOfs - 1)];
383 nextInstruction2:
384 #ifdef DEBUG_VM
385 if ( (unsigned)programCounter >= vm->codeLength ) {
386 Com_Error( ERR_DROP, "VM pc out of range" );
387 return 0;
388 }
389
390 if ( programStack <= vm->stackBottom ) {
391 Com_Error( ERR_DROP, "VM stack overflow" );
392 return 0;
393 }
394
395 if ( programStack & 3 ) {
396 Com_Error( ERR_DROP, "VM program stack misaligned" );
397 return 0;
398 }
399
400 if ( vm_debugLevel > 1 ) {
401 Com_Printf( "%s %s\n", DEBUGSTR, opnames[opcode] );
402 }
403 profileSymbol->profileCount++;
404 #endif
405 opcode = codeImage[ programCounter++ ];
406
407 switch ( opcode ) {
408 #ifdef DEBUG_VM
409 default:
410 Com_Error( ERR_DROP, "Bad VM instruction" ); // this should be scanned on load!
411 return 0;
412 #endif
413 case OP_BREAK:
414 vm->breakCount++;
415 goto nextInstruction2;
416 case OP_CONST:
417 opStackOfs++;
418 r1 = r0;
419 r0 = opStack[opStackOfs] = r2;
420
421 programCounter += 1;
422 goto nextInstruction2;
423 case OP_LOCAL:
424 opStackOfs++;
425 r1 = r0;
426 r0 = opStack[opStackOfs] = r2+programStack;
427
428 programCounter += 1;
429 goto nextInstruction2;
430
431 case OP_LOAD4:
432 #ifdef DEBUG_VM
433 if(opStack[opStackOfs] & 3)
434 {
435 Com_Error( ERR_DROP, "OP_LOAD4 misaligned" );
436 return 0;
437 }
438 #endif
439 r0 = opStack[opStackOfs] = *(int *) &image[ r0 & dataMask ];
440 goto nextInstruction2;
441 case OP_LOAD2:
442 r0 = opStack[opStackOfs] = *(unsigned short *)&image[ r0 & dataMask ];
443 goto nextInstruction2;
444 case OP_LOAD1:
445 r0 = opStack[opStackOfs] = image[ r0 & dataMask ];
446 goto nextInstruction2;
447
448 case OP_STORE4:
449 *(int *)&image[ r1 & dataMask ] = r0;
450 opStackOfs -= 2;
451 goto nextInstruction;
452 case OP_STORE2:
453 *(short *)&image[ r1 & dataMask ] = r0;
454 opStackOfs -= 2;
455 goto nextInstruction;
456 case OP_STORE1:
457 image[ r1 & dataMask ] = r0;
458 opStackOfs -= 2;
459 goto nextInstruction;
460
461 case OP_ARG:
462 // single byte offset from programStack
463 *(int *)&image[ (codeImage[programCounter] + programStack) & dataMask ] = r0;
464 opStackOfs--;
465 programCounter += 1;
466 goto nextInstruction;
467
468 case OP_BLOCK_COPY:
469 VM_BlockCopy(r1, r0, r2);
470 programCounter += 1;
471 opStackOfs -= 2;
472 goto nextInstruction;
473
474 case OP_CALL:
475 // save current program counter
476 *(int *)&image[ programStack ] = programCounter;
477
478 // jump to the location on the stack
479 programCounter = r0;
480 opStackOfs--;
481 if ( programCounter < 0 ) {
482 // system call
483 int r;
484 // int temp;
485 #ifdef DEBUG_VM
486 int stomped;
487
488 if ( vm_debugLevel ) {
489 Com_Printf( "%s---> systemcall(%i)\n", DEBUGSTR, -1 - programCounter );
490 }
491 #endif
492 // save the stack to allow recursive VM entry
493 // temp = vm->callLevel;
494 vm->programStack = programStack - 4;
495 #ifdef DEBUG_VM
496 stomped = *(int *)&image[ programStack + 4 ];
497 #endif
498 *(int *)&image[ programStack + 4 ] = -1 - programCounter;
499
500 //VM_LogSyscalls( (int *)&image[ programStack + 4 ] );
501 {
502 // the vm has ints on the stack, we expect
503 // pointers so we might have to convert it
504 if (sizeof(intptr_t) != sizeof(int)) {
505 intptr_t argarr[ MAX_VMSYSCALL_ARGS ];
506 int *imagePtr = (int *)&image[ programStack ];
507 int i;
508 for (i = 0; i < ARRAY_LEN(argarr); ++i) {
509 argarr[i] = *(++imagePtr);
510 }
511 r = vm->systemCall( argarr );
512 } else {
513 intptr_t* argptr = (intptr_t *)&image[ programStack + 4 ];
514 r = vm->systemCall( argptr );
515 }
516 }
517
518 #ifdef DEBUG_VM
519 // this is just our stack frame pointer, only needed
520 // for debugging
521 *(int *)&image[ programStack + 4 ] = stomped;
522 #endif
523
524 // save return value
525 opStackOfs++;
526 opStack[opStackOfs] = r;
527 programCounter = *(int *)&image[ programStack ];
528 // vm->callLevel = temp;
529 #ifdef DEBUG_VM
530 if ( vm_debugLevel ) {
531 Com_Printf( "%s<--- %s\n", DEBUGSTR, VM_ValueToSymbol( vm, programCounter ) );
532 }
533 #endif
534 } else if ( (unsigned)programCounter >= vm->instructionCount ) {
535 Com_Error( ERR_DROP, "VM program counter out of range in OP_CALL" );
536 return 0;
537 } else {
538 programCounter = vm->instructionPointers[ programCounter ];
539 }
540 goto nextInstruction;
541
542 // push and pop are only needed for discarded or bad function return values
543 case OP_PUSH:
544 opStackOfs++;
545 goto nextInstruction;
546 case OP_POP:
547 opStackOfs--;
548 goto nextInstruction;
549
550 case OP_ENTER:
551 #ifdef DEBUG_VM
552 profileSymbol = VM_ValueToFunctionSymbol( vm, programCounter );
553 #endif
554 // get size of stack frame
555 v1 = r2;
556
557 programCounter += 1;
558 programStack -= v1;
559 #ifdef DEBUG_VM
560 // save old stack frame for debugging traces
561 *(int *)&image[programStack+4] = programStack + v1;
562 if ( vm_debugLevel ) {
563 Com_Printf( "%s---> %s\n", DEBUGSTR, VM_ValueToSymbol( vm, programCounter - 5 ) );
564 if ( vm->breakFunction && programCounter - 5 == vm->breakFunction ) {
565 // this is to allow setting breakpoints here in the debugger
566 vm->breakCount++;
567 // vm_debugLevel = 2;
568 // VM_StackTrace( vm, programCounter, programStack );
569 }
570 // vm->callLevel++;
571 }
572 #endif
573 goto nextInstruction;
574 case OP_LEAVE:
575 // remove our stack frame
576 v1 = r2;
577
578 programStack += v1;
579
580 // grab the saved program counter
581 programCounter = *(int *)&image[ programStack ];
582 #ifdef DEBUG_VM
583 profileSymbol = VM_ValueToFunctionSymbol( vm, programCounter );
584 if ( vm_debugLevel ) {
585 // vm->callLevel--;
586 Com_Printf( "%s<--- %s\n", DEBUGSTR, VM_ValueToSymbol( vm, programCounter ) );
587 }
588 #endif
589 // check for leaving the VM
590 if ( programCounter == -1 ) {
591 goto done;
592 } else if ( (unsigned)programCounter >= vm->codeLength ) {
593 Com_Error( ERR_DROP, "VM program counter out of range in OP_LEAVE" );
594 return 0;
595 }
596 goto nextInstruction;
597
598 /*
599 ===================================================================
600 BRANCHES
601 ===================================================================
602 */
603
604 case OP_JUMP:
605 if ( (unsigned)r0 >= vm->instructionCount )
606 {
607 Com_Error( ERR_DROP, "VM program counter out of range in OP_JUMP" );
608 return 0;
609 }
610
611 programCounter = vm->instructionPointers[ r0 ];
612
613 opStackOfs--;
614 goto nextInstruction;
615
616 case OP_EQ:
617 opStackOfs -= 2;
618 if ( r1 == r0 ) {
619 programCounter = r2; //vm->instructionPointers[r2];
620 goto nextInstruction;
621 } else {
622 programCounter += 1;
623 goto nextInstruction;
624 }
625
626 case OP_NE:
627 opStackOfs -= 2;
628 if ( r1 != r0 ) {
629 programCounter = r2; //vm->instructionPointers[r2];
630 goto nextInstruction;
631 } else {
632 programCounter += 1;
633 goto nextInstruction;
634 }
635
636 case OP_LTI:
637 opStackOfs -= 2;
638 if ( r1 < r0 ) {
639 programCounter = r2; //vm->instructionPointers[r2];
640 goto nextInstruction;
641 } else {
642 programCounter += 1;
643 goto nextInstruction;
644 }
645
646 case OP_LEI:
647 opStackOfs -= 2;
648 if ( r1 <= r0 ) {
649 programCounter = r2; //vm->instructionPointers[r2];
650 goto nextInstruction;
651 } else {
652 programCounter += 1;
653 goto nextInstruction;
654 }
655
656 case OP_GTI:
657 opStackOfs -= 2;
658 if ( r1 > r0 ) {
659 programCounter = r2; //vm->instructionPointers[r2];
660 goto nextInstruction;
661 } else {
662 programCounter += 1;
663 goto nextInstruction;
664 }
665
666 case OP_GEI:
667 opStackOfs -= 2;
668 if ( r1 >= r0 ) {
669 programCounter = r2; //vm->instructionPointers[r2];
670 goto nextInstruction;
671 } else {
672 programCounter += 1;
673 goto nextInstruction;
674 }
675
676 case OP_LTU:
677 opStackOfs -= 2;
678 if ( ((unsigned)r1) < ((unsigned)r0) ) {
679 programCounter = r2; //vm->instructionPointers[r2];
680 goto nextInstruction;
681 } else {
682 programCounter += 1;
683 goto nextInstruction;
684 }
685
686 case OP_LEU:
687 opStackOfs -= 2;
688 if ( ((unsigned)r1) <= ((unsigned)r0) ) {
689 programCounter = r2; //vm->instructionPointers[r2];
690 goto nextInstruction;
691 } else {
692 programCounter += 1;
693 goto nextInstruction;
694 }
695
696 case OP_GTU:
697 opStackOfs -= 2;
698 if ( ((unsigned)r1) > ((unsigned)r0) ) {
699 programCounter = r2; //vm->instructionPointers[r2];
700 goto nextInstruction;
701 } else {
702 programCounter += 1;
703 goto nextInstruction;
704 }
705
706 case OP_GEU:
707 opStackOfs -= 2;
708 if ( ((unsigned)r1) >= ((unsigned)r0) ) {
709 programCounter = r2; //vm->instructionPointers[r2];
710 goto nextInstruction;
711 } else {
712 programCounter += 1;
713 goto nextInstruction;
714 }
715
716 case OP_EQF:
717 opStackOfs -= 2;
718
719 if(((float *) opStack)[(uint8_t) (opStackOfs + 1)] == ((float *) opStack)[(uint8_t) (opStackOfs + 2)])
720 {
721 programCounter = r2; //vm->instructionPointers[r2];
722 goto nextInstruction;
723 } else {
724 programCounter += 1;
725 goto nextInstruction;
726 }
727
728 case OP_NEF:
729 opStackOfs -= 2;
730
731 if(((float *) opStack)[(uint8_t) (opStackOfs + 1)] != ((float *) opStack)[(uint8_t) (opStackOfs + 2)])
732 {
733 programCounter = r2; //vm->instructionPointers[r2];
734 goto nextInstruction;
735 } else {
736 programCounter += 1;
737 goto nextInstruction;
738 }
739
740 case OP_LTF:
741 opStackOfs -= 2;
742
743 if(((float *) opStack)[(uint8_t) (opStackOfs + 1)] < ((float *) opStack)[(uint8_t) (opStackOfs + 2)])
744 {
745 programCounter = r2; //vm->instructionPointers[r2];
746 goto nextInstruction;
747 } else {
748 programCounter += 1;
749 goto nextInstruction;
750 }
751
752 case OP_LEF:
753 opStackOfs -= 2;
754
755 if(((float *) opStack)[(uint8_t) ((uint8_t) (opStackOfs + 1))] <= ((float *) opStack)[(uint8_t) ((uint8_t) (opStackOfs + 2))])
756 {
757 programCounter = r2; //vm->instructionPointers[r2];
758 goto nextInstruction;
759 } else {
760 programCounter += 1;
761 goto nextInstruction;
762 }
763
764 case OP_GTF:
765 opStackOfs -= 2;
766
767 if(((float *) opStack)[(uint8_t) (opStackOfs + 1)] > ((float *) opStack)[(uint8_t) (opStackOfs + 2)])
768 {
769 programCounter = r2; //vm->instructionPointers[r2];
770 goto nextInstruction;
771 } else {
772 programCounter += 1;
773 goto nextInstruction;
774 }
775
776 case OP_GEF:
777 opStackOfs -= 2;
778
779 if(((float *) opStack)[(uint8_t) (opStackOfs + 1)] >= ((float *) opStack)[(uint8_t) (opStackOfs + 2)])
780 {
781 programCounter = r2; //vm->instructionPointers[r2];
782 goto nextInstruction;
783 } else {
784 programCounter += 1;
785 goto nextInstruction;
786 }
787
788
789 //===================================================================
790
791 case OP_NEGI:
792 opStack[opStackOfs] = -r0;
793 goto nextInstruction;
794 case OP_ADD:
795 opStackOfs--;
796 opStack[opStackOfs] = r1 + r0;
797 goto nextInstruction;
798 case OP_SUB:
799 opStackOfs--;
800 opStack[opStackOfs] = r1 - r0;
801 goto nextInstruction;
802 case OP_DIVI:
803 opStackOfs--;
804 opStack[opStackOfs] = r1 / r0;
805 goto nextInstruction;
806 case OP_DIVU:
807 opStackOfs--;
808 opStack[opStackOfs] = ((unsigned) r1) / ((unsigned) r0);
809 goto nextInstruction;
810 case OP_MODI:
811 opStackOfs--;
812 opStack[opStackOfs] = r1 % r0;
813 goto nextInstruction;
814 case OP_MODU:
815 opStackOfs--;
816 opStack[opStackOfs] = ((unsigned) r1) % ((unsigned) r0);
817 goto nextInstruction;
818 case OP_MULI:
819 opStackOfs--;
820 opStack[opStackOfs] = r1 * r0;
821 goto nextInstruction;
822 case OP_MULU:
823 opStackOfs--;
824 opStack[opStackOfs] = ((unsigned) r1) * ((unsigned) r0);
825 goto nextInstruction;
826
827 case OP_BAND:
828 opStackOfs--;
829 opStack[opStackOfs] = ((unsigned) r1) & ((unsigned) r0);
830 goto nextInstruction;
831 case OP_BOR:
832 opStackOfs--;
833 opStack[opStackOfs] = ((unsigned) r1) | ((unsigned) r0);
834 goto nextInstruction;
835 case OP_BXOR:
836 opStackOfs--;
837 opStack[opStackOfs] = ((unsigned) r1) ^ ((unsigned) r0);
838 goto nextInstruction;
839 case OP_BCOM:
840 opStack[opStackOfs] = ~((unsigned) r0);
841 goto nextInstruction;
842
843 case OP_LSH:
844 opStackOfs--;
845 opStack[opStackOfs] = r1 << r0;
846 goto nextInstruction;
847 case OP_RSHI:
848 opStackOfs--;
849 opStack[opStackOfs] = r1 >> r0;
850 goto nextInstruction;
851 case OP_RSHU:
852 opStackOfs--;
853 opStack[opStackOfs] = ((unsigned) r1) >> r0;
854 goto nextInstruction;
855
856 case OP_NEGF:
857 ((float *) opStack)[opStackOfs] = -((float *) opStack)[opStackOfs];
858 goto nextInstruction;
859 case OP_ADDF:
860 opStackOfs--;
861 ((float *) opStack)[opStackOfs] = ((float *) opStack)[opStackOfs] + ((float *) opStack)[(uint8_t) (opStackOfs + 1)];
862 goto nextInstruction;
863 case OP_SUBF:
864 opStackOfs--;
865 ((float *) opStack)[opStackOfs] = ((float *) opStack)[opStackOfs] - ((float *) opStack)[(uint8_t) (opStackOfs + 1)];
866 goto nextInstruction;
867 case OP_DIVF:
868 opStackOfs--;
869 ((float *) opStack)[opStackOfs] = ((float *) opStack)[opStackOfs] / ((float *) opStack)[(uint8_t) (opStackOfs + 1)];
870 goto nextInstruction;
871 case OP_MULF:
872 opStackOfs--;
873 ((float *) opStack)[opStackOfs] = ((float *) opStack)[opStackOfs] * ((float *) opStack)[(uint8_t) (opStackOfs + 1)];
874 goto nextInstruction;
875
876 case OP_CVIF:
877 ((float *) opStack)[opStackOfs] = (float) opStack[opStackOfs];
878 goto nextInstruction;
879 case OP_CVFI:
880 opStack[opStackOfs] = Q_ftol(((float *) opStack)[opStackOfs]);
881 goto nextInstruction;
882 case OP_SEX8:
883 opStack[opStackOfs] = (signed char) opStack[opStackOfs];
884 goto nextInstruction;
885 case OP_SEX16:
886 opStack[opStackOfs] = (short) opStack[opStackOfs];
887 goto nextInstruction;
888 }
889 }
890
891 done:
892 vm->currentlyInterpreting = qfalse;
893
894 if (opStackOfs != 1 || *opStack != 0xDEADBEEF)
895 Com_Error(ERR_DROP, "Interpreter error: opStack[0] = %X, opStackOfs = %d", opStack[0], opStackOfs);
896
897 vm->programStack = stackOnEntry;
898
899 // return the result
900 return opStack[opStackOfs];
901 }