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[Profile] [BuiltinProfile] [CallCount]\n", PRVM_NAME );
221 for (i=0 ; i<prog->progs->numfunctions ; i++)
223 f = &prog->functions[i];
224 if (f->profile > max)
233 Con_Printf("%7i %7i %7i %s\n", best->profile, best->builtinsprofile, best->callcount, PRVM_GetString(best->s_name));
236 best->builtinsprofile = 0;
247 prvm_prog_t *oldprog = prog;
249 for(i = 0; i < PRVM_MAXPROGS; i++)
251 if(!PRVM_ProgLoaded(i))
260 void PRVM_PrintState(void)
265 for (i = -7; i <= 0;i++)
266 if (prog->xstatement + i >= prog->xfunction->first_statement)
267 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
270 Con_Print("null function executing??\n");
279 if( prog->depth > 0 )
281 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
285 // dump the stack so host_error can shutdown functions
287 prog->localstack_used = 0;
289 // reset the prog pointer
294 ============================================================================
297 The interpretation main loop
298 ============================================================================
305 Returns the new program statement counter
308 int PRVM_EnterFunction (mfunction_t *f)
313 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s\n", PRVM_NAME);
315 prog->stack[prog->depth].s = prog->xstatement;
316 prog->stack[prog->depth].f = prog->xfunction;
318 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
319 PRVM_ERROR ("stack overflow");
321 // save off any locals that the new function steps on
323 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
324 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s\n", PRVM_NAME);
326 for (i=0 ; i < c ; i++)
327 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
328 prog->localstack_used += c;
332 for (i=0 ; i<f->numparms ; i++)
334 for (j=0 ; j<f->parm_size[i] ; j++)
336 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
342 return f->first_statement - 1; // offset the s++
350 int PRVM_LeaveFunction (void)
354 if (prog->depth <= 0)
355 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
357 if (!prog->xfunction)
358 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s\n", PRVM_NAME);
359 // restore locals from the stack
360 c = prog->xfunction->locals;
361 prog->localstack_used -= c;
362 if (prog->localstack_used < 0)
363 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s\n", PRVM_NAME);
365 for (i=0 ; i < c ; i++)
366 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
370 prog->xfunction = prog->stack[prog->depth].f;
371 return prog->stack[prog->depth].s;
374 void PRVM_Init_Exec(void)
378 prog->localstack_used = 0;
379 // reset the string table
388 // LordHavoc: optimized
389 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
390 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
391 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
392 extern cvar_t prvm_boundscheck;
393 extern cvar_t prvm_traceqc;
394 extern int PRVM_ED_FindFieldOffset (const char *field);
395 extern ddef_t* PRVM_ED_FindGlobal(const char *name);
396 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
399 mfunction_t *f, *newf;
402 int profile, startprofile, cachedpr_trace, exitdepth;
404 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
406 if (prog->self && PRVM_G_INT(prog->self->ofs))
407 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_G_INT(prog->self->ofs)));
408 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
411 f = &prog->functions[fnum];
413 prog->trace = prvm_traceqc.integer;
415 // we know we're done when pr_depth drops to this
416 exitdepth = prog->depth;
418 // make a stack frame
419 st = &prog->statements[PRVM_EnterFunction (f)];
420 startprofile = profile = 0;
423 cachedpr_trace = prog->trace;
424 if (prvm_boundscheck.integer)
426 #define PRVMBOUNDSCHECK 1
430 #include "prvm_execprogram.h"
435 #include "prvm_execprogram.h"
440 #undef PRVMBOUNDSCHECK
444 #include "prvm_execprogram.h"
449 #include "prvm_execprogram.h"