]> icculus.org git repositories - divverent/darkplaces.git/blob - prvm_exec.c
renamed q3mface_t to q3msurface_t
[divverent/darkplaces.git] / prvm_exec.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20
21 #include "quakedef.h"
22
23 char *prvm_opnames[] =
24 {
25 "DONE",
26
27 "MUL_F",
28 "MUL_V",
29 "MUL_FV",
30 "MUL_VF",
31
32 "DIV",
33
34 "ADD_F",
35 "ADD_V",
36
37 "SUB_F",
38 "SUB_V",
39
40 "EQ_F",
41 "EQ_V",
42 "EQ_S",
43 "EQ_E",
44 "EQ_FNC",
45
46 "NE_F",
47 "NE_V",
48 "NE_S",
49 "NE_E",
50 "NE_FNC",
51
52 "LE",
53 "GE",
54 "LT",
55 "GT",
56
57 "INDIRECT",
58 "INDIRECT",
59 "INDIRECT",
60 "INDIRECT",
61 "INDIRECT",
62 "INDIRECT",
63
64 "ADDRESS",
65
66 "STORE_F",
67 "STORE_V",
68 "STORE_S",
69 "STORE_ENT",
70 "STORE_FLD",
71 "STORE_FNC",
72
73 "STOREP_F",
74 "STOREP_V",
75 "STOREP_S",
76 "STOREP_ENT",
77 "STOREP_FLD",
78 "STOREP_FNC",
79
80 "RETURN",
81
82 "NOT_F",
83 "NOT_V",
84 "NOT_S",
85 "NOT_ENT",
86 "NOT_FNC",
87
88 "IF",
89 "IFNOT",
90
91 "CALL0",
92 "CALL1",
93 "CALL2",
94 "CALL3",
95 "CALL4",
96 "CALL5",
97 "CALL6",
98 "CALL7",
99 "CALL8",
100
101 "STATE",
102
103 "GOTO",
104
105 "AND",
106 "OR",
107
108 "BITAND",
109 "BITOR"
110 };
111
112 char *PRVM_GlobalString (int ofs);
113 char *PRVM_GlobalStringNoContents (int ofs);
114
115
116 //=============================================================================
117
118 /*
119 =================
120 PRVM_PrintStatement
121 =================
122 */
123 void PRVM_PrintStatement (dstatement_t *s)
124 {
125         int             i;
126
127         if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
128         {
129                 Con_Printf("%s ",  prvm_opnames[s->op]);
130                 i = strlen(prvm_opnames[s->op]);
131                 for ( ; i<10 ; i++)
132                         Con_Print(" ");
133         }
134
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)
138         {
139                 Con_Printf("branch %i",s->a);
140         }
141         else if ( (unsigned)(s->op - OP_STORE_F) < 6)
142         {
143                 Con_Print(PRVM_GlobalString((unsigned short) s->a));
144                 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
145         }
146         else
147         {
148                 if (s->a)
149                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
150                 if (s->b)
151                         Con_Print(PRVM_GlobalString((unsigned short) s->b));
152                 if (s->c)
153                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
154         }
155         Con_Print("\n");
156 }
157
158 /*
159 ============
160 PRVM_StackTrace
161 ============
162 */
163 void PRVM_StackTrace (void)
164 {
165         mfunction_t     *f;
166         int                     i;
167
168         prog->stack[prog->depth].s = prog->xstatement;
169         prog->stack[prog->depth].f = prog->xfunction;
170         for (i = prog->depth;i > 0;i--)
171         {
172                 f = prog->stack[i].f;
173
174                 if (!f)
175                         Con_Print("<NULL FUNCTION>\n");
176                 else
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);
178         }
179 }
180
181
182 /*
183 ============
184 PRVM_Profile_f
185
186 ============
187 */
188 void PRVM_Profile_f (void)
189 {
190         mfunction_t *f, *best;
191         int i, num, max/*, howmany*/;
192
193         //howmany = 10;
194         //if (Cmd_Argc() == 2)
195         //      howmany = atoi(Cmd_Argv(1));
196         if(Cmd_Argc() != 2)
197         {
198                 Con_Print("prvm_profile <program name>\n");
199                 return;
200         }
201
202         PRVM_Begin;
203         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
204                 return;
205
206         num = 0;
207         do
208         {
209                 max = 0;
210                 best = NULL;
211                 for (i=0 ; i<prog->progs->numfunctions ; i++)
212                 {
213                         f = &prog->functions[i];
214                         if (f->profile > max)
215                         {
216                                 max = f->profile;
217                                 best = f;
218                         }
219                 }
220                 if (best)
221                 {
222                         //if (num < howmany)
223                                 Con_Printf("%s: %7i %7i %s\n", PRVM_NAME, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
224                         num++;
225                         best->profile = 0;
226                         best->builtinsprofile = 0;
227                 }
228         } while (best);
229
230         PRVM_End;
231 }
232
233 void PRVM_CrashAll()
234 {
235         int i;
236         prvm_prog_t *oldprog = prog;
237
238         for(i = 0; i < PRVM_MAXPROGS; i++)
239         {
240                 if(!PRVM_ProgLoaded(i))
241                         continue;
242                 PRVM_SetProg(i);
243                 PRVM_Crash();
244         }
245         
246         prog = oldprog;
247 }
248
249 void PRVM_PrintState(void)
250 {
251         int i;
252         if (prog->xfunction)
253         {
254                 for (i = -4;i <= 0;i++)
255                         if (prog->xstatement + i >= prog->xfunction->first_statement)
256                                 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
257         }
258         else
259                 Con_Print("null function executing??\n");
260         PRVM_StackTrace ();
261 }
262
263 void PRVM_Crash()
264 {
265         if (prog->depth < 1)
266         {
267                 // kill the stack just to be sure
268                 prog->depth = 0;
269                 prog->localstack_used = 0;
270                 return;
271         }
272
273         Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
274         PRVM_PrintState();
275
276         // dump the stack so host_error can shutdown functions
277         prog->depth = 0;
278         prog->localstack_used = 0;
279
280 }
281
282 /*
283 ============================================================================
284 PRVM_ExecuteProgram
285
286 The interpretation main loop
287 ============================================================================
288 */
289
290 /*
291 ====================
292 PRVM_EnterFunction
293
294 Returns the new program statement counter
295 ====================
296 */
297 int PRVM_EnterFunction (mfunction_t *f)
298 {
299         int             i, j, c, o;
300
301         if (!f)
302                 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s\n", PRVM_NAME);
303
304         prog->stack[prog->depth].s = prog->xstatement;
305         prog->stack[prog->depth].f = prog->xfunction;
306         prog->depth++;
307         if (prog->depth >=PRVM_MAX_STACK_DEPTH)
308                 PRVM_ERROR ("stack overflow");
309
310 // save off any locals that the new function steps on
311         c = f->locals;
312         if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
313                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s\n", PRVM_NAME);
314
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;
318
319 // copy parameters
320         o = f->parm_start;
321         for (i=0 ; i<f->numparms ; i++)
322         {
323                 for (j=0 ; j<f->parm_size[i] ; j++)
324                 {
325                         ((int *)prog->globals)[o] = ((int *)prog->globals)[OFS_PARM0+i*3+j];
326                         o++;
327                 }
328         }
329
330         prog->xfunction = f;
331         return f->first_statement - 1;  // offset the s++
332 }
333
334 /*
335 ====================
336 PRVM_LeaveFunction
337 ====================
338 */
339 int PRVM_LeaveFunction (void)
340 {
341         int             i, c;
342
343         if (prog->depth <= 0)
344                 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
345
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);
353
354         for (i=0 ; i < c ; i++)
355                 ((int *)prog->globals)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
356
357 // up stack
358         prog->depth--;
359         prog->xfunction = prog->stack[prog->depth].f;
360         return prog->stack[prog->depth].s;
361 }
362
363 void PRVM_Init_Exec(void)
364 {
365         // dump the stack
366         prog->depth = 0;
367         prog->localstack_used = 0;
368         // reset the string table
369         // nothing here yet
370 }
371
372 /*
373 ====================
374 PRVM_ExecuteProgram
375 ====================
376 */
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)
386 {
387         dstatement_t    *st;
388         mfunction_t     *f, *newf;
389         prvm_edict_t    *ed;
390         prvm_eval_t     *ptr;
391         int             profile, startprofile, cachedpr_trace, exitdepth;
392
393         if (!fnum || fnum >= prog->progs->numfunctions)
394         {
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);
398         }
399
400         f = &prog->functions[fnum];
401
402         prog->trace = prvm_traceqc.integer;
403
404         // we know we're done when pr_depth drops to this
405         exitdepth = prog->depth;
406
407 // make a stack frame
408         st = &prog->statements[PRVM_EnterFunction (f)];
409         startprofile = profile = 0;
410
411 chooseexecprogram:
412         cachedpr_trace = prog->trace;
413         if (prvm_boundscheck.integer)
414         {
415 #define PRVMBOUNDSCHECK 1
416                 if (prog->trace)
417                 {
418 #define PRVMTRACE 1
419 #include "prvm_execprogram.h"
420                 }
421                 else
422                 {
423 #undef PRVMTRACE
424 #include "prvm_execprogram.h"
425                 }
426         }
427         else
428         {
429 #undef PRVMBOUNDSCHECK
430                 if (prog->trace)
431                 {
432 #define PRVMTRACE 1
433 #include "prvm_execprogram.h"
434                 }
435                 else
436                 {
437 #undef PRVMTRACE
438 #include "prvm_execprogram.h"
439                 }
440         }
441 }