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.
30 #define MAX_STACK_DEPTH 256
31 prstack_t pr_stack[MAX_STACK_DEPTH];
34 #define LOCALSTACK_SIZE 2048
35 int localstack[LOCALSTACK_SIZE];
40 mfunction_t *pr_xfunction;
135 char *PR_GlobalString (int ofs);
136 char *PR_GlobalStringNoContents (int ofs);
139 //=============================================================================
146 void PR_PrintStatement (dstatement_t *s)
150 if ( (unsigned)s->op < sizeof(pr_opnames)/sizeof(pr_opnames[0]))
152 Con_Printf ("%s ", pr_opnames[s->op]);
153 i = strlen(pr_opnames[s->op]);
158 if (s->op == OP_IF || s->op == OP_IFNOT)
159 Con_Printf ("%sbranch %i",PR_GlobalString((unsigned short) s->a),s->b);
160 else if (s->op == OP_GOTO)
162 Con_Printf ("branch %i",s->a);
164 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
166 Con_Printf ("%s", PR_GlobalString((unsigned short) s->a));
167 Con_Printf ("%s", PR_GlobalStringNoContents((unsigned short) s->b));
172 Con_Printf ("%s", PR_GlobalString((unsigned short) s->a));
174 Con_Printf ("%s", PR_GlobalString((unsigned short) s->b));
176 Con_Printf ("%s", PR_GlobalStringNoContents((unsigned short) s->c));
186 void PR_StackTrace (void)
191 pr_stack[pr_depth].s = pr_xstatement;
192 pr_stack[pr_depth].f = pr_xfunction;
193 for (i = pr_depth;i > 0;i--)
198 Con_Printf ("<NULL FUNCTION>\n");
200 Con_Printf ("%12s : %s : statement %i\n", PR_GetString(f->s_file), PR_GetString(f->s_name), pr_stack[i].s - f->first_statement);
211 void PR_Profile_f (void)
213 mfunction_t *f, *best;
214 int i, num, max/*, howmany*/;
217 //if (Cmd_Argc() == 2)
218 // howmany = atoi(Cmd_Argv(1));
224 for (i=0 ; i<progs->numfunctions ; i++)
226 f = &pr_functions[i];
227 if (f->profile > max)
236 Con_Printf ("%7i %7i %s\n", best->profile, best->builtinsprofile, PR_GetString(best->s_name));
239 best->builtinsprofile = 0;
250 // kill the stack just to be sure
256 Con_Printf("QuakeC crash report:\n");
259 for (i = -4;i <= 0;i++)
260 if (pr_xstatement + i >= pr_xfunction->first_statement)
261 PR_PrintStatement (pr_statements + pr_xstatement + i);
264 Con_Printf("null function executing??\n");
267 // dump the stack so host_error can shutdown functions
273 ============================================================================
276 The interpretation main loop
277 ============================================================================
284 Returns the new program statement counter
287 int PR_EnterFunction (mfunction_t *f)
292 Host_Error ("PR_EnterFunction: NULL function\n");
294 pr_stack[pr_depth].s = pr_xstatement;
295 pr_stack[pr_depth].f = pr_xfunction;
297 if (pr_depth >= MAX_STACK_DEPTH)
298 Host_Error ("stack overflow");
300 // save off any locals that the new function steps on
302 if (localstack_used + c > LOCALSTACK_SIZE)
303 Host_Error ("PR_ExecuteProgram: locals stack overflow\n");
305 for (i=0 ; i < c ; i++)
306 localstack[localstack_used+i] = ((int *)pr_globals)[f->parm_start + i];
307 localstack_used += c;
311 for (i=0 ; i<f->numparms ; i++)
313 for (j=0 ; j<f->parm_size[i] ; j++)
315 ((int *)pr_globals)[o] = ((int *)pr_globals)[OFS_PARM0+i*3+j];
321 return f->first_statement - 1; // offset the s++
329 int PR_LeaveFunction (void)
334 Host_Error ("prog stack underflow");
337 Host_Error ("PR_LeaveFunction: NULL function\n");
338 // restore locals from the stack
339 c = pr_xfunction->locals;
340 localstack_used -= c;
341 if (localstack_used < 0)
342 Host_Error ("PR_ExecuteProgram: locals stack underflow\n");
344 for (i=0 ; i < c ; i++)
345 ((int *)pr_globals)[pr_xfunction->parm_start + i] = localstack[localstack_used+i];
349 pr_xfunction = pr_stack[pr_depth].f;
350 return pr_stack[pr_depth].s;
353 void PR_ReInitStrings (void);
354 void PR_Execute_ProgsLoaded(void)
359 // reset the string table
368 // LordHavoc: optimized
369 #define OPA ((eval_t *)&pr_globals[(unsigned short) st->a])
370 #define OPB ((eval_t *)&pr_globals[(unsigned short) st->b])
371 #define OPC ((eval_t *)&pr_globals[(unsigned short) st->c])
372 extern cvar_t pr_boundscheck;
373 extern cvar_t pr_traceqc;
374 void PR_ExecuteProgram (func_t fnum, const char *errormessage)
377 mfunction_t *f, *newf;
380 int profile, startprofile, cachedpr_trace, exitdepth;
382 if (!fnum || fnum >= progs->numfunctions)
384 if (pr_global_struct->self)
385 ED_Print (PROG_TO_EDICT(pr_global_struct->self));
386 Host_Error ("PR_ExecuteProgram: %s", errormessage);
389 f = &pr_functions[fnum];
391 pr_trace = pr_traceqc.integer;
393 // we know we're done when pr_depth drops to this
394 exitdepth = pr_depth;
396 // make a stack frame
397 st = &pr_statements[PR_EnterFunction (f)];
398 startprofile = profile = 0;
401 cachedpr_trace = pr_trace;
402 if (pr_boundscheck.integer)
404 #define PRBOUNDSCHECK 1
408 #include "pr_execprogram.h"
413 #include "pr_execprogram.h"
422 #include "pr_execprogram.h"
427 #include "pr_execprogram.h"
432 void PR_ReInitStrings (void)