d5cb64e9bb5b0e9b269e1b378055b401fdb99161
[framework/uifw/embryo.git] / src / bin / embryo_cc_amx.h
1 /*  Abstract Machine for the Small compiler
2  *
3  *  Copyright (c) ITB CompuPhase, 1997-2003
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$
22  */
23
24 #include "embryo_cc_osdefs.h"
25
26 #ifndef EMBRYO_CC_AMX_H
27 #define EMBRYO_CC_AMX_H
28
29 #include <sys/types.h>
30
31 /* calling convention for all interface functions and callback functions */
32
33 /* File format version                          Required AMX version
34  *   0 (original version)                       0
35  *   1 (opcodes JUMP.pri, SWITCH and CASETBL)   1
36  *   2 (compressed files)                       2
37  *   3 (public variables)                       2
38  *   4 (opcodes SWAP.pri/alt and PUSHADDR)      4
39  *   5 (tagnames table)                         4
40  *   6 (reformatted header)                     6
41  *   7 (name table, opcodes SYMTAG & SYSREQ.D)  7
42  */
43 #define CUR_FILE_VERSION  7     /* current file version; also the current AMX version */
44 #define MIN_FILE_VERSION  6     /* lowest supported file format version for the current AMX version */
45 #define MIN_AMX_VERSION   7     /* minimum AMX version needed to support the current file format */
46
47 #if !defined CELL_TYPE
48 #define CELL_TYPE
49    typedef unsigned int    ucell;
50    typedef int     cell;
51 #endif
52
53    struct tagAMX;
54    typedef             cell(*AMX_NATIVE) (struct tagAMX * amx,
55                                                            cell * params);
56    typedef int         (* AMX_CALLBACK) (struct tagAMX * amx, cell index,
57                                                 cell * result, cell * params);
58    typedef int         (* AMX_DEBUG) (struct tagAMX * amx);
59
60    typedef struct
61    {
62       char          *name;
63       AMX_NATIVE func    ;
64    } AMX_NATIVE_INFO  ;
65
66 #define AMX_USERNUM     4
67 #define sEXPMAX         19      /* maximum name length for file version <= 6 */
68 #define sNAMEMAX        31      /* maximum name length of symbol name */
69
70    typedef struct tagAMX_FUNCSTUB
71    {
72       unsigned int        address;
73       char                name[sEXPMAX + 1];
74    } __attribute__((packed)) AMX_FUNCSTUB;
75
76 /* The AMX structure is the internal structure for many functions. Not all
77  * fields are valid at all times; many fields are cached in local variables.
78  */
79    typedef struct tagAMX
80    {
81       unsigned char *base;      /* points to the AMX header ("amxhdr") plus the code, optionally also the data */
82       unsigned char *data;      /* points to separate data+stack+heap, may be NULL */
83       AMX_CALLBACK callback;
84       AMX_DEBUG debug    ;      /* debug callback */
85       /* for external functions a few registers must be accessible from the outside */
86       cell cip           ;      /* instruction pointer: relative to base + amxhdr->cod */
87       cell frm           ;      /* stack frame base: relative to base + amxhdr->dat */
88       cell hea           ;      /* top of the heap: relative to base + amxhdr->dat */
89       cell hlw           ;      /* bottom of the heap: relative to base + amxhdr->dat */
90       cell stk           ;      /* stack pointer: relative to base + amxhdr->dat */
91       cell stp           ;      /* top of the stack: relative to base + amxhdr->dat */
92       int flags          ;      /* current status, see amx_Flags() */
93       /* for assertions and debug hook */
94       cell curline       ;
95       cell curfile       ;
96       int dbgcode        ;
97       cell dbgaddr       ;
98       cell dbgparam      ;
99       char          *dbgname;
100       /* user data */
101       long                usertags[AMX_USERNUM];
102       void          *userdata[AMX_USERNUM];
103       /* native functions can raise an error */
104       int error          ;
105       /* the sleep opcode needs to store the full AMX status */
106       cell pri           ;
107       cell alt           ;
108       cell reset_stk     ;
109       cell reset_hea     ;
110       cell          *syscall_d; /* relocated value/address for the SYSCALL.D opcode */
111    } __attribute__((packed)) AMX;
112
113 /* The AMX_HEADER structure is both the memory format as the file format. The
114  * structure is used internaly.
115  */
116    typedef struct tagAMX_HEADER
117    {
118       int size       ;  /* size of the "file" */
119       unsigned short magic     ;        /* signature */
120       char file_version  ;      /* file format version */
121       char amx_version   ;      /* required version of the AMX */
122       unsigned short flags      ;
123       unsigned short defsize    ;       /* size of a definition record */
124       int cod        ;  /* initial value of COD - code block */
125       int dat        ;  /* initial value of DAT - data block */
126       int hea        ;  /* initial value of HEA - start of the heap */
127       int stp        ;  /* initial value of STP - stack top */
128       int cip        ;  /* initial value of CIP - the instruction pointer */
129       int publics    ;  /* offset to the "public functions" table */
130       int natives    ;  /* offset to the "native functions" table */
131       int libraries  ;  /* offset to the table of libraries */
132       int pubvars    ;  /* the "public variables" table */
133       int tags       ;  /* the "public tagnames" table */
134       int nametable  ;  /* name table, file version 7 only */
135    } __attribute__((packed)) AMX_HEADER;
136 #define AMX_MAGIC       0xf1e0
137
138    enum
139    {
140       AMX_ERR_NONE,
141       /* reserve the first 15 error codes for exit codes of the abstract machine */
142       AMX_ERR_EXIT,             /* forced exit */
143       AMX_ERR_ASSERT,           /* assertion failed */
144       AMX_ERR_STACKERR,         /* stack/heap collision */
145       AMX_ERR_BOUNDS,           /* index out of bounds */
146       AMX_ERR_MEMACCESS,        /* invalid memory access */
147       AMX_ERR_INVINSTR,         /* invalid instruction */
148       AMX_ERR_STACKLOW,         /* stack underflow */
149       AMX_ERR_HEAPLOW,          /* heap underflow */
150       AMX_ERR_CALLBACK,         /* no callback, or invalid callback */
151       AMX_ERR_NATIVE,           /* native function failed */
152       AMX_ERR_DIVIDE,           /* divide by zero */
153       AMX_ERR_SLEEP,            /* go into sleepmode - code can be restarted */
154
155       AMX_ERR_MEMORY = 16,      /* out of memory */
156       AMX_ERR_FORMAT,           /* invalid file format */
157       AMX_ERR_VERSION,          /* file is for a newer version of the AMX */
158       AMX_ERR_NOTFOUND,         /* function not found */
159       AMX_ERR_INDEX,            /* invalid index parameter (bad entry point) */
160       AMX_ERR_DEBUG,            /* debugger cannot run */
161       AMX_ERR_INIT,             /* AMX not initialized (or doubly initialized) */
162       AMX_ERR_USERDATA,         /* unable to set user data field (table full) */
163       AMX_ERR_INIT_JIT,         /* cannot initialize the JIT */
164       AMX_ERR_PARAMS,           /* parameter error */
165       AMX_ERR_DOMAIN,           /* domain error, expression result does not fit in range */
166    };
167
168    enum
169    {
170       DBG_INIT,                 /* query/initialize */
171       DBG_FILE,                 /* file number in curfile, filename in name */
172       DBG_LINE,                 /* line number in curline, file number in curfile */
173       DBG_SYMBOL,               /* address in dbgaddr, class/type in dbgparam */
174       DBG_CLRSYM,               /* stack address below which locals should be removed. stack address in stk */
175       DBG_CALL,                 /* function call, address jumped to in dbgaddr */
176       DBG_RETURN,               /* function returns */
177       DBG_TERMINATE,            /* program ends, code address in dbgaddr, reason in dbgparam */
178       DBG_SRANGE,               /* symbol size and dimensions (arrays); level in dbgaddr (!); length in dbgparam */
179       DBG_SYMTAG,               /* tag of the most recent symbol (if non-zero), tag in dbgparam */
180    };
181
182 #define AMX_FLAG_CHAR16   0x01  /* characters are 16-bit */
183 #define AMX_FLAG_DEBUG    0x02  /* symbolic info. available */
184 #define AMX_FLAG_COMPACT  0x04  /* compact encoding */
185 #define AMX_FLAG_BIGENDIAN 0x08 /* big endian encoding */
186 #define AMX_FLAG_NOCHECKS  0x10 /* no array bounds checking */
187 #define AMX_FLAG_BROWSE 0x4000  /* browsing/relocating or executing */
188 #define AMX_FLAG_RELOC  0x8000  /* jump/call addresses relocated */
189
190 #define AMX_EXEC_MAIN   -1      /* start at program entry point */
191 #define AMX_EXEC_CONT   -2      /* continue from last address */
192
193 #define AMX_USERTAG(a,b,c,d)    ((a) | ((b)<<8) | ((long)(c)<<16) | ((long)(d)<<24))
194
195 #define AMX_EXPANDMARGIN  64
196
197 /* for native functions that use floating point parameters, the following
198  * two macros are convenient for casting a "cell" into a "float" type _without_
199  * changing the bit pattern
200  */
201 #define amx_ftoc(f)     ( * ((cell*)&f) )       /* float to cell */
202 #define amx_ctof(c)     ( * ((float*)&c) )      /* cell to float */
203
204 #define amx_StrParam(amx,param,result) {                             \
205             cell *amx_cstr_; int amx_length_;                        \
206             amx_GetAddr((amx), (param), &amx_cstr_);                 \
207             amx_StrLen(amx_cstr_, &amx_length_);                     \
208             if (amx_length_ > 0 &&                                   \
209                 ((result) = (char*)alloca(amx_length_ + 1)) != NULL) \
210               amx_GetString((result), amx_cstr_);                    \
211             else (result) = NULL;                                    \
212 }
213
214 #endif                          /* __AMX_H */