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