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