Two new cvars for blood particles: decal alpha, decal random scaling (min/max)
[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 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 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
646 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
647 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
648 extern cvar_t prvm_traceqc;
649 extern cvar_t prvm_statementprofiling;
650 extern sizebuf_t vm_tempstringsbuf;
651 extern qboolean prvm_runawaycheck;
652
653 #ifdef PROFILING
654 /*
655 ====================
656 MVM_ExecuteProgram
657 ====================
658 */
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_statementprofiling.integer || prog->trace)
702         {
703 #define PRVMSLOWINTERPRETER 1
704 #include "prvm_execprogram.h"
705 #undef PRVMSLOWINTERPRETER
706         }
707         else
708         {
709 #include "prvm_execprogram.h"
710         }
711
712 cleanup:
713         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
714                 Con_DPrintf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
715         // delete tempstrings created by this function
716         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
717
718         f->totaltime += (Sys_DoubleTime() - calltime);
719
720         SV_FlushBroadcastMessages();
721 }
722
723 /*
724 ====================
725 CLVM_ExecuteProgram
726 ====================
727 */
728 void CLVM_ExecuteProgram (func_t fnum, const char *errormessage)
729 {
730         dstatement_t    *st, *startst;
731         mfunction_t     *f, *newf;
732         prvm_edict_t    *ed;
733         prvm_eval_t     *ptr;
734         int             jumpcount, cachedpr_trace, exitdepth;
735         int             restorevm_tempstringsbuf_cursize;
736         double  calltime;
737
738         calltime = Sys_DoubleTime();
739
740         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
741         {
742                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
743                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
744                 PRVM_ERROR ("CLVM_ExecuteProgram: %s", errormessage);
745         }
746
747         f = &prog->functions[fnum];
748
749         // after executing this function, delete all tempstrings it created
750         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
751
752         prog->trace = prvm_traceqc.integer;
753
754         // we know we're done when pr_depth drops to this
755         exitdepth = prog->depth;
756
757 // make a stack frame
758         st = &prog->statements[PRVM_EnterFunction (f)];
759         // save the starting statement pointer for profiling
760         // (when the function exits or jumps, the (st - startst) integer value is
761         // added to the function's profile counter)
762         startst = st;
763         // instead of counting instructions, we count jumps
764         jumpcount = 0;
765         // add one to the callcount of this function because otherwise engine-called functions aren't counted
766         prog->xfunction->callcount++;
767
768 chooseexecprogram:
769         cachedpr_trace = prog->trace;
770         if (prvm_statementprofiling.integer || prog->trace)
771         {
772 #define PRVMSLOWINTERPRETER 1
773 #include "prvm_execprogram.h"
774 #undef PRVMSLOWINTERPRETER
775         }
776         else
777         {
778 #include "prvm_execprogram.h"
779         }
780
781 cleanup:
782         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
783                 Con_DPrintf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
784         // delete tempstrings created by this function
785         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
786
787         f->totaltime += (Sys_DoubleTime() - calltime);
788
789         SV_FlushBroadcastMessages();
790 }
791 #endif
792
793 /*
794 ====================
795 SVVM_ExecuteProgram
796 ====================
797 */
798 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage)
799 {
800         dstatement_t    *st, *startst;
801         mfunction_t     *f, *newf;
802         prvm_edict_t    *ed;
803         prvm_eval_t     *ptr;
804         int             jumpcount, cachedpr_trace, exitdepth;
805         int             restorevm_tempstringsbuf_cursize;
806         double  calltime;
807
808         calltime = Sys_DoubleTime();
809
810         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
811         {
812                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
813                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
814                 PRVM_ERROR ("SVVM_ExecuteProgram: %s", errormessage);
815         }
816
817         f = &prog->functions[fnum];
818
819         // after executing this function, delete all tempstrings it created
820         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
821
822         prog->trace = prvm_traceqc.integer;
823
824         // we know we're done when pr_depth drops to this
825         exitdepth = prog->depth;
826
827 // make a stack frame
828         st = &prog->statements[PRVM_EnterFunction (f)];
829         // save the starting statement pointer for profiling
830         // (when the function exits or jumps, the (st - startst) integer value is
831         // added to the function's profile counter)
832         startst = st;
833         // instead of counting instructions, we count jumps
834         jumpcount = 0;
835         // add one to the callcount of this function because otherwise engine-called functions aren't counted
836         prog->xfunction->callcount++;
837
838 chooseexecprogram:
839         cachedpr_trace = prog->trace;
840         if (prvm_statementprofiling.integer || prog->trace)
841         {
842 #define PRVMSLOWINTERPRETER 1
843 #include "prvm_execprogram.h"
844 #undef PRVMSLOWINTERPRETER
845         }
846         else
847         {
848 #include "prvm_execprogram.h"
849         }
850
851 cleanup:
852         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
853                 Con_DPrintf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
854         // delete tempstrings created by this function
855         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
856
857         f->totaltime += (Sys_DoubleTime() - calltime);
858
859         SV_FlushBroadcastMessages();
860 }