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 ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
130 Con_Printf("%s ", prvm_opnames[s->op]);
131 i = strlen(prvm_opnames[s->op]);
136 if (s->op == OP_IF || s->op == OP_IFNOT)
137 Con_Printf("%sbranch %i",PRVM_GlobalString((unsigned short) s->a),s->b);
138 else if (s->op == OP_GOTO)
140 Con_Printf("branch %i",s->a);
142 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
144 Con_Print(PRVM_GlobalString((unsigned short) s->a));
145 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
150 Con_Print(PRVM_GlobalString((unsigned short) s->a));
152 Con_Print(PRVM_GlobalString((unsigned short) s->b));
154 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
164 void PRVM_StackTrace (void)
169 prog->stack[prog->depth].s = prog->xstatement;
170 prog->stack[prog->depth].f = prog->xfunction;
171 for (i = prog->depth;i > 0;i--)
173 f = prog->stack[i].f;
176 Con_Print("<NULL FUNCTION>\n");
178 Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
189 void PRVM_Profile_f (void)
191 mfunction_t *f, *best;
192 int i, num, max/*, howmany*/;
195 //if (Cmd_Argc() == 2)
196 // howmany = atoi(Cmd_Argv(1));
199 Con_Print("prvm_profile <program name>\n");
204 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
207 Con_Printf( "%s Profile:\n[Profile] [BuiltinProfile] [CallCount]\n", PRVM_NAME );
214 for (i=0 ; i<prog->progs->numfunctions ; i++)
216 f = &prog->functions[i];
217 if (f->profile > max)
226 Con_Printf("%7i %7i %7i %s\n", best->profile, best->builtinsprofile, best->callcount, PRVM_GetString(best->s_name));
229 best->builtinsprofile = 0;
240 prvm_prog_t *oldprog = prog;
242 for(i = 0; i < PRVM_MAXPROGS; i++)
244 if(!PRVM_ProgLoaded(i))
253 void PRVM_PrintState(void)
258 for (i = -7; i <= 0;i++)
259 if (prog->xstatement + i >= prog->xfunction->first_statement)
260 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
263 Con_Print("null function executing??\n");
272 if( prog->depth > 0 )
274 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
278 // dump the stack so host_error can shutdown functions
280 prog->localstack_used = 0;
282 // reset the prog pointer
287 ============================================================================
290 The interpretation main loop
291 ============================================================================
298 Returns the new program statement counter
301 int PRVM_EnterFunction (mfunction_t *f)
306 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s\n", PRVM_NAME);
308 prog->stack[prog->depth].s = prog->xstatement;
309 prog->stack[prog->depth].f = prog->xfunction;
311 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
312 PRVM_ERROR ("stack overflow");
314 // save off any locals that the new function steps on
316 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
317 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s\n", PRVM_NAME);
319 for (i=0 ; i < c ; i++)
320 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
321 prog->localstack_used += c;
325 for (i=0 ; i<f->numparms ; i++)
327 for (j=0 ; j<f->parm_size[i] ; j++)
329 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
335 return f->first_statement - 1; // offset the s++
343 int PRVM_LeaveFunction (void)
347 if (prog->depth <= 0)
348 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
350 if (!prog->xfunction)
351 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s\n", PRVM_NAME);
352 // restore locals from the stack
353 c = prog->xfunction->locals;
354 prog->localstack_used -= c;
355 if (prog->localstack_used < 0)
356 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s\n", PRVM_NAME);
358 for (i=0 ; i < c ; i++)
359 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
363 prog->xfunction = prog->stack[prog->depth].f;
364 return prog->stack[prog->depth].s;
367 void PRVM_Init_Exec(void)
371 prog->localstack_used = 0;
372 // reset the string table
381 // LordHavoc: optimized
382 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
383 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
384 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
385 extern cvar_t prvm_boundscheck;
386 extern cvar_t prvm_traceqc;
387 extern int PRVM_ED_FindFieldOffset (const char *field);
388 extern ddef_t* PRVM_ED_FindGlobal(const char *name);
389 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
392 mfunction_t *f, *newf;
395 int profile, startprofile, cachedpr_trace, exitdepth;
397 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
399 if (prog->self && PRVM_G_INT(prog->self->ofs))
400 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_G_INT(prog->self->ofs)));
401 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
404 f = &prog->functions[fnum];
406 prog->trace = prvm_traceqc.integer;
408 // we know we're done when pr_depth drops to this
409 exitdepth = prog->depth;
411 // make a stack frame
412 st = &prog->statements[PRVM_EnterFunction (f)];
413 startprofile = profile = 0;
416 cachedpr_trace = prog->trace;
417 if (prvm_boundscheck.integer)
419 #define PRVMBOUNDSCHECK 1
423 #include "prvm_execprogram.h"
428 #include "prvm_execprogram.h"
433 #undef PRVMBOUNDSCHECK
437 #include "prvm_execprogram.h"
442 #include "prvm_execprogram.h"