public/sourcepawn/sp_vm_types.h
author David Anderson <dvander@alliedmods.net>
Fri Feb 16 03:47:10 2007 +0000 (2007-02-16)
changeset 501 e209a7296936
parent 381 6cdf2043513c
child 525 d36c45088e36
permissions -rw-r--r--
removed this potential feature, not useful enough
     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	/**< 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 //Hey you! Update the string table if you add to the end of me! */
    66 
    67 /**********************************************
    68  *** The following structures are reference structures.
    69  *** They are not essential to the API, but are used
    70  ***  to hold the back end database format of the plugin
    71  ***  binary.
    72  **********************************************/
    73 
    74 /**
    75  * @brief Information about the core plugin tables.  These may or may not be present!
    76  */
    77 typedef struct sp_plugin_infotab_s
    78 {
    79 	const char *stringbase;		/**< base of string table */
    80 	uint32_t	publics_num;	/**< number of publics */
    81 	sp_file_publics_t *publics;	/**< public table */
    82 	uint32_t	natives_num;	/**< number of natives */
    83 	sp_file_natives_t *natives; /**< native table */
    84 	uint32_t	pubvars_num;	/**< number of pubvars */
    85 	sp_file_pubvars_t *pubvars;	/**< pubvars table */
    86 } sp_plugin_infotab_t;
    87 
    88 /**
    89  * @brief Information about the plugin's debug tables.  These are all present if one is present.
    90  */
    91 typedef struct sp_plugin_debug_s
    92 {
    93 	const char *stringbase;		/**< base of string table */
    94 	uint32_t	files_num;		/**< number of files */
    95 	sp_fdbg_file_t *files;		/**< files table */
    96 	uint32_t	lines_num;		/**< number of lines */
    97 	sp_fdbg_line_t *lines;		/**< lines table */
    98 	uint32_t	syms_num;		/**< number of symbols */
    99 	sp_fdbg_symbol_t *symbols;	/**< symbol table */
   100 } sp_plugin_debug_t;
   101 
   102 #define SP_FA_SELF_EXTERNAL		(1<<0)		/**< Allocation of structure is external */
   103 #define SP_FA_BASE_EXTERNAL		(1<<1)		/**< Allocation of base is external */
   104 
   105 /**
   106  * @brief The rebased memory format of a plugin.  This differs from the on-disk structure 
   107  * to ensure that the format is properly read.
   108  */
   109 typedef struct sp_plugin_s
   110 {
   111 	uint8_t		*base;			/**< Base of memory for this plugin. */
   112 	uint8_t		*pcode;			/**< P-Code of plugin */
   113 	uint32_t	pcode_size;		/**< Size of p-code */
   114 	uint8_t		*data;			/**< Data/memory layout */
   115 	uint32_t	data_size;		/**< Size of data */
   116 	uint32_t	memory;			/**< Required memory space */
   117 	uint16_t	flags;			/**< Code flags */
   118 	uint32_t	allocflags;		/**< Allocation flags */
   119 	sp_plugin_infotab_t info;	/**< Base info table */
   120 	sp_plugin_debug_t   debug;	/**< Debug info table */
   121 } sp_plugin_t;
   122 
   123 
   124 namespace SourcePawn
   125 {
   126 	class IPluginContext;
   127 	class IVirtualMachine;
   128 };
   129 
   130 struct sp_context_s;
   131 
   132 /**
   133  * @brief Native callback prototype, passed a context and a parameter stack (0=count, 1+=args).  
   134  * A cell must be returned.
   135  */
   136 typedef cell_t (*SPVM_NATIVE_FUNC)(SourcePawn::IPluginContext *, const cell_t *);
   137 
   138 /**********************************************
   139  *** The following structures are bound to the VM/JIT.
   140  *** Changing them will result in necessary recompilation.
   141  **********************************************/
   142 
   143 /**
   144  * @brief Offsets and names to a public function.
   145  */
   146 typedef struct sp_public_s
   147 {
   148 	funcid_t	funcid;		/**< Encoded function id */
   149 	uint32_t	code_offs;	/**< Relocated code offset */
   150 	const char *name;		/**< Name of function */
   151 } sp_public_t;
   152 
   153 /**
   154  * @brief Offsets and names to public variables.
   155  *
   156  * The offset is relocated and the name by default points back to the sp_plugin_infotab_t structure.
   157  */
   158 typedef struct sp_pubvar_s
   159 {
   160 	cell_t	   *offs;		/**< Pointer to data */
   161 	const char *name;		/**< Name */
   162 } sp_pubvar_t;
   163 
   164 #define SP_NATIVE_UNBOUND		(0)		/**< Native is undefined */
   165 #define SP_NATIVE_BOUND			(1)		/**< Native is bound */
   166 
   167 /** 
   168  * @brief Native lookup table, by default names point back to the sp_plugin_infotab_t structure.
   169  */
   170 typedef struct sp_native_s
   171 {
   172 	SPVM_NATIVE_FUNC	pfn;	/**< Function pointer */
   173 	const char *		name;	/**< Name of function */
   174 	uint32_t			status;	/**< Status flags */
   175 } sp_native_t;
   176 
   177 /** 
   178  * @brief Used for setting natives from modules/host apps.
   179  */
   180 typedef struct sp_nativeinfo_s
   181 {
   182 	const char		*name;	/**< Name of the native */
   183 	SPVM_NATIVE_FUNC func;	/**< Address of native implementation */
   184 } sp_nativeinfo_t;
   185 
   186 /** 
   187  * @brief Run-time debug file table
   188  */
   189 typedef struct sp_debug_file_s
   190 {
   191 	uint32_t		addr;	/**< Address into code */
   192 	const char *	name;	/**< Name of file */
   193 } sp_debug_file_t;
   194 
   195 /**
   196  * @brief Contains run-time debug line table.
   197  */
   198 typedef struct sp_debug_line_s
   199 {
   200 	uint32_t		addr;	/**< Address into code */
   201 	uint32_t		line;	/**< Line number */
   202 } sp_debug_line_t;
   203 
   204 /**
   205  * @brief These structures are equivalent.
   206  */
   207 typedef sp_fdbg_arraydim_t	sp_debug_arraydim_t;
   208 
   209 /**
   210  * @brief The majority of this struct is already located in the parent 
   211  * block.  Thus, only the relocated portions are required.
   212  */
   213 typedef struct sp_debug_symbol_s
   214 {
   215 	uint32_t		codestart;	/**< Relocated code address */
   216 	uint32_t		codeend;	/**< relocated code end address */
   217 	const char *	name;		/**< Relocated name */
   218 	sp_debug_arraydim_t *dims;	/**< Relocated dimension struct, if any */
   219 	sp_fdbg_symbol_t	*sym;	/**< Pointer to original symbol */
   220 } sp_debug_symbol_t;
   221 
   222 /**
   223  * Breaks into a debugger
   224  * Params:
   225  *  [0] - plugin context
   226  *  [1] - frm
   227  *  [2] - cip
   228  */
   229 typedef int (*SPVM_DEBUGBREAK)(struct sp_context_s *, uint32_t, uint32_t);
   230 
   231 #define SPFLAG_PLUGIN_DEBUG		(1<<0)		/**< plugin is in debug mode */
   232 
   233 /**
   234  * @brief This is the heart of the VM.  It contains all of the runtime 
   235  * information about a plugin context. Note that user[0..3] can be used for any user based pointers.  
   236  * However, vm[0..3] should not be touched, as it is reserved for the VM.
   237  */
   238 typedef struct sp_context_s
   239 {
   240 	void			*codebase;	/**< Base of generated code and memory */
   241 	sp_plugin_t		*plugin;	/**< Pointer back to parent information */
   242 	SourcePawn::IPluginContext *context;	/**< Pointer to IPluginContext */
   243 	SourcePawn::IVirtualMachine *vmbase;	/**< Pointer to IVirtualMachine */
   244 	void			*user[4];	/**< User specific pointers */
   245 	void			*vm[4];		/**< VM specific pointers */
   246 	uint32_t		flags;		/**< Compilation flags */
   247 	SPVM_DEBUGBREAK dbreak;		/**< Debug break function */
   248 	uint8_t			*memory;	/**< Data chunk */
   249 	ucell_t			mem_size;	/**< Total memory size; */
   250 	cell_t			data_size;	/**< Data chunk size, always starts at 0 */
   251 	cell_t			heap_base;	/**< Where the heap starts */
   252 	cell_t			hp;			/**< Heap pointer */
   253 	cell_t			sp;			/**< Stack pointer */
   254 	cell_t			frm;		/**< Frame pointer */
   255 	uint32_t		pushcount;	/**< Push count */
   256 	int32_t			n_err;		/**< Error code set by a native */
   257 	uint32_t		n_idx;		/**< Current native index being executed */
   258 	sp_public_t		*publics;	/**< Public functions table */
   259 	sp_pubvar_t		*pubvars;	/**< Public variables table */
   260 	sp_native_t		*natives;	/**< Natives table */
   261 	sp_debug_file_t	*files;		/**< Files */
   262 	sp_debug_line_t	*lines;		/**< Lines */
   263 	sp_debug_symbol_t *symbols;	/**< Symbols */
   264 } sp_context_t;
   265 
   266 #endif //_INCLUDE_SOURCEPAWN_VM_TYPES_H