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