2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 const char *prvm_opnames[] =
115 //=============================================================================
122 extern cvar_t prvm_coverage;
123 extern cvar_t prvm_statementprofiling;
124 extern cvar_t prvm_timeprofiling;
125 static void PRVM_PrintStatement(prvm_prog_t *prog, mstatement_t *s)
128 int opnum = (int)(s - prog->statements);
129 char valuebuf[MAX_INPUTLINE];
131 Con_Printf("s%i: ", opnum);
132 if( prog->statement_linenums )
134 if ( prog->statement_columnnums )
135 Con_Printf( "%s:%i:%i: ", PRVM_GetString( prog, prog->xfunction->s_file ), prog->statement_linenums[ opnum ], prog->statement_columnnums[ opnum ] );
137 Con_Printf( "%s:%i: ", PRVM_GetString( prog, prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
140 if (prvm_statementprofiling.integer)
141 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
143 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
145 Con_Printf("%s ", prvm_opnames[s->op]);
146 i = strlen(prvm_opnames[s->op]);
147 // don't count a preceding color tag when padding the name
148 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
153 if (s->operand[0] >= 0) Con_Printf( "%s", PRVM_GlobalString(prog, s->operand[0], valuebuf, sizeof(valuebuf)));
154 if (s->operand[1] >= 0) Con_Printf(", %s", PRVM_GlobalString(prog, s->operand[1], valuebuf, sizeof(valuebuf)));
155 if (s->operand[2] >= 0) Con_Printf(", %s", PRVM_GlobalString(prog, s->operand[2], valuebuf, sizeof(valuebuf)));
156 if (s->jumpabsolute >= 0) Con_Printf(", statement %i", s->jumpabsolute);
160 void PRVM_PrintFunctionStatements (prvm_prog_t *prog, const char *name)
162 int i, firststatement, endstatement;
164 func = PRVM_ED_FindFunction (prog, name);
167 Con_Printf("%s progs: no function named %s\n", prog->name, name);
170 firststatement = func->first_statement;
171 if (firststatement < 0)
173 Con_Printf("%s progs: function %s is builtin #%i\n", prog->name, name, -firststatement);
177 // find the end statement
178 endstatement = prog->numstatements;
179 for (i = 0;i < prog->numfunctions;i++)
180 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
181 endstatement = prog->functions[i].first_statement;
183 // now print the range of statements
184 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);
185 prog->xfunction = func;
186 for (i = firststatement;i < endstatement;i++)
188 PRVM_PrintStatement(prog, prog->statements + i);
189 if (!(prvm_coverage.integer & 4))
190 prog->statement_profile[i] = 0;
192 if (prvm_coverage.integer & 4)
193 Con_Printf("Collecting statement coverage, not flushing statement profile.\n");
202 void PRVM_PrintFunction_f(cmd_state_t *cmd)
205 if (Cmd_Argc(cmd) != 3)
207 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
211 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
214 PRVM_PrintFunctionStatements(prog, Cmd_Argv(cmd, 2));
222 void PRVM_StackTrace (prvm_prog_t *prog)
227 prog->stack[prog->depth].s = prog->xstatement;
228 prog->stack[prog->depth].f = prog->xfunction;
229 for (i = prog->depth;i > 0;i--)
231 f = prog->stack[i].f;
234 Con_Print("<NULL FUNCTION>\n");
237 if (prog->statement_linenums)
239 if (prog->statement_columnnums)
240 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);
242 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);
245 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);
250 void PRVM_ShortStackTrace(prvm_prog_t *prog, char *buf, size_t bufsize)
258 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
262 strlcpy(buf, "<NO PROG>", bufsize);
266 prog->stack[prog->depth].s = prog->xstatement;
267 prog->stack[prog->depth].f = prog->xfunction;
268 for (i = prog->depth;i > 0;i--)
270 f = prog->stack[i].f;
274 ? 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)
283 static void PRVM_CallProfile (prvm_prog_t *prog)
285 mfunction_t *f, *best;
289 double newprofiletime;
291 Con_Printf( "%s Call Profile:\n", prog->name );
298 for (i=0 ; i<prog->numfunctions ; i++)
300 f = &prog->functions[i];
301 if (max < f->totaltime)
309 sum += best->totaltime;
310 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(prog, best->s_name));
315 newprofiletime = Sys_DirtyTime();
316 Con_Printf("Total time since last profile reset: %9.4f\n", newprofiletime - prog->profiletime);
317 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
319 prog->profiletime = newprofiletime;
322 void PRVM_Profile (prvm_prog_t *prog, int maxfunctions, double mintime, int sortby)
324 mfunction_t *f, *best;
328 if(!prvm_timeprofiling.integer)
329 mintime *= 10000000; // count each statement as about 0.1µs
331 if(prvm_timeprofiling.integer)
332 Con_Printf( "%s Profile:\n[CallCount] [Time] [BuiltinTm] [Statement] [BuiltinCt] [TimeTotal] [StmtTotal] [BltnTotal] [self]\n", prog->name );
333 // 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
335 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", prog->name );
336 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
343 for (i=0 ; i<prog->numfunctions ; i++)
345 f = &prog->functions[i];
346 if(prvm_timeprofiling.integer)
350 if(f->first_statement < 0)
352 if (max < f->tprofile)
360 if (max < f->tprofile_total)
362 max = f->tprofile_total;
369 if (max < f->tprofile + f->tbprofile)
371 max = f->tprofile + f->tbprofile;
380 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
382 max = f->profile_total + f->builtinsprofile_total + f->callcount;
388 if (max < f->profile + f->builtinsprofile + f->callcount)
390 max = f->profile + f->builtinsprofile + f->callcount;
398 if (num < maxfunctions && max > mintime)
400 if(prvm_timeprofiling.integer)
402 if (best->first_statement < 0)
403 Con_Printf("%11.0f %11.6f ------------- builtin ------------- %11.6f ----------- builtin ----------- %s\n", best->callcount, best->tprofile, best->tprofile, PRVM_GetString(prog, best->s_name));
404 // %11.6f 12345678901 12345678901 12345678901 %11.6f 12345678901 12345678901 123.45%
406 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));
410 if (best->first_statement < 0)
411 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(prog, best->s_name));
412 // 12345678901 12345678901 12345678901 12345678901 123.45%
414 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));
421 best->builtinsprofile = 0;
422 best->profile_total = 0;
423 best->tprofile_total = 0;
424 best->builtinsprofile_total = 0;
436 void PRVM_CallProfile_f(cmd_state_t *cmd)
439 if (Cmd_Argc(cmd) != 2)
441 Con_Print("prvm_callprofile <program name>\n");
445 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
448 PRVM_CallProfile(prog);
457 void PRVM_Profile_f(cmd_state_t *cmd)
462 if (prvm_coverage.integer & 1)
464 Con_Printf("Collecting function coverage, cannot profile - sorry!\n");
469 if (Cmd_Argc(cmd) == 3)
470 howmany = atoi(Cmd_Argv(cmd, 2));
471 else if (Cmd_Argc(cmd) != 2)
473 Con_Print("prvm_profile <program name>\n");
477 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
480 PRVM_Profile(prog, howmany, 0, 0);
483 void PRVM_ChildProfile_f(cmd_state_t *cmd)
488 if (prvm_coverage.integer & 1)
490 Con_Printf("Collecting function coverage, cannot profile - sorry!\n");
495 if (Cmd_Argc(cmd) == 3)
496 howmany = atoi(Cmd_Argv(cmd, 2));
497 else if (Cmd_Argc(cmd) != 2)
499 Con_Print("prvm_childprofile <program name>\n");
503 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
506 PRVM_Profile(prog, howmany, 0, 1);
509 void PRVM_PrintState(prvm_prog_t *prog, int stack_index)
512 mfunction_t *func = prog->xfunction;
513 int st = prog->xstatement;
514 if (stack_index > 0 && stack_index <= prog->depth)
516 func = prog->stack[prog->depth - stack_index].f;
517 st = prog->stack[prog->depth - stack_index].s;
519 if (prog->statestring)
521 Con_Printf("Caller-provided information: %s\n", prog->statestring);
525 for (i = -7; i <= 0;i++)
526 if (st + i >= func->first_statement)
527 PRVM_PrintStatement(prog, prog->statements + st + i);
529 PRVM_StackTrace(prog);
532 extern cvar_t prvm_errordump;
533 void PRVM_Crash(prvm_prog_t *prog)
541 PRVM_serverfunction(SV_Shutdown) = 0; // don't call SV_Shutdown on crash
543 if( prog->depth > 0 )
545 Con_Printf("QuakeC crash report for %s:\n", prog->name);
546 PRVM_PrintState(prog, 0);
549 if(prvm_errordump.integer)
552 SV_Savegame_to(prog, va(vabuf, sizeof(vabuf), "crash-%s.dmp", prog->name));
555 // dump the stack so host_error can shutdown functions
557 prog->localstack_used = 0;
559 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
560 prog->tempstringsbuf.cursize = 0;
562 // reset the prog pointer
567 ============================================================================
570 The interpretation main loop
571 ============================================================================
578 Returns the new program statement counter
581 static int PRVM_EnterFunction (prvm_prog_t *prog, mfunction_t *f)
586 prog->error_cmd("PRVM_EnterFunction: NULL function in %s", prog->name);
588 prog->stack[prog->depth].s = prog->xstatement;
589 prog->stack[prog->depth].f = prog->xfunction;
590 prog->stack[prog->depth].profile_acc = -f->profile;
591 prog->stack[prog->depth].tprofile_acc = -f->tprofile + -f->tbprofile;
592 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
594 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
595 prog->error_cmd("stack overflow");
597 // save off any locals that the new function steps on
599 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
600 prog->error_cmd("PRVM_ExecuteProgram: locals stack overflow in %s", prog->name);
602 for (i=0 ; i < c ; i++)
603 prog->localstack[prog->localstack_used+i] = prog->globals.ip[f->parm_start + i];
604 prog->localstack_used += c;
608 for (i=0 ; i<f->numparms ; i++)
610 for (j=0 ; j<f->parm_size[i] ; j++)
612 prog->globals.ip[o] = prog->globals.ip[OFS_PARM0+i*3+j];
619 return f->first_statement - 1; // offset the s++
627 static int PRVM_LeaveFunction (prvm_prog_t *prog)
632 if (prog->depth <= 0)
633 prog->error_cmd("prog stack underflow in %s", prog->name);
635 if (!prog->xfunction)
636 prog->error_cmd("PR_LeaveFunction: NULL function in %s", prog->name);
637 // restore locals from the stack
638 c = prog->xfunction->locals;
639 prog->localstack_used -= c;
640 if (prog->localstack_used < 0)
641 prog->error_cmd("PRVM_ExecuteProgram: locals stack underflow in %s", prog->name);
643 for (i=0 ; i < c ; i++)
644 prog->globals.ip[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
650 prog->xfunction = prog->stack[prog->depth].f;
651 prog->stack[prog->depth].profile_acc += f->profile;
652 prog->stack[prog->depth].tprofile_acc += f->tprofile + f->tbprofile;
653 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
656 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
657 prog->stack[prog->depth-1].tprofile_acc += prog->stack[prog->depth].tprofile_acc;
658 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
662 // if f is already on the call stack...
663 // we cannot add this profile data to it now
664 // or we would add it more than once
665 // so, let's only add to the function's profile if it is the outermost call
666 f->profile_total += prog->stack[prog->depth].profile_acc;
667 f->tprofile_total += prog->stack[prog->depth].tprofile_acc;
668 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
671 return prog->stack[prog->depth].s;
674 void PRVM_Init_Exec(prvm_prog_t *prog)
678 prog->localstack_used = 0;
679 // reset the string table
688 // Note: in these two calls, prog->xfunction is assumed to be sane.
689 static const char *PRVM_WhereAmI(char *buf, size_t bufsize, prvm_prog_t *prog, mfunction_t *func, int statement)
691 if (prog->statement_linenums)
693 if (prog->statement_columnnums)
694 return va(buf, bufsize, "%s:%i:%i(%s, %i)", PRVM_GetString(prog, func->s_file), prog->statement_linenums[statement], prog->statement_columnnums[statement], PRVM_GetString(prog, func->s_name), statement - func->first_statement);
696 return va(buf, bufsize, "%s:%i(%s, %i)", PRVM_GetString(prog, func->s_file), prog->statement_linenums[statement], PRVM_GetString(prog, func->s_name), statement - func->first_statement);
699 return va(buf, bufsize, "%s(%s, %i)", PRVM_GetString(prog, func->s_file), PRVM_GetString(prog, func->s_name), statement - func->first_statement);
701 static void PRVM_FunctionCoverageEvent(prvm_prog_t *prog, mfunction_t *func)
703 ++prog->functions_covered;
704 Con_Printf("prvm_coverage: %s just called %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_GetString(prog, func->s_name), prog->functions_covered * 100.0 / prog->numfunctions);
706 void PRVM_ExplicitCoverageEvent(prvm_prog_t *prog, mfunction_t *func, int statement)
709 ++prog->explicit_covered;
710 Con_Printf("prvm_coverage: %s just executed a coverage() statement at %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_WhereAmI(vabuf, sizeof(vabuf), prog, func, statement), prog->explicit_covered * 100.0 / prog->numexplicitcoveragestatements);
712 static void PRVM_StatementCoverageEvent(prvm_prog_t *prog, mfunction_t *func, int statement)
715 ++prog->statements_covered;
716 Con_Printf("prvm_coverage: %s just executed a statement at %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_WhereAmI(vabuf, sizeof(vabuf), prog, func, statement), prog->statements_covered * 100.0 / prog->numstatements);
719 #if defined (__GNUC__) || (__clang__)
720 # ifndef CONFIG_PEDANTIC
721 # define HAVE_COMPUTED_GOTOS 1
725 #define OPA ((prvm_eval_t *)&prog->globals.fp[st->operand[0]])
726 #define OPB ((prvm_eval_t *)&prog->globals.fp[st->operand[1]])
727 #define OPC ((prvm_eval_t *)&prog->globals.fp[st->operand[2]])
728 extern cvar_t prvm_traceqc;
729 extern cvar_t prvm_statementprofiling;
730 extern qboolean prvm_runawaycheck;
739 void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
741 mstatement_t *st, *startst;
742 mfunction_t *func, *enterfunc;
745 int jumpcount, cachedpr_trace, exitdepth;
746 int restorevm_tempstringsbuf_cursize;
749 prvm_vec_t tempfloat;
750 // these may become out of date when a builtin is called, and are updated accordingly
751 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
752 unsigned int cached_entityfields = prog->entityfields;
753 unsigned int cached_entityfields_3 = prog->entityfields - 3;
754 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
755 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
756 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
757 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
758 unsigned int cached_max_edicts = prog->max_edicts;
759 // these do not change
760 mstatement_t *cached_statements = prog->statements;
761 qboolean cached_allowworldwrites = prog->allowworldwrites;
762 unsigned int cached_flag = prog->flag;
764 calltime = Sys_DirtyTime();
766 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
768 if (PRVM_allglobaledict(self))
769 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
770 prog->error_cmd("MVM_ExecuteProgram: %s", errormessage);
773 func = &prog->functions[fnum];
775 // after executing this function, delete all tempstrings it created
776 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
778 prog->trace = prvm_traceqc.integer;
780 // we know we're done when pr_depth drops to this
781 exitdepth = prog->depth;
783 // make a stack frame
784 st = &prog->statements[PRVM_EnterFunction(prog, func)];
785 // save the starting statement pointer for profiling
786 // (when the function exits or jumps, the (st - startst) integer value is
787 // added to the function's profile counter)
790 // instead of counting instructions, we count jumps
792 // add one to the callcount of this function because otherwise engine-called functions aren't counted
793 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
794 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
797 cachedpr_trace = prog->trace;
798 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
800 #define PRVMSLOWINTERPRETER 1
801 if (prvm_timeprofiling.integer)
803 #define PRVMTIMEPROFILING 1
804 #include "prvm_execprogram.h"
805 #undef PRVMTIMEPROFILING
809 #include "prvm_execprogram.h"
811 #undef PRVMSLOWINTERPRETER
815 if (prvm_timeprofiling.integer)
817 #define PRVMTIMEPROFILING 1
818 #include "prvm_execprogram.h"
819 #undef PRVMTIMEPROFILING
823 #include "prvm_execprogram.h"
828 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
829 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);
830 // delete tempstrings created by this function
831 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
833 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
834 func->totaltime += tm;
836 if (prog == SVVM_prog)
837 SV_FlushBroadcastMessages();
846 void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
848 mstatement_t *st, *startst;
849 mfunction_t *func, *enterfunc;
852 int jumpcount, cachedpr_trace, exitdepth;
853 int restorevm_tempstringsbuf_cursize;
856 prvm_vec_t tempfloat;
857 // these may become out of date when a builtin is called, and are updated accordingly
858 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
859 unsigned int cached_entityfields = prog->entityfields;
860 unsigned int cached_entityfields_3 = prog->entityfields - 3;
861 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
862 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
863 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
864 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
865 unsigned int cached_max_edicts = prog->max_edicts;
866 // these do not change
867 mstatement_t *cached_statements = prog->statements;
868 qboolean cached_allowworldwrites = prog->allowworldwrites;
869 unsigned int cached_flag = prog->flag;
871 calltime = Sys_DirtyTime();
873 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
875 if (PRVM_allglobaledict(self))
876 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
877 prog->error_cmd("CLVM_ExecuteProgram: %s", errormessage);
880 func = &prog->functions[fnum];
882 // after executing this function, delete all tempstrings it created
883 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
885 prog->trace = prvm_traceqc.integer;
887 // we know we're done when pr_depth drops to this
888 exitdepth = prog->depth;
890 // make a stack frame
891 st = &prog->statements[PRVM_EnterFunction(prog, func)];
892 // save the starting statement pointer for profiling
893 // (when the function exits or jumps, the (st - startst) integer value is
894 // added to the function's profile counter)
897 // instead of counting instructions, we count jumps
899 // add one to the callcount of this function because otherwise engine-called functions aren't counted
900 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
901 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
904 cachedpr_trace = prog->trace;
905 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
907 #define PRVMSLOWINTERPRETER 1
908 if (prvm_timeprofiling.integer)
910 #define PRVMTIMEPROFILING 1
911 #include "prvm_execprogram.h"
912 #undef PRVMTIMEPROFILING
916 #include "prvm_execprogram.h"
918 #undef PRVMSLOWINTERPRETER
922 if (prvm_timeprofiling.integer)
924 #define PRVMTIMEPROFILING 1
925 #include "prvm_execprogram.h"
926 #undef PRVMTIMEPROFILING
930 #include "prvm_execprogram.h"
935 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
936 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);
937 // delete tempstrings created by this function
938 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
940 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
941 func->totaltime += tm;
943 if (prog == SVVM_prog)
944 SV_FlushBroadcastMessages();
954 void SVVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
956 void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
959 mstatement_t *st, *startst;
960 mfunction_t *func, *enterfunc;
963 int jumpcount, cachedpr_trace, exitdepth;
964 int restorevm_tempstringsbuf_cursize;
967 prvm_vec_t tempfloat;
968 // these may become out of date when a builtin is called, and are updated accordingly
969 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
970 unsigned int cached_entityfields = prog->entityfields;
971 unsigned int cached_entityfields_3 = prog->entityfields - 3;
972 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
973 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
974 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
975 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
976 unsigned int cached_max_edicts = prog->max_edicts;
977 // these do not change
978 mstatement_t *cached_statements = prog->statements;
979 qboolean cached_allowworldwrites = prog->allowworldwrites;
980 unsigned int cached_flag = prog->flag;
982 calltime = Sys_DirtyTime();
984 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
986 if (PRVM_allglobaledict(self))
987 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
988 prog->error_cmd("SVVM_ExecuteProgram: %s", errormessage);
991 func = &prog->functions[fnum];
993 // after executing this function, delete all tempstrings it created
994 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
996 prog->trace = prvm_traceqc.integer;
998 // we know we're done when pr_depth drops to this
999 exitdepth = prog->depth;
1001 // make a stack frame
1002 st = &prog->statements[PRVM_EnterFunction(prog, func)];
1003 // save the starting statement pointer for profiling
1004 // (when the function exits or jumps, the (st - startst) integer value is
1005 // added to the function's profile counter)
1008 // instead of counting instructions, we count jumps
1010 // add one to the callcount of this function because otherwise engine-called functions aren't counted
1011 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
1012 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
1015 cachedpr_trace = prog->trace;
1016 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
1018 #define PRVMSLOWINTERPRETER 1
1019 if (prvm_timeprofiling.integer)
1021 #define PRVMTIMEPROFILING 1
1022 #include "prvm_execprogram.h"
1023 #undef PRVMTIMEPROFILING
1027 #include "prvm_execprogram.h"
1029 #undef PRVMSLOWINTERPRETER
1033 if (prvm_timeprofiling.integer)
1035 #define PRVMTIMEPROFILING 1
1036 #include "prvm_execprogram.h"
1037 #undef PRVMTIMEPROFILING
1041 #include "prvm_execprogram.h"
1046 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1047 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);
1048 // delete tempstrings created by this function
1049 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1051 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
1052 func->totaltime += tm;
1054 if (prog == SVVM_prog)
1055 SV_FlushBroadcastMessages();