]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - prvm_exec.c
patch from div0 to make universal-binary builds of darkplaces possible on Mac OS X
[xonotic/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 "^5DONE",
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 "^2EQ_F",
42 "^2EQ_V",
43 "^2EQ_S",
44 "^2EQ_E",
45 "^2EQ_FNC",
46
47 "^2NE_F",
48 "^2NE_V",
49 "^2NE_S",
50 "^2NE_E",
51 "^2NE_FNC",
52
53 "^2LE",
54 "^2GE",
55 "^2LT",
56 "^2GT",
57
58 "^6FIELD_F",
59 "^6FIELD_V",
60 "^6FIELD_S",
61 "^6FIELD_ENT",
62 "^6FIELD_FLD",
63 "^6FIELD_FNC",
64
65 "^1ADDRESS",
66
67 "STORE_F",
68 "STORE_V",
69 "STORE_S",
70 "STORE_ENT",
71 "STORE_FLD",
72 "STORE_FNC",
73
74 "^1STOREP_F",
75 "^1STOREP_V",
76 "^1STOREP_S",
77 "^1STOREP_ENT",
78 "^1STOREP_FLD",
79 "^1STOREP_FNC",
80
81 "^5RETURN",
82
83 "^2NOT_F",
84 "^2NOT_V",
85 "^2NOT_S",
86 "^2NOT_ENT",
87 "^2NOT_FNC",
88
89 "^5IF",
90 "^5IFNOT",
91
92 "^3CALL0",
93 "^3CALL1",
94 "^3CALL2",
95 "^3CALL3",
96 "^3CALL4",
97 "^3CALL5",
98 "^3CALL6",
99 "^3CALL7",
100 "^3CALL8",
101
102 "^1STATE",
103
104 "^5GOTO",
105
106 "^2AND",
107 "^2OR",
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 extern cvar_t prvm_statementprofiling;
125 void PRVM_PrintStatement (dstatement_t *s)
126 {
127         size_t i;
128         int opnum = (int)(s - prog->statements);
129
130         Con_Printf("s%i: ", opnum);
131         if( prog->statement_linenums )
132                 Con_Printf( "%s:%i: ", PRVM_GetString( prog->xfunction->s_file ), prog->statement_linenums[ opnum ] );
133
134         if (prvm_statementprofiling.integer)
135                 Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]);
136
137         if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]))
138         {
139                 Con_Printf("%s ",  prvm_opnames[s->op]);
140                 i = strlen(prvm_opnames[s->op]);
141                 // don't count a preceding color tag when padding the name
142                 if (prvm_opnames[s->op][0] == STRING_COLOR_TAG)
143                         i -= 2;
144                 for ( ; i<10 ; i++)
145                         Con_Print(" ");
146         }
147         if (s->op == OP_IF || s->op == OP_IFNOT)
148                 Con_Printf("%s, s%i",PRVM_GlobalString((unsigned short) s->a),(signed short)s->b + opnum);
149         else if (s->op == OP_GOTO)
150                 Con_Printf("s%i",(signed short)s->a + opnum);
151         else if ( (unsigned)(s->op - OP_STORE_F) < 6)
152         {
153                 Con_Print(PRVM_GlobalString((unsigned short) s->a));
154                 Con_Print(", ");
155                 Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
156         }
157         else if (s->op == OP_ADDRESS || (unsigned)(s->op - OP_LOAD_F) < 6)
158         {
159                 if (s->a)
160                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
161                 if (s->b)
162                 {
163                         Con_Print(", ");
164                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->b));
165                 }
166                 if (s->c)
167                 {
168                         Con_Print(", ");
169                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
170                 }
171         }
172         else
173         {
174                 if (s->a)
175                         Con_Print(PRVM_GlobalString((unsigned short) s->a));
176                 if (s->b)
177                 {
178                         Con_Print(", ");
179                         Con_Print(PRVM_GlobalString((unsigned short) s->b));
180                 }
181                 if (s->c)
182                 {
183                         Con_Print(", ");
184                         Con_Print(PRVM_GlobalStringNoContents((unsigned short) s->c));
185                 }
186         }
187         Con_Print("\n");
188 }
189
190 void PRVM_PrintFunctionStatements (const char *name)
191 {
192         int i, firststatement, endstatement;
193         mfunction_t *func;
194         func = PRVM_ED_FindFunction (name);
195         if (!func)
196         {
197                 Con_Printf("%s progs: no function named %s\n", PRVM_NAME, name);
198                 return;
199         }
200         firststatement = func->first_statement;
201         if (firststatement < 0)
202         {
203                 Con_Printf("%s progs: function %s is builtin #%i\n", PRVM_NAME, name, -firststatement);
204                 return;
205         }
206
207         // find the end statement
208         endstatement = prog->progs->numstatements;
209         for (i = 0;i < prog->progs->numfunctions;i++)
210                 if (endstatement > prog->functions[i].first_statement && firststatement < prog->functions[i].first_statement)
211                         endstatement = prog->functions[i].first_statement;
212
213         // now print the range of statements
214         Con_Printf("%s progs: disassembly of function %s (statements %i-%i):\n", PRVM_NAME, name, firststatement, endstatement);
215         for (i = firststatement;i < endstatement;i++)
216         {
217                 PRVM_PrintStatement(prog->statements + i);
218                 prog->statement_profile[i] = 0;
219         }
220 }
221
222 /*
223 ============
224 PRVM_PrintFunction_f
225
226 ============
227 */
228 void PRVM_PrintFunction_f (void)
229 {
230         if (Cmd_Argc() != 3)
231         {
232                 Con_Printf("usage: prvm_printfunction <program name> <function name>\n");
233                 return;
234         }
235
236         PRVM_Begin;
237         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
238                 return;
239
240         PRVM_PrintFunctionStatements(Cmd_Argv(2));
241
242         PRVM_End;
243 }
244
245 /*
246 ============
247 PRVM_StackTrace
248 ============
249 */
250 void PRVM_StackTrace (void)
251 {
252         mfunction_t     *f;
253         int                     i;
254
255         prog->stack[prog->depth].s = prog->xstatement;
256         prog->stack[prog->depth].f = prog->xfunction;
257         for (i = prog->depth;i > 0;i--)
258         {
259                 f = prog->stack[i].f;
260
261                 if (!f)
262                         Con_Print("<NULL FUNCTION>\n");
263                 else
264                         Con_Printf("%12s : %s : statement %i\n", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement);
265         }
266 }
267
268
269 /*
270 ============
271 PRVM_Profile_f
272
273 ============
274 */
275 void PRVM_Profile_f (void)
276 {
277         mfunction_t *f, *best;
278         int i, num, howmany;
279         double max;
280
281         howmany = 1<<30;
282         if (Cmd_Argc() == 3)
283                 howmany = atoi(Cmd_Argv(2));
284         else if (Cmd_Argc() != 2)
285         {
286                 Con_Print("prvm_profile <program name>\n");
287                 return;
288         }
289
290         PRVM_Begin;
291         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
292                 return;
293
294         Con_Printf( "%s Profile:\n[CallCount] [Statements] [BuiltinCost]\n", PRVM_NAME );
295
296         num = 0;
297         do
298         {
299                 max = 0;
300                 best = NULL;
301                 for (i=0 ; i<prog->progs->numfunctions ; i++)
302                 {
303                         f = &prog->functions[i];
304                         if (max < f->profile + f->builtinsprofile + f->callcount)
305                         {
306                                 max = f->profile + f->builtinsprofile + f->callcount;
307                                 best = f;
308                         }
309                 }
310                 if (best)
311                 {
312                         if (num < howmany)
313                         {
314                                 if (best->first_statement < 0)
315                                         Con_Printf("%9.0f ----- builtin ----- %s\n", best->callcount, PRVM_GetString(best->s_name));
316                                 else
317                                         Con_Printf("%9.0f %9.0f %9.0f %s\n", best->callcount, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
318                         }
319                         num++;
320                         best->profile = 0;
321                         best->builtinsprofile = 0;
322                         best->callcount = 0;
323                 }
324         } while (best);
325
326         PRVM_End;
327 }
328
329 void PRVM_CrashAll()
330 {
331         int i;
332         prvm_prog_t *oldprog = prog;
333
334         for(i = 0; i < PRVM_MAXPROGS; i++)
335         {
336                 if(!PRVM_ProgLoaded(i))
337                         continue;
338                 PRVM_SetProg(i);
339                 PRVM_Crash();
340         }
341
342         prog = oldprog;
343 }
344
345 void PRVM_PrintState(void)
346 {
347         int i;
348         if (prog->xfunction)
349         {
350                 for (i = -7; i <= 0;i++)
351                         if (prog->xstatement + i >= prog->xfunction->first_statement)
352                                 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
353         }
354         else
355                 Con_Print("null function executing??\n");
356         PRVM_StackTrace ();
357 }
358
359 void PRVM_Crash()
360 {
361         if (prog == NULL)
362                 return;
363
364         if( prog->depth > 0 )
365         {
366                 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
367                 PRVM_PrintState();
368         }
369
370         // dump the stack so host_error can shutdown functions
371         prog->depth = 0;
372         prog->localstack_used = 0;
373
374         // reset the prog pointer
375         prog = NULL;
376 }
377
378 /*
379 ============================================================================
380 PRVM_ExecuteProgram
381
382 The interpretation main loop
383 ============================================================================
384 */
385
386 /*
387 ====================
388 PRVM_EnterFunction
389
390 Returns the new program statement counter
391 ====================
392 */
393 int PRVM_EnterFunction (mfunction_t *f)
394 {
395         int             i, j, c, o;
396
397         if (!f)
398                 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
399
400         prog->stack[prog->depth].s = prog->xstatement;
401         prog->stack[prog->depth].f = prog->xfunction;
402         prog->depth++;
403         if (prog->depth >=PRVM_MAX_STACK_DEPTH)
404                 PRVM_ERROR ("stack overflow");
405
406 // save off any locals that the new function steps on
407         c = f->locals;
408         if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
409                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
410
411         for (i=0 ; i < c ; i++)
412                 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
413         prog->localstack_used += c;
414
415 // copy parameters
416         o = f->parm_start;
417         for (i=0 ; i<f->numparms ; i++)
418         {
419                 for (j=0 ; j<f->parm_size[i] ; j++)
420                 {
421                         ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
422                         o++;
423                 }
424         }
425
426         prog->xfunction = f;
427         return f->first_statement - 1;  // offset the s++
428 }
429
430 /*
431 ====================
432 PRVM_LeaveFunction
433 ====================
434 */
435 int PRVM_LeaveFunction (void)
436 {
437         int             i, c;
438
439         if (prog->depth <= 0)
440                 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
441
442         if (!prog->xfunction)
443                 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
444 // restore locals from the stack
445         c = prog->xfunction->locals;
446         prog->localstack_used -= c;
447         if (prog->localstack_used < 0)
448                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
449
450         for (i=0 ; i < c ; i++)
451                 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
452
453 // up stack
454         prog->depth--;
455         prog->xfunction = prog->stack[prog->depth].f;
456         return prog->stack[prog->depth].s;
457 }
458
459 void PRVM_Init_Exec(void)
460 {
461         // dump the stack
462         prog->depth = 0;
463         prog->localstack_used = 0;
464         // reset the string table
465         // nothing here yet
466 }
467
468 /*
469 ====================
470 PRVM_ExecuteProgram
471 ====================
472 */
473 // LordHavoc: optimized
474 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
475 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
476 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
477 extern cvar_t prvm_boundscheck;
478 extern cvar_t prvm_traceqc;
479 extern cvar_t prvm_statementprofiling;
480 extern int              PRVM_ED_FindFieldOffset (const char *field);
481 extern ddef_t*  PRVM_ED_FindGlobal(const char *name);
482 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
483 {
484         dstatement_t    *st, *startst;
485         mfunction_t     *f, *newf;
486         prvm_edict_t    *ed;
487         prvm_eval_t     *ptr;
488         int             jumpcount, cachedpr_trace, exitdepth;
489
490         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
491         {
492                 if (prog->self && PRVM_G_INT(prog->self->ofs))
493                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_G_INT(prog->self->ofs)));
494                 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
495         }
496
497         f = &prog->functions[fnum];
498
499         prog->trace = prvm_traceqc.integer;
500
501         // we know we're done when pr_depth drops to this
502         exitdepth = prog->depth;
503
504 // make a stack frame
505         st = &prog->statements[PRVM_EnterFunction (f)];
506         // save the starting statement pointer for profiling
507         // (when the function exits or jumps, the (st - startst) integer value is
508         // added to the function's profile counter)
509         startst = st;
510         // instead of counting instructions, we count jumps
511         jumpcount = 0;
512         // add one to the callcount of this function because otherwise engine-called functions aren't counted
513         prog->xfunction->callcount++;
514
515 chooseexecprogram:
516         cachedpr_trace = prog->trace;
517         if (prvm_statementprofiling.integer)
518         {
519 #define PRVMSTATEMENTPROFILING 1
520                 if (prvm_boundscheck.integer)
521                 {
522 #define PRVMBOUNDSCHECK 1
523                         if (prog->trace)
524                         {
525 #define PRVMTRACE 1
526 #include "prvm_execprogram.h"
527 #undef PRVMTRACE
528                         }
529                         else
530                         {
531 #include "prvm_execprogram.h"
532                         }
533 #undef PRVMBOUNDSCHECK
534                 }
535                 else
536                 {
537                         if (prog->trace)
538                         {
539 #define PRVMTRACE 1
540 #include "prvm_execprogram.h"
541 #undef PRVMTRACE
542                         }
543                         else
544                         {
545 #include "prvm_execprogram.h"
546                         }
547                 }
548 #undef PRVMSTATEMENTPROFILING
549         }
550         else
551         {
552                 if (prvm_boundscheck.integer)
553                 {
554 #define PRVMBOUNDSCHECK 1
555                         if (prog->trace)
556                         {
557 #define PRVMTRACE 1
558 #include "prvm_execprogram.h"
559 #undef PRVMTRACE
560                         }
561                         else
562                         {
563 #include "prvm_execprogram.h"
564                         }
565 #undef PRVMBOUNDSCHECK
566                 }
567                 else
568                 {
569                         if (prog->trace)
570                         {
571 #define PRVMTRACE 1
572 #include "prvm_execprogram.h"
573 #undef PRVMTRACE
574                         }
575                         else
576                         {
577 #include "prvm_execprogram.h"
578                         }
579                 }
580         }
581 }