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