]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - prvm_exec.c
changed a ton of developer cvar checks into developer_extra/insane
[xonotic/darkplaces.git] / prvm_exec.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22 #include "progsvm.h"
23
24 char *prvm_opnames[] =
25 {
26 "^5DONE",
27
28 "MUL_F",
29 "MUL_V",
30 "MUL_FV",
31 "MUL_VF",
32
33 "DIV",
34
35 "ADD_F",
36 "ADD_V",
37
38 "SUB_F",
39 "SUB_V",
40
41 "^2EQ_F",
42 "^2EQ_V",
43 "^2EQ_S",
44 "^2EQ_E",
45 "^2EQ_FNC",
46
47 "^2NE_F",
48 "^2NE_V",
49 "^2NE_S",
50 "^2NE_E",
51 "^2NE_FNC",
52
53 "^2LE",
54 "^2GE",
55 "^2LT",
56 "^2GT",
57
58 "^6FIELD_F",
59 "^6FIELD_V",
60 "^6FIELD_S",
61 "^6FIELD_ENT",
62 "^6FIELD_FLD",
63 "^6FIELD_FNC",
64
65 "^1ADDRESS",
66
67 "STORE_F",
68 "STORE_V",
69 "STORE_S",
70 "STORE_ENT",
71 "STORE_FLD",
72 "STORE_FNC",
73
74 "^1STOREP_F",
75 "^1STOREP_V",
76 "^1STOREP_S",
77 "^1STOREP_ENT",
78 "^1STOREP_FLD",
79 "^1STOREP_FNC",
80
81 "^5RETURN",
82
83 "^2NOT_F",
84 "^2NOT_V",
85 "^2NOT_S",
86 "^2NOT_ENT",
87 "^2NOT_FNC",
88
89 "^5IF",
90 "^5IFNOT",
91
92 "^3CALL0",
93 "^3CALL1",
94 "^3CALL2",
95 "^3CALL3",
96 "^3CALL4",
97 "^3CALL5",
98 "^3CALL6",
99 "^3CALL7",
100 "^3CALL8",
101
102 "^1STATE",
103
104 "^5GOTO",
105
106 "^2AND",
107 "^2OR",
108
109 "BITAND",
110 "BITOR"
111 };
112
113 char *PRVM_GlobalString (int ofs);
114 char *PRVM_GlobalStringNoContents (int ofs);
115 extern ddef_t *PRVM_ED_FieldAtOfs(int ofs);
116
117
118 //=============================================================================
119
120 /*
121 =================
122 PRVM_PrintStatement
123 =================
124 */
125 extern cvar_t prvm_statementprofiling;
126 void PRVM_PrintStatement (dstatement_t *s)
127 {
128         size_t i;
129         int opnum = (int)(s - prog->statements);
130
131         Con_Printf("s%i: ", opnum);
132         if( prog->statement_linenums )
133                 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
134
135         if (prvm_statementprofiling.integer)
136                 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
137
138         if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
139         {
140                 Con_Printf("%s ",  prvm_opnames[s->op]);
141                 i = strlen(prvm_opnames[s->op]);
142                 // don't count a preceding color tag when padding the name
143                 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
144                         i -= 2;
145                 for ( ; i<10 ; i++)
146                         Con_Print(" ");
147         }
148         if (s->op == OP_IF || s->op == OP_IFNOT)
149                 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
150         else if (s->op == OP_GOTO)
151                 Con_Printf("s%i",(signed short)s->a + opnum);
152         else if ( (unsigned)(s->op - OP_STORE_F) < 6)
153         {
154                 Con_Print(PRVM_GlobalString((unsigned short) s->a));
155                 Con_Print(", ");
156                 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
157         }
158         else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
159         {
160                 if (s->a)
161                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
162                 if (s->b)
163                 {
164                         Con_Print(", ");
165                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
166                 }
167                 if (s->c)
168                 {
169                         Con_Print(", ");
170                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
171                 }
172         }
173         else
174         {
175                 if (s->a)
176                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
177                 if (s->b)
178                 {
179                         Con_Print(", ");
180                         Con_Print(PRVM_GlobalString((unsigned short) s->b));
181                 }
182                 if (s->c)
183                 {
184                         Con_Print(", ");
185                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
186                 }
187         }
188         Con_Print("\n");
189 }
190
191 void PRVM_PrintFunctionStatements (const char *name)
192 {
193         int i, firststatement, endstatement;
194         mfunction_t *func;
195         func = PRVM_ED_FindFunction (name);
196         if (!func)
197         {
198                 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
199                 return;
200         }
201         firststatement = func->first_statement;
202         if (firststatement < 0)
203         {
204                 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
205                 return;
206         }
207
208         // find the end statement
209         endstatement = prog->progs->numstatements;
210         for (i = 0;i < prog->progs->numfunctions;i++)
211                 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
212                         endstatement = prog->functions[i].first_statement;
213
214         // now print the range of statements
215         Con_Printf("%s progs: disassembly of function %s (statements %i-%i, locals %i-%i):\n", PRVM_NAME, name, firststatement, endstatement, func->parm_start, func->parm_start + func->locals - 1);
216         for (i = firststatement;i < endstatement;i++)
217         {
218                 PRVM_PrintStatement(prog->statements + i);
219                 prog->statement_profile[i] = 0;
220         }
221 }
222
223 /*
224 ============
225 PRVM_PrintFunction_f
226
227 ============
228 */
229 void PRVM_PrintFunction_f (void)
230 {
231         if (Cmd_Argc() != 3)
232         {
233                 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
234                 return;
235         }
236
237         PRVM_Begin;
238         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
239                 return;
240
241         PRVM_PrintFunctionStatements(Cmd_Argv(2));
242
243         PRVM_End;
244 }
245
246 /*
247 ============
248 PRVM_StackTrace
249 ============
250 */
251 void PRVM_StackTrace (void)
252 {
253         mfunction_t     *f;
254         int                     i;
255
256         prog->stack[prog->depth].s = prog->xstatement;
257         prog->stack[prog->depth].f = prog->xfunction;
258         for (i = prog->depth;i > 0;i--)
259         {
260                 f = prog->stack[i].f;
261
262                 if (!f)
263                         Con_Print("<NULL FUNCTION>\n");
264                 else
265                         Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
266         }
267 }
268
269 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
270 {
271         mfunction_t     *f;
272         int                     i;
273
274         if(prog)
275         {
276                 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
277         }
278         else
279         {
280                 strlcpy(buf, "<NO PROG>", bufsize);
281                 return;
282         }
283
284         prog->stack[prog->depth].s = prog->xstatement;
285         prog->stack[prog->depth].f = prog->xfunction;
286         for (i = prog->depth;i > 0;i--)
287         {
288                 f = prog->stack[i].f;
289
290                 if(strlcat(buf,
291                         f
292                                 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
293                                 : "<NULL> ",
294                         bufsize
295                 ) >= bufsize)
296                         break;
297         }
298 }
299
300
301 void PRVM_CallProfile (void)
302 {
303         mfunction_t *f, *best;
304         int i;
305         double max;
306         double sum;
307
308         Con_Printf( "%s Call Profile:\n", PRVM_NAME );
309
310         sum = 0;
311         do
312         {
313                 max = 0;
314                 best = NULL;
315                 for (i=0 ; i<prog->progs->numfunctions ; i++)
316                 {
317                         f = &prog->functions[i];
318                         if (max < f->totaltime)
319                         {
320                                 max = f->totaltime;
321                                 best = f;
322                         }
323                 }
324                 if (best)
325                 {
326                         sum += best->totaltime;
327                         Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
328                         best->totaltime = 0;
329                 }
330         } while (best);
331
332         Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
333         Con_Printf("       - used by QC code of this VM: %9.4f\n", sum);
334
335         prog->starttime = Sys_DoubleTime();
336 }
337
338 void PRVM_Profile (int maxfunctions, int mininstructions, int sortby)
339 {
340         mfunction_t *f, *best;
341         int i, num;
342         double max;
343
344         Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
345         //                        12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
346
347         num = 0;
348         do
349         {
350                 max = 0;
351                 best = NULL;
352                 for (i=0 ; i<prog->progs->numfunctions ; i++)
353                 {
354                         f = &prog->functions[i];
355                         if(sortby)
356                         {
357                                 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
358                                 {
359                                         max = f->profile_total + f->builtinsprofile_total + f->callcount;
360                                         best = f;
361                                 }
362                         }
363                         else
364                         {
365                                 if (max < f->profile + f->builtinsprofile + f->callcount)
366                                 {
367                                         max = f->profile + f->builtinsprofile + f->callcount;
368                                         best = f;
369                                 }
370                         }
371                 }
372                 if (best)
373                 {
374                         if (num < maxfunctions && max >= mininstructions)
375                         {
376                                 if (best->first_statement < 0)
377                                         Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
378                                         //                 12345678901 12345678901 12345678901 12345678901 123.45%
379                                 else
380                                         Con_Printf("%11.0f %11.0f %11.0f %11.0f %11.0f %6.2f%% %s\n", best->callcount, best->profile, best->builtinsprofile, best->profile_total, best->builtinsprofile_total, (best->profile + best->builtinsprofile) * 100.0 / (best->profile_total + best->builtinsprofile_total), PRVM_GetString(best->s_name));
381                         }
382                         num++;
383                         best->profile = 0;
384                         best->builtinsprofile = 0;
385                         best->profile_total = 0;
386                         best->builtinsprofile_total = 0;
387                         best->callcount = 0;
388                 }
389         } while (best);
390 }
391
392 /*
393 ============
394 PRVM_CallProfile_f
395
396 ============
397 */
398 void PRVM_CallProfile_f (void)
399 {
400         if (Cmd_Argc() != 2)
401         {
402                 Con_Print("prvm_callprofile <program name>\n");
403                 return;
404         }
405
406         PRVM_Begin;
407         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
408                 return;
409
410         PRVM_CallProfile();
411
412         PRVM_End;
413 }
414
415 /*
416 ============
417 PRVM_Profile_f
418
419 ============
420 */
421 void PRVM_Profile_f (void)
422 {
423         int howmany;
424
425         howmany = 1<<30;
426         if (Cmd_Argc() == 3)
427                 howmany = atoi(Cmd_Argv(2));
428         else if (Cmd_Argc() != 2)
429         {
430                 Con_Print("prvm_profile <program name>\n");
431                 return;
432         }
433
434         PRVM_Begin;
435         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
436                 return;
437
438         PRVM_Profile(howmany, 1, 0);
439
440         PRVM_End;
441 }
442
443 void PRVM_ChildProfile_f (void)
444 {
445         int howmany;
446
447         howmany = 1<<30;
448         if (Cmd_Argc() == 3)
449                 howmany = atoi(Cmd_Argv(2));
450         else if (Cmd_Argc() != 2)
451         {
452                 Con_Print("prvm_childprofile <program name>\n");
453                 return;
454         }
455
456         PRVM_Begin;
457         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
458                 return;
459
460         PRVM_Profile(howmany, 1, 1);
461
462         PRVM_End;
463 }
464
465 void PRVM_CrashAll(void)
466 {
467         int i;
468         prvm_prog_t *oldprog = prog;
469
470         for(i = 0; i < PRVM_MAXPROGS; i++)
471         {
472                 if(!PRVM_ProgLoaded(i))
473                         continue;
474                 PRVM_SetProg(i);
475                 PRVM_Crash();
476         }
477
478         prog = oldprog;
479 }
480
481 void PRVM_PrintState(void)
482 {
483         int i;
484         if(prog->statestring)
485         {
486                 Con_Printf("Caller-provided information: %s\n", prog->statestring);
487         }
488         if (prog->xfunction)
489         {
490                 for (i = -7; i <= 0;i++)
491                         if (prog->xstatement + i >= prog->xfunction->first_statement)
492                                 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
493         }
494         else
495                 Con_Print("null function executing??\n");
496         PRVM_StackTrace ();
497 }
498
499 extern sizebuf_t vm_tempstringsbuf;
500 extern cvar_t prvm_errordump;
501 void Host_Savegame_to (const char *name);
502 void PRVM_Crash(void)
503 {
504         if (prog == NULL)
505                 return;
506
507         prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
508
509         if( prog->depth > 0 )
510         {
511                 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
512                 PRVM_PrintState();
513         }
514
515         if(prvm_errordump.integer)
516         {
517                 // make a savegame
518                 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
519         }
520
521         // dump the stack so host_error can shutdown functions
522         prog->depth = 0;
523         prog->localstack_used = 0;
524
525         // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
526         vm_tempstringsbuf.cursize = 0;
527
528         // reset the prog pointer
529         prog = NULL;
530 }
531
532 /*
533 ============================================================================
534 PRVM_ExecuteProgram
535
536 The interpretation main loop
537 ============================================================================
538 */
539
540 /*
541 ====================
542 PRVM_EnterFunction
543
544 Returns the new program statement counter
545 ====================
546 */
547 int PRVM_EnterFunction (mfunction_t *f)
548 {
549         int             i, j, c, o;
550
551         if (!f)
552                 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
553
554         prog->stack[prog->depth].s = prog->xstatement;
555         prog->stack[prog->depth].f = prog->xfunction;
556         prog->stack[prog->depth].profile_acc = -f->profile;
557         prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
558         prog->depth++;
559         if (prog->depth >=PRVM_MAX_STACK_DEPTH)
560                 PRVM_ERROR ("stack overflow");
561
562 // save off any locals that the new function steps on
563         c = f->locals;
564         if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
565                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
566
567         for (i=0 ; i < c ; i++)
568                 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
569         prog->localstack_used += c;
570
571 // copy parameters
572         o = f->parm_start;
573         for (i=0 ; i<f->numparms ; i++)
574         {
575                 for (j=0 ; j<f->parm_size[i] ; j++)
576                 {
577                         ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
578                         o++;
579                 }
580         }
581
582         ++f->recursion;
583         prog->xfunction = f;
584         return f->first_statement - 1;  // offset the s++
585 }
586
587 /*
588 ====================
589 PRVM_LeaveFunction
590 ====================
591 */
592 int PRVM_LeaveFunction (void)
593 {
594         int             i, c;
595         mfunction_t *f;
596
597         if (prog->depth <= 0)
598                 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
599
600         if (!prog->xfunction)
601                 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
602 // restore locals from the stack
603         c = prog->xfunction->locals;
604         prog->localstack_used -= c;
605         if (prog->localstack_used < 0)
606                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
607
608         for (i=0 ; i < c ; i++)
609                 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
610
611 // up stack
612         prog->depth--;
613         f = prog->xfunction;
614         --f->recursion;
615         prog->xfunction = prog->stack[prog->depth].f;
616         prog->stack[prog->depth].profile_acc += f->profile;
617         prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
618         if(prog->depth > 0)
619         {
620                 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
621                 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
622         }
623         if(!f->recursion)
624         {
625                 // if f is already on the call stack...
626                 // we cannot add this profile data to it now
627                 // or we would add it more than once
628                 // so, let's only add to the function's profile if it is the outermost call
629                 f->profile_total += prog->stack[prog->depth].profile_acc;
630                 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
631         }
632         
633         return prog->stack[prog->depth].s;
634 }
635
636 void PRVM_Init_Exec(void)
637 {
638         // dump the stack
639         prog->depth = 0;
640         prog->localstack_used = 0;
641         // reset the string table
642         // nothing here yet
643 }
644
645 /*
646 ====================
647 MVM_ExecuteProgram
648 ====================
649 */
650 // LordHavoc: optimized
651 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
652 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
653 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
654 extern cvar_t prvm_traceqc;
655 extern cvar_t prvm_statementprofiling;
656 extern sizebuf_t vm_tempstringsbuf;
657 extern qboolean prvm_runawaycheck;
658 extern qboolean prvm_boundscheck;
659 void MVM_ExecuteProgram (func_t fnum, const char *errormessage)
660 {
661         dstatement_t    *st, *startst;
662         mfunction_t     *f, *newf;
663         prvm_edict_t    *ed;
664         prvm_eval_t     *ptr;
665         int             jumpcount, cachedpr_trace, exitdepth;
666         int             restorevm_tempstringsbuf_cursize;
667         double  calltime;
668
669         calltime = Sys_DoubleTime();
670
671         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
672         {
673                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
674                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
675                 PRVM_ERROR ("MVM_ExecuteProgram: %s", errormessage);
676         }
677
678         f = &prog->functions[fnum];
679
680         // after executing this function, delete all tempstrings it created
681         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
682
683         prog->trace = prvm_traceqc.integer;
684
685         // we know we're done when pr_depth drops to this
686         exitdepth = prog->depth;
687
688 // make a stack frame
689         st = &prog->statements[PRVM_EnterFunction (f)];
690         // save the starting statement pointer for profiling
691         // (when the function exits or jumps, the (st - startst) integer value is
692         // added to the function's profile counter)
693         startst = st;
694         // instead of counting instructions, we count jumps
695         jumpcount = 0;
696         // add one to the callcount of this function because otherwise engine-called functions aren't counted
697         prog->xfunction->callcount++;
698
699 chooseexecprogram:
700         cachedpr_trace = prog->trace;
701         if (prvm_runawaycheck)
702         {
703 #define PRVMRUNAWAYCHECK 1
704                 if (prvm_statementprofiling.integer)
705                 {
706 #define PRVMSTATEMENTPROFILING 1
707                         if (prvm_boundscheck)
708                         {
709 #define PRVMBOUNDSCHECK 1
710                                 if (prog->trace)
711                                 {
712 #define PRVMTRACE 1
713 #include "prvm_execprogram.h"
714 #undef PRVMTRACE
715                                 }
716                                 else
717                                 {
718 #include "prvm_execprogram.h"
719                                 }
720 #undef PRVMBOUNDSCHECK
721                         }
722                         else
723                         {
724                                 if (prog->trace)
725                                 {
726 #define PRVMTRACE 1
727 #include "prvm_execprogram.h"
728 #undef PRVMTRACE
729                                 }
730                                 else
731                                 {
732 #include "prvm_execprogram.h"
733                                 }
734                         }
735 #undef PRVMSTATEMENTPROFILING
736                 }
737                 else
738                 {
739                         if (prvm_boundscheck)
740                         {
741 #define PRVMBOUNDSCHECK 1
742                                 if (prog->trace)
743                                 {
744 #define PRVMTRACE 1
745 #include "prvm_execprogram.h"
746 #undef PRVMTRACE
747                                 }
748                                 else
749                                 {
750 #include "prvm_execprogram.h"
751                                 }
752 #undef PRVMBOUNDSCHECK
753                         }
754                         else
755                         {
756                                 if (prog->trace)
757                                 {
758 #define PRVMTRACE 1
759 #include "prvm_execprogram.h"
760 #undef PRVMTRACE
761                                 }
762                                 else
763                                 {
764 #include "prvm_execprogram.h"
765                                 }
766                         }
767                 }
768 #undef PRVMRUNAWAYCHECK
769         }
770         else
771         {
772                 if (prvm_statementprofiling.integer)
773                 {
774 #define PRVMSTATEMENTPROFILING 1
775                         if (prvm_boundscheck)
776                         {
777 #define PRVMBOUNDSCHECK 1
778                                 if (prog->trace)
779                                 {
780 #define PRVMTRACE 1
781 #include "prvm_execprogram.h"
782 #undef PRVMTRACE
783                                 }
784                                 else
785                                 {
786 #include "prvm_execprogram.h"
787                                 }
788 #undef PRVMBOUNDSCHECK
789                         }
790                         else
791                         {
792                                 if (prog->trace)
793                                 {
794 #define PRVMTRACE 1
795 #include "prvm_execprogram.h"
796 #undef PRVMTRACE
797                                 }
798                                 else
799                                 {
800 #include "prvm_execprogram.h"
801                                 }
802                         }
803 #undef PRVMSTATEMENTPROFILING
804                 }
805                 else
806                 {
807                         if (prvm_boundscheck)
808                         {
809 #define PRVMBOUNDSCHECK 1
810                                 if (prog->trace)
811                                 {
812 #define PRVMTRACE 1
813 #include "prvm_execprogram.h"
814 #undef PRVMTRACE
815                                 }
816                                 else
817                                 {
818 #include "prvm_execprogram.h"
819                                 }
820 #undef PRVMBOUNDSCHECK
821                         }
822                         else
823                         {
824                                 if (prog->trace)
825                                 {
826 #define PRVMTRACE 1
827 #include "prvm_execprogram.h"
828 #undef PRVMTRACE
829                                 }
830                                 else
831                                 {
832 #include "prvm_execprogram.h"
833                                 }
834                         }
835                 }
836         }
837
838 cleanup:
839         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
840                 Con_DPrintf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
841         // delete tempstrings created by this function
842         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
843
844         f->totaltime += (Sys_DoubleTime() - calltime);
845
846         SV_FlushBroadcastMessages();
847 }
848
849 /*
850 ====================
851 CLVM_ExecuteProgram
852 ====================
853 */
854 // LordHavoc: optimized
855 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
856 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
857 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
858 extern cvar_t prvm_traceqc;
859 extern cvar_t prvm_statementprofiling;
860 extern sizebuf_t vm_tempstringsbuf;
861 extern qboolean prvm_runawaycheck;
862 extern qboolean prvm_boundscheck;
863 void CLVM_ExecuteProgram (func_t fnum, const char *errormessage)
864 {
865         dstatement_t    *st, *startst;
866         mfunction_t     *f, *newf;
867         prvm_edict_t    *ed;
868         prvm_eval_t     *ptr;
869         int             jumpcount, cachedpr_trace, exitdepth;
870         int             restorevm_tempstringsbuf_cursize;
871         double  calltime;
872
873         calltime = Sys_DoubleTime();
874
875         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
876         {
877                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
878                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
879                 PRVM_ERROR ("CLVM_ExecuteProgram: %s", errormessage);
880         }
881
882         f = &prog->functions[fnum];
883
884         // after executing this function, delete all tempstrings it created
885         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
886
887         prog->trace = prvm_traceqc.integer;
888
889         // we know we're done when pr_depth drops to this
890         exitdepth = prog->depth;
891
892 // make a stack frame
893         st = &prog->statements[PRVM_EnterFunction (f)];
894         // save the starting statement pointer for profiling
895         // (when the function exits or jumps, the (st - startst) integer value is
896         // added to the function's profile counter)
897         startst = st;
898         // instead of counting instructions, we count jumps
899         jumpcount = 0;
900         // add one to the callcount of this function because otherwise engine-called functions aren't counted
901         prog->xfunction->callcount++;
902
903 chooseexecprogram:
904         cachedpr_trace = prog->trace;
905         if (prvm_runawaycheck)
906         {
907 #define PRVMRUNAWAYCHECK 1
908                 if (prvm_statementprofiling.integer)
909                 {
910 #define PRVMSTATEMENTPROFILING 1
911                         if (prvm_boundscheck)
912                         {
913 #define PRVMBOUNDSCHECK 1
914                                 if (prog->trace)
915                                 {
916 #define PRVMTRACE 1
917 #include "prvm_execprogram.h"
918 #undef PRVMTRACE
919                                 }
920                                 else
921                                 {
922 #include "prvm_execprogram.h"
923                                 }
924 #undef PRVMBOUNDSCHECK
925                         }
926                         else
927                         {
928                                 if (prog->trace)
929                                 {
930 #define PRVMTRACE 1
931 #include "prvm_execprogram.h"
932 #undef PRVMTRACE
933                                 }
934                                 else
935                                 {
936 #include "prvm_execprogram.h"
937                                 }
938                         }
939 #undef PRVMSTATEMENTPROFILING
940                 }
941                 else
942                 {
943                         if (prvm_boundscheck)
944                         {
945 #define PRVMBOUNDSCHECK 1
946                                 if (prog->trace)
947                                 {
948 #define PRVMTRACE 1
949 #include "prvm_execprogram.h"
950 #undef PRVMTRACE
951                                 }
952                                 else
953                                 {
954 #include "prvm_execprogram.h"
955                                 }
956 #undef PRVMBOUNDSCHECK
957                         }
958                         else
959                         {
960                                 if (prog->trace)
961                                 {
962 #define PRVMTRACE 1
963 #include "prvm_execprogram.h"
964 #undef PRVMTRACE
965                                 }
966                                 else
967                                 {
968 #include "prvm_execprogram.h"
969                                 }
970                         }
971                 }
972 #undef PRVMRUNAWAYCHECK
973         }
974         else
975         {
976                 if (prvm_statementprofiling.integer)
977                 {
978 #define PRVMSTATEMENTPROFILING 1
979                         if (prvm_boundscheck)
980                         {
981 #define PRVMBOUNDSCHECK 1
982                                 if (prog->trace)
983                                 {
984 #define PRVMTRACE 1
985 #include "prvm_execprogram.h"
986 #undef PRVMTRACE
987                                 }
988                                 else
989                                 {
990 #include "prvm_execprogram.h"
991                                 }
992 #undef PRVMBOUNDSCHECK
993                         }
994                         else
995                         {
996                                 if (prog->trace)
997                                 {
998 #define PRVMTRACE 1
999 #include "prvm_execprogram.h"
1000 #undef PRVMTRACE
1001                                 }
1002                                 else
1003                                 {
1004 #include "prvm_execprogram.h"
1005                                 }
1006                         }
1007 #undef PRVMSTATEMENTPROFILING
1008                 }
1009                 else
1010                 {
1011                         if (prvm_boundscheck)
1012                         {
1013 #define PRVMBOUNDSCHECK 1
1014                                 if (prog->trace)
1015                                 {
1016 #define PRVMTRACE 1
1017 #include "prvm_execprogram.h"
1018 #undef PRVMTRACE
1019                                 }
1020                                 else
1021                                 {
1022 #include "prvm_execprogram.h"
1023                                 }
1024 #undef PRVMBOUNDSCHECK
1025                         }
1026                         else
1027                         {
1028                                 if (prog->trace)
1029                                 {
1030 #define PRVMTRACE 1
1031 #include "prvm_execprogram.h"
1032 #undef PRVMTRACE
1033                                 }
1034                                 else
1035                                 {
1036 #include "prvm_execprogram.h"
1037                                 }
1038                         }
1039                 }
1040         }
1041
1042 cleanup:
1043         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1044                 Con_DPrintf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1045         // delete tempstrings created by this function
1046         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1047
1048         f->totaltime += (Sys_DoubleTime() - calltime);
1049
1050         SV_FlushBroadcastMessages();
1051 }
1052
1053 /*
1054 ====================
1055 SVVM_ExecuteProgram
1056 ====================
1057 */
1058 // LordHavoc: optimized
1059 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
1060 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
1061 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
1062 extern cvar_t prvm_traceqc;
1063 extern cvar_t prvm_statementprofiling;
1064 extern sizebuf_t vm_tempstringsbuf;
1065 extern qboolean prvm_runawaycheck;
1066 extern qboolean prvm_boundscheck;
1067 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage)
1068 {
1069         dstatement_t    *st, *startst;
1070         mfunction_t     *f, *newf;
1071         prvm_edict_t    *ed;
1072         prvm_eval_t     *ptr;
1073         int             jumpcount, cachedpr_trace, exitdepth;
1074         int             restorevm_tempstringsbuf_cursize;
1075         double  calltime;
1076
1077         calltime = Sys_DoubleTime();
1078
1079         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
1080         {
1081                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
1082                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
1083                 PRVM_ERROR ("SVVM_ExecuteProgram: %s", errormessage);
1084         }
1085
1086         f = &prog->functions[fnum];
1087
1088         // after executing this function, delete all tempstrings it created
1089         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
1090
1091         prog->trace = prvm_traceqc.integer;
1092
1093         // we know we're done when pr_depth drops to this
1094         exitdepth = prog->depth;
1095
1096 // make a stack frame
1097         st = &prog->statements[PRVM_EnterFunction (f)];
1098         // save the starting statement pointer for profiling
1099         // (when the function exits or jumps, the (st - startst) integer value is
1100         // added to the function's profile counter)
1101         startst = st;
1102         // instead of counting instructions, we count jumps
1103         jumpcount = 0;
1104         // add one to the callcount of this function because otherwise engine-called functions aren't counted
1105         prog->xfunction->callcount++;
1106
1107 chooseexecprogram:
1108         cachedpr_trace = prog->trace;
1109         if (prvm_runawaycheck)
1110         {
1111 #define PRVMRUNAWAYCHECK 1
1112                 if (prvm_statementprofiling.integer)
1113                 {
1114 #define PRVMSTATEMENTPROFILING 1
1115                         if (prvm_boundscheck)
1116                         {
1117 #define PRVMBOUNDSCHECK 1
1118                                 if (prog->trace)
1119                                 {
1120 #define PRVMTRACE 1
1121 #include "prvm_execprogram.h"
1122 #undef PRVMTRACE
1123                                 }
1124                                 else
1125                                 {
1126 #include "prvm_execprogram.h"
1127                                 }
1128 #undef PRVMBOUNDSCHECK
1129                         }
1130                         else
1131                         {
1132                                 if (prog->trace)
1133                                 {
1134 #define PRVMTRACE 1
1135 #include "prvm_execprogram.h"
1136 #undef PRVMTRACE
1137                                 }
1138                                 else
1139                                 {
1140 #include "prvm_execprogram.h"
1141                                 }
1142                         }
1143 #undef PRVMSTATEMENTPROFILING
1144                 }
1145                 else
1146                 {
1147                         if (prvm_boundscheck)
1148                         {
1149 #define PRVMBOUNDSCHECK 1
1150                                 if (prog->trace)
1151                                 {
1152 #define PRVMTRACE 1
1153 #include "prvm_execprogram.h"
1154 #undef PRVMTRACE
1155                                 }
1156                                 else
1157                                 {
1158 #include "prvm_execprogram.h"
1159                                 }
1160 #undef PRVMBOUNDSCHECK
1161                         }
1162                         else
1163                         {
1164                                 if (prog->trace)
1165                                 {
1166 #define PRVMTRACE 1
1167 #include "prvm_execprogram.h"
1168 #undef PRVMTRACE
1169                                 }
1170                                 else
1171                                 {
1172 #include "prvm_execprogram.h"
1173                                 }
1174                         }
1175                 }
1176 #undef PRVMRUNAWAYCHECK
1177         }
1178         else
1179         {
1180                 if (prvm_statementprofiling.integer)
1181                 {
1182 #define PRVMSTATEMENTPROFILING 1
1183                         if (prvm_boundscheck)
1184                         {
1185 #define PRVMBOUNDSCHECK 1
1186                                 if (prog->trace)
1187                                 {
1188 #define PRVMTRACE 1
1189 #include "prvm_execprogram.h"
1190 #undef PRVMTRACE
1191                                 }
1192                                 else
1193                                 {
1194 #include "prvm_execprogram.h"
1195                                 }
1196 #undef PRVMBOUNDSCHECK
1197                         }
1198                         else
1199                         {
1200                                 if (prog->trace)
1201                                 {
1202 #define PRVMTRACE 1
1203 #include "prvm_execprogram.h"
1204 #undef PRVMTRACE
1205                                 }
1206                                 else
1207                                 {
1208 #include "prvm_execprogram.h"
1209                                 }
1210                         }
1211 #undef PRVMSTATEMENTPROFILING
1212                 }
1213                 else
1214                 {
1215                         if (prvm_boundscheck)
1216                         {
1217 #define PRVMBOUNDSCHECK 1
1218                                 if (prog->trace)
1219                                 {
1220 #define PRVMTRACE 1
1221 #include "prvm_execprogram.h"
1222 #undef PRVMTRACE
1223                                 }
1224                                 else
1225                                 {
1226 #include "prvm_execprogram.h"
1227                                 }
1228 #undef PRVMBOUNDSCHECK
1229                         }
1230                         else
1231                         {
1232                                 if (prog->trace)
1233                                 {
1234 #define PRVMTRACE 1
1235 #include "prvm_execprogram.h"
1236 #undef PRVMTRACE
1237                                 }
1238                                 else
1239                                 {
1240 #include "prvm_execprogram.h"
1241                                 }
1242                         }
1243                 }
1244         }
1245
1246 cleanup:
1247         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1248                 Con_DPrintf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1249         // delete tempstrings created by this function
1250         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1251
1252         f->totaltime += (Sys_DoubleTime() - calltime);
1253
1254         SV_FlushBroadcastMessages();
1255 }