sourcepawn/include/sp_vm_types.h
author Borja Ferrer <faluco@alliedmods.net>
Tue Aug 01 10:15:01 2006 +0000 (2006-08-01)
changeset 43 a6fd3d47dd92
parent 40 289b495ab208
child 47 50d5e52ded7e
permissions -rw-r--r--
changed comment
     1 #ifndef _INCLUDE_SOURCEPAWN_VM_TYPES_H
     2 #define _INCLUDE_SOURCEPAWN_VM_TYPES_H
     3 
     4 #include "sp_file_headers.h"
     5 
     6 typedef	uint32_t	ucell_t;
     7 typedef int32_t		cell_t;
     8 
     9 /**
    10  * Error codes
    11  */
    12 #define SP_ERR_NONE					0
    13 #define SP_ERR_FILE_FORMAT			1	/* File format unrecognized */
    14 #define SP_ERR_DECOMPRESSOR			2	/* A decompressor was not found */
    15 #define SP_ERR_HEAPLOW				3	/* Not enough space left on the heap */
    16 #define SP_ERR_PARAM				4	/* Invalid parameter */
    17 #define SP_ERR_INVALID_ADDRESS		5	/* A memory address was not valid */
    18 #define SP_ERR_NOT_FOUND			6	/* The object in question was not found */
    19 
    20 /**********************************************
    21  *** The following structures are reference structures.
    22  *** They are not essential to the API, but are used
    23  ***  to hold the backend database format of the plugin
    24  ***  binary.
    25  **********************************************/
    26 
    27 /**
    28  * Information about the core plugin tables.
    29  * These may or may not be present!
    30  */
    31 typedef struct sp_plugin_infotab_s
    32 {
    33 	const char *stringbase;		/* base of string table */
    34 	uint32_t	publics_num;	/* number of publics */
    35 	sp_file_publics_t *publics;	/* public table */
    36 	uint32_t	natives_num;	/* number of natives */
    37 	sp_file_natives_t *natives; /* native table */
    38 	uint32_t	pubvars_num;	/* number of pubvars */
    39 	sp_file_pubvars_t *pubvars;	/* pubvars table */	
    40 } sp_plugin_infotab_t;
    41 
    42 /**
    43  * Information about the plugin's debug tables.
    44  * These are all present if one is present.
    45  */
    46 typedef struct sp_plugin_debug_s
    47 {
    48 	const char *stringbase;		/* base of string table */
    49 	uint32_t	files_num;		/* number of files */
    50 	sp_fdbg_file_t *files;		/* files table */
    51 	uint32_t	lines_num;		/* number of lines */
    52 	sp_fdbg_line_t *lines;		/* lines table */
    53 	uint32_t	syms_num;		/* number of symbols */
    54 	sp_fdbg_symbol_t *symbols;	/* symbol table */
    55 } sp_plugin_debug_t;
    56 
    57 #define SP_FA_SELF_EXTERNAL		(1<<0)
    58 #define SP_FA_BASE_EXTERNAL		(1<<1)
    59 
    60 /**
    61  * The rebased, in-memory format of a plugin.
    62  * This differs from the on-disk structure to ensure
    63  *  that the format is properly read.
    64  */
    65 typedef struct sp_plugin_s
    66 {
    67 	uint8_t		*base;			/* base of memory */
    68 	uint8_t		*pcode;			/* p-code */
    69 	uint32_t	pcode_size;		/* size of p-code */
    70 	uint8_t		*data;			/* data size */
    71 	uint32_t	data_size;		/* size of data */
    72 	uint32_t	memory;			/* required memory */
    73 	uint16_t	flags;			/* code flags */
    74 	uint32_t	allocflags;		/* allocation flags */
    75 	sp_plugin_infotab_t *info;	/* base info table */
    76 	sp_plugin_debug_t *debug;	/* debug info table */
    77 } sp_plugin_t;
    78 
    79 struct sp_context_s;
    80 typedef int (*SPVM_NATIVE_FUNC)(struct sp_context_s *, cell_t *);
    81 
    82 /**********************************************
    83  *** The following structures are bound to the VM/JIT.
    84  *** Changing them will result in necessary recompilation.
    85  **********************************************/
    86 
    87 /**
    88  * Offsets and names to a public function.
    89  * By default, these point back to the string table 
    90  *  in the sp_plugin_infotab_t structure.
    91  */
    92 typedef struct sp_public_s
    93 {
    94 	uint32_t	offs;		/* code offset */
    95 	const char *name;		/* name */
    96 } sp_public_t;
    97 
    98 /**
    99  * Offsets and names to public variables.
   100  * The offset is relocated and the name by default
   101  *  points back to the sp_plugin_infotab_t structure.
   102  */
   103 typedef struct sp_pubvar_s
   104 {
   105 	cell_t	   *offs;		/* pointer to data */
   106 	const char *name;		/* name */
   107 } sp_pubvar_t;
   108 
   109 #define SP_NATIVE_NONE		(0)		/* Native is not yet found */
   110 #define SP_NATIVE_OKAY		(1)		/* Native has been added */
   111 #define SP_NATIVE_PENDING	(2)		/* Native is marked as usable but replaceable */
   112 
   113 /** 
   114  * Native lookup table, by default names
   115  *  point back to the sp_plugin_infotab_t structure.
   116  * A native is NULL if unit
   117  */
   118 typedef struct sp_native_s
   119 {
   120 	SPVM_NATIVE_FUNC	pfn;	/* function pointer */
   121 	const char *		name;	/* name of function */
   122 	uint32_t			status;	/* status flags */
   123 } sp_native_t;
   124 
   125 /** 
   126  * Debug file table
   127  */
   128 typedef struct sp_debug_file_s
   129 {
   130 	uint32_t		addr;	/* address into code */
   131 	const char *	name;	/* name of file */
   132 } sp_debug_file_t;
   133 
   134 /**
   135  * Note that line is missing.  It is not necessary since
   136  *  this can be retrieved from the base plugin info.
   137  */
   138 typedef struct sp_debug_line_s
   139 {
   140 	uint32_t		addr;	/* address into code */
   141 	uint32_t		line;	/* line no. */
   142 } sp_debug_line_t;
   143 
   144 typedef sp_fdbg_arraydim_t	sp_debug_arraydim_t;
   145 
   146 /**
   147  * The majority of this struct is already located in the parent 
   148  *  block.  Thus, only the relocated portions are required.
   149  */
   150 typedef struct sp_debug_symbol_s
   151 {
   152 	uint32_t		codestart;	/* relocated code address */
   153 	uint32_t		codeend;	/* relocated code end address */
   154 	const char *	name;		/* relocated name */
   155 	sp_debug_arraydim_t *dims;	/* relocated dimension struct, if any */
   156 	sp_fdbg_symbol_t	*sym;	/* pointer to original symbol */
   157 } sp_debug_symbol_t;
   158 
   159 /**
   160  * Executes a Context.
   161  * @sp_context_s	- Execution Context
   162  * @uint32_t		- Offset from code pointer
   163  * @res				- return value of function
   164  * @return			- error code (0=none)
   165  */
   166 typedef int (*SPVM_EXEC)(struct sp_context_s *,
   167 							uint32_t,
   168 							cell_t *res);
   169 
   170 #define SP_CONTEXT_DEBUG			(1<<0)		/* in debug mode */
   171 #define SP_CONTEXT_INHERIT_MEMORY	(1<<1)		/* inherits memory pointers */
   172 #define SP_CONTEXT_INHERIT_CODE		(1<<2)		/* inherits code pointers */
   173 
   174 /**
   175  * This is the heart of the VM.  It contains all of the runtime
   176  *  information about a plugin context.  
   177  * It is split into three sections.
   178  */
   179 typedef struct sp_context_s
   180 {
   181 	/* parent information */
   182 	void			*base;		/* base of generated code and memory */
   183 	sp_plugin_t		*plugin;	/* pointer back to parent information */
   184 	struct sp_context_s *parent;	/* pointer to parent context */
   185 	uint32_t		flags;		/* context flags */
   186 	/* execution specific data */
   187 	SPVM_EXEC		exec;		/* execution base */
   188 	cell_t			pri;		/* PRI register */
   189 	cell_t			alt;		/* ALT register */
   190 	uint8_t			*data;		/* data chunk */
   191 	cell_t			heapbase;	/* heap base */
   192 	cell_t			hp;			/* heap pointer */
   193 	cell_t			sp;			/* stack pointer */
   194 	ucell_t			memory;		/* total memory size; */
   195 	int32_t			err;		/* error code */
   196 	uint32_t		pushcount;	/* push count */
   197 	/* context rebased database */
   198 	sp_public_t		*publics;	/* public functions table */
   199 	sp_pubvar_t		*pubvars;	/* public variables table */
   200 	sp_native_t		*natives;	/* natives table */
   201 	sp_debug_file_t	*files;		/* files */
   202 	sp_debug_line_t	*lines;		/* lines */
   203 	sp_debug_symbol_t *symbols;	/* symbols */
   204 } sp_context_t;
   205 								
   206 #endif //_INCLUDE_SOURCEPAWN_VM_TYPES_H
   207