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[] =
113 char *PRVM_GlobalString (int ofs);
114 char *PRVM_GlobalStringNoContents (int ofs);
115 extern ddef_t *PRVM_ED_FieldAtOfs(int ofs);
118 //=============================================================================
125 extern cvar_t prvm_statementprofiling;
126 extern cvar_t prvm_timeprofiling;
127 void PRVM_PrintStatement(mstatement_t *s)
130 int opnum = (int)(s - prog->statements);
132 Con_Printf("s%i: ", opnum);
133 if( prog->statement_linenums )
134 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
136 if (prvm_statementprofiling.integer)
137 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
139 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
141 Con_Printf("%s ", prvm_opnames[s->op]);
142 i = strlen(prvm_opnames[s->op]);
143 // don't count a preceding color tag when padding the name
144 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
149 if (s->operand[0] >= 0) Con_Printf( "%s", PRVM_GlobalString(s->operand[0]));
150 if (s->operand[1] >= 0) Con_Printf(", %s", PRVM_GlobalString(s->operand[1]));
151 if (s->operand[2] >= 0) Con_Printf(", %s", PRVM_GlobalString(s->operand[2]));
152 if (s->jumpabsolute >= 0) Con_Printf(", statement %i", s->jumpabsolute);
156 void PRVM_PrintFunctionStatements (const char *name)
158 int i, firststatement, endstatement;
160 func = PRVM_ED_FindFunction (name);
163 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
166 firststatement = func->first_statement;
167 if (firststatement < 0)
169 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
173 // find the end statement
174 endstatement = prog->numstatements;
175 for (i = 0;i < prog->numfunctions;i++)
176 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
177 endstatement = prog->functions[i].first_statement;
179 // now print the range of statements
180 Con_Printf("%s progs: disassembly of function %s (statements %i-%i, locals %i-%i):\n", PRVM_NAME, name, firststatement, endstatement, func->parm_start, func->parm_start + func->locals - 1);
181 for (i = firststatement;i < endstatement;i++)
183 PRVM_PrintStatement(prog->statements + i);
184 prog->statement_profile[i] = 0;
194 void PRVM_PrintFunction_f (void)
198 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
203 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
206 PRVM_PrintFunctionStatements(Cmd_Argv(2));
216 void PRVM_StackTrace (void)
221 prog->stack[prog->depth].s = prog->xstatement;
222 prog->stack[prog->depth].f = prog->xfunction;
223 for (i = prog->depth;i > 0;i--)
225 f = prog->stack[i].f;
228 Con_Print("<NULL FUNCTION>\n");
230 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
234 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
241 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
245 strlcpy(buf, "<NO PROG>", bufsize);
249 prog->stack[prog->depth].s = prog->xstatement;
250 prog->stack[prog->depth].f = prog->xfunction;
251 for (i = prog->depth;i > 0;i--)
253 f = prog->stack[i].f;
257 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
266 void PRVM_CallProfile (void)
268 mfunction_t *f, *best;
273 Con_Printf( "%s Call Profile:\n", PRVM_NAME );
280 for (i=0 ; i<prog->numfunctions ; i++)
282 f = &prog->functions[i];
283 if (max < f->totaltime)
291 sum += best->totaltime;
292 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
297 Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
298 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
300 prog->starttime = Sys_DoubleTime();
303 void PRVM_Profile (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", PRVM_NAME );
314 // 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
316 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_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(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(best->s_name));
391 if (best->first_statement < 0)
392 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
393 // 12345678901 12345678901 12345678901 12345678901 123.45%
395 Con_Printf("%11.0f %11.0f %11.0f %11.0f %11.0f %6.2f%% %s\n", best->callcount, best->profile, best->builtinsprofile, best->profile_total, best->builtinsprofile_total, (best->profile + best->builtinsprofile) * 100.0 / (best->profile_total + best->builtinsprofile_total), PRVM_GetString(best->s_name));
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)
421 Con_Print("prvm_callprofile <program name>\n");
426 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
440 void PRVM_Profile_f (void)
446 howmany = atoi(Cmd_Argv(2));
447 else if (Cmd_Argc() != 2)
449 Con_Print("prvm_profile <program name>\n");
454 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
457 PRVM_Profile(howmany, 0, 0);
462 void PRVM_ChildProfile_f (void)
468 howmany = atoi(Cmd_Argv(2));
469 else if (Cmd_Argc() != 2)
471 Con_Print("prvm_childprofile <program name>\n");
476 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
479 PRVM_Profile(howmany, 0, 1);
484 void PRVM_CrashAll(void)
487 prvm_prog_t *oldprog = prog;
489 for(i = 0; i < PRVM_MAXPROGS; i++)
491 if(!PRVM_ProgLoaded(i))
500 void PRVM_PrintState(void)
503 if(prog->statestring)
505 Con_Printf("Caller-provided information: %s\n", prog->statestring);
509 for (i = -7; i <= 0;i++)
510 if (prog->xstatement + i >= prog->xfunction->first_statement)
511 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
514 Con_Print("null function executing??\n");
518 extern sizebuf_t vm_tempstringsbuf;
519 extern cvar_t prvm_errordump;
520 void Host_Savegame_to (const char *name);
521 void PRVM_Crash(void)
526 PRVM_serverfunction(SV_Shutdown) = 0; // don't call SV_Shutdown on crash
528 if( prog->depth > 0 )
530 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
534 if(prvm_errordump.integer)
537 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
540 // dump the stack so host_error can shutdown functions
542 prog->localstack_used = 0;
544 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
545 vm_tempstringsbuf.cursize = 0;
547 // reset the prog pointer
552 ============================================================================
555 The interpretation main loop
556 ============================================================================
563 Returns the new program statement counter
566 int PRVM_EnterFunction (mfunction_t *f)
571 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
573 prog->stack[prog->depth].s = prog->xstatement;
574 prog->stack[prog->depth].f = prog->xfunction;
575 prog->stack[prog->depth].profile_acc = -f->profile;
576 prog->stack[prog->depth].tprofile_acc = -f->tprofile + -f->tbprofile;
577 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
579 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
580 PRVM_ERROR ("stack overflow");
582 // save off any locals that the new function steps on
584 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
585 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
587 for (i=0 ; i < c ; i++)
588 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
589 prog->localstack_used += c;
593 for (i=0 ; i<f->numparms ; i++)
595 for (j=0 ; j<f->parm_size[i] ; j++)
597 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
604 return f->first_statement - 1; // offset the s++
612 int PRVM_LeaveFunction (void)
617 if (prog->depth <= 0)
618 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
620 if (!prog->xfunction)
621 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
622 // restore locals from the stack
623 c = prog->xfunction->locals;
624 prog->localstack_used -= c;
625 if (prog->localstack_used < 0)
626 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
628 for (i=0 ; i < c ; i++)
629 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
635 prog->xfunction = prog->stack[prog->depth].f;
636 prog->stack[prog->depth].profile_acc += f->profile;
637 prog->stack[prog->depth].tprofile_acc += f->tprofile + f->tbprofile;
638 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
641 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
642 prog->stack[prog->depth-1].tprofile_acc += prog->stack[prog->depth].tprofile_acc;
643 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
647 // if f is already on the call stack...
648 // we cannot add this profile data to it now
649 // or we would add it more than once
650 // so, let's only add to the function's profile if it is the outermost call
651 f->profile_total += prog->stack[prog->depth].profile_acc;
652 f->tprofile_total += prog->stack[prog->depth].tprofile_acc;
653 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
656 return prog->stack[prog->depth].s;
659 void PRVM_Init_Exec(void)
663 prog->localstack_used = 0;
664 // reset the string table
668 #define OPA ((prvm_eval_t *)&prog->globals.generic[st->operand[0]])
669 #define OPB ((prvm_eval_t *)&prog->globals.generic[st->operand[1]])
670 #define OPC ((prvm_eval_t *)&prog->globals.generic[st->operand[2]])
671 extern cvar_t prvm_traceqc;
672 extern cvar_t prvm_statementprofiling;
673 extern sizebuf_t vm_tempstringsbuf;
674 extern qboolean prvm_runawaycheck;
682 void MVM_ExecuteProgram (func_t fnum, const char *errormessage)
684 mstatement_t *st, *startst;
685 mfunction_t *f, *newf;
688 int jumpcount, cachedpr_trace, exitdepth;
689 int restorevm_tempstringsbuf_cursize;
693 calltime = Sys_DoubleTime();
695 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
697 if (PRVM_allglobaledict(self))
698 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
699 PRVM_ERROR ("MVM_ExecuteProgram: %s", errormessage);
702 f = &prog->functions[fnum];
704 // after executing this function, delete all tempstrings it created
705 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
707 prog->trace = prvm_traceqc.integer;
709 // we know we're done when pr_depth drops to this
710 exitdepth = prog->depth;
712 // make a stack frame
713 st = &prog->statements[PRVM_EnterFunction (f)];
714 // save the starting statement pointer for profiling
715 // (when the function exits or jumps, the (st - startst) integer value is
716 // added to the function's profile counter)
719 // instead of counting instructions, we count jumps
721 // add one to the callcount of this function because otherwise engine-called functions aren't counted
722 prog->xfunction->callcount++;
725 cachedpr_trace = prog->trace;
726 if (prvm_statementprofiling.integer || prog->trace)
728 #define PRVMSLOWINTERPRETER 1
729 if (prvm_timeprofiling.integer)
731 #define PRVMTIMEPROFILING 1
732 #include "prvm_execprogram.h"
733 #undef PRVMTIMEPROFILING
737 #include "prvm_execprogram.h"
739 #undef PRVMSLOWINTERPRETER
743 if (prvm_timeprofiling.integer)
745 #define PRVMTIMEPROFILING 1
746 #include "prvm_execprogram.h"
747 #undef PRVMTIMEPROFILING
751 #include "prvm_execprogram.h"
756 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
757 Con_DPrintf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
758 // delete tempstrings created by this function
759 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
761 f->totaltime += (Sys_DoubleTime() - calltime);
763 SV_FlushBroadcastMessages();
771 void CLVM_ExecuteProgram (func_t fnum, const char *errormessage)
773 mstatement_t *st, *startst;
774 mfunction_t *f, *newf;
777 int jumpcount, cachedpr_trace, exitdepth;
778 int restorevm_tempstringsbuf_cursize;
782 calltime = Sys_DoubleTime();
784 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
786 if (PRVM_allglobaledict(self))
787 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
788 PRVM_ERROR ("CLVM_ExecuteProgram: %s", errormessage);
791 f = &prog->functions[fnum];
793 // after executing this function, delete all tempstrings it created
794 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
796 prog->trace = prvm_traceqc.integer;
798 // we know we're done when pr_depth drops to this
799 exitdepth = prog->depth;
801 // make a stack frame
802 st = &prog->statements[PRVM_EnterFunction (f)];
803 // save the starting statement pointer for profiling
804 // (when the function exits or jumps, the (st - startst) integer value is
805 // added to the function's profile counter)
808 // instead of counting instructions, we count jumps
810 // add one to the callcount of this function because otherwise engine-called functions aren't counted
811 prog->xfunction->callcount++;
814 cachedpr_trace = prog->trace;
815 if (prvm_statementprofiling.integer || prog->trace)
817 #define PRVMSLOWINTERPRETER 1
818 if (prvm_timeprofiling.integer)
820 #define PRVMTIMEPROFILING 1
821 #include "prvm_execprogram.h"
822 #undef PRVMTIMEPROFILING
826 #include "prvm_execprogram.h"
828 #undef PRVMSLOWINTERPRETER
832 if (prvm_timeprofiling.integer)
834 #define PRVMTIMEPROFILING 1
835 #include "prvm_execprogram.h"
836 #undef PRVMTIMEPROFILING
840 #include "prvm_execprogram.h"
845 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
846 Con_DPrintf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
847 // delete tempstrings created by this function
848 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
850 f->totaltime += (Sys_DoubleTime() - calltime);
852 SV_FlushBroadcastMessages();
861 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage)
863 mstatement_t *st, *startst;
864 mfunction_t *f, *newf;
867 int jumpcount, cachedpr_trace, exitdepth;
868 int restorevm_tempstringsbuf_cursize;
872 calltime = Sys_DoubleTime();
874 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
876 if (PRVM_allglobaledict(self))
877 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
878 PRVM_ERROR ("SVVM_ExecuteProgram: %s", errormessage);
881 f = &prog->functions[fnum];
883 // after executing this function, delete all tempstrings it created
884 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
886 prog->trace = prvm_traceqc.integer;
888 // we know we're done when pr_depth drops to this
889 exitdepth = prog->depth;
891 // make a stack frame
892 st = &prog->statements[PRVM_EnterFunction (f)];
893 // save the starting statement pointer for profiling
894 // (when the function exits or jumps, the (st - startst) integer value is
895 // added to the function's profile counter)
898 // instead of counting instructions, we count jumps
900 // add one to the callcount of this function because otherwise engine-called functions aren't counted
901 prog->xfunction->callcount++;
904 cachedpr_trace = prog->trace;
905 if (prvm_statementprofiling.integer || prog->trace)
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 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
936 Con_DPrintf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
937 // delete tempstrings created by this function
938 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
940 f->totaltime += (Sys_DoubleTime() - calltime);
942 SV_FlushBroadcastMessages();