a88cd26f8e34d4b341fc3a461045ebd38bb7160f
[framework/uifw/embryo.git] / src / lib / embryo_private.h
1 #ifndef _EMBRYO_PRIVATE_H
2 #define _EMBRYO_PRIVATE_H
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <stdarg.h>
10 #include <string.h>
11
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #ifdef HAVE_ALLOCA_H
17 # include <alloca.h>
18 #elif defined __GNUC__
19 # define alloca __builtin_alloca
20 #elif defined _AIX
21 # define alloca __alloca
22 #elif defined _MSC_VER
23 # include <malloc.h>
24 # define alloca _alloca
25 #else
26 # include <stddef.h>
27 # ifdef  __cplusplus
28 extern "C"
29 # endif
30 void *alloca (size_t);
31 #endif
32
33 #include <eina_types.h>
34
35 #include "Embryo.h"
36
37 #ifdef __GNUC__
38 # if __GNUC__ >= 4
39 // BROKEN in gcc 4 on amd64
40 //#  pragma GCC visibility push(hidden)
41 # endif
42 #endif
43
44 typedef enum _Embryo_Opcode Embryo_Opcode;
45
46 enum _Embryo_Opcode
47 {
48    EMBRYO_OP_NONE,
49      EMBRYO_OP_LOAD_PRI,
50      EMBRYO_OP_LOAD_ALT,
51      EMBRYO_OP_LOAD_S_PRI,
52      EMBRYO_OP_LOAD_S_ALT,
53      EMBRYO_OP_LREF_PRI,
54      EMBRYO_OP_LREF_ALT,
55      EMBRYO_OP_LREF_S_PRI,
56      EMBRYO_OP_LREF_S_ALT,
57      EMBRYO_OP_LOAD_I,
58      EMBRYO_OP_LODB_I,
59      EMBRYO_OP_CONST_PRI,
60      EMBRYO_OP_CONST_ALT,
61      EMBRYO_OP_ADDR_PRI,
62      EMBRYO_OP_ADDR_ALT,
63      EMBRYO_OP_STOR_PRI,
64      EMBRYO_OP_STOR_ALT,
65      EMBRYO_OP_STOR_S_PRI,
66      EMBRYO_OP_STOR_S_ALT,
67      EMBRYO_OP_SREF_PRI,
68      EMBRYO_OP_SREF_ALT,
69      EMBRYO_OP_SREF_S_PRI,
70      EMBRYO_OP_SREF_S_ALT,
71      EMBRYO_OP_STOR_I,
72      EMBRYO_OP_STRB_I,
73      EMBRYO_OP_LIDX,
74      EMBRYO_OP_LIDX_B,
75      EMBRYO_OP_IDXADDR,
76      EMBRYO_OP_IDXADDR_B,
77      EMBRYO_OP_ALIGN_PRI,
78      EMBRYO_OP_ALIGN_ALT,
79      EMBRYO_OP_LCTRL,
80      EMBRYO_OP_SCTRL,
81      EMBRYO_OP_MOVE_PRI,
82      EMBRYO_OP_MOVE_ALT,
83      EMBRYO_OP_XCHG,
84      EMBRYO_OP_PUSH_PRI,
85      EMBRYO_OP_PUSH_ALT,
86      EMBRYO_OP_PUSH_R,
87      EMBRYO_OP_PUSH_C,
88      EMBRYO_OP_PUSH,
89      EMBRYO_OP_PUSH_S,
90      EMBRYO_OP_POP_PRI,
91      EMBRYO_OP_POP_ALT,
92      EMBRYO_OP_STACK,
93      EMBRYO_OP_HEAP,
94      EMBRYO_OP_PROC,
95      EMBRYO_OP_RET,
96      EMBRYO_OP_RETN,
97      EMBRYO_OP_CALL,
98      EMBRYO_OP_CALL_PRI,
99      EMBRYO_OP_JUMP,
100      EMBRYO_OP_JREL,
101      EMBRYO_OP_JZER,
102      EMBRYO_OP_JNZ,
103      EMBRYO_OP_JEQ,
104      EMBRYO_OP_JNEQ,
105      EMBRYO_OP_JLESS,
106      EMBRYO_OP_JLEQ,
107      EMBRYO_OP_JGRTR,
108      EMBRYO_OP_JGEQ,
109      EMBRYO_OP_JSLESS,
110      EMBRYO_OP_JSLEQ,
111      EMBRYO_OP_JSGRTR,
112      EMBRYO_OP_JSGEQ,
113      EMBRYO_OP_SHL,
114      EMBRYO_OP_SHR,
115      EMBRYO_OP_SSHR,
116      EMBRYO_OP_SHL_C_PRI,
117      EMBRYO_OP_SHL_C_ALT,
118      EMBRYO_OP_SHR_C_PRI,
119      EMBRYO_OP_SHR_C_ALT,
120      EMBRYO_OP_SMUL,
121      EMBRYO_OP_SDIV,
122      EMBRYO_OP_SDIV_ALT,
123      EMBRYO_OP_UMUL,
124      EMBRYO_OP_UDIV,
125      EMBRYO_OP_UDIV_ALT,
126      EMBRYO_OP_ADD,
127      EMBRYO_OP_SUB,
128      EMBRYO_OP_SUB_ALT,
129      EMBRYO_OP_AND,
130      EMBRYO_OP_OR,
131      EMBRYO_OP_XOR,
132      EMBRYO_OP_NOT,
133      EMBRYO_OP_NEG,
134      EMBRYO_OP_INVERT,
135      EMBRYO_OP_ADD_C,
136      EMBRYO_OP_SMUL_C,
137      EMBRYO_OP_ZERO_PRI,
138      EMBRYO_OP_ZERO_ALT,
139      EMBRYO_OP_ZERO,
140      EMBRYO_OP_ZERO_S,
141      EMBRYO_OP_SIGN_PRI,
142      EMBRYO_OP_SIGN_ALT,
143      EMBRYO_OP_EQ,
144      EMBRYO_OP_NEQ,
145      EMBRYO_OP_LESS,
146      EMBRYO_OP_LEQ,
147      EMBRYO_OP_GRTR,
148      EMBRYO_OP_GEQ,
149      EMBRYO_OP_SLESS,
150      EMBRYO_OP_SLEQ,
151      EMBRYO_OP_SGRTR,
152      EMBRYO_OP_SGEQ,
153      EMBRYO_OP_EQ_C_PRI,
154      EMBRYO_OP_EQ_C_ALT,
155      EMBRYO_OP_INC_PRI,
156      EMBRYO_OP_INC_ALT,
157      EMBRYO_OP_INC,
158      EMBRYO_OP_INC_S,
159      EMBRYO_OP_INC_I,
160      EMBRYO_OP_DEC_PRI,
161      EMBRYO_OP_DEC_ALT,
162      EMBRYO_OP_DEC,
163      EMBRYO_OP_DEC_S,
164      EMBRYO_OP_DEC_I,
165      EMBRYO_OP_MOVS,
166      EMBRYO_OP_CMPS,
167      EMBRYO_OP_FILL,
168      EMBRYO_OP_HALT,
169      EMBRYO_OP_BOUNDS,
170      EMBRYO_OP_SYSREQ_PRI,
171      EMBRYO_OP_SYSREQ_C,
172      EMBRYO_OP_FILE,
173      EMBRYO_OP_LINE,
174      EMBRYO_OP_SYMBOL,
175      EMBRYO_OP_SRANGE,
176      EMBRYO_OP_JUMP_PRI,
177      EMBRYO_OP_SWITCH,
178      EMBRYO_OP_CASETBL,
179      EMBRYO_OP_SWAP_PRI,
180      EMBRYO_OP_SWAP_ALT,
181      EMBRYO_OP_PUSHADDR,
182      EMBRYO_OP_NOP,
183      EMBRYO_OP_SYSREQ_D,
184      EMBRYO_OP_SYMTAG,
185      /* ----- */
186      EMBRYO_OP_NUM_OPCODES
187 };
188
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))) \
201 : (entry)->name)
202 #else
203 # define GETENTRYNAME(hdr, entry) \
204 (((hdr)->defsize == 2 * sizeof(unsigned int)) \
205 ? (char *)((unsigned char*)(hdr) + *((unsigned int *)(entry) + 1)) \
206 : (entry)->name)
207 #endif
208
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);
228
229 typedef struct _Embryo_Param        Embryo_Param;
230 typedef struct _Embryo_Header       Embryo_Header;
231 typedef struct _Embryo_Func_Stub    Embryo_Func_Stub;
232
233 typedef Embryo_Cell (*Embryo_Native)(Embryo_Program *ep, Embryo_Cell *params);
234
235 struct _Embryo_Param
236 {
237    char        *string;
238    Embryo_Cell *cell_array;
239    int          cell_array_size;
240    Embryo_Cell  cell;
241 };
242
243 struct _Embryo_Program
244 {
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 */
256    int error;
257    /* the sleep opcode needs to store the full Embryo_Program status */
258    Embryo_Cell pri;
259    Embryo_Cell alt;
260    Embryo_Cell reset_stk;
261    Embryo_Cell reset_hea;
262    Embryo_Cell *syscall_d; /* relocated value/address for the SYSCALL.D opcode */
263
264    /* extended stuff */
265    Embryo_Native *native_calls;
266    int            native_calls_size;
267    int            native_calls_alloc;
268
269    unsigned char *code;
270    unsigned char  dont_free_code : 1;
271    Embryo_Cell    retval;
272
273    Embryo_Param  *params;
274    int            params_size;
275    int            params_alloc;
276
277    int            run_count;
278
279    int            max_run_cycles;
280
281    void          *data;
282 };
283
284 struct _Embryo_Func_Stub
285 {
286    int  address;
287    char name[sEXPMAX+1];
288 } __attribute__((packed));
289
290 struct _Embryo_Header
291 {
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 */
296    short flags;
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));
310
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);
316
317 #endif