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);
318 //=============================================================================
325 extern cvar_t prvm_statementprofiling;
326 void PRVM_PrintStatement (dstatement_t *s)
329 int opnum = (int)(s - prog->statements);
331 Con_Printf("s%i: ", opnum);
332 if( prog->statement_linenums )
333 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
335 if (prvm_statementprofiling.integer)
336 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
338 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
340 Con_Printf("%s ", prvm_opnames[s->op]);
341 i = strlen(prvm_opnames[s->op]);
342 // don't count a preceding color tag when padding the name
343 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
348 if (s->op == OP_IF || s->op == OP_IFNOT)
349 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
350 else if (s->op == OP_GOTO)
351 Con_Printf("s%i",(signed short)s->a + opnum);
352 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
354 Con_Print(PRVM_GlobalString((unsigned short) s->a));
356 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
358 else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
361 Con_Print(PRVM_GlobalString((unsigned short) s->a));
365 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
370 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
376 Con_Print(PRVM_GlobalString((unsigned short) s->a));
380 Con_Print(PRVM_GlobalString((unsigned short) s->b));
384 if(!s->b) // added for debugging --blub
385 Con_Print(", (nil)");
387 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
393 void PRVM_PrintFunctionStatements (const char *name)
395 int i, firststatement, endstatement;
397 func = PRVM_ED_FindFunction (name);
400 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
403 firststatement = func->first_statement;
404 if (firststatement < 0)
406 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
410 // find the end statement
411 endstatement = prog->progs->numstatements;
412 for (i = 0;i < prog->progs->numfunctions;i++)
413 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
414 endstatement = prog->functions[i].first_statement;
416 // now print the range of statements
417 Con_Printf("%s progs: disassembly of function %s (statements %i-%i):\n", PRVM_NAME, name, firststatement, endstatement);
418 for (i = firststatement;i < endstatement;i++)
420 PRVM_PrintStatement(prog->statements + i);
421 prog->statement_profile[i] = 0;
431 void PRVM_PrintFunction_f (void)
435 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
440 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
443 PRVM_PrintFunctionStatements(Cmd_Argv(2));
453 void PRVM_StackTrace (void)
458 prog->stack[prog->depth].s = prog->xstatement;
459 prog->stack[prog->depth].f = prog->xfunction;
460 for (i = prog->depth;i > 0;i--)
462 f = prog->stack[i].f;
465 Con_Print("<NULL FUNCTION>\n");
467 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
471 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
478 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
482 strlcpy(buf, "<NO PROG>", bufsize);
486 prog->stack[prog->depth].s = prog->xstatement;
487 prog->stack[prog->depth].f = prog->xfunction;
488 for (i = prog->depth;i > 0;i--)
490 f = prog->stack[i].f;
494 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
503 void PRVM_CallProfile (void)
505 mfunction_t *f, *best;
510 Con_Printf( "%s Call Profile:\n", PRVM_NAME );
517 for (i=0 ; i<prog->progs->numfunctions ; i++)
519 f = &prog->functions[i];
520 if (max < f->totaltime)
528 sum += best->totaltime;
529 Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
534 Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
535 Con_Printf(" - used by QC code of this VM: %9.4f\n", sum);
537 prog->starttime = Sys_DoubleTime();
540 void PRVM_Profile (int maxfunctions, int mininstructions, int sortby)
542 mfunction_t *f, *best;
546 Con_Printf( "%s Profile:\n[CallCount] [Statement] [BuiltinCt] [StmtTotal] [BltnTotal] [self]\n", PRVM_NAME );
547 // 12345678901 12345678901 12345678901 12345678901 12345678901 123.45%
554 for (i=0 ; i<prog->progs->numfunctions ; i++)
556 f = &prog->functions[i];
559 if (max < f->profile_total + f->builtinsprofile_total + f->callcount)
561 max = f->profile_total + f->builtinsprofile_total + f->callcount;
567 if (max < f->profile + f->builtinsprofile + f->callcount)
569 max = f->profile + f->builtinsprofile + f->callcount;
576 if (num < maxfunctions && max >= mininstructions)
578 if (best->first_statement < 0)
579 Con_Printf("%11.0f ----------------------- builtin ----------------------- %s\n", best->callcount, PRVM_GetString(best->s_name));
580 // 12345678901 12345678901 12345678901 12345678901 123.45%
582 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));
586 best->builtinsprofile = 0;
587 best->profile_total = 0;
588 best->builtinsprofile_total = 0;
600 void PRVM_CallProfile_f (void)
604 Con_Print("prvm_callprofile <program name>\n");
609 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
623 void PRVM_Profile_f (void)
629 howmany = atoi(Cmd_Argv(2));
630 else if (Cmd_Argc() != 2)
632 Con_Print("prvm_profile <program name>\n");
637 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
640 PRVM_Profile(howmany, 1, 0);
645 void PRVM_ChildProfile_f (void)
651 howmany = atoi(Cmd_Argv(2));
652 else if (Cmd_Argc() != 2)
654 Con_Print("prvm_childprofile <program name>\n");
659 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
662 PRVM_Profile(howmany, 1, 1);
667 void PRVM_CrashAll(void)
670 prvm_prog_t *oldprog = prog;
672 for(i = 0; i < PRVM_MAXPROGS; i++)
674 if(!PRVM_ProgLoaded(i))
683 void PRVM_PrintState(void)
686 if(prog->statestring)
688 Con_Printf("Caller-provided information: %s\n", prog->statestring);
692 for (i = -7; i <= 0;i++)
693 if (prog->xstatement + i >= prog->xfunction->first_statement)
694 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
697 Con_Print("null function executing??\n");
701 extern sizebuf_t vm_tempstringsbuf;
702 extern cvar_t prvm_errordump;
703 void Host_Savegame_to (const char *name);
704 void PRVM_Crash(void)
709 prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
711 if( prog->depth > 0 )
713 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
717 if(prvm_errordump.integer)
720 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
723 // dump the stack so host_error can shutdown functions
725 prog->localstack_used = 0;
727 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
728 vm_tempstringsbuf.cursize = 0;
730 // reset the prog pointer
735 ============================================================================
738 The interpretation main loop
739 ============================================================================
746 Returns the new program statement counter
749 int PRVM_EnterFunction (mfunction_t *f)
754 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
756 prog->stack[prog->depth].s = prog->xstatement;
757 prog->stack[prog->depth].f = prog->xfunction;
758 prog->stack[prog->depth].profile_acc = -f->profile;
759 prog->stack[prog->depth].builtinsprofile_acc = -f->builtinsprofile;
761 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
762 PRVM_ERROR ("stack overflow");
764 // save off any locals that the new function steps on
766 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
767 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
769 for (i=0 ; i < c ; i++)
770 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
771 prog->localstack_used += c;
775 for (i=0 ; i<f->numparms ; i++)
777 for (j=0 ; j<f->parm_size[i] ; j++)
779 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
786 return f->first_statement - 1; // offset the s++
794 int PRVM_LeaveFunction (void)
799 if (prog->depth <= 0)
800 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
802 if (!prog->xfunction)
803 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
804 // restore locals from the stack
805 c = prog->xfunction->locals;
806 prog->localstack_used -= c;
807 if (prog->localstack_used < 0)
808 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
810 for (i=0 ; i < c ; i++)
811 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
817 prog->xfunction = prog->stack[prog->depth].f;
818 prog->stack[prog->depth].profile_acc += f->profile;
819 prog->stack[prog->depth].builtinsprofile_acc += f->builtinsprofile;
822 prog->stack[prog->depth-1].profile_acc += prog->stack[prog->depth].profile_acc;
823 prog->stack[prog->depth-1].builtinsprofile_acc += prog->stack[prog->depth].builtinsprofile_acc;
827 // if f is already on the call stack...
828 // we cannot add this profile data to it now
829 // or we would add it more than once
830 // so, let's only add to the function's profile if it is the outermost call
831 f->profile_total += prog->stack[prog->depth].profile_acc;
832 f->builtinsprofile_total += prog->stack[prog->depth].builtinsprofile_acc;
835 return prog->stack[prog->depth].s;
838 void PRVM_Init_Exec(void)
842 prog->localstack_used = 0;
843 // reset the string table
847 void CStateOp(prvm_prog_t *prog, float vara, float varb, mfunction_t *currentFunc)
849 // This function should not be called...
850 PRVM_ERROR("CStateOp not implemented.\n");
853 void CWStateOp(prvm_prog_t *prog, float vara, float varb, mfunction_t *currentFunc)
855 // This function should not be called...
856 PRVM_ERROR("CWStateOp not implemented.\n");
859 void ThinkTimeOp(prvm_prog_t *prog, prvm_edict_t *ent, float varb)
861 ent->fields.server->nextthink = prog->globals.server->time + varb;
869 // LordHavoc: optimized
870 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
871 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
872 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
873 extern cvar_t prvm_traceqc;
874 extern cvar_t prvm_statementprofiling;
875 extern sizebuf_t vm_tempstringsbuf;
876 extern qboolean prvm_runawaycheck;
877 extern qboolean prvm_boundscheck;
878 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
880 dstatement_t *st, *startst;
881 mfunction_t *f, *newf;
884 int jumpcount, cachedpr_trace, exitdepth;
885 int restorevm_tempstringsbuf_cursize;
888 calltime = Sys_DoubleTime();
890 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
892 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
893 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
894 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
897 f = &prog->functions[fnum];
899 // after executing this function, delete all tempstrings it created
900 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
902 prog->trace = prvm_traceqc.integer;
904 // we know we're done when pr_depth drops to this
905 exitdepth = prog->depth;
907 // make a stack frame
908 st = &prog->statements[PRVM_EnterFunction (f)];
909 // save the starting statement pointer for profiling
910 // (when the function exits or jumps, the (st - startst) integer value is
911 // added to the function's profile counter)
913 // instead of counting instructions, we count jumps
915 // add one to the callcount of this function because otherwise engine-called functions aren't counted
916 prog->xfunction->callcount++;
919 cachedpr_trace = prog->trace;
920 if (prvm_runawaycheck)
922 #define PRVMRUNAWAYCHECK 1
923 if (prvm_statementprofiling.integer)
925 #define PRVMSTATEMENTPROFILING 1
926 if (prvm_boundscheck)
928 #define PRVMBOUNDSCHECK 1
932 #include "prvm_execprogram.h"
937 #include "prvm_execprogram.h"
939 #undef PRVMBOUNDSCHECK
946 #include "prvm_execprogram.h"
951 #include "prvm_execprogram.h"
954 #undef PRVMSTATEMENTPROFILING
958 if (prvm_boundscheck)
960 #define PRVMBOUNDSCHECK 1
964 #include "prvm_execprogram.h"
969 #include "prvm_execprogram.h"
971 #undef PRVMBOUNDSCHECK
978 #include "prvm_execprogram.h"
983 #include "prvm_execprogram.h"
987 #undef PRVMRUNAWAYCHECK
991 if (prvm_statementprofiling.integer)
993 #define PRVMSTATEMENTPROFILING 1
994 if (prvm_boundscheck)
996 #define PRVMBOUNDSCHECK 1
1000 #include "prvm_execprogram.h"
1005 #include "prvm_execprogram.h"
1007 #undef PRVMBOUNDSCHECK
1014 #include "prvm_execprogram.h"
1019 #include "prvm_execprogram.h"
1022 #undef PRVMSTATEMENTPROFILING
1026 if (prvm_boundscheck)
1028 #define PRVMBOUNDSCHECK 1
1032 #include "prvm_execprogram.h"
1037 #include "prvm_execprogram.h"
1039 #undef PRVMBOUNDSCHECK
1046 #include "prvm_execprogram.h"
1051 #include "prvm_execprogram.h"
1058 if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
1059 Con_Printf("PRVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1060 // delete tempstrings created by this function
1061 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1063 f->totaltime += (Sys_DoubleTime() - calltime);
1065 SV_FlushBroadcastMessages();