]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_exec.c
... forgot to add the files, I'm too used to git now :P
[xonotic/darkplaces.git] / prvm_exec.c
index af50433f66c17ab2baf658110a3bf37cd9606b3b..97130220029d56a6a55f86bebe5a486658edac58 100644 (file)
@@ -112,6 +112,7 @@ char *prvm_opnames[] =
 
 char *PRVM_GlobalString (int ofs);
 char *PRVM_GlobalStringNoContents (int ofs);
+extern ddef_t *PRVM_ED_FieldAtOfs(int ofs);
 
 
 //=============================================================================
@@ -211,7 +212,7 @@ void PRVM_PrintFunctionStatements (const char *name)
                        endstatement = prog->functions[i].first_statement;
 
        // now print the range of statements
-       Con_Printf("%s progs: disassembly of function %s (statements %i-%i):\n", PRVM_NAME, name, firststatement, endstatement);
+       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);
        for (i = firststatement;i < endstatement;i++)
        {
                PRVM_PrintStatement(prog->statements + i);
@@ -334,13 +335,14 @@ void PRVM_CallProfile (void)
        prog->starttime = Sys_DoubleTime();
 }
 
-void PRVM_Profile (int maxfunctions, int mininstructions)
+void PRVM_Profile (int maxfunctions, int mininstructions, int sortby)
 {
        mfunction_t *f, *best;
        int i, num;
        double max;
 
-       Con_Printf( "%s Profile:\n[CallCount] [Statements] [BuiltinCost]\n", PRVM_NAME );
+       Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
+       //                        12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
 
        num = 0;
        do
@@ -350,10 +352,21 @@ void PRVM_Profile (int maxfunctions, int mininstructions)
                for (i=0 ; i<prog->progs->numfunctions ; i++)
                {
                        f = &prog->functions[i];
-                       if (max < f->profile + f->builtinsprofile + f->callcount)
+                       if(sortby)
                        {
-                               max = f->profile + f->builtinsprofile + f->callcount;
-                               best = f;
+                               if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
+                               {
+                                       max = f->profile_total + f->builtinsprofile_total + f->callcount;
+                                       best = f;
+                               }
+                       }
+                       else
+                       {
+                               if (max < f->profile + f->builtinsprofile + f->callcount)
+                               {
+                                       max = f->profile + f->builtinsprofile + f->callcount;
+                                       best = f;
+                               }
                        }
                }
                if (best)
@@ -361,13 +374,16 @@ void PRVM_Profile (int maxfunctions, int mininstructions)
                        if (num < maxfunctions && max >= mininstructions)
                        {
                                if (best->first_statement < 0)
-                                       Con_Printf("%9.0f ----- builtin ----- %s\n", best->callcount, PRVM_GetString(best->s_name));
+                                       Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
+                                       //                 12345678901 12345678901 12345678901 12345678901 123.45%
                                else
-                                       Con_Printf("%9.0f %9.0f %9.0f %s\n", best->callcount, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
+                                       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));
                        }
                        num++;
                        best->profile = 0;
                        best->builtinsprofile = 0;
+                       best->profile_total = 0;
+                       best->builtinsprofile_total = 0;
                        best->callcount = 0;
                }
        } while (best);
@@ -419,7 +435,29 @@ void PRVM_Profile_f (void)
        if(!PRVM_SetProgFromString(Cmd_Argv(1)))
                return;
 
-       PRVM_Profile(howmany, 1);
+       PRVM_Profile(howmany, 1, 0);
+
+       PRVM_End;
+}
+
+void PRVM_ChildProfile_f (void)
+{
+       int howmany;
+
+       howmany = 1<<30;
+       if (Cmd_Argc() == 3)
+               howmany = atoi(Cmd_Argv(2));
+       else if (Cmd_Argc() != 2)
+       {
+               Con_Print("prvm_childprofile <program name>\n");
+               return;
+       }
+
+       PRVM_Begin;
+       if(!PRVM_SetProgFromString(Cmd_Argv(1)))
+               return;
+
+       PRVM_Profile(howmany, 1, 1);
 
        PRVM_End;
 }
@@ -515,6 +553,8 @@ int PRVM_EnterFunction (mfunction_t *f)
 
        prog->stack[prog->depth].s = prog->xstatement;
        prog->stack[prog->depth].f = prog->xfunction;
+       prog->stack[prog->depth].profile_acc = -f->profile;
+       prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
        prog->depth++;
        if (prog->depth >=PRVM_MAX_STACK_DEPTH)
                PRVM_ERROR ("stack overflow");
@@ -539,6 +579,7 @@ int PRVM_EnterFunction (mfunction_t *f)
                }
        }
 
+       ++f->recursion;
        prog->xfunction = f;
        return f->first_statement - 1;  // offset the s++
 }
