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 static const char *prvm_opnames[] =
294 //=============================================================================
301 extern cvar_t prvm_coverage;
302 extern cvar_t prvm_statementprofiling;
303 extern cvar_t prvm_timeprofiling;
304 static void PRVM_PrintStatement(prvm_prog_t *prog, mstatement_t *s)
307 int opnum = (int)(s - prog->statements);
308 char valuebuf[MAX_INPUTLINE];
311 Con_Printf("s%i: ", opnum);
312 if( prog->statement_linenums )
314 if ( prog->statement_columnnums )
315 Con_Printf( "%s:%i:%i: ", PRVM_GetString( prog, prog->xfunction->s_file ), prog->statement_linenums[ opnum ], prog->statement_columnnums[ opnum ] );
317 Con_Printf( "%s:%i: ", PRVM_GetString( prog, prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
320 if (prvm_statementprofiling.integer)
321 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
323 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]) && prvm_opnames[s->op])
324 opname = prvm_opnames[s->op];
326 opname = valuebuf, dpsnprintf(valuebuf, sizeof(valuebuf), "OPCODE_%u", (unsigned)s->op);
327 Con_Printf("%s ", opname);
329 // don't count a preceding color tag when padding the name
330 if (opname[0] == STRING_COLOR_TAG)
335 if (s->operand[0] >= 0) Con_Printf( "%s", PRVM_GlobalString(prog, s->operand[0], valuebuf, sizeof(valuebuf)));
336 if (s->operand[1] >= 0) Con_Printf(", %s", PRVM_GlobalString(prog, s->operand[1], valuebuf, sizeof(valuebuf)));
337 if (s->operand[2] >= 0) Con_Printf(", %s", PRVM_GlobalString(prog, s->operand[2], valuebuf, sizeof(valuebuf)));
338 if (s->jumpabsolute >= 0) Con_Printf(", statement %i", s->jumpabsolute);
342 void PRVM_PrintFunctionStatements (prvm_prog_t *prog, const char *name)
344 int i, firststatement, endstatement;
346 func = PRVM_ED_FindFunction (prog, name);
349 Con_Printf("%s progs: no function named %s\n", prog->name, name);
352 firststatement = func->first_statement;
353 if (firststatement < 0)
355 Con_Printf("%s progs: function %s is builtin #%i\n", prog->name, name, -firststatement);
359 // find the end statement
360 endstatement = prog->numstatements;
361 for (i = 0;i < prog->numfunctions;i++)
362 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
363 endstatement = prog->functions[i].first_statement;
365 // now print the range of statements
366 Con_Printf("%s progs: disassembly of function %s (statements %i-%i, locals %i-%i):\n", prog->name, name, firststatement, endstatement, func->parm_start, func->parm_start + func->locals - 1);
367 prog->xfunction = func;
368 for (i = firststatement;i < endstatement;i++)
370 PRVM_PrintStatement(prog, prog->statements + i);
371 if (!(prvm_coverage.integer & 4))
372 prog->statement_profile[i] = 0;
374 if (prvm_coverage.integer & 4)
375 Con_Printf("Collecting statement coverage, not flushing statement profile.\n");
384 void PRVM_PrintFunction_f(cmd_state_t *cmd)
387 if (Cmd_Argc(cmd) != 3)
389 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
393 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
396 PRVM_PrintFunctionStatements(prog, Cmd_Argv(cmd, 2));
404 void PRVM_StackTrace (prvm_prog_t *prog)
409 prog->stack[prog->depth].s = prog->xstatement;
410 prog->stack[prog->depth].f = prog->xfunction;
411 for (i = prog->depth;i > 0;i--)
413 f = prog->stack[i].f;
416 Con_Print("<NULL FUNCTION>\n");
419 if (prog->statement_linenums)
421 if (prog->statement_columnnums)
422 Con_Printf("%12s:%i:%i : %s : statement %i\n", PRVM_GetString(prog, f->s_file), prog->statement_linenums[prog->stack[i].s], prog->statement_columnnums[prog->stack[i].s], PRVM_GetString(prog, f->s_name), prog->stack[i].s - f->first_statement);
424 Con_Printf("%12s:%i : %s : statement %i\n", PRVM_GetString(prog, f->s_file), prog->statement_linenums[prog->stack[i].s], PRVM_GetString(prog, f->s_name), prog->stack[i].s - f->first_statement);
427 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(prog, f->s_file), PRVM_GetString(prog, f->s_name), prog->stack[i].s - f->first_statement);
432 void PRVM_ShortStackTrace(prvm_prog_t *prog, char *buf, size_t bufsize)
441 i = dpsnprintf(buf, bufsize, "(%s) ", prog->name);
446 dp_strlcpy(buf, "<NO PROG>", bufsize);
450 prog->stack[prog->depth].s = prog->xstatement;
451 prog->stack[prog->depth].f = prog->xfunction;
452 for (i = prog->depth;i > 0;i--)
454 f = prog->stack[i].f;
459 ? va(vabuf, sizeof(vabuf), "%s:%s(%i) ", PRVM_GetString(prog, f->s_file), PRVM_GetString(prog, f->s_name), prog->stack[i].s - f->first_statement)
462 if (p == buf + bufsize)
468 static void PRVM_CallProfile (prvm_prog_t *prog)
470 mfunction_t *f, *best;
474 double newprofiletime;
476 Con_Printf( "%s Call Profile:\n", prog->name );
483 for (i=0 ; i<prog->numfunctions ; i++)
485 f = &prog->functions[i];
486 if (max < f->totaltime)
494 sum += best->totaltime;
495 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(prog, best->s_name));
500 newprofiletime = Sys_DirtyTime();
501 Con_Printf("Total time since last profile reset: %9.4f\n", newprofiletime - prog->profiletime);
502 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
504 prog->profiletime = newprofiletime;
507 void PRVM_Profile (prvm_prog_t *prog, int maxfunctions, double mintime, int sortby)
509 mfunction_t *f, *best;
513 if(!prvm_timeprofiling.integer)
514 mintime *= 10000000; // count each statement as about 0.1µs
516 if(prvm_timeprofiling.integer)
517 Con_Printf( "%s Profile:\n[CallCount] [Time] [BuiltinTm] [Statement] [BuiltinCt] [TimeTotal] [StmtTotal] [BltnTotal] [self]\n", prog->name );
518 // 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
520 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", prog->name );
521 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
528 for (i=0 ; i<prog->numfunctions ; i++)
530 f = &prog->functions[i];
531 if(prvm_timeprofiling.integer)
535 if(f->first_statement < 0)
537 if (max < f->tprofile)
545 if (max < f->tprofile_total)
547 max = f->tprofile_total;
554 if (max < f->tprofile + f->tbprofile)
556 max = f->tprofile + f->tbprofile;
565 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
567 max = f->profile_total + f->builtinsprofile_total + f->callcount;
573 if (max < f->profile + f->builtinsprofile + f->callcount)
575 max = f->profile + f->builtinsprofile + f->callcount;
583 if (num < maxfunctions && max > mintime)
585 if(prvm_timeprofiling.integer)
587 if (best->first_statement < 0)
588 Con_Printf("%11.0f %11.6f ------------- builtin ------------- %11.6f ----------- builtin ----------- %s\n", best->callcount, best->tprofile, best->tprofile, PRVM_GetString(prog, best->s_name));
589 // %11.6f 12345678901 12345678901 12345678901 %11.6f 12345678901 12345678901 123.45%
591 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(prog, best->s_name));
595 if (best->first_statement < 0)
596 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(prog, best->s_name));
597 // 12345678901 12345678901 12345678901 12345678901 123.45%
599 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(prog, best->s_name));
606 best->builtinsprofile = 0;
607 best->profile_total = 0;
608 best->tprofile_total = 0;
609 best->builtinsprofile_total = 0;
621 void PRVM_CallProfile_f(cmd_state_t *cmd)
624 if (Cmd_Argc(cmd) != 2)
626 Con_Print("prvm_callprofile <program name>\n");
630 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
633 PRVM_CallProfile(prog);
642 void PRVM_Profile_f(cmd_state_t *cmd)
647 if (prvm_coverage.integer & 1)
649 Con_Printf("Collecting function coverage, cannot profile - sorry!\n");
654 if (Cmd_Argc(cmd) == 3)
655 howmany = atoi(Cmd_Argv(cmd, 2));
656 else if (Cmd_Argc(cmd) != 2)
658 Con_Print("prvm_profile <program name>\n");
662 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
665 PRVM_Profile(prog, howmany, 0, 0);
668 void PRVM_ChildProfile_f(cmd_state_t *cmd)
673 if (prvm_coverage.integer & 1)
675 Con_Printf("Collecting function coverage, cannot profile - sorry!\n");
680 if (Cmd_Argc(cmd) == 3)
681 howmany = atoi(Cmd_Argv(cmd, 2));
682 else if (Cmd_Argc(cmd) != 2)
684 Con_Print("prvm_childprofile <program name>\n");
688 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
691 PRVM_Profile(prog, howmany, 0, 1);
694 void PRVM_PrintState(prvm_prog_t *prog, int stack_index)
697 mfunction_t *func = prog->xfunction;
698 int st = prog->xstatement;
699 if (stack_index > 0 && stack_index <= prog->depth)
701 func = prog->stack[prog->depth - stack_index].f;
702 st = prog->stack[prog->depth - stack_index].s;
704 if (prog->statestring)
706 Con_Printf("Caller-provided information: %s\n", prog->statestring);
710 for (i = -7; i <= 0;i++)
711 if (st + i >= func->first_statement)
712 PRVM_PrintStatement(prog, prog->statements + st + i);
714 PRVM_StackTrace(prog);
717 extern cvar_t prvm_errordump;
718 void PRVM_Crash(void)
724 // determine which program crashed
725 for (i = 0; i < PRVM_PROG_MAX; ++i)
726 if (PRVM_GetProg(i)->loaded && PRVM_GetProg(i)->depth > 0)
728 if (i >= PRVM_PROG_MAX)
729 return; // none of them crashed
730 prog = PRVM_GetProg(i);
732 Con_Printf("QuakeC crash report for %s:\n", prog->name);
733 PRVM_PrintState(prog, 0);
735 // don't call graceful shutdown on crash
736 if (prog == SVVM_prog)
737 PRVM_serverfunction(SV_Shutdown) = 0;
738 else if (prog == CLVM_prog)
739 PRVM_clientfunction(CSQC_Shutdown) = 0;
741 if(prvm_errordump.integer && (prog == SVVM_prog || prog == CLVM_prog))
744 SV_Savegame_to(prog, va(vabuf, sizeof(vabuf), "crash-%s.dmp", prog->name));
747 // dump the stack so host_error can shutdown functions
749 prog->localstack_used = 0;
753 ============================================================================
756 The interpretation main loop
757 ============================================================================
764 Returns the new program statement counter
767 static int PRVM_EnterFunction (prvm_prog_t *prog, mfunction_t *f)
772 prog->error_cmd("PRVM_EnterFunction: NULL function in %s", prog->name);
774 prog->stack[prog->depth].s = prog->xstatement;
775 prog->stack[prog->depth].f = prog->xfunction;
776 prog->stack[prog->depth].profile_acc = -f->profile;
777 prog->stack[prog->depth].tprofile_acc = -f->tprofile + -f->tbprofile;
778 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
780 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
781 prog->error_cmd("stack overflow");
783 // save off any locals that the new function steps on
785 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
786 prog->error_cmd("PRVM_ExecuteProgram: locals stack overflow in %s", prog->name);
788 for (i=0 ; i < c ; i++)
789 prog->localstack[prog->localstack_used+i] = prog->globals.ip[f->parm_start + i];
790 prog->localstack_used += c;
794 for (i=0 ; i<f->numparms ; i++)
796 for (j=0 ; j<f->parm_size[i] ; j++)
798 prog->globals.ip[o] = prog->globals.ip[OFS_PARM0+i*3+j];
805 return f->first_statement - 1; // offset the s++
813 static int PRVM_LeaveFunction (prvm_prog_t *prog)
818 if (prog->depth <= 0)
819 prog->error_cmd("prog stack underflow in %s", prog->name);
821 if (!prog->xfunction)
822 prog->error_cmd("PR_LeaveFunction: NULL function in %s", prog->name);
823 // restore locals from the stack
824 c = prog->xfunction->locals;
825 prog->localstack_used -= c;
826 if (prog->localstack_used < 0)
827 prog->error_cmd("PRVM_ExecuteProgram: locals stack underflow in %s", prog->name);
829 for (i=0 ; i < c ; i++)
830 prog->globals.ip[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
836 prog->xfunction = prog->stack[prog->depth].f;
837 prog->stack[prog->depth].profile_acc += f->profile;
838 prog->stack[prog->depth].tprofile_acc += f->tprofile + f->tbprofile;
839 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
842 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
843 prog->stack[prog->depth-1].tprofile_acc += prog->stack[prog->depth].tprofile_acc;
844 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
848 // if f is already on the call stack...
849 // we cannot add this profile data to it now
850 // or we would add it more than once
851 // so, let's only add to the function's profile if it is the outermost call
852 f->profile_total += prog->stack[prog->depth].profile_acc;
853 f->tprofile_total += prog->stack[prog->depth].tprofile_acc;
854 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
857 return prog->stack[prog->depth].s;
860 void PRVM_Init_Exec(prvm_prog_t *prog)
864 prog->localstack_used = 0;
865 // reset the string table
874 // Note: in these two calls, prog->xfunction is assumed to be sane.
875 static const char *PRVM_WhereAmI(char *buf, size_t bufsize, prvm_prog_t *prog, mfunction_t *func, int statement)
877 if (prog->statement_linenums)
879 if (prog->statement_columnnums)
880 return va(buf, bufsize, "%s:%i:%i(%s, %i)", PRVM_GetString(prog, func->s_file), prog->statement_linenums[statement], prog->statement_columnnums[statement], PRVM_GetString(prog, func->s_name), statement - func->first_statement);
882 return va(buf, bufsize, "%s:%i(%s, %i)", PRVM_GetString(prog, func->s_file), prog->statement_linenums[statement], PRVM_GetString(prog, func->s_name), statement - func->first_statement);
885 return va(buf, bufsize, "%s(%s, %i)", PRVM_GetString(prog, func->s_file), PRVM_GetString(prog, func->s_name), statement - func->first_statement);
887 static void PRVM_FunctionCoverageEvent(prvm_prog_t *prog, mfunction_t *func)
889 ++prog->functions_covered;
890 Con_Printf("prvm_coverage: %s just called %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_GetString(prog, func->s_name), prog->functions_covered * 100.0 / prog->numfunctions);
892 void PRVM_ExplicitCoverageEvent(prvm_prog_t *prog, mfunction_t *func, int statement)
895 ++prog->explicit_covered;
896 Con_Printf("prvm_coverage: %s just executed a coverage() statement at %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_WhereAmI(vabuf, sizeof(vabuf), prog, func, statement), prog->explicit_covered * 100.0 / prog->numexplicitcoveragestatements);
898 static void PRVM_StatementCoverageEvent(prvm_prog_t *prog, mfunction_t *func, int statement)
901 ++prog->statements_covered;
902 Con_Printf("prvm_coverage: %s just executed a statement at %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_WhereAmI(vabuf, sizeof(vabuf), prog, func, statement), prog->statements_covered * 100.0 / prog->numstatements);
905 #if defined (__GNUC__) || (__clang__) || (__TINYC__)
906 # ifndef CONFIG_PEDANTIC
907 # define HAVE_COMPUTED_GOTOS 1
911 #define OPA ((prvm_eval_t *)&globals[st->operand[0]])
912 #define OPB ((prvm_eval_t *)&globals[st->operand[1]])
913 #define OPC ((prvm_eval_t *)&globals[st->operand[2]])
914 extern cvar_t prvm_traceqc;
915 extern cvar_t prvm_statementprofiling;
916 extern qbool prvm_runawaycheck;
925 void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
927 mstatement_t *st, *startst;
928 mfunction_t *func, *enterfunc;
931 int jumpcount, cachedpr_trace, exitdepth;
932 int restorevm_tempstringsbuf_cursize;
935 prvm_vec_t tempfloat;
936 // these may become out of date when a builtin is called, and are updated accordingly
937 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
938 unsigned int cached_entityfields = prog->entityfields;
939 unsigned int cached_entityfields_3 = prog->entityfields - 3;
940 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
941 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
942 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
943 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
944 unsigned int cached_max_edicts = prog->max_edicts;
945 // these do not change
946 mstatement_t *cached_statements = prog->statements;
947 qbool cached_allowworldwrites = prog->allowworldwrites;
948 unsigned int cached_flag = prog->flag;
950 prvm_vec_t *globals = prog->globals.fp;
952 calltime = Sys_DirtyTime();
954 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
956 if (PRVM_allglobaledict(self))
957 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
958 prog->error_cmd("MVM_ExecuteProgram: %s", errormessage);
961 func = &prog->functions[fnum];
963 // after executing this function, delete all tempstrings it created
964 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
966 prog->trace = prvm_traceqc.integer;
968 // we know we're done when pr_depth drops to this
969 exitdepth = prog->depth;
971 // make a stack frame
972 st = &prog->statements[PRVM_EnterFunction(prog, func)];
973 // save the starting statement pointer for profiling
974 // (when the function exits or jumps, the (st - startst) integer value is
975 // added to the function's profile counter)
978 // instead of counting instructions, we count jumps
980 // add one to the callcount of this function because otherwise engine-called functions aren't counted
981 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
982 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
985 cachedpr_trace = prog->trace;
986 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
988 #define PRVMSLOWINTERPRETER 1
989 if (prvm_timeprofiling.integer)
991 #define PRVMTIMEPROFILING 1
992 #include "prvm_execprogram.h"
993 #undef PRVMTIMEPROFILING
997 #include "prvm_execprogram.h"
999 #undef PRVMSLOWINTERPRETER
1003 if (prvm_timeprofiling.integer)
1005 #define PRVMTIMEPROFILING 1
1006 #include "prvm_execprogram.h"
1007 #undef PRVMTIMEPROFILING
1011 #include "prvm_execprogram.h"
1016 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1017 Con_DPrintf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog, prog->functions[fnum].s_name), prog->tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1018 // delete tempstrings created by this function
1019 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1021 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
1022 func->totaltime += tm;
1024 if (prog == SVVM_prog)
1025 SV_FlushBroadcastMessages();
1027 #endif // CONFIG_MENU
1030 ====================
1032 ====================
1034 void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
1036 mstatement_t *st, *startst;
1037 mfunction_t *func, *enterfunc;
1040 int jumpcount, cachedpr_trace, exitdepth;
1041 int restorevm_tempstringsbuf_cursize;
1044 prvm_vec_t tempfloat;
1045 // these may become out of date when a builtin is called, and are updated accordingly
1046 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
1047 unsigned int cached_entityfields = prog->entityfields;
1048 unsigned int cached_entityfields_3 = prog->entityfields - 3;
1049 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
1050 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
1051 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
1052 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
1053 unsigned int cached_max_edicts = prog->max_edicts;
1054 // these do not change
1055 mstatement_t *cached_statements = prog->statements;
1056 qbool cached_allowworldwrites = prog->allowworldwrites;
1057 unsigned int cached_flag = prog->flag;
1059 prvm_vec_t *globals = prog->globals.fp;
1061 calltime = Sys_DirtyTime();
1063 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
1065 if (PRVM_allglobaledict(self))
1066 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
1067 prog->error_cmd("CLVM_ExecuteProgram: %s", errormessage);
1070 func = &prog->functions[fnum];
1072 // after executing this function, delete all tempstrings it created
1073 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
1075 prog->trace = prvm_traceqc.integer;
1077 // we know we're done when pr_depth drops to this
1078 exitdepth = prog->depth;
1080 // make a stack frame
1081 st = &prog->statements[PRVM_EnterFunction(prog, func)];
1082 // save the starting statement pointer for profiling
1083 // (when the function exits or jumps, the (st - startst) integer value is
1084 // added to the function's profile counter)
1087 // instead of counting instructions, we count jumps
1089 // add one to the callcount of this function because otherwise engine-called functions aren't counted
1090 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
1091 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
1094 cachedpr_trace = prog->trace;
1095 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
1097 #define PRVMSLOWINTERPRETER 1
1098 if (prvm_timeprofiling.integer)
1100 #define PRVMTIMEPROFILING 1
1101 #include "prvm_execprogram.h"
1102 #undef PRVMTIMEPROFILING
1106 #include "prvm_execprogram.h"
1108 #undef PRVMSLOWINTERPRETER
1112 if (prvm_timeprofiling.integer)
1114 #define PRVMTIMEPROFILING 1
1115 #include "prvm_execprogram.h"
1116 #undef PRVMTIMEPROFILING
1120 #include "prvm_execprogram.h"
1125 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1126 Con_DPrintf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog, prog->functions[fnum].s_name), prog->tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1127 // delete tempstrings created by this function
1128 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1130 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
1131 func->totaltime += tm;
1133 if (prog == SVVM_prog)
1134 SV_FlushBroadcastMessages();
1139 ====================
1141 ====================
1144 void SVVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
1146 void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
1149 mstatement_t *st, *startst;
1150 mfunction_t *func, *enterfunc;
1153 int jumpcount, cachedpr_trace, exitdepth;
1154 int restorevm_tempstringsbuf_cursize;
1157 prvm_vec_t tempfloat;
1158 // these may become out of date when a builtin is called, and are updated accordingly
1159 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
1160 unsigned int cached_entityfields = prog->entityfields;
1161 unsigned int cached_entityfields_3 = prog->entityfields - 3;
1162 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
1163 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
1164 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
1165 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
1166 unsigned int cached_max_edicts = prog->max_edicts;
1167 // these do not change
1168 mstatement_t *cached_statements = prog->statements;
1169 qbool cached_allowworldwrites = prog->allowworldwrites;
1170 unsigned int cached_flag = prog->flag;
1172 prvm_vec_t *globals = prog->globals.fp;
1174 calltime = Sys_DirtyTime();
1176 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
1178 if (PRVM_allglobaledict(self))
1179 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
1180 prog->error_cmd("SVVM_ExecuteProgram: %s", errormessage);
1183 func = &prog->functions[fnum];
1185 // after executing this function, delete all tempstrings it created
1186 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
1188 prog->trace = prvm_traceqc.integer;
1190 // we know we're done when pr_depth drops to this
1191 exitdepth = prog->depth;
1193 // make a stack frame
1194 st = &prog->statements[PRVM_EnterFunction(prog, func)];
1195 // save the starting statement pointer for profiling
1196 // (when the function exits or jumps, the (st - startst) integer value is
1197 // added to the function's profile counter)
1200 // instead of counting instructions, we count jumps
1202 // add one to the callcount of this function because otherwise engine-called functions aren't counted
1203 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
1204 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
1207 cachedpr_trace = prog->trace;
1208 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
1210 #define PRVMSLOWINTERPRETER 1
1211 if (prvm_timeprofiling.integer)
1213 #define PRVMTIMEPROFILING 1
1214 #include "prvm_execprogram.h"
1215 #undef PRVMTIMEPROFILING
1219 #include "prvm_execprogram.h"
1221 #undef PRVMSLOWINTERPRETER
1225 if (prvm_timeprofiling.integer)
1227 #define PRVMTIMEPROFILING 1
1228 #include "prvm_execprogram.h"
1229 #undef PRVMTIMEPROFILING
1233 #include "prvm_execprogram.h"
1238 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1239 Con_DPrintf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog, prog->functions[fnum].s_name), prog->tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1240 // delete tempstrings created by this function
1241 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1243 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
1244 func->totaltime += tm;
1246 if (prog == SVVM_prog)
1247 SV_FlushBroadcastMessages();