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 void PRVM_PrintStatement (dstatement_t *s)
128 if( prog->statement_linenums ) {
131 opnum = s - prog->statements;
132 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
135 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
137 Con_Printf("%s ", prvm_opnames[s->op]);
138 i = strlen(prvm_opnames[s->op]);
143 if (s->op == OP_IF || s->op == OP_IFNOT)
144 Con_Printf("%sbranch %i",PRVM_GlobalString((unsigned short) s->a),s->b);
145 else if (s->op == OP_GOTO)
147 Con_Printf("branch %i",s->a);
149 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
151 Con_Print(PRVM_GlobalString((unsigned short) s->a));
152 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
157 Con_Print(PRVM_GlobalString((unsigned short) s->a));
159 Con_Print(PRVM_GlobalString((unsigned short) s->b));
161 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
171 void PRVM_StackTrace (void)
176 prog->stack[prog->depth].s = prog->xstatement;
177 prog->stack[prog->depth].f = prog->xfunction;
178 for (i = prog->depth;i > 0;i--)
180 f = prog->stack[i].f;
183 Con_Print("<NULL FUNCTION>\n");
185 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
196 void PRVM_Profile_f (void)
198 mfunction_t *f, *best;
199 int i, num, max/*, howmany*/;
202 //if (Cmd_Argc() == 2)
203 // howmany = atoi(Cmd_Argv(1));
206 Con_Print("prvm_profile <program name>\n");
211 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
214 Con_Printf( "%s Profile:\n[CallCount] [Statements] [BuiltinCost]\n", PRVM_NAME );
221 for (i=0 ; i<prog->progs->numfunctions ; i++)
223 f = &prog->functions[i];
224 if (max < f->profile + f->builtinsprofile + f->callcount)
226 max = f->profile + f->builtinsprofile + f->callcount;
233 if (best->first_statement < 0)
234 Con_Printf("%7i -- builtin -- %s\n", best->callcount, PRVM_GetString(best->s_name));
236 Con_Printf("%7i%7i%7i %s\n", best->callcount, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
239 best->builtinsprofile = 0;
250 prvm_prog_t *oldprog = prog;
252 for(i = 0; i < PRVM_MAXPROGS; i++)
254 if(!PRVM_ProgLoaded(i))
263 void PRVM_PrintState(void)
268 for (i = -7; i <= 0;i++)
269 if (prog->xstatement + i >= prog->xfunction->first_statement)
270 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
273 Con_Print("null function executing??\n");
282 if( prog->depth > 0 )
284 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
288 // dump the stack so host_error can shutdown functions
290 prog->localstack_used = 0;
292 // reset the prog pointer
297 ============================================================================
300 The interpretation main loop
301 ============================================================================
308 Returns the new program statement counter
311 int PRVM_EnterFunction (mfunction_t *f)
316 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
318 prog->stack[prog->depth].s = prog->xstatement;
319 prog->stack[prog->depth].f = prog->xfunction;
321 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
322 PRVM_ERROR ("stack overflow");
324 // save off any locals that the new function steps on
326 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
327 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
329 for (i=0 ; i < c ; i++)
330 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
331 prog->localstack_used += c;
335 for (i=0 ; i<f->numparms ; i++)
337 for (j=0 ; j<f->parm_size[i] ; j++)
339 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
345 return f->first_statement - 1; // offset the s++
353 int PRVM_LeaveFunction (void)
357 if (prog->depth <= 0)
358 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
360 if (!prog->xfunction)
361 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
362 // restore locals from the stack
363 c = prog->xfunction->locals;
364 prog->localstack_used -= c;
365 if (prog->localstack_used < 0)
366 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
368 for (i=0 ; i < c ; i++)
369 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
373 prog->xfunction = prog->stack[prog->depth].f;
374 return prog->stack[prog->depth].s;
377 void PRVM_Init_Exec(void)
381 prog->localstack_used = 0;
382 // reset the string table
391 // LordHavoc: optimized
392 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
393 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
394 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
395 extern cvar_t prvm_boundscheck;
396 extern cvar_t prvm_traceqc;
397 extern int PRVM_ED_FindFieldOffset (const char *field);
398 extern ddef_t* PRVM_ED_FindGlobal(const char *name);
399 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
401 dstatement_t *st, *startst;
402 mfunction_t *f, *newf;
405 int jumpcount, cachedpr_trace, exitdepth;
407 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
409 if (prog->self && PRVM_G_INT(prog->self->ofs))
410 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_G_INT(prog->self->ofs)));
411 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
414 f = &prog->functions[fnum];
416 prog->trace = prvm_traceqc.integer;
418 // we know we're done when pr_depth drops to this
419 exitdepth = prog->depth;
421 // make a stack frame
422 st = &prog->statements[PRVM_EnterFunction (f)];
423 // save the starting statement pointer for profiling
424 // (when the function exits or jumps, the (st - startst) integer value is
425 // added to the function's profile counter)
427 // instead of counting instructions, we count jumps
429 // add one to the callcount of this function because otherwise engine-called functions aren't counted
430 prog->xfunction->callcount++;
433 cachedpr_trace = prog->trace;
434 if (prvm_boundscheck.integer)
436 #define PRVMBOUNDSCHECK 1
440 #include "prvm_execprogram.h"
445 #include "prvm_execprogram.h"
450 #undef PRVMBOUNDSCHECK
454 #include "prvm_execprogram.h"
459 #include "prvm_execprogram.h"