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);
33 #include <eina_types.h>
39 // BROKEN in gcc 4 on amd64
40 //# pragma GCC visibility push(hidden)
44 typedef enum _Embryo_Opcode Embryo_Opcode;
170 EMBRYO_OP_SYSREQ_PRI,
186 EMBRYO_OP_NUM_OPCODES
189 #define NUMENTRIES(hdr, field, nextfield) \
190 (int)(((hdr)->nextfield - (hdr)->field) / (hdr)->defsize)
191 #define GETENTRY(hdr, table, index) \
192 (Embryo_Func_Stub *)((unsigned char*)(hdr) + \
193 (int)(hdr)->table + index * (hdr)->defsize)
194 #ifdef WORDS_BIGENDIAN
195 static int __inline __entryswap32(int v)
196 {int vv; vv = v; embryo_swap_32((unsigned int *)&vv); return vv;}
197 # define GETENTRYNAME(hdr, entry) \
198 (((hdr)->defsize == 2 * sizeof(unsigned int)) \
199 ? (char *)((unsigned char*)(hdr) + \
200 __entryswap32(*((unsigned int *)(entry) + 1))) \
203 # define GETENTRYNAME(hdr, entry) \
204 (((hdr)->defsize == 2 * sizeof(unsigned int)) \
205 ? (char *)((unsigned char*)(hdr) + *((unsigned int *)(entry) + 1)) \
209 #define CUR_FILE_VERSION 7 /* current file version; also the current Embryo_Program version */
210 #define MIN_FILE_VERSION 7 /* lowest supported file format version for the current Embryo_Program version */
211 #define MIN_AMX_VERSION 7 /* minimum Embryo_Program version needed to support the current file format */
212 #define sEXPMAX 19 /* maximum name length for file version <= 6 */
213 #define sNAMEMAX 31 /* maximum name length of symbol name */
214 #define EMBRYO_MAGIC 0xf1e0 /* magic byte pattern */
215 #define EMBRYO_FLAG_COMPACT 0x04 /* compact encoding */
216 #define EMBRYO_FLAG_RELOC 0x8000 /* jump/call addresses relocated */
217 #define GETPARAM(v) (v = *(Embryo_Cell *)cip++)
218 #define PUSH(v) (stk -= sizeof(Embryo_Cell), *(Embryo_Cell *)(data + (int)stk) = v)
219 #define POP(v) (v = *(Embryo_Cell *)(data + (int)stk), stk += sizeof(Embryo_Cell))
220 #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;}
221 #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;}
222 #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;}
223 #define STKMARGIN ((Embryo_Cell)(16 * sizeof(Embryo_Cell)))
224 #define CHKMARGIN() if ((hea + STKMARGIN) > stk) {ep->error = EMBRYO_ERROR_STACKERR; return 0;}
225 #define CHKSTACK() if (stk > ep->stp) {ep->run_count--; ep->error = EMBRYO_ERROR_STACKLOW; return 0;}
226 #define CHKHEAP() if (hea < ep->hlw) {ep->run_count--; ep->error = EMBRYO_ERROR_HEAPLOW; return 0;}
227 #define CHKMEM(x) if ((((x) >= hea) && ((x) < stk)) || ((Embryo_UCell)(x) >= (Embryo_UCell)ep->stp)) ABORT(ep, EMBRYO_ERROR_MEMACCESS);
229 typedef struct _Embryo_Param Embryo_Param;
230 typedef struct _Embryo_Header Embryo_Header;
231 typedef struct _Embryo_Func_Stub Embryo_Func_Stub;
233 typedef Embryo_Cell (*Embryo_Native)(Embryo_Program *ep, Embryo_Cell *params);
238 Embryo_Cell *cell_array;
243 struct _Embryo_Program
245 unsigned char *base; /* points to the Embryo_Program header ("ephdr") plus the code, optionally also the data */
246 int pushes; /* number of pushes - pops */
247 /* for external functions a few registers must be accessible from the outside */
248 Embryo_Cell cip; /* instruction pointer: relative to base + ephdr->cod */
249 Embryo_Cell frm; /* stack frame base: relative to base + ephdr->dat */
250 Embryo_Cell hea; /* top of the heap: relative to base + ephdr->dat */
251 Embryo_Cell hlw; /* bottom of the heap: relative to base + ephdr->dat */
252 Embryo_Cell stk; /* stack pointer: relative to base + ephdr->dat */
253 Embryo_Cell stp; /* top of the stack: relative to base + ephdr->dat */
254 int flags; /* current status */
255 /* native functions can raise an error */
257 /* the sleep opcode needs to store the full Embryo_Program status */
260 Embryo_Cell reset_stk;
261 Embryo_Cell reset_hea;
262 Embryo_Cell *syscall_d; /* relocated value/address for the SYSCALL.D opcode */
265 Embryo_Native *native_calls;
266 int native_calls_size;
267 int native_calls_alloc;
270 unsigned char dont_free_code : 1;
273 Embryo_Param *params;
284 struct _Embryo_Func_Stub
287 char name[sEXPMAX+1];
288 } __attribute__((packed));
290 struct _Embryo_Header
292 unsigned int size; /* size of the "file" */
293 unsigned short magic; /* signature */
294 char file_version; /* file format version */
295 char ep_version; /* required version of the Embryo_Program */
297 short defsize; /* size of a definition record */
298 int cod; /* initial value of COD - code block */
299 int dat; /* initial value of DAT - data block */
300 int hea; /* initial value of HEA - start of the heap */
301 int stp; /* initial value of STP - stack top */
302 int cip; /* initial value of CIP - the instruction pointer */
303 int publics; /* offset to the "public functions" table */
304 int natives; /* offset to the "native functions" table */
305 int libraries; /* offset to the table of libraries */
306 int pubvars; /* the "public variables" table */
307 int tags; /* the "public tagnames" table */
308 int nametable; /* name table, file version 7 only */
309 } __attribute__((packed));
311 void _embryo_args_init(Embryo_Program *ep);
312 void _embryo_fp_init(Embryo_Program *ep);
313 void _embryo_rand_init(Embryo_Program *ep);
314 void _embryo_str_init(Embryo_Program *ep);
315 void _embryo_time_init(Embryo_Program *ep);