Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

/home/groups/n/no/noxwizard/cvs/src/amx/amx.h

Go to the documentation of this file.
00001 /*  Abstract Machine for the Small compiler
00002  *
00003  *  Copyright (c) ITB CompuPhase, 1997-2002
00004  *  This file may be freely used. No warranties of any kind.
00005  *
00006  *  Changed by Luxor for FreeBSD Compatibility
00007  */
00008 #ifndef __AMX_H
00009 #define __AMX_H
00010 
00011 //prevent assertion failure in sc2.c with gcc 3.2 and later
00012 #ifndef NDEBUG
00013 #define NDEBUG
00014 #endif
00015 
00016 #if defined __unix__
00017 #define LINUX
00018 #endif
00019 
00020 #if defined __unix__
00021   #include "sclinux.h"
00022 #endif
00023 
00024 #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
00025   /* The ISO C99 defines the int16_t and int_32t types. If the compiler got
00026    * here, these types are probably undefined.
00027    */
00028   #if (defined __LCC__ || defined __GNUC__) && !defined __FreeBSD__
00029     #include <stdint.h>
00030   #else
00031     #if defined __FreeBSD__
00032       #include <inttypes.h>
00033     #else
00034       typedef short int           int16_t;
00035       typedef unsigned short int  uint16_t;
00036       #if defined SN_TARGET_PS2
00037         typedef int               int32_t;
00038         typedef unsigned int      uint32_t;
00039       #else
00040         typedef long int          int32_t;
00041         typedef unsigned long int uint32_t;
00042       #endif
00043     #endif
00044   #endif
00045 #endif
00046 
00047 /* Some compilers do not support the #pragma align, which should be fine. Some
00048  * compilers give a warning on unknown #pragmas, which is not so fine...
00049  */
00050 #if defined SN_TARGET_PS2
00051   #define AMX_NO_ALIGN
00052 #endif
00053 
00054 #ifdef  __cplusplus
00055 extern  "C" {
00056 #endif
00057 
00058 /* calling convention for native functions */
00059 #if !defined AMX_NATIVE_CALL
00060   #define AMX_NATIVE_CALL
00061 #endif
00062 /* calling convention for all interface functions and callback functions */
00063 #if !defined AMXAPI
00064   #define AMXAPI
00065 #endif
00066 
00067 /* File format version                          Required AMX version
00068  *   0 (original version)                       0
00069  *   1 (opcodes JUMP.pri, SWITCH and CASETBL)   1
00070  *   2 (compressed files)                       2
00071  *   3 (public variables)                       2
00072  *   4 (opcodes SWAP.pri/alt and PUSHADDR)      4
00073  *   5 (tagnames table)                         4
00074  *   6 (reformatted header)                     6
00075  */
00076 #define MIN_FILE_VERSION  6     /* lowest file format version */
00077 #define CUR_FILE_VERSION  6     /* current AMX version (parallel with file version) */
00078 
00079 #if !defined CELL_TYPE
00080   #define CELL_TYPE
00081   #if defined(BIT16)
00082     typedef uint16_t  ucell;    /* only for type casting */
00083     typedef int16_t   cell;
00084   #elif defined __alpha__ || defined __x86_64__
00085     typedef uint64_t  ucell;
00086     typedef int64_t   cell;
00087   #else
00088     typedef uint32_t  ucell;
00089     typedef int32_t   cell;
00090   #endif
00091 #endif
00092 
00093 struct __amx;
00094 typedef cell (AMX_NATIVE_CALL *AMX_NATIVE)(struct __amx *amx, cell *params);
00095 typedef int (AMXAPI *AMX_CALLBACK)(struct __amx *amx, cell index,
00096                                    cell *result, cell *params);
00097 typedef int (AMXAPI *AMX_DEBUG)(struct __amx *amx);
00098 #if !defined FAR
00099   #define FAR
00100 #endif
00101 
00102 #if defined _MSC_VER
00103   #pragma warning(disable:4103)  /* disable warning message 4103 that complains
00104                                   * about pragma pack in a header file */
00105 #endif
00106 
00107 #if !defined AMX_NO_ALIGN
00108   #if defined __GNUC__
00109     #pragma pack(1)         /* structures must be packed (byte-aligned) */
00110   #else
00111     #pragma pack(push)
00112     #pragma pack(1)         /* structures must be packed (byte-aligned) */
00113     #if defined __TURBOC__
00114       #pragma option -a-    /* "pack" pragma for older Borland compilers */
00115     #endif
00116   #endif
00117 #endif
00118 
00119 typedef struct {
00120   char FAR *name;
00121   AMX_NATIVE func;
00122 } AMX_NATIVE_INFO;
00123 
00124 #define AMX_USERNUM     4
00125 #define sEXPMAX         30
00126 typedef struct {
00127   cell address;
00128   char name[sEXPMAX+1];
00129 } AMX_FUNCSTUB;
00130 
00131 /* The AMX structure is the internal structure for many functions. Not all
00132  * fields are valid at all times; many fields are cached in local variables.
00133  */
00134 typedef struct __amx {
00135   unsigned char FAR *base;      /* points to the AMX header ("amxhdr") plus the code, optionally also the data */
00136   unsigned char FAR *data;      /* points to separate data+stack+heap, may be NULL */
00137   AMX_CALLBACK callback;
00138   AMX_DEBUG debug;
00139   /* for external functions a few registers must be accessible from the outside */
00140   cell cip;                     /* relative to base + amxhdr->cod */
00141   cell frm;                     /* relative to base + amxhdr->dat */
00142   cell hea, hlw, stk, stp;      /* all four are relative to base + amxhdr->dat */
00143   int flags;                    /* current status, see amx_Flags() */
00144   /* for assertions and debug hook */
00145   cell curline, curfile;
00146   int dbgcode;
00147   cell dbgaddr, dbgparam;
00148   char FAR *dbgname;
00149   /* user data */
00150   long usertags[AMX_USERNUM];
00151   void FAR *userdata[AMX_USERNUM];
00152   /* native functions can raise an error */
00153   int error;
00154   /* the sleep opcode needs to store the full AMX status */
00155   cell pri, alt, reset_stk, reset_hea;
00156   #if defined JIT
00157     /* support variables for the JIT */
00158     int reloc_size;     /* required temporary buffer for relocations */
00159     long code_size;     /* estimated memory footprint of the native code */
00160   #endif
00161 } AMX;
00162 
00163 /* The AMX_HEADER structure is both the memory format as the file format. The
00164  * structure is used internaly.
00165  */
00166 typedef struct __amx_header {
00167   int32_t size;         /* size of the "file" */
00168   uint16_t magic;       /* signature */
00169   char    file_version; /* file format version */
00170   char    amx_version;  /* required version of the AMX */
00171   int16_t flags;
00172   int16_t defsize;
00173   int32_t cod;          /* initial value of COD - code block */
00174   int32_t dat;          /* initial value of DAT - data block */
00175   int32_t hea;          /* initial value of HEA - start of the heap */
00176   int32_t stp;          /* initial value of STP - stack top */
00177   int32_t cip;          /* initial value of CIP - the instruction pointer */
00178   int32_t publics;      /* offset to the "public functions" table */
00179   int32_t natives;      /* offset to the "native functions" table */
00180   int32_t libraries;    /* offset to the table of libraries */
00181   int32_t pubvars;      /* the "public variables" table */
00182   int32_t tags;         /* the "public tagnames" table */
00183 } AMX_HEADER;
00184 #define AMX_MAGIC       0xf1e0
00185 
00186 static const char amx_errMsg [30] [50] = {
00187         "No error", "forced exit", "assertion failed", "stack/heap collision", "index out of bounds", "invalid memory access", "invalid instruction", "stack underflow",
00188         "heap underflow", "no callback, or invalid callback", "native function failed", "divide by zero", "go into sleepmode - code can be restarted", "", ""
00189         , "",""
00190         "out of memory", "invalid file format", "file is for a newer version of the AMX", "function not found", "invalid index parameter (bad entry point)",
00191         "debugger cannot run", "AMX not initialized (or doubly initialized)", "unable to set user data field (table full)", "cannot initialize the JIT", 
00192         "parameter error", "", "", "", };
00193 
00194 enum {
00195   AMX_ERR_NONE,
00196   /* reserve the first 15 error codes for exit codes of the abstract machine */
00197   AMX_ERR_EXIT,         /* forced exit */
00198   AMX_ERR_ASSERT,       /* assertion failed */
00199   AMX_ERR_STACKERR,     /* stack/heap collision */
00200   AMX_ERR_BOUNDS,       /* index out of bounds */
00201   AMX_ERR_MEMACCESS,    /* invalid memory access */
00202   AMX_ERR_INVINSTR,     /* invalid instruction */
00203   AMX_ERR_STACKLOW,     /* stack underflow */
00204   AMX_ERR_HEAPLOW,      /* heap underflow */
00205   AMX_ERR_CALLBACK,     /* no callback, or invalid callback */
00206   AMX_ERR_NATIVE,       /* native function failed */
00207   AMX_ERR_DIVIDE,       /* divide by zero */
00208   AMX_ERR_SLEEP,        /* go into sleepmode - code can be restarted */
00209 
00210   AMX_ERR_MEMORY = 16,  /* out of memory */
00211   AMX_ERR_FORMAT,       /* invalid file format */
00212   AMX_ERR_VERSION,      /* file is for a newer version of the AMX */
00213   AMX_ERR_NOTFOUND,     /* function not found */
00214   AMX_ERR_INDEX,        /* invalid index parameter (bad entry point) */
00215   AMX_ERR_DEBUG,        /* debugger cannot run */
00216   AMX_ERR_INIT,         /* AMX not initialized (or doubly initialized) */
00217   AMX_ERR_USERDATA,     /* unable to set user data field (table full) */
00218   AMX_ERR_INIT_JIT,     /* cannot initialize the JIT */
00219   AMX_ERR_PARAMS,       /* parameter error */
00220 };
00221 
00222 enum {
00223   DBG_INIT,             /* query/initialize */
00224   DBG_FILE,             /* file number in curfile, filename in name */
00225   DBG_LINE,             /* line number in curline, file number in curfile */
00226   DBG_SYMBOL,           /* address in dbgaddr, class/type in dbgparam */
00227   DBG_CLRSYM,           /* stack address below which locals should be removed. stack address in stk */
00228   DBG_CALL,             /* function call, address jumped to in dbgaddr */
00229   DBG_RETURN,           /* function returns */
00230   DBG_TERMINATE,        /* program ends, code address in dbgaddr, reason in dbgparam */
00231   DBG_SRANGE,           /* symbol size and dimensions (arrays); level in dbgaddr (!); length in dbgparam */
00232 };
00233 
00234 #define AMX_FLAG_CHAR16   0x01  /* characters are 16-bit */
00235 #define AMX_FLAG_DEBUG    0x02  /* symbolic info. available */
00236 #define AMX_FLAG_COMPACT  0x04  /* compact encoding */
00237 #define AMX_FLAG_BIGENDIAN 0x08 /* big endian encoding */
00238 #define AMX_FLAG_BROWSE 0x4000
00239 #define AMX_FLAG_RELOC  0x8000  /* jump/call addresses relocated */
00240 
00241 #define AMX_EXEC_MAIN   -1      /* start at program entry point */
00242 #define AMX_EXEC_CONT   -2      /* continue from last address */
00243 
00244 #define AMX_USERTAG(a,b,c,d)    ((a) | ((b)<<8) | ((long)(c)<<16) | ((long)(d)<<24))
00245 
00246 uint16_t * AMXAPI amx_Align16(uint16_t *v);
00247 uint32_t * AMXAPI amx_Align32(uint32_t *v);
00248 int AMXAPI amx_Allot(AMX *amx, int cells, cell *amx_addr, cell **phys_addr);
00249 int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, cell *params);
00250 int AMXAPI amx_Clone(AMX *amxClone, AMX *amxSource, void *data);
00251 int AMXAPI amx_Debug(AMX *amx); /* default debug procedure, does nothing */
00252 int AMXAPI amx_Exec(AMX *amx, cell *retval, int index, int numparams, ...);
00253 int AMXAPI amx_Execv(AMX *amx, cell *retval, int index, int numparams, cell params[]);
00254 int AMXAPI amx_FindPublic(AMX *amx, char *funcname, int *index);
00255 int AMXAPI amx_FindPubVar(AMX *amx, char *varname, cell *amx_addr);
00256 int AMXAPI amx_FindTagId(AMX *amx, cell tag_id, char *tagname);
00257 int AMXAPI amx_Flags(AMX *amx,uint16_t *flags);
00258 int AMXAPI amx_GetAddr(AMX *amx,cell amx_addr,cell **phys_addr);
00259 int AMXAPI amx_GetPublic(AMX *amx, int index, char *funcname);
00260 int AMXAPI amx_GetPubVar(AMX *amx, int index, char *varname, cell *amx_addr);
00261 int AMXAPI amx_GetString(char *dest,cell *source);
00262 int AMXAPI amx_GetTag(AMX *amx, int index, char *tagname, cell *tag_id);
00263 int AMXAPI amx_GetUserData(AMX *amx, long tag, void **ptr);
00264 int AMXAPI amx_Init(AMX *amx, void *program);
00265 int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code);
00266 int AMXAPI amx_MemInfo(AMX *amx, long *codesize, long *datasize, long *stackheap);
00267 int AMXAPI amx_NameLength(AMX *amx, int *length);
00268 AMX_NATIVE_INFO * AMXAPI amx_NativeInfo(char *name,AMX_NATIVE func);
00269 int AMXAPI amx_NumPublics(AMX *amx, int *number);
00270 int AMXAPI amx_NumPubVars(AMX *amx, int *number);
00271 int AMXAPI amx_NumTags(AMX *amx, int *number);
00272 int AMXAPI amx_RaiseError(AMX *amx, int error);
00273 int AMXAPI amx_Register(AMX *amx, AMX_NATIVE_INFO *nativelist, int number);
00274 int AMXAPI amx_Release(AMX *amx, cell amx_addr);
00275 int AMXAPI amx_SetCallback(AMX *amx, AMX_CALLBACK callback);
00276 int AMXAPI amx_SetDebugHook(AMX *amx, AMX_DEBUG debug);
00277 int AMXAPI amx_SetString(cell *dest, char *source, int pack);
00278 int AMXAPI amx_SetUserData(AMX *amx, long tag, void *ptr);
00279 int AMXAPI amx_StrLen(cell *cstring, int *length);
00280 char * amx_FindFunctionbyIndex(AMX *amx, int index);
00281 
00282 #if !defined AMX_NO_ALIGN
00283   #if defined __GNUC__
00284     #pragma pack()    /* reset default packing */
00285   #else
00286     #pragma pack(pop) /* reset previous packing */
00287   #endif
00288 #endif
00289 
00290 #ifdef  __cplusplus
00291 }
00292 #endif
00293 
00294 #endif /* __AMX_H */
SourceForge.net Logo