]> icculus.org git repositories - divverent/darkplaces.git/blob - prvm_exec.c
Another MSVC error fixed
[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         size_t i;
127
128         if( prog->statement_linenums ) {
129                 int opnum;
130
131                 opnum = s - prog->statements;
132                 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
133         }
134
135         if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
136         {
137                 Con_Printf("%s ",  prvm_opnames[s->op]);
138                 i = strlen(prvm_opnames[s->op]);
139                 for ( ; i<10 ; i++)
140                         Con_Print(" ");
141         }
142
143         if (s->op == OP_IF || s->op == OP_IFNOT)
144                 Con_Printf("%sbranch %i",PRVM_GlobalString((unsigned short) s->a),s->b);
145         else if (s->op == OP_GOTO)
146         {
147                 Con_Printf("branch %i",s->a);
148         }
149         else if ( (unsigned)(s->op - OP_STORE_F) < 6)
150         {
151                 Con_Print(PRVM_GlobalString((unsigned short) s->a));
152                 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
153         }
154         else
155         {
156                 if (s->a)
157                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
158                 if (s->b)
159                         Con_Print(PRVM_GlobalString((unsigned short) s->b));
160                 if (s->c)
161                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
162         }
163         Con_Print("\n");
164 }
165
166 /*
167 ============
168 PRVM_StackTrace
169 ============
170 */
171 void PRVM_StackTrace (void)
172 {
173         mfunction_t     *f;
174         int                     i;
175
176         prog->stack[prog->depth].s = prog->xstatement;
177         prog->stack[prog->depth].f = prog->xfunction;
178         for (i = prog->depth;i > 0;i--)
179         {
180                 f = prog->stack[i].f;
181
182                 if (!f)
183                         Con_Print("<NULL FUNCTION>\n");
184                 else
185                         Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
186         }
187 }
188
189
190 /*
191 ============
192 PRVM_Profile_f
193
194 ============
195 */
196 void PRVM_Profile_f (void)
197 {
198         mfunction_t *f, *best;
199         int i, num, max/*, howmany*/;
200
201         //howmany = 10;
202         //if (Cmd_Argc() == 2)
203         //      howmany = atoi(Cmd_Argv(1));
204         if(Cmd_Argc() != 2)
205         {
206                 Con_Print("prvm_profile <program name>\n");
207                 return;
208         }
209
210         PRVM_Begin;
211         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
212                 return;
213
214         Con_Printf( "%s Profile:\n[Profile] [BuiltinProfile] [CallCount]\n", PRVM_NAME );
215
216         num = 0;
217         do
218         {
219                 max = 0;
220                 best = NULL;
221                 for (i=0 ; i<prog->progs->numfunctions ; i++)
222                 {
223                         f = &prog->functions[i];
224                         if (f->profile > max)
225                         {
226                                 max = f->profile;
227                                 best = f;
228                         }
229                 }
230                 if (best)
231                 {
232                         //if (num < howmany)
233                                 Con_Printf("%7i %7i %7i %s\n", best->profile, best->builtinsprofile, best->callcount, PRVM_GetString(best->s_name));
234                         num++;
235                         best->profile = 0;
236                         best->builtinsprofile = 0;
237                         best->callcount = 0;
238                 }
239         } while (best);
240
241         PRVM_End;
242 }
243
244 void PRVM_CrashAll()
245 {
246         int i;
247         prvm_prog_t *oldprog = prog;
248
249         for(i = 0; i < PRVM_MAXPROGS; i++)
250         {
251                 if(!PRVM_ProgLoaded(i))
252                         continue;
253                 PRVM_SetProg(i);
254                 PRVM_Crash();
255         }
256
257         prog = oldprog;
258 }
259
260 void PRVM_PrintState(void)
261 {
262         int i;
263         if (prog->xfunction)
264         {
265                 for (i = -7; i <= 0;i++)
266                         if (prog->xstatement + i >= prog->xfunction->first_statement)
267                                 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
268         }
269         else
270                 Con_Print("null function executing??\n");
271         PRVM_StackTrace ();
272 }
273
274 void PRVM_Crash()
275 {
276         if (prog == NULL)
277                 return;
278
279         if( prog->depth > 0 )
280         {
281                 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
282                 PRVM_PrintState();
283         }
284
285         // dump the stack so host_error can shutdown functions
286         prog->depth = 0;
287         prog->localstack_used = 0;
288
289         // reset the prog pointer
290         prog = NULL;
291 }
292
293 /*
294 ============================================================================
295 PRVM_ExecuteProgram
296
297 The interpretation main loop
298 ============================================================================
299 */
300
301 /*
302 ====================
303 PRVM_EnterFunction
304
305 Returns the new program statement counter
306 ====================
307 */
308 int PRVM_EnterFunction (mfunction_t *f)
309 {
310         int             i, j, c, o;
311
312         if (!f)
313                 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s\n", PRVM_NAME);
314
315         prog->stack[prog->depth].s = prog->xstatement;
316         prog->stack[prog->depth].f = prog->xfunction;
317         prog->depth++;
318         if (prog->depth >=PRVM_MAX_STACK_DEPTH)
319                 PRVM_ERROR ("stack overflow");
320
321 // save off any locals that the new function steps on
322         c = f->locals;
323         if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
324                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s\n", PRVM_NAME);
325
326         for (i=0 ; i < c ; i++)
327                 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
328         prog->localstack_used += c;
329
330 // copy parameters
331         o = f->parm_start;
332         for (i=0 ; i<f->numparms ; i++)
333         {
334                 for (j=0 ; j<f->parm_size[i] ; j++)
335                 {
336                         ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
337                         o++;
338                 }
339         }
340
341         prog->xfunction = f;
342         return f->first_statement - 1;  // offset the s++
343 }
344
345 /*
346 ====================
347 PRVM_LeaveFunction
348 ====================
349 */
350 int PRVM_LeaveFunction (void)
351 {
352         int             i, c;
353
354         if (prog->depth <= 0)
355                 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
356
357         if (!prog->xfunction)
358                 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s\n", PRVM_NAME);
359 // restore locals from the stack
360         c = prog->xfunction->locals;
361         prog->localstack_used -= c;
362         if (prog->localstack_used < 0)
363                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s\n", PRVM_NAME);
364
365         for (i=0 ; i < c ; i++)
366                 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
367
368 // up stack
369         prog->depth--;
370         prog->xfunction = prog->stack[prog->depth].f;
371         return prog->stack[prog->depth].s;
372 }
373
374 void PRVM_Init_Exec(void)
375 {
376         // dump the stack
377         prog->depth = 0;
378         prog->localstack_used = 0;
379         // reset the string table
380         // nothing here yet
381 }
382
383 /*
384 ====================
385 PRVM_ExecuteProgram
386 ====================
387 */
388 // LordHavoc: optimized
389 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
390 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
391 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
392 extern cvar_t prvm_boundscheck;
393 extern cvar_t prvm_traceqc;
394 extern int              PRVM_ED_FindFieldOffset (const char *field);
395 extern ddef_t*  PRVM_ED_FindGlobal(const char *name);
396 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
397 {
398         dstatement_t    *st;
399         mfunction_t     *f, *newf;
400         prvm_edict_t    *ed;
401         prvm_eval_t     *ptr;
402         int             profile, startprofile, cachedpr_trace, exitdepth;
403
404         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
405         {
406                 if (prog->self && PRVM_G_INT(prog->self->ofs))
407                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_G_INT(prog->self->ofs)));
408                 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
409         }
410
411         f = &prog->functions[fnum];
412
413         prog->trace = prvm_traceqc.integer;
414
415         // we know we're done when pr_depth drops to this
416         exitdepth = prog->depth;
417
418 // make a stack frame
419         st = &prog->statements[PRVM_EnterFunction (f)];
420         startprofile = profile = 0;
421
422 chooseexecprogram:
423         cachedpr_trace = prog->trace;
424         if (prvm_boundscheck.integer)
425         {
426 #define PRVMBOUNDSCHECK 1
427                 if (prog->trace)
428                 {
429 #define PRVMTRACE 1
430 #include "prvm_execprogram.h"
431                 }
432                 else
433                 {
434 #undef PRVMTRACE
435 #include "prvm_execprogram.h"
436                 }
437         }
438         else
439         {
440 #undef PRVMBOUNDSCHECK
441                 if (prog->trace)
442                 {
443 #define PRVMTRACE 1
444 #include "prvm_execprogram.h"
445                 }
446                 else
447                 {
448 #undef PRVMTRACE
449 #include "prvm_execprogram.h"
450                 }
451         }
452 }