From e930dfaae75dc31b1e9191877671e4ebf4b50657 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Tue, 9 Oct 2018 02:52:07 +0200 Subject: [PATCH] - create ScriptCall function pointer on VMScriptFunction --- src/scripting/vm/jit_call.cpp | 3 ++- src/scripting/vm/vmexec.cpp | 1 - src/scripting/vm/vmexec.h | 26 ++++------------------ src/scripting/vm/vmframe.cpp | 42 ++++++++++++++++++++++++++++++++++- src/scripting/vm/vmintern.h | 8 +++++-- 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/scripting/vm/jit_call.cpp b/src/scripting/vm/jit_call.cpp index 94958fbc04f..82c6d5ea3e9 100644 --- a/src/scripting/vm/jit_call.cpp +++ b/src/scripting/vm/jit_call.cpp @@ -417,7 +417,8 @@ int JitCompiler::DoCall(VMFrameStack *stack, VMFunction *call, int b, int c, VMV else { VMCalls[0]++; - numret = VMExec(static_cast(call), param, b, returns, c); + auto sfunc = static_cast(call); + numret = sfunc->ScriptCall(sfunc, param, b, returns, c); } return numret; diff --git a/src/scripting/vm/vmexec.cpp b/src/scripting/vm/vmexec.cpp index bfe5b5d323a..bd6c2f5f53b 100644 --- a/src/scripting/vm/vmexec.cpp +++ b/src/scripting/vm/vmexec.cpp @@ -37,7 +37,6 @@ #include "r_state.h" #include "stats.h" #include "vmintern.h" -#include "jit.h" #include "types.h" extern cycle_t VMCycles[10]; diff --git a/src/scripting/vm/vmexec.h b/src/scripting/vm/vmexec.h index df5efb210d4..eec5009e9c8 100644 --- a/src/scripting/vm/vmexec.h +++ b/src/scripting/vm/vmexec.h @@ -711,7 +711,8 @@ static int ExecScriptFunc(VMFrameStack *stack, VMReturn *ret, int numret) else { VMCalls[0]++; - numret = Exec(static_cast(call), reg.param + f->NumParam - b, b, returns, C); + auto sfunc = static_cast(call); + numret = sfunc->ScriptCall(sfunc, reg.param + f->NumParam - b, b, returns, C); } assert(numret == C && "Number of parameters returned differs from what was expected by the caller"); f->NumParam -= B; @@ -753,7 +754,8 @@ static int ExecScriptFunc(VMFrameStack *stack, VMReturn *ret, int numret) else { // FIXME: Not a true tail call VMCalls[0]++; - return Exec(static_cast(call), reg.param + f->NumParam - B, B, ret, numret); + auto sfunc = static_cast(call); + return sfunc->ScriptCall(sfunc, reg.param + f->NumParam - B, B, ret, numret); } } NEXTOP; @@ -2051,26 +2053,6 @@ static int Exec(VMScriptFunction *func, VMValue *params, int numparams, VMReturn VMFillParams(params, newf, numparams); try { - if (!func->JitCompiled) - { - func->JitFunc = JitCompile(func); - func->JitCompiled = true; - } - if (func->JitFunc) - { - JitExceptionInfo exceptInfo; - exceptInfo.reason = -1; - int result = func->JitFunc(stack, ret, numret, &exceptInfo); - if (exceptInfo.reason != -1) - { - if (exceptInfo.cppException) - std::rethrow_exception(exceptInfo.cppException); - else - ThrowAbortException(func, exceptInfo.pcOnJitAbort, (EVMAbortException)exceptInfo.reason, nullptr); - } - return result; - } - numret = ExecScriptFunc(stack, ret, numret); } catch (...) diff --git a/src/scripting/vm/vmframe.cpp b/src/scripting/vm/vmframe.cpp index b9114c48903..5157bf37732 100644 --- a/src/scripting/vm/vmframe.cpp +++ b/src/scripting/vm/vmframe.cpp @@ -213,6 +213,45 @@ int VMScriptFunction::PCToLine(const VMOP *pc) return -1; } +int VMScriptFunction::FirstScriptCall(VMScriptFunction *func, VMValue *params, int numparams, VMReturn *ret, int numret) +{ + func->JitFunc = JitCompile(func); + if (func->JitFunc) + func->ScriptCall = &VMScriptFunction::JitCall; + else + func->ScriptCall = VMExec; + + return func->ScriptCall(func, params, numparams, ret, numret); +} + +int VMScriptFunction::JitCall(VMScriptFunction *func, VMValue *params, int numparams, VMReturn *ret, int numret) +{ + VMFrameStack *stack = &GlobalVMStack; + VMFrame *newf = stack->AllocFrame(func); + VMFillParams(params, newf, numparams); + try + { + JitExceptionInfo exceptInfo; + exceptInfo.reason = -1; + int result = func->JitFunc(stack, ret, numret, &exceptInfo); + if (exceptInfo.reason != -1) + { + if (exceptInfo.cppException) + std::rethrow_exception(exceptInfo.cppException); + else + ThrowAbortException(func, exceptInfo.pcOnJitAbort, (EVMAbortException)exceptInfo.reason, nullptr); + } + return result; + } + catch (...) + { + stack->PopFrame(); + throw; + } + stack->PopFrame(); + return numret; +} + //=========================================================================== // // VMFrame :: InitRegS @@ -464,7 +503,8 @@ int VMCall(VMFunction *func, VMValue *params, int numparams, VMReturn *results, { VMCycles[0].Clock(); VMCalls[0]++; - int numret = VMExec(static_cast(func), params, numparams, results, numresults); + auto sfunc = static_cast(func); + int numret = sfunc->ScriptCall(sfunc, params, numparams, results, numresults); VMCycles[0].Unclock(); return numret; } diff --git a/src/scripting/vm/vmintern.h b/src/scripting/vm/vmintern.h index 8d1f01b5577..935695d7d5d 100644 --- a/src/scripting/vm/vmintern.h +++ b/src/scripting/vm/vmintern.h @@ -478,11 +478,15 @@ class VMScriptFunction : public VMFunction VM_UBYTE NumArgs; // Number of arguments this function takes TArray SpecialInits; // list of all contents on the extra stack which require construction and destruction - bool JitCompiled = false; - JitFuncPtr JitFunc = nullptr; + int(*ScriptCall)(VMScriptFunction *func, VMValue *params, int numparams, VMReturn *ret, int numret) = &VMScriptFunction::FirstScriptCall; void InitExtra(void *addr); void DestroyExtra(void *addr); int AllocExtraStack(PType *type); int PCToLine(const VMOP *pc); + +private: + static int FirstScriptCall(VMScriptFunction *func, VMValue *params, int numparams, VMReturn *ret, int numret); + static int JitCall(VMScriptFunction *func, VMValue *params, int numparams, VMReturn *ret, int numret); + JitFuncPtr JitFunc = nullptr; };