sourcemod/sourcepawn/jit/api.h
David Anderson a1afa23bc4 Implement a new stack and error handling model for the SourcePawn VM.
This has three major changes to SourcePawn. First, the API now supports the concept of "exceptions". The exception state is a global property of an instance of the SourcePawn VM. Exceptions can be caught or suppressed. Many places in SourceMod have been updated to check exceptions instead of errors.

The new API obsoletes major parts of the embedder API - all but one method of invoking functions is obsoleted, and the debug interface has been scrapped. Extensions using the native API will not be affected, however, ThrowNativeError has been deprecated in favor of ReportError.

Second, the SourcePawn concept of a "stack" has been unified at the API level. A stack frame iterator now iterates over all SourcePawn invocations, rather than the topmost plugin. This makes error handling more consistent and removes another dependency on context-per-plugin.

Finally, the implementation of stack frames has been changed dramatically. Rather than maintain a complicated and expensive return pointer stack, we now rely on the implicit one provided by the CPU. The stack frame iterator now walks the JIT stack directly. This removes many unnecessary bookkeeping instructions from the generated code, in particular making the CALL instruction 40% faster.

These changes required some fair surgery to the JIT. Its error paths are now slightly more complicated, as they have to throw an exception rather than return an error code. In addition, any path that can throw an exception is now responsible for creating an "exit frame", which exists to tell the stack frame iterator about transitions from the JIT to the VM.
2015-03-04 23:45:30 -08:00

80 lines
3.1 KiB
C++

// 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_vm_api_h_
#define _include_sourcepawn_vm_api_h_
#include <sp_vm_api.h>
#include <am-utility.h> // Replace with am-cxx later.
namespace sp {
using namespace SourcePawn;
class SourcePawnEngine : public ISourcePawnEngine
{
public:
SourcePawnEngine();
sp_plugin_t *LoadFromFilePointer(FILE *fp, int *err) KE_OVERRIDE;
sp_plugin_t *LoadFromMemory(void *base, sp_plugin_t *plugin, int *err) KE_OVERRIDE;
int FreeFromMemory(sp_plugin_t *plugin) KE_OVERRIDE;
void *BaseAlloc(size_t size) KE_OVERRIDE;
void BaseFree(void *memory) KE_OVERRIDE;
void *ExecAlloc(size_t size) KE_OVERRIDE;
void ExecFree(void *address) KE_OVERRIDE;
IDebugListener *SetDebugListener(IDebugListener *pListener) KE_OVERRIDE;
unsigned int GetContextCallCount() KE_OVERRIDE;
unsigned int GetEngineAPIVersion() KE_OVERRIDE;
void *AllocatePageMemory(size_t size) KE_OVERRIDE;
void SetReadWrite(void *ptr) KE_OVERRIDE;
void SetReadExecute(void *ptr) KE_OVERRIDE;
void FreePageMemory(void *ptr) KE_OVERRIDE;
void SetReadWriteExecute(void *ptr);
const char *GetErrorString(int err);
};
class SourcePawnEngine2 : public ISourcePawnEngine2
{
public:
SourcePawnEngine2();
unsigned int GetAPIVersion() KE_OVERRIDE;
const char *GetEngineName() KE_OVERRIDE;
const char *GetVersionString() KE_OVERRIDE;
IPluginRuntime *LoadPlugin(ICompilation *co, const char *file, int *err) KE_OVERRIDE;
SPVM_NATIVE_FUNC CreateFakeNative(SPVM_FAKENATIVE_FUNC callback, void *pData) KE_OVERRIDE;
void DestroyFakeNative(SPVM_NATIVE_FUNC func) KE_OVERRIDE;
IDebugListener *SetDebugListener(IDebugListener *listener) KE_OVERRIDE;
ICompilation *StartCompilation() KE_OVERRIDE;
const char *GetErrorString(int err) KE_OVERRIDE;
bool Initialize() KE_OVERRIDE;
void Shutdown() KE_OVERRIDE;
IPluginRuntime *CreateEmptyRuntime(const char *name, uint32_t memory) KE_OVERRIDE;
bool InstallWatchdogTimer(size_t timeout_ms) KE_OVERRIDE;
bool SetJitEnabled(bool enabled) KE_OVERRIDE;
bool IsJitEnabled() KE_OVERRIDE;
void SetProfiler(IProfiler *profiler) KE_OVERRIDE;
void EnableProfiling() KE_OVERRIDE;
void DisableProfiling() KE_OVERRIDE;
void SetProfilingTool(IProfilingTool *tool) KE_OVERRIDE;
IPluginRuntime *LoadBinaryFromFile(const char *file, char *error, size_t maxlength) KE_OVERRIDE;
ISourcePawnEnvironment *Environment() KE_OVERRIDE;
};
extern size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
extern size_t UTIL_FormatVA(char *buffer, size_t maxlength, const char *fmt, va_list ap);
} // namespace sp
#endif // _include_sourcepawn_vm_api_h_