3 * ================================================================
4 * SourcePawn, Copyright (C) 2004-2007 AlliedModders LLC.
6 * ================================================================
8 * This file is part of the SourceMod/SourcePawn SDK. This file may only be
9 * used or modified under the Terms and Conditions of its License Agreement,
10 * which is found in public/licenses/LICENSE.txt. As of this notice, derivative
11 * works must be licensed under the GNU General Public License (version 2 or
12 * greater). A copy of the GPL is included under public/licenses/GPL.txt.
14 * To view the latest information, see: http://www.sourcemod.net/license.php
19 #ifndef _INCLUDE_SOURCEPAWN_VM_TYPES_H
20 #define _INCLUDE_SOURCEPAWN_VM_TYPES_H
24 * @brief Contains all run-time SourcePawn structures.
27 #include "sp_file_headers.h"
29 typedef uint32_t ucell_t; /**< Unsigned 32bit integer */
30 typedef int32_t cell_t; /**< Basic 32bit signed integer type for plugins */
31 typedef uint32_t funcid_t; /**< Function index code */
33 #include "sp_typeutil.h"
35 #define SP_MAX_EXEC_PARAMS 32 /**< Maximum number of parameters in a function */
38 * @brief Error codes for SourcePawn routines.
40 #define SP_ERROR_NONE 0 /**< No error occurred */
41 #define SP_ERROR_FILE_FORMAT 1 /**< File format unrecognized */
42 #define SP_ERROR_DECOMPRESSOR 2 /**< A decompressor was not found */
43 #define SP_ERROR_HEAPLOW 3 /**< Not enough space left on the heap */
44 #define SP_ERROR_PARAM 4 /**< Invalid parameter or parameter type */
45 #define SP_ERROR_INVALID_ADDRESS 5 /**< A memory address was not valid */
46 #define SP_ERROR_NOT_FOUND 6 /**< The object in question was not found */
47 #define SP_ERROR_INDEX 7 /**< Invalid index parameter */
48 #define SP_ERROR_STACKLOW 8 /**< Nnot enough space left on the stack */
49 #define SP_ERROR_NOTDEBUGGING 9 /**< Debug mode was not on or debug section not found */
50 #define SP_ERROR_INVALID_INSTRUCTION 10 /**< Invalid instruction was encountered */
51 #define SP_ERROR_MEMACCESS 11 /**< Invalid memory access */
52 #define SP_ERROR_STACKMIN 12 /**< Stack went beyond its minimum value */
53 #define SP_ERROR_HEAPMIN 13 /**< Heap went beyond its minimum value */
54 #define SP_ERROR_DIVIDE_BY_ZERO 14 /**< Division by zero */
55 #define SP_ERROR_ARRAY_BOUNDS 15 /**< Array index is out of bounds */
56 #define SP_ERROR_INSTRUCTION_PARAM 16 /**< Instruction had an invalid parameter */
57 #define SP_ERROR_STACKLEAK 17 /**< A native leaked an item on the stack */
58 #define SP_ERROR_HEAPLEAK 18 /**< A native leaked an item on the heap */
59 #define SP_ERROR_ARRAY_TOO_BIG 19 /**< A dynamic array is too big */
60 #define SP_ERROR_TRACKER_BOUNDS 20 /**< Tracker stack is out of bounds */
61 #define SP_ERROR_INVALID_NATIVE 21 /**< Native was pending or invalid */
62 #define SP_ERROR_PARAMS_MAX 22 /**< Maximum number of parameters reached */
63 #define SP_ERROR_NATIVE 23 /**< Error originates from a native */
64 #define SP_ERROR_NOT_RUNNABLE 24 /**< Function or plugin is not runnable */
65 #define SP_ERROR_ABORTED 25 /**< Function call was aborted */
66 //Hey you! Update the string table if you add to the end of me! */
68 /**********************************************
69 *** The following structures are reference structures.
70 *** They are not essential to the API, but are used
71 *** to hold the back end database format of the plugin
73 **********************************************/
76 * @brief Information about the core plugin tables. These may or may not be present!
78 typedef struct sp_plugin_infotab_s
80 const char *stringbase; /**< base of string table */
81 uint32_t publics_num; /**< number of publics */
82 sp_file_publics_t *publics; /**< public table */
83 uint32_t natives_num; /**< number of natives */
84 sp_file_natives_t *natives; /**< native table */
85 uint32_t pubvars_num; /**< number of pubvars */
86 sp_file_pubvars_t *pubvars; /**< pubvars table */
87 } sp_plugin_infotab_t;
90 * @brief Information about the plugin's debug tables. These are all present if one is present.
92 typedef struct sp_plugin_debug_s
94 const char *stringbase; /**< base of string table */
95 uint32_t files_num; /**< number of files */
96 sp_fdbg_file_t *files; /**< files table */
97 uint32_t lines_num; /**< number of lines */
98 sp_fdbg_line_t *lines; /**< lines table */
99 uint32_t syms_num; /**< number of symbols */
100 sp_fdbg_symbol_t *symbols; /**< symbol table */
103 #define SP_FA_SELF_EXTERNAL (1<<0) /**< Allocation of structure is external */
104 #define SP_FA_BASE_EXTERNAL (1<<1) /**< Allocation of base is external */
107 * @brief The rebased memory format of a plugin. This differs from the on-disk structure
108 * to ensure that the format is properly read.
110 typedef struct sp_plugin_s
112 uint8_t *base; /**< Base of memory for this plugin. */
113 uint8_t *pcode; /**< P-Code of plugin */
114 uint32_t pcode_size; /**< Size of p-code */
115 uint8_t *data; /**< Data/memory layout */
116 uint32_t data_size; /**< Size of data */
117 uint32_t memory; /**< Required memory space */
118 uint16_t flags; /**< Code flags */
119 uint32_t allocflags; /**< Allocation flags */
120 sp_plugin_infotab_t info; /**< Base info table */
121 sp_plugin_debug_t debug; /**< Debug info table */
127 class IPluginContext;
128 class IVirtualMachine;
134 * @brief Native callback prototype, passed a context and a parameter stack (0=count, 1+=args).
135 * A cell must be returned.
137 typedef cell_t (*SPVM_NATIVE_FUNC)(SourcePawn::IPluginContext *, const cell_t *);
140 * @brief Fake native callback prototype, passed a context, parameter stack, and private data.
141 * A cell must be returned.
143 typedef cell_t (*SPVM_FAKENATIVE_FUNC)(SourcePawn::IPluginContext *, const cell_t *, void *);
145 /**********************************************
146 *** The following structures are bound to the VM/JIT.
147 *** Changing them will result in necessary recompilation.
148 **********************************************/
151 * @brief Offsets and names to a public function.
153 typedef struct sp_public_s
155 funcid_t funcid; /**< Encoded function id */
156 uint32_t code_offs; /**< Relocated code offset */
157 const char *name; /**< Name of function */
161 * @brief Offsets and names to public variables.
163 * The offset is relocated and the name by default points back to the sp_plugin_infotab_t structure.
165 typedef struct sp_pubvar_s
167 cell_t *offs; /**< Pointer to data */
168 const char *name; /**< Name */
171 #define SP_NATIVE_UNBOUND (0) /**< Native is undefined */
172 #define SP_NATIVE_BOUND (1) /**< Native is bound */
175 * @brief Native lookup table, by default names point back to the sp_plugin_infotab_t structure.
177 typedef struct sp_native_s
179 SPVM_NATIVE_FUNC pfn; /**< Function pointer */
180 const char * name; /**< Name of function */
181 uint32_t status; /**< Status flags */
185 * @brief Used for setting natives from modules/host apps.
187 typedef struct sp_nativeinfo_s
189 const char *name; /**< Name of the native */
190 SPVM_NATIVE_FUNC func; /**< Address of native implementation */
194 * @brief Run-time debug file table
196 typedef struct sp_debug_file_s
198 uint32_t addr; /**< Address into code */
199 const char * name; /**< Name of file */
203 * @brief Contains run-time debug line table.
205 typedef struct sp_debug_line_s
207 uint32_t addr; /**< Address into code */
208 uint32_t line; /**< Line number */
212 * @brief These structures are equivalent.
214 typedef sp_fdbg_arraydim_t sp_debug_arraydim_t;
217 * @brief The majority of this struct is already located in the parent
218 * block. Thus, only the relocated portions are required.
220 typedef struct sp_debug_symbol_s
222 uint32_t codestart; /**< Relocated code address */
223 uint32_t codeend; /**< relocated code end address */
224 const char * name; /**< Relocated name */
225 sp_debug_arraydim_t *dims; /**< Relocated dimension struct, if any */
226 sp_fdbg_symbol_t *sym; /**< Pointer to original symbol */
230 * Breaks into a debugger
232 * [0] - plugin context
236 typedef int (*SPVM_DEBUGBREAK)(struct sp_context_s *, uint32_t, uint32_t);
238 #define SPFLAG_PLUGIN_DEBUG (1<<0) /**< plugin is in debug mode */
239 #define SPFLAG_PLUGIN_PAUSED (1<<1) /**< plugin is "paused" (blocked from executing) */
242 * @brief This is the heart of the VM. It contains all of the runtime
243 * information about a plugin context. Note that user[0..3] can be used for any user based pointers.
244 * However, vm[0..3] should not be touched, as it is reserved for the VM.
246 typedef struct sp_context_s
248 void *codebase; /**< Base of generated code and memory */
249 sp_plugin_t *plugin; /**< Pointer back to parent information */
250 SourcePawn::IPluginContext *context; /**< Pointer to IPluginContext */
251 SourcePawn::IVirtualMachine *vmbase; /**< Pointer to IVirtualMachine */
252 void *user[4]; /**< User specific pointers */
253 void *vm[4]; /**< VM specific pointers */
254 uint32_t flags; /**< Compilation flags */
255 SPVM_DEBUGBREAK dbreak; /**< Debug break function */
256 uint8_t *memory; /**< Data chunk */
257 ucell_t mem_size; /**< Total memory size; */
258 cell_t data_size; /**< Data chunk size, always starts at 0 */
259 cell_t heap_base; /**< Where the heap starts */
260 cell_t hp; /**< Heap pointer */
261 cell_t sp; /**< Stack pointer */
262 cell_t frm; /**< Frame pointer */
263 uint32_t pushcount; /**< Push count */
264 int32_t n_err; /**< Error code set by a native */
265 uint32_t n_idx; /**< Current native index being executed */
266 sp_public_t *publics; /**< Public functions table */
267 sp_pubvar_t *pubvars; /**< Public variables table */
268 sp_native_t *natives; /**< Natives table */
269 sp_debug_file_t *files; /**< Files */
270 sp_debug_line_t *lines; /**< Lines */
271 sp_debug_symbol_t *symbols; /**< Symbols */
274 #endif //_INCLUDE_SOURCEPAWN_VM_TYPES_H