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 prog->xfunction = func;
180 for (i = firststatement;i < endstatement;i++)
182 PRVM_PrintStatement(prog, prog->statements + i);
183 prog->statement_profile[i] = 0;
193 void PRVM_PrintFunction_f (void)
198 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
202 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1))))
205 PRVM_PrintFunctionStatements(prog, Cmd_Argv(2));
213 void PRVM_StackTrace (prvm_prog_t *prog)
218 prog->stack[prog->depth].s = prog->xstatement;
219 prog->stack[prog->depth].f = prog->xfunction;
220 for (i = prog->depth;i > 0;i--)
222 f = prog->stack[i].f;
225 Con_Print("<NULL FUNCTION>\n");
227 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);
231 void PRVM_ShortStackTrace(prvm_prog_t *prog, char *buf, size_t bufsize)
239 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
243 strlcpy(buf, "<NO PROG>", bufsize);
247 prog->stack[prog->depth].s = prog->xstatement;
248 prog->stack[prog->depth].f = prog->xfunction;
249 for (i = prog->depth;i > 0;i--)
251 f = prog->stack[i].f;
255 ? 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)
264 static void PRVM_CallProfile (prvm_prog_t *prog)
266 mfunction_t *f, *best;
270 double newprofiletime;
272 Con_Printf( "%s Call Profile:\n", prog->name );
279 for (i=0 ; i<prog->numfunctions ; i++)
281 f = &prog->functions[i];
282 if (max < f->totaltime)
290 sum += best->totaltime;
291 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(prog, best->s_name));
296 newprofiletime = Sys_DirtyTime();
297 Con_Printf("Total time since last profile reset: %9.4f\n", newprofiletime - prog->profiletime);
298 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
300 prog->profiletime = newprofiletime;
303 void PRVM_Profile (prvm_prog_t *prog, int maxfunctions, double mintime, int sortby)
305 mfunction_t *f, *best;
309 if(!prvm_timeprofiling.integer)
310 mintime *= 10000000; // count each statement as about 0.1µs
312 if(prvm_timeprofiling.integer)
313 Con_Printf( "%s Profile:\n[CallCount] [Time] [BuiltinTm] [Statement] [BuiltinCt] [TimeTotal] [StmtTotal] [BltnTotal] [self]\n", prog->name );
314 // 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
316 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", prog->name );
317 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
324 for (i=0 ; i<prog->numfunctions ; i++)
326 f = &prog->functions[i];
327 if(prvm_timeprofiling.integer)
331 if(f->first_statement < 0)
333 if (max < f->tprofile)
341 if (max < f->tprofile_total)
343 max = f->tprofile_total;
350 if (max < f->tprofile + f->tbprofile)
352 max = f->tprofile + f->tbprofile;
361 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
363 max = f->profile_total + f->builtinsprofile_total + f->callcount;
369 if (max < f->profile + f->builtinsprofile + f->callcount)
371 max = f->profile + f->builtinsprofile + f->callcount;
379 if (num < maxfunctions && max > mintime)
381 if(prvm_timeprofiling.integer)
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(prog, best->s_name));
385 // %11.6f 12345678901 12345678901 12345678901 %11.6f 12345678901 12345678901 123.45%
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(prog, best->s_name));
391 if (best->first_statement < 0)
392 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(prog, best->s_name));
393 // 12345678901 12345678901 12345678901 12345678901 123.45%
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(prog, best->s_name));
402 best->builtinsprofile = 0;
403 best->profile_total = 0;
404 best->tprofile_total = 0;
405 best->builtinsprofile_total = 0;
417 void PRVM_CallProfile_f (void)
422 Con_Print("prvm_callprofile <program name>\n");
426 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1))))
429 PRVM_CallProfile(prog);
438 void PRVM_Profile_f (void)
445 howmany = atoi(Cmd_Argv(2));
446 else if (Cmd_Argc() != 2)
448 Con_Print("prvm_profile <program name>\n");
452 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1))))
455 PRVM_Profile(prog, howmany, 0, 0);
458 void PRVM_ChildProfile_f (void)
465 howmany = atoi(Cmd_Argv(2));
466 else if (Cmd_Argc() != 2)
468 Con_Print("prvm_childprofile <program name>\n");
472 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1))))
475 PRVM_Profile(prog, howmany, 0, 1);
478 void PRVM_PrintState(prvm_prog_t *prog)
481 if (prog->statestring)
483 Con_Printf("Caller-provided information: %s\n", prog->statestring);
487 for (i = -7; i <= 0;i++)
488 if (prog->xstatement + i >= prog->xfunction->first_statement)
489 PRVM_PrintStatement(prog, prog->statements + prog->xstatement + i);
492 Con_Print("null function executing??\n");
493 PRVM_StackTrace(prog);
496 extern cvar_t prvm_errordump;
497 void PRVM_Crash(prvm_prog_t *prog)
505 PRVM_serverfunction(SV_Shutdown) = 0; // don't call SV_Shutdown on crash
507 if( prog->depth > 0 )
509 Con_Printf("QuakeC crash report for %s:\n", prog->name);
510 PRVM_PrintState(prog);
513 if(prvm_errordump.integer)
516 Host_Savegame_to(prog, va(vabuf, sizeof(vabuf), "crash-%s.dmp", prog->name));
519 // dump the stack so host_error can shutdown functions
521 prog->localstack_used = 0;
523 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
524 prog->tempstringsbuf.cursize = 0;
526 // reset the prog pointer
531 ============================================================================
534 The interpretation main loop
535 ============================================================================
542 Returns the new program statement counter
545 static int PRVM_EnterFunction (prvm_prog_t *prog, mfunction_t *f)
550 prog->error_cmd("PRVM_EnterFunction: NULL function in %s", prog->name);
552 prog->stack[prog->depth].s = prog->xstatement;
553 prog->stack[prog->depth].f = prog->xfunction;
554 prog->stack[prog->depth].profile_acc = -f->profile;
555 prog->stack[prog->depth].tprofile_acc = -f->tprofile + -f->tbprofile;
556 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
558 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
559 prog->error_cmd("stack overflow");
561 // save off any locals that the new function steps on
563 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
564 prog->error_cmd("PRVM_ExecuteProgram: locals stack overflow in %s", prog->name);
566 for (i=0 ; i < c ; i++)
567 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
568 prog->localstack_used += c;
572 for (i=0 ; i<f->numparms ; i++)
574 for (j=0 ; j<f->parm_size[i] ; j++)
576 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
583 return f->first_statement - 1; // offset the s++
591 static int PRVM_LeaveFunction (prvm_prog_t *prog)
596 if (prog->depth <= 0)
597 prog->error_cmd("prog stack underflow in %s", prog->name);
599 if (!prog->xfunction)
600 prog->error_cmd("PR_LeaveFunction: NULL function in %s", prog->name);
601 // restore locals from the stack
602 c = prog->xfunction->locals;
603 prog->localstack_used -= c;
604 if (prog->localstack_used < 0)
605 prog->error_cmd("PRVM_ExecuteProgram: locals stack underflow in %s", prog->name);
607 for (i=0 ; i < c ; i++)
608 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
614 prog->xfunction = prog->stack[prog->depth].f;
615 prog->stack[prog->depth].profile_acc += f->profile;
616 prog->stack[prog->depth].tprofile_acc += f->tprofile + f->tbprofile;
617 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
620 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
621 prog->stack[prog->depth-1].tprofile_acc += prog->stack[prog->depth].tprofile_acc;
622 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
626 // if f is already on the call stack...
627 // we cannot add this profile data to it now
628 // or we would add it more than once
629 // so, let's only add to the function's profile if it is the outermost call
630 f->profile_total += prog->stack[prog->depth].profile_acc;
631 f->tprofile_total += prog->stack[prog->depth].tprofile_acc;
632 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
635 return prog->stack[prog->depth].s;
638 void PRVM_Init_Exec(prvm_prog_t *prog)
642 prog->localstack_used = 0;
643 // reset the string table
647 #define OPA ((prvm_eval_t *)&prog->globals.generic[st->operand[0]])
648 #define OPB ((prvm_eval_t *)&prog->globals.generic[st->operand[1]])
649 #define OPC ((prvm_eval_t *)&prog->globals.generic[st->operand[2]])
650 extern cvar_t prvm_traceqc;
651 extern cvar_t prvm_statementprofiling;
652 extern qboolean prvm_runawaycheck;
660 void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
662 mstatement_t *st, *startst;
663 mfunction_t *f, *newf;
666 int jumpcount, cachedpr_trace, exitdepth;
667 int restorevm_tempstringsbuf_cursize;
671 calltime = Sys_DirtyTime();
673 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
675 if (PRVM_allglobaledict(self))
676 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
677 prog->error_cmd("MVM_ExecuteProgram: %s", errormessage);
680 f = &prog->functions[fnum];
682 // after executing this function, delete all tempstrings it created
683 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
685 prog->trace = prvm_traceqc.integer;
687 // we know we're done when pr_depth drops to this
688 exitdepth = prog->depth;
690 // make a stack frame
691 st = &prog->statements[PRVM_EnterFunction(prog, f)];
692 // save the starting statement pointer for profiling
693 // (when the function exits or jumps, the (st - startst) integer value is
694 // added to the function's profile counter)
697 // instead of counting instructions, we count jumps
699 // add one to the callcount of this function because otherwise engine-called functions aren't counted
700 prog->xfunction->callcount++;
703 cachedpr_trace = prog->trace;
704 if (prvm_statementprofiling.integer || prog->trace)
706 #define PRVMSLOWINTERPRETER 1
707 if (prvm_timeprofiling.integer)
709 #define PRVMTIMEPROFILING 1
710 #include "prvm_execprogram.h"
711 #undef PRVMTIMEPROFILING
715 #include "prvm_execprogram.h"
717 #undef PRVMSLOWINTERPRETER
721 if (prvm_timeprofiling.integer)
723 #define PRVMTIMEPROFILING 1
724 #include "prvm_execprogram.h"
725 #undef PRVMTIMEPROFILING
729 #include "prvm_execprogram.h"
734 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
735 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);
736 // delete tempstrings created by this function
737 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
739 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
742 if (prog == SVVM_prog)
743 SV_FlushBroadcastMessages();
751 void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
753 mstatement_t *st, *startst;
754 mfunction_t *f, *newf;
757 int jumpcount, cachedpr_trace, exitdepth;
758 int restorevm_tempstringsbuf_cursize;
762 calltime = Sys_DirtyTime();
764 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
766 if (PRVM_allglobaledict(self))
767 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
768 prog->error_cmd("CLVM_ExecuteProgram: %s", errormessage);
771 f = &prog->functions[fnum];
773 // after executing this function, delete all tempstrings it created
774 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
776 prog->trace = prvm_traceqc.integer;
778 // we know we're done when pr_depth drops to this
779 exitdepth = prog->depth;
781 // make a stack frame
782 st = &prog->statements[PRVM_EnterFunction(prog, f)];
783 // save the starting statement pointer for profiling
784 // (when the function exits or jumps, the (st - startst) integer value is
785 // added to the function's profile counter)
788 // instead of counting instructions, we count jumps
790 // add one to the callcount of this function because otherwise engine-called functions aren't counted
791 prog->xfunction->callcount++;
794 cachedpr_trace = prog->trace;
795 if (prvm_statementprofiling.integer || prog->trace)
797 #define PRVMSLOWINTERPRETER 1
798 if (prvm_timeprofiling.integer)
800 #define PRVMTIMEPROFILING 1
801 #include "prvm_execprogram.h"
802 #undef PRVMTIMEPROFILING
806 #include "prvm_execprogram.h"
808 #undef PRVMSLOWINTERPRETER
812 if (prvm_timeprofiling.integer)
814 #define PRVMTIMEPROFILING 1
815 #include "prvm_execprogram.h"
816 #undef PRVMTIMEPROFILING
820 #include "prvm_execprogram.h"
825 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
826 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);
827 // delete tempstrings created by this function
828 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
830 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
833 if (prog == SVVM_prog)
834 SV_FlushBroadcastMessages();
844 void SVVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
846 void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
849 mstatement_t *st, *startst;
850 mfunction_t *f, *newf;
853 int jumpcount, cachedpr_trace, exitdepth;
854 int restorevm_tempstringsbuf_cursize;
858 calltime = Sys_DirtyTime();
860 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
862 if (PRVM_allglobaledict(self))
863 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
864 prog->error_cmd("SVVM_ExecuteProgram: %s", errormessage);
867 f = &prog->functions[fnum];
869 // after executing this function, delete all tempstrings it created
870 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
872 prog->trace = prvm_traceqc.integer;
874 // we know we're done when pr_depth drops to this
875 exitdepth = prog->depth;
877 // make a stack frame
878 st = &prog->statements[PRVM_EnterFunction(prog, f)];
879 // save the starting statement pointer for profiling
880 // (when the function exits or jumps, the (st - startst) integer value is
881 // added to the function's profile counter)
884 // instead of counting instructions, we count jumps
886 // add one to the callcount of this function because otherwise engine-called functions aren't counted
887 prog->xfunction->callcount++;
890 cachedpr_trace = prog->trace;
891 if (prvm_statementprofiling.integer || prog->trace)
893 #define PRVMSLOWINTERPRETER 1
894 if (prvm_timeprofiling.integer)
896 #define PRVMTIMEPROFILING 1
897 #include "prvm_execprogram.h"
898 #undef PRVMTIMEPROFILING
902 #include "prvm_execprogram.h"
904 #undef PRVMSLOWINTERPRETER
908 if (prvm_timeprofiling.integer)
910 #define PRVMTIMEPROFILING 1
911 #include "prvm_execprogram.h"
912 #undef PRVMTIMEPROFILING
916 #include "prvm_execprogram.h"
921 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
922 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);
923 // delete tempstrings created by this function
924 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
926 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
929 if (prog == SVVM_prog)
930 SV_FlushBroadcastMessages();