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 // stacktrace writes into pr_stack[MAX_STACK_DEPTH]
32 // thus increase the array, so depth wont be overwritten
33 prstack_t pr_stack[MAX_STACK_DEPTH+1];
36 #define LOCALSTACK_SIZE 2048
37 int localstack[LOCALSTACK_SIZE];
42 mfunction_t *pr_xfunction;
137 char *PR_GlobalString (int ofs);
138 char *PR_GlobalStringNoContents (int ofs);
141 //=============================================================================
148 void PR_PrintStatement (dstatement_t *s)
152 if ( (unsigned)s->op < sizeof(pr_opnames)/sizeof(pr_opnames[0]))
154 Con_Printf ("%s ", pr_opnames[s->op]);
155 i = strlen(pr_opnames[s->op]);
160 if (s->op == OP_IF || s->op == OP_IFNOT)
161 Con_Printf ("%sbranch %i",PR_GlobalString((unsigned short) s->a),s->b);
162 else if (s->op == OP_GOTO)
164 Con_Printf ("branch %i",s->a);
166 else if ( (unsigned)(s->op - OP_STORE_F) < 6)
168 Con_Printf ("%s", PR_GlobalString((unsigned short) s->a));
169 Con_Printf ("%s", PR_GlobalStringNoContents((unsigned short) s->b));
174 Con_Printf ("%s", PR_GlobalString((unsigned short) s->a));
176 Con_Printf ("%s", PR_GlobalString((unsigned short) s->b));
178 Con_Printf ("%s", PR_GlobalStringNoContents((unsigned short) s->c));
188 void PR_StackTrace (void)
193 pr_stack[pr_depth].s = pr_xstatement;
194 pr_stack[pr_depth].f = pr_xfunction;
195 for (i = pr_depth;i > 0;i--)
200 Con_Printf ("<NULL FUNCTION>\n");
202 Con_Printf ("%12s : %s : statement %i\n", PR_GetString(f->s_file), PR_GetString(f->s_name), pr_stack[i].s - f->first_statement);
213 void PR_Profile_f (void)
215 mfunction_t *f, *best;
216 int i, num, max/*, howmany*/;
219 //if (Cmd_Argc() == 2)
220 // howmany = atoi(Cmd_Argv(1));
226 for (i=0 ; i<progs->numfunctions ; i++)
228 f = &pr_functions[i];
229 if (f->profile > max)
238 Con_Printf ("%7i %7i %s\n", best->profile, best->builtinsprofile, PR_GetString(best->s_name));
241 best->builtinsprofile = 0;
252 // kill the stack just to be sure
258 Con_Printf("QuakeC crash report:\n");
261 for (i = -4;i <= 0;i++)
262 if (pr_xstatement + i >= pr_xfunction->first_statement)
263 PR_PrintStatement (pr_statements + pr_xstatement + i);
266 Con_Printf("null function executing??\n");
269 // dump the stack so host_error can shutdown functions
275 ============================================================================
278 The interpretation main loop
279 ============================================================================
286 Returns the new program statement counter
289 int PR_EnterFunction (mfunction_t *f)
294 Host_Error ("PR_EnterFunction: NULL function\n");
296 pr_stack[pr_depth].s = pr_xstatement;
297 pr_stack[pr_depth].f = pr_xfunction;
299 if (pr_depth >= MAX_STACK_DEPTH)
300 Host_Error ("stack overflow");
302 // save off any locals that the new function steps on
304 if (localstack_used + c > LOCALSTACK_SIZE)
305 Host_Error ("PR_ExecuteProgram: locals stack overflow\n");
307 for (i=0 ; i < c ; i++)
308 localstack[localstack_used+i] = ((int *)pr_globals)[f->parm_start + i];
309 localstack_used += c;
313 for (i=0 ; i<f->numparms ; i++)
315 for (j=0 ; j<f->parm_size[i] ; j++)
317 ((int *)pr_globals)[o] = ((int *)pr_globals)[OFS_PARM0+i*3+j];
323 return f->first_statement - 1; // offset the s++
331 int PR_LeaveFunction (void)
336 Host_Error ("prog stack underflow");
339 Host_Error ("PR_LeaveFunction: NULL function\n");
340 // restore locals from the stack
341 c = pr_xfunction->locals;
342 localstack_used -= c;
343 if (localstack_used < 0)
344 Host_Error ("PR_ExecuteProgram: locals stack underflow\n");
346 for (i=0 ; i < c ; i++)
347 ((int *)pr_globals)[pr_xfunction->parm_start + i] = localstack[localstack_used+i];
351 pr_xfunction = pr_stack[pr_depth].f;
352 return pr_stack[pr_depth].s;
355 void PR_ReInitStrings (void);
356 void PR_Execute_ProgsLoaded(void)
361 // reset the string table
370 // LordHavoc: optimized
371 #define OPA ((eval_t *)&pr_globals[(unsigned short) st->a])
372 #define OPB ((eval_t *)&pr_globals[(unsigned short) st->b])
373 #define OPC ((eval_t *)&pr_globals[(unsigned short) st->c])
374 extern cvar_t pr_boundscheck;
375 extern cvar_t pr_traceqc;
376 void PR_ExecuteProgram (func_t fnum, const char *errormessage)
379 mfunction_t *f, *newf;
382 int profile, startprofile, cachedpr_trace, exitdepth;
384 if (!fnum || fnum >= progs->numfunctions)
386 if (pr_global_struct->self)
387 ED_Print (PROG_TO_EDICT(pr_global_struct->self));
388 Host_Error ("PR_ExecuteProgram: %s", errormessage);
391 f = &pr_functions[fnum];
393 pr_trace = pr_traceqc.integer;
395 // we know we're done when pr_depth drops to this
396 exitdepth = pr_depth;
398 // make a stack frame
399 st = &pr_statements[PR_EnterFunction (f)];
400 startprofile = profile = 0;
403 cachedpr_trace = pr_trace;
404 if (pr_boundscheck.integer)
406 #define PRBOUNDSCHECK 1
410 #include "pr_execprogram.h"
415 #include "pr_execprogram.h"
424 #include "pr_execprogram.h"
429 #include "pr_execprogram.h"
434 void PR_ReInitStrings (void)