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