1 #ifndef _EMBRYO_PRIVATE_H
2 #define _EMBRYO_PRIVATE_H
5 #include <eina_types.h>
10 // BROKEN in gcc 4 on amd64
11 //# pragma GCC visibility push(hidden)
15 typedef enum _Embryo_Opcode Embryo_Opcode;
141 EMBRYO_OP_SYSREQ_PRI,
157 EMBRYO_OP_NUM_OPCODES
160 #define NUMENTRIES(hdr, field, nextfield) \
161 (int)(((hdr)->nextfield - (hdr)->field) / (hdr)->defsize)
162 #define GETENTRY(hdr, table, index) \
163 (Embryo_Func_Stub *)((unsigned char*)(hdr) + \
164 (int)(hdr)->table + index * (hdr)->defsize)
165 #ifdef WORDS_BIGENDIAN
166 static int __inline __entryswap32(int v)
167 {int vv; vv = v; embryo_swap_32((unsigned int *)&vv); return vv;}
168 # define GETENTRYNAME(hdr, entry) \
169 (((hdr)->defsize == 2 * sizeof(unsigned int)) \
170 ? (char *)((unsigned char*)(hdr) + \
171 __entryswap32(*((unsigned int *)(entry) + 1))) \
174 # define GETENTRYNAME(hdr, entry) \
175 (((hdr)->defsize == 2 * sizeof(unsigned int)) \
176 ? (char *)((unsigned char*)(hdr) + *((unsigned int *)(entry) + 1)) \
180 #define CUR_FILE_VERSION 7 /* current file version; also the current Embryo_Program version */
181 #define MIN_FILE_VERSION 7 /* lowest supported file format version for the current Embryo_Program version */
182 #define MIN_AMX_VERSION 7 /* minimum Embryo_Program version needed to support the current file format */
183 #define sEXPMAX 19 /* maximum name length for file version <= 6 */
184 #define sNAMEMAX 31 /* maximum name length of symbol name */
185 #define EMBRYO_MAGIC 0xf1e0 /* magic byte pattern */
186 #define EMBRYO_FLAG_COMPACT 0x04 /* compact encoding */
187 #define EMBRYO_FLAG_RELOC 0x8000 /* jump/call addresses relocated */
188 #define GETPARAM(v) (v = *(Embryo_Cell *)cip++)
189 #define PUSH(v) (stk -= sizeof(Embryo_Cell), *(Embryo_Cell *)(data + (int)stk) = v)
190 #define POP(v) (v = *(Embryo_Cell *)(data + (int)stk), stk += sizeof(Embryo_Cell))
191 #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;}
192 #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;}
193 #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;}
194 #define STKMARGIN ((Embryo_Cell)(16 * sizeof(Embryo_Cell)))
195 #define CHKMARGIN() if ((hea + STKMARGIN) > stk) {ep->error = EMBRYO_ERROR_STACKERR; return 0;}
196 #define CHKSTACK() if (stk > ep->stp) {ep->run_count--; ep->error = EMBRYO_ERROR_STACKLOW; return 0;}
197 #define CHKHEAP() if (hea < ep->hlw) {ep->run_count--; ep->error = EMBRYO_ERROR_HEAPLOW; return 0;}
198 #define CHKMEM(x) if ((((x) >= hea) && ((x) < stk)) || ((Embryo_UCell)(x) >= (Embryo_UCell)ep->stp)) ABORT(ep, EMBRYO_ERROR_MEMACCESS);
200 typedef struct _Embryo_Param Embryo_Param;
201 typedef struct _Embryo_Header Embryo_Header;
202 typedef struct _Embryo_Func_Stub Embryo_Func_Stub;
204 typedef Embryo_Cell (*Embryo_Native)(Embryo_Program *ep, Embryo_Cell *params);
209 Embryo_Cell *cell_array;
214 struct _Embryo_Program
216 unsigned char *base; /* points to the Embryo_Program header ("ephdr") plus the code, optionally also the data */
217 int pushes; /* number of pushes - pops */
218 /* for external functions a few registers must be accessible from the outside */
219 Embryo_Cell cip; /* instruction pointer: relative to base + ephdr->cod */
220 Embryo_Cell frm; /* stack frame base: relative to base + ephdr->dat */
221 Embryo_Cell hea; /* top of the heap: relative to base + ephdr->dat */
222 Embryo_Cell hlw; /* bottom of the heap: relative to base + ephdr->dat */
223 Embryo_Cell stk; /* stack pointer: relative to base + ephdr->dat */
224 Embryo_Cell stp; /* top of the stack: relative to base + ephdr->dat */
225 int flags; /* current status */
226 /* native functions can raise an error */
228 /* the sleep opcode needs to store the full Embryo_Program status */
231 Embryo_Cell reset_stk;
232 Embryo_Cell reset_hea;
233 Embryo_Cell *syscall_d; /* relocated value/address for the SYSCALL.D opcode */
236 Embryo_Native *native_calls;
237 int native_calls_size;
238 int native_calls_alloc;
241 unsigned char dont_free_code : 1;
244 Embryo_Param *params;
255 struct _Embryo_Func_Stub
258 char name[sEXPMAX+1];
259 } __attribute__((packed));
261 struct _Embryo_Header
263 unsigned int size; /* size of the "file" */
264 unsigned short magic; /* signature */
265 char file_version; /* file format version */
266 char ep_version; /* required version of the Embryo_Program */
268 short defsize; /* size of a definition record */
269 int cod; /* initial value of COD - code block */
270 int dat; /* initial value of DAT - data block */
271 int hea; /* initial value of HEA - start of the heap */
272 int stp; /* initial value of STP - stack top */
273 int cip; /* initial value of CIP - the instruction pointer */
274 int publics; /* offset to the "public functions" table */
275 int natives; /* offset to the "native functions" table */
276 int libraries; /* offset to the table of libraries */
277 int pubvars; /* the "public variables" table */
278 int tags; /* the "public tagnames" table */
279 int nametable; /* name table, file version 7 only */
280 } __attribute__((packed));
282 void _embryo_args_init(Embryo_Program *ep);
283 void _embryo_fp_init(Embryo_Program *ep);
284 void _embryo_rand_init(Embryo_Program *ep);
285 void _embryo_str_init(Embryo_Program *ep);
286 void _embryo_time_init(Embryo_Program *ep);