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