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.
23 char *prvm_opnames[] =
112 char *PRVM_GlobalString (int ofs);
113 char *PRVM_GlobalStringNoContents (int ofs);
116 //=============================================================================
123 void PRVM_PrintStatement (dstatement_t *s)
127 if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
129 Con_Printf ("%s ", prvm_opnames[s->op]);
130 i = strlen(prvm_opnames[s->op]);
135 if (s->op == OP_IF || s->op == OP_IFNOT)
136 Con_Printf ("%sbranch %i",PRVM_GlobalString((unsigned short) s->a),s->b);
137 else if (s->op == OP_GOTO)
139 Con_Printf ("branch %i",s->a);
141 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
143 Con_Printf ("%s", PRVM_GlobalString((unsigned short) s->a));
144 Con_Printf ("%s", PRVM_GlobalStringNoContents((unsigned short) s->b));
149 Con_Printf ("%s", PRVM_GlobalString((unsigned short) s->a));
151 Con_Printf ("%s", PRVM_GlobalString((unsigned short) s->b));
153 Con_Printf ("%s", PRVM_GlobalStringNoContents((unsigned short) s->c));
163 void PRVM_StackTrace (void)
168 prog->stack[prog->depth].s = prog->xstatement;
169 prog->stack[prog->depth].f = prog->xfunction;
170 for (i = prog->depth;i > 0;i--)
172 f = prog->stack[i].f;
175 Con_Printf ("<NULL FUNCTION>\n");
177 Con_Printf ("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
188 void PRVM_Profile_f (void)
190 mfunction_t *f, *best;
191 int i, num, max/*, howmany*/;
194 //if (Cmd_Argc() == 2)
195 // howmany = atoi(Cmd_Argv(1));
198 Con_Printf("prvm_profile <program name>\n");
203 if(!PRVM_SetProgFromString(Cmd_Argv(1)))
211 for (i=0 ; i<prog->progs->numfunctions ; i++)
213 f = &prog->functions[i];
214 if (f->profile > max)
223 Con_Printf ("%s: %7i %7i %s\n", PRVM_NAME, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
226 best->builtinsprofile = 0;
236 prvm_prog_t *oldprog = prog;
238 for(i = 0; i < PRVM_MAXPROGS; i++)
240 if(!PRVM_ProgLoaded(i))
249 void PRVM_PrintState(void)
254 for (i = -4;i <= 0;i++)
255 if (prog->xstatement + i >= prog->xfunction->first_statement)
256 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
259 Con_Printf("null function executing??\n");
267 // kill the stack just to be sure
269 prog->localstack_used = 0;
273 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
276 // dump the stack so host_error can shutdown functions
278 prog->localstack_used = 0;
283 ============================================================================
286 The interpretation main loop
287 ============================================================================
294 Returns the new program statement counter
297 int PRVM_EnterFunction (mfunction_t *f)
302 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s\n", PRVM_NAME);
304 prog->stack[prog->depth].s = prog->xstatement;
305 prog->stack[prog->depth].f = prog->xfunction;
307 if (prog->depth >=PRVM_MAX_STACK_DEPTH)
308 PRVM_ERROR ("stack overflow");
310 // save off any locals that the new function steps on
312 if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
313 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s\n", PRVM_NAME);
315 for (i=0 ; i < c ; i++)
316 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals)[f->parm_start + i];
317 prog->localstack_used += c;
321 for (i=0 ; i<f->numparms ; i++)
323 for (j=0 ; j<f->parm_size[i] ; j++)
325 ((int *)prog->globals)[o] = ((int *)prog->globals)[OFS_PARM0+i*3+j];
331 return f->first_statement - 1; // offset the s++
339 int PRVM_LeaveFunction (void)
343 if (prog->depth <= 0)
344 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
346 if (!prog->xfunction)
347 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s\n", PRVM_NAME);
348 // restore locals from the stack
349 c = prog->xfunction->locals;
350 prog->localstack_used -= c;
351 if (prog->localstack_used < 0)
352 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s\n", PRVM_NAME);
354 for (i=0 ; i < c ; i++)
355 ((int *)prog->globals)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
359 prog->xfunction = prog->stack[prog->depth].f;
360 return prog->stack[prog->depth].s;
363 void PRVM_Init_Exec(void)
367 prog->localstack_used = 0;
368 // reset the string table
377 // LordHavoc: optimized
378 #define OPA ((prvm_eval_t *)&prog->globals[(unsigned short) st->a])
379 #define OPB ((prvm_eval_t *)&prog->globals[(unsigned short) st->b])
380 #define OPC ((prvm_eval_t *)&prog->globals[(unsigned short) st->c])
381 extern cvar_t prvm_boundscheck;
382 extern cvar_t prvm_traceqc;
383 extern int PRVM_ED_FindFieldOffset (const char *field);
384 extern ddef_t* PRVM_ED_FindGlobal(const char *name);
385 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
388 mfunction_t *f, *newf;
391 int profile, startprofile, cachedpr_trace, exitdepth;
393 if (!fnum || fnum >= prog->progs->numfunctions)
395 if (prog->self && PRVM_G_INT(prog->self->ofs))
396 PRVM_ED_Print (PRVM_PROG_TO_EDICT(PRVM_G_INT(prog->self->ofs)));
397 PRVM_ERROR ("PR_ExecuteProgram: %s", errormessage);
400 f = &prog->functions[fnum];
402 prog->trace = prvm_traceqc.integer;
404 // we know we're done when pr_depth drops to this
405 exitdepth = prog->depth;
407 // make a stack frame
408 st = &prog->statements[PRVM_EnterFunction (f)];
409 startprofile = profile = 0;
412 cachedpr_trace = prog->trace;
413 if (prvm_boundscheck.integer)
415 #define PRVMBOUNDSCHECK 1
419 #include "prvm_execprogram.h"
424 #include "prvm_execprogram.h"
429 #undef PRVMBOUNDSCHECK
433 #include "prvm_execprogram.h"
438 #include "prvm_execprogram.h"