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