@@ -551,6 +592,7 @@ PRVM_LeaveFunction
 int PRVM_LeaveFunction (void)
 {
        int             i, c;
+       mfunction_t *f;
 
        if (prog->depth <= 0)
                PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
@@ -568,7 +610,26 @@ int PRVM_LeaveFunction (void)
 
 // up stack
        prog->depth--;
+       f = prog->xfunction;
+       --f->recursion;
        prog->xfunction = prog->stack[prog->depth].f;
+       prog->stack[prog->depth].profile_acc += f->profile;
+       prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
+       if(prog->depth > 0)
+       {
+               prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
+               prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
+       }
+       if(!f->recursion)
+       {
+               // if f is already on the call stack...
+               // we cannot add this profile data to it now
+               // or we would add it more than once
+               // so, let's only add to the function's profile if it is the outermost call
+               f->profile_total += prog->stack[prog->depth].profile_acc;
+               f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
+       }
+       
        return prog->stack[prog->depth].s;
 }
 
@@ -583,7 +644,415 @@ void PRVM_Init_Exec(void)
 
 /*
 ====================
-PRVM_ExecuteProgram
+MVM_ExecuteProgram
+====================
+*/
+// LordHavoc: optimized
+#define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
+#define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
+#define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
+extern cvar_t prvm_traceqc;
+extern cvar_t prvm_statementprofiling;
+extern sizebuf_t vm_tempstringsbuf;
+extern qboolean prvm_runawaycheck;
+extern qboolean prvm_boundscheck;
+void MVM_ExecuteProgram (func_t fnum, const char *errormessage)
+{
+       dstatement_t    *st, *startst;
+       mfunction_t     *f, *newf;
+       prvm_edict_t    *ed;
+       prvm_eval_t     *ptr;
+       int             jumpcount, cachedpr_trace, exitdepth;
+       int             restorevm_tempstringsbuf_cursize;
+       double  calltime;
+
+       calltime = Sys_DoubleTime();
+
+       if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
+       {
+               if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
+                       PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
+               PRVM_ERROR ("MVM_ExecuteProgram: %s", errormessage);
+       }
+
+       f = &prog->functions[fnum];
+
+       // after executing this function, delete all tempstrings it created
+       restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
+
+       prog->trace = prvm_traceqc.integer;
+
+       // we know we're done when pr_depth drops to this
+       exitdepth = prog->depth;
+
+// make a stack frame
+       st = &prog->statements[PRVM_EnterFunction (f)];
+       // save the starting statement pointer for profiling
+       // (when the function exits or jumps, the (st - startst) integer value is
+       // added to the function's profile counter)
+       startst = st;
+       // instead of counting instructions, we count jumps
+       jumpcount = 0;
+       // add one to the callcount of this function because otherwise engine-called functions aren't counted
+       prog->xfunction->callcount++;
+
+chooseexecprogram:
+       cachedpr_trace = prog->trace;
+       if (prvm_runawaycheck)
+       {
+#define PRVMRUNAWAYCHECK 1
+               if (prvm_statementprofiling.integer)
+               {
+#define PRVMSTATEMENTPROFILING 1
+                       if (prvm_boundscheck)
+                       {
+#define PRVMBOUNDSCHECK 1
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+#undef PRVMBOUNDSCHECK
+                       }
+                       else
+                       {
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+                       }
+#undef PRVMSTATEMENTPROFILING
+               }
+               else
+               {
+                       if (prvm_boundscheck)
+                       {
+#define PRVMBOUNDSCHECK 1
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+#undef PRVMBOUNDSCHECK
+                       }
+                       else
+                       {
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+                       }
+               }
+#undef PRVMRUNAWAYCHECK
+       }
+       else
+       {
+               if (prvm_statementprofiling.integer)
+               {
+#define PRVMSTATEMENTPROFILING 1
+                       if (prvm_boundscheck)
+                       {
+#define PRVMBOUNDSCHECK 1
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+#undef PRVMBOUNDSCHECK
+                       }
+                       else
+                       {
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+                       }
+#undef PRVMSTATEMENTPROFILING
+               }
+               else
+               {
+                       if (prvm_boundscheck)
+                       {
+#define PRVMBOUNDSCHECK 1
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+#undef PRVMBOUNDSCHECK
+                       }
+                       else
+                       {
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+                       }
+               }
+       }
+
+cleanup:
+       if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
+               Con_Printf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
+       // delete tempstrings created by this function
+       vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
+
+       f->totaltime += (Sys_DoubleTime() - calltime);
+
+       SV_FlushBroadcastMessages();
+}
+
+/*
+====================
+CLVM_ExecuteProgram
+====================
+*/
+// LordHavoc: optimized
+#define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
+#define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
+#define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
+extern cvar_t prvm_traceqc;
+extern cvar_t prvm_statementprofiling;
+extern sizebuf_t vm_tempstringsbuf;
+extern qboolean prvm_runawaycheck;
+extern qboolean prvm_boundscheck;
+void CLVM_ExecuteProgram (func_t fnum, const char *errormessage)
+{
+       dstatement_t    *st, *startst;
+       mfunction_t     *f, *newf;
+       prvm_edict_t    *ed;
+       prvm_eval_t     *ptr;
+       int             jumpcount, cachedpr_trace, exitdepth;
+       int             restorevm_tempstringsbuf_cursize;
+       double  calltime;
+
+       calltime = Sys_DoubleTime();
+
+       if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
+       {
+               if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
+                       PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
+               PRVM_ERROR ("CLVM_ExecuteProgram: %s", errormessage);
+       }
+
+       f = &prog->functions[fnum];
+
+       // after executing this function, delete all tempstrings it created
+       restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
+
+       prog->trace = prvm_traceqc.integer;
+
+       // we know we're done when pr_depth drops to this
+       exitdepth = prog->depth;
+
+// make a stack frame
+       st = &prog->statements[PRVM_EnterFunction (f)];
+       // save the starting statement pointer for profiling
+       // (when the function exits or jumps, the (st - startst) integer value is
+       // added to the function's profile counter)
+       startst = st;
+       // instead of counting instructions, we count jumps
+       jumpcount = 0;
+       // add one to the callcount of this function because otherwise engine-called functions aren't counted
+       prog->xfunction->callcount++;
+
+chooseexecprogram:
+       cachedpr_trace = prog->trace;
+       if (prvm_runawaycheck)
+       {
+#define PRVMRUNAWAYCHECK 1
+               if (prvm_statementprofiling.integer)
+               {
+#define PRVMSTATEMENTPROFILING 1
+                       if (prvm_boundscheck)
+                       {
+#define PRVMBOUNDSCHECK 1
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+#undef PRVMBOUNDSCHECK
+                       }
+                       else
+                       {
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+                       }
+#undef PRVMSTATEMENTPROFILING
+               }
+               else
+               {
+                       if (prvm_boundscheck)
+                       {
+#define PRVMBOUNDSCHECK 1
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+#undef PRVMBOUNDSCHECK
+                       }
+                       else
+                       {
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+                       }
+               }
+#undef PRVMRUNAWAYCHECK
+       }
+       else
+       {
+               if (prvm_statementprofiling.integer)
+               {
+#define PRVMSTATEMENTPROFILING 1
+                       if (prvm_boundscheck)
+                       {
+#define PRVMBOUNDSCHECK 1
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+#undef PRVMBOUNDSCHECK
+                       }
+                       else
+                       {
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+                       }
+#undef PRVMSTATEMENTPROFILING
+               }
+               else
+               {
+                       if (prvm_boundscheck)
+                       {
+#define PRVMBOUNDSCHECK 1
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+#undef PRVMBOUNDSCHECK
+                       }
+                       else
+                       {
+                               if (prog->trace)
+                               {
+#define PRVMTRACE 1
+#include "prvm_execprogram.h"
+#undef PRVMTRACE
+                               }
+                               else
+                               {
+#include "prvm_execprogram.h"
+                               }
+                       }
+               }
+       }
+
+cleanup:
+       if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
+               Con_Printf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
+       // delete tempstrings created by this function
+       vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
+
+       f->totaltime += (Sys_DoubleTime() - calltime);
+
+       SV_FlushBroadcastMessages();
+}
+
+/*
+====================
+SVVM_ExecuteProgram
 ====================
 */
 // LordHavoc: optimized
@@ -595,7 +1064,7 @@ extern cvar_t prvm_statementprofiling;
 extern sizebuf_t vm_tempstringsbuf;
 extern qboolean prvm_runawaycheck;
 extern qboolean prvm_boundscheck;
-void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
+void SVVM_ExecuteProgram (func_t fnum, const char *errormessage)
 {
        dstatement_t    *st, *startst;
        mfunction_t     *f, *newf;
@@ -611,7 +1080,7 @@ void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
        {
                if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
                        PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
-               PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
+               PRVM_ERROR ("SVVM_ExecuteProgram: %s", errormessage);
        }
 
        f = &prog->functions[fnum];
@@ -776,11 +1245,11 @@ chooseexecprogram:
 
 cleanup:
        if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
-               Con_Printf("PRVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
+               Con_Printf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
        // delete tempstrings created by this function
        vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
 
-       prog->functions[fnum].totaltime += (Sys_DoubleTime() - calltime);
+       f->totaltime += (Sys_DoubleTime() - calltime);
 
        SV_FlushBroadcastMessages();
 }