Add more debugging prints in Draw_CachePic, most of these are for edge cases.
[xonotic/darkplaces.git] / prvm_execprogram.h
1 // NEED to reset startst after calling this! startst may or may not be clobbered!
2 #define ADVANCE_PROFILE_BEFORE_JUMP() \
3         prog->xfunction->profile += (st - startst); \
4         if (prvm_statementprofiling.integer || (prvm_coverage.integer & 4)) { \
5                 /* All statements from startst+1 to st have been hit. */ \
6                 while (++startst <= st) { \
7                         if (prog->statement_profile[startst - cached_statements]++ == 0 && (prvm_coverage.integer & 4)) \
8                                 PRVM_StatementCoverageEvent(prog, prog->xfunction, startst - cached_statements); \
9                 } \
10                 /* Observe: startst now is clobbered (now at st+1)! */ \
11         }
12
13 #ifdef PRVMTIMEPROFILING
14 #define PRE_ERROR() \
15         ADVANCE_PROFILE_BEFORE_JUMP(); \
16         prog->xstatement = st - cached_statements; \
17         tm = Sys_DirtyTime(); \
18         prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0; \
19         startst = st; \
20         starttm = tm
21 #else
22 #define PRE_ERROR() \
23         ADVANCE_PROFILE_BEFORE_JUMP(); \
24         prog->xstatement = st - cached_statements; \
25         startst = st
26 #endif
27
28 // This code isn't #ifdef/#define protectable, don't try.
29
30 #if HAVE_COMPUTED_GOTOS && !(PRVMSLOWINTERPRETER || PRVMTIMEPROFILING)
31   // NOTE: Due to otherwise duplicate labels, only ONE interpreter path may
32   // ever hit this!
33 # define USE_COMPUTED_GOTOS 1
34 #endif
35
36 #if USE_COMPUTED_GOTOS
37   // Must exactly match opcode_e enum in pr_comp.h
38     const static void *dispatchtable[] = {
39         &&handle_OP_DONE,
40         &&handle_OP_MUL_F,
41         &&handle_OP_MUL_V,
42         &&handle_OP_MUL_FV,
43         &&handle_OP_MUL_VF,
44         &&handle_OP_DIV_F,
45         &&handle_OP_ADD_F,
46         &&handle_OP_ADD_V,
47         &&handle_OP_SUB_F,
48         &&handle_OP_SUB_V,
49
50         &&handle_OP_EQ_F,
51         &&handle_OP_EQ_V,
52         &&handle_OP_EQ_S,
53         &&handle_OP_EQ_E,
54         &&handle_OP_EQ_FNC,
55
56         &&handle_OP_NE_F,
57         &&handle_OP_NE_V,
58         &&handle_OP_NE_S,
59         &&handle_OP_NE_E,
60         &&handle_OP_NE_FNC,
61
62         &&handle_OP_LE,
63         &&handle_OP_GE,
64         &&handle_OP_LT,
65         &&handle_OP_GT,
66
67         &&handle_OP_LOAD_F,
68         &&handle_OP_LOAD_V,
69         &&handle_OP_LOAD_S,
70         &&handle_OP_LOAD_ENT,
71         &&handle_OP_LOAD_FLD,
72         &&handle_OP_LOAD_FNC,
73
74         &&handle_OP_ADDRESS,
75
76         &&handle_OP_STORE_F,
77         &&handle_OP_STORE_V,
78         &&handle_OP_STORE_S,
79         &&handle_OP_STORE_ENT,
80         &&handle_OP_STORE_FLD,
81         &&handle_OP_STORE_FNC,
82
83         &&handle_OP_STOREP_F,
84         &&handle_OP_STOREP_V,
85         &&handle_OP_STOREP_S,
86         &&handle_OP_STOREP_ENT,
87         &&handle_OP_STOREP_FLD,
88         &&handle_OP_STOREP_FNC,
89
90         &&handle_OP_RETURN,
91         &&handle_OP_NOT_F,
92         &&handle_OP_NOT_V,
93         &&handle_OP_NOT_S,
94         &&handle_OP_NOT_ENT,
95         &&handle_OP_NOT_FNC,
96         &&handle_OP_IF,
97         &&handle_OP_IFNOT,
98         &&handle_OP_CALL0,
99         &&handle_OP_CALL1,
100         &&handle_OP_CALL2,
101         &&handle_OP_CALL3,
102         &&handle_OP_CALL4,
103         &&handle_OP_CALL5,
104         &&handle_OP_CALL6,
105         &&handle_OP_CALL7,
106         &&handle_OP_CALL8,
107         &&handle_OP_STATE,
108         &&handle_OP_GOTO,
109         &&handle_OP_AND,
110         &&handle_OP_OR,
111
112         &&handle_OP_BITAND,
113         &&handle_OP_BITOR
114             };
115 #define DISPATCH_OPCODE() \
116     goto *dispatchtable[(++st)->op]
117 #define HANDLE_OPCODE(opcode) handle_##opcode
118
119     DISPATCH_OPCODE(); // jump to first opcode
120 #else // USE_COMPUTED_GOTOS
121 #define DISPATCH_OPCODE() break
122 #define HANDLE_OPCODE(opcode) case opcode
123
124 #if PRVMSLOWINTERPRETER
125                 {
126                         if (prog->watch_global_type != ev_void)
127                         {
128                                 prvm_eval_t *g = PRVM_GLOBALFIELDVALUE(prog->watch_global);
129                                 prog->xstatement = st + 1 - cached_statements;
130                                 PRVM_Watchpoint(prog, 1, "Global watchpoint hit by engine", prog->watch_global_type, &prog->watch_global_value, g);
131                         }
132                         if (prog->watch_field_type != ev_void && prog->watch_edict < prog->max_edicts)
133                         {
134                                 prvm_eval_t *g = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
135                                 prog->xstatement = st + 1 - cached_statements;
136                                 PRVM_Watchpoint(prog, 1, "Entityfield watchpoint hit by engine", prog->watch_field_type, &prog->watch_edictfield_value, g);
137                         }
138                 }
139 #endif
140
141                 while (1)
142                 {
143                         st++;
144 #endif // USE_COMPUTED_GOTOS
145
146 #if !USE_COMPUTED_GOTOS
147
148 #if PRVMSLOWINTERPRETER
149                         if (prog->trace)
150                                 PRVM_PrintStatement(prog, st);
151                         if (prog->break_statement >= 0)
152                                 if ((st - cached_statements) == prog->break_statement)
153                                 {
154                                         prog->xstatement = st - cached_statements;
155                                         PRVM_Breakpoint(prog, prog->break_stack_index, "Breakpoint hit");
156                                 }
157 #endif
158                         switch (st->op)
159                         {
160 #endif
161                         HANDLE_OPCODE(OP_ADD_F):
162                                 OPC->_float = OPA->_float + OPB->_float;
163                                 DISPATCH_OPCODE();
164                         HANDLE_OPCODE(OP_ADD_V):
165                                 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
166                                 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
167                                 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
168                                 DISPATCH_OPCODE();
169                         HANDLE_OPCODE(OP_SUB_F):
170                                 OPC->_float = OPA->_float - OPB->_float;
171                                 DISPATCH_OPCODE();
172                         HANDLE_OPCODE(OP_SUB_V):
173                                 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
174                                 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
175                                 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
176                                 DISPATCH_OPCODE();
177                         HANDLE_OPCODE(OP_MUL_F):
178                                 OPC->_float = OPA->_float * OPB->_float;
179                                 DISPATCH_OPCODE();
180                         HANDLE_OPCODE(OP_MUL_V):
181                                 OPC->_float = OPA->vector[0]*OPB->vector[0] + OPA->vector[1]*OPB->vector[1] + OPA->vector[2]*OPB->vector[2];
182                                 DISPATCH_OPCODE();
183                         HANDLE_OPCODE(OP_MUL_FV):
184                                 tempfloat = OPA->_float;
185                                 OPC->vector[0] = tempfloat * OPB->vector[0];
186                                 OPC->vector[1] = tempfloat * OPB->vector[1];
187                                 OPC->vector[2] = tempfloat * OPB->vector[2];
188                                 DISPATCH_OPCODE();
189                         HANDLE_OPCODE(OP_MUL_VF):
190                                 tempfloat = OPB->_float;
191                                 OPC->vector[0] = tempfloat * OPA->vector[0];
192                                 OPC->vector[1] = tempfloat * OPA->vector[1];
193                                 OPC->vector[2] = tempfloat * OPA->vector[2];
194                                 DISPATCH_OPCODE();
195                         HANDLE_OPCODE(OP_DIV_F):
196                                 if( OPB->_float != 0.0f )
197                                 {
198                                         OPC->_float = OPA->_float / OPB->_float;
199                                 }
200                                 else
201                                 {
202                                         if (developer.integer)
203                                         {
204                                                 PRE_ERROR();
205                                                 VM_Warning(prog, "Attempted division by zero in %s\n", prog->name );
206                                         }
207                                         OPC->_float = 0.0f;
208                                 }
209                                 DISPATCH_OPCODE();
210                         HANDLE_OPCODE(OP_BITAND):
211                                 OPC->_float = (prvm_int_t)OPA->_float & (prvm_int_t)OPB->_float;
212                                 DISPATCH_OPCODE();
213                         HANDLE_OPCODE(OP_BITOR):
214                                 OPC->_float = (prvm_int_t)OPA->_float | (prvm_int_t)OPB->_float;
215                                 DISPATCH_OPCODE();
216                         HANDLE_OPCODE(OP_GE):
217                                 OPC->_float = OPA->_float >= OPB->_float;
218                                 DISPATCH_OPCODE();
219                         HANDLE_OPCODE(OP_LE):
220                                 OPC->_float = OPA->_float <= OPB->_float;
221                                 DISPATCH_OPCODE();
222                         HANDLE_OPCODE(OP_GT):
223                                 OPC->_float = OPA->_float > OPB->_float;
224                                 DISPATCH_OPCODE();
225                         HANDLE_OPCODE(OP_LT):
226                                 OPC->_float = OPA->_float < OPB->_float;
227                                 DISPATCH_OPCODE();
228                         HANDLE_OPCODE(OP_AND):
229                                 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) && FLOAT_IS_TRUE_FOR_INT(OPB->_int); // TODO change this back to float, and add AND_I to be used by fteqcc for anything not a float
230                                 DISPATCH_OPCODE();
231                         HANDLE_OPCODE(OP_OR):
232                                 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) || FLOAT_IS_TRUE_FOR_INT(OPB->_int); // TODO change this back to float, and add OR_I to be used by fteqcc for anything not a float
233                                 DISPATCH_OPCODE();
234                         HANDLE_OPCODE(OP_NOT_F):
235                                 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
236                                 DISPATCH_OPCODE();
237                         HANDLE_OPCODE(OP_NOT_V):
238                                 OPC->_float = !OPA->vector[0] && !OPA->vector[1] && !OPA->vector[2];
239                                 DISPATCH_OPCODE();
240                         HANDLE_OPCODE(OP_NOT_S):
241                                 OPC->_float = !OPA->string || !*PRVM_GetString(prog, OPA->string);
242                                 DISPATCH_OPCODE();
243                         HANDLE_OPCODE(OP_NOT_FNC):
244                                 OPC->_float = !OPA->function;
245                                 DISPATCH_OPCODE();
246                         HANDLE_OPCODE(OP_NOT_ENT):
247                                 OPC->_float = (OPA->edict == 0);
248                                 DISPATCH_OPCODE();
249                         HANDLE_OPCODE(OP_EQ_F):
250                                 OPC->_float = OPA->_float == OPB->_float;
251                                 DISPATCH_OPCODE();
252                         HANDLE_OPCODE(OP_EQ_V):
253                                 OPC->_float = (OPA->vector[0] == OPB->vector[0]) && (OPA->vector[1] == OPB->vector[1]) && (OPA->vector[2] == OPB->vector[2]);
254                                 DISPATCH_OPCODE();
255                         HANDLE_OPCODE(OP_EQ_S):
256                                 OPC->_float = !strcmp(PRVM_GetString(prog, OPA->string),PRVM_GetString(prog, OPB->string));
257                                 DISPATCH_OPCODE();
258                         HANDLE_OPCODE(OP_EQ_E):
259                                 OPC->_float = OPA->_int == OPB->_int;
260                                 DISPATCH_OPCODE();
261                         HANDLE_OPCODE(OP_EQ_FNC):
262                                 OPC->_float = OPA->function == OPB->function;
263                                 DISPATCH_OPCODE();
264                         HANDLE_OPCODE(OP_NE_F):
265                                 OPC->_float = OPA->_float != OPB->_float;
266                                 DISPATCH_OPCODE();
267                         HANDLE_OPCODE(OP_NE_V):
268                                 OPC->_float = (OPA->vector[0] != OPB->vector[0]) || (OPA->vector[1] != OPB->vector[1]) || (OPA->vector[2] != OPB->vector[2]);
269                                 DISPATCH_OPCODE();
270                         HANDLE_OPCODE(OP_NE_S):
271                                 OPC->_float = strcmp(PRVM_GetString(prog, OPA->string),PRVM_GetString(prog, OPB->string));
272                                 DISPATCH_OPCODE();
273                         HANDLE_OPCODE(OP_NE_E):
274                                 OPC->_float = OPA->_int != OPB->_int;
275                                 DISPATCH_OPCODE();
276                         HANDLE_OPCODE(OP_NE_FNC):
277                                 OPC->_float = OPA->function != OPB->function;
278                                 DISPATCH_OPCODE();
279
280                 //==================
281                         HANDLE_OPCODE(OP_STORE_F):
282                         HANDLE_OPCODE(OP_STORE_ENT):
283                         HANDLE_OPCODE(OP_STORE_FLD):            // integers
284                         HANDLE_OPCODE(OP_STORE_S):
285                         HANDLE_OPCODE(OP_STORE_FNC):            // pointers
286                                 OPB->_int = OPA->_int;
287                                 DISPATCH_OPCODE();
288                         HANDLE_OPCODE(OP_STORE_V):
289                                 OPB->ivector[0] = OPA->ivector[0];
290                                 OPB->ivector[1] = OPA->ivector[1];
291                                 OPB->ivector[2] = OPA->ivector[2];
292                                 DISPATCH_OPCODE();
293
294                         HANDLE_OPCODE(OP_STOREP_F):
295                         HANDLE_OPCODE(OP_STOREP_ENT):
296                         HANDLE_OPCODE(OP_STOREP_FLD):           // integers
297                         HANDLE_OPCODE(OP_STOREP_S):
298                         HANDLE_OPCODE(OP_STOREP_FNC):           // pointers
299                                 if ((prvm_uint_t)OPB->_int - cached_entityfields >= cached_entityfieldsarea_entityfields)
300                                 {
301                                         if ((prvm_uint_t)OPB->_int >= cached_entityfieldsarea)
302                                         {
303                                                 PRE_ERROR();
304                                                 prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
305                                                 goto cleanup;
306                                         }
307                                         if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
308                                         {
309                                                 PRE_ERROR();
310                                                 VM_Warning(prog, "assignment to world.%s (field %i) in %s\n", PRVM_GetString(prog, PRVM_ED_FieldAtOfs(prog, OPB->_int)->s_name), (int)OPB->_int, prog->name);
311                                         }
312                                 }
313                                 ptr = (prvm_eval_t *)(cached_edictsfields + OPB->_int);
314                                 ptr->_int = OPA->_int;
315                                 DISPATCH_OPCODE();
316                         HANDLE_OPCODE(OP_STOREP_V):
317                                 if ((prvm_uint_t)OPB->_int - cached_entityfields > (prvm_uint_t)cached_entityfieldsarea_entityfields_3)
318                                 {
319                                         if ((prvm_uint_t)OPB->_int > cached_entityfieldsarea_3)
320                                         {
321                                                 PRE_ERROR();
322                                                 prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
323                                                 goto cleanup;
324                                         }
325                                         if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
326                                         {
327                                                 PRE_ERROR();
328                                                 VM_Warning(prog, "assignment to world.%s (field %i) in %s\n", PRVM_GetString(prog, PRVM_ED_FieldAtOfs(prog, OPB->_int)->s_name), (int)OPB->_int, prog->name);
329                                         }
330                                 }
331                                 ptr = (prvm_eval_t *)(cached_edictsfields + OPB->_int);
332                                 ptr->ivector[0] = OPA->ivector[0];
333                                 ptr->ivector[1] = OPA->ivector[1];
334                                 ptr->ivector[2] = OPA->ivector[2];
335                                 DISPATCH_OPCODE();
336
337                         HANDLE_OPCODE(OP_ADDRESS):
338                                 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
339                                 {
340                                         PRE_ERROR();
341                                         prog->error_cmd("%s Progs attempted to address an out of bounds edict number", prog->name);
342                                         goto cleanup;
343                                 }
344                                 if ((prvm_uint_t)OPB->_int >= cached_entityfields)
345                                 {
346                                         PRE_ERROR();
347                                         prog->error_cmd("%s attempted to address an invalid field (%i) in an edict", prog->name, (int)OPB->_int);
348                                         goto cleanup;
349                                 }
350 #if 0
351                                 if (OPA->edict == 0 && !cached_allowworldwrites)
352                                 {
353                                         PRE_ERROR();
354                                         prog->error_cmd("forbidden assignment to null/world entity in %s", prog->name);
355                                         goto cleanup;
356                                 }
357 #endif
358                                 OPC->_int = OPA->edict * cached_entityfields + OPB->_int;
359                                 DISPATCH_OPCODE();
360
361                         HANDLE_OPCODE(OP_LOAD_F):
362                         HANDLE_OPCODE(OP_LOAD_FLD):
363                         HANDLE_OPCODE(OP_LOAD_ENT):
364                         HANDLE_OPCODE(OP_LOAD_S):
365                         HANDLE_OPCODE(OP_LOAD_FNC):
366                                 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
367                                 {
368                                         PRE_ERROR();
369                                         prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
370                                         goto cleanup;
371                                 }
372                                 if ((prvm_uint_t)OPB->_int >= cached_entityfields)
373                                 {
374                                         PRE_ERROR();
375                                         prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
376                                         goto cleanup;
377                                 }
378                                 ed = PRVM_PROG_TO_EDICT(OPA->edict);
379                                 OPC->_int = ((prvm_eval_t *)(ed->fields.ip + OPB->_int))->_int;
380                                 DISPATCH_OPCODE();
381
382                         HANDLE_OPCODE(OP_LOAD_V):
383                                 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
384                                 {
385                                         PRE_ERROR();
386                                         prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
387                                         goto cleanup;
388                                 }
389                                 if ((prvm_uint_t)OPB->_int > cached_entityfields_3)
390                                 {
391                                         PRE_ERROR();
392                                         prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
393                                         goto cleanup;
394                                 }
395                                 ed = PRVM_PROG_TO_EDICT(OPA->edict);
396                                 ptr = (prvm_eval_t *)(ed->fields.ip + OPB->_int);
397                                 OPC->ivector[0] = ptr->ivector[0];
398                                 OPC->ivector[1] = ptr->ivector[1];
399                                 OPC->ivector[2] = ptr->ivector[2];
400                                 DISPATCH_OPCODE();
401
402                 //==================
403
404                         HANDLE_OPCODE(OP_IFNOT):
405                                 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
406                                 // TODO add an "int-if", and change this one to OPA->_float
407                                 // although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
408                                 // and entity, string, field values can never have that value
409                                 {
410                                         ADVANCE_PROFILE_BEFORE_JUMP();
411                                         st = cached_statements + st->jumpabsolute - 1;  // offset the st++
412                                         startst = st;
413                                         // no bounds check needed, it is done when loading progs
414                                         if (++jumpcount == 10000000 && prvm_runawaycheck)
415                                         {
416                                                 prog->xstatement = st - cached_statements;
417                                                 PRVM_Profile(prog, 1<<30, 1000000, 0);
418                                                 prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
419                                         }
420                                 }
421                                 DISPATCH_OPCODE();
422
423                         HANDLE_OPCODE(OP_IF):
424                                 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
425                                 // TODO add an "int-if", and change this one, as well as the FLOAT_IS_TRUE_FOR_INT usages, to OPA->_float
426                                 // although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
427                                 // and entity, string, field values can never have that value
428                                 {
429                                         ADVANCE_PROFILE_BEFORE_JUMP();
430                                         st = cached_statements + st->jumpabsolute - 1;  // offset the st++
431                                         startst = st;
432                                         // no bounds check needed, it is done when loading progs
433                                         if (++jumpcount == 10000000 && prvm_runawaycheck)
434                                         {
435                                                 prog->xstatement = st - cached_statements;
436                                                 PRVM_Profile(prog, 1<<30, 0.01, 0);
437                                                 prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
438                                         }
439                                 }
440                                 DISPATCH_OPCODE();
441
442                         HANDLE_OPCODE(OP_GOTO):
443                                 ADVANCE_PROFILE_BEFORE_JUMP();
444                                 st = cached_statements + st->jumpabsolute - 1;  // offset the st++
445                                 startst = st;
446                                 // no bounds check needed, it is done when loading progs
447                                 if (++jumpcount == 10000000 && prvm_runawaycheck)
448                                 {
449                                         prog->xstatement = st - cached_statements;
450                                         PRVM_Profile(prog, 1<<30, 0.01, 0);
451                                         prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
452                                 }
453                                 DISPATCH_OPCODE();
454
455                         HANDLE_OPCODE(OP_CALL0):
456                         HANDLE_OPCODE(OP_CALL1):
457                         HANDLE_OPCODE(OP_CALL2):
458                         HANDLE_OPCODE(OP_CALL3):
459                         HANDLE_OPCODE(OP_CALL4):
460                         HANDLE_OPCODE(OP_CALL5):
461                         HANDLE_OPCODE(OP_CALL6):
462                         HANDLE_OPCODE(OP_CALL7):
463                         HANDLE_OPCODE(OP_CALL8):
464 #ifdef PRVMTIMEPROFILING 
465                                 tm = Sys_DirtyTime();
466                                 prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
467                                 starttm = tm;
468 #endif
469                                 ADVANCE_PROFILE_BEFORE_JUMP();
470                                 startst = st;
471                                 prog->xstatement = st - cached_statements;
472                                 prog->argc = st->op - OP_CALL0;
473                                 if (!OPA->function)
474                                 {
475                                         prog->error_cmd("NULL function in %s", prog->name);
476                                 }
477
478                                 if(!OPA->function || OPA->function < 0 || OPA->function >= prog->numfunctions)
479                                 {
480                                         PRE_ERROR();
481                                         prog->error_cmd("%s CALL outside the program", prog->name);
482                                         goto cleanup;
483                                 }
484
485                                 enterfunc = &prog->functions[OPA->function];
486                                 if (enterfunc->callcount++ == 0 && (prvm_coverage.integer & 1))
487                                         PRVM_FunctionCoverageEvent(prog, enterfunc);
488
489                                 if (enterfunc->first_statement < 0)
490                                 {
491                                         // negative first_statement values are built in functions
492                                         int builtinnumber = -enterfunc->first_statement;
493                                         prog->xfunction->builtinsprofile++;
494                                         if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
495                                         {
496                                                 prog->builtins[builtinnumber](prog);
497 #ifdef PRVMTIMEPROFILING 
498                                                 tm = Sys_DirtyTime();
499                                                 enterfunc->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
500                                                 prog->xfunction->tbprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
501                                                 starttm = tm;
502 #endif
503                                                 // builtins may cause ED_Alloc() to be called, update cached variables
504                                                 cached_edictsfields = prog->edictsfields;
505                                                 cached_entityfields = prog->entityfields;
506                                                 cached_entityfields_3 = prog->entityfields - 3;
507                                                 cached_entityfieldsarea = prog->entityfieldsarea;
508                                                 cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
509                                                 cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
510                                                 cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
511                                                 cached_max_edicts = prog->max_edicts;
512                                                 // these do not change
513                                                 //cached_statements = prog->statements;
514                                                 //cached_allowworldwrites = prog->allowworldwrites;
515                                                 //cached_flag = prog->flag;
516                                                 // if prog->trace changed we need to change interpreter path
517                                                 if (prog->trace != cachedpr_trace)
518                                                         goto chooseexecprogram;
519                                         }
520                                         else
521                                                 prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
522                                 }
523                                 else
524                                         st = cached_statements + PRVM_EnterFunction(prog, enterfunc);
525                                 startst = st;
526                                 DISPATCH_OPCODE();
527
528                         HANDLE_OPCODE(OP_DONE):
529                         HANDLE_OPCODE(OP_RETURN):
530 #ifdef PRVMTIMEPROFILING 
531                                 tm = Sys_DirtyTime();
532                                 prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
533                                 starttm = tm;
534 #endif
535                                 ADVANCE_PROFILE_BEFORE_JUMP();
536                                 prog->xstatement = st - cached_statements;
537
538                                 prog->globals.ip[OFS_RETURN  ] = prog->globals.ip[st->operand[0]  ];
539                                 prog->globals.ip[OFS_RETURN+1] = prog->globals.ip[st->operand[0]+1];
540                                 prog->globals.ip[OFS_RETURN+2] = prog->globals.ip[st->operand[0]+2];
541
542                                 st = cached_statements + PRVM_LeaveFunction(prog);
543                                 startst = st;
544                                 if (prog->depth <= exitdepth)
545                                         goto cleanup; // all done
546                                 DISPATCH_OPCODE();
547
548                         HANDLE_OPCODE(OP_STATE):
549                                 if(cached_flag & PRVM_OP_STATE)
550                                 {
551                                         ed = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
552                                         PRVM_gameedictfloat(ed,nextthink) = PRVM_gameglobalfloat(time) + 0.1;
553                                         PRVM_gameedictfloat(ed,frame) = OPA->_float;
554                                         PRVM_gameedictfunction(ed,think) = OPB->function;
555                                 }
556                                 else
557                                 {
558                                         PRE_ERROR();
559                                         prog->xstatement = st - cached_statements;
560                                         prog->error_cmd("OP_STATE not supported by %s", prog->name);
561                                 }
562                                 DISPATCH_OPCODE();
563
564 // LordHavoc: to be enabled when Progs version 7 (or whatever it will be numbered) is finalized
565 /*
566                         HANDLE_OPCODE(OP_ADD_I):
567                                 OPC->_int = OPA->_int + OPB->_int;
568                                 DISPATCH_OPCODE();
569                         HANDLE_OPCODE(OP_ADD_IF):
570                                 OPC->_int = OPA->_int + (prvm_int_t) OPB->_float;
571                                 DISPATCH_OPCODE();
572                         HANDLE_OPCODE(OP_ADD_FI):
573                                 OPC->_float = OPA->_float + (prvm_vec_t) OPB->_int;
574                                 DISPATCH_OPCODE();
575                         HANDLE_OPCODE(OP_SUB_I):
576                                 OPC->_int = OPA->_int - OPB->_int;
577                                 DISPATCH_OPCODE();
578                         HANDLE_OPCODE(OP_SUB_IF):
579                                 OPC->_int = OPA->_int - (prvm_int_t) OPB->_float;
580                                 DISPATCH_OPCODE();
581                         HANDLE_OPCODE(OP_SUB_FI):
582                                 OPC->_float = OPA->_float - (prvm_vec_t) OPB->_int;
583                                 DISPATCH_OPCODE();
584                         HANDLE_OPCODE(OP_MUL_I):
585                                 OPC->_int = OPA->_int * OPB->_int;
586                                 DISPATCH_OPCODE();
587                         HANDLE_OPCODE(OP_MUL_IF):
588                                 OPC->_int = OPA->_int * (prvm_int_t) OPB->_float;
589                                 DISPATCH_OPCODE();
590                         HANDLE_OPCODE(OP_MUL_FI):
591                                 OPC->_float = OPA->_float * (prvm_vec_t) OPB->_int;
592                                 DISPATCH_OPCODE();
593                         HANDLE_OPCODE(OP_MUL_VI):
594                                 OPC->vector[0] = (prvm_vec_t) OPB->_int * OPA->vector[0];
595                                 OPC->vector[1] = (prvm_vec_t) OPB->_int * OPA->vector[1];
596                                 OPC->vector[2] = (prvm_vec_t) OPB->_int * OPA->vector[2];
597                                 DISPATCH_OPCODE();
598                         HANDLE_OPCODE(OP_DIV_VF):
599                                 {
600                                         float temp = 1.0f / OPB->_float;
601                                         OPC->vector[0] = temp * OPA->vector[0];
602                                         OPC->vector[1] = temp * OPA->vector[1];
603                                         OPC->vector[2] = temp * OPA->vector[2];
604                                 }
605                                 DISPATCH_OPCODE();
606                         HANDLE_OPCODE(OP_DIV_I):
607                                 OPC->_int = OPA->_int / OPB->_int;
608                                 DISPATCH_OPCODE();
609                         HANDLE_OPCODE(OP_DIV_IF):
610                                 OPC->_int = OPA->_int / (prvm_int_t) OPB->_float;
611                                 DISPATCH_OPCODE();
612                         HANDLE_OPCODE(OP_DIV_FI):
613                                 OPC->_float = OPA->_float / (prvm_vec_t) OPB->_int;
614                                 DISPATCH_OPCODE();
615                         HANDLE_OPCODE(OP_CONV_IF):
616                                 OPC->_float = OPA->_int;
617                                 DISPATCH_OPCODE();
618                         HANDLE_OPCODE(OP_CONV_FI):
619                                 OPC->_int = OPA->_float;
620                                 DISPATCH_OPCODE();
621                         HANDLE_OPCODE(OP_BITAND_I):
622                                 OPC->_int = OPA->_int & OPB->_int;
623                                 DISPATCH_OPCODE();
624                         HANDLE_OPCODE(OP_BITOR_I):
625                                 OPC->_int = OPA->_int | OPB->_int;
626                                 DISPATCH_OPCODE();
627                         HANDLE_OPCODE(OP_BITAND_IF):
628                                 OPC->_int = OPA->_int & (prvm_int_t)OPB->_float;
629                                 DISPATCH_OPCODE();
630                         HANDLE_OPCODE(OP_BITOR_IF):
631                                 OPC->_int = OPA->_int | (prvm_int_t)OPB->_float;
632                                 DISPATCH_OPCODE();
633                         HANDLE_OPCODE(OP_BITAND_FI):
634                                 OPC->_float = (prvm_int_t)OPA->_float & OPB->_int;
635                                 DISPATCH_OPCODE();
636                         HANDLE_OPCODE(OP_BITOR_FI):
637                                 OPC->_float = (prvm_int_t)OPA->_float | OPB->_int;
638                                 DISPATCH_OPCODE();
639                         HANDLE_OPCODE(OP_GE_I):
640                                 OPC->_float = OPA->_int >= OPB->_int;
641                                 DISPATCH_OPCODE();
642                         HANDLE_OPCODE(OP_LE_I):
643                                 OPC->_float = OPA->_int <= OPB->_int;
644                                 DISPATCH_OPCODE();
645                         HANDLE_OPCODE(OP_GT_I):
646                                 OPC->_float = OPA->_int > OPB->_int;
647                                 DISPATCH_OPCODE();
648                         HANDLE_OPCODE(OP_LT_I):
649                                 OPC->_float = OPA->_int < OPB->_int;
650                                 DISPATCH_OPCODE();
651                         HANDLE_OPCODE(OP_AND_I):
652                                 OPC->_float = OPA->_int && OPB->_int;
653                                 DISPATCH_OPCODE();
654                         HANDLE_OPCODE(OP_OR_I):
655                                 OPC->_float = OPA->_int || OPB->_int;
656                                 DISPATCH_OPCODE();
657                         HANDLE_OPCODE(OP_GE_IF):
658                                 OPC->_float = (prvm_vec_t)OPA->_int >= OPB->_float;
659                                 DISPATCH_OPCODE();
660                         HANDLE_OPCODE(OP_LE_IF):
661                                 OPC->_float = (prvm_vec_t)OPA->_int <= OPB->_float;
662                                 DISPATCH_OPCODE();
663                         HANDLE_OPCODE(OP_GT_IF):
664                                 OPC->_float = (prvm_vec_t)OPA->_int > OPB->_float;
665                                 DISPATCH_OPCODE();
666                         HANDLE_OPCODE(OP_LT_IF):
667                                 OPC->_float = (prvm_vec_t)OPA->_int < OPB->_float;
668                                 DISPATCH_OPCODE();
669                         HANDLE_OPCODE(OP_AND_IF):
670                                 OPC->_float = (prvm_vec_t)OPA->_int && OPB->_float;
671                                 DISPATCH_OPCODE();
672                         HANDLE_OPCODE(OP_OR_IF):
673                                 OPC->_float = (prvm_vec_t)OPA->_int || OPB->_float;
674                                 DISPATCH_OPCODE();
675                         HANDLE_OPCODE(OP_GE_FI):
676                                 OPC->_float = OPA->_float >= (prvm_vec_t)OPB->_int;
677                                 DISPATCH_OPCODE();
678                         HANDLE_OPCODE(OP_LE_FI):
679                                 OPC->_float = OPA->_float <= (prvm_vec_t)OPB->_int;
680                                 DISPATCH_OPCODE();
681                         HANDLE_OPCODE(OP_GT_FI):
682                                 OPC->_float = OPA->_float > (prvm_vec_t)OPB->_int;
683                                 DISPATCH_OPCODE();
684                         HANDLE_OPCODE(OP_LT_FI):
685                                 OPC->_float = OPA->_float < (prvm_vec_t)OPB->_int;
686                                 DISPATCH_OPCODE();
687                         HANDLE_OPCODE(OP_AND_FI):
688                                 OPC->_float = OPA->_float && (prvm_vec_t)OPB->_int;
689                                 DISPATCH_OPCODE();
690                         HANDLE_OPCODE(OP_OR_FI):
691                                 OPC->_float = OPA->_float || (prvm_vec_t)OPB->_int;
692                                 DISPATCH_OPCODE();
693                         HANDLE_OPCODE(OP_NOT_I):
694                                 OPC->_float = !OPA->_int;
695                                 DISPATCH_OPCODE();
696                         HANDLE_OPCODE(OP_EQ_I):
697                                 OPC->_float = OPA->_int == OPB->_int;
698                                 DISPATCH_OPCODE();
699                         HANDLE_OPCODE(OP_EQ_IF):
700                                 OPC->_float = (prvm_vec_t)OPA->_int == OPB->_float;
701                                 DISPATCH_OPCODE();
702                         HANDLE_OPCODE(OP_EQ_FI):
703                                 OPC->_float = OPA->_float == (prvm_vec_t)OPB->_int;
704                                 DISPATCH_OPCODE();
705                         HANDLE_OPCODE(OP_NE_I):
706                                 OPC->_float = OPA->_int != OPB->_int;
707                                 DISPATCH_OPCODE();
708                         HANDLE_OPCODE(OP_NE_IF):
709                                 OPC->_float = (prvm_vec_t)OPA->_int != OPB->_float;
710                                 DISPATCH_OPCODE();
711                         HANDLE_OPCODE(OP_NE_FI):
712                                 OPC->_float = OPA->_float != (prvm_vec_t)OPB->_int;
713                                 DISPATCH_OPCODE();
714                         HANDLE_OPCODE(OP_STORE_I):
715                                 OPB->_int = OPA->_int;
716                                 DISPATCH_OPCODE();
717                         HANDLE_OPCODE(OP_STOREP_I):
718 #if PRBOUNDSCHECK
719                                 if (OPB->_int < 0 || OPB->_int + 4 > pr_edictareasize)
720                                 {
721                                         PRE_ERROR();
722                                         prog->error_cmd("%s Progs attempted to write to an out of bounds edict", prog->name);
723                                         goto cleanup;
724                                 }
725 #endif
726                                 ptr = (prvm_eval_t *)(prog->edictsfields + OPB->_int);
727                                 ptr->_int = OPA->_int;
728                                 DISPATCH_OPCODE();
729                         HANDLE_OPCODE(OP_LOAD_I):
730 #if PRBOUNDSCHECK
731                                 if (OPA->edict < 0 || OPA->edict >= prog->max_edicts)
732                                 {
733                                         PRE_ERROR();
734                                         prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
735                                         goto cleanup;
736                                 }
737                                 if (OPB->_int < 0 || OPB->_int >= progs->entityfields)
738                                 {
739                                         PRE_ERROR();
740                                         prog->error_cmd("%s Progs attempted to read an invalid field in an edict", prog->name);
741                                         goto cleanup;
742                                 }
743 #endif
744                                 ed = PRVM_PROG_TO_EDICT(OPA->edict);
745                                 OPC->_int = ((prvm_eval_t *)((int *)ed->v + OPB->_int))->_int;
746                                 DISPATCH_OPCODE();
747
748                         HANDLE_OPCODE(OP_GSTOREP_I):
749                         HANDLE_OPCODE(OP_GSTOREP_F):
750                         HANDLE_OPCODE(OP_GSTOREP_ENT):
751                         HANDLE_OPCODE(OP_GSTOREP_FLD):          // integers
752                         HANDLE_OPCODE(OP_GSTOREP_S):
753                         HANDLE_OPCODE(OP_GSTOREP_FNC):          // pointers
754 #if PRBOUNDSCHECK
755                                 if (OPB->_int < 0 || OPB->_int >= pr_globaldefs)
756                                 {
757                                         PRE_ERROR();
758                                         prog->error_cmd("%s Progs attempted to write to an invalid indexed global", prog->name);
759                                         goto cleanup;
760                                 }
761 #endif
762                                 pr_iglobals[OPB->_int] = OPA->_int;
763                                 DISPATCH_OPCODE();
764                         HANDLE_OPCODE(OP_GSTOREP_V):
765 #if PRBOUNDSCHECK
766                                 if (OPB->_int < 0 || OPB->_int + 2 >= pr_globaldefs)
767                                 {
768                                         PRE_ERROR();
769                                         prog->error_cmd("%s Progs attempted to write to an invalid indexed global", prog->name);
770                                         goto cleanup;
771                                 }
772 #endif
773                                 pr_iglobals[OPB->_int  ] = OPA->ivector[0];
774                                 pr_iglobals[OPB->_int+1] = OPA->ivector[1];
775                                 pr_iglobals[OPB->_int+2] = OPA->ivector[2];
776                                 DISPATCH_OPCODE();
777
778                         HANDLE_OPCODE(OP_GADDRESS):
779                                 i = OPA->_int + (prvm_int_t) OPB->_float;
780 #if PRBOUNDSCHECK
781                                 if (i < 0 || i >= pr_globaldefs)
782                                 {
783                                         PRE_ERROR();
784                                         prog->error_cmd("%s Progs attempted to address an out of bounds global", prog->name);
785                                         goto cleanup;
786                                 }
787 #endif
788                                 OPC->_int = pr_iglobals[i];
789                                 DISPATCH_OPCODE();
790
791                         HANDLE_OPCODE(OP_GLOAD_I):
792                         HANDLE_OPCODE(OP_GLOAD_F):
793                         HANDLE_OPCODE(OP_GLOAD_FLD):
794                         HANDLE_OPCODE(OP_GLOAD_ENT):
795                         HANDLE_OPCODE(OP_GLOAD_S):
796                         HANDLE_OPCODE(OP_GLOAD_FNC):
797 #if PRBOUNDSCHECK
798                                 if (OPA->_int < 0 || OPA->_int >= pr_globaldefs)
799                                 {
800                                         PRE_ERROR();
801                                         prog->error_cmd("%s Progs attempted to read an invalid indexed global", prog->name);
802                                         goto cleanup;
803                                 }
804 #endif
805                                 OPC->_int = pr_iglobals[OPA->_int];
806                                 DISPATCH_OPCODE();
807
808                         HANDLE_OPCODE(OP_GLOAD_V):
809 #if PRBOUNDSCHECK
810                                 if (OPA->_int < 0 || OPA->_int + 2 >= pr_globaldefs)
811                                 {
812                                         PRE_ERROR();
813                                         prog->error_cmd("%s Progs attempted to read an invalid indexed global", prog->name);
814                                         goto cleanup;
815                                 }
816 #endif
817                                 OPC->ivector[0] = pr_iglobals[OPA->_int  ];
818                                 OPC->ivector[1] = pr_iglobals[OPA->_int+1];
819                                 OPC->ivector[2] = pr_iglobals[OPA->_int+2];
820                                 DISPATCH_OPCODE();
821
822                         HANDLE_OPCODE(OP_BOUNDCHECK):
823                                 if (OPA->_int < 0 || OPA->_int >= st->b)
824                                 {
825                                         PRE_ERROR();
826                                         prog->error_cmd("%s Progs boundcheck failed at line number %d, value is < 0 or >= %d", prog->name, st->b, st->c);
827                                         goto cleanup;
828                                 }
829                                 DISPATCH_OPCODE();
830
831 */
832
833 #if !USE_COMPUTED_GOTOS
834                         default:
835                                 PRE_ERROR();
836                                 prog->error_cmd("Bad opcode %i in %s", st->op, prog->name);
837                                 goto cleanup;
838                         }
839 #if PRVMSLOWINTERPRETER
840                         {
841                                 if (prog->watch_global_type != ev_void)
842                                 {
843                                         prvm_eval_t *g = PRVM_GLOBALFIELDVALUE(prog->watch_global);
844                                         prog->xstatement = st - cached_statements;
845                                         PRVM_Watchpoint(prog, 0, "Global watchpoint hit", prog->watch_global_type, &prog->watch_global_value, g);
846                                 }
847                                 if (prog->watch_field_type != ev_void && prog->watch_edict < prog->max_edicts)
848                                 {
849                                         prvm_eval_t *g = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
850                                         prog->xstatement = st - cached_statements;
851                                         PRVM_Watchpoint(prog, 0, "Entityfield watchpoint hit", prog->watch_field_type, &prog->watch_edictfield_value, g);
852                                 }
853                         }
854 #endif
855                 }
856 #endif // !USE_COMPUTED_GOTOS
857
858 #undef DISPATCH_OPCODE
859 #undef HANDLE_OPCODE
860 #undef USE_COMPUTED_GOTOS
861 #undef PRE_ERROR
862 #undef ADVANCE_PROFILE_BEFORE_JUMP