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)
542 mfunction_t *f, *best;
546 Con_Printf( "%s Profile:\n[CallCount] [Statements] [BuiltinCost]\n", PRVM_NAME );
553 for (i=0 ; i<prog->progs->numfunctions ; i++)
555 f = &prog->functions[i];
556 if (max < f->profile + f->builtinsprofile + f->callcount)
558 max = f->profile + f->builtinsprofile + f->callcount;
564 if (num < maxfunctions && max >= mininstructions)
566 if (best->first_statement < 0)
567 Con_Printf("%9.0f ----- builtin ----- %s\n", best->callcount, PRVM_GetString(best->s_name));
569 Con_Printf("%9.0f %9.0f %9.0f %s\n", best->callcount, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
573 best->builtinsprofile = 0;
585 void PRVM_CallProfile_f (void)
589 Con_Print("prvm_callprofile <program name>\n");
594 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
608 void PRVM_Profile_f (void)
614 howmany = atoi(Cmd_Argv(2));
615 else if (Cmd_Argc() != 2)
617 Con_Print("prvm_profile <program name>\n");
622 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
625 PRVM_Profile(howmany, 1);
630 void PRVM_CrashAll(void)
633 prvm_prog_t *oldprog = prog;
635 for(i = 0; i < PRVM_MAXPROGS; i++)
637 if(!PRVM_ProgLoaded(i))
646 void PRVM_PrintState(void)
649 if(prog->statestring)
651 Con_Printf("Caller-provided information: %s\n", prog->statestring);
655 for (i = -7; i <= 0;i++)
656 if (prog->xstatement + i >= prog->xfunction->first_statement)
657 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
660 Con_Print("null function executing??\n");
664 extern sizebuf_t vm_tempstringsbuf;
665 extern cvar_t prvm_errordump;
666 void Host_Savegame_to (const char *name);
667 void PRVM_Crash(void)
672 prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
674 if( prog->depth > 0 )
676 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
680 if(prvm_errordump.integer)
683 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
686 // dump the stack so host_error can shutdown functions
688 prog->localstack_used = 0;
690 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
691 vm_tempstringsbuf.cursize = 0;
693 // reset the prog pointer
698 ============================================================================
701 The interpretation main loop
702 ============================================================================
709 Returns the new program statement counter
712 int PRVM_EnterFunction (mfunction_t *f)
717 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
719 prog->stack[prog->depth].s = prog->xstatement;
720 prog->stack[prog->depth].f = prog->xfunction;
722 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
723 PRVM_ERROR ("stack overflow");
725 // save off any locals that the new function steps on
727 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
728 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
730 for (i=0 ; i < c ; i++)
731 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
732 prog->localstack_used += c;
736 for (i=0 ; i<f->numparms ; i++)
738 for (j=0 ; j<f->parm_size[i] ; j++)
740 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
746 return f->first_statement - 1; // offset the s++
754 int PRVM_LeaveFunction (void)
758 if (prog->depth <= 0)
759 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
761 if (!prog->xfunction)
762 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
763 // restore locals from the stack
764 c = prog->xfunction->locals;
765 prog->localstack_used -= c;
766 if (prog->localstack_used < 0)
767 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
769 for (i=0 ; i < c ; i++)
770 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
774 prog->xfunction = prog->stack[prog->depth].f;
775 return prog->stack[prog->depth].s;
778 void PRVM_Init_Exec(void)
782 prog->localstack_used = 0;
783 // reset the string table
787 void CStateOp(prvm_prog_t *prog, float vara, float varb, mfunction_t *currentFunc)
789 // This function should not be called...
790 PRVM_ERROR("CStateOp not implemented.\n");
793 void CWStateOp(prvm_prog_t *prog, float vara, float varb, mfunction_t *currentFunc)
795 // This function should not be called...
796 PRVM_ERROR("CWStateOp not implemented.\n");
799 void ThinkTimeOp(prvm_prog_t *prog, prvm_edict_t *ent, float varb)
801 ent->fields.server->nextthink = prog->globals.server->time + varb;
809 // LordHavoc: optimized
810 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
811 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
812 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
813 extern cvar_t prvm_traceqc;
814 extern cvar_t prvm_statementprofiling;
815 extern sizebuf_t vm_tempstringsbuf;
816 extern qboolean prvm_runawaycheck;
817 extern qboolean prvm_boundscheck;
818 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
820 dstatement_t *st, *startst;
821 mfunction_t *f, *newf;
824 int jumpcount, cachedpr_trace, exitdepth;
825 int restorevm_tempstringsbuf_cursize;
828 calltime = Sys_DoubleTime();
830 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
832 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
833 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
834 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
837 f = &prog->functions[fnum];
839 // after executing this function, delete all tempstrings it created
840 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
842 prog->trace = prvm_traceqc.integer;
844 // we know we're done when pr_depth drops to this
845 exitdepth = prog->depth;
847 // make a stack frame
848 st = &prog->statements[PRVM_EnterFunction (f)];
849 // save the starting statement pointer for profiling
850 // (when the function exits or jumps, the (st - startst) integer value is
851 // added to the function's profile counter)
853 // instead of counting instructions, we count jumps
855 // add one to the callcount of this function because otherwise engine-called functions aren't counted
856 prog->xfunction->callcount++;
859 cachedpr_trace = prog->trace;
860 if (prvm_runawaycheck)
862 #define PRVMRUNAWAYCHECK 1
863 if (prvm_statementprofiling.integer)
865 #define PRVMSTATEMENTPROFILING 1
866 if (prvm_boundscheck)
868 #define PRVMBOUNDSCHECK 1
872 #include "prvm_execprogram.h"
877 #include "prvm_execprogram.h"
879 #undef PRVMBOUNDSCHECK
886 #include "prvm_execprogram.h"
891 #include "prvm_execprogram.h"
894 #undef PRVMSTATEMENTPROFILING
898 if (prvm_boundscheck)
900 #define PRVMBOUNDSCHECK 1
904 #include "prvm_execprogram.h"
909 #include "prvm_execprogram.h"
911 #undef PRVMBOUNDSCHECK
918 #include "prvm_execprogram.h"
923 #include "prvm_execprogram.h"
927 #undef PRVMRUNAWAYCHECK
931 if (prvm_statementprofiling.integer)
933 #define PRVMSTATEMENTPROFILING 1
934 if (prvm_boundscheck)
936 #define PRVMBOUNDSCHECK 1
940 #include "prvm_execprogram.h"
945 #include "prvm_execprogram.h"
947 #undef PRVMBOUNDSCHECK
954 #include "prvm_execprogram.h"
959 #include "prvm_execprogram.h"
962 #undef PRVMSTATEMENTPROFILING
966 if (prvm_boundscheck)
968 #define PRVMBOUNDSCHECK 1
972 #include "prvm_execprogram.h"
977 #include "prvm_execprogram.h"
979 #undef PRVMBOUNDSCHECK
986 #include "prvm_execprogram.h"
991 #include "prvm_execprogram.h"
998 if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
999 Con_Printf("PRVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
1000 // delete tempstrings created by this function
1001 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
1003 prog->functions[fnum].totaltime += (Sys_DoubleTime() - calltime);
1005 SV_FlushBroadcastMessages();