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_statementprofiling;
123 extern cvar_t prvm_timeprofiling;
124 static void PRVM_PrintStatement(prvm_prog_t *prog, mstatement_t *s)
127 int opnum = (int)(s - prog->statements);
128 char valuebuf[MAX_INPUTLINE];
130 Con_Printf("s%i: ", opnum);
131 if( prog->statement_linenums )
132 Con_Printf( "%s:%i: ", PRVM_GetString( prog, prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
134 if (prvm_statementprofiling.integer)
135 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
137 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
139 Con_Printf("%s ", prvm_opnames[s->op]);
140 i = strlen(prvm_opnames[s->op]);
141 // don't count a preceding color tag when padding the name
142 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
147 if (s->operand[0] >= 0) Con_Printf( "%s", PRVM_GlobalString(prog, s->operand[0], valuebuf, sizeof(valuebuf)));
148 if (s->operand[1] >= 0) Con_Printf(", %s", PRVM_GlobalString(prog, s->operand[1], valuebuf, sizeof(valuebuf)));
149 if (s->operand[2] >= 0) Con_Printf(", %s", PRVM_GlobalString(prog, s->operand[2], valuebuf, sizeof(valuebuf)));
150 if (s->jumpabsolute >= 0) Con_Printf(", statement %i", s->jumpabsolute);
154 void PRVM_PrintFunctionStatements (prvm_prog_t *prog, const char *name)
156 int i, firststatement, endstatement;
158 func = PRVM_ED_FindFunction (prog, name);
161 Con_Printf("%s progs: no function named %s\n", prog->name, name);
164 firststatement = func->first_statement;
165 if (firststatement < 0)
167 Con_Printf("%s progs: function %s is builtin #%i\n", prog->name, name, -firststatement);
171 // find the end statement
172 endstatement = prog->numstatements;
173 for (i = 0;i < prog->numfunctions;i++)
174 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
175 endstatement = prog->functions[i].first_statement;
177 // now print the range of statements
178 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);
179 for (i = firststatement;i < endstatement;i++)
181 PRVM_PrintStatement(prog, prog->statements + i);
182 prog->statement_profile[i] = 0;
192 void PRVM_PrintFunction_f (void)
197 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
201 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1))))
204 PRVM_PrintFunctionStatements(prog, Cmd_Argv(2));
212 void PRVM_StackTrace (prvm_prog_t *prog)
217 prog->stack[prog->depth].s = prog->xstatement;
218 prog->stack[prog->depth].f = prog->xfunction;
219 for (i = prog->depth;i > 0;i--)
221 f = prog->stack[i].f;
224 Con_Print("<NULL FUNCTION>\n");
226 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);
230 void PRVM_ShortStackTrace(prvm_prog_t *prog, char *buf, size_t bufsize)
238 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
242 strlcpy(buf, "<NO PROG>", bufsize);
246 prog->stack[prog->depth].s = prog->xstatement;
247 prog->stack[prog->depth].f = prog->xfunction;
248 for (i = prog->depth;i > 0;i--)
250 f = prog->stack[i].f;
254 ? 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)
263 static void PRVM_CallProfile (prvm_prog_t *prog)
265 mfunction_t *f, *best;
269 double newprofiletime;
271 Con_Printf( "%s Call Profile:\n", prog->name );
278 for (i=0 ; i<prog->numfunctions ; i++)
280 f = &prog->functions[i];
281 if (max < f->totaltime)
289 sum += best->totaltime;
290 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(prog, best->s_name));
295 newprofiletime = Sys_DirtyTime();
296 Con_Printf("Total time since last profile reset: %9.4f\n", newprofiletime - prog->profiletime);
297 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
299 prog->profiletime = newprofiletime;
302 void PRVM_Profile (prvm_prog_t *prog, int maxfunctions, double mintime, int sortby)
304 mfunction_t *f, *best;
308 if(!prvm_timeprofiling.integer)
309 mintime *= 10000000; // count each statement as about 0.1µs
311 if(prvm_timeprofiling.integer)
312 Con_Printf( "%s Profile:\n[CallCount] [Time] [BuiltinTm] [Statement] [BuiltinCt] [TimeTotal] [StmtTotal] [BltnTotal] [self]\n", prog->name );
313 // 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
315 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", prog->name );
316 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
323 for (i=0 ; i<prog->numfunctions ; i++)
325 f = &prog->functions[i];
326 if(prvm_timeprofiling.integer)
330 if(f->first_statement < 0)
332 if (max < f->tprofile)
340 if (max < f->tprofile_total)
342 max = f->tprofile_total;
349 if (max < f->tprofile + f->tbprofile)
351 max = f->tprofile + f->tbprofile;
360 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
362 max = f->profile_total + f->builtinsprofile_total + f->callcount;
368 if (max < f->profile + f->builtinsprofile + f->callcount)
370 max = f->profile + f->builtinsprofile + f->callcount;
378 if (num < maxfunctions && max > mintime)
380 if(prvm_timeprofiling.integer)
382 if (best->first_statement < 0)
383 Con_Printf("%11.0f %11.6f ------------- builtin ------------- %11.6f ----------- builtin ----------- %s\n", best->callcount, best->tprofile, best->tprofile, PRVM_GetString(prog, best->s_name));
384 // %11.6f 12345678901 12345678901 12345678901 %11.6f 12345678901 12345678901 123.45%
386 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));
390 if (best->first_statement < 0)
391 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(prog, best->s_name));
392 // 12345678901 12345678901 12345678901 12345678901 123.45%
394 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));
401 best->builtinsprofile = 0;
402 best->profile_total = 0;
403 best->tprofile_total = 0;
404 best->builtinsprofile_total = 0;
416 void PRVM_CallProfile_f (void)
421 Con_Print("prvm_callprofile <program name>\n");
425 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1))))
428 PRVM_CallProfile(prog);
437 void PRVM_Profile_f (void)
444 howmany = atoi(Cmd_Argv(2));
445 else if (Cmd_Argc() != 2)
447 Con_Print("prvm_profile <program name>\n");
451 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1))))
454 PRVM_Profile(prog, howmany, 0, 0);
457 void PRVM_ChildProfile_f (void)
464 howmany = atoi(Cmd_Argv(2));
465 else if (Cmd_Argc() != 2)
467 Con_Print("prvm_childprofile <program name>\n");
471 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1))))
474 PRVM_Profile(prog, howmany, 0, 1);
477 void PRVM_PrintState(prvm_prog_t *prog)
480 if (prog->statestring)
482 Con_Printf("Caller-provided information: %s\n", prog->statestring);
486 for (i = -7; i <= 0;i++)
487 if (prog->xstatement + i >= prog->xfunction->first_statement)
488 PRVM_PrintStatement(prog, prog->statements + prog->xstatement + i);
491 Con_Print("null function executing??\n");
492 PRVM_StackTrace(prog);
495 extern cvar_t prvm_errordump;
496 void PRVM_Crash(prvm_prog_t *prog)
504 PRVM_serverfunction(SV_Shutdown) = 0; // don't call SV_Shutdown on crash
506 if( prog->depth > 0 )
508 Con_Printf("QuakeC crash report for %s:\n", prog->name);
509 PRVM_PrintState(prog);
512 if(prvm_errordump.integer)
515 Host_Savegame_to(prog, va(vabuf, sizeof(vabuf), "crash-%s.dmp", prog->name));
518 // dump the stack so host_error can shutdown functions
520 prog->localstack_used = 0;
522 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
523 prog->tempstringsbuf.cursize = 0;
525 // reset the prog pointer
530 ============================================================================
533 The interpretation main loop
534 ============================================================================
541 Returns the new program statement counter
544 static int PRVM_EnterFunction (prvm_prog_t *prog, mfunction_t *f)
549 prog->error_cmd("PRVM_EnterFunction: NULL function in %s", prog->name);
551 prog->stack[prog->depth].s = prog->xstatement;
552 prog->stack[prog->depth].f = prog->xfunction;
553 prog->stack[prog->depth].profile_acc = -f->profile;
554 prog->stack[prog->depth].tprofile_acc = -f->tprofile + -f->tbprofile;
555 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
557 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
558 prog->error_cmd("stack overflow");
560 // save off any locals that the new function steps on
562 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
563 prog->error_cmd("PRVM_ExecuteProgram: locals stack overflow in %s", prog->name);
565 for (i=0 ; i < c ; i++)
566 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
567 prog->localstack_used += c;
571 for (i=0 ; i<f->numparms ; i++)
573 for (j=0 ; j<f->parm_size[i] ; j++)
575 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
582 return f->first_statement - 1; // offset the s++
590 static int PRVM_LeaveFunction (prvm_prog_t *prog)
595 if (prog->depth <= 0)
596 prog->error_cmd("prog stack underflow in %s", prog->name);
598 if (!prog->xfunction)
599 prog->error_cmd("PR_LeaveFunction: NULL function in %s", prog->name);
600 // restore locals from the stack
601 c = prog->xfunction->locals;
602 prog->localstack_used -= c;
603 if (prog->localstack_used < 0)
604 prog->error_cmd("PRVM_ExecuteProgram: locals stack underflow in %s", prog->name);
606 for (i=0 ; i < c ; i++)
607 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
613 prog->xfunction = prog->stack[prog->depth].f;
614 prog->stack[prog->depth].profile_acc += f->profile;
615 prog->stack[prog->depth].tprofile_acc += f->tprofile + f->tbprofile;
616 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
619 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
620 prog->stack[prog->depth-1].tprofile_acc += prog->stack[prog->depth].tprofile_acc;
621 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
625 // if f is already on the call stack...
626 // we cannot add this profile data to it now
627 // or we would add it more than once
628 // so, let's only add to the function's profile if it is the outermost call
629 f->profile_total += prog->stack[prog->depth].profile_acc;
630 f->tprofile_total += prog->stack[prog->depth].tprofile_acc;
631 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
634 return prog->stack[prog->depth].s;
637 void PRVM_Init_Exec(prvm_prog_t *prog)
641 prog->localstack_used = 0;
642 // reset the string table
646 #define OPA ((prvm_eval_t *)&prog->globals.generic[st->operand[0]])
647 #define OPB ((prvm_eval_t *)&prog->globals.generic[st->operand[1]])
648 #define OPC ((prvm_eval_t *)&prog->globals.generic[st->operand[2]])
649 extern cvar_t prvm_traceqc;
650 extern cvar_t prvm_statementprofiling;
651 extern qboolean prvm_runawaycheck;
659 void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
661 mstatement_t *st, *startst;
662 mfunction_t *f, *newf;
665 int jumpcount, cachedpr_trace, exitdepth;
666 int restorevm_tempstringsbuf_cursize;
670 calltime = Sys_DirtyTime();
672 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
674 if (PRVM_allglobaledict(self))
675 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
676 prog->error_cmd("MVM_ExecuteProgram: %s", errormessage);
679 f = &prog->functions[fnum];
681 // after executing this function, delete all tempstrings it created
682 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
684 prog->trace = prvm_traceqc.integer;
686 // we know we're done when pr_depth drops to this
687 exitdepth = prog->depth;
689 // make a stack frame
690 st = &prog->statements[PRVM_EnterFunction(prog, f)];
691 // save the starting statement pointer for profiling
692 // (when the function exits or jumps, the (st - startst) integer value is
693 // added to the function's profile counter)
696 // instead of counting instructions, we count jumps
698 // add one to the callcount of this function because otherwise engine-called functions aren't counted
699 prog->xfunction->callcount++;
702 cachedpr_trace = prog->trace;
703 if (prvm_statementprofiling.integer || prog->trace)
705 #define PRVMSLOWINTERPRETER 1
706 if (prvm_timeprofiling.integer)
708 #define PRVMTIMEPROFILING 1
709 #include "prvm_execprogram.h"
710 #undef PRVMTIMEPROFILING
714 #include "prvm_execprogram.h"
716 #undef PRVMSLOWINTERPRETER
720 if (prvm_timeprofiling.integer)
722 #define PRVMTIMEPROFILING 1
723 #include "prvm_execprogram.h"
724 #undef PRVMTIMEPROFILING
728 #include "prvm_execprogram.h"
733 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
734 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);
735 // delete tempstrings created by this function
736 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
738 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
741 if (prog == SVVM_prog)
742 SV_FlushBroadcastMessages();
750 void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
752 mstatement_t *st, *startst;
753 mfunction_t *f, *newf;
756 int jumpcount, cachedpr_trace, exitdepth;
757 int restorevm_tempstringsbuf_cursize;
761 calltime = Sys_DirtyTime();
763 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
765 if (PRVM_allglobaledict(self))
766 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
767 prog->error_cmd("CLVM_ExecuteProgram: %s", errormessage);
770 f = &prog->functions[fnum];
772 // after executing this function, delete all tempstrings it created
773 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
775 prog->trace = prvm_traceqc.integer;
777 // we know we're done when pr_depth drops to this
778 exitdepth = prog->depth;
780 // make a stack frame
781 st = &prog->statements[PRVM_EnterFunction(prog, f)];
782 // save the starting statement pointer for profiling
783 // (when the function exits or jumps, the (st - startst) integer value is
784 // added to the function's profile counter)
787 // instead of counting instructions, we count jumps
789 // add one to the callcount of this function because otherwise engine-called functions aren't counted
790 prog->xfunction->callcount++;
793 cachedpr_trace = prog->trace;
794 if (prvm_statementprofiling.integer || prog->trace)
796 #define PRVMSLOWINTERPRETER 1
797 if (prvm_timeprofiling.integer)
799 #define PRVMTIMEPROFILING 1
800 #include "prvm_execprogram.h"
801 #undef PRVMTIMEPROFILING
805 #include "prvm_execprogram.h"
807 #undef PRVMSLOWINTERPRETER
811 if (prvm_timeprofiling.integer)
813 #define PRVMTIMEPROFILING 1
814 #include "prvm_execprogram.h"
815 #undef PRVMTIMEPROFILING
819 #include "prvm_execprogram.h"
824 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
825 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);
826 // delete tempstrings created by this function
827 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
829 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
832 if (prog == SVVM_prog)
833 SV_FlushBroadcastMessages();
843 void SVVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
845 void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
848 mstatement_t *st, *startst;
849 mfunction_t *f, *newf;
852 int jumpcount, cachedpr_trace, exitdepth;
853 int restorevm_tempstringsbuf_cursize;
857 calltime = Sys_DirtyTime();
859 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
861 if (PRVM_allglobaledict(self))
862 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
863 prog->error_cmd("SVVM_ExecuteProgram: %s", errormessage);
866 f = &prog->functions[fnum];
868 // after executing this function, delete all tempstrings it created
869 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
871 prog->trace = prvm_traceqc.integer;
873 // we know we're done when pr_depth drops to this
874 exitdepth = prog->depth;
876 // make a stack frame
877 st = &prog->statements[PRVM_EnterFunction(prog, f)];
878 // save the starting statement pointer for profiling
879 // (when the function exits or jumps, the (st - startst) integer value is
880 // added to the function's profile counter)
883 // instead of counting instructions, we count jumps
885 // add one to the callcount of this function because otherwise engine-called functions aren't counted
886 prog->xfunction->callcount++;
889 cachedpr_trace = prog->trace;
890 if (prvm_statementprofiling.integer || prog->trace)
892 #define PRVMSLOWINTERPRETER 1
893 if (prvm_timeprofiling.integer)
895 #define PRVMTIMEPROFILING 1
896 #include "prvm_execprogram.h"
897 #undef PRVMTIMEPROFILING
901 #include "prvm_execprogram.h"
903 #undef PRVMSLOWINTERPRETER
907 if (prvm_timeprofiling.integer)
909 #define PRVMTIMEPROFILING 1
910 #include "prvm_execprogram.h"
911 #undef PRVMTIMEPROFILING
915 #include "prvm_execprogram.h"
920 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
921 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);
922 // delete tempstrings created by this function
923 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
925 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
928 if (prog == SVVM_prog)
929 SV_FlushBroadcastMessages();