Merge branch 'simpl-fns'

This commit is contained in:
David Anderson 2015-02-25 22:21:43 -08:00
commit b5ae5defcf
6 changed files with 47 additions and 36 deletions

View File

@ -550,17 +550,15 @@ PluginContext::Execute2(IPluginFunction *function, const cell_t *params, unsigne
EnterProfileScope scriptScope("SourcePawn", cfun->FullName()); EnterProfileScope scriptScope("SourcePawn", cfun->FullName());
/* See if we have to compile the callee. */ /* See if we have to compile the callee. */
if (Environment::get()->IsJitEnabled() && if (Environment::get()->IsJitEnabled()) {
(fn = m_pRuntime->m_PubJitFuncs[public_id]) == NULL)
{
/* We might not have to - check pcode offset. */ /* We might not have to - check pcode offset. */
fn = m_pRuntime->GetJittedFunctionByOffset(cfun->Public()->code_offs); if ((fn = cfun->cachedCompiledFunction()) == nullptr) {
if (fn) { fn = m_pRuntime->GetJittedFunctionByOffset(cfun->Public()->code_offs);
m_pRuntime->m_PubJitFuncs[public_id] = fn; if (!fn) {
} else { if ((fn = CompileFunction(m_pRuntime, cfun->Public()->code_offs, &ir)) == NULL)
if ((fn = CompileFunction(m_pRuntime, cfun->Public()->code_offs, &ir)) == NULL) return ir;
return ir; }
m_pRuntime->m_PubJitFuncs[public_id] = fn; cfun->setCachedCompiledFunction(fn);
} }
} }

View File

@ -34,7 +34,6 @@ PluginRuntime::PluginRuntime()
: m_Debug(&m_plugin), : m_Debug(&m_plugin),
m_pCtx(NULL), m_pCtx(NULL),
m_PubFuncs(NULL), m_PubFuncs(NULL),
m_PubJitFuncs(NULL),
m_CompSerial(0) m_CompSerial(0)
{ {
memset(&m_plugin, 0, sizeof(m_plugin)); memset(&m_plugin, 0, sizeof(m_plugin));
@ -42,7 +41,6 @@ PluginRuntime::PluginRuntime()
m_MaxFuncs = 0; m_MaxFuncs = 0;
m_NumFuncs = 0; m_NumFuncs = 0;
float_table_ = NULL; float_table_ = NULL;
function_map_ = NULL;
alt_pcode_ = NULL; alt_pcode_ = NULL;
memset(m_CodeHash, 0, sizeof(m_CodeHash)); memset(m_CodeHash, 0, sizeof(m_CodeHash));
@ -65,9 +63,7 @@ PluginRuntime::~PluginRuntime()
for (uint32_t i = 0; i < m_plugin.num_publics; i++) for (uint32_t i = 0; i < m_plugin.num_publics; i++)
delete m_PubFuncs[i]; delete m_PubFuncs[i];
delete [] m_PubFuncs; delete [] m_PubFuncs;
delete [] m_PubJitFuncs;
delete [] float_table_; delete [] float_table_;
delete [] function_map_;
delete [] alt_pcode_; delete [] alt_pcode_;
for (size_t i = 0; i < m_JitFunctions.length(); i++) for (size_t i = 0; i < m_JitFunctions.length(); i++)
@ -286,8 +282,6 @@ int PluginRuntime::CreateFromMemory(sp_file_hdr_t *hdr, uint8_t *base)
if (m_plugin.num_publics > 0) { if (m_plugin.num_publics > 0) {
m_PubFuncs = new ScriptedInvoker *[m_plugin.num_publics]; m_PubFuncs = new ScriptedInvoker *[m_plugin.num_publics];
memset(m_PubFuncs, 0, sizeof(ScriptedInvoker *) * m_plugin.num_publics); memset(m_PubFuncs, 0, sizeof(ScriptedInvoker *) * m_plugin.num_publics);
m_PubJitFuncs = new CompiledFunction *[m_plugin.num_publics];
memset(m_PubJitFuncs, 0, sizeof(CompiledFunction *) * m_plugin.num_publics);
} }
MD5 md5_pcode; MD5 md5_pcode;
@ -303,9 +297,9 @@ int PluginRuntime::CreateFromMemory(sp_file_hdr_t *hdr, uint8_t *base)
m_pCtx = new PluginContext(this); m_pCtx = new PluginContext(this);
SetupFloatNativeRemapping(); SetupFloatNativeRemapping();
function_map_size_ = m_plugin.pcode_size / sizeof(cell_t) + 1;
function_map_ = new CompiledFunction *[function_map_size_]; if (!function_map_.init(32))
memset(function_map_, 0, function_map_size_ * sizeof(CompiledFunction *)); return SP_ERROR_OUT_OF_MEMORY;
return SP_ERROR_NONE; return SP_ERROR_NONE;
} }
@ -315,24 +309,20 @@ PluginRuntime::AddJittedFunction(CompiledFunction *fn)
{ {
m_JitFunctions.append(fn); m_JitFunctions.append(fn);
cell_t pcode_offset = fn->GetCodeOffset(); ucell_t pcode_offset = fn->GetCodeOffset();
assert(pcode_offset % 4 == 0); FunctionMap::Insert p = function_map_.findForAdd(pcode_offset);
assert(!p.found());
uint32_t pcode_index = pcode_offset / 4; function_map_.add(p, pcode_offset, fn);
assert(pcode_index < function_map_size_);
function_map_[pcode_index] = fn;
} }
CompiledFunction * CompiledFunction *
PluginRuntime::GetJittedFunctionByOffset(cell_t pcode_offset) PluginRuntime::GetJittedFunctionByOffset(cell_t pcode_offset)
{ {
assert(pcode_offset % 4 == 0); FunctionMap::Result r = function_map_.find(pcode_offset);
if (r.found())
uint32_t pcode_index = pcode_offset / 4; return r->value;
assert(pcode_index < function_map_size_); return nullptr;
return function_map_[pcode_index];
} }
int int

View File

@ -16,6 +16,7 @@
#include <sp_vm_api.h> #include <sp_vm_api.h>
#include <am-vector.h> #include <am-vector.h>
#include <am-inlinelist.h> #include <am-inlinelist.h>
#include <am-hashmap.h>
#include "jit_shared.h" #include "jit_shared.h"
#include "compiled-function.h" #include "compiled-function.h"
#include "scripted-invoker.h" #include "scripted-invoker.h"
@ -110,15 +111,24 @@ class PluginRuntime
unsigned int m_NumFuncs; unsigned int m_NumFuncs;
unsigned int m_MaxFuncs; unsigned int m_MaxFuncs;
floattbl_t *float_table_; floattbl_t *float_table_;
CompiledFunction **function_map_;
size_t function_map_size_; struct FunctionMapPolicy {
static inline uint32_t hash(ucell_t value) {
return ke::HashInteger<4>(value);
}
static inline bool matches(ucell_t a, ucell_t b) {
return a == b;
}
};
typedef ke::HashMap<ucell_t, CompiledFunction *, FunctionMapPolicy> FunctionMap;
FunctionMap function_map_;
ke::Vector<CompiledFunction *> m_JitFunctions; ke::Vector<CompiledFunction *> m_JitFunctions;
public: public:
DebugInfo m_Debug; DebugInfo m_Debug;
PluginContext *m_pCtx; PluginContext *m_pCtx;
ScriptedInvoker **m_PubFuncs; ScriptedInvoker **m_PubFuncs;
CompiledFunction **m_PubJitFuncs;
public: public:
unsigned int m_CompSerial; unsigned int m_CompSerial;

View File

@ -52,7 +52,8 @@ ScriptedInvoker::GetParentContext()
ScriptedInvoker::ScriptedInvoker(PluginRuntime *runtime, funcid_t id, uint32_t pub_id) ScriptedInvoker::ScriptedInvoker(PluginRuntime *runtime, funcid_t id, uint32_t pub_id)
: m_curparam(0), : m_curparam(0),
m_errorstate(SP_ERROR_NONE), m_errorstate(SP_ERROR_NONE),
m_FnId(id) m_FnId(id),
cc_function_(nullptr)
{ {
m_pRuntime = runtime; m_pRuntime = runtime;

View File

@ -19,6 +19,10 @@ class PluginRuntime;
using namespace SourcePawn; using namespace SourcePawn;
namespace sp {
class CompiledFunction;
}
struct ParamInfo struct ParamInfo
{ {
int flags; /* Copy-back flags */ int flags; /* Copy-back flags */
@ -70,6 +74,13 @@ class ScriptedInvoker : public IPluginFunction
return public_; return public_;
} }
sp::CompiledFunction *cachedCompiledFunction() const {
return cc_function_;
}
void setCachedCompiledFunction(sp::CompiledFunction *fn) {
cc_function_ = fn;
}
private: private:
int _PushString(const char *string, int sz_flags, int cp_flags, size_t len); int _PushString(const char *string, int sz_flags, int cp_flags, size_t len);
int SetError(int err); int SetError(int err);
@ -83,6 +94,7 @@ class ScriptedInvoker : public IPluginFunction
funcid_t m_FnId; funcid_t m_FnId;
char *full_name_; char *full_name_;
sp_public_t *public_; sp_public_t *public_;
sp::CompiledFunction *cc_function_;
}; };
#endif //_INCLUDE_SOURCEMOD_BASEFUNCTION_H_ #endif //_INCLUDE_SOURCEMOD_BASEFUNCTION_H_

View File

@ -165,7 +165,7 @@ CompileFromThunk(PluginRuntime *runtime, cell_t pcode_offs, void **addrp, char *
} }
#if defined JIT_SPEW #if defined JIT_SPEW
g_engine1.GetDebugHook()->OnDebugSpew( Environment::get()->debugger()->OnDebugSpew(
"Patching thunk to %s::%s\n", "Patching thunk to %s::%s\n",
runtime->plugin()->name, runtime->plugin()->name,
GetFunctionName(runtime->plugin(), pcode_offs)); GetFunctionName(runtime->plugin(), pcode_offs));