fix typo
[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 const 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 extern cvar_t prvm_timeprofiling;
127 void PRVM_PrintStatement (dstatement_t *s)
128 {
129         size_t i;
130         int opnum = (int)(s - prog->statements);
131
132         Con_Printf("s%i: ", opnum);
133         if( prog->statement_linenums )
134                 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
135
136         if (prvm_statementprofiling.integer)
137                 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
138
139         if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
140         {
141                 Con_Printf("%s ",  prvm_opnames[s->op]);
142                 i = strlen(prvm_opnames[s->op]);
143                 // don't count a preceding color tag when padding the name
144                 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
145                         i -= 2;
146                 for ( ; i<10 ; i++)
147                         Con_Print(" ");
148         }
149         if (s->op == OP_IF || s->op == OP_IFNOT)
150                 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
151         else if (s->op == OP_GOTO)
152                 Con_Printf("s%i",(signed short)s->a + opnum);
153         else if ( (unsigned)(s->op - OP_STORE_F) < 6)
154         {
155                 Con_Print(PRVM_GlobalString((unsigned short) s->a));
156                 Con_Print(", ");
157                 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
158         }
159         else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
160         {
161                 if (s->a)
162                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
163                 if (s->b)
164                 {
165                         Con_Print(", ");
166                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
167                 }
168                 if (s->c)
169                 {
170                         Con_Print(", ");
171                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
172                 }
173         }
174         else
175         {
176                 if (s->a)
177                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
178                 if (s->b)
179                 {
180                         Con_Print(", ");
181                         Con_Print(PRVM_GlobalString((unsigned short) s->b));
182                 }
183                 if (s->c)
184                 {
185                         Con_Print(", ");
186                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
187                 }
188         }
189         Con_Print("\n");
190 }
191
192 void PRVM_PrintFunctionStatements (const char *name)
193 {
194         int i, firststatement, endstatement;
195         mfunction_t *func;
196         func = PRVM_ED_FindFunction (name);
197         if (!func)
198         {
199                 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
200                 return;
201         }
202         firststatement = func->first_statement;
203         if (firststatement < 0)
204         {
205                 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
206                 return;
207         }
208
209         // find the end statement
210         endstatement = prog->progs->numstatements;
211         for (i = 0;i < prog->progs->numfunctions;i++)
212                 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
213                         endstatement = prog->functions[i].first_statement;
214
215         // now print the range of statements
216         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);
217         for (i = firststatement;i < endstatement;i++)
218         {
219                 PRVM_PrintStatement(prog->statements + i);
220                 prog->statement_profile[i] = 0;
221         }
222 }
223
224 /*
225 ============
226 PRVM_PrintFunction_f
227
228 ============
229 */
230 void PRVM_PrintFunction_f (void)
231 {
232         if (Cmd_Argc() != 3)
233         {
234                 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
235                 return;
236         }
237
238         PRVM_Begin;
239         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
240                 return;
241
242         PRVM_PrintFunctionStatements(Cmd_Argv(2));
243
244         PRVM_End;
245 }
246
247 /*
248 ============
249 PRVM_StackTrace
250 ============
251 */
252 void PRVM_StackTrace (void)
253 {
254         mfunction_t     *f;
255         int                     i;
256
257         prog->stack[prog->depth].s = prog->xstatement;
258         prog->stack[prog->depth].f = prog->xfunction;
259         for (i = prog->depth;i > 0;i--)
260         {
261                 f = prog->stack[i].f;
262
263                 if (!f)
264                         Con_Print("<NULL FUNCTION>\n");
265                 else
266                         Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
267         }
268 }
269
270 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
271 {
272         mfunction_t     *f;
273         int                     i;
274
275         if(prog)
276         {
277                 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
278         }
279         else
280         {
281                 strlcpy(buf, "<NO PROG>", bufsize);
282                 return;
283         }
284
285         prog->stack[prog->depth].s = prog->xstatement;
286         prog->stack[prog->depth].f = prog->xfunction;
287         for (i = prog->depth;i > 0;i--)
288         {
289                 f = prog->stack[i].f;
290
291                 if(strlcat(buf,
292                         f
293                                 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
294                                 : "<NULL> ",
295                         bufsize
296                 ) >= bufsize)
297                         break;
298         }
299 }
300
301
302 void PRVM_CallProfile (void)
303 {
304         mfunction_t *f, *best;
305         int i;
306         double max;
307         double sum;
308
309         Con_Printf( "%s Call Profile:\n", PRVM_NAME );
310
311         sum = 0;
312         do
313         {
314                 max = 0;
315                 best = NULL;
316                 for (i=0 ; i<prog->progs->numfunctions ; i++)
317                 {
318                         f = &prog->functions[i];
319                         if (max < f->totaltime)
320                         {
321                                 max = f->totaltime;
322                                 best = f;
323                         }
324                 }
325                 if (best)
326                 {
327                         sum += best->totaltime;
328                         Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
329                         best->totaltime = 0;
330                 }
331         } while (best);
332
333         Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
334         Con_Printf("       - used by QC code of this VM: %9.4f\n", sum);
335
336         prog->starttime = Sys_DoubleTime();
337 }
338
339 void PRVM_Profile (int maxfunctions, double mintime, int sortby)
340 {
341         mfunction_t *f, *best;
342         int i, num;
343         double max;
344
345         if(!prvm_timeprofiling.integer)
346                 mintime *= 10000000; // count each statement as about 0.1┬Ás
347
348         if(prvm_timeprofiling.integer)
349                 Con_Printf( "%s Profile:\n[CallCount]      [Time] [BuiltinTm] [Statement] [BuiltinCt] [TimeTotal] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
350                 //                        12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
351         else
352                 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
353                 //                        12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
354
355         num = 0;
356         do
357         {
358                 max = 0;
359                 best = NULL;
360                 for (i=0 ; i<prog->progs->numfunctions ; i++)
361                 {
362                         f = &prog->functions[i];
363                         if(prvm_timeprofiling.integer)
364                         {
365                                 if(sortby)
366                                 {
367                                         if(f->first_statement < 0)
368                                         {
369                                                 if (max < f->tprofile)
370                                                 {
371                                                         max = f->tprofile;
372                                                         best = f;
373                                                 }
374                                         }
375                                         else
376                                         {
377                                                 if (max < f->tprofile_total)
378                                                 {
379                                                         max = f->tprofile_total;
380                                                         best = f;
381                                                 }
382                                         }
383                                 }
384                                 else
385                                 {
386                                         if (max < f->tprofile + f->tbprofile)
387                                         {
388                                                 max = f->tprofile + f->tbprofile;
389                                                 best = f;
390                                         }
391                                 }
392                         }
393                         else
394                         {
395                                 if(sortby)
396                                 {
397                                         if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
398                                         {
399                                                 max = f->profile_total + f->builtinsprofile_total + f->callcount;
400                                                 best = f;
401                                         }
402                                 }
403                                 else
404                                 {
405                                         if (max < f->profile + f->builtinsprofile + f->callcount)
406                                         {
407                                                 max = f->profile + f->builtinsprofile + f->callcount;
408                                                 best = f;
409                                         }
410                                 }
411                         }
412                 }
413                 if (best)
414                 {
415                         if (num < maxfunctions && max > mintime)
416                         {
417                                 if(prvm_timeprofiling.integer)
418                                 {
419                                         if (best->first_statement < 0)
420                                                 Con_Printf("%11.0f %11.6f ------------- builtin ------------- %11.6f ----------- builtin ----------- %s\n", best->callcount, best->tprofile, best->tprofile, PRVM_GetString(best->s_name));
421                                         //                 %11.6f 12345678901 12345678901 12345678901 %11.6f 12345678901 12345678901 123.45%
422                                         else
423                                                 Con_Printf("%11.0f %11.6f %11.6f %11.0f %11.0f %11.6f %11.0f %11.0f %6.2f%% %s\n", best->callcount, best->tprofile, best->tbprofile, best->profile, best->builtinsprofile, best->tprofile_total, best->profile_total, best->builtinsprofile_total, (best->tprofile_total > 0) ? ((best->tprofile) * 100.0 / (best->tprofile_total)) : -99.99, PRVM_GetString(best->s_name));
424                                 }
425                                 else
426                                 {
427                                         if (best->first_statement < 0)
428                                                 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
429                                         //                 12345678901 12345678901 12345678901 12345678901 123.45%
430                                         else
431                                                 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));
432                                 }
433                         }
434                         num++;
435                         best->profile = 0;
436                         best->tprofile = 0;
437                         best->tbprofile = 0;
438                         best->builtinsprofile = 0;
439                         best->profile_total = 0;
440                         best->tprofile_total = 0;
441                         best->builtinsprofile_total = 0;
442                         best->callcount = 0;
443                 }
444         } while (best);
445 }
446
447 /*
448 ============
449 PRVM_CallProfile_f
450
451 ============
452 */
453 void PRVM_CallProfile_f (void)
454 {
455         if (Cmd_Argc() != 2)
456         {
457                 Con_Print("prvm_callprofile <program name>\n");
458                 return;
459         }
460
461         PRVM_Begin;
462         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
463                 return;
464
465         PRVM_CallProfile();
466
467         PRVM_End;
468 }
469
470 /*
471 ============
472 PRVM_Profile_f
473
474 ============
475 */
476 void PRVM_Profile_f (void)
477 {
478         int howmany;
479
480         howmany = 1<<30;
481         if (Cmd_Argc() == 3)
482                 howmany = atoi(Cmd_Argv(2));
483         else if (Cmd_Argc() != 2)
484         {
485                 Con_Print("prvm_profile <program name>\n");
486                 return;
487         }
488
489         PRVM_Begin;
490         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
491                 return;
492
493         PRVM_Profile(howmany, 0, 0);
494
495         PRVM_End;
496 }
497
498 void PRVM_ChildProfile_f (void)
499 {
500         int howmany;
501
502         howmany = 1<<30;
503         if (Cmd_Argc() == 3)
504                 howmany = atoi(Cmd_Argv(2));
505         else if (Cmd_Argc() != 2)
506         {
507                 Con_Print("prvm_childprofile <program name>\n");
508                 return;
509         }
510
511         PRVM_Begin;
512         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
513                 return;
514
515         PRVM_Profile(howmany, 0, 1);
516
517         PRVM_End;
518 }
519
520 void PRVM_CrashAll(void)
521 {
522         int i;
523         prvm_prog_t *oldprog = prog;
524
525         for(i = 0; i < PRVM_MAXPROGS; i++)
526         {
527                 if(!PRVM_ProgLoaded(i))
528                         continue;
529                 PRVM_SetProg(i);
530                 PRVM_Crash();
531         }
532
533         prog = oldprog;
534 }
535
536 void PRVM_PrintState(void)
537 {
538         int i;
539         if(prog->statestring)
540         {
541                 Con_Printf("Caller-provided information: %s\n", prog->statestring);
542         }
543         if (prog->xfunction)
544         {
545                 for (i = -7; i <= 0;i++)
546                         if (prog->xstatement + i >= prog->xfunction->first_statement)
547                                 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
548         }
549         else
550                 Con_Print("null function executing??\n");
551         PRVM_StackTrace ();
552 }
553
554 extern sizebuf_t vm_tempstringsbuf;
555 extern cvar_t prvm_errordump;
556 void Host_Savegame_to (const char *name);
557 void PRVM_Crash(void)
558 {
559         if (prog == NULL)
560                 return;
561
562         prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
563
564         if( prog->depth > 0 )
565         {
566                 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
567                 PRVM_PrintState();
568         }
569
570         if(prvm_errordump.integer)
571         {
572                 // make a savegame
573                 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
574         }
575
576         // dump the stack so host_error can shutdown functions
577         prog->depth = 0;
578         prog->localstack_used = 0;
579
580         // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
581         vm_tempstringsbuf.cursize = 0;
582
583         // reset the prog pointer
584         prog = NULL;
585 }
586
587 /*
588 ============================================================================
589 PRVM_ExecuteProgram
590
591 The interpretation main loop
592 ============================================================================
593 */
594
595 /*
596 ====================
597 PRVM_EnterFunction
598
599 Returns the new program statement counter
600 ====================
601 */
602 int PRVM_EnterFunction (mfunction_t *f)
603 {
604         int             i, j, c, o;
605
606         if (!f)
607                 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
608
609         prog->stack[prog->depth].s = prog->xstatement;
610         prog->stack[prog->depth].f = prog->xfunction;
611         prog->stack[prog->depth].profile_acc = -f->profile;
612         prog->stack[prog->depth].tprofile_acc = -f->tprofile + -f->tbprofile;
613         prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
614         prog->depth++;
615         if (prog->depth >=PRVM_MAX_STACK_DEPTH)
616                 PRVM_ERROR ("stack overflow");
617
618 // save off any locals that the new function steps on
619         c = f->locals;
620         if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
621                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
622
623         for (i=0 ; i < c ; i++)
624                 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
625         prog->localstack_used += c;
626
627 // copy parameters
628         o = f->parm_start;
629         for (i=0 ; i<f->numparms ; i++)
630         {
631                 for (j=0 ; j<f->parm_size[i] ; j++)
632                 {
633                         ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
634                         o++;
635                 }
636         }
637
638         ++f->recursion;
639         prog->xfunction = f;
640         return f->first_statement - 1;  // offset the s++
641 }
642
643 /*
644 ====================
645 PRVM_LeaveFunction
646 ====================
647 */
648 int PRVM_LeaveFunction (void)
649 {
650         int             i, c;
651         mfunction_t *f;
652
653         if (prog->depth <= 0)
654                 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
655
656         if (!prog->xfunction)
657                 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
658 // restore locals from the stack
659         c = prog->xfunction->locals;
660         prog->localstack_used -= c;
661         if (prog->localstack_used < 0)
662                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
663
664         for (i=0 ; i < c ; i++)
665                 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
666
667 // up stack
668         prog->depth--;
669         f = prog->xfunction;
670         --f->recursion;
671         prog->xfunction = prog->stack[prog->depth].f;
672         prog->stack[prog->depth].profile_acc += f->profile;
673         prog->stack[prog->depth].tprofile_acc += f->tprofile + f->tbprofile;
674         prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
675         if(prog->depth > 0)
676         {
677                 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
678                 prog->stack[prog->depth-1].tprofile_acc += prog->stack[prog->depth].tprofile_acc;
679                 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
680         }
681         if(!f->recursion)
682         {
683                 // if f is already on the call stack...
684                 // we cannot add this profile data to it now
685                 // or we would add it more than once
686                 // so, let's only add to the function's profile if it is the outermost call
687                 f->profile_total += prog->stack[prog->depth].profile_acc;
688                 f->tprofile_total += prog->stack[prog->depth].tprofile_acc;
689                 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
690         }
691         
692         return prog->stack[prog->depth].s;
693 }
694
695 void PRVM_Init_Exec(void)
696 {
697         // dump the stack
698         prog->depth = 0;
699         prog->localstack_used = 0;
700         // reset the string table
701         // nothing here yet
702 }
703
704 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
705 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
706 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
707 extern cvar_t prvm_traceqc;
708 extern cvar_t prvm_statementprofiling;
709 extern sizebuf_t vm_tempstringsbuf;
710 extern qboolean prvm_runawaycheck;
711
712 #ifdef PROFILING
713 /*
714 ====================
715 MVM_ExecuteProgram
716 ====================
717 */
718 void MVM_ExecuteProgram (func_t fnum, const char *errormessage)
719 {
720         dstatement_t    *st, *startst;
721         mfunction_t     *f, *newf;
722         prvm_edict_t    *ed;
723         prvm_eval_t     *ptr;
724         int             jumpcount, cachedpr_trace, exitdepth;
725         int             restorevm_tempstringsbuf_cursize;
726         double  calltime;
727         double tm, starttm;
728
729         calltime = Sys_DoubleTime();
730
731         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
732         {
733                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
734                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
735                 PRVM_ERROR ("MVM_ExecuteProgram: %s", errormessage);
736         }
737
738         f = &prog->functions[fnum];
739
740         // after executing this function, delete all tempstrings it created
741         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
742
743         prog->trace = prvm_traceqc.integer;
744
745         // we know we're done when pr_depth drops to this
746         exitdepth = prog->depth;
747
748 // make a stack frame
749         st = &prog->statements[PRVM_EnterFunction (f)];
750         // save the starting statement pointer for profiling
751         // (when the function exits or jumps, the (st - startst) integer value is
752         // added to the function's profile counter)
753         startst = st;
754         starttm = calltime;
755         // instead of counting instructions, we count jumps
756         jumpcount = 0;
757         // add one to the callcount of this function because otherwise engine-called functions aren't counted
758         prog->xfunction->callcount++;
759
760 chooseexecprogram:
761         cachedpr_trace = prog->trace;
762         if (prvm_statementprofiling.integer || prog->trace)
763         {
764 #define PRVMSLOWINTERPRETER 1
765                 if (prvm_timeprofiling.integer)
766                 {
767 #define PRVMTIMEPROFILING 1
768 #include "prvm_execprogram.h"
769 #undef PRVMTIMEPROFILING
770                 }
771                 else
772                 {
773 #include "prvm_execprogram.h"
774                 }
775 #undef PRVMSLOWINTERPRETER
776         }
777         else
778         {
779                 if (prvm_timeprofiling.integer)
780                 {
781 #define PRVMTIMEPROFILING 1
782 #include "prvm_execprogram.h"
783 #undef PRVMTIMEPROFILING
784                 }
785                 else
786                 {
787 #include "prvm_execprogram.h"
788                 }
789         }
790
791 cleanup:
792         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
793                 Con_DPrintf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
794         // delete tempstrings created by this function
795         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
796
797         f->totaltime += (Sys_DoubleTime() - calltime);
798
799         SV_FlushBroadcastMessages();
800 }
801
802 /*
803 ====================
804 CLVM_ExecuteProgram
805 ====================
806 */
807 void CLVM_ExecuteProgram (func_t fnum, const char *errormessage)
808 {
809         dstatement_t    *st, *startst;
810         mfunction_t     *f, *newf;
811         prvm_edict_t    *ed;
812         prvm_eval_t     *ptr;
813         int             jumpcount, cachedpr_trace, exitdepth;
814         int             restorevm_tempstringsbuf_cursize;
815         double  calltime;
816         double tm, starttm;
817
818         calltime = Sys_DoubleTime();
819
820         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
821         {
822                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
823                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
824                 PRVM_ERROR ("CLVM_ExecuteProgram: %s", errormessage);
825         }
826
827         f = &prog->functions[fnum];
828
829         // after executing this function, delete all tempstrings it created
830         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
831
832         prog->trace = prvm_traceqc.integer;
833
834         // we know we're done when pr_depth drops to this
835         exitdepth = prog->depth;
836
837 // make a stack frame
838         st = &prog->statements[PRVM_EnterFunction (f)];
839         // save the starting statement pointer for profiling
840         // (when the function exits or jumps, the (st - startst) integer value is
841         // added to the function's profile counter)
842         startst = st;
843         starttm = calltime;
844         // instead of counting instructions, we count jumps
845         jumpcount = 0;
846         // add one to the callcount of this function because otherwise engine-called functions aren't counted
847         prog->xfunction->callcount++;
848
849 chooseexecprogram:
850         cachedpr_trace = prog->trace;
851         if (prvm_statementprofiling.integer || prog->trace)
852         {
853 #define PRVMSLOWINTERPRETER 1
854                 if (prvm_timeprofiling.integer)
855                 {
856 #define PRVMTIMEPROFILING 1
857 #include "prvm_execprogram.h"
858 #undef PRVMTIMEPROFILING
859                 }
860                 else
861                 {
862 #include "prvm_execprogram.h"
863                 }
864 #undef PRVMSLOWINTERPRETER
865         }
866         else
867         {
868                 if (prvm_timeprofiling.integer)
869                 {
870 #define PRVMTIMEPROFILING 1
871 #include "prvm_execprogram.h"
872 #undef PRVMTIMEPROFILING
873                 }
874                 else
875                 {
876 #include "prvm_execprogram.h"
877                 }
878         }
879
880 cleanup:
881         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
882                 Con_DPrintf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
883         // delete tempstrings created by this function
884         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
885
886         f->totaltime += (Sys_DoubleTime() - calltime);
887
888         SV_FlushBroadcastMessages();
889 }
890 #endif
891
892 /*
893 ====================
894 SVVM_ExecuteProgram
895 ====================
896 */
897 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage)
898 {
899         dstatement_t    *st, *startst;
900         mfunction_t     *f, *newf;
901         prvm_edict_t    *ed;
902         prvm_eval_t     *ptr;
903         int             jumpcount, cachedpr_trace, exitdepth;
904         int             restorevm_tempstringsbuf_cursize;
905         double  calltime;
906         double tm, starttm;
907
908         calltime = Sys_DoubleTime();
909
910         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
911         {
912                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
913                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
914                 PRVM_ERROR ("SVVM_ExecuteProgram: %s", errormessage);
915         }
916
917         f = &prog->functions[fnum];
918
919         // after executing this function, delete all tempstrings it created
920         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
921
922         prog->trace = prvm_traceqc.integer;
923
924         // we know we're done when pr_depth drops to this
925         exitdepth = prog->depth;
926
927 // make a stack frame
928         st = &prog->statements[PRVM_EnterFunction (f)];
929         // save the starting statement pointer for profiling
930         // (when the function exits or jumps, the (st - startst) integer value is
931         // added to the function's profile counter)
932         startst = st;
933         starttm = calltime;
934         // instead of counting instructions, we count jumps
935         jumpcount = 0;
936         // add one to the callcount of this function because otherwise engine-called functions aren't counted
937         prog->xfunction->callcount++;
938
939 chooseexecprogram:
940         cachedpr_trace = prog->trace;
941         if (prvm_statementprofiling.integer || prog->trace)
942         {
943 #define PRVMSLOWINTERPRETER 1
944                 if (prvm_timeprofiling.integer)
945                 {
946 #define PRVMTIMEPROFILING 1
947 #include "prvm_execprogram.h"
948 #undef PRVMTIMEPROFILING
949                 }
950                 else
951                 {
952 #include "prvm_execprogram.h"
953                 }
954 #undef PRVMSLOWINTERPRETER
955         }
956         else
957         {
958                 if (prvm_timeprofiling.integer)
959                 {
960 #define PRVMTIMEPROFILING 1
961 #include "prvm_execprogram.h"
962 #undef PRVMTIMEPROFILING
963                 }
964                 else
965                 {
966 #include "prvm_execprogram.h"
967                 }
968         }
969
970 cleanup:
971         if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
972                 Con_DPrintf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
973         // delete tempstrings created by this function
974         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
975
976         f->totaltime += (Sys_DoubleTime() - calltime);
977
978         SV_FlushBroadcastMessages();
979 }