X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=prvm_exec.c;h=fd0ff40b9f5fe802adea919a93cc0e288c68a419;hp=40b19d44c9b3c91a4628134ec1963c660a094b93;hb=0024f172c0c10136e5ae18914be014a5b422ffb8;hpb=48dd658f8bbfa60efbd5f56d1c208727c4418078 diff --git a/prvm_exec.c b/prvm_exec.c index 40b19d44..fd0ff40b 100644 --- a/prvm_exec.c +++ b/prvm_exec.c @@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" #include "progsvm.h" -const char *prvm_opnames[] = +static const char *prvm_opnames[] = { "^5DONE", @@ -107,7 +107,186 @@ const char *prvm_opnames[] = "^2OR", "BITAND", -"BITOR" +"BITOR", + + + + +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, + +"STORE_I", + +NULL, +NULL, + +"ADD_I", +"ADD_FI", +"ADD_IF", + +"SUB_I", +"SUB_FI", +"SUB_IF", +"CONV_IF", +"CONV_FI", + +NULL, +NULL, + +"LOAD_I", +"STOREP_I", + +NULL, +NULL, + +"BITAND_I", +"BITOR_I", + +"MUL_I", +"DIV_I", +"EQ_I", +"NE_I", + +NULL, +NULL, + +"NOT_I", + +"DIV_VF", + +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, + +"STORE_P", + +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, +NULL, + +"LE_I", +"GE_I", +"LT_I", +"GT_I", + +"LE_IF", +"GE_IF", +"LT_IF", +"GT_IF", + +"LE_FI", +"GE_FI", +"LT_FI", +"GT_FI", + +"EQ_IF", +"EQ_FI", + +NULL, +NULL, +NULL, +NULL, + +"MUL_IF", +"MUL_FI", +"MUL_VI", + +NULL, + +"DIV_IF", +"DIV_FI", +"BITAND_IF", +"BITOR_IF", +"BITAND_FI", +"BITOR_FI", +"AND_I", +"OR_I", +"AND_IF", +"OR_IF", +"AND_FI", +"OR_FI", +"NE_IF", +"NE_FI", + +"GSTOREP_I", +"GSTOREP_F", +"GSTOREP_ENT", +"GSTOREP_FLD", +"GSTOREP_S", +"GSTOREP_FNC", +"GSTOREP_V", +"GADDRESS", +"GLOAD_I", +"GLOAD_F", +"GLOAD_FLD", +"GLOAD_ENT", +"GLOAD_S", +"GLOAD_FNC", +"BOUNDCHECK", +NULL, +NULL, +NULL, +NULL, +"GLOAD_V", }; @@ -119,6 +298,7 @@ const char *prvm_opnames[] = PRVM_PrintStatement ================= */ +extern cvar_t prvm_coverage; extern cvar_t prvm_statementprofiling; extern cvar_t prvm_timeprofiling; static void PRVM_PrintStatement(prvm_prog_t *prog, mstatement_t *s) @@ -126,6 +306,7 @@ static void PRVM_PrintStatement(prvm_prog_t *prog, mstatement_t *s) size_t i; int opnum = (int)(s - prog->statements); char valuebuf[MAX_INPUTLINE]; + const char *opname; Con_Printf("s%i: ", opnum); if( prog->statement_linenums ) @@ -139,16 +320,18 @@ static void PRVM_PrintStatement(prvm_prog_t *prog, mstatement_t *s) if (prvm_statementprofiling.integer) Con_Printf("%7.0f ", prog->statement_profile[s - prog->statements]); - if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0])) - { - Con_Printf("%s ", prvm_opnames[s->op]); - i = strlen(prvm_opnames[s->op]); - // don't count a preceding color tag when padding the name - if (prvm_opnames[s->op][0] == STRING_COLOR_TAG) - i -= 2; - for ( ; i<10 ; i++) - Con_Print(" "); - } + if ( (unsigned)s->op < sizeof(prvm_opnames)/sizeof(prvm_opnames[0]) && prvm_opnames[s->op]) + opname = prvm_opnames[s->op]; + else + opname = valuebuf, dpsnprintf(valuebuf, sizeof(valuebuf), "OPCODE_%u", (unsigned)s->op); + Con_Printf("%s ", opname); + i = strlen(opname); + // don't count a preceding color tag when padding the name + if (opname[0] == STRING_COLOR_TAG) + i -= 2; + for ( ; i<10 ; i++) + Con_Print(" "); + if (s->operand[0] >= 0) Con_Printf( "%s", PRVM_GlobalString(prog, s->operand[0], valuebuf, sizeof(valuebuf))); if (s->operand[1] >= 0) Con_Printf(", %s", PRVM_GlobalString(prog, s->operand[1], valuebuf, sizeof(valuebuf))); if (s->operand[2] >= 0) Con_Printf(", %s", PRVM_GlobalString(prog, s->operand[2], valuebuf, sizeof(valuebuf))); @@ -185,8 +368,11 @@ void PRVM_PrintFunctionStatements (prvm_prog_t *prog, const char *name) for (i = firststatement;i < endstatement;i++) { PRVM_PrintStatement(prog, prog->statements + i); - prog->statement_profile[i] = 0; + if (!(prvm_coverage.integer & 4)) + prog->statement_profile[i] = 0; } + if (prvm_coverage.integer & 4) + Con_Printf("Collecting statement coverage, not flushing statement profile.\n"); } /* @@ -195,19 +381,19 @@ PRVM_PrintFunction_f ============ */ -void PRVM_PrintFunction_f (void) +void PRVM_PrintFunction_f(cmd_state_t *cmd) { prvm_prog_t *prog; - if (Cmd_Argc() != 3) + if (Cmd_Argc(cmd) != 3) { Con_Printf("usage: prvm_printfunction \n"); return; } - if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1)))) + if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1)))) return; - PRVM_PrintFunctionStatements(prog, Cmd_Argv(2)); + PRVM_PrintFunctionStatements(prog, Cmd_Argv(cmd, 2)); } /* @@ -429,16 +615,16 @@ PRVM_CallProfile_f ============ */ -void PRVM_CallProfile_f (void) +void PRVM_CallProfile_f(cmd_state_t *cmd) { prvm_prog_t *prog; - if (Cmd_Argc() != 2) + if (Cmd_Argc(cmd) != 2) { Con_Print("prvm_callprofile \n"); return; } - if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1)))) + if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1)))) return; PRVM_CallProfile(prog); @@ -450,41 +636,53 @@ PRVM_Profile_f ============ */ -void PRVM_Profile_f (void) +void PRVM_Profile_f(cmd_state_t *cmd) { prvm_prog_t *prog; int howmany; + if (prvm_coverage.integer & 1) + { + Con_Printf("Collecting function coverage, cannot profile - sorry!\n"); + return; + } + howmany = 1<<30; - if (Cmd_Argc() == 3) - howmany = atoi(Cmd_Argv(2)); - else if (Cmd_Argc() != 2) + if (Cmd_Argc(cmd) == 3) + howmany = atoi(Cmd_Argv(cmd, 2)); + else if (Cmd_Argc(cmd) != 2) { Con_Print("prvm_profile \n"); return; } - if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1)))) + if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1)))) return; PRVM_Profile(prog, howmany, 0, 0); } -void PRVM_ChildProfile_f (void) +void PRVM_ChildProfile_f(cmd_state_t *cmd) { prvm_prog_t *prog; int howmany; + if (prvm_coverage.integer & 1) + { + Con_Printf("Collecting function coverage, cannot profile - sorry!\n"); + return; + } + howmany = 1<<30; - if (Cmd_Argc() == 3) - howmany = atoi(Cmd_Argv(2)); - else if (Cmd_Argc() != 2) + if (Cmd_Argc(cmd) == 3) + howmany = atoi(Cmd_Argv(cmd, 2)); + else if (Cmd_Argc(cmd) != 2) { Con_Print("prvm_childprofile \n"); return; } - if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(1)))) + if (!(prog = PRVM_FriendlyProgFromString(Cmd_Argv(cmd, 1)))) return; PRVM_Profile(prog, howmany, 0, 1); @@ -517,6 +715,9 @@ extern cvar_t prvm_errordump; void PRVM_Crash(prvm_prog_t *prog) { char vabuf[1024]; + + cl.csqc_loaded = false; + if (prog == NULL) return; if (!prog->loaded) @@ -533,7 +734,7 @@ void PRVM_Crash(prvm_prog_t *prog) if(prvm_errordump.integer) { // make a savegame - Host_Savegame_to(prog, va(vabuf, sizeof(vabuf), "crash-%s.dmp", prog->name)); + SV_Savegame_to(prog, va(vabuf, sizeof(vabuf), "crash-%s.dmp", prog->name)); } // dump the stack so host_error can shutdown functions @@ -664,12 +865,54 @@ void PRVM_Init_Exec(prvm_prog_t *prog) // nothing here yet } -#define OPA ((prvm_eval_t *)&prog->globals.fp[st->operand[0]]) -#define OPB ((prvm_eval_t *)&prog->globals.fp[st->operand[1]]) -#define OPC ((prvm_eval_t *)&prog->globals.fp[st->operand[2]]) +/* +================== +Coverage +================== +*/ +// Note: in these two calls, prog->xfunction is assumed to be sane. +static const char *PRVM_WhereAmI(char *buf, size_t bufsize, prvm_prog_t *prog, mfunction_t *func, int statement) +{ + if (prog->statement_linenums) + { + if (prog->statement_columnnums) + return va(buf, bufsize, "%s:%i:%i(%s, %i)", PRVM_GetString(prog, func->s_file), prog->statement_linenums[statement], prog->statement_columnnums[statement], PRVM_GetString(prog, func->s_name), statement - func->first_statement); + else + return va(buf, bufsize, "%s:%i(%s, %i)", PRVM_GetString(prog, func->s_file), prog->statement_linenums[statement], PRVM_GetString(prog, func->s_name), statement - func->first_statement); + } + else + return va(buf, bufsize, "%s(%s, %i)", PRVM_GetString(prog, func->s_file), PRVM_GetString(prog, func->s_name), statement - func->first_statement); +} +static void PRVM_FunctionCoverageEvent(prvm_prog_t *prog, mfunction_t *func) +{ + ++prog->functions_covered; + Con_Printf("prvm_coverage: %s just called %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_GetString(prog, func->s_name), prog->functions_covered * 100.0 / prog->numfunctions); +} +void PRVM_ExplicitCoverageEvent(prvm_prog_t *prog, mfunction_t *func, int statement) +{ + char vabuf[128]; + ++prog->explicit_covered; + Con_Printf("prvm_coverage: %s just executed a coverage() statement at %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_WhereAmI(vabuf, sizeof(vabuf), prog, func, statement), prog->explicit_covered * 100.0 / prog->numexplicitcoveragestatements); +} +static void PRVM_StatementCoverageEvent(prvm_prog_t *prog, mfunction_t *func, int statement) +{ + char vabuf[128]; + ++prog->statements_covered; + Con_Printf("prvm_coverage: %s just executed a statement at %s for the first time. Coverage: %.2f%%.\n", prog->name, PRVM_WhereAmI(vabuf, sizeof(vabuf), prog, func, statement), prog->statements_covered * 100.0 / prog->numstatements); +} + +#if defined (__GNUC__) || (__clang__) || (__TINYC__) +# ifndef CONFIG_PEDANTIC +# define HAVE_COMPUTED_GOTOS 1 +# endif +#endif + +#define OPA ((prvm_eval_t *)&globals[st->operand[0]]) +#define OPB ((prvm_eval_t *)&globals[st->operand[1]]) +#define OPC ((prvm_eval_t *)&globals[st->operand[2]]) extern cvar_t prvm_traceqc; extern cvar_t prvm_statementprofiling; -extern qboolean prvm_runawaycheck; +extern qbool prvm_runawaycheck; #ifdef PROFILING #ifdef CONFIG_MENU @@ -681,7 +924,7 @@ MVM_ExecuteProgram void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage) { mstatement_t *st, *startst; - mfunction_t *f, *newf; + mfunction_t *func, *enterfunc; prvm_edict_t *ed; prvm_eval_t *ptr; int jumpcount, cachedpr_trace, exitdepth; @@ -690,7 +933,7 @@ void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessag double tm, starttm; prvm_vec_t tempfloat; // these may become out of date when a builtin is called, and are updated accordingly - prvm_vec_t *cached_edictsfields = prog->edictsfields; + prvm_vec_t *cached_edictsfields = prog->edictsfields.fp; unsigned int cached_entityfields = prog->entityfields; unsigned int cached_entityfields_3 = prog->entityfields - 3; unsigned int cached_entityfieldsarea = prog->entityfieldsarea; @@ -700,9 +943,11 @@ void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessag unsigned int cached_max_edicts = prog->max_edicts; // these do not change mstatement_t *cached_statements = prog->statements; - qboolean cached_allowworldwrites = prog->allowworldwrites; + qbool cached_allowworldwrites = prog->allowworldwrites; unsigned int cached_flag = prog->flag; + prvm_vec_t *globals = prog->globals.fp; + calltime = Sys_DirtyTime(); if (!fnum || fnum >= (unsigned int)prog->numfunctions) @@ -712,7 +957,7 @@ void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessag prog->error_cmd("MVM_ExecuteProgram: %s", errormessage); } - f = &prog->functions[fnum]; + func = &prog->functions[fnum]; // after executing this function, delete all tempstrings it created restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize; @@ -723,7 +968,7 @@ void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessag exitdepth = prog->depth; // make a stack frame - st = &prog->statements[PRVM_EnterFunction(prog, f)]; + st = &prog->statements[PRVM_EnterFunction(prog, func)]; // save the starting statement pointer for profiling // (when the function exits or jumps, the (st - startst) integer value is // added to the function's profile counter) @@ -732,11 +977,12 @@ void MVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessag // instead of counting instructions, we count jumps jumpcount = 0; // add one to the callcount of this function because otherwise engine-called functions aren't counted - prog->xfunction->callcount++; + if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1)) + PRVM_FunctionCoverageEvent(prog, prog->xfunction); chooseexecprogram: cachedpr_trace = prog->trace; - if (prvm_statementprofiling.integer || prog->trace || prog->watch_global >= 0 || prog->watch_edict >= 0 || prog->break_statement >= 0) + if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0) { #define PRVMSLOWINTERPRETER 1 if (prvm_timeprofiling.integer) @@ -772,7 +1018,7 @@ cleanup: prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize; tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0; - f->totaltime += tm; + func->totaltime += tm; if (prog == SVVM_prog) SV_FlushBroadcastMessages(); @@ -787,7 +1033,7 @@ CLVM_ExecuteProgram void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessage) { mstatement_t *st, *startst; - mfunction_t *f, *newf; + mfunction_t *func, *enterfunc; prvm_edict_t *ed; prvm_eval_t *ptr; int jumpcount, cachedpr_trace, exitdepth; @@ -796,7 +1042,7 @@ void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa double tm, starttm; prvm_vec_t tempfloat; // these may become out of date when a builtin is called, and are updated accordingly - prvm_vec_t *cached_edictsfields = prog->edictsfields; + prvm_vec_t *cached_edictsfields = prog->edictsfields.fp; unsigned int cached_entityfields = prog->entityfields; unsigned int cached_entityfields_3 = prog->entityfields - 3; unsigned int cached_entityfieldsarea = prog->entityfieldsarea; @@ -806,9 +1052,11 @@ void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa unsigned int cached_max_edicts = prog->max_edicts; // these do not change mstatement_t *cached_statements = prog->statements; - qboolean cached_allowworldwrites = prog->allowworldwrites; + qbool cached_allowworldwrites = prog->allowworldwrites; unsigned int cached_flag = prog->flag; + prvm_vec_t *globals = prog->globals.fp; + calltime = Sys_DirtyTime(); if (!fnum || fnum >= (unsigned int)prog->numfunctions) @@ -818,7 +1066,7 @@ void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa prog->error_cmd("CLVM_ExecuteProgram: %s", errormessage); } - f = &prog->functions[fnum]; + func = &prog->functions[fnum]; // after executing this function, delete all tempstrings it created restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize; @@ -829,7 +1077,7 @@ void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa exitdepth = prog->depth; // make a stack frame - st = &prog->statements[PRVM_EnterFunction(prog, f)]; + st = &prog->statements[PRVM_EnterFunction(prog, func)]; // save the starting statement pointer for profiling // (when the function exits or jumps, the (st - startst) integer value is // added to the function's profile counter) @@ -838,11 +1086,12 @@ void CLVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa // instead of counting instructions, we count jumps jumpcount = 0; // add one to the callcount of this function because otherwise engine-called functions aren't counted - prog->xfunction->callcount++; + if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1)) + PRVM_FunctionCoverageEvent(prog, prog->xfunction); chooseexecprogram: cachedpr_trace = prog->trace; - if (prvm_statementprofiling.integer || prog->trace || prog->watch_global >= 0 || prog->watch_edict >= 0 || prog->break_statement >= 0) + if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0) { #define PRVMSLOWINTERPRETER 1 if (prvm_timeprofiling.integer) @@ -878,7 +1127,7 @@ cleanup: prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize; tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0; - f->totaltime += tm; + func->totaltime += tm; if (prog == SVVM_prog) SV_FlushBroadcastMessages(); @@ -897,7 +1146,7 @@ void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa #endif { mstatement_t *st, *startst; - mfunction_t *f, *newf; + mfunction_t *func, *enterfunc; prvm_edict_t *ed; prvm_eval_t *ptr; int jumpcount, cachedpr_trace, exitdepth; @@ -906,7 +1155,7 @@ void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa double tm, starttm; prvm_vec_t tempfloat; // these may become out of date when a builtin is called, and are updated accordingly - prvm_vec_t *cached_edictsfields = prog->edictsfields; + prvm_vec_t *cached_edictsfields = prog->edictsfields.fp; unsigned int cached_entityfields = prog->entityfields; unsigned int cached_entityfields_3 = prog->entityfields - 3; unsigned int cached_entityfieldsarea = prog->entityfieldsarea; @@ -916,9 +1165,11 @@ void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa unsigned int cached_max_edicts = prog->max_edicts; // these do not change mstatement_t *cached_statements = prog->statements; - qboolean cached_allowworldwrites = prog->allowworldwrites; + qbool cached_allowworldwrites = prog->allowworldwrites; unsigned int cached_flag = prog->flag; + prvm_vec_t *globals = prog->globals.fp; + calltime = Sys_DirtyTime(); if (!fnum || fnum >= (unsigned int)prog->numfunctions) @@ -928,7 +1179,7 @@ void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa prog->error_cmd("SVVM_ExecuteProgram: %s", errormessage); } - f = &prog->functions[fnum]; + func = &prog->functions[fnum]; // after executing this function, delete all tempstrings it created restorevm_tempstringsbuf_cursize = prog->tempstringsbuf.cursize; @@ -939,7 +1190,7 @@ void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa exitdepth = prog->depth; // make a stack frame - st = &prog->statements[PRVM_EnterFunction(prog, f)]; + st = &prog->statements[PRVM_EnterFunction(prog, func)]; // save the starting statement pointer for profiling // (when the function exits or jumps, the (st - startst) integer value is // added to the function's profile counter) @@ -948,11 +1199,12 @@ void PRVM_ExecuteProgram (prvm_prog_t *prog, func_t fnum, const char *errormessa // instead of counting instructions, we count jumps jumpcount = 0; // add one to the callcount of this function because otherwise engine-called functions aren't counted - prog->xfunction->callcount++; + if (prog->xfunction->callcount++ == 0 && (prvm_coverage.integer & 1)) + PRVM_FunctionCoverageEvent(prog, prog->xfunction); chooseexecprogram: cachedpr_trace = prog->trace; - if (prvm_statementprofiling.integer || prog->trace || prog->watch_global >= 0 || prog->watch_edict >= 0 || prog->break_statement >= 0) + if (prog->trace || prog->watch_global_type != ev_void || prog->watch_field_type != ev_void || prog->break_statement >= 0) { #define PRVMSLOWINTERPRETER 1 if (prvm_timeprofiling.integer) @@ -988,7 +1240,7 @@ cleanup: prog->tempstringsbuf.cursize = restorevm_tempstringsbuf_cursize; tm = Sys_DirtyTime() - calltime;if (tm < 0 || tm >= 1800) tm = 0; - f->totaltime += tm; + func->totaltime += tm; if (prog == SVVM_prog) SV_FlushBroadcastMessages();