new abstract machine; adapted a bit

This commit is contained in:
Pavol Marko 2004-03-27 16:58:13 +00:00
parent 05cf53e1b9
commit 5c1287bbfb
3 changed files with 818 additions and 183 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
/* Abstract Machine for the Small compiler /* Abstract Machine for the Small compiler
* *
* Copyright (c) ITB CompuPhase, 1997-2003 * Copyright (c) ITB CompuPhase, 1997-2004
* *
* This software is provided "as-is", without any express or implied warranty. * This software is provided "as-is", without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from * In no event will the authors be held liable for any damages arising from
@ -10,18 +10,32 @@
* including commercial applications, and to alter it and redistribute it * including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions: * freely, subject to the following restrictions:
* *
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in
* a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* Version: $Id$
*/ */
#if defined __linux__ #if defined __linux__
#include <sclinux.h> #include <sclinux.h>
#endif #endif
#ifndef __AMX_H
#define __AMX_H #ifndef AMX_H_INCLUDED
#if !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L #define AMX_H_INCLUDED
#if defined __LCC__ || defined __DMC__ || defined __linux__
#include <stdint.h>
#elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
/* The ISO C99 defines the int16_t and int_32t types. If the compiler got /* The ISO C99 defines the int16_t and int_32t types. If the compiler got
* here, these types are probably undefined. * here, these types are probably undefined.
*/ */
#if defined __LCC__ || defined __linux__ #if defined __FreeBSD__
#include <stdint.h> #include <inttypes.h>
#else #else
typedef short int int16_t; typedef short int int16_t;
typedef unsigned short int uint16_t; typedef unsigned short int uint16_t;
@ -32,16 +46,28 @@
typedef long int int32_t; typedef long int int32_t;
typedef unsigned long int uint32_t; typedef unsigned long int uint32_t;
#endif #endif
#if defined __WIN32__ || defined _WIN32 || defined WIN32
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#define HAVE_I64
#elif defined __GNUC__
typedef long long int64_t;
typedef unsigned long long uint64_t;
#define HAVE_I64
#endif
#endif #endif
#endif #endif
#if defined __WIN32__ || defined _WIN32 || defined WIN32 || defined __MSDOS__
#if defined __WIN32__ || defined _WIN32 || defined WIN32 /* || defined __MSDOS__ */
#if !defined alloca #if !defined alloca
#define alloca(n) _alloca(n) #define alloca(n) _alloca(n)
#endif #endif
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* calling convention for native functions */ /* calling convention for native functions */
#if !defined AMX_NATIVE_CALL #if !defined AMX_NATIVE_CALL
#define AMX_NATIVE_CALL #define AMX_NATIVE_CALL
@ -59,6 +85,7 @@ extern "C" {
#if !defined AMXEXPORT #if !defined AMXEXPORT
#define AMXEXPORT #define AMXEXPORT
#endif #endif
/* File format version Required AMX version /* File format version Required AMX version
* 0 (original version) 0 * 0 (original version) 0
* 1 (opcodes JUMP.pri, SWITCH and CASETBL) 1 * 1 (opcodes JUMP.pri, SWITCH and CASETBL) 1
@ -72,16 +99,28 @@ extern "C" {
#define CUR_FILE_VERSION 7 /* current file version; also the current AMX version */ #define CUR_FILE_VERSION 7 /* current file version; also the current AMX version */
#define MIN_FILE_VERSION 6 /* lowest supported file format version for the current AMX version */ #define MIN_FILE_VERSION 6 /* lowest supported file format version for the current AMX version */
#define MIN_AMX_VERSION 7 /* minimum AMX version needed to support the current file format */ #define MIN_AMX_VERSION 7 /* minimum AMX version needed to support the current file format */
#if !defined CELL_TYPE
#define CELL_TYPE #if defined BIT16
#if defined(BIT16) #define SMALL_CELL_SIZE 16 /* for backward compatibility */
typedef uint16_t ucell; /* only for type casting */ #endif
#if !defined SMALL_CELL_SIZE
#define SMALL_CELL_SIZE 32 /* by default, use 32-bit cells */
#endif
#if SMALL_CELL_SIZE==16
typedef uint16_t ucell;
typedef int16_t cell; typedef int16_t cell;
#else #elif SMALL_CELL_SIZE==32
typedef uint32_t ucell; typedef uint32_t ucell;
typedef int32_t cell; typedef int32_t cell;
#endif #elif SMALL_CELL_SIZE==64
typedef uint64_t ucell;
typedef int64_t cell;
#else
#error Unsupported cell size (SMALL_CELL_SIZE)
#endif #endif
#define UNPACKEDMAX ((1 << (sizeof(cell)-1)*8) - 1)
struct tagAMX; struct tagAMX;
typedef cell (AMX_NATIVE_CALL *AMX_NATIVE)(struct tagAMX *amx, cell *params); typedef cell (AMX_NATIVE_CALL *AMX_NATIVE)(struct tagAMX *amx, cell *params);
typedef int (AMXAPI *AMX_CALLBACK)(struct tagAMX *amx, cell index, typedef int (AMXAPI *AMX_CALLBACK)(struct tagAMX *amx, cell index,
@ -90,22 +129,26 @@ typedef int (AMXAPI *AMX_DEBUG)(struct tagAMX *amx);
#if !defined _FAR #if !defined _FAR
#define _FAR #define _FAR
#endif #endif
#if defined _MSC_VER #if defined _MSC_VER
#pragma warning(disable:4103) /* disable warning message 4103 that complains #pragma warning(disable:4103) /* disable warning message 4103 that complains
* about pragma pack in a header file */ * about pragma pack in a header file */
#pragma warning(disable:4100) /* "'%$S' : unreferenced formal parameter" */ #pragma warning(disable:4100) /* "'%$S' : unreferenced formal parameter" */
#endif #endif
/* Some compilers do not support the #pragma align, which should be fine. Some /* Some compilers do not support the #pragma align, which should be fine. Some
* compilers give a warning on unknown #pragmas, which is not so fine... * compilers give a warning on unknown #pragmas, which is not so fine...
*/ */
#if defined SN_TARGET_PS2 || defined __GNUC__ #if defined SN_TARGET_PS2 || defined __GNUC__
#define AMX_NO_ALIGN #define AMX_NO_ALIGN
#endif #endif
#if defined __GNUC__ #if defined __GNUC__
#define PACKED __attribute__((packed)) #define PACKED __attribute__((packed))
#else #else
#define PACKED #define PACKED
#endif #endif
#if !defined AMX_NO_ALIGN #if !defined AMX_NO_ALIGN
#if defined __linux__ #if defined __linux__
#pragma pack(1) /* structures must be packed (byte-aligned) */ #pragma pack(1) /* structures must be packed (byte-aligned) */
@ -117,17 +160,21 @@ typedef int (AMXAPI *AMX_DEBUG)(struct tagAMX *amx);
#endif #endif
#endif #endif
#endif #endif
typedef struct { typedef struct {
char _FAR *name PACKED; const char _FAR *name PACKED;
AMX_NATIVE func PACKED; AMX_NATIVE func PACKED;
} AMX_NATIVE_INFO PACKED; } AMX_NATIVE_INFO PACKED;
#define AMX_USERNUM 4 #define AMX_USERNUM 4
#define sEXPMAX 19 /* maximum name length for file version <= 6 */ #define sEXPMAX 19 /* maximum name length for file version <= 6 */
#define sNAMEMAX 31 /* maximum name length of symbol name */ #define sNAMEMAX 31 /* maximum name length of symbol name */
typedef struct tagAMX_FUNCSTUB { typedef struct tagAMX_FUNCSTUB {
uint32_t address PACKED; uint32_t address PACKED;
char name[sEXPMAX+1] PACKED; const char name[sEXPMAX+1] PACKED;
} AMX_FUNCSTUB PACKED; } AMX_FUNCSTUB PACKED;
/* The AMX structure is the internal structure for many functions. Not all /* The AMX structure is the internal structure for many functions. Not all
* fields are valid at all times; many fields are cached in local variables. * fields are valid at all times; many fields are cached in local variables.
*/ */
@ -161,13 +208,14 @@ typedef struct tagAMX {
cell alt PACKED; cell alt PACKED;
cell reset_stk PACKED; cell reset_stk PACKED;
cell reset_hea PACKED; cell reset_hea PACKED;
cell _FAR *syscall_d PACKED; /* relocated value/address for the SYSCALL.D opcode */ cell sysreq_d PACKED; /* relocated address/value for the SYSREQ.D opcode */
#if defined JIT #if defined JIT
/* support variables for the JIT */ /* support variables for the JIT */
int reloc_size PACKED; /* required temporary buffer for relocations */ int reloc_size PACKED; /* required temporary buffer for relocations */
long code_size PACKED; /* estimated memory footprint of the native code */ long code_size PACKED; /* estimated memory footprint of the native code */
#endif #endif
} AMX PACKED; } AMX PACKED;
/* The AMX_HEADER structure is both the memory format as the file format. The /* The AMX_HEADER structure is both the memory format as the file format. The
* structure is used internaly. * structure is used internaly.
*/ */
@ -191,6 +239,7 @@ typedef struct tagAMX_HEADER {
int32_t nametable PACKED; /* name table, file version 7 only */ int32_t nametable PACKED; /* name table, file version 7 only */
} AMX_HEADER PACKED; } AMX_HEADER PACKED;
#define AMX_MAGIC 0xf1e0 #define AMX_MAGIC 0xf1e0
enum { enum {
AMX_ERR_NONE, AMX_ERR_NONE,
/* reserve the first 15 error codes for exit codes of the abstract machine */ /* reserve the first 15 error codes for exit codes of the abstract machine */
@ -206,6 +255,7 @@ enum {
AMX_ERR_NATIVE, /* native function failed */ AMX_ERR_NATIVE, /* native function failed */
AMX_ERR_DIVIDE, /* divide by zero */ AMX_ERR_DIVIDE, /* divide by zero */
AMX_ERR_SLEEP, /* go into sleepmode - code can be restarted */ AMX_ERR_SLEEP, /* go into sleepmode - code can be restarted */
AMX_ERR_MEMORY = 16, /* out of memory */ AMX_ERR_MEMORY = 16, /* out of memory */
AMX_ERR_FORMAT, /* invalid file format */ AMX_ERR_FORMAT, /* invalid file format */
AMX_ERR_VERSION, /* file is for a newer version of the AMX */ AMX_ERR_VERSION, /* file is for a newer version of the AMX */
@ -218,6 +268,7 @@ enum {
AMX_ERR_PARAMS, /* parameter error */ AMX_ERR_PARAMS, /* parameter error */
AMX_ERR_DOMAIN, /* domain error, expression result does not fit in range */ AMX_ERR_DOMAIN, /* domain error, expression result does not fit in range */
}; };
enum { enum {
DBG_INIT, /* query/initialize */ DBG_INIT, /* query/initialize */
DBG_FILE, /* file number in curfile, filename in name */ DBG_FILE, /* file number in curfile, filename in name */
@ -230,34 +281,52 @@ enum {
DBG_SRANGE, /* symbol size and dimensions (arrays); level in dbgaddr (!); length in dbgparam */ DBG_SRANGE, /* symbol size and dimensions (arrays); level in dbgaddr (!); length in dbgparam */
DBG_SYMTAG, /* tag of the most recent symbol (if non-zero), tag in dbgparam */ DBG_SYMTAG, /* tag of the most recent symbol (if non-zero), tag in dbgparam */
}; };
#define AMX_FLAG_CHAR16 0x01 /* characters are 16-bit */
/* AMX_FLAG_CHAR16 0x01 no longer used */
#define AMX_FLAG_DEBUG 0x02 /* symbolic info. available */ #define AMX_FLAG_DEBUG 0x02 /* symbolic info. available */
#define AMX_FLAG_COMPACT 0x04 /* compact encoding */ #define AMX_FLAG_COMPACT 0x04 /* compact encoding */
#define AMX_FLAG_BIGENDIAN 0x08 /* big endian encoding */ #define AMX_FLAG_BIGENDIAN 0x08 /* big endian encoding */
#define AMX_FLAG_NOCHECKS 0x10 /* no array bounds checking */ #define AMX_FLAG_NOCHECKS 0x10 /* no array bounds checking */
#define AMX_FLAG_BROWSE 0x4000 /* browsing/relocating or executing */ #define AMX_FLAG_BROWSE 0x4000 /* browsing/relocating or executing */
#define AMX_FLAG_RELOC 0x8000 /* jump/call addresses relocated */ #define AMX_FLAG_RELOC 0x8000 /* jump/call addresses relocated */
#define AMX_EXEC_MAIN -1 /* start at program entry point */ #define AMX_EXEC_MAIN -1 /* start at program entry point */
#define AMX_EXEC_CONT -2 /* continue from last address */ #define AMX_EXEC_CONT -2 /* continue from last address */
#define AMX_USERTAG(a,b,c,d) ((a) | ((b)<<8) | ((long)(c)<<16) | ((long)(d)<<24)) #define AMX_USERTAG(a,b,c,d) ((a) | ((b)<<8) | ((long)(c)<<16) | ((long)(d)<<24))
#define AMX_EXPANDMARGIN 64 #define AMX_EXPANDMARGIN 64
/* for native functions that use floating point parameters, the following /* for native functions that use floating point parameters, the following
* two macros are convenient for casting a "cell" into a "float" type _without_ * two macros are convenient for casting a "cell" into a "float" type _without_
* changing the bit pattern * changing the bit pattern
*/ */
#define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */ #if SMALL_CELL_SIZE==32
#define amx_ctof(c) ( * ((float*)&c) ) /* cell to float */ #define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */
#define amx_StrParam(amx,param,result) { \ #define amx_ctof(c) ( * ((float*)&c) ) /* cell to float */
#elif SMALL_CELL_SIZE==64
#define amx_ftoc(f) ( * ((cell*)&f) ) /* float to cell */
#define amx_ctof(c) ( * ((double*)&c) ) /* cell to float */
#else
#error Unsupported cell size
#endif
#define amx_StrParam(amx,param,result) \
do { \
cell *amx_cstr_; int amx_length_; \ cell *amx_cstr_; int amx_length_; \
amx_GetAddr((amx), (param), &amx_cstr_); \ amx_GetAddr((amx), (param), &amx_cstr_); \
amx_StrLen(amx_cstr_, &amx_length_); \ amx_StrLen(amx_cstr_, &amx_length_); \
if (amx_length_ > 0 && \ if (amx_length_ > 0 && \
((result) = (char*)alloca(amx_length_ + 1)) != NULL) \ ((result) = (void*)alloca((amx_length_ + 1) * sizeof(*(result)))) != NULL) \
amx_GetString((result), amx_cstr_); \ amx_GetString((char*)(result), amx_cstr_, sizeof(*(result))>1); \
else (result) = NULL; \ else (result) = NULL; \
} } while (0)
uint16_t * AMXAPI amx_Align16(uint16_t *v); uint16_t * AMXAPI amx_Align16(uint16_t *v);
uint32_t * AMXAPI amx_Align32(uint32_t *v); uint32_t * AMXAPI amx_Align32(uint32_t *v);
#if defined _I64_MAX || defined HAVE_I64
uint64_t * AMXAPI amx_Align64(uint64_t *v);
#endif
int AMXAPI amx_Allot(AMX *amx, int cells, cell *amx_addr, cell **phys_addr); int AMXAPI amx_Allot(AMX *amx, int cells, cell *amx_addr, cell **phys_addr);
int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, cell *params); int AMXAPI amx_Callback(AMX *amx, cell index, cell *result, cell *params);
int AMXAPI amx_Cleanup(AMX *amx); int AMXAPI amx_Cleanup(AMX *amx);
@ -265,23 +334,23 @@ int AMXAPI amx_Clone(AMX *amxClone, AMX *amxSource, void *data);
int AMXAPI amx_Debug(AMX *amx); /* default debug procedure, does nothing */ int AMXAPI amx_Debug(AMX *amx); /* default debug procedure, does nothing */
int AMXAPI amx_Exec(AMX *amx, cell *retval, int index, int numparams, ...); int AMXAPI amx_Exec(AMX *amx, cell *retval, int index, int numparams, ...);
int AMXAPI amx_Execv(AMX *amx, cell *retval, int index, int numparams, cell params[]); int AMXAPI amx_Execv(AMX *amx, cell *retval, int index, int numparams, cell params[]);
int AMXAPI amx_FindNative(AMX *amx, char *name, int *index); int AMXAPI amx_FindNative(AMX *amx, const char *name, int *index);
int AMXAPI amx_FindPublic(AMX *amx, char *funcname, int *index); int AMXAPI amx_FindPublic(AMX *amx, const char *funcname, int *index);
int AMXAPI amx_FindPubVar(AMX *amx, char *varname, cell *amx_addr); int AMXAPI amx_FindPubVar(AMX *amx, const char *varname, cell *amx_addr);
int AMXAPI amx_FindTagId(AMX *amx, cell tag_id, char *tagname); int AMXAPI amx_FindTagId(AMX *amx, cell tag_id, char *tagname);
int AMXAPI amx_Flags(AMX *amx,uint16_t *flags); int AMXAPI amx_Flags(AMX *amx,uint16_t *flags);
int AMXAPI amx_GetAddr(AMX *amx,cell amx_addr,cell **phys_addr); int AMXAPI amx_GetAddr(AMX *amx,cell amx_addr,cell **phys_addr);
int AMXAPI amx_GetNative(AMX *amx, int index, char *funcname); int AMXAPI amx_GetNative(AMX *amx, int index, char *funcname);
int AMXAPI amx_GetPublic(AMX *amx, int index, char *funcname); int AMXAPI amx_GetPublic(AMX *amx, int index, char *funcname);
int AMXAPI amx_GetPubVar(AMX *amx, int index, char *varname, cell *amx_addr); int AMXAPI amx_GetPubVar(AMX *amx, int index, char *varname, cell *amx_addr);
int AMXAPI amx_GetString(char *dest,cell *source); int AMXAPI amx_GetString(char *dest,const cell *source, int use_wchar);
int AMXAPI amx_GetTag(AMX *amx, int index, char *tagname, cell *tag_id); int AMXAPI amx_GetTag(AMX *amx, int index, char *tagname, cell *tag_id);
int AMXAPI amx_GetUserData(AMX *amx, long tag, void **ptr); int AMXAPI amx_GetUserData(AMX *amx, long tag, void **ptr);
int AMXAPI amx_Init(AMX *amx, void *program); int AMXAPI amx_Init(AMX *amx, void *program);
int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code); int AMXAPI amx_InitJIT(AMX *amx, void *reloc_table, void *native_code);
int AMXAPI amx_MemInfo(AMX *amx, long *codesize, long *datasize, long *stackheap); int AMXAPI amx_MemInfo(AMX *amx, long *codesize, long *datasize, long *stackheap);
int AMXAPI amx_NameLength(AMX *amx, int *length); int AMXAPI amx_NameLength(AMX *amx, int *length);
AMX_NATIVE_INFO * AMXAPI amx_NativeInfo(char *name,AMX_NATIVE func); AMX_NATIVE_INFO * AMXAPI amx_NativeInfo(const char *name,AMX_NATIVE func);
int AMXAPI amx_NumNatives(AMX *amx, int *number); int AMXAPI amx_NumNatives(AMX *amx, int *number);
int AMXAPI amx_NumPublics(AMX *amx, int *number); int AMXAPI amx_NumPublics(AMX *amx, int *number);
int AMXAPI amx_NumPubVars(AMX *amx, int *number); int AMXAPI amx_NumPubVars(AMX *amx, int *number);
@ -291,10 +360,14 @@ int AMXAPI amx_Register(AMX *amx, AMX_NATIVE_INFO *nativelist, int number);
int AMXAPI amx_Release(AMX *amx, cell amx_addr); int AMXAPI amx_Release(AMX *amx, cell amx_addr);
int AMXAPI amx_SetCallback(AMX *amx, AMX_CALLBACK callback); int AMXAPI amx_SetCallback(AMX *amx, AMX_CALLBACK callback);
int AMXAPI amx_SetDebugHook(AMX *amx, AMX_DEBUG debug); int AMXAPI amx_SetDebugHook(AMX *amx, AMX_DEBUG debug);
int AMXAPI amx_SetString(cell *dest, char *source, int pack); int AMXAPI amx_SetString(cell *dest, const char *source, int pack, int use_wchar);
int AMXAPI amx_SetUserData(AMX *amx, long tag, void *ptr); int AMXAPI amx_SetUserData(AMX *amx, long tag, void *ptr);
char * AMXAPI amx_StrError(int errnum);
int AMXAPI amx_StrLen(cell *cstring, int *length); int AMXAPI amx_StrLen(cell *cstring, int *length);
int AMXAPI amx_UTF8Get(const char *string, const char **endptr, cell *value);
int AMXAPI amx_UTF8Put(char *string, char **endptr, int maxchars, cell value);
int AMXAPI amx_UTF8Check(const char *string);
#if !defined AMX_NO_ALIGN #if !defined AMX_NO_ALIGN
#if defined __linux__ #if defined __linux__
#pragma pack() /* reset default packing */ #pragma pack() /* reset default packing */
@ -302,7 +375,9 @@ int AMXAPI amx_StrLen(cell *cstring, int *length);
#pragma pack(pop) /* reset previous packing */ #pragma pack(pop) /* reset previous packing */
#endif #endif
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* __AMX_H */
#endif /* AMX_H_INCLUDED */

View File

@ -1,24 +1,62 @@
/* Core module for the Small AMX /* Core module for the Small AMX
* *
* Copyright (c) ITB CompuPhase, 1997-2002 * Copyright (c) ITB CompuPhase, 1997-2004
* This file may be freely used. No warranties of any kind. *
* This software is provided "as-is", without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in
* a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* *
* Version: $Id$ * Version: $Id$
*/ */
#if defined _UNICODE || defined __UNICODE__ || defined UNICODE
# if !defined UNICODE /* for Windows */
# define UNICODE
# endif
# if !defined _UNICODE /* for C library */
# define _UNICODE
# endif
#endif
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include <assert.h> #include <assert.h>
#include "amx.h" #include "amx.h"
#if defined __WIN32__ || defined _WIN32 || defined WIN32 || defined _Windows
#define NOPROPLIST #include <windows.h>
#endif
/* A few compilers do not provide the ANSI C standard "time" functions */ /* A few compilers do not provide the ANSI C standard "time" functions */
#if !defined SN_TARGET_PS2 && !defined _WIN32_WCE #if !defined SN_TARGET_PS2 && !defined _WIN32_WCE
#include <time.h> #include <time.h>
#endif #endif
#if defined _UNICODE
# include <tchar.h>
#elif !defined __T
typedef char TCHAR;
# define __T(string) string
# define _tcschr strchr
# define _tcscpy strcpy
# define _tcsdup strdup
# define _tcslen strlen
# define _stprintf sprintf
#endif
#define CHARBITS (8*sizeof(char)) #define CHARBITS (8*sizeof(char))
typedef unsigned char uchar; typedef unsigned char uchar;
@ -28,6 +66,7 @@ typedef struct _property_list {
cell id; cell id;
char *name; char *name;
cell value; cell value;
//??? safe AMX (owner of the property)
} proplist; } proplist;
static proplist proproot = { NULL }; static proplist proproot = { NULL };
@ -106,7 +145,7 @@ static cell AMX_NATIVE_CALL numargs(AMX *amx, cell *params)
cell bytes; cell bytes;
hdr=(AMX_HEADER *)amx->base; hdr=(AMX_HEADER *)amx->base;
data=amx->base+(int)hdr->dat; data=amx->data ? amx->data : amx->base+(int)hdr->dat;
/* the number of bytes is on the stack, at "frm + 2*cell" */ /* the number of bytes is on the stack, at "frm + 2*cell" */
bytes= * (cell *)(data+(int)amx->frm+2*sizeof(cell)); bytes= * (cell *)(data+(int)amx->frm+2*sizeof(cell));
/* the number of arguments is the number of bytes divided /* the number of arguments is the number of bytes divided
@ -121,7 +160,7 @@ static cell AMX_NATIVE_CALL getarg(AMX *amx, cell *params)
cell value; cell value;
hdr=(AMX_HEADER *)amx->base; hdr=(AMX_HEADER *)amx->base;
data=amx->base+(int)hdr->dat; data=amx->data ? amx->data : amx->base+(int)hdr->dat;
/* get the base value */ /* get the base value */
value= * (cell *)(data+(int)amx->frm+((int)params[1]+3)*sizeof(cell)); value= * (cell *)(data+(int)amx->frm+((int)params[1]+3)*sizeof(cell));
/* adjust the address in "value" in case of an array access */ /* adjust the address in "value" in case of an array access */
@ -138,7 +177,7 @@ static cell AMX_NATIVE_CALL setarg(AMX *amx, cell *params)
cell value; cell value;
hdr=(AMX_HEADER *)amx->base; hdr=(AMX_HEADER *)amx->base;
data=amx->base+(int)hdr->dat; data=amx->data ? amx->data : amx->base+(int)hdr->dat;
/* get the base value */ /* get the base value */
value= * (cell *)(data+(int)amx->frm+((int)params[1]+3)*sizeof(cell)); value= * (cell *)(data+(int)amx->frm+((int)params[1]+3)*sizeof(cell));
/* adjust the address in "value" in case of an array access */ /* adjust the address in "value" in case of an array access */
@ -174,7 +213,7 @@ static cell AMX_NATIVE_CALL funcidx(AMX *amx,cell *params)
return 0; return 0;
} /* if */ } /* if */
amx_GetString(name,cstr); amx_GetString(name,cstr,0);
err=amx_FindPublic(amx,name,&index); err=amx_FindPublic(amx,name,&index);
if (err!=AMX_ERR_NONE) if (err!=AMX_ERR_NONE)
index=-1; /* this is not considered a fatal error */ index=-1; /* this is not considered a fatal error */
@ -186,7 +225,7 @@ int amx_StrPack(cell *dest,cell *source)
int len; int len;
amx_StrLen(source,&len); amx_StrLen(source,&len);
if ((ucell)*source>UCHAR_MAX) { if ((ucell)*source>UNPACKEDMAX) {
/* source string is already packed */ /* source string is already packed */
while (len >= 0) { while (len >= 0) {
*dest++ = *source++; *dest++ = *source++;
@ -214,7 +253,7 @@ int amx_StrPack(cell *dest,cell *source)
int amx_StrUnpack(cell *dest,cell *source) int amx_StrUnpack(cell *dest,cell *source)
{ {
if ((ucell)*source>UCHAR_MAX) { if ((ucell)*source>UNPACKEDMAX) {
/* unpack string, from top down (so string can be unpacked in place) */ /* unpack string, from top down (so string can be unpacked in place) */
cell c; cell c;
int i,len; int i,len;
@ -256,14 +295,15 @@ static cell AMX_NATIVE_CALL core_strlen(AMX *amx,cell *params)
static cell AMX_NATIVE_CALL strpack(AMX *amx,cell *params) static cell AMX_NATIVE_CALL strpack(AMX *amx,cell *params)
{ {
cell *cdest,*csrc; cell *cdest,*csrc;
int len,needed,lastaddr,err; int len,needed,err;
size_t lastaddr;
/* calculate number of cells needed for (packed) destination */ /* calculate number of cells needed for (packed) destination */
amx_GetAddr(amx,params[2],&csrc); amx_GetAddr(amx,params[2],&csrc);
amx_StrLen(csrc,&len); amx_StrLen(csrc,&len);
needed=(len+sizeof(cell))/sizeof(cell); /* # of cells needed */ needed=(len+sizeof(cell))/sizeof(cell); /* # of cells needed */
assert(needed>0); assert(needed>0);
lastaddr=params[1]+sizeof(cell)*needed-1; lastaddr=(size_t)(params[1]+sizeof(cell)*needed-1);
if (verify_addr(amx,(cell)lastaddr)!=AMX_ERR_NONE) if (verify_addr(amx,(cell)lastaddr)!=AMX_ERR_NONE)
return 0; return 0;
@ -278,13 +318,14 @@ static cell AMX_NATIVE_CALL strpack(AMX *amx,cell *params)
static cell AMX_NATIVE_CALL strunpack(AMX *amx,cell *params) static cell AMX_NATIVE_CALL strunpack(AMX *amx,cell *params)
{ {
cell *cdest,*csrc; cell *cdest,*csrc;
int len,err,lastaddr; int len,err;
size_t lastaddr;
/* calculate number of cells needed for (packed) destination */ /* calculate number of cells needed for (packed) destination */
amx_GetAddr(amx,params[2],&csrc); amx_GetAddr(amx,params[2],&csrc);
amx_StrLen(csrc,&len); amx_StrLen(csrc,&len);
assert(len>=0); assert(len>=0);
lastaddr=params[1]+sizeof(cell)*(len+1)-1; lastaddr=(size_t)(params[1]+sizeof(cell)*(len+1)-1);
if (verify_addr(amx,(cell)lastaddr)!=AMX_ERR_NONE) if (verify_addr(amx,(cell)lastaddr)!=AMX_ERR_NONE)
return 0; return 0;
@ -303,27 +344,46 @@ static cell AMX_NATIVE_CALL swapchars(AMX *amx,cell *params)
{ {
union { union {
cell c; cell c;
#if defined BIT16 #if SMALL_CELL_SIZE==16
uchar b[2]; uchar b[2];
#else #elif SMALL_CELL_SIZE==32
uchar b[4]; uchar b[4];
#elif SMALL_CELL_SIZE==64
uchar b[8];
#else
#error Unsupported cell size
#endif #endif
} value; } value;
uchar t; uchar t;
assert((size_t)params[0]==sizeof(cell)); assert((size_t)params[0]==sizeof(cell));
value.c = params[1]; value.c = params[1];
#if defined BIT16 #if SMALL_CELL_SIZE==16
t = value.b[0]; t = value.b[0];
value.b[0] = value.b[1]; value.b[0] = value.b[1];
value.b[1] = t; value.b[1] = t;
#else #elif SMALL_CELL_SIZE==32
t = value.b[0]; t = value.b[0];
value.b[0] = value.b[3]; value.b[0] = value.b[3];
value.b[3] = t; value.b[3] = t;
t = value.b[1]; t = value.b[1];
value.b[1] = value.b[2]; value.b[1] = value.b[2];
value.b[2] = t; value.b[2] = t;
#elif SMALL_CELL_SIZE==64
t = value.b[0];
value.b[0] = value.b[7];
value.b[7] = t;
t = value.b[1];
value.b[1] = value.b[6];
value.b[6] = t;
t = value.b[2];
value.b[2] = value.b[5];
value.b[5] = t;
t = value.b[3];
value.b[3] = value.b[4];
value.b[4] = t;
#else
#error Unsupported cell size
#endif #endif
return value.c; return value.c;
} }
@ -333,8 +393,13 @@ static cell AMX_NATIVE_CALL swapchars(AMX *amx,cell *params)
#endif #endif
static cell AMX_NATIVE_CALL core_tolower(AMX *amx,cell *params) static cell AMX_NATIVE_CALL core_tolower(AMX *amx,cell *params)
{ {
assert((size_t)params[0]==sizeof(cell)); #if defined __WIN32__ || defined _WIN32 || defined WIN32
return (cell)CharLower((LPTSTR)params[1]);
#elif defined _Windows
return (cell)AnsiLower((LPSTR)params[1]);
#else
return tolower((int)params[1]); return tolower((int)params[1]);
#endif
} }
#if defined __BORLANDC__ || defined __WATCOMC__ #if defined __BORLANDC__ || defined __WATCOMC__
@ -342,8 +407,13 @@ static cell AMX_NATIVE_CALL core_tolower(AMX *amx,cell *params)
#endif #endif
static cell AMX_NATIVE_CALL core_toupper(AMX *amx,cell *params) static cell AMX_NATIVE_CALL core_toupper(AMX *amx,cell *params)
{ {
assert((size_t)params[0]==sizeof(cell)); #if defined __WIN32__ || defined _WIN32 || defined WIN32
return (cell)CharUpper((LPTSTR)params[1]);
#elif defined _Windows
return (cell)AnsiUpper((LPSTR)params[1]);
#else
return toupper((int)params[1]); return toupper((int)params[1]);
#endif
} }
#if defined __BORLANDC__ || defined __WATCOMC__ #if defined __BORLANDC__ || defined __WATCOMC__
@ -382,7 +452,7 @@ static char *MakePackedString(cell *cptr)
amx_StrLen(cptr,&len); amx_StrLen(cptr,&len);
dest=(char *)malloc(len+sizeof(cell)); dest=(char *)malloc(len+sizeof(cell));
amx_GetString(dest,cptr); amx_GetString(dest,cptr,0);
return dest; return dest;
} }
@ -403,7 +473,7 @@ static cell AMX_NATIVE_CALL getproperty(AMX *amx,cell *params)
return 0; return 0;
} /* if */ } /* if */
amx_GetAddr(amx,params[4],&cstr); amx_GetAddr(amx,params[4],&cstr);
amx_SetString(cstr,item->name,1); amx_SetString(cstr,item->name,1,0);
} /* if */ } /* if */
free(name); free(name);
return (item!=NULL) ? item->value : 0; return (item!=NULL) ? item->value : 0;
@ -503,22 +573,6 @@ static cell AMX_NATIVE_CALL core_random(AMX *amx,cell *params)
return (cell)result; return (cell)result;
} }
#if 0
void core_Init(void)
{
/* reduced to a do-nothing routine */
}
void core_Exit(void)
{
#if !defined NOPROPLIST
while (proproot.next!=NULL)
list_delete(&proproot,proproot.next);
#endif
}
#endif
AMX_NATIVE_INFO core_Natives[] = { AMX_NATIVE_INFO core_Natives[] = {
{ "numargs", numargs }, { "numargs", numargs },
@ -545,4 +599,20 @@ AMX_NATIVE_INFO core_Natives[] = {
{ NULL, NULL } /* terminator */ { NULL, NULL } /* terminator */
}; };
int AMXEXPORT amx_CoreInit(AMX *amx)
{
return amx_Register(amx, core_Natives, -1);
}
#if defined __BORLANDC__ || defined __WATCOMC__
#pragma argsused
#endif
int AMXEXPORT amx_CoreCleanup(AMX *amx)
{
#if !defined NOPROPLIST
//??? delete only the properties owned by the AMX
while (proproot.next!=NULL)
list_delete(&proproot,proproot.next);
#endif
return AMX_ERR_NONE;
}