diff --git a/sourcepawn/jit/BaseRuntime.cpp b/sourcepawn/jit/BaseRuntime.cpp index 2fade850..3cff6e51 100644 --- a/sourcepawn/jit/BaseRuntime.cpp +++ b/sourcepawn/jit/BaseRuntime.cpp @@ -277,10 +277,10 @@ int BaseRuntime::CreateFromMemory(sp_file_hdr_t *hdr, uint8_t *base) } if (m_plugin.num_publics > 0) { - m_PubFuncs = new CFunction *[m_plugin.num_publics]; - memset(m_PubFuncs, 0, sizeof(CFunction *) * m_plugin.num_publics); - m_PubJitFuncs = new JitFunction *[m_plugin.num_publics]; - memset(m_PubJitFuncs, 0, sizeof(JitFunction *) * m_plugin.num_publics); + m_PubFuncs = new ScriptedInvoker *[m_plugin.num_publics]; + memset(m_PubFuncs, 0, sizeof(ScriptedInvoker *) * m_plugin.num_publics); + m_PubJitFuncs = new Function *[m_plugin.num_publics]; + memset(m_PubJitFuncs, 0, sizeof(Function *) * m_plugin.num_publics); } MD5 md5_pcode; @@ -298,18 +298,18 @@ int BaseRuntime::CreateFromMemory(sp_file_hdr_t *hdr, uint8_t *base) SetupFloatNativeRemapping(); function_map_size_ = m_plugin.pcode_size / sizeof(cell_t) + 1; - function_map_ = new JitFunction *[function_map_size_]; - memset(function_map_, 0, function_map_size_ * sizeof(JitFunction *)); + function_map_ = new Function *[function_map_size_]; + memset(function_map_, 0, function_map_size_ * sizeof(Function *)); return SP_ERROR_NONE; } void -BaseRuntime::AddJittedFunction(JitFunction *fn) +BaseRuntime::AddJittedFunction(Function *fn) { m_JitFunctions.append(fn); - cell_t pcode_offset = fn->GetPCodeAddress(); + cell_t pcode_offset = fn->GetCodeOffset(); assert(pcode_offset % 4 == 0); uint32_t pcode_index = pcode_offset / 4; @@ -318,7 +318,7 @@ BaseRuntime::AddJittedFunction(JitFunction *fn) function_map_[pcode_index] = fn; } -JitFunction * +Function * BaseRuntime::GetJittedFunctionByOffset(cell_t pcode_offset) { assert(pcode_offset % 4 == 0); @@ -483,7 +483,7 @@ BaseRuntime::GetDebugInfo() IPluginFunction * BaseRuntime::GetFunctionById(funcid_t func_id) { - CFunction *pFunc = NULL; + ScriptedInvoker *pFunc = NULL; if (func_id & 1) { func_id >>= 1; @@ -491,7 +491,7 @@ BaseRuntime::GetFunctionById(funcid_t func_id) return NULL; pFunc = m_PubFuncs[func_id]; if (!pFunc) { - m_PubFuncs[func_id] = new CFunction(this, (func_id << 1) | 1, func_id); + m_PubFuncs[func_id] = new ScriptedInvoker(this, (func_id << 1) | 1, func_id); pFunc = m_PubFuncs[func_id]; } } @@ -499,15 +499,15 @@ BaseRuntime::GetFunctionById(funcid_t func_id) return pFunc; } -CFunction * +ScriptedInvoker * BaseRuntime::GetPublicFunction(size_t index) { - CFunction *pFunc = m_PubFuncs[index]; + ScriptedInvoker *pFunc = m_PubFuncs[index]; if (!pFunc) { sp_public_t *pub = NULL; GetPublicByIndex(index, &pub); if (pub) - m_PubFuncs[index] = new CFunction(this, (index << 1) | 1, index); + m_PubFuncs[index] = new ScriptedInvoker(this, (index << 1) | 1, index); pFunc = m_PubFuncs[index]; } diff --git a/sourcepawn/jit/BaseRuntime.h b/sourcepawn/jit/BaseRuntime.h index 6ee331be..80d87a09 100644 --- a/sourcepawn/jit/BaseRuntime.h +++ b/sourcepawn/jit/BaseRuntime.h @@ -6,10 +6,10 @@ #include #include #include "jit_shared.h" +#include "jit_function.h" #include "sp_vm_function.h" class BaseContext; -class JitFunction; class DebugInfo : public IPluginDebugInfo { @@ -67,11 +67,11 @@ class BaseRuntime virtual size_t GetMemUsage(); virtual unsigned char *GetCodeHash(); virtual unsigned char *GetDataHash(); - JitFunction *GetJittedFunctionByOffset(cell_t pcode_offset); - void AddJittedFunction(JitFunction *fn); + Function *GetJittedFunctionByOffset(cell_t pcode_offset); + void AddJittedFunction(Function *fn); void SetName(const char *name); unsigned GetNativeReplacement(size_t index); - CFunction *GetPublicFunction(size_t index); + ScriptedInvoker *GetPublicFunction(size_t index); BaseContext *GetBaseContext(); const sp_plugin_t *plugin() const { @@ -81,7 +81,7 @@ class BaseRuntime size_t NumJitFunctions() const { return m_JitFunctions.length(); } - JitFunction *GetJitFunction(size_t i) const { + Function *GetJitFunction(size_t i) const { return m_JitFunctions[i]; } @@ -94,15 +94,15 @@ class BaseRuntime unsigned int m_NumFuncs; unsigned int m_MaxFuncs; floattbl_t *float_table_; - JitFunction **function_map_; + Function **function_map_; size_t function_map_size_; - ke::Vector m_JitFunctions; + ke::Vector m_JitFunctions; public: DebugInfo m_Debug; BaseContext *m_pCtx; - CFunction **m_PubFuncs; - JitFunction **m_PubJitFuncs; + ScriptedInvoker **m_PubFuncs; + Function **m_PubJitFuncs; private: ICompilation *co_; diff --git a/sourcepawn/jit/Makefile.shell b/sourcepawn/jit/Makefile.shell index 127f0aaa..192a64f4 100644 --- a/sourcepawn/jit/Makefile.shell +++ b/sourcepawn/jit/Makefile.shell @@ -58,7 +58,7 @@ INCLUDE = -I. -I.. -I$(SMSDK)/public -I$(SMSDK)/public/jit -I$(SMSDK)/public/jit CFLAGS += -D_LINUX -Dstricmp=strcasecmp -D_stricmp=strcasecmp -D_strnicmp=strncasecmp -Dstrnicmp=strncasecmp \ -D_snprintf=snprintf -D_vsnprintf=vsnprintf -D_alloca=alloca -Dstrcmpi=strcasecmp -Wall -DHAVE_STDINT_H \ -m32 -Wno-uninitialized -Werror -DSPSHELL -ggdb3 -Wno-unused -CXXFLAGS += -Wno-non-virtual-dtor -fno-exceptions -fno-rtti -Wno-delete-non-virtual-dtor +CXXFLAGS += -Wno-non-virtual-dtor -fno-exceptions -fno-rtti -Wno-delete-non-virtual-dtor -std=c++11 ################################################ ### DO NOT EDIT BELOW HERE FOR MOST PROJECTS ### diff --git a/sourcepawn/jit/jit_function.cpp b/sourcepawn/jit/jit_function.cpp index ffba3124..0ffafa0b 100644 --- a/sourcepawn/jit/jit_function.cpp +++ b/sourcepawn/jit/jit_function.cpp @@ -1,31 +1,27 @@ -// vim: set ts=8 ts=2 sw=2 tw=99 et: +// vim: set sts=2 ts=8 sw=2 tw=99 et: +// +// Copyright (C) 2006-2015 AlliedModders LLC +// +// This file is part of SourcePawn. SourcePawn is free software: you can +// redistribute it and/or modify it under the terms of the GNU General Public +// License as published by the Free Software Foundation, either version 3 of +// the License, or (at your option) any later version. +// +// You should have received a copy of the GNU General Public License along with +// SourcePawn. If not, see http://www.gnu.org/licenses/. +// #include "jit_function.h" #include "sp_vm_engine.h" #include "jit_x86.h" -JitFunction::JitFunction(void *entry_addr, cell_t pcode_offs, LoopEdge *edges, uint32_t nedges) - : m_pEntryAddr(entry_addr), - m_PcodeOffs(pcode_offs), - edges_(edges), - nedges_(nedges) +Function::Function(void *entry_addr, cell_t pcode_offs, FixedArray *edges) + : entry_(entry_addr), + code_offset_(pcode_offs), + edges_(edges) { } -JitFunction::~JitFunction() +Function::~Function() { - delete [] edges_; - g_Jit.FreeCode(m_pEntryAddr); + g_Jit.FreeCode(entry_); } - -void * -JitFunction::GetEntryAddress() const -{ - return m_pEntryAddr; -} - -cell_t -JitFunction::GetPCodeAddress() const -{ - return m_PcodeOffs; -} - diff --git a/sourcepawn/jit/jit_function.h b/sourcepawn/jit/jit_function.h index 81465425..64690cc1 100644 --- a/sourcepawn/jit/jit_function.h +++ b/sourcepawn/jit/jit_function.h @@ -1,9 +1,25 @@ -// vim: set ts=8 sts=2 sw=2 tw=99 et: +// vim: set sts=2 ts=8 sw=2 tw=99 et: +// +// Copyright (C) 2006-2015 AlliedModders LLC +// +// This file is part of SourcePawn. SourcePawn is free software: you can +// redistribute it and/or modify it under the terms of the GNU General Public +// License as published by the Free Software Foundation, either version 3 of +// the License, or (at your option) any later version. +// +// You should have received a copy of the GNU General Public License along with +// SourcePawn. If not, see http://www.gnu.org/licenses/. +// #ifndef _INCLUDE_SOURCEPAWN_JIT2_FUNCTION_H_ #define _INCLUDE_SOURCEPAWN_JIT2_FUNCTION_H_ #include -#include +#include +#include + +namespace sp { + +using namespace ke; struct LoopEdge { @@ -11,28 +27,32 @@ struct LoopEdge int32_t disp32; }; -class JitFunction +class Function { public: - JitFunction(void *entry_addr, cell_t pcode_offs, LoopEdge *edges, uint32_t nedges); - ~JitFunction(); + Function(void *entry_addr, cell_t pcode_offs, FixedArray *edges); + ~Function(); public: - void *GetEntryAddress() const; - cell_t GetPCodeAddress() const; + void *GetEntryAddress() const { + return entry_; + } + cell_t GetCodeOffset() const { + return code_offset_; + } uint32_t NumLoopEdges() const { - return nedges_; + return edges_->length(); } const LoopEdge &GetLoopEdge(size_t i) const { - assert(i < nedges_); - return edges_[i]; + return edges_->at(i); } private: - void *m_pEntryAddr; - cell_t m_PcodeOffs; - LoopEdge *edges_; - uint32_t nedges_; + void *entry_; + cell_t code_offset_; + AutoPtr> edges_; }; +} + #endif //_INCLUDE_SOURCEPAWN_JIT2_FUNCTION_H_ diff --git a/sourcepawn/jit/sp_vm_basecontext.cpp b/sourcepawn/jit/sp_vm_basecontext.cpp index f7a9e10b..34d8f837 100644 --- a/sourcepawn/jit/sp_vm_basecontext.cpp +++ b/sourcepawn/jit/sp_vm_basecontext.cpp @@ -525,7 +525,7 @@ BaseContext::Execute2(IPluginFunction *function, const cell_t *params, unsigned int ir; int serial; cell_t *sp; - JitFunction *fn; + Function *fn; cell_t _ignore_result; EnterProfileScope profileScope("SourcePawn", "EnterJIT"); @@ -538,7 +538,7 @@ BaseContext::Execute2(IPluginFunction *function, const cell_t *params, unsigned return SP_ERROR_INVALID_ADDRESS; unsigned public_id = fnid >> 1; - CFunction *cfun = m_pRuntime->GetPublicFunction(public_id); + ScriptedInvoker *cfun = m_pRuntime->GetPublicFunction(public_id); if (!cfun) return SP_ERROR_NOT_FOUND; diff --git a/sourcepawn/jit/sp_vm_engine.h b/sourcepawn/jit/sp_vm_engine.h index 8188413c..2957f8dc 100644 --- a/sourcepawn/jit/sp_vm_engine.h +++ b/sourcepawn/jit/sp_vm_engine.h @@ -68,8 +68,6 @@ class SourcePawnEngine : public ISourcePawnEngine void ReportError(BaseRuntime *runtime, int err, const char *errstr, cell_t rp_start); public: //Plugin function stuff - CFunction *GetFunctionFromPool(funcid_t f, IPluginContext *plugin); - void ReleaseFunctionToPool(CFunction *func); IDebugListener *GetDebugHook(); private: diff --git a/sourcepawn/jit/sp_vm_function.cpp b/sourcepawn/jit/sp_vm_function.cpp index ba5ea4e0..354065fb 100644 --- a/sourcepawn/jit/sp_vm_function.cpp +++ b/sourcepawn/jit/sp_vm_function.cpp @@ -20,36 +20,36 @@ * FUNCTION CALLING * ********************/ -CFunction::~CFunction() +ScriptedInvoker::~ScriptedInvoker() { delete [] full_name_; } bool -CFunction::IsRunnable() +ScriptedInvoker::IsRunnable() { return !m_pRuntime->IsPaused(); } int -CFunction::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result) +ScriptedInvoker::CallFunction(const cell_t *params, unsigned int num_params, cell_t *result) { return CallFunction2(m_pRuntime->GetDefaultContext(), params, num_params, result); } int -CFunction::CallFunction2(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result) +ScriptedInvoker::CallFunction2(IPluginContext *pContext, const cell_t *params, unsigned int num_params, cell_t *result) { return pContext->Execute2(this, params, num_params, result); } IPluginContext * -CFunction::GetParentContext() +ScriptedInvoker::GetParentContext() { return m_pRuntime->GetDefaultContext(); } -CFunction::CFunction(BaseRuntime *runtime, funcid_t id, uint32_t pub_id) +ScriptedInvoker::ScriptedInvoker(BaseRuntime *runtime, funcid_t id, uint32_t pub_id) : m_curparam(0), m_errorstate(SP_ERROR_NONE), m_FnId(id) @@ -67,7 +67,7 @@ CFunction::CFunction(BaseRuntime *runtime, funcid_t id, uint32_t pub_id) strcpy(&full_name_[rt_len + 2], public_->name); } -int CFunction::PushCell(cell_t cell) +int ScriptedInvoker::PushCell(cell_t cell) { if (m_curparam >= SP_MAX_EXEC_PARAMS) return SetError(SP_ERROR_PARAMS_MAX); @@ -80,13 +80,13 @@ int CFunction::PushCell(cell_t cell) } int -CFunction::PushCellByRef(cell_t *cell, int flags) +ScriptedInvoker::PushCellByRef(cell_t *cell, int flags) { return PushArray(cell, 1, flags); } int -CFunction::PushFloat(float number) +ScriptedInvoker::PushFloat(float number) { cell_t val = *(cell_t *)&number; @@ -94,13 +94,13 @@ CFunction::PushFloat(float number) } int -CFunction::PushFloatByRef(float *number, int flags) +ScriptedInvoker::PushFloatByRef(float *number, int flags) { return PushCellByRef((cell_t *)number, flags); } int -CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback) +ScriptedInvoker::PushArray(cell_t *inarray, unsigned int cells, int copyback) { if (m_curparam >= SP_MAX_EXEC_PARAMS) { @@ -121,19 +121,19 @@ CFunction::PushArray(cell_t *inarray, unsigned int cells, int copyback) } int -CFunction::PushString(const char *string) +ScriptedInvoker::PushString(const char *string) { return _PushString(string, SM_PARAM_STRING_COPY, 0, strlen(string)+1); } int -CFunction::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags) +ScriptedInvoker::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags) { return _PushString(buffer, sz_flags, cp_flags, length); } int -CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t len) +ScriptedInvoker::_PushString(const char *string, int sz_flags, int cp_flags, size_t len) { if (m_curparam >= SP_MAX_EXEC_PARAMS) return SetError(SP_ERROR_PARAMS_MAX); @@ -153,7 +153,7 @@ CFunction::_PushString(const char *string, int sz_flags, int cp_flags, size_t le } void -CFunction::Cancel() +ScriptedInvoker::Cancel() { if (!m_curparam) return; @@ -163,13 +163,13 @@ CFunction::Cancel() } int -CFunction::Execute(cell_t *result) +ScriptedInvoker::Execute(cell_t *result) { return Execute2(m_pRuntime->GetDefaultContext(), result); } int -CFunction::Execute2(IPluginContext *ctx, cell_t *result) +ScriptedInvoker::Execute2(IPluginContext *ctx, cell_t *result) { int err = SP_ERROR_NONE; @@ -303,19 +303,19 @@ CFunction::Execute2(IPluginContext *ctx, cell_t *result) } IPluginRuntime * -CFunction::GetParentRuntime() +ScriptedInvoker::GetParentRuntime() { return m_pRuntime; } funcid_t -CFunction::GetFunctionID() +ScriptedInvoker::GetFunctionID() { return m_FnId; } int -CFunction::SetError(int err) +ScriptedInvoker::SetError(int err) { m_errorstate = err; diff --git a/sourcepawn/jit/sp_vm_function.h b/sourcepawn/jit/sp_vm_function.h index ea1a722d..2a489c88 100644 --- a/sourcepawn/jit/sp_vm_function.h +++ b/sourcepawn/jit/sp_vm_function.h @@ -34,15 +34,14 @@ struct ParamInfo }; class CPlugin; -class JitFunction; -class CFunction : public IPluginFunction +class ScriptedInvoker : public IPluginFunction { friend class SourcePawnEngine; public: - CFunction(BaseRuntime *pRuntime, funcid_t fnid, uint32_t pub_id); - ~CFunction(); + ScriptedInvoker(BaseRuntime *pRuntime, funcid_t fnid, uint32_t pub_id); + ~ScriptedInvoker(); public: virtual int PushCell(cell_t cell); diff --git a/sourcepawn/jit/x86/jit_x86.cpp b/sourcepawn/jit/x86/jit_x86.cpp index 26e4098f..482e0038 100644 --- a/sourcepawn/jit/x86/jit_x86.cpp +++ b/sourcepawn/jit/x86/jit_x86.cpp @@ -280,7 +280,7 @@ CompileFromThunk(BaseRuntime *runtime, cell_t pcode_offs, void **addrp, char *pc if (!g_WatchdogTimer.HandleInterrupt()) return SP_ERROR_TIMEOUT; - JitFunction *fn = runtime->GetJittedFunctionByOffset(pcode_offs); + Function *fn = runtime->GetJittedFunctionByOffset(pcode_offs); if (!fn) { int err; fn = g_Jit.CompileFunction(runtime, pcode_offs, &err); @@ -320,7 +320,7 @@ Compiler::~Compiler() delete [] jump_map_; } -JitFunction * +Function * Compiler::emit(int *errp) { if (cip_ >= code_end_ || *cip_ != OP_PROC) { @@ -375,13 +375,14 @@ Compiler::emit(int *errp) return NULL; } - LoopEdge *edges = new LoopEdge[backward_jumps_.length()]; + AutoPtr> edges( + new FixedArray(backward_jumps_.length())); for (size_t i = 0; i < backward_jumps_.length(); i++) { - edges[i].offset = backward_jumps_[i]; - edges[i].disp32 = *reinterpret_cast(code + edges[i].offset - 4); + edges->at(i).offset = backward_jumps_[i]; + edges->at(i).disp32 = *reinterpret_cast(code + edges->at(i).offset - 4); } - return new JitFunction(code, pcode_start_, edges, backward_jumps_.length()); + return new Function(code, pcode_start_, edges.take()); } bool @@ -1479,7 +1480,7 @@ Compiler::emitCall() // Store the CIP of the function we're about to call. __ movl(Operand(cipAddr()), offset); - JitFunction *fun = rt_->GetJittedFunctionByOffset(offset); + Function *fun = rt_->GetJittedFunctionByOffset(offset); if (!fun) { // Need to emit a delayed thunk. CallThunk *thunk = new CallThunk(offset); @@ -1925,11 +1926,11 @@ JITX86::ShutdownJIT() KE_DestroyCodeCache(g_pCodeCache); } -JitFunction * +Function * JITX86::CompileFunction(BaseRuntime *prt, cell_t pcode_offs, int *err) { Compiler cc(prt, pcode_offs); - JitFunction *fun = cc.emit(err); + Function *fun = cc.emit(err); if (!fun) return NULL; @@ -2029,12 +2030,12 @@ CompData::SetOption(const char *key, const char *val) } int -JITX86::InvokeFunction(BaseRuntime *runtime, JitFunction *fn, cell_t *result) +JITX86::InvokeFunction(BaseRuntime *runtime, Function *fn, cell_t *result) { sp_context_t *ctx = runtime->GetBaseContext()->GetCtx(); // Note that cip, hp, sp are saved and restored by Execute2(). - ctx->cip = fn->GetPCodeAddress(); + ctx->cip = fn->GetCodeOffset(); JIT_EXECUTE pfn = (JIT_EXECUTE)m_pJitEntry; @@ -2080,7 +2081,7 @@ JITX86::PatchAllJumpsForTimeout() for (ke::InlineList::iterator iter = runtimes_.begin(); iter != runtimes_.end(); iter++) { BaseRuntime *rt = *iter; for (size_t i = 0; i < rt->NumJitFunctions(); i++) { - JitFunction *fun = rt->GetJitFunction(i); + Function *fun = rt->GetJitFunction(i); uint8_t *base = reinterpret_cast(fun->GetEntryAddress()); for (size_t j = 0; j < fun->NumLoopEdges(); j++) { @@ -2099,7 +2100,7 @@ JITX86::UnpatchAllJumpsFromTimeout() for (ke::InlineList::iterator iter = runtimes_.begin(); iter != runtimes_.end(); iter++) { BaseRuntime *rt = *iter; for (size_t i = 0; i < rt->NumJitFunctions(); i++) { - JitFunction *fun = rt->GetJitFunction(i); + Function *fun = rt->GetJitFunction(i); uint8_t *base = reinterpret_cast(fun->GetEntryAddress()); for (size_t j = 0; j < fun->NumLoopEdges(); j++) { diff --git a/sourcepawn/jit/x86/jit_x86.h b/sourcepawn/jit/x86/jit_x86.h index daaec0f2..f84ce106 100644 --- a/sourcepawn/jit/x86/jit_x86.h +++ b/sourcepawn/jit/x86/jit_x86.h @@ -88,7 +88,7 @@ class Compiler Compiler(BaseRuntime *rt, cell_t pcode_offs); ~Compiler(); - JitFunction *emit(int *errp); + Function *emit(int *errp); private: bool setup(cell_t pcode_offs); @@ -160,9 +160,9 @@ class JITX86 void FreeContextVars(sp_context_t *ctx); SPVM_NATIVE_FUNC CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData); void DestroyFakeNative(SPVM_NATIVE_FUNC func); - JitFunction *CompileFunction(BaseRuntime *runtime, cell_t pcode_offs, int *err); + Function *CompileFunction(BaseRuntime *runtime, cell_t pcode_offs, int *err); ICompilation *ApplyOptions(ICompilation *_IN, ICompilation *_OUT); - int InvokeFunction(BaseRuntime *runtime, JitFunction *fn, cell_t *result); + int InvokeFunction(BaseRuntime *runtime, Function *fn, cell_t *result); void RegisterRuntime(BaseRuntime *rt); void DeregisterRuntime(BaseRuntime *rt);