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 p = buf + max(0, dpsnprintf(buf, bufsize, "(%s) ", prog->name));
445 dp_strlcpy(buf, "<NO PROG>", bufsize);
449 prog->stack[prog->depth].s = prog->xstatement;
450 prog->stack[prog->depth].f = prog->xfunction;
451 for (i = prog->depth;i > 0;i--)
453 f = prog->stack[i].f;
458 ? 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)
461 if (p == buf + bufsize)
467 static void PRVM_CallProfile (prvm_prog_t *prog)
469 mfunction_t *f, *best;
473 double newprofiletime;
475 Con_Printf( "%s Call Profile:\n", prog->name );
482 for (i=0 ; i<prog->numfunctions ; i++)
484 f = &prog->functions[i];
485 if (max < f->totaltime)
493 sum += best->totaltime;
494 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(prog, best->s_name));
499 newprofiletime = Sys_DirtyTime();
500 Con_Printf("Total time since last profile reset: %9.4f\n", newprofiletime - prog->profiletime);
501 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
503 prog->profiletime = newprofiletime;
506 void PRVM_Profile (prvm_prog_t *prog, int maxfunctions, double mintime, int sortby)
508 mfunction_t *f, *best;
512 if(!prvm_timeprofiling.integer)
513 mintime *= 10000000; // count each statement as about 0.1µs
515 if(prvm_timeprofiling.integer)
516 Con_Printf( "%s Profile:\n[CallCount] [Time] [BuiltinTm] [Statement] [BuiltinCt] [TimeTotal] [StmtTotal] [BltnTotal] [self]\n", prog->name );
517 // 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
519 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", prog->name );
520 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
527 for (i=0 ; i<prog->numfunctions ; i++)
529 f = &prog->functions[i];
530 if(prvm_timeprofiling.integer)
534 if(f->first_statement < 0)
536 if (max < f->tprofile)
544 if (max < f->tprofile_total)
546 max = f->tprofile_total;
553 if (max < f->tprofile + f->tbprofile)
555 max = f->tprofile + f->tbprofile;
564 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
566 max = f->profile_total + f->builtinsprofile_total + f->callcount;
572 if (max < f->profile + f->builtinsprofile + f->callcount)
574 max = f->profile + f->builtinsprofile + f->callcount;
582 if (num < maxfunctions && max > mintime)
584 if(prvm_timeprofiling.integer)
586 if (best->first_statement < 0)
587 Con_Printf("%11.0f %11.6f ------------- builtin ------------- %11.6f ----------- builtin ----------- %s\n", best->callcount, best->tprofile, best->tprofile, PRVM_GetString(prog, best->s_name));
588 // %11.6f 12345678901 12345678901 12345678901 %11.6f 12345678901 12345678901 123.45%
590 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));
594 if (best->first_statement < 0)
595 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(prog, best->s_name));
596 // 12345678901 12345678901 12345678901 12345678901 123.45%
598 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));
605 best->builtinsprofile = 0;
606 best->profile_total = 0;
607 best->tprofile_total = 0;
608 best->builtinsprofile_total = 0;
620 void PRVM_CallProfile_f(cmd_state_t *cmd)
623 if (Cmd_Argc(cmd) != 2)
625 Con_Print("prvm_callprofile <program name>\n");
629 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
632 PRVM_CallProfile(prog);
641 void PRVM_Profile_f(cmd_state_t *cmd)
646 if (prvm_coverage.integer & 1)
648 Con_Printf("Collecting function coverage, cannot profile - sorry!\n");
653 if (Cmd_Argc(cmd) == 3)
654 howmany = atoi(Cmd_Argv(cmd, 2));
655 else if (Cmd_Argc(cmd) != 2)
657 Con_Print("prvm_profile <program name>\n");
661 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
664 PRVM_Profile(prog, howmany, 0, 0);
667 void PRVM_ChildProfile_f(cmd_state_t *cmd)
672 if (prvm_coverage.integer & 1)
674 Con_Printf("Collecting function coverage, cannot profile - sorry!\n");
679 if (Cmd_Argc(cmd) == 3)
680 howmany = atoi(Cmd_Argv(cmd, 2));
681 else if (Cmd_Argc(cmd) != 2)
683 Con_Print("prvm_childprofile <program name>\n");
687 if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1))))
690 PRVM_Profile(prog, howmany, 0, 1);
693 void PRVM_PrintState(prvm_prog_t *prog, int stack_index)
696 mfunction_t *func = prog->xfunction;
697 int st = prog->xstatement;
698 if (stack_index > 0 && stack_index <= prog->depth)
700 func = prog->stack[prog->depth - stack_index].f;
701 st = prog->stack[prog->depth - stack_index].s;
703 if (prog->statestring)
705 Con_Printf("Caller-provided information: %s\n", prog->statestring);
709 for (i = -7; i <= 0;i++)
710 if (st + i >= func->first_statement)
711 PRVM_PrintStatement(prog, prog->statements + st + i);
713 PRVM_StackTrace(prog);
716 extern cvar_t prvm_errordump;
717 void PRVM_Crash(prvm_prog_t *prog)
720 int outfd = sys.outfd;
722 cl.csqc_loaded = false;
729 // set output to stderr
730 sys.outfd = fileno(stderr);
732 PRVM_serverfunction(SV_Shutdown) = 0; // don't call SV_Shutdown on crash
734 if( prog->depth > 0 )
736 Con_Printf("QuakeC crash report for %s:\n", prog->name);
737 PRVM_PrintState(prog, 0);
740 if(prvm_errordump.integer)
743 SV_Savegame_to(prog, va(vabuf, sizeof(vabuf), "crash-%s.dmp", prog->name));
746 // dump the stack so host_error can shutdown functions
748 prog->localstack_used = 0;
750 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
751 prog->tempstringsbuf.cursize = 0;
753 // reset the prog pointer
756 // restore configured outfd
761 ============================================================================
764 The interpretation main loop
765 ============================================================================
772 Returns the new program statement counter
775 static int PRVM_EnterFunction (prvm_prog_t *prog, mfunction_t *f)
780 prog->error_cmd("PRVM_EnterFunction: NULL function in %s", prog->name);
782 prog->stack[prog->depth].s = prog->xstatement;
783 prog->stack[prog->depth].f = prog->xfunction;
784 prog->stack[prog->depth].profile_acc = -f->profile;
785 prog->stack[prog->depth].tprofile_acc = -f->tprofile + -f->tbprofile;
786 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
788 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
789 prog->error_cmd("stack overflow");
791 // save off any locals that the new function steps on
793 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
794 prog->error_cmd("PRVM_ExecuteProgram: locals stack overflow in %s", prog->name);
796 for (i=0 ; i < c ; i++)
797 prog->localstack[prog->localstack_used+i] = prog->globals.ip[f->parm_start + i];
798 prog->localstack_used += c;
802 for (i=0 ; i<f->numparms ; i++)
804 for (j=0 ; j<f->parm_size[i] ; j++)
806 prog->globals.ip[o] = prog->globals.ip[OFS_PARM0+i*3+j];
813 return f->first_statement - 1; // offset the s++
821 static int PRVM_LeaveFunction (prvm_prog_t *prog)
826 if (prog->depth <= 0)
827 prog->error_cmd("prog stack underflow in %s", prog->name);
829 if (!prog->xfunction)
830 prog->error_cmd("PR_LeaveFunction: NULL function in %s", prog->name);
831 // restore locals from the stack
832 c = prog->xfunction->locals;
833 prog->localstack_used -= c;
834 if (prog->localstack_used < 0)
835 prog->error_cmd("PRVM_ExecuteProgram: locals stack underflow in %s", prog->name);
837 for (i=0 ; i < c ; i++)
838 prog->globals.ip[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
844 prog->xfunction = prog->stack[prog->depth].f;
845 prog->stack[prog->depth].profile_acc += f->profile;
846 prog->stack[prog->depth].tprofile_acc += f->tprofile + f->tbprofile;
847 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
850 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
851 prog->stack[prog->depth-1].tprofile_acc += prog->stack[prog->depth].tprofile_acc;
852 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
856 // if f is already on the call stack...
857 // we cannot add this profile data to it now
858 // or we would add it more than once
859 // so, let's only add to the function's profile if it is the outermost call
860 f->profile_total += prog->stack[prog->depth].profile_acc;
861 f->tprofile_total += prog->stack[prog->depth].tprofile_acc;
862 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
865 return prog->stack[prog->depth].s;
868 void PRVM_Init_Exec(prvm_prog_t *prog)
872 prog->localstack_used = 0;
873 // reset the string table
882 // Note: in these two calls, prog->xfunction is assumed to be sane.
883 static const char *PRVM_WhereAmI(char *buf, size_t bufsize, prvm_prog_t *prog, mfunction_t *func, int statement)
885 if (prog->statement_linenums)
887 if (prog->statement_columnnums)
888 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);
890 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);
893 return va(buf, bufsize, "%s(%s, %i)", PRVM_GetString(prog, func->s_file), PRVM_GetString(prog, func->s_name), statement - func->first_statement);
895 static void PRVM_FunctionCoverageEvent(prvm_prog_t *prog, mfunction_t *func)
897 ++prog->functions_covered;
898 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);
900 void PRVM_ExplicitCoverageEvent(prvm_prog_t *prog, mfunction_t *func, int statement)
903 ++prog->explicit_covered;
904 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);
906 static void PRVM_StatementCoverageEvent(prvm_prog_t *prog, mfunction_t *func, int statement)
909 ++prog->statements_covered;
910 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);
913 #if defined (__GNUC__) || (__clang__) || (__TINYC__)
914 # ifndef CONFIG_PEDANTIC
915 # define HAVE_COMPUTED_GOTOS 1
919 #define OPA ((prvm_eval_t *)&globals[st->operand[0]])
920 #define OPB ((prvm_eval_t *)&globals[st->operand[1]])
921 #define OPC ((prvm_eval_t *)&globals[st->operand[2]])
922 extern cvar_t prvm_traceqc;
923 extern cvar_t prvm_statementprofiling;
924 extern qbool prvm_runawaycheck;
933 void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
935 mstatement_t *st, *startst;
936 mfunction_t *func, *enterfunc;
939 int jumpcount, cachedpr_trace, exitdepth;
940 int restorevm_tempstringsbuf_cursize;
943 prvm_vec_t tempfloat;
944 // these may become out of date when a builtin is called, and are updated accordingly
945 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
946 unsigned int cached_entityfields = prog->entityfields;
947 unsigned int cached_entityfields_3 = prog->entityfields - 3;
948 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
949 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
950 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
951 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
952 unsigned int cached_max_edicts = prog->max_edicts;
953 // these do not change
954 mstatement_t *cached_statements = prog->statements;
955 qbool cached_allowworldwrites = prog->allowworldwrites;
956 unsigned int cached_flag = prog->flag;
958 prvm_vec_t *globals = prog->globals.fp;
960 calltime = Sys_DirtyTime();
962 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
964 if (PRVM_allglobaledict(self))
965 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
966 prog->error_cmd("MVM_ExecuteProgram: %s", errormessage);
969 func = &prog->functions[fnum];
971 // after executing this function, delete all tempstrings it created
972 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
974 prog->trace = prvm_traceqc.integer;
976 // we know we're done when pr_depth drops to this
977 exitdepth = prog->depth;
979 // make a stack frame
980 st = &prog->statements[PRVM_EnterFunction(prog, func)];
981 // save the starting statement pointer for profiling
982 // (when the function exits or jumps, the (st - startst) integer value is
983 // added to the function's profile counter)
986 // instead of counting instructions, we count jumps
988 // add one to the callcount of this function because otherwise engine-called functions aren't counted
989 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
990 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
993 cachedpr_trace = prog->trace;
994 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
996 #define PRVMSLOWINTERPRETER 1
997 if (prvm_timeprofiling.integer)
999 #define PRVMTIMEPROFILING 1
1000 #include "prvm_execprogram.h"
1001 #undef PRVMTIMEPROFILING
1005 #include "prvm_execprogram.h"
1007 #undef PRVMSLOWINTERPRETER
1011 if (prvm_timeprofiling.integer)
1013 #define PRVMTIMEPROFILING 1
1014 #include "prvm_execprogram.h"
1015 #undef PRVMTIMEPROFILING
1019 #include "prvm_execprogram.h"
1024 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1025 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);
1026 // delete tempstrings created by this function
1027 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1029 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
1030 func->totaltime += tm;
1032 if (prog == SVVM_prog)
1033 SV_FlushBroadcastMessages();
1038 ====================
1040 ====================
1042 void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
1044 mstatement_t *st, *startst;
1045 mfunction_t *func, *enterfunc;
1048 int jumpcount, cachedpr_trace, exitdepth;
1049 int restorevm_tempstringsbuf_cursize;
1052 prvm_vec_t tempfloat;
1053 // these may become out of date when a builtin is called, and are updated accordingly
1054 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
1055 unsigned int cached_entityfields = prog->entityfields;
1056 unsigned int cached_entityfields_3 = prog->entityfields - 3;
1057 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
1058 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
1059 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
1060 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
1061 unsigned int cached_max_edicts = prog->max_edicts;
1062 // these do not change
1063 mstatement_t *cached_statements = prog->statements;
1064 qbool cached_allowworldwrites = prog->allowworldwrites;
1065 unsigned int cached_flag = prog->flag;
1067 prvm_vec_t *globals = prog->globals.fp;
1069 calltime = Sys_DirtyTime();
1071 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
1073 if (PRVM_allglobaledict(self))
1074 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
1075 prog->error_cmd("CLVM_ExecuteProgram: %s", errormessage);
1078 func = &prog->functions[fnum];
1080 // after executing this function, delete all tempstrings it created
1081 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
1083 prog->trace = prvm_traceqc.integer;
1085 // we know we're done when pr_depth drops to this
1086 exitdepth = prog->depth;
1088 // make a stack frame
1089 st = &prog->statements[PRVM_EnterFunction(prog, func)];
1090 // save the starting statement pointer for profiling
1091 // (when the function exits or jumps, the (st - startst) integer value is
1092 // added to the function's profile counter)
1095 // instead of counting instructions, we count jumps
1097 // add one to the callcount of this function because otherwise engine-called functions aren't counted
1098 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
1099 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
1102 cachedpr_trace = prog->trace;
1103 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
1105 #define PRVMSLOWINTERPRETER 1
1106 if (prvm_timeprofiling.integer)
1108 #define PRVMTIMEPROFILING 1
1109 #include "prvm_execprogram.h"
1110 #undef PRVMTIMEPROFILING
1114 #include "prvm_execprogram.h"
1116 #undef PRVMSLOWINTERPRETER
1120 if (prvm_timeprofiling.integer)
1122 #define PRVMTIMEPROFILING 1
1123 #include "prvm_execprogram.h"
1124 #undef PRVMTIMEPROFILING
1128 #include "prvm_execprogram.h"
1133 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1134 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);
1135 // delete tempstrings created by this function
1136 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1138 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
1139 func->totaltime += tm;
1141 if (prog == SVVM_prog)
1142 SV_FlushBroadcastMessages();
1147 ====================
1149 ====================
1152 void SVVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
1154 void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage)
1157 mstatement_t *st, *startst;
1158 mfunction_t *func, *enterfunc;
1161 int jumpcount, cachedpr_trace, exitdepth;
1162 int restorevm_tempstringsbuf_cursize;
1165 prvm_vec_t tempfloat;
1166 // these may become out of date when a builtin is called, and are updated accordingly
1167 prvm_vec_t *cached_edictsfields = prog->edictsfields.fp;
1168 unsigned int cached_entityfields = prog->entityfields;
1169 unsigned int cached_entityfields_3 = prog->entityfields - 3;
1170 unsigned int cached_entityfieldsarea = prog->entityfieldsarea;
1171 unsigned int cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
1172 unsigned int cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
1173 unsigned int cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
1174 unsigned int cached_max_edicts = prog->max_edicts;
1175 // these do not change
1176 mstatement_t *cached_statements = prog->statements;
1177 qbool cached_allowworldwrites = prog->allowworldwrites;
1178 unsigned int cached_flag = prog->flag;
1180 prvm_vec_t *globals = prog->globals.fp;
1182 calltime = Sys_DirtyTime();
1184 if (!fnum || fnum >= (unsigned int)prog->numfunctions)
1186 if (PRVM_allglobaledict(self))
1187 PRVM_ED_Print(prog, PRVM_PROG_TO_EDICT(PRVM_allglobaledict(self)), NULL);
1188 prog->error_cmd("SVVM_ExecuteProgram: %s", errormessage);
1191 func = &prog->functions[fnum];
1193 // after executing this function, delete all tempstrings it created
1194 restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize;
1196 prog->trace = prvm_traceqc.integer;
1198 // we know we're done when pr_depth drops to this
1199 exitdepth = prog->depth;
1201 // make a stack frame
1202 st = &prog->statements[PRVM_EnterFunction(prog, func)];
1203 // save the starting statement pointer for profiling
1204 // (when the function exits or jumps, the (st - startst) integer value is
1205 // added to the function's profile counter)
1208 // instead of counting instructions, we count jumps
1210 // add one to the callcount of this function because otherwise engine-called functions aren't counted
1211 if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1))
1212 PRVM_FunctionCoverageEvent(prog, prog->xfunction);
1215 cachedpr_trace = prog->trace;
1216 if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0)
1218 #define PRVMSLOWINTERPRETER 1
1219 if (prvm_timeprofiling.integer)
1221 #define PRVMTIMEPROFILING 1
1222 #include "prvm_execprogram.h"
1223 #undef PRVMTIMEPROFILING
1227 #include "prvm_execprogram.h"
1229 #undef PRVMSLOWINTERPRETER
1233 if (prvm_timeprofiling.integer)
1235 #define PRVMTIMEPROFILING 1
1236 #include "prvm_execprogram.h"
1237 #undef PRVMTIMEPROFILING
1241 #include "prvm_execprogram.h"
1246 if (developer_insane.integer && prog->tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1247 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);
1248 // delete tempstrings created by this function
1249 prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1251 tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0;
1252 func->totaltime += tm;
1254 if (prog == SVVM_prog)
1255 SV_FlushBroadcastMessages();