public/sourcepawn/sp_vm_types.h
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 1089 275107f9ca95
child 1231 17badaf66ce4
permissions -rw-r--r--
Removed strange debugging code
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourcePawn, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * All rights reserved.
     6  * ================================================================
     7  *
     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.
    13  * 
    14  * To view the latest information, see: http://www.sourcemod.net/license.php
    15  *
    16  * Version: $Id$
    17  */
    18 
    19 #ifndef _INCLUDE_SOURCEPAWN_VM_TYPES_H
    20 #define _INCLUDE_SOURCEPAWN_VM_TYPES_H
    21 
    22 /**
    23  * @file sp_vm_types.h
    24  * @brief Contains all run-time SourcePawn structures.
    25  */
    26 
    27 #include "sp_file_headers.h"
    28 
    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 */
    32 
    33 #include "sp_typeutil.h"
    34 
    35 #define SP_MAX_EXEC_PARAMS				32	/**< Maximum number of parameters in a function */
    36 
    37 /**
    38  * @brief Error codes for SourcePawn routines.
    39  */
    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	/**< Not 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! */
    67 
    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
    72  ***  binary.
    73  **********************************************/
    74 
    75 /**
    76  * @brief Information about the core plugin tables.  These may or may not be present!
    77  */
    78 typedef struct sp_plugin_infotab_s
    79 {
    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;
    88 
    89 /**
    90  * @brief Information about the plugin's debug tables.  These are all present if one is present.
    91  */
    92 typedef struct sp_plugin_debug_s
    93 {
    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 */
   101 } sp_plugin_debug_t;
   102 
   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 */
   105 
   106 /**
   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.
   109  */
   110 typedef struct sp_plugin_s
   111 {
   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 */
   122 } sp_plugin_t;
   123 
   124 
   125 namespace SourcePawn
   126 {
   127 	class IPluginContext;
   128 	class IVirtualMachine;
   129 };
   130 
   131 struct sp_context_s;
   132 
   133 /**
   134  * @brief Native callback prototype, passed a context and a parameter stack (0=count, 1+=args).  
   135  * A cell must be returned.
   136  */
   137 typedef cell_t (*SPVM_NATIVE_FUNC)(SourcePawn::IPluginContext *, const cell_t *);
   138 
   139 /**
   140  * @brief Fake native callback prototype, passed a context, parameter stack, and private data.
   141  * A cell must be returned.
   142  */
   143 typedef cell_t (*SPVM_FAKENATIVE_FUNC)(SourcePawn::IPluginContext *, const cell_t *, void *);
   144 
   145 /**********************************************
   146  *** The following structures are bound to the VM/JIT.
   147  *** Changing them will result in necessary recompilation.
   148  **********************************************/
   149 
   150 /**
   151  * @brief Offsets and names to a public function.
   152  */
   153 typedef struct sp_public_s
   154 {
   155 	funcid_t	funcid;		/**< Encoded function id */
   156 	uint32_t	code_offs;	/**< Relocated code offset */
   157 	const char *name;		/**< Name of function */
   158 } sp_public_t;
   159 
   160 /**
   161  * @brief Offsets and names to public variables.
   162  *
   163  * The offset is relocated and the name by default points back to the sp_plugin_infotab_t structure.
   164  */
   165 typedef struct sp_pubvar_s
   166 {
   167 	cell_t	   *offs;		/**< Pointer to data */
   168 	const char *name;		/**< Name */
   169 } sp_pubvar_t;
   170 
   171 #define SP_NATIVE_UNBOUND		(0)		/**< Native is undefined */
   172 #define SP_NATIVE_BOUND			(1)		/**< Native is bound */
   173 
   174 #define SP_NTVFLAG_OPTIONAL		(1<<0)	/**< Native is optional */
   175 
   176 /** 
   177  * @brief Native lookup table, by default names point back to the sp_plugin_infotab_t structure.
   178  */
   179 typedef struct sp_native_s
   180 {
   181 	SPVM_NATIVE_FUNC	pfn;	/**< Function pointer */
   182 	const char *		name;	/**< Name of function */
   183 	uint32_t			status;	/**< Status flags */
   184 	uint32_t			flags;	/**< Native flags */
   185 } sp_native_t;
   186 
   187 /** 
   188  * @brief Used for setting natives from modules/host apps.
   189  */
   190 typedef struct sp_nativeinfo_s
   191 {
   192 	const char		*name;	/**< Name of the native */
   193 	SPVM_NATIVE_FUNC func;	/**< Address of native implementation */
   194 } sp_nativeinfo_t;
   195 
   196 /** 
   197  * @brief Run-time debug file table
   198  */
   199 typedef struct sp_debug_file_s
   200 {
   201 	uint32_t		addr;	/**< Address into code */
   202 	const char *	name;	/**< Name of file */
   203 } sp_debug_file_t;
   204 
   205 /**
   206  * @brief Contains run-time debug line table.
   207  */
   208 typedef struct sp_debug_line_s
   209 {
   210 	uint32_t		addr;	/**< Address into code */
   211 	uint32_t		line;	/**< Line number */
   212 } sp_debug_line_t;
   213 
   214 /**
   215  * @brief These structures are equivalent.
   216  */
   217 typedef sp_fdbg_arraydim_t	sp_debug_arraydim_t;
   218 
   219 /**
   220  * @brief The majority of this struct is already located in the parent 
   221  * block.  Thus, only the relocated portions are required.
   222  */
   223 typedef struct sp_debug_symbol_s
   224 {
   225 	uint32_t		codestart;	/**< Relocated code address */
   226 	uint32_t		codeend;	/**< Relocated code end address */
   227 	const char *	name;		/**< Relocated name */
   228 	sp_debug_arraydim_t *dims;	/**< Relocated dimension struct, if any */
   229 	sp_fdbg_symbol_t	*sym;	/**< Pointer to original symbol */
   230 } sp_debug_symbol_t;
   231 
   232 /**
   233  * Breaks into a debugger
   234  * Params:
   235  *  [0] - plugin context
   236  *  [1] - frm
   237  *  [2] - cip
   238  */
   239 typedef int (*SPVM_DEBUGBREAK)(struct sp_context_s *, uint32_t, uint32_t);
   240 
   241 #define SPFLAG_PLUGIN_DEBUG		(1<<0)		/**< plugin is in debug mode */
   242 #define SPFLAG_PLUGIN_PAUSED	(1<<1)		/**< plugin is "paused" (blocked from executing) */
   243 
   244 /**
   245  * @brief This is the heart of the VM.  It contains all of the runtime 
   246  * information about a plugin context. Note that user[0..3] can be used for any user based pointers.  
   247  * However, vm[0..3] should not be touched, as it is reserved for the VM.
   248  */
   249 typedef struct sp_context_s
   250 {
   251 	void			*codebase;	/**< Base of generated code and memory */
   252 	sp_plugin_t		*plugin;	/**< Pointer back to parent information */
   253 	SourcePawn::IPluginContext *context;	/**< Pointer to IPluginContext */
   254 	SourcePawn::IVirtualMachine *vmbase;	/**< Pointer to IVirtualMachine */
   255 	void			*user[4];	/**< User specific pointers */
   256 	void			*vm[4];		/**< VM specific pointers */
   257 	uint32_t		flags;		/**< Compilation flags */
   258 	SPVM_DEBUGBREAK dbreak;		/**< Debug break function */
   259 	uint8_t			*memory;	/**< Data chunk */
   260 	ucell_t			mem_size;	/**< Total memory size; */
   261 	cell_t			data_size;	/**< Data chunk size, always starts at 0 */
   262 	cell_t			heap_base;	/**< Where the heap starts */
   263 	cell_t			hp;			/**< Heap pointer */
   264 	cell_t			sp;			/**< Stack pointer */
   265 	cell_t			frm;		/**< Frame pointer */
   266 	uint32_t		pushcount;	/**< Push count */
   267 	int32_t			n_err;		/**< Error code set by a native */
   268 	uint32_t		n_idx;		/**< Current native index being executed */
   269 	sp_public_t		*publics;	/**< Public functions table */
   270 	sp_pubvar_t		*pubvars;	/**< Public variables table */
   271 	sp_native_t		*natives;	/**< Natives table */
   272 	sp_debug_file_t	*files;		/**< Files */
   273 	sp_debug_line_t	*lines;		/**< Lines */
   274 	sp_debug_symbol_t *symbols;	/**< Symbols */
   275 } sp_context_t;
   276 
   277 #endif //_INCLUDE_SOURCEPAWN_VM_TYPES_H