]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/source/fteqcc-src/pr_exec.c
Better comment on my last change
[voretournament/voretournament.git] / misc / source / fteqcc-src / pr_exec.c
1 #define PROGSUSED
2 #include "progsint.h"
3 //#include "editor.h"
4
5 #define HunkAlloc BADGDFG sdfhhsf FHS
6
7
8 #define Host_Error Sys_Error
9
10 // I put the following here to resolve "undefined reference to `__imp__vsnprintf'" with MinGW64 ~ Moodles
11 #ifdef _WIN32
12         #if (_MSC_VER >= 1400)
13                 //with MSVC 8, use MS extensions
14                 #define snprintf linuxlike_snprintf_vc8
15                 int VARGS linuxlike_snprintf_vc8(char *buffer, int size, const char *format, ...) LIKEPRINTF(3);
16                 #define vsnprintf(a, b, c, d) vsnprintf_s(a, b, _TRUNCATE, c, d)
17         #else
18                 //msvc crap
19                 #define snprintf linuxlike_snprintf
20                 int VARGS linuxlike_snprintf(char *buffer, int size, const char *format, ...) LIKEPRINTF(3);
21                 #define vsnprintf linuxlike_vsnprintf
22                 int VARGS linuxlike_vsnprintf(char *buffer, int size, const char *format, va_list argptr);
23         #endif
24 #endif
25
26
27 //=============================================================================
28
29 /*
30 =================
31 PR_PrintStatement
32 =================
33 */
34 /*
35 void PR_PrintStatement (progfuncs_t *progfuncs, dstatement16_t *s)
36 {
37         int             i;
38 printf("PR_PrintStatement is unsupported\n");
39 return;
40         if ( (unsigned)s->op < OP_NUMOPS)
41         {
42                 printf ("%s ",  pr_opcodes[s->op].name);
43                 i = strlen(pr_opcodes[s->op].name);
44                 for ( ; i<10 ; i++)
45                         printf (" ");
46         }
47
48         if (s->op == OP_IF || s->op == OP_IFNOT)
49                 printf ("%sbranch %i",PR_GlobalString(progfuncs, s->a),s->b);
50         else if (s->op == OP_GOTO)
51         {
52                 printf ("branch %i",s->a);
53         }
54         else if ( (unsigned)(s->op - OP_STORE_F) < 6)
55         {
56                 printf ("%s",PR_GlobalString(progfuncs, s->a));
57                 printf ("%s", PR_GlobalStringNoContents(progfuncs, s->b));
58         }
59         else
60         {
61                 if (s->a)
62                         printf ("%s",PR_GlobalString(progfuncs, s->a));
63                 if (s->b)
64                         printf ("%s",PR_GlobalString(progfuncs, s->b));
65                 if (s->c)
66                         printf ("%s", PR_GlobalStringNoContents(progfuncs, s->c));
67         }
68         printf ("\n");
69 }
70 */
71
72 /*
73 ============
74 PR_StackTrace
75 ============
76 */
77 char *QC_ucase(char *str)
78 {
79         static char s[1024];
80         strcpy(s, str);
81         str = s;
82
83         while(*str)
84         {
85                 if (*str >= 'a' && *str <= 'z')
86                         *str = *str - 'a' + 'A';
87                 str++;
88         }
89         return s;
90 }
91
92 void PR_StackTrace (progfuncs_t *progfuncs)
93 {
94         dfunction_t     *f;
95         int                     i;
96         int progs;
97
98 #ifdef STACKTRACE
99         int arg;
100         int *globalbase;
101 #endif
102         progs = -1;
103
104         if (pr_depth == 0)
105         {
106                 printf ("<NO STACK>\n");
107                 return;
108         }
109
110 #ifdef STACKTRACE
111         globalbase = (int *)pr_globals + pr_xfunction->parm_start - pr_xfunction->locals;
112 #endif
113
114         pr_stack[pr_depth].f = pr_xfunction;
115         pr_stack[pr_depth].s = pr_xstatement;
116         for (i=pr_depth ; i>0 ; i--)
117         {
118                 f = pr_stack[i].f;
119
120                 if (!f)
121                 {
122                         printf ("<NO FUNCTION>\n");
123                 }
124                 else
125                 {
126                         if (pr_stack[i].progsnum != progs)
127                         {
128                                 progs = pr_stack[i].progsnum;
129
130                                 printf ("<%s>\n", pr_progstate[progs].filename);
131                         }
132                         if (!f->s_file)
133                                 printf ("stripped     : %s\n", f->s_name+progfuncs->stringtable);
134                         else
135                         {
136                                 if (pr_progstate[progs].linenums)
137                                         printf ("%12s %i : %s\n", f->s_file+progfuncs->stringtable, pr_progstate[progs].linenums[pr_stack[i].s], f->s_name+progfuncs->stringtable);
138                                 else
139                                         printf ("%12s : %s\n", f->s_file+progfuncs->stringtable, f->s_name+progfuncs->stringtable);
140                         }
141
142 #ifdef STACKTRACE
143
144                         for (arg = 0; arg < f->locals; arg++)
145                         {
146                                 ddef16_t *local;
147                                 local = ED_GlobalAtOfs16(progfuncs, f->parm_start+arg);
148                                 if (!local)
149                                 {
150                                         printf("    ofs %i: %f : %i\n", f->parm_start+arg, *(float *)(globalbase - f->locals+arg), *(int *)(globalbase - f->locals+arg) );
151                                 }
152                                 else
153                                 {
154                                         printf("    %s: %s\n", local->s_name+progfuncs->stringtable, PR_ValueString(progfuncs, local->type, (eval_t*)(globalbase - f->locals+arg)));
155                                         if (local->type == ev_vector)
156                                                 arg+=2;
157                                 }
158                         }
159
160                         if (i == pr_depth)
161                                 globalbase = localstack + localstack_used;
162                         else
163                                 globalbase -= f->locals;
164 #endif
165                 }
166         }
167 }
168
169 /*
170 ============
171 PR_Profile_f
172
173 ============
174 */
175 /*
176 void PR_Profile_f (void)
177 {
178         dfunction_t     *f, *best;
179         int                     max;
180         int                     num;
181         unsigned int                    i;
182
183         num = 0;
184         do
185         {
186                 max = 0;
187                 best = NULL;
188                 for (i=0 ; i<pr_progs->numfunctions ; i++)
189                 {
190                         f = &pr_functions[i];
191                         if (f->profile > max && f->first_statement >=0)
192                         {
193                                 max = f->profile;
194                                 best = f;
195                         }
196                 }
197                 if (best)
198                 {
199                         if (num < 10)
200                                 printf ("%7i %s\n", best->profile, best->s_name);
201                         num++;
202                         best->profile = 0;
203                 }
204         } while (best);
205 }
206 */
207
208
209 /*
210 ============
211 PR_RunError
212
213 Aborts the currently executing function
214 ============
215 */
216 void VARGS PR_RunError (progfuncs_t *progfuncs, char *error, ...)
217 {
218         va_list         argptr;
219         char            string[1024];
220
221         va_start (argptr,error);
222         Q_vsnprintf (string,sizeof(string)-1, error,argptr);
223         va_end (argptr);
224
225 //      {
226 //              void SV_EndRedirect (void);
227 //              SV_EndRedirect();
228 //      }
229
230 //      PR_PrintStatement (pr_statements + pr_xstatement);
231         PR_StackTrace (progfuncs);
232         printf ("\n");
233
234 //editbadfile(pr_strings + pr_xfunction->s_file, -1);
235
236 //      pr_depth = 0;           // dump the stack so host_error can shutdown functions
237 //      prinst->exitdepth = 0;
238
239         Abort ("%s", string);
240 }
241
242 /*
243 ============================================================================
244 PR_ExecuteProgram
245
246 The interpretation main loop
247 ============================================================================
248 */
249
250 /*
251 ====================
252 PR_EnterFunction
253
254 Returns the new program statement counter
255 ====================
256 */
257 void    PR_AbortStack                   (progfuncs_t *progfuncs);
258 int ASMCALL PR_EnterFunction (progfuncs_t *progfuncs, dfunction_t *f, int progsnum)
259 {
260         int             i, j, c, o;
261
262         pr_stack[pr_depth].s = pr_xstatement;
263         pr_stack[pr_depth].f = pr_xfunction;
264         pr_stack[pr_depth].progsnum = progsnum;
265         pr_stack[pr_depth].pushed = pr_spushed;
266         pr_depth++;
267         if (pr_depth == MAX_STACK_DEPTH)
268         {
269                 pr_depth--;
270                 PR_StackTrace (progfuncs);
271
272                 printf ("stack overflow on call to %s\n", progfuncs->stringtable+f->s_name);
273
274                 //comment this out if you want the progs to try to continue anyway (could cause infinate loops)
275                 PR_AbortStack(progfuncs);
276                 Abort("Stack Overflow in %s\n", progfuncs->stringtable+f->s_name);
277                 return pr_xstatement;
278         }
279
280         localstack_used += pr_spushed;  //make sure the call doesn't hurt pushed pointers
281
282 // save off any locals that the new function steps on (to a side place, fromwhere they are restored on exit)
283         c = f->locals;
284         if (localstack_used + c > LOCALSTACK_SIZE)
285         {
286                 localstack_used -= pr_spushed;
287                 pr_depth--;
288                 PR_RunError (progfuncs, "PR_ExecuteProgram: locals stack overflow\n");
289         }
290
291         for (i=0 ; i < c ; i++)
292                 localstack[localstack_used+i] = ((int *)pr_globals)[f->parm_start + i];
293         localstack_used += c;
294
295 // copy parameters (set initial values)
296         o = f->parm_start;
297         for (i=0 ; i<f->numparms ; i++)
298         {
299                 for (j=0 ; j<f->parm_size[i] ; j++)
300                 {
301                         ((int *)pr_globals)[o] = ((int *)pr_globals)[OFS_PARM0+i*3+j];
302                         o++;
303                 }
304         }
305
306         pr_xfunction = f;
307         return f->first_statement - 1;  // offset the s++
308 }
309
310 /*
311 ====================
312 PR_LeaveFunction
313 ====================
314 */
315 int ASMCALL PR_LeaveFunction (progfuncs_t *progfuncs)
316 {
317         int             i, c;
318
319         if (pr_depth <= 0)
320                 Sys_Error ("prog stack underflow");
321
322 // restore locals from the stack
323         c = pr_xfunction->locals;
324         localstack_used -= c;
325         if (localstack_used < 0)
326                 PR_RunError (progfuncs, "PR_ExecuteProgram: locals stack underflow\n");
327
328         for (i=0 ; i < c ; i++)
329                 ((int *)pr_globals)[pr_xfunction->parm_start + i] = localstack[localstack_used+i];
330
331 // up stack
332         pr_depth--;
333         PR_MoveParms(progfuncs, pr_stack[pr_depth].progsnum, pr_typecurrent);
334         PR_SwitchProgs(progfuncs, pr_stack[pr_depth].progsnum);
335         pr_xfunction = pr_stack[pr_depth].f;
336         pr_spushed = pr_stack[pr_depth].pushed;
337
338         localstack_used -= pr_spushed;
339         return pr_stack[pr_depth].s;
340 }
341
342 ddef32_t *ED_FindLocalOrGlobal(progfuncs_t *progfuncs, char *name, eval_t **val)
343 {
344         static ddef32_t def;
345         ddef32_t *def32;
346         ddef16_t *def16;
347         int i;
348
349         switch (pr_progstate[pr_typecurrent].structtype)
350         {
351         case PST_DEFAULT:
352         case PST_KKQWSV:
353                 //this gets parms fine, but not locals
354                 if (pr_xfunction)
355                 for (i = 0; i < pr_xfunction->numparms; i++)
356                 {
357                         def16 = ED_GlobalAtOfs16(progfuncs, pr_xfunction->parm_start+i);
358                         if (!def16)
359                                 continue;
360                         if (!strcmp(def16->s_name+progfuncs->stringtable, name))
361                         {
362                                 *val = (eval_t *)&pr_progstate[pr_typecurrent].globals[pr_xfunction->parm_start+i];
363
364                                 //we need something like this for functions that are not the top layer
365         //                      *val = (eval_t *)&localstack[localstack_used-pr_xfunction->numparms*4];
366                                 def.ofs = def16->ofs;
367                                 def.s_name = def16->s_name;
368                                 def.type = def16->type;
369                                 return &def;
370                         }
371                 }
372                 def16 = ED_FindGlobal16(progfuncs, name);
373                 if (!def16)
374                         return NULL;
375                 def.ofs = def16->ofs;
376                 def.type = def16->type;
377                 def.s_name = def16->s_name;
378                 def32 = &def;
379                 break;
380         case PST_QTEST:
381         case PST_FTE32:
382                 //this gets parms fine, but not locals
383                 if (pr_xfunction)
384                 for (i = 0; i < pr_xfunction->numparms; i++)
385                 {
386                         def32 = ED_GlobalAtOfs32(progfuncs, pr_xfunction->parm_start+i);
387                         if (!def32)
388                                 continue;
389                         if (!strcmp(def32->s_name+progfuncs->stringtable, name))
390                         {
391                                 *val = (eval_t *)&pr_progstate[pr_typecurrent].globals[pr_xfunction->parm_start+i];
392
393                                 //we need something like this for functions that are not the top layer
394         //                      *val = (eval_t *)&localstack[localstack_used-pr_xfunction->numparms*4];
395                                 return def32;
396                         }
397                 }
398                 def32 = ED_FindGlobal32(progfuncs, name);
399                 if (!def32)
400                         return NULL;
401                 break;
402         default:
403                 Sys_Error("Bad struct type in ED_FindLocalOrGlobal");
404                 def32 = NULL;
405         }
406
407         *val = (eval_t *)&pr_progstate[pr_typecurrent].globals[def32->ofs];
408         return &def;
409 }
410
411 char *COM_TrimString(char *str)
412 {
413         int i;
414         static char buffer[256];
415         while (*str <= ' ' && *str>'\0')
416                 str++;
417
418         for (i = 0; i < 255; i++)
419         {
420                 if (*str <= ' ')
421                         break;
422                 buffer[i] = *str++;
423         }
424         buffer[i] = '\0';
425         return buffer;
426 }
427
428 char *EvaluateDebugString(progfuncs_t *progfuncs, char *key)
429 {
430         static char buf[256];
431         char *c, *c2;
432         ddef32_t *def;
433         fdef_t *fdef;
434         eval_t *val;
435         char *assignment;
436         int type;
437         ddef32_t fakedef;
438         eval_t fakeval;
439
440         assignment = strchr(key, '=');
441         if (assignment)
442                 *assignment = '\0';
443
444         c = strchr(key, '.');
445         if (c) *c = '\0';
446         def = ED_FindLocalOrGlobal(progfuncs, key, &val);
447         if (!def)
448         {
449                 if (atoi(key))
450                 {
451                         def = &fakedef;
452                         def->ofs = 0;
453                         def->type = ev_entity;
454                         val = &fakeval;
455                         val->edict = atoi(key);
456                 }
457         }
458         if (c) *c = '.';
459         if (!def)
460         {
461                 return "(Bad string)";
462         }
463         type = def->type;
464
465         //go through ent vars
466         c = strchr(key, '.');
467         while(c)
468         {
469                 c2 = c+1;
470                 c = strchr(c2, '.');
471                 type = type &~DEF_SAVEGLOBAL;
472                 if (current_progstate->types)
473                         type = current_progstate->types[type].type;
474                 if (type != ev_entity)
475                         return "'.' without entity";
476                 if (c)*c = '\0';
477                 fdef = ED_FindField(progfuncs, COM_TrimString(c2));
478                 if (c)*c = '.';
479                 if (!fdef)
480                         return "(Bad string)";
481                 val = (eval_t *) (((char *)PROG_TO_EDICT(progfuncs, val->_int)->fields) + fdef->ofs*4);
482                 type = fdef->type;
483         }
484
485         if (assignment)
486         {
487                 assignment++;
488                 switch (type&~DEF_SAVEGLOBAL)
489                 {
490                 case ev_string:
491                         *(string_t *)val = PR_StringToProgs(progfuncs, ED_NewString (progfuncs, assignment, 0));
492                         break;
493
494                 case ev_float:
495                         *(float *)val = (float)atof (assignment);
496                         break;
497
498                 case ev_integer:
499                         *(int *)val = atoi (assignment);
500                         break;
501
502 /*              case ev_vector:
503                         strcpy (string, assignment);
504                         v = string;
505                         w = string;
506                         for (i=0 ; i<3 ; i++)
507                         {
508                                 while (*v && *v != ' ')
509                                         v++;
510                                 *v = 0;
511                                 ((float *)d)[i] = (float)atof (w);
512                                 w = v = v+1;
513                         }
514                         break;
515 */
516                 case ev_entity:
517                         *(int *)val = EDICT_TO_PROG(progfuncs, EDICT_NUM(progfuncs, atoi (assignment)));
518                         break;
519
520                 case ev_field:
521                         fdef = ED_FindField (progfuncs, assignment);
522                         if (!fdef)
523                         {
524                                 size_t l,nl = strlen(assignment);
525                                 strcpy(buf, "Can't find field ");
526                                 l = strlen(buf);
527                                 if (nl > sizeof(buf)-l-2)
528                                         nl = sizeof(buf)-l-2;
529                                 memcpy(buf+l, assignment, nl);
530                                 assignment[l+nl+0] = '\n';
531                                 assignment[l+nl+1] = 0;
532                                 return buf;
533                         }
534                         *(int *)val = G_INT(fdef->ofs);
535                         break;
536
537                 case ev_function:
538                         {
539                                 dfunction_t *func;
540                                 int i;
541                                 int progsnum = -1;
542                                 char *s = assignment;
543                                 if (s[0] && s[1] == ':')
544                                 {
545                                         progsnum = atoi(s);
546                                         s+=2;
547                                 }
548                                 else if (s[0] && s[1] && s[2] == ':')
549                                 {
550                                         progsnum = atoi(s);
551                                         s+=3;
552                                 }
553
554                                 func = ED_FindFunction (progfuncs, s, &i, progsnum);
555                                 if (!func)
556                                 {
557                                         size_t l,nl = strlen(s);
558
559                                         assignment[-1] = '=';
560
561                                         strcpy(buf, "Can't find field ");
562                                         l = strlen(buf);
563                                         if (nl > sizeof(buf)-l-2)
564                                                 nl = sizeof(buf)-l-2;
565                                         memcpy(buf+l, assignment, nl);
566                                         assignment[l+nl+0] = '\n';
567                                         assignment[l+nl+1] = 0;
568                                         return buf;
569                                 }
570                                 *(func_t *)val = (func - pr_progstate[i].functions) | (i<<24);
571                         }
572                         break;
573
574                 default:
575                         break;
576
577                 }
578                 assignment[-1] = '=';
579         }
580         strcpy(buf, PR_ValueString(progfuncs, type, val));
581
582         return buf;
583 }
584
585 int debugstatement;
586 //int EditorHighlightLine(window_t *wnd, int line);
587 void SetExecutionToLine(progfuncs_t *progfuncs, int linenum)
588 {
589         int pn = pr_typecurrent;
590         int snum;
591         dfunction_t *f = pr_xfunction;
592
593         switch(current_progstate->structtype)
594         {
595         case PST_DEFAULT:
596         case PST_QTEST:
597                 for (snum = f->first_statement; pr_progstate[pn].linenums[snum] < linenum; snum++)
598                 {
599                         if (pr_statements16[snum].op == OP_DONE)
600                                 return;
601                 }
602                 break;
603         case PST_KKQWSV:
604         case PST_FTE32:
605                 for (snum = f->first_statement; pr_progstate[pn].linenums[snum] < linenum; snum++)
606                 {
607                         if (pr_statements32[snum].op == OP_DONE)
608                                 return;
609                 }
610                 break;
611         default:
612                 Sys_Error("Bad struct type");
613                 snum = 0;
614         }
615         debugstatement = snum;
616 //      EditorHighlightLine(editwnd, pr_progstate[pn].linenums[snum]);
617 }
618
619 //0 clear. 1 set, 2 toggle, 3 check
620 int PR_ToggleBreakpoint(progfuncs_t *progfuncs, char *filename, int linenum, int flag)  //write alternate route to work by function name.
621 {
622         int ret=0;
623         unsigned int fl;
624         unsigned int i;
625         int pn = pr_typecurrent;
626         dfunction_t *f;
627         int op = 0; //warning about not being initialized before use
628
629         for (pn = 0; (unsigned)pn < maxprogs; pn++)
630         {
631                 if (!pr_progstate || !pr_progstate[pn].progs)
632                         continue;
633
634                 if (linenum)    //linenum is set means to set the breakpoint on a file and line
635                 {
636                         if (!pr_progstate[pn].linenums)
637                                 continue;
638
639                         for (f = pr_progstate[pn].functions, fl = 0; fl < pr_progstate[pn].progs->numfunctions; f++, fl++)
640                         {
641                                 if (!stricmp(f->s_file+progfuncs->stringtable, filename))
642                                 {
643                                         for (i = f->first_statement; ; i++)
644                                         {
645                                                 if (pr_progstate[pn].linenums[i] >= linenum)
646                                                 {
647                                                         fl = pr_progstate[pn].linenums[i];
648                                                         for (; ; i++)
649                                                         {
650                                                                 if ((unsigned int)pr_progstate[pn].linenums[i] > fl)
651                                                                         break;
652
653                                                                 switch(pr_progstate[pn].structtype)
654                                                                 {
655                                                                 case PST_DEFAULT:
656                                                                 case PST_QTEST:
657                                                                         op = ((dstatement16_t*)pr_progstate[pn].statements + i)->op;
658                                                                         break;
659                                                                 case PST_KKQWSV:
660                                                                 case PST_FTE32:
661                                                                         op = ((dstatement32_t*)pr_progstate[pn].statements + i)->op;
662                                                                         break;
663                                                                 default:
664                                                                         Sys_Error("Bad structtype");
665                                                                         op = 0;
666                                                                 }
667                                                                 switch (flag)
668                                                                 {
669                                                                 default:
670                                                                         if (op & 0x8000)
671                                                                         {
672                                                                                 op &= ~0x8000;
673                                                                                 ret = false;
674                                                                                 flag = 0;
675                                                                         }
676                                                                         else
677                                                                         {
678                                                                                 op |= 0x8000;
679                                                                                 ret = true;
680                                                                                 flag = 1;
681                                                                         }
682                                                                         break;
683                                                                 case 0:
684                                                                         op &= ~0x8000;
685                                                                         ret = false;
686                                                                         break;
687                                                                 case 1:
688                                                                         op |= 0x8000;
689                                                                         ret = true;
690                                                                         break;
691                                                                 case 3:
692                                                                         if (op & 0x8000)
693                                                                                 return true;
694                                                                 }
695                                                                 switch(pr_progstate[pn].structtype)
696                                                                 {
697                                                                 case PST_DEFAULT:
698                                                                 case PST_QTEST:
699                                                                         ((dstatement16_t*)pr_progstate[pn].statements + i)->op = op;
700                                                                         break;
701                                                                 case PST_KKQWSV:
702                                                                 case PST_FTE32:
703                                                                         ((dstatement32_t*)pr_progstate[pn].statements + i)->op = op;
704                                                                         break;
705                                                                 default:
706                                                                         Sys_Error("Bad structtype");
707                                                                         op = 0;
708                                                                 }
709                                                         }
710                                                         goto cont;
711                                                 }
712                                         }
713                                 }
714                         }
715                 }
716                 else    //set the breakpoint on the first statement of the function specified.
717                 {
718                         for (f = pr_progstate[pn].functions, fl = 0; fl < pr_progstate[pn].progs->numfunctions; f++, fl++)
719                         {
720                                 if (!strcmp(f->s_name+progfuncs->stringtable, filename))
721                                 {
722                                         i = f->first_statement;
723                                         switch(pr_progstate[pn].structtype)
724                                         {
725                                         case PST_DEFAULT:
726                                         case PST_QTEST:
727                                                 op = ((dstatement16_t*)pr_progstate[pn].statements + i)->op;
728                                                 break;
729                                         case PST_KKQWSV:
730                                         case PST_FTE32:
731                                                 op = ((dstatement32_t*)pr_progstate[pn].statements + i)->op;
732                                                 break;
733                                         default:
734                                                 Sys_Error("Bad structtype");
735                                         }
736                                         switch (flag)
737                                         {
738                                         default:
739                                                 if (op & 0x8000)
740                                                 {
741                                                         op &= ~0x8000;
742                                                         ret = false;
743                                                         flag = 0;
744                                                 }
745                                                 else
746                                                 {
747                                                         op |= 0x8000;
748                                                         ret = true;
749                                                         flag = 1;
750                                                 }
751                                                 break;
752                                         case 0:
753                                                 op &= ~0x8000;
754                                                 ret = false;
755                                                 break;
756                                         case 1:
757                                                 op |= 0x8000;
758                                                 ret = true;
759                                                 break;
760                                         case 3:
761                                                 if (op & 0x8000)
762                                                         return true;
763                                         }
764                                         switch(pr_progstate[pn].structtype)
765                                         {
766                                         case PST_DEFAULT:
767                                         case PST_QTEST:
768                                                 ((dstatement16_t*)pr_progstate[pn].statements + i)->op = op;
769                                                 break;
770                                         case PST_KKQWSV:
771                                         case PST_FTE32:
772                                                 ((dstatement32_t*)pr_progstate[pn].statements + i)->op = op;
773                                                 break;
774                                         default:
775                                                 Sys_Error("Bad structtype");
776                                         }
777                                         break;
778                                 }
779                         }
780                 }
781 cont:
782                 continue;
783         }
784
785         return ret;
786 }
787
788 int ShowStep(progfuncs_t *progfuncs, int statement)
789 {
790 //      return statement;
791 //      texture realcursortex;
792 static int lastline = 0;
793 static char *lastfile = 0;
794
795         int pn = pr_typecurrent;
796         int i;
797         dfunction_t *f = pr_xfunction;
798
799         if (f && pr_progstate[pn].linenums && externs->useeditor)
800         {
801                 if (lastline == pr_progstate[pn].linenums[statement] && lastfile == f->s_file+progfuncs->stringtable)
802                         return statement;       //no info/same line as last time
803
804                 lastline = pr_progstate[pn].linenums[statement];
805                 lastfile = f->s_file+progfuncs->stringtable;
806
807                 lastline = externs->useeditor(progfuncs, lastfile, lastline, 0, NULL);
808
809                 if (pr_progstate[pn].linenums[statement] != lastline)
810                 {
811                         for (i = f->first_statement; ; i++)
812                         {
813                                 if (lastline == pr_progstate[pn].linenums[i])
814                                 {
815                                         return i;
816                                 }
817                                 else if (lastline <= pr_progstate[pn].linenums[i])
818                                 {
819                                         return statement;
820                                 }
821                         }
822                 }
823         }
824         else if (f)     //annoying.
825         {
826                 if (*(f->s_file+progfuncs->stringtable))        //if we can't get the filename, then it was stripped, and debugging it like this is useless
827                         if (externs->useeditor)
828                                 externs->useeditor(progfuncs, f->s_file+progfuncs->stringtable, -1, 0, NULL);
829                 return statement;
830         }
831
832
833         return statement;
834 }
835
836 //DMW: all pointer functions are modified to be absoloute pointers from NULL not sv_edicts
837 /*
838 ====================
839 PR_ExecuteProgram
840 ====================
841 */
842 void PR_ExecuteCode (progfuncs_t *progfuncs, int s)
843 {
844         eval_t  *t, *swtch=NULL;
845
846         int swtchtype = 0; //warning about not being initialized before use
847         dstatement16_t  *st16;
848         dstatement32_t  *st32;
849         dfunction_t     *newf;
850         int             runaway;
851         int             i;
852         int p;
853         edictrun_t      *ed;
854         eval_t  *ptr;
855
856         float *glob;
857
858         int fnum;
859
860         prinst->continuestatement = -1;
861 #ifdef QCJIT
862         if (prinst->jit)
863         {
864                 PR_EnterJIT(progfuncs, prinst->jit, s);
865                 return;
866         }
867 #endif
868         fnum = pr_xfunction - pr_functions;
869
870         runaway = 100000000;
871
872 #define PRBOUNDSCHECK
873 #define RUNAWAYCHECK()                                                  \
874         if (!--runaway)                                                         \
875         {                                                                                       \
876                 pr_xstatement = st-pr_statements;               \
877                 PR_StackTrace(progfuncs);                               \
878                 printf ("runaway loop error");                  \
879                 while(pr_depth > prinst->exitdepth)             \
880                         PR_LeaveFunction(progfuncs);            \
881                 pr_spushed = 0;                                                 \
882                 return;                                                                 \
883         }
884
885 #define OPA ((eval_t *)&glob[st->a])
886 #define OPB ((eval_t *)&glob[st->b])
887 #define OPC ((eval_t *)&glob[st->c])
888
889 restart:        //jumped to when the progs might have changed.
890         glob = pr_globals;
891         switch (current_progstate->structtype)
892         {
893         case PST_DEFAULT:
894         case PST_QTEST:
895 #define INTSIZE 16
896                 st16 = &pr_statements16[s];
897                 while (pr_trace)
898                 {
899                         #define DEBUGABLE
900                         #ifdef SEPARATEINCLUDES
901                                 #include "execloop16d.h"
902                         #else
903                                 #include "execloop.h"
904                         #endif
905                         #undef DEBUGABLE
906                 }
907
908                 while(1)
909                 {
910                         #include "execloop.h"
911                 }
912 #undef INTSIZE
913                 Sys_Error("PR_ExecuteProgram - should be unreachable");
914                 break;
915         case PST_KKQWSV:
916         case PST_FTE32:
917 #define INTSIZE 32
918                 st32 = &pr_statements32[s];
919                 while (pr_trace)
920                 {
921                         #define DEBUGABLE
922                         #ifdef SEPARATEINCLUDES
923                                 #include "execloop32d.h"
924                         #else
925                                 #include "execloop.h"
926                         #endif
927                         #undef DEBUGABLE
928                 }
929
930                 while(1)
931                 {
932                         #ifdef SEPARATEINCLUDES
933                                 #include "execloop32.h"
934                         #else
935                                 #include "execloop.h"
936                         #endif
937                 }
938 #undef INTSIZE
939                 Sys_Error("PR_ExecuteProgram - should be unreachable");
940                 break;
941         default:
942                 Sys_Error("PR_ExecuteProgram - bad structtype");
943         }
944 }
945
946
947 void PR_ExecuteProgram (progfuncs_t *progfuncs, func_t fnum)
948 {
949         dfunction_t     *f;
950         int             i;
951         unsigned int initial_progs;
952         int             oldexitdepth;
953
954         int s;
955
956         int tempdepth;
957
958         unsigned int newprogs = (fnum & 0xff000000)>>24;
959
960         initial_progs = pr_typecurrent;
961         if (newprogs != initial_progs)
962         {
963                 if (newprogs >= maxprogs || !&pr_progstate[newprogs].globals)   //can happen with hexen2...
964                 {
965                         printf("PR_ExecuteProgram: tried branching into invalid progs\n");
966                         return;
967                 }
968                 PR_MoveParms(progfuncs, newprogs, pr_typecurrent);
969                 PR_SwitchProgs(progfuncs, newprogs);
970         }
971
972         if (!(fnum & ~0xff000000) || (signed)(fnum & ~0xff000000) >= pr_progs->numfunctions)
973         {
974 //              if (pr_global_struct->self)
975 //                      ED_Print (PROG_TO_EDICT(pr_global_struct->self));
976                 printf("PR_ExecuteProgram: NULL function from exe\n");
977 //              Host_Error ("PR_ExecuteProgram: NULL function from exe");
978
979 //              PR_MoveParms(0, pr_typecurrent);
980                 PR_SwitchProgs(progfuncs, initial_progs);
981                 return;
982         }
983
984         oldexitdepth = prinst->exitdepth;
985
986         f = &pr_functions[fnum & ~0xff000000];
987
988         if (f->first_statement < 0)
989         {       // negative statements are built in functions
990                 i = -f->first_statement;
991
992                 if (i < externs->numglobalbuiltins)
993                         (*externs->globalbuiltins[i]) (progfuncs, (struct globalvars_s *)current_progstate->globals);
994                 else
995                 {
996                         i -= externs->numglobalbuiltins;
997                         if (i > current_progstate->numbuiltins)
998                         {
999                                 printf ("Bad builtin call number %i (from exe)\n", -f->first_statement);
1000                         //      PR_MoveParms(p, pr_typecurrent);
1001                                 PR_SwitchProgs(progfuncs, initial_progs);
1002                                 return;
1003                         }
1004                         current_progstate->builtins [i] (progfuncs, (struct globalvars_s *)current_progstate->globals);
1005                 }
1006                 PR_MoveParms(progfuncs, initial_progs, pr_typecurrent);
1007                 PR_SwitchProgs(progfuncs, initial_progs);
1008                 return;
1009         }
1010
1011         if (pr_trace)
1012                 pr_trace--;
1013
1014 // make a stack frame
1015         prinst->exitdepth = pr_depth;
1016
1017
1018         s = PR_EnterFunction (progfuncs, f, initial_progs);
1019
1020         tempdepth = prinst->numtempstringsstack;
1021         PR_ExecuteCode(progfuncs, s);
1022
1023
1024         PR_MoveParms(progfuncs, initial_progs, pr_typecurrent);
1025         PR_SwitchProgs(progfuncs, initial_progs);
1026
1027         PR_FreeTemps(progfuncs, tempdepth);
1028         prinst->numtempstringsstack = tempdepth;
1029
1030         prinst->exitdepth = oldexitdepth;
1031 }
1032
1033
1034
1035
1036
1037
1038 typedef struct {
1039         int fnum;
1040         int progsnum;
1041         int statement;
1042 } qcthreadstack_t;
1043 typedef struct qcthread_s {
1044         int fstackdepth;
1045         qcthreadstack_t fstack[MAX_STACK_DEPTH];
1046         int lstackused;
1047         int lstack[LOCALSTACK_SIZE];
1048         int xstatement;
1049         int xfunction;
1050         progsnum_t xprogs;
1051 } qcthread_t;
1052
1053 struct qcthread_s *PR_ForkStack(progfuncs_t *progfuncs)
1054 {       //QC code can call builtins that call qc code.
1055         //to get around the problems of restoring the builtins we simply don't save the thread over the builtin.
1056         int i, l;
1057         int ed = prinst->exitdepth;
1058         int localsoffset, baselocalsoffset;
1059         qcthread_t *thread = memalloc(sizeof(qcthread_t));
1060         dfunction_t *f;
1061
1062         //copy out the functions stack.
1063         for (i = 0,localsoffset=0; i < ed; i++)
1064         {
1065                 if (i+1 == pr_depth)
1066                         f = pr_xfunction;
1067                 else
1068                         f = pr_stack[i+1].f;
1069                 localsoffset += f->locals;      //this is where it crashes
1070         }
1071         baselocalsoffset = localsoffset;
1072         for (i = ed; i < pr_depth; i++)
1073         {
1074                 thread->fstack[i-ed].fnum = pr_stack[i].f - pr_progstate[pr_stack[i].progsnum].functions;
1075                 thread->fstack[i-ed].progsnum = pr_stack[i].progsnum;
1076                 thread->fstack[i-ed].statement = pr_stack[i].s;
1077
1078                 if (i+1 == pr_depth)
1079                         f = pr_xfunction;
1080                 else
1081                         f = pr_stack[i+1].f;
1082                 localsoffset += f->locals;
1083         }
1084         thread->fstackdepth = pr_depth - ed;
1085
1086         for (i = pr_depth - 1; i >= ed ; i--)
1087         {
1088                 if (i+1 == pr_depth)
1089                         f = pr_xfunction;
1090                 else
1091                         f = pr_stack[i+1].f;
1092                 localsoffset -= f->locals;
1093                 for (l = 0; l < f->locals; l++)
1094                 {
1095                         thread->lstack[localsoffset-baselocalsoffset + l ] = ((int *)pr_globals)[f->parm_start + l];
1096                         ((int *)pr_globals)[f->parm_start + l] = localstack[localsoffset+l];    //copy the old value into the globals (so the older functions have the correct locals.
1097                 }
1098         }
1099
1100         for (i = ed; i < pr_depth ; i++)        //we need to get the locals back to how they were.
1101         {
1102                 if (i+1 == pr_depth)
1103                         f = pr_xfunction;
1104                 else
1105                         f = pr_stack[i+1].f;
1106
1107                 for (l = 0; l < f->locals; l++)
1108                 {
1109                         ((int *)pr_globals)[f->parm_start + l] = thread->lstack[localsoffset-baselocalsoffset + l];
1110                 }
1111                 localsoffset += f->locals;
1112         }
1113         thread->lstackused = localsoffset - baselocalsoffset;
1114
1115         thread->xstatement = pr_xstatement;
1116         thread->xfunction = pr_xfunction - pr_progstate[pr_typecurrent].functions;
1117         thread->xprogs = pr_typecurrent;
1118
1119         return thread;
1120 }
1121
1122 void PR_ResumeThread (progfuncs_t *progfuncs, struct qcthread_s *thread)
1123 {
1124         dfunction_t     *f, *oldf;
1125         int             i,l,ls;
1126         progsnum_t initial_progs;
1127         int             oldexitdepth;
1128
1129         int s;
1130         int tempdepth;
1131
1132         progsnum_t prnum = thread->xprogs;
1133         int fnum = thread->xfunction;
1134
1135         if (localstack_used + thread->lstackused > LOCALSTACK_SIZE)
1136                 PR_RunError(progfuncs, "Too many locals on resumtion of QC thread\n");
1137
1138         if (pr_depth + thread->fstackdepth > MAX_STACK_DEPTH)
1139                 PR_RunError(progfuncs, "Too large stack on resumtion of QC thread\n");
1140
1141
1142         //do progs switching stuff as appropriate. (fteqw only)
1143         initial_progs = pr_typecurrent;
1144         PR_MoveParms(progfuncs, prnum, pr_typecurrent);
1145         PR_SwitchProgs(progfuncs, prnum);
1146
1147
1148         oldexitdepth = prinst->exitdepth;
1149         prinst->exitdepth = pr_depth;
1150
1151         ls = 0;
1152         //add on the callstack.
1153         for (i = 0; i < thread->fstackdepth; i++)
1154         {
1155                 if (pr_depth == prinst->exitdepth)
1156                 {
1157                         pr_stack[pr_depth].f = pr_xfunction;
1158                         pr_stack[pr_depth].s = pr_xstatement;
1159                         pr_stack[pr_depth].progsnum = initial_progs;
1160                 }
1161                 else
1162                 {
1163                         pr_stack[pr_depth].progsnum = thread->fstack[i].progsnum;
1164                         pr_stack[pr_depth].f = pr_progstate[thread->fstack[i].progsnum].functions + thread->fstack[i].fnum;
1165                         pr_stack[pr_depth].s = thread->fstack[i].statement;
1166                 }
1167
1168                 if (i+1 == thread->fstackdepth)
1169                         f = &pr_functions[fnum];
1170                 else
1171                         f = pr_progstate[thread->fstack[i+1].progsnum].functions + thread->fstack[i+1].fnum;
1172                 for (l = 0; l < f->locals; l++)
1173                 {
1174                         localstack[localstack_used++] = ((int *)pr_globals)[f->parm_start + l];
1175                         ((int *)pr_globals)[f->parm_start + l] = thread->lstack[ls++];
1176                 }
1177
1178                 pr_depth++;
1179         }
1180
1181         if (ls != thread->lstackused)
1182                 PR_RunError(progfuncs, "Thread stores incorrect locals count\n");
1183
1184
1185         f = &pr_functions[fnum];
1186
1187 //      thread->lstackused -= f->locals;        //the current function is the odd one out.
1188
1189         //add on the locals stack
1190         memcpy(localstack+localstack_used, thread->lstack, sizeof(int)*thread->lstackused);
1191         localstack_used += thread->lstackused;
1192
1193         //bung the locals of the current function on the stack.
1194 //      for (i=0 ; i < f->locals ; i++)
1195 //              ((int *)pr_globals)[f->parm_start + i] = 0xff00ff00;//thread->lstack[thread->lstackused+i];
1196
1197
1198 //      PR_EnterFunction (progfuncs, f, initial_progs);
1199         oldf = pr_xfunction;
1200         pr_xfunction = f;
1201         s = thread->xstatement;
1202
1203         tempdepth = prinst->numtempstringsstack;
1204         PR_ExecuteCode(progfuncs, s);
1205
1206
1207         PR_MoveParms(progfuncs, initial_progs, pr_typecurrent);
1208         PR_SwitchProgs(progfuncs, initial_progs);
1209         PR_FreeTemps(progfuncs, tempdepth);
1210         prinst->numtempstringsstack = tempdepth;
1211
1212         prinst->exitdepth = oldexitdepth;
1213         pr_xfunction = oldf;
1214 }
1215
1216 void    PR_AbortStack                   (progfuncs_t *progfuncs)
1217 {
1218         while(pr_depth > prinst->exitdepth+1)
1219                 PR_LeaveFunction(progfuncs);
1220         prinst->continuestatement = 0;
1221 }
1222