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);
117 //=============================================================================
124 extern cvar_t prvm_statementprofiling;
125 void PRVM_PrintStatement (dstatement_t *s)
128 int opnum = (int)(s - prog->statements);
130 Con_Printf("s%i: ", opnum);
131 if( prog->statement_linenums )
132 Con_Printf( "%s:%i: ", PRVM_GetString( 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->op == OP_IF || s->op == OP_IFNOT)
148 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
149 else if (s->op == OP_GOTO)
150 Con_Printf("s%i",(signed short)s->a + opnum);
151 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
153 Con_Print(PRVM_GlobalString((unsigned short) s->a));
155 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
157 else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
160 Con_Print(PRVM_GlobalString((unsigned short) s->a));
164 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
169 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
175 Con_Print(PRVM_GlobalString((unsigned short) s->a));
179 Con_Print(PRVM_GlobalString((unsigned short) s->b));
184 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
190 void PRVM_PrintFunctionStatements (const char *name)
192 int i, firststatement, endstatement;
194 func = PRVM_ED_FindFunction (name);
197 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
200 firststatement = func->first_statement;
201 if (firststatement < 0)
203 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
207 // find the end statement
208 endstatement = prog->progs->numstatements;
209 for (i = 0;i < prog->progs->numfunctions;i++)
210 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
211 endstatement = prog->functions[i].first_statement;
213 // now print the range of statements
214 Con_Printf("%s progs: disassembly of function %s (statements %i-%i):\n", PRVM_NAME, name, firststatement, endstatement);
215 for (i = firststatement;i < endstatement;i++)
217 PRVM_PrintStatement(prog->statements + i);
218 prog->statement_profile[i] = 0;
228 void PRVM_PrintFunction_f (void)
232 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
237 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
240 PRVM_PrintFunctionStatements(Cmd_Argv(2));
250 void PRVM_StackTrace (void)
255 prog->stack[prog->depth].s = prog->xstatement;
256 prog->stack[prog->depth].f = prog->xfunction;
257 for (i = prog->depth;i > 0;i--)
259 f = prog->stack[i].f;
262 Con_Print("<NULL FUNCTION>\n");
264 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
268 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
275 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
279 strlcpy(buf, "<NO PROG>", bufsize);
283 prog->stack[prog->depth].s = prog->xstatement;
284 prog->stack[prog->depth].f = prog->xfunction;
285 for (i = prog->depth;i > 0;i--)
287 f = prog->stack[i].f;
291 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
300 void PRVM_CallProfile (void)
302 mfunction_t *f, *best;
307 Con_Printf( "%s Call Profile:\n", PRVM_NAME );
314 for (i=0 ; i<prog->progs->numfunctions ; i++)
316 f = &prog->functions[i];
317 if (max < f->totaltime)
325 sum += best->totaltime;
326 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
331 Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
332 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
334 prog->starttime = Sys_DoubleTime();
337 void PRVM_Profile (int maxfunctions, int mininstructions, int sortby)
339 mfunction_t *f, *best;
343 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
344 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
351 for (i=0 ; i<prog->progs->numfunctions ; i++)
353 f = &prog->functions[i];
356 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
358 max = f->profile_total + f->builtinsprofile_total + f->callcount;
364 if (max < f->profile + f->builtinsprofile + f->callcount)
366 max = f->profile + f->builtinsprofile + f->callcount;
373 if (num < maxfunctions && max >= mininstructions)
375 if (best->first_statement < 0)
376 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
377 // 12345678901 12345678901 12345678901 12345678901 123.45%
379 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));
383 best->builtinsprofile = 0;
384 best->profile_total = 0;
385 best->builtinsprofile_total = 0;
397 void PRVM_CallProfile_f (void)
401 Con_Print("prvm_callprofile <program name>\n");
406 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
420 void PRVM_Profile_f (void)
426 howmany = atoi(Cmd_Argv(2));
427 else if (Cmd_Argc() != 2)
429 Con_Print("prvm_profile <program name>\n");
434 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
437 PRVM_Profile(howmany, 1, 0);
442 void PRVM_ChildProfile_f (void)
448 howmany = atoi(Cmd_Argv(2));
449 else if (Cmd_Argc() != 2)
451 Con_Print("prvm_childprofile <program name>\n");
456 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
459 PRVM_Profile(howmany, 1, 1);
464 void PRVM_CrashAll(void)
467 prvm_prog_t *oldprog = prog;
469 for(i = 0; i < PRVM_MAXPROGS; i++)
471 if(!PRVM_ProgLoaded(i))
480 void PRVM_PrintState(void)
483 if(prog->statestring)
485 Con_Printf("Caller-provided information: %s\n", prog->statestring);
489 for (i = -7; i <= 0;i++)
490 if (prog->xstatement + i >= prog->xfunction->first_statement)
491 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
494 Con_Print("null function executing??\n");
498 extern sizebuf_t vm_tempstringsbuf;
499 extern cvar_t prvm_errordump;
500 void Host_Savegame_to (const char *name);
501 void PRVM_Crash(void)
506 prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
508 if( prog->depth > 0 )
510 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
514 if(prvm_errordump.integer)
517 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
520 // dump the stack so host_error can shutdown functions
522 prog->localstack_used = 0;
524 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
525 vm_tempstringsbuf.cursize = 0;
527 // reset the prog pointer
532 ============================================================================
535 The interpretation main loop
536 ============================================================================
543 Returns the new program statement counter
546 int PRVM_EnterFunction (mfunction_t *f)
551 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
553 prog->stack[prog->depth].s = prog->xstatement;
554 prog->stack[prog->depth].f = prog->xfunction;
555 prog->stack[prog->depth].profile_acc = -f->profile;
556 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
558 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
559 PRVM_ERROR ("stack overflow");
561 // save off any locals that the new function steps on
563 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
564 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_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 int PRVM_LeaveFunction (void)
596 if (prog->depth <= 0)
597 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
599 if (!prog->xfunction)
600 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
601 // restore locals from the stack
602 c = prog->xfunction->locals;
603 prog->localstack_used -= c;
604 if (prog->localstack_used < 0)
605 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_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].builtinsprofile_acc += f->builtinsprofile;
619 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
620 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
624 // if f is already on the call stack...
625 // we cannot add this profile data to it now
626 // or we would add it more than once
627 // so, let's only add to the function's profile if it is the outermost call
628 f->profile_total += prog->stack[prog->depth].profile_acc;
629 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
632 return prog->stack[prog->depth].s;
635 void PRVM_Init_Exec(void)
639 prog->localstack_used = 0;
640 // reset the string table
649 // LordHavoc: optimized
650 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
651 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
652 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
653 extern cvar_t prvm_traceqc;
654 extern cvar_t prvm_statementprofiling;
655 extern sizebuf_t vm_tempstringsbuf;
656 extern qboolean prvm_runawaycheck;
657 extern qboolean prvm_boundscheck;
658 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
660 dstatement_t *st, *startst;
661 mfunction_t *f, *newf;
664 int jumpcount, cachedpr_trace, exitdepth;
665 int restorevm_tempstringsbuf_cursize;
668 calltime = Sys_DoubleTime();
670 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
672 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
673 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
674 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
677 f = &prog->functions[fnum];
679 // after executing this function, delete all tempstrings it created
680 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
682 prog->trace = prvm_traceqc.integer;
684 // we know we're done when pr_depth drops to this
685 exitdepth = prog->depth;
687 // make a stack frame
688 st = &prog->statements[PRVM_EnterFunction (f)];
689 // save the starting statement pointer for profiling
690 // (when the function exits or jumps, the (st - startst) integer value is
691 // added to the function's profile counter)
693 // instead of counting instructions, we count jumps
695 // add one to the callcount of this function because otherwise engine-called functions aren't counted
696 prog->xfunction->callcount++;
699 cachedpr_trace = prog->trace;
700 if (prvm_runawaycheck)
702 #define PRVMRUNAWAYCHECK 1
703 if (prvm_statementprofiling.integer)
705 #define PRVMSTATEMENTPROFILING 1
706 if (prvm_boundscheck)
708 #define PRVMBOUNDSCHECK 1
712 #include "prvm_execprogram.h"
717 #include "prvm_execprogram.h"
719 #undef PRVMBOUNDSCHECK
726 #include "prvm_execprogram.h"
731 #include "prvm_execprogram.h"
734 #undef PRVMSTATEMENTPROFILING
738 if (prvm_boundscheck)
740 #define PRVMBOUNDSCHECK 1
744 #include "prvm_execprogram.h"
749 #include "prvm_execprogram.h"
751 #undef PRVMBOUNDSCHECK
758 #include "prvm_execprogram.h"
763 #include "prvm_execprogram.h"
767 #undef PRVMRUNAWAYCHECK
771 if (prvm_statementprofiling.integer)
773 #define PRVMSTATEMENTPROFILING 1
774 if (prvm_boundscheck)
776 #define PRVMBOUNDSCHECK 1
780 #include "prvm_execprogram.h"
785 #include "prvm_execprogram.h"
787 #undef PRVMBOUNDSCHECK
794 #include "prvm_execprogram.h"
799 #include "prvm_execprogram.h"
802 #undef PRVMSTATEMENTPROFILING
806 if (prvm_boundscheck)
808 #define PRVMBOUNDSCHECK 1
812 #include "prvm_execprogram.h"
817 #include "prvm_execprogram.h"
819 #undef PRVMBOUNDSCHECK
826 #include "prvm_execprogram.h"
831 #include "prvm_execprogram.h"
838 if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
839 Con_Printf("PRVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
840 // delete tempstrings created by this function
841 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
843 f->totaltime += (Sys_DoubleTime() - calltime);
845 SV_FlushBroadcastMessages();