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 (dstatement_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->op == OP_IF || s->op == OP_IFNOT)
150 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
151 else if (s->op == OP_GOTO)
152 Con_Printf("s%i",(signed short)s->a + opnum);
153 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
155 Con_Print(PRVM_GlobalString((unsigned short) s->a));
157 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
159 else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
162 Con_Print(PRVM_GlobalString((unsigned short) s->a));
166 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
171 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
177 Con_Print(PRVM_GlobalString((unsigned short) s->a));
181 Con_Print(PRVM_GlobalString((unsigned short) s->b));
186 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
192 void PRVM_PrintFunctionStatements (const char *name)
194 int i, firststatement, endstatement;
196 func = PRVM_ED_FindFunction (name);
199 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
202 firststatement = func->first_statement;
203 if (firststatement < 0)
205 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
209 // find the end statement
210 endstatement = prog->progs->numstatements;
211 for (i = 0;i < prog->progs->numfunctions;i++)
212 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
213 endstatement = prog->functions[i].first_statement;
215 // now print the range of statements
216 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);
217 for (i = firststatement;i < endstatement;i++)
219 PRVM_PrintStatement(prog->statements + i);
220 prog->statement_profile[i] = 0;
230 void PRVM_PrintFunction_f (void)
234 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
239 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
242 PRVM_PrintFunctionStatements(Cmd_Argv(2));
252 void PRVM_StackTrace (void)
257 prog->stack[prog->depth].s = prog->xstatement;
258 prog->stack[prog->depth].f = prog->xfunction;
259 for (i = prog->depth;i > 0;i--)
261 f = prog->stack[i].f;
264 Con_Print("<NULL FUNCTION>\n");
266 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
270 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
277 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
281 strlcpy(buf, "<NO PROG>", bufsize);
285 prog->stack[prog->depth].s = prog->xstatement;
286 prog->stack[prog->depth].f = prog->xfunction;
287 for (i = prog->depth;i > 0;i--)
289 f = prog->stack[i].f;
293 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
302 void PRVM_CallProfile (void)
304 mfunction_t *f, *best;
309 Con_Printf( "%s Call Profile:\n", PRVM_NAME );
316 for (i=0 ; i<prog->progs->numfunctions ; i++)
318 f = &prog->functions[i];
319 if (max < f->totaltime)
327 sum += best->totaltime;
328 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
333 Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
334 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
336 prog->starttime = Sys_DoubleTime();
339 void PRVM_Profile (int maxfunctions, double mintime, int sortby)
341 mfunction_t *f, *best;
345 if(!prvm_timeprofiling.integer)
346 mintime *= 10000000; // count each statement as about 0.1µs
348 if(prvm_timeprofiling.integer)
349 Con_Printf( "%s Profile:\n[CallCount] [Time] [BuiltinTm] [Statement] [BuiltinCt] [TimeTotal] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
350 // 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
352 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
353 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
360 for (i=0 ; i<prog->progs->numfunctions ; i++)
362 f = &prog->functions[i];
363 if(prvm_timeprofiling.integer)
367 if(f->first_statement < 0)
369 if (max < f->tprofile)
377 if (max < f->tprofile_total)
379 max = f->tprofile_total;
386 if (max < f->tprofile + f->tbprofile)
388 max = f->tprofile + f->tbprofile;
397 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
399 max = f->profile_total + f->builtinsprofile_total + f->callcount;
405 if (max < f->profile + f->builtinsprofile + f->callcount)
407 max = f->profile + f->builtinsprofile + f->callcount;
415 if (num < maxfunctions && max > mintime)
417 if(prvm_timeprofiling.integer)
419 if (best->first_statement < 0)
420 Con_Printf("%11.0f %11.6f ------------- builtin ------------- %11.6f ----------- builtin ----------- %s\n", best->callcount, best->tprofile, best->tprofile, PRVM_GetString(best->s_name));
421 // %11.6f 12345678901 12345678901 12345678901 %11.6f 12345678901 12345678901 123.45%
423 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));
427 if (best->first_statement < 0)
428 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
429 // 12345678901 12345678901 12345678901 12345678901 123.45%
431 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));
438 best->builtinsprofile = 0;
439 best->profile_total = 0;
440 best->tprofile_total = 0;
441 best->builtinsprofile_total = 0;
453 void PRVM_CallProfile_f (void)
457 Con_Print("prvm_callprofile <program name>\n");
462 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
476 void PRVM_Profile_f (void)
482 howmany = atoi(Cmd_Argv(2));
483 else if (Cmd_Argc() != 2)
485 Con_Print("prvm_profile <program name>\n");
490 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
493 PRVM_Profile(howmany, 0, 0);
498 void PRVM_ChildProfile_f (void)
504 howmany = atoi(Cmd_Argv(2));
505 else if (Cmd_Argc() != 2)
507 Con_Print("prvm_childprofile <program name>\n");
512 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
515 PRVM_Profile(howmany, 0, 1);
520 void PRVM_CrashAll(void)
523 prvm_prog_t *oldprog = prog;
525 for(i = 0; i < PRVM_MAXPROGS; i++)
527 if(!PRVM_ProgLoaded(i))
536 void PRVM_PrintState(void)
539 if(prog->statestring)
541 Con_Printf("Caller-provided information: %s\n", prog->statestring);
545 for (i = -7; i <= 0;i++)
546 if (prog->xstatement + i >= prog->xfunction->first_statement)
547 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
550 Con_Print("null function executing??\n");
554 extern sizebuf_t vm_tempstringsbuf;
555 extern cvar_t prvm_errordump;
556 void Host_Savegame_to (const char *name);
557 void PRVM_Crash(void)
562 prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
564 if( prog->depth > 0 )
566 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
570 if(prvm_errordump.integer)
573 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
576 // dump the stack so host_error can shutdown functions
578 prog->localstack_used = 0;
580 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
581 vm_tempstringsbuf.cursize = 0;
583 // reset the prog pointer
588 ============================================================================
591 The interpretation main loop
592 ============================================================================
599 Returns the new program statement counter
602 int PRVM_EnterFunction (mfunction_t *f)
607 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
609 prog->stack[prog->depth].s = prog->xstatement;
610 prog->stack[prog->depth].f = prog->xfunction;
611 prog->stack[prog->depth].profile_acc = -f->profile;
612 prog->stack[prog->depth].tprofile_acc = -f->tprofile + -f->tbprofile;
613 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
615 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
616 PRVM_ERROR ("stack overflow");
618 // save off any locals that the new function steps on
620 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
621 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
623 for (i=0 ; i < c ; i++)
624 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
625 prog->localstack_used += c;
629 for (i=0 ; i<f->numparms ; i++)
631 for (j=0 ; j<f->parm_size[i] ; j++)
633 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
640 return f->first_statement - 1; // offset the s++
648 int PRVM_LeaveFunction (void)
653 if (prog->depth <= 0)
654 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
656 if (!prog->xfunction)
657 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
658 // restore locals from the stack
659 c = prog->xfunction->locals;
660 prog->localstack_used -= c;
661 if (prog->localstack_used < 0)
662 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
664 for (i=0 ; i < c ; i++)
665 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
671 prog->xfunction = prog->stack[prog->depth].f;
672 prog->stack[prog->depth].profile_acc += f->profile;
673 prog->stack[prog->depth].tprofile_acc += f->tprofile + f->tbprofile;
674 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
677 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
678 prog->stack[prog->depth-1].tprofile_acc += prog->stack[prog->depth].tprofile_acc;
679 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
683 // if f is already on the call stack...
684 // we cannot add this profile data to it now
685 // or we would add it more than once
686 // so, let's only add to the function's profile if it is the outermost call
687 f->profile_total += prog->stack[prog->depth].profile_acc;
688 f->tprofile_total += prog->stack[prog->depth].tprofile_acc;
689 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
692 return prog->stack[prog->depth].s;
695 void PRVM_Init_Exec(void)
699 prog->localstack_used = 0;
700 // reset the string table
704 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
705 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
706 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
707 extern cvar_t prvm_traceqc;
708 extern cvar_t prvm_statementprofiling;
709 extern sizebuf_t vm_tempstringsbuf;
710 extern qboolean prvm_runawaycheck;
718 void MVM_ExecuteProgram (func_t fnum, const char *errormessage)
720 dstatement_t *st, *startst;
721 mfunction_t *f, *newf;
724 int jumpcount, cachedpr_trace, exitdepth;
725 int restorevm_tempstringsbuf_cursize;
729 calltime = Sys_DoubleTime();
731 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
733 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
734 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
735 PRVM_ERROR ("MVM_ExecuteProgram: %s", errormessage);
738 f = &prog->functions[fnum];
740 // after executing this function, delete all tempstrings it created
741 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
743 prog->trace = prvm_traceqc.integer;
745 // we know we're done when pr_depth drops to this
746 exitdepth = prog->depth;
748 // make a stack frame
749 st = &prog->statements[PRVM_EnterFunction (f)];
750 // save the starting statement pointer for profiling
751 // (when the function exits or jumps, the (st - startst) integer value is
752 // added to the function's profile counter)
755 // instead of counting instructions, we count jumps
757 // add one to the callcount of this function because otherwise engine-called functions aren't counted
758 prog->xfunction->callcount++;
761 cachedpr_trace = prog->trace;
762 if (prvm_statementprofiling.integer || prog->trace)
764 #define PRVMSLOWINTERPRETER 1
765 if (prvm_timeprofiling.integer)
767 #define PRVMTIMEPROFILING 1
768 #include "prvm_execprogram.h"
769 #undef PRVMTIMEPROFILING
773 #include "prvm_execprogram.h"
775 #undef PRVMSLOWINTERPRETER
779 if (prvm_timeprofiling.integer)
781 #define PRVMTIMEPROFILING 1
782 #include "prvm_execprogram.h"
783 #undef PRVMTIMEPROFILING
787 #include "prvm_execprogram.h"
792 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
793 Con_DPrintf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
794 // delete tempstrings created by this function
795 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
797 f->totaltime += (Sys_DoubleTime() - calltime);
799 SV_FlushBroadcastMessages();
807 void CLVM_ExecuteProgram (func_t fnum, const char *errormessage)
809 dstatement_t *st, *startst;
810 mfunction_t *f, *newf;
813 int jumpcount, cachedpr_trace, exitdepth;
814 int restorevm_tempstringsbuf_cursize;
818 calltime = Sys_DoubleTime();
820 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
822 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
823 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
824 PRVM_ERROR ("CLVM_ExecuteProgram: %s", errormessage);
827 f = &prog->functions[fnum];
829 // after executing this function, delete all tempstrings it created
830 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
832 prog->trace = prvm_traceqc.integer;
834 // we know we're done when pr_depth drops to this
835 exitdepth = prog->depth;
837 // make a stack frame
838 st = &prog->statements[PRVM_EnterFunction (f)];
839 // save the starting statement pointer for profiling
840 // (when the function exits or jumps, the (st - startst) integer value is
841 // added to the function's profile counter)
844 // instead of counting instructions, we count jumps
846 // add one to the callcount of this function because otherwise engine-called functions aren't counted
847 prog->xfunction->callcount++;
850 cachedpr_trace = prog->trace;
851 if (prvm_statementprofiling.integer || prog->trace)
853 #define PRVMSLOWINTERPRETER 1
854 if (prvm_timeprofiling.integer)
856 #define PRVMTIMEPROFILING 1
857 #include "prvm_execprogram.h"
858 #undef PRVMTIMEPROFILING
862 #include "prvm_execprogram.h"
864 #undef PRVMSLOWINTERPRETER
868 if (prvm_timeprofiling.integer)
870 #define PRVMTIMEPROFILING 1
871 #include "prvm_execprogram.h"
872 #undef PRVMTIMEPROFILING
876 #include "prvm_execprogram.h"
881 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
882 Con_DPrintf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
883 // delete tempstrings created by this function
884 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
886 f->totaltime += (Sys_DoubleTime() - calltime);
888 SV_FlushBroadcastMessages();
897 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage)
899 dstatement_t *st, *startst;
900 mfunction_t *f, *newf;
903 int jumpcount, cachedpr_trace, exitdepth;
904 int restorevm_tempstringsbuf_cursize;
908 calltime = Sys_DoubleTime();
910 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
912 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
913 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
914 PRVM_ERROR ("SVVM_ExecuteProgram: %s", errormessage);
917 f = &prog->functions[fnum];
919 // after executing this function, delete all tempstrings it created
920 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
922 prog->trace = prvm_traceqc.integer;
924 // we know we're done when pr_depth drops to this
925 exitdepth = prog->depth;
927 // make a stack frame
928 st = &prog->statements[PRVM_EnterFunction (f)];
929 // save the starting statement pointer for profiling
930 // (when the function exits or jumps, the (st - startst) integer value is
931 // added to the function's profile counter)
934 // instead of counting instructions, we count jumps
936 // add one to the callcount of this function because otherwise engine-called functions aren't counted
937 prog->xfunction->callcount++;
940 cachedpr_trace = prog->trace;
941 if (prvm_statementprofiling.integer || prog->trace)
943 #define PRVMSLOWINTERPRETER 1
944 if (prvm_timeprofiling.integer)
946 #define PRVMTIMEPROFILING 1
947 #include "prvm_execprogram.h"
948 #undef PRVMTIMEPROFILING
952 #include "prvm_execprogram.h"
954 #undef PRVMSLOWINTERPRETER
958 if (prvm_timeprofiling.integer)
960 #define PRVMTIMEPROFILING 1
961 #include "prvm_execprogram.h"
962 #undef PRVMTIMEPROFILING
966 #include "prvm_execprogram.h"
971 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
972 Con_DPrintf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
973 // delete tempstrings created by this function
974 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
976 f->totaltime += (Sys_DoubleTime() - calltime);
978 SV_FlushBroadcastMessages();