1 #ifndef _EMBRYO_PRIVATE_H
2 #define _EMBRYO_PRIVATE_H
18 #elif defined __GNUC__
19 # define alloca __builtin_alloca
21 # define alloca __alloca
22 #elif defined _MSC_VER
24 # define alloca _alloca
30 void *alloca (size_t);
37 // BROKEN in gcc 4 on amd64
38 //# pragma GCC visibility push(hidden)
42 #if HAVE___ATTRIBUTE__
43 #define __UNUSED__ __attribute__((unused))
48 typedef enum _Embryo_Opcode Embryo_Opcode;
174 EMBRYO_OP_SYSREQ_PRI,
190 EMBRYO_OP_NUM_OPCODES
193 #define NUMENTRIES(hdr, field, nextfield) \
194 (int)(((hdr)->nextfield - (hdr)->field) / (hdr)->defsize)
195 #define GETENTRY(hdr, table, index) \
196 (Embryo_Func_Stub *)((unsigned char*)(hdr) + \
197 (int)(hdr)->table + index * (hdr)->defsize)
198 #ifdef WORDS_BIGENDIAN
199 static int __inline __entryswap32(int v)
200 {int vv; vv = v; embryo_swap_32((unsigned int *)&vv); return vv;}
201 # define GETENTRYNAME(hdr, entry) \
202 (((hdr)->defsize == 2 * sizeof(unsigned int)) \
203 ? (char *)((unsigned char*)(hdr) + \
204 __entryswap32(*((unsigned int *)(entry) + 1))) \
207 # define GETENTRYNAME(hdr, entry) \
208 (((hdr)->defsize == 2 * sizeof(unsigned int)) \
209 ? (char *)((unsigned char*)(hdr) + *((unsigned int *)(entry) + 1)) \
213 #define CUR_FILE_VERSION 7 /* current file version; also the current Embryo_Program version */
214 #define MIN_FILE_VERSION 7 /* lowest supported file format version for the current Embryo_Program version */
215 #define MIN_AMX_VERSION 7 /* minimum Embryo_Program version needed to support the current file format */
216 #define sEXPMAX 19 /* maximum name length for file version <= 6 */
217 #define sNAMEMAX 31 /* maximum name length of symbol name */
218 #define EMBRYO_MAGIC 0xf1e0 /* magic byte pattern */
219 #define EMBRYO_FLAG_COMPACT 0x04 /* compact encoding */
220 #define EMBRYO_FLAG_RELOC 0x8000 /* jump/call addresses relocated */
221 #define GETPARAM(v) (v = *(Embryo_Cell *)cip++)
222 #define PUSH(v) (stk -= sizeof(Embryo_Cell), *(Embryo_Cell *)(data + (int)stk) = v)
223 #define POP(v) (v = *(Embryo_Cell *)(data + (int)stk), stk += sizeof(Embryo_Cell))
224 #define ABORT(ep,v) {(ep)->stk = reset_stk; (ep)->hea = reset_hea; (ep)->run_count--; ep->error = v; (ep)->max_run_cycles = max_run_cycles; return EMBRYO_PROGRAM_FAIL;}
225 #define OK(ep,v) {(ep)->stk = reset_stk; (ep)->hea = reset_hea; (ep)->run_count--; ep->error = v; (ep)->max_run_cycles = max_run_cycles; return EMBRYO_PROGRAM_OK;}
226 #define TOOLONG(ep) {(ep)->pri = pri; (ep)->cip = (Embryo_Cell)((unsigned char *)cip - code); (ep)->alt = alt; (ep)->frm = frm; (ep)->stk = stk; (ep)->hea = hea; (ep)->reset_stk = reset_stk; (ep)->reset_hea = reset_hea; (ep)->run_count--; (ep)->max_run_cycles = max_run_cycles; return EMBRYO_PROGRAM_TOOLONG;}
227 #define STKMARGIN ((Embryo_Cell)(16 * sizeof(Embryo_Cell)))
228 #define CHKMARGIN() if ((hea + STKMARGIN) > stk) {ep->error = EMBRYO_ERROR_STACKERR; return 0;}
229 #define CHKSTACK() if (stk > ep->stp) {ep->run_count--; ep->error = EMBRYO_ERROR_STACKLOW; return 0;}
230 #define CHKHEAP() if (hea < ep->hlw) {ep->run_count--; ep->error = EMBRYO_ERROR_HEAPLOW; return 0;}
231 #define CHKMEM(x) if ((((x) >= hea) && ((x) < stk)) || ((Embryo_UCell)(x) >= (Embryo_UCell)ep->stp)) ABORT(ep, EMBRYO_ERROR_MEMACCESS);
233 typedef struct _Embryo_Param Embryo_Param;
234 typedef struct _Embryo_Header Embryo_Header;
235 typedef struct _Embryo_Func_Stub Embryo_Func_Stub;
237 typedef Embryo_Cell (*Embryo_Native)(Embryo_Program *ep, Embryo_Cell *params);
242 Embryo_Cell *cell_array;
247 struct _Embryo_Program
249 unsigned char *base; /* points to the Embryo_Program header ("ephdr") plus the code, optionally also the data */
250 int pushes; /* number of pushes - pops */
251 /* for external functions a few registers must be accessible from the outside */
252 Embryo_Cell cip; /* instruction pointer: relative to base + ephdr->cod */
253 Embryo_Cell frm; /* stack frame base: relative to base + ephdr->dat */
254 Embryo_Cell hea; /* top of the heap: relative to base + ephdr->dat */
255 Embryo_Cell hlw; /* bottom of the heap: relative to base + ephdr->dat */
256 Embryo_Cell stk; /* stack pointer: relative to base + ephdr->dat */
257 Embryo_Cell stp; /* top of the stack: relative to base + ephdr->dat */
258 int flags; /* current status */
259 /* native functions can raise an error */
261 /* the sleep opcode needs to store the full Embryo_Program status */
264 Embryo_Cell reset_stk;
265 Embryo_Cell reset_hea;
266 Embryo_Cell *syscall_d; /* relocated value/address for the SYSCALL.D opcode */
269 Embryo_Native *native_calls;
270 int native_calls_size;
271 int native_calls_alloc;
274 unsigned char dont_free_code : 1;
277 Embryo_Param *params;
288 struct _Embryo_Func_Stub
291 char name[sEXPMAX+1];
292 } __attribute__((packed));
294 struct _Embryo_Header
296 unsigned int size; /* size of the "file" */
297 unsigned short magic; /* signature */
298 char file_version; /* file format version */
299 char ep_version; /* required version of the Embryo_Program */
301 short defsize; /* size of a definition record */
302 int cod; /* initial value of COD - code block */
303 int dat; /* initial value of DAT - data block */
304 int hea; /* initial value of HEA - start of the heap */
305 int stp; /* initial value of STP - stack top */
306 int cip; /* initial value of CIP - the instruction pointer */
307 int publics; /* offset to the "public functions" table */
308 int natives; /* offset to the "native functions" table */
309 int libraries; /* offset to the table of libraries */
310 int pubvars; /* the "public variables" table */
311 int tags; /* the "public tagnames" table */
312 int nametable; /* name table, file version 7 only */
313 } __attribute__((packed));
315 void _embryo_args_init(Embryo_Program *ep);
316 void _embryo_fp_init(Embryo_Program *ep);
317 void _embryo_rand_init(Embryo_Program *ep);
318 void _embryo_str_init(Embryo_Program *ep);
319 void _embryo_time_init(Embryo_Program *ep);