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