amx.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* Pawn Abstract Machine (for the Pawn language)
  2. *
  3. * Copyright (c) ITB CompuPhase, 1997-2005
  4. *
  5. * This software is provided "as-is", without any express or implied warranty.
  6. * In no event will the authors be held liable for any damages arising from
  7. * the use of this software.
  8. *
  9. * Permission is granted to anyone to use this software for any purpose,
  10. * including commercial applications, and to alter it and redistribute it
  11. * freely, subject to the following restrictions:
  12. *
  13. * 1. The origin of this software must not be misrepresented; you must not
  14. * claim that you wrote the original software. If you use this software in
  15. * a product, an acknowledgment in the product documentation would be
  16. * appreciated but is not required.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. *
  21. * Version: $Id: amx.h,v 1.5 2006/03/26 16:56:15 spookie Exp $
  22. */
  23. #if defined FREEBSD && !defined __FreeBSD__
  24. #define __FreeBSD__
  25. #endif
  26. #if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__
  27. #include <sclinux.h>
  28. #endif
  29. #ifndef AMX_H_INCLUDED
  30. #define AMX_H_INCLUDED
  31. #if defined HAVE_STDINT_H
  32. #include <stdint.h>
  33. #else
  34. #if defined __LCC__ || defined __DMC__ || defined LINUX
  35. #if defined HAVE_INTTYPES_H
  36. #include <inttypes.h>
  37. #else
  38. #include <stdint.h>
  39. #endif
  40. #elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
  41. /* The ISO C99 defines the int16_t and int_32t types. If the compiler got
  42. * here, these types are probably undefined.
  43. */
  44. #if defined __MACH__
  45. #include <ppc/types.h>
  46. typedef unsigned short int uint16_t;
  47. typedef unsigned long int uint32_t;
  48. #elif defined __FreeBSD__
  49. #include <inttypes.h>
  50. #else
  51. typedef short int int16_t;
  52. typedef unsigned short int uint16_t;
  53. #if defined SN_TARGET_PS2
  54. typedef int int32_t;
  55. typedef unsigned int uint32_t;
  56. #else
  57. typedef long int int32_t;
  58. typedef unsigned long int uint32_t;
  59. #endif
  60. #if defined __WIN32__ || defined _WIN32 || defined WIN32
  61. typedef __int64 int64_t;
  62. typedef unsigned __int64 uint64_t;
  63. #define HAVE_I64
  64. #elif defined __GNUC__
  65. typedef long long int64_t;
  66. typedef unsigned long long uint64_t;
  67. #define HAVE_I64
  68. #endif
  69. #endif
  70. #endif
  71. #define HAVE_STDINT_H
  72. #endif
  73. #if defined _LP64 || defined WIN64 || defined _WIN64
  74. #if !defined __64BIT__
  75. #define __64BIT__
  76. #endif
  77. #endif
  78. #if HAVE_ALLOCA_H
  79. #include <alloca.h>
  80. #endif
  81. #if defined __WIN32__ || defined _WIN32 || defined WIN32 /* || defined __MSDOS__ */
  82. #if !defined alloca
  83. #define alloca(n) _alloca(n)
  84. #endif
  85. #endif
  86. #if !defined arraysize
  87. #define arraysize(array) (sizeof(array) / sizeof((array)[0]))
  88. #endif
  89. #ifdef __cplusplus
  90. extern "C" {
  91. #endif
  92. #if defined PAWN_DLL
  93. #if !defined AMX_NATIVE_CALL
  94. #define AMX_NATIVE_CALL __stdcall
  95. #endif
  96. #if !defined AMXAPI
  97. #define AMXAPI __stdcall
  98. #endif
  99. #endif
  100. /* calling convention for native functions */
  101. #if !defined AMX_NATIVE_CALL
  102. #define AMX_NATIVE_CALL
  103. #endif
  104. /* calling convention for all interface functions and callback functions */
  105. #if !defined AMXAPI
  106. #if defined STDECL
  107. #define AMXAPI __stdcall
  108. #elif defined CDECL
  109. #define AMXAPI __cdecl
  110. #elif defined GCC_HASCLASSVISIBILITY
  111. #define AMXAPI __attribute__ ((visibility("default")))
  112. #else
  113. #define AMXAPI
  114. #endif
  115. #endif
  116. #if !defined AMXEXPORT
  117. #define AMXEXPORT
  118. #endif
  119. /* File format version Required AMX version
  120. * 0 (original version) 0
  121. * 1 (opcodes JUMP.pri, SWITCH and CASETBL) 1
  122. * 2 (compressed files) 2
  123. * 3 (public variables) 2
  124. * 4 (opcodes SWAP.pri/alt and PUSHADDR) 4
  125. * 5 (tagnames table) 4
  126. * 6 (reformatted header) 6
  127. * 7 (name table, opcodes SYMTAG & SYSREQ.D) 7
  128. * 8 (opcode STMT, renewed debug interface) 8
  129. */
  130. #define CUR_FILE_VERSION 8 /* current file version; also the current AMX version */
  131. #define MIN_FILE_VERSION 6 /* lowest supported file format version for the current AMX version */
  132. #define MIN_AMX_VERSION 8 /* minimum AMX version needed to support the current file format */
  133. #if !defined PAWN_CELL_SIZE
  134. #define PAWN_CELL_SIZE 32 /* by default, use 32-bit cells */
  135. #endif
  136. #if PAWN_CELL_SIZE==16
  137. typedef uint16_t ucell;
  138. typedef int16_t cell;
  139. #elif PAWN_CELL_SIZE==32
  140. typedef uint32_t ucell;
  141. typedef int32_t cell;
  142. #elif PAWN_CELL_SIZE==64
  143. typedef uint64_t ucell;
  144. typedef int64_t cell;
  145. #else
  146. #error Unsupported cell size (PAWN_CELL_SIZE)
  147. #endif
  148. #define UNPACKEDMAX ((1L << (sizeof(cell)-1)*8) - 1)
  149. #define UNLIMITED (~1u >> 1)
  150. struct tagAMX;
  151. typedef cell (AMX_NATIVE_CALL *AMX_NATIVE)(struct tagAMX *amx, cell *params);
  152. typedef int (AMXAPI *AMX_CALLBACK)(struct tagAMX *amx, cell index,
  153. cell *result, cell *params);
  154. typedef int (AMXAPI *AMX_DEBUG)(struct tagAMX *amx);
  155. #if !defined _FAR
  156. #define _FAR
  157. #endif
  158. #if defined _MSC_VER
  159. #pragma warning(disable:4103) /* disable warning message 4103 that complains
  160. * about pragma pack in a header file */
  161. #pragma warning(disable:4100) /* "'%$S' : unreferenced formal parameter" */
  162. #endif
  163. /* Some compilers do not support the #pragma align, which should be fine. Some
  164. * compilers give a warning on unknown #pragmas, which is not so fine...
  165. */
  166. #if (defined SN_TARGET_PS2 || defined __GNUC__) && !defined AMX_NO_ALIGN
  167. #define AMX_NO_ALIGN
  168. #endif
  169. #if defined __GNUC__
  170. #define PACKED __attribute__((packed))
  171. #else
  172. #define PACKED
  173. #endif
  174. #if !defined AMX_NO_ALIGN
  175. #if defined LINUX || defined __FreeBSD__
  176. #pragma pack(1) /* structures must be packed (byte-aligned) */
  177. #elif defined MACOS && defined __MWERKS__
  178. #pragma options align=mac68k
  179. #else
  180. #pragma pack(push)
  181. #pragma pack(1) /* structures must be packed (byte-aligned) */
  182. #if defined __TURBOC__
  183. #pragma option -a- /* "pack" pragma for older Borland compilers */
  184. #endif
  185. #endif
  186. #endif
  187. typedef struct tagAMX_NATIVE_INFO {
  188. const char _FAR *name PACKED;
  189. AMX_NATIVE func PACKED;
  190. } PACKED AMX_NATIVE_INFO;
  191. #define AMX_USERNUM 4
  192. #define sEXPMAX 19 /* maximum name length for file version <= 6 */
  193. #define sNAMEMAX 31 /* maximum name length of symbol name */
  194. typedef struct tagAMX_FUNCSTUB {
  195. ucell address PACKED;
  196. char name[sEXPMAX+1] PACKED;
  197. } PACKED AMX_FUNCSTUB;
  198. typedef struct tagFUNCSTUBNT {
  199. ucell address PACKED;
  200. uint32_t nameofs PACKED;
  201. } PACKED AMX_FUNCSTUBNT;
  202. /* The AMX structure is the internal structure for many functions. Not all
  203. * fields are valid at all times; many fields are cached in local variables.
  204. */
  205. typedef struct tagAMX {
  206. unsigned char _FAR *base PACKED; /* points to the AMX header plus the code, optionally also the data */
  207. unsigned char _FAR *data PACKED; /* points to separate data+stack+heap, may be NULL */
  208. AMX_CALLBACK callback PACKED;
  209. AMX_DEBUG debug PACKED; /* debug callback */
  210. /* for external functions a few registers must be accessible from the outside */
  211. cell cip PACKED; /* instruction pointer: relative to base + amxhdr->cod */
  212. cell frm PACKED; /* stack frame base: relative to base + amxhdr->dat */
  213. cell hea PACKED; /* top of the heap: relative to base + amxhdr->dat */
  214. cell hlw PACKED; /* bottom of the heap: relative to base + amxhdr->dat */
  215. cell stk PACKED; /* stack pointer: relative to base + amxhdr->dat */
  216. cell stp PACKED; /* top of the stack: relative to base + amxhdr->dat */
  217. int flags PACKED; /* current status, see amx_Flags() */
  218. /* user data */
  219. long usertags[AMX_USERNUM] PACKED;
  220. void _FAR *userdata[AMX_USERNUM] PACKED;
  221. /* native functions can raise an error */
  222. int error PACKED;
  223. /* passing parameters requires a "count" field */
  224. int paramcount;
  225. /* the sleep opcode needs to store the full AMX status */
  226. cell pri PACKED;
  227. cell alt PACKED;
  228. cell reset_stk PACKED;
  229. cell reset_hea PACKED;
  230. cell sysreq_d PACKED; /* relocated address/value for the SYSREQ.D opcode */
  231. #if defined JIT
  232. /* support variables for the JIT */
  233. int reloc_size PACKED; /* required temporary buffer for relocations */
  234. long code_size PACKED; /* estimated memory footprint of the native code */
  235. #endif
  236. } PACKED AMX;
  237. /* The AMX_HEADER structure is both the memory format as the file format. The
  238. * structure is used internaly.
  239. */
  240. typedef struct tagAMX_HEADER {
  241. int32_t size PACKED; /* size of the "file" */
  242. uint16_t magic PACKED; /* signature */
  243. char file_version PACKED; /* file format version */
  244. char amx_version PACKED; /* required version of the AMX */
  245. int16_t flags PACKED;
  246. int16_t defsize PACKED; /* size of a definition record */
  247. int32_t cod PACKED; /* initial value of COD - code block */
  248. int32_t dat PACKED; /* initial value of DAT - data block */
  249. int32_t hea PACKED; /* initial value of HEA - start of the heap */
  250. int32_t stp PACKED; /* initial value of STP - stack top */
  251. int32_t cip PACKED; /* initial value of CIP - the instruction pointer */
  252. int32_t publics PACKED; /* offset to the "public functions" table */
  253. int32_t natives PACKED; /* offset to the "native functions" table */
  254. int32_t libraries PACKED; /* offset to the table of libraries */
  255. int32_t pubvars PACKED; /* the "public variables" table */
  256. int32_t tags PACKED; /* the "public tagnames" table */
  257. int32_t nametable PACKED; /* name table */
  258. } PACKED AMX_HEADER;
  259. #if PAWN_CELL_SIZE==16
  260. #define AMX_MAGIC 0xf1e2
  261. #elif PAWN_CELL_SIZE==32
  262. #define AMX_MAGIC 0xf1e0
  263. #elif PAWN_CELL_SIZE==64
  264. #define AMX_MAGIC 0xf1e1
  265. #endif
  266. enum {
  267. AMX_ERR_NONE,
  268. /* reserve the first 15 error codes for exit codes of the abstract machine */
  269. AMX_ERR_EXIT, /* forced exit */
  270. AMX_ERR_ASSERT, /* assertion failed */
  271. AMX_ERR_STACKERR, /* stack/heap collision */
  272. AMX_ERR_BOUNDS, /* index out of bounds */
  273. AMX_ERR_MEMACCESS, /* invalid memory access */
  274. AMX_ERR_INVINSTR, /* invalid instruction */
  275. AMX_ERR_STACKLOW, /* stack underflow */
  276. AMX_ERR_HEAPLOW, /* heap underflow */
  277. AMX_ERR_CALLBACK, /* no callback, or invalid callback */
  278. AMX_ERR_NATIVE, /* native function failed */
  279. AMX_ERR_DIVIDE, /* divide by zero */
  280. AMX_ERR_SLEEP, /* go into sleepmode - code can be restarted */
  281. AMX_ERR_INVSTATE, /* invalid state for this access */
  282. AMX_ERR_MEMORY = 16, /* out of memory */
  283. AMX_ERR_FORMAT, /* invalid file format */
  284. AMX_ERR_VERSION, /* file is for a newer version of the AMX */
  285. AMX_ERR_NOTFOUND, /* function not found */
  286. AMX_ERR_INDEX, /* invalid index parameter (bad entry point) */
  287. AMX_ERR_DEBUG, /* debugger cannot run */
  288. AMX_ERR_INIT, /* AMX not initialized (or doubly initialized) */
  289. AMX_ERR_USERDATA, /* unable to set user data field (table full) */
  290. AMX_ERR_INIT_JIT, /* cannot initialize the JIT */
  291. AMX_ERR_PARAMS, /* parameter error */
  292. AMX_ERR_DOMAIN, /* domain error, expression result does not fit in range */
  293. AMX_ERR_GENERAL, /* general error (unknown or unspecific error) */
  294. };
  295. /* AMX_FLAG_CHAR16 0x01 no longer used */
  296. #define AMX_FLAG_DEBUG 0x02 /* symbolic info. available */
  297. #define AMX_FLAG_COMPACT 0x04 /* compact encoding */
  298. #define AMX_FLAG_BYTEOPC 0x08 /* opcode is a byte (not a cell) */
  299. #define AMX_FLAG_NOCHECKS 0x10 /* no array bounds checking; no STMT opcode */
  300. #define AMX_FLAG_NTVREG 0x1000 /* all native functions are registered */
  301. #define AMX_FLAG_JITC 0x2000 /* abstract machine is JIT compiled */
  302. #define AMX_FLAG_BROWSE 0x4000 /* busy browsing */
  303. #define AMX_FLAG_RELOC 0x8000 /* jump/call addresses relocated */
  304. #define AMX_EXEC_MAIN -1 /* start at program entry point */
  305. #define AMX_EXEC_CONT -2 /* continue from last address */
  306. #define AMX_USERTAG(a,b,c,d) ((a) | ((b)<<8) | ((long)(c)<<16) | ((long)(d)<<24))
  307. #if !defined AMX_COMPACTMARGIN
  308. #define AMX_COMPACTMARGIN 64
  309. #endif
  310. /* for native functions that use floating point parameters, the following
  311. * two macros are convenient for casting a "cell" into a "float" type _without_
  312. * changing the bit pattern
  313. */
  314. #if PAWN_CELL_SIZE==32
  315. #define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */
  316. #define amx_ctof(c) ( * ((float*)&c) ) /* cell to float */
  317. #elif PAWN_CELL_SIZE==64
  318. #define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */
  319. #define amx_ctof(c) ( * ((double*)&c) ) /* cell to float */
  320. #else
  321. #error Unsupported cell size
  322. #endif
  323. #define amx_StrParam(amx,param,result) \
  324. do { \
  325. cell *amx_cstr_; int amx_length_; \
  326. amx_GetAddr((amx), (param), &amx_cstr_); \
  327. amx_StrLen(amx_cstr_, &amx_length_); \
  328. if (amx_length_ > 0 && \
  329. ((result) = (char*)alloca((amx_length_ + 1) * sizeof(*(result)))) != NULL) \
  330. amx_GetString((char*)(result), amx_cstr_, sizeof(*(result))>1, amx_length_ + 1); \
  331. else (result) = NULL; \
  332. } while (0)
  333. uint16_t * AMXAPI amx_Align16(uint16_t *v);
  334. uint32_t * AMXAPI amx_Align32(uint32_t *v);
  335. #if defined _I64_MAX || defined HAVE_I64
  336. uint64_t * AMXAPI amx_Align64(uint64_t *v);
  337. #endif
  338. int AMXAPI amx_Allot(AMX *amx, int cells, cell *amx_addr, cell **phys_addr);
  339. int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, cell *params);
  340. int AMXAPI amx_Cleanup(AMX *amx);
  341. int AMXAPI amx_Clone(AMX *amxClone, AMX *amxSource, void *data);
  342. int AMXAPI amx_Exec(AMX *amx, cell *retval, int index);
  343. int AMXAPI amx_FindNative(AMX *amx, const char *name, int *index);
  344. int AMXAPI amx_FindPublic(AMX *amx, const char *funcname, int *index);
  345. int AMXAPI amx_FindPubVar(AMX *amx, const char *varname, cell *amx_addr);
  346. int AMXAPI amx_FindTagId(AMX *amx, cell tag_id, char *tagname);
  347. int AMXAPI amx_Flags(AMX *amx,uint16_t *flags);
  348. int AMXAPI amx_GetAddr(AMX *amx,cell amx_addr,cell **phys_addr);
  349. int AMXAPI amx_GetNative(AMX *amx, int index, char *funcname);
  350. int AMXAPI amx_GetPublic(AMX *amx, int index, char *funcname);
  351. int AMXAPI amx_GetPubVar(AMX *amx, int index, char *varname, cell *amx_addr);
  352. int AMXAPI amx_GetString(char *dest,const cell *source, int use_wchar, size_t size);
  353. int AMXAPI amx_GetTag(AMX *amx, int index, char *tagname, cell *tag_id);
  354. int AMXAPI amx_GetUserData(AMX *amx, long tag, void **ptr);
  355. int AMXAPI amx_Init(AMX *amx, void *program);
  356. int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code);
  357. int AMXAPI amx_MemInfo(AMX *amx, long *codesize, long *datasize, long *stackheap);
  358. int AMXAPI amx_NameLength(AMX *amx, int *length);
  359. AMX_NATIVE_INFO * AMXAPI amx_NativeInfo(const char *name, AMX_NATIVE func);
  360. int AMXAPI amx_NumNatives(AMX *amx, int *number);
  361. int AMXAPI amx_NumPublics(AMX *amx, int *number);
  362. int AMXAPI amx_NumPubVars(AMX *amx, int *number);
  363. int AMXAPI amx_NumTags(AMX *amx, int *number);
  364. int AMXAPI amx_Push(AMX *amx, cell value);
  365. int AMXAPI amx_PushArray(AMX *amx, cell *amx_addr, cell **phys_addr, const cell array[], int numcells);
  366. int AMXAPI amx_PushString(AMX *amx, cell *amx_addr, cell **phys_addr, const char *string, int pack, int use_wchar);
  367. int AMXAPI amx_RaiseError(AMX *amx, int error);
  368. int AMXAPI amx_Register(AMX *amx, const AMX_NATIVE_INFO *nativelist, int number);
  369. int AMXAPI amx_Release(AMX *amx, cell amx_addr);
  370. int AMXAPI amx_SetCallback(AMX *amx, AMX_CALLBACK callback);
  371. int AMXAPI amx_SetDebugHook(AMX *amx, AMX_DEBUG debug);
  372. int AMXAPI amx_SetString(cell *dest, const char *source, int pack, int use_wchar, size_t size);
  373. int AMXAPI amx_SetUserData(AMX *amx, long tag, void *ptr);
  374. int AMXAPI amx_StrLen(const cell *cstring, int *length);
  375. int AMXAPI amx_UTF8Check(const char *string, int *length);
  376. int AMXAPI amx_UTF8Get(const char *string, const char **endptr, cell *value);
  377. int AMXAPI amx_UTF8Len(const cell *cstr, int *length);
  378. int AMXAPI amx_UTF8Put(char *string, char **endptr, int maxchars, cell value);
  379. #if PAWN_CELL_SIZE==16
  380. #define amx_AlignCell(v) amx_Align16(v)
  381. #elif PAWN_CELL_SIZE==32
  382. #define amx_AlignCell(v) amx_Align32(v)
  383. #elif PAWN_CELL_SIZE==64 && (defined _I64_MAX || defined HAVE_I64)
  384. #define amx_AlignCell(v) amx_Align64(v)
  385. #else
  386. #error Unsupported cell size
  387. #endif
  388. #define amx_RegisterFunc(amx, name, func) \
  389. amx_Register((amx), amx_NativeInfo((name),(func)), 1);
  390. #if !defined AMX_NO_ALIGN
  391. #if defined LINUX || defined __FreeBSD__
  392. #pragma pack() /* reset default packing */
  393. #elif defined MACOS && defined __MWERKS__
  394. #pragma options align=reset
  395. #else
  396. #pragma pack(pop) /* reset previous packing */
  397. #endif
  398. #endif
  399. #ifdef __cplusplus
  400. }
  401. #endif
  402. #endif /* AMX_H_INCLUDED */