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