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;
239 prvm_prog_t *oldprog = prog;
241 for(i = 0; i < PRVM_MAXPROGS; i++)
243 if(!PRVM_ProgLoaded(i))
252 void PRVM_PrintState(void)
257 for (i = -7; i <= 0;i++)
258 if (prog->xstatement + i >= prog->xfunction->first_statement)
259 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
262 Con_Print("null function executing??\n");
269 //TODO: make this more compilant with PR_Crash
272 // kill the stack just to be sure
274 prog->localstack_used = 0;
278 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
281 // dump the stack so host_error can shutdown functions
283 prog->localstack_used = 0;
288 ============================================================================
291 The interpretation main loop
292 ============================================================================
299 Returns the new program statement counter
302 int PRVM_EnterFunction (mfunction_t *f)
307 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s\n", PRVM_NAME);
309 prog->stack[prog->depth].s = prog->xstatement;
310 prog->stack[prog->depth].f = prog->xfunction;
312 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
313 PRVM_ERROR ("stack overflow");
315 // save off any locals that the new function steps on
317 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
318 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s\n", PRVM_NAME);
320 for (i=0 ; i < c ; i++)
321 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
322 prog->localstack_used += c;
326 for (i=0 ; i<f->numparms ; i++)
328 for (j=0 ; j<f->parm_size[i] ; j++)
330 ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
336 return f->first_statement - 1; // offset the s++
344 int PRVM_LeaveFunction (void)
348 if (prog->depth <= 0)
349 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
351 if (!prog->xfunction)
352 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s\n", PRVM_NAME);
353 // restore locals from the stack
354 c = prog->xfunction->locals;
355 prog->localstack_used -= c;
356 if (prog->localstack_used < 0)
357 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s\n", PRVM_NAME);
359 for (i=0 ; i < c ; i++)
360 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
364 prog->xfunction = prog->stack[prog->depth].f;
365 return prog->stack[prog->depth].s;
368 void PRVM_Init_Exec(void)
372 prog->localstack_used = 0;
373 // reset the string table
382 // LordHavoc: optimized
383 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
384 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
385 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
386 extern cvar_t prvm_boundscheck;
387 extern cvar_t prvm_traceqc;
388 extern int PRVM_ED_FindFieldOffset (const char *field);
389 extern ddef_t* PRVM_ED_FindGlobal(const char *name);
390 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
393 mfunction_t *f, *newf;
396 int profile, startprofile, cachedpr_trace, exitdepth;
398 if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
400 if (prog->self && PRVM_G_INT(prog->self->ofs))
401 PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_G_INT(prog->self->ofs)));
402 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
405 f = &prog->functions[fnum];
407 prog->trace = prvm_traceqc.integer;
409 // we know we're done when pr_depth drops to this
410 exitdepth = prog->depth;
412 // make a stack frame
413 st = &prog->statements[PRVM_EnterFunction (f)];
414 startprofile = profile = 0;
417 cachedpr_trace = prog->trace;
418 if (prvm_boundscheck.integer)
420 #define PRVMBOUNDSCHECK 1
424 #include "prvm_execprogram.h"
429 #include "prvm_execprogram.h"
434 #undef PRVMBOUNDSCHECK
438 #include "prvm_execprogram.h"
443 #include "prvm_execprogram.h"