]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_execprogram.h
Fix setinfo.
[xonotic/darkplaces.git] / prvm_execprogram.h
index 3e3ceef0bac2fa0cae57da2d436b2e6eb628bdf7..6578d78902de24c88a91ea591cf09366eaeaa7f6 100644 (file)
@@ -1,15 +1,27 @@
-#ifdef PRVMTIMEPROFILING 
-#define PreError() \
+// NEED to reset startst after calling this! startst may or may not be clobbered!
+#define ADVANCE_PROFILE_BEFORE_JUMP() \
+       prog->xfunction->profile += (st - startst); \
+       if (prvm_statementprofiling.integer || (prvm_coverage.integer & 4)) { \
+               /* All statements from startst+1 to st have been hit. */ \
+               while (++startst <= st) { \
+                       if (prog->statement_profile[startst - cached_statements]++ == 0 && (prvm_coverage.integer & 4)) \
+                               PRVM_StatementCoverageEvent(prog, prog->xfunction, startst - cached_statements); \
+               } \
+               /* Observe: startst now is clobbered (now at st+1)! */ \
+       }
+
+#ifdef PRVMTIMEPROFILING
+#define PRE_ERROR() \
+       ADVANCE_PROFILE_BEFORE_JUMP(); \
        prog->xstatement = st - cached_statements; \
        tm = Sys_DirtyTime(); \
-       prog->xfunction->profile += (st - startst); \
        prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0; \
        startst = st; \
        starttm = tm
 #else
-#define PreError() \
+#define PRE_ERROR() \
+       ADVANCE_PROFILE_BEFORE_JUMP(); \
        prog->xstatement = st - cached_statements; \
-       prog->xfunction->profile += (st - startst); \
        startst = st
 #endif
 
                {
                        if (prog->watch_global_type != ev_void)
                        {
-                               prvm_eval_t *f = PRVM_GLOBALFIELDVALUE(prog->watch_global);
+                               prvm_eval_t *g = PRVM_GLOBALFIELDVALUE(prog->watch_global);
                                prog->xstatement = st + 1 - cached_statements;
-                               PRVM_Watchpoint(prog, 1, "Global watchpoint hit by engine", prog->watch_global_type, &prog->watch_global_value, f);
+                               PRVM_Watchpoint(prog, 1, "Global watchpoint hit by engine", prog->watch_global_type, &prog->watch_global_value, g);
                        }
                        if (prog->watch_field_type != ev_void && prog->watch_edict < prog->max_edicts)
                        {
-                               prvm_eval_t *f = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
+                               prvm_eval_t *g = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
                                prog->xstatement = st + 1 - cached_statements;
-                               PRVM_Watchpoint(prog, 1, "Entityfield watchpoint hit by engine", prog->watch_field_type, &prog->watch_edictfield_value, f);
+                               PRVM_Watchpoint(prog, 1, "Entityfield watchpoint hit by engine", prog->watch_field_type, &prog->watch_edictfield_value, g);
                        }
                }
 #endif
 #if PRVMSLOWINTERPRETER
                        if (prog->trace)
                                PRVM_PrintStatement(prog, st);
