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[] =
113 char *PRVM_GlobalString (int ofs);
114 char *PRVM_GlobalStringNoContents (int ofs);
117 //=============================================================================
124 extern cvar_t prvm_statementprofiling;
125 void PRVM_PrintStatement (dstatement_t *s)
128 int opnum = (int)(s - prog->statements);
130 Con_Printf("s%i: ", opnum);
131 if( prog->statement_linenums )
132 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
134 if (prvm_statementprofiling.integer)
135 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
137 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
139 Con_Printf("%s ", prvm_opnames[s->op]);
140 i = strlen(prvm_opnames[s->op]);
141 // don't count a preceding color tag when padding the name
142 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
147 if (s->op == OP_IF || s->op == OP_IFNOT)
148 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
149 else if (s->op == OP_GOTO)
150 Con_Printf("s%i",(signed short)s->a + opnum);
151 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
153 Con_Print(PRVM_GlobalString((unsigned short) s->a));
155 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
157 else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
160 Con_Print(PRVM_GlobalString((unsigned short) s->a));
164 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
169 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
175 Con_Print(PRVM_GlobalString((unsigned short) s->a));
179 Con_Print(PRVM_GlobalString((unsigned short) s->b));
184 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
190 void PRVM_PrintFunctionStatements (const char *name)
192 int i, firststatement, endstatement;
194 func = PRVM_ED_FindFunction (name);
197 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
200 firststatement = func->first_statement;
201 if (firststatement < 0)
203 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
207 // find the end statement
208 endstatement = prog->progs->numstatements;
209 for (i = 0;i < prog->progs->numfunctions;i++)
210 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
211 endstatement = prog->functions[i].first_statement;
213 // now print the range of statements
214 Con_Printf("%s progs: disassembly of function %s (statements %i-%i):\n", PRVM_NAME, name, firststatement, endstatement);
215 for (i = firststatement;i < endstatement;i++)
217 PRVM_PrintStatement(prog->statements + i);
218 prog->statement_profile[i] = 0;
228 void PRVM_PrintFunction_f (void)
232 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
237 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
240 PRVM_PrintFunctionStatements(Cmd_Argv(2));
250 void PRVM_StackTrace (void)
255 prog->stack[prog->depth].s = prog->xstatement;
256 prog->stack[prog->depth].f = prog->xfunction;
257 for (i = prog->depth;i > 0;i--)
259 f = prog->stack[i].f;
262 Con_Print("<NULL FUNCTION>\n");
264 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
269 void PRVM_Profile (int maxfunctions, int mininstructions)
271 mfunction_t *f, *best;
275 Con_Printf( "%s Profile:\n[CallCount] [Statements] [BuiltinCost]\n", PRVM_NAME );
282 for (i=0 ; i<prog->progs->numfunctions ; i++)
284 f = &prog->functions[i];
285 if (max < f->profile + f->builtinsprofile + f->callcount)
287 max = f->profile + f->builtinsprofile + f->callcount;
293 if (num < maxfunctions && max >= mininstructions)
295 if (best->first_statement < 0)
296 Con_Printf("%9.0f ----- builtin ----- %s\n", best->callcount, PRVM_GetString(best->s_name));
298 Con_Printf("%9.0f %9.0f %9.0f %s\n", best->callcount, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
302 best->builtinsprofile = 0;
314 void PRVM_Profile_f (void)
320 howmany = atoi(Cmd_Argv(2));
321 else if (Cmd_Argc() != 2)
323 Con_Print("prvm_profile <program name>\n");
328 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
331 PRVM_Profile(howmany, 1);
339 prvm_prog_t *oldprog = prog;
341 for(i = 0; i < PRVM_MAXPROGS; i++)
343 if(!PRVM_ProgLoaded(i))
352 void PRVM_PrintState(void)
357 for (i = -7; i <= 0;i++)
358 if (prog->xstatement + i >= prog->xfunction->first_statement)
359 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
362 Con_Print("null function executing??\n");
366 extern sizebuf_t vm_tempstringsbuf;
367 void PRVM_Crash(void)
372 if( prog->depth > 0 )
374 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
378 // dump the stack so host_error can shutdown functions
380 prog->localstack_used = 0;
382 // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
383 vm_tempstringsbuf.cursize = 0;
385 // reset the prog pointer
390 ============================================================================
393 The interpretation main loop
394 ============================================================================
401 Returns the new program statement counter
404 int PRVM_EnterFunction (mfunction_t *f)
409 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
411 prog->stack[prog->depth].s = prog->xstatement;
412 prog->stack[prog->depth].f = prog->xfunction;
414 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
415 PRVM_ERROR ("stack overflow");
417 // save off any locals that the new function steps on
419 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
420 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
422 for (i=0 ; i < c ; i++)
423 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
424 prog->localstack_used += c;
428 for (i=0 ; i<f->numparms ; i++)
430 for (j=0 ; j<f->parm_size[i] ; j++)
432 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
438 return f->first_statement - 1; // offset the s++
446 int PRVM_LeaveFunction (void)
450 if (prog->depth <= 0)
451 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
453 if (!prog->xfunction)
454 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
455 // restore locals from the stack
456 c = prog->xfunction->locals;
457 prog->localstack_used -= c;
458 if (prog->localstack_used < 0)
459 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
461 for (i=0 ; i < c ; i++)
462 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
466 prog->xfunction = prog->stack[prog->depth].f;
467 return prog->stack[prog->depth].s;
470 void PRVM_Init_Exec(void)
474 prog->localstack_used = 0;
475 // reset the string table
484 // LordHavoc: optimized
485 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
486 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
487 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
488 extern cvar_t prvm_boundscheck;
489 extern cvar_t prvm_traceqc;
490 extern cvar_t prvm_statementprofiling;
491 extern sizebuf_t vm_tempstringsbuf;
492 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
494 dstatement_t *st, *startst;
495 mfunction_t *f, *newf;
498 int jumpcount, cachedpr_trace, exitdepth;
499 int restorevm_tempstringsbuf_cursize;
501 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
503 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
504 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict));
505 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
508 f = &prog->functions[fnum];
510 // after executing this function, delete all tempstrings it created
511 restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
513 prog->trace = prvm_traceqc.integer;
515 // we know we're done when pr_depth drops to this
516 exitdepth = prog->depth;
518 // make a stack frame
519 st = &prog->statements[PRVM_EnterFunction (f)];
520 // save the starting statement pointer for profiling
521 // (when the function exits or jumps, the (st - startst) integer value is
522 // added to the function's profile counter)
524 // instead of counting instructions, we count jumps
526 // add one to the callcount of this function because otherwise engine-called functions aren't counted
527 prog->xfunction->callcount++;
530 cachedpr_trace = prog->trace;
531 if (prvm_statementprofiling.integer)
533 #define PRVMSTATEMENTPROFILING 1
534 if (prvm_boundscheck.integer)
536 #define PRVMBOUNDSCHECK 1
540 #include "prvm_execprogram.h"
545 #include "prvm_execprogram.h"
547 #undef PRVMBOUNDSCHECK
554 #include "prvm_execprogram.h"
559 #include "prvm_execprogram.h"
562 #undef PRVMSTATEMENTPROFILING
566 if (prvm_boundscheck.integer)
568 #define PRVMBOUNDSCHECK 1
572 #include "prvm_execprogram.h"
577 #include "prvm_execprogram.h"
579 #undef PRVMBOUNDSCHECK
586 #include "prvm_execprogram.h"
591 #include "prvm_execprogram.h"
597 if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
598 Con_Printf("PRVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
599 // delete tempstrings created by this function
600 vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;