]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - prvm_exec.c
oops, move the print for that string upwards, as other code there uses va() too
[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 void PRVM_ShortStackTrace(char *buf, size_t bufsize)
269 {
270         mfunction_t     *f;
271         int                     i;
272
273         if(prog)
274         {
275                 dpsnprintf(buf, bufsize, "(%s) ", prog->name);
276         }
277         else
278         {
279                 strlcpy(buf, "<NO PROG>", sizeof(buf));
280                 return;
281         }
282
283         prog->stack[prog->depth].s = prog->xstatement;
284         prog->stack[prog->depth].f = prog->xfunction;
285         for (i = prog->depth;i > 0;i--)
286         {
287                 f = prog->stack[i].f;
288
289                 if(strlcat(buf,
290                         f
291                                 ? va("%s:%s(%i) ", PRVM_GetString(f->s_file), PRVM_GetString(f->s_name), prog->stack[i].s - f->first_statement)
292                                 : "<NULL> ",
293                         bufsize
294                 ) >= bufsize)
295                         break;
296         }
297 }
298
299
300 void PRVM_CallProfile ()
301 {
302         mfunction_t *f, *best;
303         int i;
304         double max;
305         double sum;
306
307         Con_Printf( "%s Call Profile:\n", PRVM_NAME );
308
309         sum = 0;
310         do
311         {
312                 max = 0;
313                 best = NULL;
314                 for (i=0 ; i<prog->progs->numfunctions ; i++)
315                 {
316                         f = &prog->functions[i];
317                         if (max < f->totaltime)
318                         {
319                                 max = f->totaltime;
320                                 best = f;
321                         }
322                 }
323                 if (best)
324                 {
325                         sum += best->totaltime;
326                         Con_Printf("%9.4f %s\n", best->totaltime, PRVM_GetString(best->s_name));
327                         best->totaltime = 0;
328                 }
329         } while (best);
330
331         Con_Printf("Total time since last profile reset: %9.4f\n", Sys_DoubleTime() - prog->starttime);
332         Con_Printf("       - used by QC code of this VM: %9.4f\n", sum);
333
334         prog->starttime = Sys_DoubleTime();
335 }
336
337 void PRVM_Profile (int maxfunctions, int mininstructions)
338 {
339         mfunction_t *f, *best;
340         int i, num;
341         double max;
342
343         Con_Printf( "%s Profile:\n[CallCount] [Statements] [BuiltinCost]\n", PRVM_NAME );
344
345         num = 0;
346         do
347         {
348                 max = 0;
349                 best = NULL;
350                 for (i=0 ; i<prog->progs->numfunctions ; i++)
351                 {
352                         f = &prog->functions[i];
353                         if (max < f->profile + f->builtinsprofile + f->callcount)
354                         {
355                                 max = f->profile + f->builtinsprofile + f->callcount;
356                                 best = f;
357                         }
358                 }
359                 if (best)
360                 {
361                         if (num < maxfunctions && max >= mininstructions)
362                         {
363                                 if (best->first_statement < 0)
364                                         Con_Printf("%9.0f ----- builtin ----- %s\n", best->callcount, PRVM_GetString(best->s_name));
365                                 else
366                                         Con_Printf("%9.0f %9.0f %9.0f %s\n", best->callcount, best->profile, best->builtinsprofile, PRVM_GetString(best->s_name));
367                         }
368                         num++;
369                         best->profile = 0;
370                         best->builtinsprofile = 0;
371                         best->callcount = 0;
372                 }
373         } while (best);
374 }
375
376 /*
377 ============
378 PRVM_CallProfile_f
379
380 ============
381 */
382 void PRVM_CallProfile_f (void)
383 {
384         if (Cmd_Argc() != 2)
385         {
386                 Con_Print("prvm_callprofile <program name>\n");
387                 return;
388         }
389
390         PRVM_Begin;
391         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
392                 return;
393
394         PRVM_CallProfile();
395
396         PRVM_End;
397 }
398
399 /*
400 ============
401 PRVM_Profile_f
402
403 ============
404 */
405 void PRVM_Profile_f (void)
406 {
407         int howmany;
408
409         howmany = 1<<30;
410         if (Cmd_Argc() == 3)
411                 howmany = atoi(Cmd_Argv(2));
412         else if (Cmd_Argc() != 2)
413         {
414                 Con_Print("prvm_profile <program name>\n");
415                 return;
416         }
417
418         PRVM_Begin;
419         if(!PRVM_SetProgFromString(Cmd_Argv(1)))
420                 return;
421
422         PRVM_Profile(howmany, 1);
423
424         PRVM_End;
425 }
426
427 void PRVM_CrashAll()
428 {
429         int i;
430         prvm_prog_t *oldprog = prog;
431
432         for(i = 0; i < PRVM_MAXPROGS; i++)
433         {
434                 if(!PRVM_ProgLoaded(i))
435                         continue;
436                 PRVM_SetProg(i);
437                 PRVM_Crash();
438         }
439
440         prog = oldprog;
441 }
442
443 void PRVM_PrintState(void)
444 {
445         int i;
446         if(prog->statestring)
447         {
448                 Con_Printf("Caller-provided information: %s\n", prog->statestring);
449         }
450         if (prog->xfunction)
451         {
452                 for (i = -7; i <= 0;i++)
453                         if (prog->xstatement + i >= prog->xfunction->first_statement)
454                                 PRVM_PrintStatement (prog->statements + prog->xstatement + i);
455         }
456         else
457                 Con_Print("null function executing??\n");
458         PRVM_StackTrace ();
459 }
460
461 extern sizebuf_t vm_tempstringsbuf;
462 extern cvar_t prvm_errordump;
463 void Host_Savegame_to (const char *name);
464 void PRVM_Crash(void)
465 {
466         if (prog == NULL)
467                 return;
468
469         prog->funcoffsets.SV_Shutdown = 0; // don't call SV_Shutdown on crash
470
471         if( prog->depth > 0 )
472         {
473                 Con_Printf("QuakeC crash report for %s:\n", PRVM_NAME);
474                 PRVM_PrintState();
475         }
476
477         if(prvm_errordump.integer)
478         {
479                 // make a savegame
480                 Host_Savegame_to(va("crash-%s.dmp", PRVM_NAME));
481         }
482
483         // dump the stack so host_error can shutdown functions
484         prog->depth = 0;
485         prog->localstack_used = 0;
486
487         // delete all tempstrings (FIXME: is this safe in VM->engine->VM recursion?)
488         vm_tempstringsbuf.cursize = 0;
489
490         // reset the prog pointer
491         prog = NULL;
492 }
493
494 /*
495 ============================================================================
496 PRVM_ExecuteProgram
497
498 The interpretation main loop
499 ============================================================================
500 */
501
502 /*
503 ====================
504 PRVM_EnterFunction
505
506 Returns the new program statement counter
507 ====================
508 */
509 int PRVM_EnterFunction (mfunction_t *f)
510 {
511         int             i, j, c, o;
512
513         if (!f)
514                 PRVM_ERROR ("PRVM_EnterFunction: NULL function in %s", PRVM_NAME);
515
516         prog->stack[prog->depth].s = prog->xstatement;
517         prog->stack[prog->depth].f = prog->xfunction;
518         prog->depth++;
519         if (prog->depth >=PRVM_MAX_STACK_DEPTH)
520                 PRVM_ERROR ("stack overflow");
521
522 // save off any locals that the new function steps on
523         c = f->locals;
524         if (prog->localstack_used + c > PRVM_LOCALSTACK_SIZE)
525                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack overflow in %s", PRVM_NAME);
526
527         for (i=0 ; i < c ; i++)
528                 prog->localstack[prog->localstack_used+i] = ((int *)prog->globals.generic)[f->parm_start + i];
529         prog->localstack_used += c;
530
531 // copy parameters
532         o = f->parm_start;
533         for (i=0 ; i<f->numparms ; i++)
534         {
535                 for (j=0 ; j<f->parm_size[i] ; j++)
536                 {
537                         ((int *)prog->globals.generic)[o] = ((int *)prog->globals.generic)[OFS_PARM0+i*3+j];
538                         o++;
539                 }
540         }
541
542         prog->xfunction = f;
543         return f->first_statement - 1;  // offset the s++
544 }
545
546 /*
547 ====================
548 PRVM_LeaveFunction
549 ====================
550 */
551 int PRVM_LeaveFunction (void)
552 {
553         int             i, c;
554
555         if (prog->depth <= 0)
556                 PRVM_ERROR ("prog stack underflow in %s", PRVM_NAME);
557
558         if (!prog->xfunction)
559                 PRVM_ERROR ("PR_LeaveFunction: NULL function in %s", PRVM_NAME);
560 // restore locals from the stack
561         c = prog->xfunction->locals;
562         prog->localstack_used -= c;
563         if (prog->localstack_used < 0)
564                 PRVM_ERROR ("PRVM_ExecuteProgram: locals stack underflow in %s", PRVM_NAME);
565
566         for (i=0 ; i < c ; i++)
567                 ((int *)prog->globals.generic)[prog->xfunction->parm_start + i] = prog->localstack[prog->localstack_used+i];
568
569 // up stack
570         prog->depth--;
571         prog->xfunction = prog->stack[prog->depth].f;
572         return prog->stack[prog->depth].s;
573 }
574
575 void PRVM_Init_Exec(void)
576 {
577         // dump the stack
578         prog->depth = 0;
579         prog->localstack_used = 0;
580         // reset the string table
581         // nothing here yet
582 }
583
584 /*
585 ====================
586 PRVM_ExecuteProgram
587 ====================
588 */
589 // LordHavoc: optimized
590 #define OPA ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->a])
591 #define OPB ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->b])
592 #define OPC ((prvm_eval_t *)&prog->globals.generic[(unsigned short) st->c])
593 extern cvar_t prvm_traceqc;
594 extern cvar_t prvm_statementprofiling;
595 extern sizebuf_t vm_tempstringsbuf;
596 extern qboolean prvm_runawaycheck;
597 extern qboolean prvm_boundscheck;
598 void PRVM_ExecuteProgram (func_t fnum, const char *errormessage)
599 {
600         dstatement_t    *st, *startst;
601         mfunction_t     *f, *newf;
602         prvm_edict_t    *ed;
603         prvm_eval_t     *ptr;
604         int             jumpcount, cachedpr_trace, exitdepth;
605         int             restorevm_tempstringsbuf_cursize;
606         double  calltime;
607
608         calltime = Sys_DoubleTime();
609
610         if (!fnum || fnum >= (unsigned int)prog->progs->numfunctions)
611         {
612                 if (prog->globaloffsets.self >= 0 && PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict)
613                         PRVM_ED_Print(PRVM_PROG_TO_EDICT(PRVM_GLOBALFIELDVALUE(prog->globaloffsets.self)->edict), NULL);
614                 PRVM_ERROR ("PRVM_ExecuteProgram: %s", errormessage);
615         }
616
617         f = &prog->functions[fnum];
618
619         // after executing this function, delete all tempstrings it created
620         restorevm_tempstringsbuf_cursize = vm_tempstringsbuf.cursize;
621
622         prog->trace = prvm_traceqc.integer;
623
624         // we know we're done when pr_depth drops to this
625         exitdepth = prog->depth;
626
627 // make a stack frame
628         st = &prog->statements[PRVM_EnterFunction (f)];
629         // save the starting statement pointer for profiling
630         // (when the function exits or jumps, the (st - startst) integer value is
631         // added to the function's profile counter)
632         startst = st;
633         // instead of counting instructions, we count jumps
634         jumpcount = 0;
635         // add one to the callcount of this function because otherwise engine-called functions aren't counted
636         prog->xfunction->callcount++;
637
638 chooseexecprogram:
639         cachedpr_trace = prog->trace;
640         if (prvm_runawaycheck)
641         {
642 #define PRVMRUNAWAYCHECK 1
643                 if (prvm_statementprofiling.integer)
644                 {
645 #define PRVMSTATEMENTPROFILING 1
646                         if (prvm_boundscheck)
647                         {
648 #define PRVMBOUNDSCHECK 1
649                                 if (prog->trace)
650                                 {
651 #define PRVMTRACE 1
652 #include "prvm_execprogram.h"
653 #undef PRVMTRACE
654                                 }
655                                 else
656                                 {
657 #include "prvm_execprogram.h"
658                                 }
659 #undef PRVMBOUNDSCHECK
660                         }
661                         else
662                         {
663                                 if (prog->trace)
664                                 {
665 #define PRVMTRACE 1
666 #include "prvm_execprogram.h"
667 #undef PRVMTRACE
668                                 }
669                                 else
670                                 {
671 #include "prvm_execprogram.h"
672                                 }
673                         }
674 #undef PRVMSTATEMENTPROFILING
675                 }
676                 else
677                 {
678                         if (prvm_boundscheck)
679                         {
680 #define PRVMBOUNDSCHECK 1
681                                 if (prog->trace)
682                                 {
683 #define PRVMTRACE 1
684 #include "prvm_execprogram.h"
685 #undef PRVMTRACE
686                                 }
687                                 else
688                                 {
689 #include "prvm_execprogram.h"
690                                 }
691 #undef PRVMBOUNDSCHECK
692                         }
693                         else
694                         {
695                                 if (prog->trace)
696                                 {
697 #define PRVMTRACE 1
698 #include "prvm_execprogram.h"
699 #undef PRVMTRACE
700                                 }
701                                 else
702                                 {
703 #include "prvm_execprogram.h"
704                                 }
705                         }
706                 }
707 #undef PRVMRUNAWAYCHECK
708         }
709         else
710         {
711                 if (prvm_statementprofiling.integer)
712                 {
713 #define PRVMSTATEMENTPROFILING 1
714                         if (prvm_boundscheck)
715                         {
716 #define PRVMBOUNDSCHECK 1
717                                 if (prog->trace)
718                                 {
719 #define PRVMTRACE 1
720 #include "prvm_execprogram.h"
721 #undef PRVMTRACE
722                                 }
723                                 else
724                                 {
725 #include "prvm_execprogram.h"
726                                 }
727 #undef PRVMBOUNDSCHECK
728                         }
729                         else
730                         {
731                                 if (prog->trace)
732                                 {
733 #define PRVMTRACE 1
734 #include "prvm_execprogram.h"
735 #undef PRVMTRACE
736                                 }
737                                 else
738                                 {
739 #include "prvm_execprogram.h"
740                                 }
741                         }
742 #undef PRVMSTATEMENTPROFILING
743                 }
744                 else
745                 {
746                         if (prvm_boundscheck)
747                         {
748 #define PRVMBOUNDSCHECK 1
749                                 if (prog->trace)
750                                 {
751 #define PRVMTRACE 1
752 #include "prvm_execprogram.h"
753 #undef PRVMTRACE
754                                 }
755                                 else
756                                 {
757 #include "prvm_execprogram.h"
758                                 }
759 #undef PRVMBOUNDSCHECK
760                         }
761                         else
762                         {
763                                 if (prog->trace)
764                                 {
765 #define PRVMTRACE 1
766 #include "prvm_execprogram.h"
767 #undef PRVMTRACE
768                                 }
769                                 else
770                                 {
771 #include "prvm_execprogram.h"
772                                 }
773                         }
774                 }
775         }
776
777 cleanup:
778         if (developer.integer >= 200 && vm_tempstringsbuf.cursize > restorevm_tempstringsbuf_cursize)
779                 Con_Printf("PRVM_ExecuteProgram: %s used %i bytes of tempstrings\n", PRVM_GetString(prog->functions[fnum].s_name), vm_tempstringsbuf.cursize - restorevm_tempstringsbuf_cursize);
780         // delete tempstrings created by this function
781         vm_tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize;
782
783         prog->functions[fnum].totaltime += (Sys_DoubleTime() - calltime);
784
785         SV_FlushBroadcastMessages();
786 }