-                       if (prog->statement_profile[st - cached_statements]++ == 0 && (prvm_coverage.integer & 4))
-                               PRVM_StatementCoverageEvent(prog, prog->xfunction, st - cached_statements);
                        if (prog->break_statement >= 0)
                                if ((st - cached_statements) == prog->break_statement)
                                {
                                {
                                        if (developer.integer)
                                        {
-                                               PreError();
+                                               PRE_ERROR();
                                                VM_Warning(prog, "Attempted division by zero in %s\n", prog->name );
                                        }
                                        OPC->_float = 0.0f;
                                {
                                        if ((prvm_uint_t)OPB->_int >= cached_entityfieldsarea)
                                        {
-                                               PreError();
+                                               PRE_ERROR();
                                                prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
                                                goto cleanup;
                                        }
                                        if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
                                        {
-                                               PreError();
+                                               PRE_ERROR();
                                                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);
                                        }
                                }
                                {
                                        if ((prvm_uint_t)OPB->_int > cached_entityfieldsarea_3)
                                        {
-                                               PreError();
+                                               PRE_ERROR();
                                                prog->error_cmd("%s attempted to write to an out of bounds edict (%i)", prog->name, (int)OPB->_int);
                                                goto cleanup;
                                        }
                                        if ((prvm_uint_t)OPB->_int < cached_entityfields && !cached_allowworldwrites)
                                        {
-                                               PreError();
+                                               PRE_ERROR();
                                                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);
                                        }
                                }
                        HANDLE_OPCODE(OP_ADDRESS):
                                if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to address an out of bounds edict number", prog->name);
                                        goto cleanup;
                                }
                                if ((prvm_uint_t)OPB->_int >= cached_entityfields)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s attempted to address an invalid field (%i) in an edict", prog->name, (int)OPB->_int);
                                        goto cleanup;
                                }
 #if 0
                                if (OPA->edict == 0 && !cached_allowworldwrites)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("forbidden assignment to null/world entity in %s", prog->name);
                                        goto cleanup;
                                }
                        HANDLE_OPCODE(OP_LOAD_FNC):
                                if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
                                        goto cleanup;
                                }
                                if ((prvm_uint_t)OPB->_int >= cached_entityfields)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
                                        goto cleanup;
                                }
                        HANDLE_OPCODE(OP_LOAD_V):
                                if ((prvm_uint_t)OPA->edict >= cached_max_edicts)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
                                        goto cleanup;
                                }
                                if ((prvm_uint_t)OPB->_int > cached_entityfields_3)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s attempted to read an invalid field in an edict (%i)", prog->name, (int)OPB->_int);
                                        goto cleanup;
                                }
                                // although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
                                // and entity, string, field values can never have that value
                                {
-                                       prog->xfunction->profile += (st - startst);
+                                       ADVANCE_PROFILE_BEFORE_JUMP();
                                        st = cached_statements + st->jumpabsolute - 1;  // offset the st++
                                        startst = st;
                                        // no bounds check needed, it is done when loading progs
                                // although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
                                // and entity, string, field values can never have that value
                                {
-                                       prog->xfunction->profile += (st - startst);
+                                       ADVANCE_PROFILE_BEFORE_JUMP();
                                        st = cached_statements + st->jumpabsolute - 1;  // offset the st++
                                        startst = st;
                                        // no bounds check needed, it is done when loading progs
                                DISPATCH_OPCODE();
 
                        HANDLE_OPCODE(OP_GOTO):
-                               prog->xfunction->profile += (st - startst);
+                               ADVANCE_PROFILE_BEFORE_JUMP();
                                st = cached_statements + st->jumpabsolute - 1;  // offset the st++
                                startst = st;
                                // no bounds check needed, it is done when loading progs
                                prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
                                starttm = tm;
 #endif
-                               prog->xfunction->profile += (st - startst);
+                               ADVANCE_PROFILE_BEFORE_JUMP();
                                startst = st;
                                prog->xstatement = st - cached_statements;
                                prog->argc = st->op - OP_CALL0;
                                if (!OPA->function)
+                               {
                                        prog->error_cmd("NULL function in %s", prog->name);
+                               }
 
                                if(!OPA->function || OPA->function < 0 || OPA->function >= prog->numfunctions)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s CALL outside the program", prog->name);
                                        goto cleanup;
                                }
 
-                               newf = &prog->functions[OPA->function];
-                               if (newf->callcount++ == 0 && (prvm_coverage.integer & 1))
-                                       PRVM_FunctionCoverageEvent(prog, newf);
+                               enterfunc = &prog->functions[OPA->function];
+                               if (enterfunc->callcount++ == 0 && (prvm_coverage.integer & 1))
+                                       PRVM_FunctionCoverageEvent(prog, enterfunc);
 
-                               if (newf->first_statement < 0)
+                               if (enterfunc->first_statement < 0)
                                {
                                        // negative first_statement values are built in functions
-                                       int builtinnumber = -newf->first_statement;
+                                       int builtinnumber = -enterfunc->first_statement;
                                        prog->xfunction->builtinsprofile++;
                                        if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
                                        {
                                                prog->builtins[builtinnumber](prog);
 #ifdef PRVMTIMEPROFILING 
                                                tm = Sys_DirtyTime();
-                                               newf->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
+                                               enterfunc->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
                                                prog->xfunction->tbprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
                                                starttm = tm;
 #endif
                                                prog->error_cmd("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, prog->name);
                                }
                                else
-                                       st = cached_statements + PRVM_EnterFunction(prog, newf);
+                                       st = cached_statements + PRVM_EnterFunction(prog, enterfunc);
                                startst = st;
                                DISPATCH_OPCODE();
 
                                prog->xfunction->tprofile += (tm - starttm >= 0 && tm - starttm < 1800) ? (tm - starttm) : 0;
                                starttm = tm;
 #endif
-                               prog->xfunction->profile += (st - startst);
+                               ADVANCE_PROFILE_BEFORE_JUMP();
                                prog->xstatement = st - cached_statements;
 
                                prog->globals.ip[OFS_RETURN  ] = prog->globals.ip[st->operand[0]  ];
                                }
                                else
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->xstatement = st - cached_statements;
                                        prog->error_cmd("OP_STATE not supported by %s", prog->name);
                                }
 #if PRBOUNDSCHECK
                                if (OPB->_int < 0 || OPB->_int + 4 > pr_edictareasize)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to write to an out of bounds edict", prog->name);
                                        goto cleanup;
                                }
 #if PRBOUNDSCHECK
                                if (OPA->edict < 0 || OPA->edict >= prog->max_edicts)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to read an out of bounds edict number", prog->name);
                                        goto cleanup;
                                }
                                if (OPB->_int < 0 || OPB->_int >= progs->entityfields)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to read an invalid field in an edict", prog->name);
                                        goto cleanup;
                                }
 #if PRBOUNDSCHECK
                                if (OPB->_int < 0 || OPB->_int >= pr_globaldefs)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to write to an invalid indexed global", prog->name);
                                        goto cleanup;
                                }
 #if PRBOUNDSCHECK
                                if (OPB->_int < 0 || OPB->_int + 2 >= pr_globaldefs)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to write to an invalid indexed global", prog->name);
                                        goto cleanup;
                                }
 #if PRBOUNDSCHECK
                                if (i < 0 || i >= pr_globaldefs)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to address an out of bounds global", prog->name);
                                        goto cleanup;
                                }
 #if PRBOUNDSCHECK
                                if (OPA->_int < 0 || OPA->_int >= pr_globaldefs)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to read an invalid indexed global", prog->name);
                                        goto cleanup;
                                }
 #if PRBOUNDSCHECK
                                if (OPA->_int < 0 || OPA->_int + 2 >= pr_globaldefs)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs attempted to read an invalid indexed global", prog->name);
                                        goto cleanup;
                                }
                        HANDLE_OPCODE(OP_BOUNDCHECK):
                                if (OPA->_int < 0 || OPA->_int >= st->b)
                                {
-                                       PreError();
+                                       PRE_ERROR();
                                        prog->error_cmd("%s Progs boundcheck failed at line number %d, value is < 0 or >= %d", prog->name, st->b, st->c);
                                        goto cleanup;
                                }
 
 #if !USE_COMPUTED_GOTOS
                        default:
