]> git.xonotic.org Git - xonotic/darkplaces.git/blob - prvm_execprogram.h
Implemented garbage collection of lost references to strzone strings in the PRVM...
[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_FNC):            // pointers
285                                 OPB->_int = OPA->_int;
286                                 DISPATCH_OPCODE();
287                         HANDLE_OPCODE(OP_STORE_S):
288                                 // refresh the garbage collection on the string - this guards
289                                 // against a certain sort of repeated migration to earlier
290                                 // points in the scan that could otherwise result in the string
291                                 // being freed for being unused
292                                 PRVM_GetString(prog, OPA->_int);
293                                 OPB->_int = OPA->_int;
294                         DISPATCH_OPCODE();
295                         HANDLE_OPCODE(OP_STORE_V):
296                                 OPB->ivector[0] = OPA->ivector[0];
297                                 OPB->ivector[1] = OPA->ivector[1];
298                                 OPB->ivector[2] = OPA->ivector[2];
299                                 DISPATCH_OPCODE();
300
301                         HANDLE_OPCODE(OP_STOREP_F):
302                         HANDLE_OPCODE(OP_STOREP_ENT):
303                         HANDLE_OPCODE(OP_STOREP_FLD):           // integers
304                         HANDLE_OPCODE(OP_STOREP_FNC):           // pointers
305                                 if ((prvm_uint_t)OPB->_int - cached_entityfields >= cached_entityfieldsarea_entityfields)
306                                 {
307                                         if ((prvm_uint_t)OPB->_int >= cached_entityfieldsarea)
308                                         {
309                                                 PRE_ERROR();
310                                                 prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
311                                                 goto cleanup;
312                                         }
313                                         if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
314                                         {
315                                                 PRE_ERROR();
316                                                 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);
317                                         }
318                                 }
319                                 ptr = (prvm_eval_t *)(cached_edictsfields + OPB->_int);
320                                 ptr->_int = OPA->_int;
321                                 DISPATCH_OPCODE();
322                         HANDLE_OPCODE(OP_STOREP_S):
323                                 if ((prvm_uint_t)OPB->_int - cached_entityfields >= cached_entityfieldsarea_entityfields)
324                                 {
325                                         if ((prvm_uint_t)OPB->_int >= cached_entityfieldsarea)
326                                         {
327                                                 PRE_ERROR();
328                                                 prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
329                                                 goto cleanup;
330                                         }
331                                         if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
332                                         {
333                                                 PRE_ERROR();
334                                                 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);
335                                         }
336                                 }
337                                 // refresh the garbage collection on the string - this guards
338                                 // against a certain sort of repeated migration to earlier
339                                 // points in the scan that could otherwise result in the string
340                                 // being freed for being unused
341                                 PRVM_GetString(prog, OPA->_int);
342                                 ptr = (prvm_eval_t *)(cached_edictsfields + OPB->_int);
343                                 ptr->_int = OPA->_int;
344                                 DISPATCH_OPCODE();
345                         HANDLE_OPCODE(OP_STOREP_V):
346                                 if ((prvm_uint_t)OPB->_int - cached_entityfields > (prvm_uint_t)cached_entityfieldsarea_entityfields_3)
347                                 {
348                                         if ((prvm_uint_t)OPB->_int > cached_entityfieldsarea_3)
349                                         {
350                                                 PRE_ERROR();
351                                                 prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
352                                                 goto cleanup;
353                                         }
354                                         if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
355                                         {
356                                                 PRE_ERROR();
357                                                 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);
358                                         }
359                                 }
360                                 ptr = (prvm_eval_t *)(cached_edictsfields + OPB->_int);
361                                 ptr->ivector[0] = OPA->ivector[0];
362                                 ptr->ivector[1] = OPA->ivector[1];
363                                 ptr->ivector[2] = OPA->ivector[2];
364                                 DISPATCH_OPCODE();
365
366                         HANDLE_OPCODE(OP_ADDRESS):
367                                 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
368                                 {
369                                         PRE_ERROR();
370                                         prog->error_cmd("%s Progs attempted to address an out of bounds edict number", prog->name);
371                                         goto cleanup;
372                                 }
373                                 if ((prvm_uint_t)OPB->_int >= cached_entityfields)
374                                 {
375                                         PRE_ERROR();
376                                         prog->error_cmd("%s attempted to address an invalid field (%i) in an edict", prog->name, (int)OPB->_int);
377                                         goto cleanup;
378                                 }
379 #if 0
380                                 if (OPA->edict == 0 && !cached_allowworldwrites)
381                                 {
382                                         PRE_ERROR();
383                                         prog->error_cmd("forbidden assignment to null/world entity in %s", prog->name);
384                                         goto cleanup;
385                                 }
386 #endif
387                                 OPC->_int = OPA->edict * cached_entityfields + OPB->_int;
388                                 DISPATCH_OPCODE();
389
390                         HANDLE_OPCODE(OP_LOAD_F):
391                         HANDLE_OPCODE(OP_LOAD_FLD):
392                         HANDLE_OPCODE(OP_LOAD_ENT):
393                         HANDLE_OPCODE(OP_LOAD_FNC):
394                                 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
395                                 {
396                                         PRE_ERROR();
397                                         prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
398                                         goto cleanup;
399                                 }
400                                 if ((prvm_uint_t)OPB->_int >= cached_entityfields)
401                                 {
402                                         PRE_ERROR();
403                                         prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
404                                         goto cleanup;
405                                 }
406                                 ed = PRVM_PROG_TO_EDICT(OPA->edict);
407                                 OPC->_int = ((prvm_eval_t *)(ed->fields.ip + OPB->_int))->_int;
408                                 DISPATCH_OPCODE();
409                         HANDLE_OPCODE(OP_LOAD_S):
410                                 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
411                                 {
412                                         PRE_ERROR();
413                                         prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
414                                         goto cleanup;
415                                 }
416                                 if ((prvm_uint_t)OPB->_int >= cached_entityfields)
417                                 {
418                                         PRE_ERROR();
419                                         prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
420                                         goto cleanup;
421                                 }
422                                 ed = PRVM_PROG_TO_EDICT(OPA->edict);
423                                 OPC->_int = ((prvm_eval_t *)(ed->fields.ip + OPB->_int))->_int;
424                                 // refresh the garbage collection on the string - this guards
425                                 // against a certain sort of repeated migration to earlier
426                                 // points in the scan that could otherwise result in the string
427                                 // being freed for being unused
428                                 PRVM_GetString(prog, OPC->_int);
429                                 DISPATCH_OPCODE();
430
431                         HANDLE_OPCODE(OP_LOAD_V):
432                                 if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
433                                 {
434                                         PRE_ERROR();
435                                         prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
436                                         goto cleanup;
437                                 }
438                                 if ((prvm_uint_t)OPB->_int > cached_entityfields_3)
439                                 {
440                                         PRE_ERROR();
441                                         prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
442                                         goto cleanup;
443                                 }
444                                 ed = PRVM_PROG_TO_EDICT(OPA->edict);
445                                 ptr = (prvm_eval_t *)(ed->fields.ip + OPB->_int);
446                                 OPC->ivector[0] = ptr->ivector[0];
447                                 OPC->ivector[1] = ptr->ivector[1];
448                                 OPC->ivector[2] = ptr->ivector[2];
449                                 DISPATCH_OPCODE();
450
451                 //==================
452
453                         HANDLE_OPCODE(OP_IFNOT):
454                                 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
455                                 // TODO add an "int-if", and change this one to OPA->_float
456                                 // although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
457                                 // and entity, string, field values can never have that value
458                                 {
459                                         ADVANCE_PROFILE_BEFORE_JUMP();
460                                         st = cached_statements + st->jumpabsolute - 1;  // offset the st++
461                                         startst = st;
462                                         // no bounds check needed, it is done when loading progs
463                                         if (++jumpcount == 10000000 && prvm_runawaycheck)
464                                         {
465                                                 prog->xstatement = st - cached_statements;
466                                                 PRVM_Profile(prog, 1<<30, 1000000, 0);
467                                                 prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
468                                         }
469                                 }
470                                 DISPATCH_OPCODE();
471
472                         HANDLE_OPCODE(OP_IF):
473                                 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
474                                 // TODO add an "int-if", and change this one, as well as the FLOAT_IS_TRUE_FOR_INT usages, to OPA->_float
475                                 // although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
476                                 // and entity, string, field values can never have that value
477                                 {
478                                         ADVANCE_PROFILE_BEFORE_JUMP();
479                                         st = cached_statements + st->jumpabsolute - 1;  // offset the st++
480                                         startst = st;
481                                         // no bounds check needed, it is done when loading progs
482                                         if (++jumpcount == 10000000 && prvm_runawaycheck)
483                                         {
484                                                 prog->xstatement = st - cached_statements;
485                                                 PRVM_Profile(prog, 1<<30, 0.01, 0);
486                                                 prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
487                                         }
488                                 }
489                                 DISPATCH_OPCODE();
490
491                         HANDLE_OPCODE(OP_GOTO):
492                                 ADVANCE_PROFILE_BEFORE_JUMP();
493                                 st = cached_statements + st->jumpabsolute - 1;  // offset the st++
494                                 startst = st;
495                                 // no bounds check needed, it is done when loading progs
496                                 if (++jumpcount == 10000000 && prvm_runawaycheck)
497                                 {
498                                         prog->xstatement = st - cached_statements;
499                                         PRVM_Profile(prog, 1<<30, 0.01, 0);
500                                         prog->error_cmd("%s runaway loop counter hit limit of %d jumps\ntip: read above for list of most-executed functions", prog->name, jumpcount);
501                                 }
502                                 DISPATCH_OPCODE();
503
504                         HANDLE_OPCODE(OP_CALL0):
505                         HANDLE_OPCODE(OP_CALL1):
506                         HANDLE_OPCODE(OP_CALL2):
507                         HANDLE_OPCODE(OP_CALL3):
508                         HANDLE_OPCODE(OP_CALL4):
509                         HANDLE_OPCODE(OP_CALL5):
510                         HANDLE_OPCODE(OP_CALL6):
511                         HANDLE_OPCODE(OP_CALL7):
512                         HANDLE_OPCODE(OP_CALL8):
513 #ifdef PRVMTIMEPROFILING 
514                                 tm = Sys_DirtyTime();
515                                 prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
516                                 starttm = tm;
517 #endif
518                                 ADVANCE_PROFILE_BEFORE_JUMP();
519                                 startst = st;
520                                 prog->xstatement = st - cached_statements;
521                                 prog->argc = st->op - OP_CALL0;
522                                 if (!OPA->function)
523                                 {
524                                         prog->error_cmd("NULL function in %s", prog->name);
525                                 }
526
527                                 if(!OPA->function || OPA->function < 0 || OPA->function >= prog->numfunctions)
528                                 {
529                                         PRE_ERROR();
530                                         prog->error_cmd("%s CALL outside the program", prog->name);
531                                         goto cleanup;
532                                 }
533
534                                 enterfunc = &prog->functions[OPA->function];
535                                 if (enterfunc->callcount++ == 0 && (prvm_coverage.integer & 1))
536                                         PRVM_FunctionCoverageEvent(prog, enterfunc);
537
538                                 if (enterfunc->first_statement < 0)
539                                 {
540                                         // negative first_statement values are built in functions
541                                         int builtinnumber = -enterfunc->first_statement;
542                                         prog->xfunction->builtinsprofile++;
543                                         if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
544                                         {
545                                                 prog->builtins[builtinnumber](prog);
546 #ifdef PRVMTIMEPROFILING 
547                                                 tm = Sys_DirtyTime();
548                                                 enterfunc->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
549                                                 prog->xfunction->tbprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
550                                                 starttm = tm;
551 #endif
552                                                 // builtins may cause ED_Alloc() to be called, update cached variables
553                                                 cached_edictsfields = prog->edictsfields.fp;
554                                                 cached_entityfields = prog->entityfields;
555                                                 cached_entityfields_3 = prog->entityfields - 3;
556                                                 cached_entityfieldsarea = prog->entityfieldsarea;
557                                                 cached_entityfieldsarea_entityfields = prog->entityfieldsarea - prog->entityfields;
558                                                 cached_entityfieldsarea_3 = prog->entityfieldsarea - 3;
559                                                 cached_entityfieldsarea_entityfields_3 = prog->entityfieldsarea - prog->entityfields - 3;
560                                                 cached_max_edicts = prog->max_edicts;
561                                                 // these do not change
562                                                 //cached_statements = prog->statements;
563                                                 //cached_allowworldwrites = prog->allowworldwrites;
564                                                 //cached_flag = prog->flag;
565                                                 // if prog->trace changed we need to change interpreter path
566                                                 if (prog->trace != cachedpr_trace)
567                                                         goto chooseexecprogram;
568                                         }
569                                         else
570                                                 prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
571                                 }
572                                 else
573                                         st = cached_statements + PRVM_EnterFunction(prog, enterfunc);
574                                 startst = st;
575                                 DISPATCH_OPCODE();
576
577                         HANDLE_OPCODE(OP_DONE):
578                         HANDLE_OPCODE(OP_RETURN):
579 #ifdef PRVMTIMEPROFILING 
580                                 tm = Sys_DirtyTime();
581                                 prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
582                                 starttm = tm;
583 #endif
584                                 ADVANCE_PROFILE_BEFORE_JUMP();
585                                 prog->xstatement = st - cached_statements;
586
587                                 prog->globals.ip[OFS_RETURN  ] = prog->globals.ip[st->operand[0]  ];
588                                 prog->globals.ip[OFS_RETURN+1] = prog->globals.ip[st->operand[0]+1];
589                                 prog->globals.ip[OFS_RETURN+2] = prog->globals.ip[st->operand[0]+2];
590
591                                 st = cached_statements + PRVM_LeaveFunction(prog);
592                                 startst = st;
593                                 if (prog->depth <= exitdepth)
594                                         goto cleanup; // all done
595                                 DISPATCH_OPCODE();
596
597                         HANDLE_OPCODE(OP_STATE):
598                                 if(cached_flag & PRVM_OP_STATE)
599                                 {
600                                         ed = PRVM_PROG_TO_EDICT(PRVM_gameglobaledict(self));
601                                         PRVM_gameedictfloat(ed,nextthink) = PRVM_gameglobalfloat(time) + 0.1;
602                                         PRVM_gameedictfloat(ed,frame) = OPA->_float;
603                                         PRVM_gameedictfunction(ed,think) = OPB->function;
604                                 }
605                                 else
606                                 {
607                                         PRE_ERROR();
608                                         prog->xstatement = st - cached_statements;
609                                         prog->error_cmd("OP_STATE not supported by %s", prog->name);
610                                 }
611                                 DISPATCH_OPCODE();
612
613 // LadyHavoc: to be enabled when Progs version 7 (or whatever it will be numbered) is finalized
614 /*
615                         HANDLE_OPCODE(OP_ADD_I):
616                                 OPC->_int = OPA->_int + OPB->_int;
617                                 DISPATCH_OPCODE();
618                         HANDLE_OPCODE(OP_ADD_IF):
619                                 OPC->_int = OPA->_int + (prvm_int_t) OPB->_float;
620                                 DISPATCH_OPCODE();
621                         HANDLE_OPCODE(OP_ADD_FI):
622                                 OPC->_float = OPA->_float + (prvm_vec_t) OPB->_int;
623                                 DISPATCH_OPCODE();
624                         HANDLE_OPCODE(OP_SUB_I):
625                                 OPC->_int = OPA->_int - OPB->_int;
626                                 DISPATCH_OPCODE();
627                         HANDLE_OPCODE(OP_SUB_IF):
628                                 OPC->_int = OPA->_int - (prvm_int_t) OPB->_float;
629                                 DISPATCH_OPCODE();
630                         HANDLE_OPCODE(OP_SUB_FI):
631                                 OPC->_float = OPA->_float - (prvm_vec_t) OPB->_int;
632                                 DISPATCH_OPCODE();
633                         HANDLE_OPCODE(OP_MUL_I):
634                                 OPC->_int = OPA->_int * OPB->_int;
635                                 DISPATCH_OPCODE();
636                         HANDLE_OPCODE(OP_MUL_IF):
637                                 OPC->_int = OPA->_int * (prvm_int_t) OPB->_float;
638                                 DISPATCH_OPCODE();
639                         HANDLE_OPCODE(OP_MUL_FI):
640                                 OPC->_float = OPA->_float * (prvm_vec_t) OPB->_int;
641                                 DISPATCH_OPCODE();
642                         HANDLE_OPCODE(OP_MUL_VI):
643                                 OPC->vector[0] = (prvm_vec_t) OPB->_int * OPA->vector[0];
644                                 OPC->vector[1] = (prvm_vec_t) OPB->_int * OPA->vector[1];
645                                 OPC->vector[2] = (prvm_vec_t) OPB->_int * OPA->vector[2];
646                                 DISPATCH_OPCODE();
647                         HANDLE_OPCODE(OP_DIV_VF):
648                                 {
649                                         float temp = 1.0f / OPB->_float;
650                                         OPC->vector[0] = temp * OPA->vector[0];
651                                         OPC->vector[1] = temp * OPA->vector[1];
652                                         OPC->vector[2] = temp * OPA->vector[2];
653                                 }
654                                 DISPATCH_OPCODE();
655                         HANDLE_OPCODE(OP_DIV_I):
656                                 OPC->_int = OPA->_int / OPB->_int;
657                                 DISPATCH_OPCODE();
658                         HANDLE_OPCODE(OP_DIV_IF):
659                                 OPC->_int = OPA->_int / (prvm_int_t) OPB->_float;
660                                 DISPATCH_OPCODE();
661                         HANDLE_OPCODE(OP_DIV_FI):
662                                 OPC->_float = OPA->_float / (prvm_vec_t) OPB->_int;
663                                 DISPATCH_OPCODE();
664                         HANDLE_OPCODE(OP_CONV_IF):
665                                 OPC->_float = OPA->_int;
666                                 DISPATCH_OPCODE();
667                         HANDLE_OPCODE(OP_CONV_FI):
668                                 OPC->_int = OPA->_float;
669                                 DISPATCH_OPCODE();
670                         HANDLE_OPCODE(OP_BITAND_I):
671                                 OPC->_int = OPA->_int & OPB->_int;
672                                 DISPATCH_OPCODE();
673                         HANDLE_OPCODE(OP_BITOR_I):
674                                 OPC->_int = OPA->_int | OPB->_int;
675                                 DISPATCH_OPCODE();
676                         HANDLE_OPCODE(OP_BITAND_IF):
677                                 OPC->_int = OPA->_int & (prvm_int_t)OPB->_float;
678                                 DISPATCH_OPCODE();
679                         HANDLE_OPCODE(OP_BITOR_IF):
680                                 OPC->_int = OPA->_int | (prvm_int_t)OPB->_float;
681                                 DISPATCH_OPCODE();
682                         HANDLE_OPCODE(OP_BITAND_FI):
683                                 OPC->_float = (prvm_int_t)OPA->_float & OPB->_int;
684                                 DISPATCH_OPCODE();
685                         HANDLE_OPCODE(OP_BITOR_FI):
686                                 OPC->_float = (prvm_int_t)OPA->_float | OPB->_int;
687                                 DISPATCH_OPCODE();
688                         HANDLE_OPCODE(OP_GE_I):
689                                 OPC->_float = OPA->_int >= OPB->_int;
690                                 DISPATCH_OPCODE();
691                         HANDLE_OPCODE(OP_LE_I):
692                                 OPC->_float = OPA->_int <= OPB->_int;
693                                 DISPATCH_OPCODE();
694                         HANDLE_OPCODE(OP_GT_I):
695                                 OPC->_float = OPA->_int > OPB->_int;
696                                 DISPATCH_OPCODE();
697                         HANDLE_OPCODE(OP_LT_I):
698                                 OPC->_float = OPA->_int < OPB->_int;
699                                 DISPATCH_OPCODE();
700                         HANDLE_OPCODE(OP_AND_I):
701                                 OPC->_float = OPA->_int && OPB->_int;
702                                 DISPATCH_OPCODE();
703                         HANDLE_OPCODE(OP_OR_I):
704                                 OPC->_float = OPA->_int || OPB->_int;
705                                 DISPATCH_OPCODE();
706                         HANDLE_OPCODE(OP_GE_IF):
707                                 OPC->_float = (prvm_vec_t)OPA->_int >= OPB->_float;
708                                 DISPATCH_OPCODE();
709                         HANDLE_OPCODE(OP_LE_IF):
710                                 OPC->_float = (prvm_vec_t)OPA->_int <= OPB->_float;
711                                 DISPATCH_OPCODE();
712                         HANDLE_OPCODE(OP_GT_IF):
713                                 OPC->_float = (prvm_vec_t)OPA->_int > OPB->_float;
714                                 DISPATCH_OPCODE();
715                         HANDLE_OPCODE(OP_LT_IF):
716                                 OPC->_float = (prvm_vec_t)OPA->_int < OPB->_float;
717                                 DISPATCH_OPCODE();
718                         HANDLE_OPCODE(OP_AND_IF):
719                                 OPC->_float = (prvm_vec_t)OPA->_int && OPB->_float;
720                                 DISPATCH_OPCODE();
721                         HANDLE_OPCODE(OP_OR_IF):
722                                 OPC->_float = (prvm_vec_t)OPA->_int || OPB->_float;
723                                 DISPATCH_OPCODE();
724                         HANDLE_OPCODE(OP_GE_FI):
725                                 OPC->_float = OPA->_float >= (prvm_vec_t)OPB->_int;
726                                 DISPATCH_OPCODE();
727                         HANDLE_OPCODE(OP_LE_FI):
728                                 OPC->_float = OPA->_float <= (prvm_vec_t)OPB->_int;
729                                 DISPATCH_OPCODE();
730                         HANDLE_OPCODE(OP_GT_FI):
731                                 OPC->_float = OPA->_float > (prvm_vec_t)OPB->_int;
732                                 DISPATCH_OPCODE();
733                         HANDLE_OPCODE(OP_LT_FI):
734                                 OPC->_float = OPA->_float < (prvm_vec_t)OPB->_int;
735                                 DISPATCH_OPCODE();
736                         HANDLE_OPCODE(OP_AND_FI):
737                                 OPC->_float = OPA->_float && (prvm_vec_t)OPB->_int;
738                                 DISPATCH_OPCODE();
739                         HANDLE_OPCODE(OP_OR_FI):
740                                 OPC->_float = OPA->_float || (prvm_vec_t)OPB->_int;
741                                 DISPATCH_OPCODE();
742                         HANDLE_OPCODE(OP_NOT_I):
743                                 OPC->_float = !OPA->_int;
744                                 DISPATCH_OPCODE();
745                         HANDLE_OPCODE(OP_EQ_I):
746                                 OPC->_float = OPA->_int == OPB->_int;
747                                 DISPATCH_OPCODE();
748                         HANDLE_OPCODE(OP_EQ_IF):
749                                 OPC->_float = (prvm_vec_t)OPA->_int == OPB->_float;
750                                 DISPATCH_OPCODE();
751                         HANDLE_OPCODE(OP_EQ_FI):
752                                 OPC->_float = OPA->_float == (prvm_vec_t)OPB->_int;
753                                 DISPATCH_OPCODE();
754                         HANDLE_OPCODE(OP_NE_I):
755                                 OPC->_float = OPA->_int != OPB->_int;
756                                 DISPATCH_OPCODE();
757                         HANDLE_OPCODE(OP_NE_IF):
758                                 OPC->_float = (prvm_vec_t)OPA->_int != OPB->_float;
759                                 DISPATCH_OPCODE();
760                         HANDLE_OPCODE(OP_NE_FI):
761                                 OPC->_float = OPA->_float != (prvm_vec_t)OPB->_int;
762                                 DISPATCH_OPCODE();
763                         HANDLE_OPCODE(OP_STORE_I):
764                                 OPB->_int = OPA->_int;
765                                 DISPATCH_OPCODE();
766                         HANDLE_OPCODE(OP_STOREP_I):
767 #if PRBOUNDSCHECK
768                                 if (OPB->_int < 0 || OPB->_int + 4 > pr_edictareasize)
769                                 {
770                                         PRE_ERROR();
771                                         prog->error_cmd("%s Progs attempted to write to an out of bounds edict", prog->name);
772                                         goto cleanup;
773                                 }
774 #endif
775                                 ptr = (prvm_eval_t *)(prog->edictsfields + OPB->_int);
776                                 ptr->_int = OPA->_int;
777                                 DISPATCH_OPCODE();
778                         HANDLE_OPCODE(OP_LOAD_I):
779 #if PRBOUNDSCHECK
780                                 if (OPA->edict < 0 || OPA->edict >= prog->max_edicts)
781                                 {
782                                         PRE_ERROR();
783                                         prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
784                                         goto cleanup;
785                                 }
786                                 if (OPB->_int < 0 || OPB->_int >= progs->entityfields)
787                                 {
788                                         PRE_ERROR();
789                                         prog->error_cmd("%s Progs attempted to read an invalid field in an edict", prog->name);
790                                         goto cleanup;
791                                 }
792 #endif
793                                 ed = PRVM_PROG_TO_EDICT(OPA->edict);
794                                 OPC->_int = ((prvm_eval_t *)((int *)ed->v + OPB->_int))->_int;
795                                 DISPATCH_OPCODE();
796
797                         HANDLE_OPCODE(OP_GSTOREP_I):
798                         HANDLE_OPCODE(OP_GSTOREP_F):
799                         HANDLE_OPCODE(OP_GSTOREP_ENT):
800                         HANDLE_OPCODE(OP_GSTOREP_FLD):          // integers
801                         HANDLE_OPCODE(OP_GSTOREP_S):
802                         HANDLE_OPCODE(OP_GSTOREP_FNC):          // pointers
803 #if PRBOUNDSCHECK
804                                 if (OPB->_int < 0 || OPB->_int >= pr_globaldefs)
805                                 {
806                                         PRE_ERROR();
807                                         prog->error_cmd("%s Progs attempted to write to an invalid indexed global", prog->name);
808                                         goto cleanup;
809                                 }
810 #endif
811                                 pr_iglobals[OPB->_int] = OPA->_int;
812                                 DISPATCH_OPCODE();
813                         HANDLE_OPCODE(OP_GSTOREP_V):
814 #if PRBOUNDSCHECK
815                                 if (OPB->_int < 0 || OPB->_int + 2 >= pr_globaldefs)
816                                 {
817                                         PRE_ERROR();
818                                         prog->error_cmd("%s Progs attempted to write to an invalid indexed global", prog->name);
819                                         goto cleanup;
820                                 }
821 #endif
822                                 pr_iglobals[OPB->_int  ] = OPA->ivector[0];
823                                 pr_iglobals[OPB->_int+1] = OPA->ivector[1];
824                                 pr_iglobals[OPB->_int+2] = OPA->ivector[2];
825                                 DISPATCH_OPCODE();
826
827                         HANDLE_OPCODE(OP_GADDRESS):
828                                 i = OPA->_int + (prvm_int_t) OPB->_float;
829 #if PRBOUNDSCHECK
830                                 if (i < 0 || i >= pr_globaldefs)
831                                 {
832                                         PRE_ERROR();
833                                         prog->error_cmd("%s Progs attempted to address an out of bounds global", prog->name);
834                                         goto cleanup;
835                                 }
836 #endif
837                                 OPC->_int = pr_iglobals[i];
838                                 DISPATCH_OPCODE();
839
840                         HANDLE_OPCODE(OP_GLOAD_I):
841                         HANDLE_OPCODE(OP_GLOAD_F):
842                         HANDLE_OPCODE(OP_GLOAD_FLD):
843                         HANDLE_OPCODE(OP_GLOAD_ENT):
844                         HANDLE_OPCODE(OP_GLOAD_S):
845                         HANDLE_OPCODE(OP_GLOAD_FNC):
846 #if PRBOUNDSCHECK
847                                 if (OPA->_int < 0 || OPA->_int >= pr_globaldefs)
848                                 {
849                                         PRE_ERROR();
850                                         prog->error_cmd("%s Progs attempted to read an invalid indexed global", prog->name);
851                                         goto cleanup;
852                                 }
853 #endif
854                                 OPC->_int = pr_iglobals[OPA->_int];
855                                 DISPATCH_OPCODE();
856
857                         HANDLE_OPCODE(OP_GLOAD_V):
858 #if PRBOUNDSCHECK
859                                 if (OPA->_int < 0 || OPA->_int + 2 >= pr_globaldefs)
860                                 {
861                                         PRE_ERROR();
862                                         prog->error_cmd("%s Progs attempted to read an invalid indexed global", prog->name);
863                                         goto cleanup;
864                                 }
865 #endif
866                                 OPC->ivector[0] = pr_iglobals[OPA->_int  ];
867                                 OPC->ivector[1] = pr_iglobals[OPA->_int+1];
868                                 OPC->ivector[2] = pr_iglobals[OPA->_int+2];
869                                 DISPATCH_OPCODE();
870
871                         HANDLE_OPCODE(OP_BOUNDCHECK):
872                                 if (OPA->_int < 0 || OPA->_int >= st->b)
873                                 {
874                                         PRE_ERROR();
875                                         prog->error_cmd("%s Progs boundcheck failed at line number %d, value is < 0 or >= %d", prog->name, st->b, st->c);
876                                         goto cleanup;
877                                 }
878                                 DISPATCH_OPCODE();
879
880 */
881
882 #if !USE_COMPUTED_GOTOS
883                         default:
884                                 PRE_ERROR();
885                                 prog->error_cmd("Bad opcode %i in %s", st->op, prog->name);
886                                 goto cleanup;
887                         }
888 #if PRVMSLOWINTERPRETER
889                         {
890                                 if (prog->watch_global_type != ev_void)
891                                 {
892                                         prvm_eval_t *g = PRVM_GLOBALFIELDVALUE(prog->watch_global);
893                                         prog->xstatement = st - cached_statements;
894                                         PRVM_Watchpoint(prog, 0, "Global watchpoint hit", prog->watch_global_type, &prog->watch_global_value, g);
895                                 }
896                                 if (prog->watch_field_type != ev_void && prog->watch_edict < prog->max_edicts)
897                                 {
898                                         prvm_eval_t *g = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
899                                         prog->xstatement = st - cached_statements;
900                                         PRVM_Watchpoint(prog, 0, "Entityfield watchpoint hit", prog->watch_field_type, &prog->watch_edictfield_value, g);
901                                 }
902                         }
903 #endif
904                 }
905 #endif // !USE_COMPUTED_GOTOS
906
907 #undef DISPATCH_OPCODE
908 #undef HANDLE_OPCODE
909 #undef USE_COMPUTED_GOTOS
910 #undef PRE_ERROR
911 #undef ADVANCE_PROFILE_BEFORE_JUMP