]> icculus.org git repositories - divverent/darkplaces.git/blob - prvm_exec.c
Added a query queue (test commit so Willis can test it).
[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 #include "progsvm.h"
23
24 char *prvm_opnames[] =
25 {
26 "DONE",
27
28 "MUL_F",
29 "MUL_V",
30 "MUL_FV",
31 "MUL_VF",
32
33 "DIV",
34
35 "ADD_F",
36 "ADD_V",
37
38 "SUB_F",
39 "SUB_V",
40
41 "EQ_F",
42 "EQ_V",
43 "EQ_S",
44 "EQ_E",
45 "EQ_FNC",
46
47 "NE_F",
48 "NE_V",
49 "NE_S",
50 "NE_E",
51 "NE_FNC",
52
53 "LE",
54 "GE",
55 "LT",
56 "GT",
57
58 "INDIRECT",
59 "INDIRECT",
60 "INDIRECT",
61 "INDIRECT",
62 "INDIRECT",
63 "INDIRECT",
64
65 "ADDRESS",
66
67 "STORE_F",
68 "STORE_V",
69 "STORE_S",
70 "STORE_ENT",
71 "STORE_FLD",
72 "STORE_FNC",
73
74 "STOREP_F",
75 "STOREP_V",
76 "STOREP_S",
77 "STOREP_ENT",
78 "STOREP_FLD",
79 "STOREP_FNC",
80
81 "RETURN",
82
83 "NOT_F",
84 "NOT_V",
85 "NOT_S",
86 "NOT_ENT",
87 "NOT_FNC",
88
89 "IF",
90 "IFNOT",
91
92 "CALL0",
93 "CALL1",
94 "CALL2",
95 "CALL3",
96 "CALL4",
97 "CALL5",
98 "CALL6",
99 "CALL7",
100 "CALL8",
101
102 "STATE",
103
104 "GOTO",
105
106 "AND",
107 "OR",
108
109 "BITAND",
110 "BITOR"
111 };
112
113 char *PRVM_GlobalString (int ofs);
114 char *PRVM_GlobalStringNoContents (int ofs);
115
116
117 //=============================================================================
118
119 /*
120 =================
121 PRVM_PrintStatement
122 =================
123 */
124 void PRVM_PrintStatement (dstatement_t *s)
125 {
126         int             i;
127
128         if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
129         {
130                 Con_Printf("%s ",  prvm_opnames[s->op]);
131                 i = strlen(prvm_opnames[s->op]);
132                 for ( ; i<10 ; i++)
133                         Con_Print(" ");
134         }
135
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)
139         {
140                 Con_Printf("branch %i",s->a);
141         }
142         else if ( (unsigned)(s->op - OP_STORE_F) < 6)
143         {
144                 Con_Print(PRVM_GlobalString((unsigned short) s->a));
145                 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
146         }
147         else
148         {
149                 if (s->a)
150                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
151                 if (s->b)
152                         Con_Print(PRVM_GlobalString((unsigned short) s->b));
153                 if (s->c)
154                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
155         }
156         Con_Print("\n");
157 }
158
159 /*
160 ============
161 PRVM_StackTrace
162 ============
163 */
164 void PRVM_StackTrace (void)
165 {
166         mfunction_t     *f;
167         int                     i;
168
169         prog->stack[prog->depth].s = prog->xstatement;
170         prog->stack[prog->depth].f = prog->xfunction;
171         for (i = prog->depth;i > 0;i--)
172         {
173                 f = prog->stack[i].f;
174
175                 if (!f)
176                         Con_Print("<NULL FUNCTION>\n");
177                 else
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);
179         }
180 }
181
182
183 /*
184 ============
185 PRVM_Profile_f
186
187 ============
188 */
189 void PRVM_Profile_f (void)
190 {
191         mfunction_t *f, *best;
192         int i, num, max/*, howmany*/;
193
194         //howmany = 10;
195         //if (Cmd_Argc() == 2)
196         //      howmany = atoi(Cmd_Argv(1));
197         if(Cmd_Argc() != 2)
198         {
199                 Con_Print("prvm_profile <program name>\n");
200                 return;
201         }
202         
203         PRVM_Begin;
204         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
205                 return;
206
207         Con_Printf( "%s Profile:\n[Profile] [BuiltinProfile] [CallCount]\n", PRVM_NAME );
208
209         num = 0;
210         do
211         {
212                 max = 0;
213                 best = NULL;
214                 for (i=0 ; i<prog->progs->numfunctions ; i++)
215                 {
216                         f = &prog->functions[i];
217                         if (f->profile > max)
218                         {
219                                 max = f->profile;
220                                 best = f;
221                         }
222                 }
223                 if (best)
224                 {
225                         //if (num < howmany)
226                                 Con_Printf("%7i %7i %7i %s\n", best->profile, best->builtinsprofile, best->callcount, PRVM_GetString(best->s_name));
227                         num++;
228                         best->profile = 0;
229                         best->builtinsprofile = 0;
230                 }
231         } while (best);
232
233         PRVM_End;
234 }
235
236 void PRVM_CrashAll()
237 {
238         int i;
239         prvm_prog_t *oldprog = prog;
240
241         for(i = 0; i < PRVM_MAXPROGS; i++)
242         {
243                 if(!PRVM_ProgLoaded(i))
244                         continue;
245                 PRVM_SetProg(i);
246                 PRVM_Crash();
247         }
248         
249         prog = oldprog;
250 }
251
252 void PRVM_PrintState(void)
253 {
254         int i;
255         if (prog->xfunction)
256         {
257                 for (i = -7; i <= 0;i++)
258                         if (prog->xstatement + i >= prog->xfunction->first_statement)
259                                 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
260         }
261         else
262                 Con_Print("null function executing??\n");
263         PRVM_StackTrace ();
264 }
265
266 void PRVM_Crash()
267 {
268         if( prog->depth > 0 )
269         {
270                 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
271                 PRVM_PrintState();
272         }
273
274         // dump the stack so host_error can shutdown functions
275         prog->depth = 0;
276         prog->localstack_used = 0;
277
278         // reset the prog pointer
279         prog = NULL;
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.generic)[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.generic)[o] = ((int *)prog->globals.generic)[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.generic)[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.generic[(unsigned short) st->a])
379 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
380 #define OPC ((prvm_eval_t *)&prog->globals.generic[(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 >= (unsigned int)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 ("PRVM_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 }