-                               PreError();
+                               PRE_ERROR();
                                prog->error_cmd("Bad opcode %i in %s", st->op, prog->name);
                                goto cleanup;
                        }
                        {
                                if (prog->watch_global_type != ev_void)
                                {
-                                       prvm_eval_t *f = PRVM_GLOBALFIELDVALUE(prog->watch_global);
+                                       prvm_eval_t *g = PRVM_GLOBALFIELDVALUE(prog->watch_global);
                                        prog->xstatement = st - cached_statements;
-                                       PRVM_Watchpoint(prog, 0, "Global watchpoint hit", prog->watch_global_type, &prog->watch_global_value, f);
+                                       PRVM_Watchpoint(prog, 0, "Global watchpoint hit", prog->watch_global_type, &prog->watch_global_value, g);
                                }
                                if (prog->watch_field_type != ev_void && prog->watch_edict < prog->max_edicts)
                                {
-                                       prvm_eval_t *f = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
+                                       prvm_eval_t *g = PRVM_EDICTFIELDVALUE(prog->edicts + prog->watch_edict, prog->watch_field);
                                        prog->xstatement = st - cached_statements;
-                                       PRVM_Watchpoint(prog, 0, "Entityfield watchpoint hit", prog->watch_field_type, &prog->watch_edictfield_value, f);
+                                       PRVM_Watchpoint(prog, 0, "Entityfield watchpoint hit", prog->watch_field_type, &prog->watch_edictfield_value, g);
                                }
                        }
 #endif
 #undef DISPATCH_OPCODE
 #undef HANDLE_OPCODE
 #undef USE_COMPUTED_GOTOS
-
-
-#undef PreError
+#undef PRE_ERROR
+#undef ADVANCE_PROFILE_BEFORE_JUMP