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 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 void PRVM_PrintStatement (dstatement_t *s)
129 int opnum = (int)(s - prog->statements);
131 Con_Printf("s%i: ", opnum);
132 if( prog->statement_linenums )
133 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
135 if (prvm_statementprofiling.integer)
136 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
138 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
140 Con_Printf("%s ", prvm_opnames[s->op]);
141 i = strlen(prvm_opnames[s->op]);
142 // don't count a preceding color tag when padding the name
143 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
148 if (s->op == OP_IF || s->op == OP_IFNOT)
149 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
150 else if (s->op == OP_GOTO)
151 Con_Printf("s%i",(signed short)s->a + opnum);
152 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
154 Con_Print(PRVM_GlobalString((unsigned short) s->a));
156 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
158 else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
161 Con_Print(PRVM_GlobalString((unsigned short) s->a));
165 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
170 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
176 Con_Print(PRVM_GlobalString((unsigned short) s->a));
180 Con_Print(PRVM_GlobalString((unsigned short) s->b));
185 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
191 void PRVM_PrintFunctionStatements (const char *name)
193 int i, firststatement, endstatement;
195 func = PRVM_ED_FindFunction (name);
198 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
201 firststatement = func->first_statement;
202 if (firststatement < 0)
204 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
208 // find the end statement
209 endstatement = prog->progs->numstatements;
210 for (i = 0;i < prog->progs->numfunctions;i++)
211 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
212 endstatement = prog->functions[i].first_statement;
214 // now print the range of statements
215 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);
216 for (i = firststatement;i < endstatement;i++)
218 PRVM_PrintStatement(prog->statements + i);
219 prog->statement_profile[i] = 0;
229 void PRVM_PrintFunction_f (void)
233 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
238 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
241 PRVM_PrintFunctionStatements(Cmd_Argv(2));
251 void PRVM_StackTrace (void)
256 prog->stack[prog->depth].s = prog->xstatement;
257 prog->stack[prog->depth].f = prog->xfunction;
258 for (i = prog->depth;i > 0;i--)
260 f = prog->stack[i].f;
263 Con_Print("<NULL FUNCTION>\n");
265 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
269 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
276 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
280 strlcpy(buf, "<NO PROG>", bufsize);
284 prog->stack[prog->depth].s = prog->xstatement;
285 prog->stack[prog->depth].f = prog->xfunction;
286 for (i = prog->depth;i > 0;i--)
288 f = prog->stack[i].f;
292 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
301 void PRVM_CallProfile (void)
303 mfunction_t *f, *best;
308 Con_Printf( "%s Call Profile:\n", PRVM_NAME );
315 for (i=0 ; i<prog->progs->numfunctions ; i++)
317 f = &prog->functions[i];
318 if (max < f->totaltime)
326 sum += best->totaltime;
327 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
332 Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
333 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
335 prog->starttime = Sys_DoubleTime();
338 void PRVM_Profile (int maxfunctions, int mininstructions, int sortby)
340 mfunction_t *f, *best;
344 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
345 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
352 for (i=0 ; i<prog->progs->numfunctions ; i++)
354 f = &prog->functions[i];
357 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
359 max = f->profile_total + f->builtinsprofile_total + f->callcount;
365 if (max < f->profile + f->builtinsprofile + f->callcount)
367 max = f->profile + f->builtinsprofile + f->callcount;
374 if (num < maxfunctions && max >= mininstructions)
376 if (best->first_statement < 0)
377 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
378 // 12345678901 12345678901 12345678901 12345678901 123.45%
380 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));
384 best->builtinsprofile = 0;
385 best->profile_total = 0;
386 best->builtinsprofile_total = 0;
398 void PRVM_CallProfile_f (void)
402 Con_Print("prvm_callprofile <program name>\n");
407 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
421 void PRVM_Profile_f (void)
427 howmany = atoi(Cmd_Argv(2));
428 else if (Cmd_Argc() != 2)
430 Con_Print("prvm_profile <program name>\n");
435 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
438 PRVM_Profile(howmany, 1, 0);
443 void PRVM_ChildProfile_f (void)
449 howmany = atoi(Cmd_Argv(2));
450 else if (Cmd_Argc() != 2)
452 Con_Print("prvm_childprofile <program name>\n");
457 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
460 PRVM_Profile(howmany, 1, 1);
465 void PRVM_CrashAll(void)
468 prvm_prog_t *oldprog = prog;
470 for(i = 0; i < PRVM_MAXPROGS; i++)
472 if(!PRVM_ProgLoaded(i))
481 void PRVM_PrintState(void)
484 if(prog->statestring)
486 Con_Printf("Caller-provided information: %s\n", prog->statestring);
490 for (i = -7; i <= 0;i++)
491 if (prog->xstatement + i >= prog->xfunction->first_statement)
492 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
495 Con_Print("null function executing??\n");
499 extern sizebuf_t vm_tempstringsbuf;
500 extern cvar_t prvm_errordump;
501 void Host_Savegame_to (const char *name);
502 void PRVM_Crash(void)
507 prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
509 if( prog->depth > 0 )
511 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
515 if(prvm_errordump.integer)
518 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
521 // dump the stack so host_error can shutdown functions
523 prog->localstack_used = 0;
525 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
526 vm_tempstringsbuf.cursize = 0;
528 // reset the prog pointer
533 ============================================================================
536 The interpretation main loop
537 ============================================================================
544 Returns the new program statement counter
547 int PRVM_EnterFunction (mfunction_t *f)
552 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
554 prog->stack[prog->depth].s = prog->xstatement;
555 prog->stack[prog->depth].f = prog->xfunction;
556 prog->stack[prog->depth].profile_acc = -f->profile;
557 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
559 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
560 PRVM_ERROR ("stack overflow");
562 // save off any locals that the new function steps on
564 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
565 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
567 for (i=0 ; i < c ; i++)
568 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
569 prog->localstack_used += c;
573 for (i=0 ; i<f->numparms ; i++)
575 for (j=0 ; j<f->parm_size[i] ; j++)
577 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
584 return f->first_statement - 1; // offset the s++
592 int PRVM_LeaveFunction (void)
597 if (prog->depth <= 0)
598 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
600 if (!prog->xfunction)
601 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
602 // restore locals from the stack
603 c = prog->xfunction->locals;
604 prog->localstack_used -= c;
605 if (prog->localstack_used < 0)
606 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
608 for (i=0 ; i < c ; i++)
609 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
615 prog->xfunction = prog->stack[prog->depth].f;
616 prog->stack[prog->depth].profile_acc += f->profile;
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].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->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
633 return prog->stack[prog->depth].s;
636 void PRVM_Init_Exec(void)
640 prog->localstack_used = 0;
641 // reset the string table
645 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
646 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
647 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
648 extern cvar_t prvm_traceqc;
649 extern cvar_t prvm_statementprofiling;
650 extern sizebuf_t vm_tempstringsbuf;
651 extern qboolean prvm_runawaycheck;
659 void MVM_ExecuteProgram (func_t fnum, const char *errormessage)
661 dstatement_t *st, *startst;
662 mfunction_t *f, *newf;
665 int jumpcount, cachedpr_trace, exitdepth;
666 int restorevm_tempstringsbuf_cursize;
669 calltime = Sys_DoubleTime();
671 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
673 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
674 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
675 PRVM_ERROR ("MVM_ExecuteProgram: %s", errormessage);
678 f = &prog->functions[fnum];
680 // after executing this function, delete all tempstrings it created
681 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
683 prog->trace = prvm_traceqc.integer;
685 // we know we're done when pr_depth drops to this
686 exitdepth = prog->depth;
688 // make a stack frame
689 st = &prog->statements[PRVM_EnterFunction (f)];
690 // save the starting statement pointer for profiling
691 // (when the function exits or jumps, the (st - startst) integer value is
692 // added to the function's profile counter)
694 // instead of counting instructions, we count jumps
696 // add one to the callcount of this function because otherwise engine-called functions aren't counted
697 prog->xfunction->callcount++;
700 cachedpr_trace = prog->trace;
701 if (prvm_statementprofiling.integer || prog->trace)
703 #define PRVMSLOWINTERPRETER 1
704 #include "prvm_execprogram.h"
705 #undef PRVMSLOWINTERPRETER
709 #include "prvm_execprogram.h"
713 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
714 Con_DPrintf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
715 // delete tempstrings created by this function
716 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
718 f->totaltime += (Sys_DoubleTime() - calltime);
720 SV_FlushBroadcastMessages();
728 void CLVM_ExecuteProgram (func_t fnum, const char *errormessage)
730 dstatement_t *st, *startst;
731 mfunction_t *f, *newf;
734 int jumpcount, cachedpr_trace, exitdepth;
735 int restorevm_tempstringsbuf_cursize;
738 calltime = Sys_DoubleTime();
740 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
742 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
743 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
744 PRVM_ERROR ("CLVM_ExecuteProgram: %s", errormessage);
747 f = &prog->functions[fnum];
749 // after executing this function, delete all tempstrings it created
750 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
752 prog->trace = prvm_traceqc.integer;
754 // we know we're done when pr_depth drops to this
755 exitdepth = prog->depth;
757 // make a stack frame
758 st = &prog->statements[PRVM_EnterFunction (f)];
759 // save the starting statement pointer for profiling
760 // (when the function exits or jumps, the (st - startst) integer value is
761 // added to the function's profile counter)
763 // instead of counting instructions, we count jumps
765 // add one to the callcount of this function because otherwise engine-called functions aren't counted
766 prog->xfunction->callcount++;
769 cachedpr_trace = prog->trace;
770 if (prvm_statementprofiling.integer || prog->trace)
772 #define PRVMSLOWINTERPRETER 1
773 #include "prvm_execprogram.h"
774 #undef PRVMSLOWINTERPRETER
778 #include "prvm_execprogram.h"
782 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
783 Con_DPrintf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
784 // delete tempstrings created by this function
785 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
787 f->totaltime += (Sys_DoubleTime() - calltime);
789 SV_FlushBroadcastMessages();
798 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage)
800 dstatement_t *st, *startst;
801 mfunction_t *f, *newf;
804 int jumpcount, cachedpr_trace, exitdepth;
805 int restorevm_tempstringsbuf_cursize;
808 calltime = Sys_DoubleTime();
810 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
812 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
813 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
814 PRVM_ERROR ("SVVM_ExecuteProgram: %s", errormessage);
817 f = &prog->functions[fnum];
819 // after executing this function, delete all tempstrings it created
820 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
822 prog->trace = prvm_traceqc.integer;
824 // we know we're done when pr_depth drops to this
825 exitdepth = prog->depth;
827 // make a stack frame
828 st = &prog->statements[PRVM_EnterFunction (f)];
829 // save the starting statement pointer for profiling
830 // (when the function exits or jumps, the (st - startst) integer value is
831 // added to the function's profile counter)
833 // instead of counting instructions, we count jumps
835 // add one to the callcount of this function because otherwise engine-called functions aren't counted
836 prog->xfunction->callcount++;
839 cachedpr_trace = prog->trace;
840 if (prvm_statementprofiling.integer || prog->trace)
842 #define PRVMSLOWINTERPRETER 1
843 #include "prvm_execprogram.h"
844 #undef PRVMSLOWINTERPRETER
848 #include "prvm_execprogram.h"
852 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
853 Con_DPrintf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
854 // delete tempstrings created by this function
855 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
857 f->totaltime += (Sys_DoubleTime() - calltime);
859 SV_FlushBroadcastMessages();