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[] =
116 "OP_DIVSTORE_F", //70
129 "OP_FETCH_GBL_F", //80
166 //mostly they are various different ways of adding two vars with conversions.
196 "OP_STOREP_IF", //120
219 "OP_POINTER_ADD", //32 bit pointers
224 "OP_LOADA_ENT", //140
236 "OP_LOADP_FLD", //150
258 //-------------------------------------
259 //string manipulation.
260 "OP_ADD_SF", //(char*)c = (char*)a + (float)b
261 "OP_SUB_S", //(float)c = (char*)a - (char*)b
262 "OP_STOREP_C",//(float)c = *(char*)b = (float)a
263 "OP_LOADP_C", //(float)c = *(char*) //170
264 //-------------------------------------
286 //erm... FTEQCC doesn't make use of these... These are for DP.
288 "OP_GSTOREP_F", //190
290 "OP_GSTOREP_FLD", // integers
292 "OP_GSTOREP_FNC", // pointers
298 "OP_GLOAD_ENT", //200
303 //back to ones that we do use.
305 "OP_PUSH", //push 4octets onto the local-stack (which is ALWAYS poped on function return). Returns a pointer.
306 "OP_POP", //pop those ones that were pushed (don't over do it). Needs assembler.
314 char *PRVM_GlobalString (int ofs);
315 char *PRVM_GlobalStringNoContents (int ofs);
316 extern ddef_t *PRVM_ED_FieldAtOfs(int ofs);
319 //=============================================================================
326 extern cvar_t prvm_statementprofiling;
327 void PRVM_PrintStatement (dstatement_t *s)
330 int opnum = (int)(s - prog->statements);
332 Con_Printf("s%i: ", opnum);
333 if( prog->statement_linenums )
334 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
336 if (prvm_statementprofiling.integer)
337 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
339 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
341 Con_Printf("%s ", prvm_opnames[s->op]);
342 i = strlen(prvm_opnames[s->op]);
343 // don't count a preceding color tag when padding the name
344 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
349 if (s->op == OP_IF || s->op == OP_IFNOT)
350 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
351 else if (s->op == OP_GOTO)
352 Con_Printf("s%i",(signed short)s->a + opnum);
353 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
355 Con_Print(PRVM_GlobalString((unsigned short) s->a));
357 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
359 else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
362 Con_Print(PRVM_GlobalString((unsigned short) s->a));
366 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
371 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
377 Con_Print(PRVM_GlobalString((unsigned short) s->a));
381 Con_Print(PRVM_GlobalString((unsigned short) s->b));
385 if(!s->b) // added for debugging --blub
386 Con_Print(", (nil)");
388 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
394 void PRVM_PrintFunctionStatements (const char *name)
396 int i, firststatement, endstatement;
398 func = PRVM_ED_FindFunction (name);
401 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
404 firststatement = func->first_statement;
405 if (firststatement < 0)
407 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
411 // find the end statement
412 endstatement = prog->progs->numstatements;
413 for (i = 0;i < prog->progs->numfunctions;i++)
414 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
415 endstatement = prog->functions[i].first_statement;
417 // now print the range of statements
418 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);
419 for (i = firststatement;i < endstatement;i++)
421 PRVM_PrintStatement(prog->statements + i);
422 prog->statement_profile[i] = 0;
432 void PRVM_PrintFunction_f (void)
436 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
441 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
444 PRVM_PrintFunctionStatements(Cmd_Argv(2));
454 void PRVM_StackTrace (void)
459 prog->stack[prog->depth].s = prog->xstatement;
460 prog->stack[prog->depth].f = prog->xfunction;
461 for (i = prog->depth;i > 0;i--)
463 f = prog->stack[i].f;
466 Con_Print("<NULL FUNCTION>\n");
468 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
472 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
479 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
483 strlcpy(buf, "<NO PROG>", bufsize);
487 prog->stack[prog->depth].s = prog->xstatement;
488 prog->stack[prog->depth].f = prog->xfunction;
489 for (i = prog->depth;i > 0;i--)
491 f = prog->stack[i].f;
495 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
504 void PRVM_CallProfile (void)
506 mfunction_t *f, *best;
511 Con_Printf( "%s Call Profile:\n", PRVM_NAME );
518 for (i=0 ; i<prog->progs->numfunctions ; i++)
520 f = &prog->functions[i];
521 if (max < f->totaltime)
529 sum += best->totaltime;
530 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
535 Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
536 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
538 prog->starttime = Sys_DoubleTime();
541 void PRVM_Profile (int maxfunctions, int mininstructions, int sortby)
543 mfunction_t *f, *best;
547 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
548 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
555 for (i=0 ; i<prog->progs->numfunctions ; i++)
557 f = &prog->functions[i];
560 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
562 max = f->profile_total + f->builtinsprofile_total + f->callcount;
568 if (max < f->profile + f->builtinsprofile + f->callcount)
570 max = f->profile + f->builtinsprofile + f->callcount;
577 if (num < maxfunctions && max >= mininstructions)
579 if (best->first_statement < 0)
580 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
581 // 12345678901 12345678901 12345678901 12345678901 123.45%
583 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));
587 best->builtinsprofile = 0;
588 best->profile_total = 0;
589 best->builtinsprofile_total = 0;
601 void PRVM_CallProfile_f (void)
605 Con_Print("prvm_callprofile <program name>\n");
610 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
624 void PRVM_Profile_f (void)
630 howmany = atoi(Cmd_Argv(2));
631 else if (Cmd_Argc() != 2)
633 Con_Print("prvm_profile <program name>\n");
638 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
641 PRVM_Profile(howmany, 1, 0);
646 void PRVM_ChildProfile_f (void)
652 howmany = atoi(Cmd_Argv(2));
653 else if (Cmd_Argc() != 2)
655 Con_Print("prvm_childprofile <program name>\n");
660 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
663 PRVM_Profile(howmany, 1, 1);
668 void PRVM_CrashAll(void)
671 prvm_prog_t *oldprog = prog;
673 for(i = 0; i < PRVM_MAXPROGS; i++)
675 if(!PRVM_ProgLoaded(i))
684 void PRVM_PrintState(void)
687 if(prog->statestring)
689 Con_Printf("Caller-provided information: %s\n", prog->statestring);
693 for (i = -7; i <= 0;i++)
694 if (prog->xstatement + i >= prog->xfunction->first_statement)
695 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
698 Con_Print("null function executing??\n");
702 extern sizebuf_t vm_tempstringsbuf;
703 extern cvar_t prvm_errordump;
704 void Host_Savegame_to (const char *name);
705 void PRVM_Crash(void)
710 prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
712 if( prog->depth > 0 )
714 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
718 if(prvm_errordump.integer)
721 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
724 // dump the stack so host_error can shutdown functions
726 prog->localstack_used = 0;
728 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
729 vm_tempstringsbuf.cursize = 0;
731 // reset the prog pointer
736 ============================================================================
739 The interpretation main loop
740 ============================================================================
747 Returns the new program statement counter
750 int PRVM_EnterFunction (mfunction_t *f)
755 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
757 prog->stack[prog->depth].s = prog->xstatement;
758 prog->stack[prog->depth].f = prog->xfunction;
759 prog->stack[prog->depth].profile_acc = -f->profile;
760 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
762 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
763 PRVM_ERROR ("stack overflow");
765 // save off any locals that the new function steps on
767 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
768 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
770 for (i=0 ; i < c ; i++)
771 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
772 prog->localstack_used += c;
776 for (i=0 ; i<f->numparms ; i++)
778 for (j=0 ; j<f->parm_size[i] ; j++)
780 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
787 return f->first_statement - 1; // offset the s++
795 int PRVM_LeaveFunction (void)
800 if (prog->depth <= 0)
801 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
803 if (!prog->xfunction)
804 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
805 // restore locals from the stack
806 c = prog->xfunction->locals;
807 prog->localstack_used -= c;
808 if (prog->localstack_used < 0)
809 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
811 for (i=0 ; i < c ; i++)
812 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
818 prog->xfunction = prog->stack[prog->depth].f;
819 prog->stack[prog->depth].profile_acc += f->profile;
820 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
823 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
824 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
828 // if f is already on the call stack...
829 // we cannot add this profile data to it now
830 // or we would add it more than once
831 // so, let's only add to the function's profile if it is the outermost call
832 f->profile_total += prog->stack[prog->depth].profile_acc;
833 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
836 return prog->stack[prog->depth].s;
839 void PRVM_Init_Exec(void)
843 prog->localstack_used = 0;
844 // reset the string table
848 void CStateOp(prvm_prog_t *prog, float vara, float varb, mfunction_t *currentFunc)
850 // This function should not be called...
851 PRVM_ERROR("CStateOp not implemented.\n");
854 void CWStateOp(prvm_prog_t *prog, float vara, float varb, mfunction_t *currentFunc)
856 // This function should not be called...
857 PRVM_ERROR("CWStateOp not implemented.\n");
860 void ThinkTimeOp(prvm_prog_t *prog, prvm_edict_t *ent, float varb)
862 ent->fields.server->nextthink = prog->globals.server->time + varb;
865 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
866 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
867 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
868 extern cvar_t prvm_traceqc;
869 extern cvar_t prvm_statementprofiling;
870 extern sizebuf_t vm_tempstringsbuf;
871 extern qboolean prvm_runawaycheck;
879 void MVM_ExecuteProgram (func_t fnum, const char *errormessage)
881 dstatement_t *st, *startst;
882 mfunction_t *f, *newf;
885 int jumpcount, cachedpr_trace, exitdepth;
886 int restorevm_tempstringsbuf_cursize;
889 calltime = Sys_DoubleTime();
891 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
893 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
894 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
895 PRVM_ERROR ("MVM_ExecuteProgram: %s", errormessage);
898 f = &prog->functions[fnum];
900 // after executing this function, delete all tempstrings it created
901 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
903 prog->trace = prvm_traceqc.integer;
905 // we know we're done when pr_depth drops to this
906 exitdepth = prog->depth;
908 // make a stack frame
909 st = &prog->statements[PRVM_EnterFunction (f)];
910 // save the starting statement pointer for profiling
911 // (when the function exits or jumps, the (st - startst) integer value is
912 // added to the function's profile counter)
914 // instead of counting instructions, we count jumps
916 // add one to the callcount of this function because otherwise engine-called functions aren't counted
917 prog->xfunction->callcount++;
920 cachedpr_trace = prog->trace;
921 if (prvm_statementprofiling.integer || prog->trace)
923 #define PRVMSLOWINTERPRETER 1
924 #include "prvm_execprogram.h"
925 #undef PRVMSLOWINTERPRETER
929 #include "prvm_execprogram.h"
933 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
934 Con_DPrintf("MVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
935 // delete tempstrings created by this function
936 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
938 f->totaltime += (Sys_DoubleTime() - calltime);
940 SV_FlushBroadcastMessages();
948 void CLVM_ExecuteProgram (func_t fnum, const char *errormessage)
950 dstatement_t *st, *startst;
951 mfunction_t *f, *newf;
954 int jumpcount, cachedpr_trace, exitdepth;
955 int restorevm_tempstringsbuf_cursize;
958 calltime = Sys_DoubleTime();
960 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
962 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
963 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
964 PRVM_ERROR ("CLVM_ExecuteProgram: %s", errormessage);
967 f = &prog->functions[fnum];
969 // after executing this function, delete all tempstrings it created
970 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
972 prog->trace = prvm_traceqc.integer;
974 // we know we're done when pr_depth drops to this
975 exitdepth = prog->depth;
977 // make a stack frame
978 st = &prog->statements[PRVM_EnterFunction (f)];
979 // save the starting statement pointer for profiling
980 // (when the function exits or jumps, the (st - startst) integer value is
981 // added to the function's profile counter)
983 // instead of counting instructions, we count jumps
985 // add one to the callcount of this function because otherwise engine-called functions aren't counted
986 prog->xfunction->callcount++;
989 cachedpr_trace = prog->trace;
990 if (prvm_statementprofiling.integer || prog->trace)
992 #define PRVMSLOWINTERPRETER 1
993 #include "prvm_execprogram.h"
994 #undef PRVMSLOWINTERPRETER
998 #include "prvm_execprogram.h"
1002 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1003 Con_DPrintf("CLVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1004 // delete tempstrings created by this function
1005 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1007 f->totaltime += (Sys_DoubleTime() - calltime);
1009 SV_FlushBroadcastMessages();
1014 ====================
1016 ====================
1018 void SVVM_ExecuteProgram (func_t fnum, const char *errormessage)
1020 dstatement_t *st, *startst;
1021 mfunction_t *f, *newf;
1024 int jumpcount, cachedpr_trace, exitdepth;
1025 int restorevm_tempstringsbuf_cursize;
1028 calltime = Sys_DoubleTime();
1030 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
1032 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
1033 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
1034 PRVM_ERROR ("SVVM_ExecuteProgram: %s", errormessage);
1037 f = &prog->functions[fnum];
1039 // after executing this function, delete all tempstrings it created
1040 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
1042 prog->trace = prvm_traceqc.integer;
1044 // we know we're done when pr_depth drops to this
1045 exitdepth = prog->depth;
1047 // make a stack frame
1048 st = &prog->statements[PRVM_EnterFunction (f)];
1049 // save the starting statement pointer for profiling
1050 // (when the function exits or jumps, the (st - startst) integer value is
1051 // added to the function's profile counter)
1053 // instead of counting instructions, we count jumps
1055 // add one to the callcount of this function because otherwise engine-called functions aren't counted
1056 prog->xfunction->callcount++;
1059 cachedpr_trace = prog->trace;
1060 if (prvm_statementprofiling.integer || prog->trace)
1062 #define PRVMSLOWINTERPRETER 1
1063 #include "prvm_execprogram.h"
1064 #undef PRVMSLOWINTERPRETER
1068 #include "prvm_execprogram.h"
1072 if (developer_insane.integer && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1073 Con_DPrintf("SVVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1074 // delete tempstrings created by this function
1075 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1077 f->totaltime += (Sys_DoubleTime() - calltime);
1079 SV_FlushBroadcastMessages();