]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_exec.c
significantly reduce code size in prvm_exec.c by simplifying the
[xonotic/darkplaces.git] / prvm_exec.c
index b6c8a0acf70081cc23a61fec4d8899ebcaf5e936..6dfdc150d2e1179956c3fe8a9a181475ac0fbae8 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);
@@ -641,12 +642,6 @@ void PRVM_Init_Exec(void)
        // nothing here yet
 }
 
-/*
-====================
-PRVM_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])
@@ -654,8 +649,14 @@ extern cvar_t prvm_traceqc;
 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)
+
+#ifdef PROFILING
+/*
+====================
+MVM_ExecuteProgram
+====================
+*/
+void MVM_ExecuteProgram (func_t fnum, const char *errormessage)
 {
        dstatement_t    *st, *startst;
        mfunction_t     *f, *newf;
@@ -671,7 +672,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 ("MVM_ExecuteProgram: %s", errormessage);
        }
 
        f = &prog->functions[fnum];
@@ -697,150 +698,163 @@ void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
 
 chooseexecprogram:
        cachedpr_trace = prog->trace;
-       if (prvm_runawaycheck)
+       if (prvm_statementprofiling.integer || prog->trace)
        {
-#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
-                               {
+#define PRVMSLOWINTERPRETER 1
 #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
+#undef PRVMSLOWINTERPRETER
        }
        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
+       }
+
+cleanup:
+       if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
+               Con_DPrintf("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
+====================
+*/
+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_statementprofiling.integer || prog->trace)
+       {
+#define PRVMSLOWINTERPRETER 1
 #include "prvm_execprogram.h"
-#undef PRVMTRACE
-                               }
-                               else
-                               {
+#undef PRVMSLOWINTERPRETER
+       }
+       else
+       {
 #include "prvm_execprogram.h"
-                               }
-#undef PRVMBOUNDSCHECK
-                       }
-                       else
-                       {
-                               if (prog->trace)
-                               {
-#define PRVMTRACE 1
+       }
+
+cleanup:
+       if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
+               Con_DPrintf("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();
+}
+#endif
+
+/*
+====================
+SVVM_ExecuteProgram
+====================
+*/
+void SVVM_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 ("SVVM_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_statementprofiling.integer || prog->trace)
+       {
+#define PRVMSLOWINTERPRETER 1
 #include "prvm_execprogram.h"
-#undef PRVMTRACE
-                               }
-                               else
-                               {
+#undef PRVMSLOWINTERPRETER
+       }
+       else
+       {
 #include "prvm_execprogram.h"
-                               }
-                       }
-               }
        }
 
 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);
+       if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
+               Con_DPrintf("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();
 }