1 /* Abstract Machine for the Small compiler
3 * Copyright (c) ITB CompuPhase, 1997-2003
4 * Portions Copyright (c) Carsten Haitzler, 2004-2010 <raster@rasterman.com>
6 * This software is provided "as-is", without any express or implied warranty.
7 * In no event will the authors be held liable for any damages arising from
8 * the use of this software.
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software in
16 * a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 * 3. This notice may not be removed or altered from any source distribution.
33 #include "embryo_private.h"
36 #define JUMPABS(base, ip) ((Embryo_Cell *)(code + (*ip)))
38 #ifdef WORDS_BIGENDIAN
39 static void _embryo_byte_swap_16 (unsigned short *v);
40 static void _embryo_byte_swap_32 (unsigned int *v);
42 static int _embryo_native_call (Embryo_Program *ep, Embryo_Cell index, Embryo_Cell *result, Embryo_Cell *params);
43 static int _embryo_func_get (Embryo_Program *ep, int index, char *funcname);
44 static int _embryo_var_get (Embryo_Program *ep, int index, char *varname, Embryo_Cell *ep_addr);
45 static int _embryo_program_init (Embryo_Program *ep, void *code);
47 #ifdef WORDS_BIGENDIAN
49 _embryo_byte_swap_16(unsigned short *v)
53 s = (unsigned char *)v;
54 t = s[0]; s[0] = s[1]; s[1] = t;
58 _embryo_byte_swap_32(unsigned int *v)
62 s = (unsigned char *)v;
63 t = s[0]; s[0] = s[3]; s[3] = t;
64 t = s[1]; s[1] = s[2]; s[2] = t;
69 _embryo_native_call(Embryo_Program *ep, Embryo_Cell index, Embryo_Cell *result, Embryo_Cell *params)
72 Embryo_Func_Stub *func_entry;
75 hdr = (Embryo_Header *)ep->base;
76 func_entry = GETENTRY(hdr, natives, index);
77 if ((func_entry->address <= 0) ||
78 (func_entry->address > ep->native_calls_size))
80 ep->error = EMBRYO_ERROR_CALLBACK;
83 f = ep->native_calls[func_entry->address - 1];
86 ep->error = EMBRYO_ERROR_CALLBACK;
89 ep->error = EMBRYO_ERROR_NONE;
90 *result = f(ep, params);
95 _embryo_func_get(Embryo_Program *ep, int index, char *funcname)
98 Embryo_Func_Stub *func;
100 hdr = (Embryo_Header *)ep->code;
101 if (index >= (Embryo_Cell)NUMENTRIES(hdr, publics, natives))
102 return EMBRYO_ERROR_INDEX;
104 func = GETENTRY(hdr, publics, index);
105 strcpy(funcname, GETENTRYNAME(hdr, func));
106 return EMBRYO_ERROR_NONE;
110 _embryo_var_get(Embryo_Program *ep, int index, char *varname, Embryo_Cell *ep_addr)
114 Embryo_Func_Stub *var;
116 hdr=(Embryo_Header *)ep->base;
117 if (index >= (Embryo_Cell)NUMENTRIES(hdr, pubvars, tags))
118 return EMBRYO_ERROR_INDEX;
120 var = GETENTRY(hdr, pubvars, index);
121 strcpy(varname, GETENTRYNAME(hdr, var));
122 *ep_addr = var->address;
123 return EMBRYO_ERROR_NONE;
127 _embryo_program_init(Embryo_Program *ep, void *code)
131 if ((ep->flags & EMBRYO_FLAG_RELOC)) return 1;
132 ep->code = (unsigned char *)code;
133 hdr = (Embryo_Header *)ep->code;
134 #ifdef WORDS_BIGENDIAN
135 embryo_swap_32((unsigned int *)&hdr->size);
136 embryo_swap_16((unsigned short *)&hdr->magic);
137 embryo_swap_16((unsigned short *)&hdr->flags);
138 embryo_swap_16((unsigned short *)&hdr->defsize);
139 embryo_swap_32((unsigned int *)&hdr->cod);
140 embryo_swap_32((unsigned int *)&hdr->dat);
141 embryo_swap_32((unsigned int *)&hdr->hea);
142 embryo_swap_32((unsigned int *)&hdr->stp);
143 embryo_swap_32((unsigned int *)&hdr->cip);
144 embryo_swap_32((unsigned int *)&hdr->publics);
145 embryo_swap_32((unsigned int *)&hdr->natives);
146 embryo_swap_32((unsigned int *)&hdr->libraries);
147 embryo_swap_32((unsigned int *)&hdr->pubvars);
148 embryo_swap_32((unsigned int *)&hdr->tags);
149 embryo_swap_32((unsigned int *)&hdr->nametable);
152 if (hdr->magic != EMBRYO_MAGIC) return 0;
153 if ((hdr->file_version < MIN_FILE_VERSION) ||
154 (hdr->ep_version > CUR_FILE_VERSION)) return 0;
155 if ((hdr->defsize != sizeof(Embryo_Func_Stub)) &&
156 (hdr->defsize != (2 * sizeof(unsigned int)))) return 0;
157 if (hdr->defsize == (2 * sizeof(unsigned int)))
161 len = (unsigned short*)((unsigned char*)ep->code + hdr->nametable);
162 #ifdef WORDS_BIGENDIAN
163 embryo_swap_16((unsigned short *)len);
165 if (*len > sNAMEMAX) return 0;
167 if (hdr->stp <= 0) return 0;
168 if ((hdr->flags & EMBRYO_FLAG_COMPACT)) return 0;
170 #ifdef WORDS_BIGENDIAN
172 Embryo_Func_Stub *fs;
175 /* also align all addresses in the public function, public variable and */
176 /* public tag tables */
177 fs = GETENTRY(hdr, publics, 0);
178 num = NUMENTRIES(hdr, publics, natives);
179 for (i = 0; i < num; i++)
181 embryo_swap_32(&(fs->address));
182 fs = (Embryo_Func_Stub *)((unsigned char *)fs + hdr->defsize);
185 fs = GETENTRY(hdr, pubvars, 0);
186 num = NUMENTRIES(hdr, pubvars, tags);
187 for (i = 0; i < num; i++)
189 embryo_swap_32(&(fs->address));
190 fs = (Embryo_Func_Stub *)((unsigned char *)fs + hdr->defsize);
193 fs = GETENTRY(hdr, tags, 0);
194 num = NUMENTRIES(hdr, tags, nametable);
195 for (i = 0; i < num; i++)
197 embryo_swap_32(&(fs->address));
198 fs = (Embryo_Func_Stub *)((unsigned char *)fs + hdr->defsize);
202 ep->flags = EMBRYO_FLAG_RELOC;
205 Embryo_Cell cip, code_size, cip_end;
208 code_size = hdr->dat - hdr->cod;
209 code = (Embryo_Cell *)((unsigned char *)ep->code + (int)hdr->cod);
210 cip_end = code_size / sizeof(Embryo_Cell);
211 for (cip = 0; cip < cip_end; cip++)
213 /* move this here - later we probably want something that verifies opcodes
214 * are valid and ok...
216 #ifdef WORDS_BIGENDIAN
217 embryo_swap_32(&(code[cip]));
222 /* init native api for handling floating point - default in embryo */
223 _embryo_args_init(ep);
225 _embryo_rand_init(ep);
226 _embryo_str_init(ep);
227 _embryo_time_init(ep);
231 /*** EXPORTED CALLS ***/
233 EAPI Embryo_Program *
234 embryo_program_new(void *data, int size)
239 if (size < (int)sizeof(Embryo_Header)) return NULL;
241 ep = calloc(1, sizeof(Embryo_Program));
242 if (!ep) return NULL;
244 code_data = malloc(size);
250 memcpy(code_data, data, size);
251 if (_embryo_program_init(ep, code_data)) return ep;
257 EAPI Embryo_Program *
258 embryo_program_const_new(void *data, int size)
262 if (size < (int)sizeof(Embryo_Header)) return NULL;
264 ep = calloc(1, sizeof(Embryo_Program));
265 if (!ep) return NULL;
267 if (_embryo_program_init(ep, data))
269 ep->dont_free_code = 1;
276 EAPI Embryo_Program *
277 embryo_program_load(const char *file)
282 void *program = NULL;
283 int program_size = 0;
285 f = fopen(file, "rb");
287 fseek(f, 0, SEEK_END);
288 program_size = ftell(f);
289 fseek(f, 0L, SEEK_SET);
290 if (program_size < (int)sizeof(Embryo_Header))
295 if (fread(&hdr, sizeof(Embryo_Header), 1, f) != 1)
300 fseek(f, 0L, SEEK_SET);
301 #ifdef WORDS_BIGENDIAN
302 embryo_swap_32((unsigned int *)(&hdr.size));
304 if ((int)hdr.size < program_size) program_size = hdr.size;
305 program = malloc(program_size);
311 if (fread(program, program_size, 1, f) != 1)
317 ep = embryo_program_new(program, program_size);
324 embryo_program_free(Embryo_Program *ep)
328 if (ep->base) free(ep->base);
329 if ((!ep->dont_free_code) && (ep->code)) free(ep->code);
330 if (ep->native_calls) free(ep->native_calls);
331 for (i = 0; i < ep->params_size; i++)
333 if (ep->params[i].string) free(ep->params[i].string);
334 if (ep->params[i].cell_array) free(ep->params[i].cell_array);
336 if (ep->params) free(ep->params);
342 embryo_program_native_call_add(Embryo_Program *ep, const char *name, Embryo_Cell (*func) (Embryo_Program *ep, Embryo_Cell *params))
344 Embryo_Func_Stub *func_entry;
348 if ((!ep ) || (!name) || (!func)) return;
349 if (strlen(name) > sNAMEMAX) return;
351 hdr = (Embryo_Header *)ep->code;
352 if (hdr->defsize < 1) return;
353 num = NUMENTRIES(hdr, natives, libraries);
354 if (num <= 0) return;
356 ep->native_calls_size++;
357 if (ep->native_calls_size > ep->native_calls_alloc)
359 Embryo_Native *calls;
361 ep->native_calls_alloc += 32;
362 calls = realloc(ep->native_calls,
363 ep->native_calls_alloc * sizeof(Embryo_Native));
366 ep->native_calls_size--;
367 ep->native_calls_alloc -= 32;
370 ep->native_calls = calls;
372 ep->native_calls[ep->native_calls_size - 1] = func;
374 func_entry = GETENTRY(hdr, natives, 0);
375 for (i = 0; i < num; i++)
377 if (func_entry->address == 0)
381 entry_name = GETENTRYNAME(hdr, func_entry);
382 if ((entry_name) && (!strcmp(entry_name, name)))
384 func_entry->address = ep->native_calls_size;
385 /* FIXME: embryo_cc is putting in multiple native */
386 /* function call entries - so we need to fill in all */
392 (Embryo_Func_Stub *)((unsigned char *)func_entry + hdr->defsize);
398 embryo_program_vm_reset(Embryo_Program *ep)
402 if ((!ep) || (!ep->base)) return;
403 hdr = (Embryo_Header *)ep->code;
404 memcpy(ep->base, hdr, hdr->size);
405 *(Embryo_Cell *)(ep->base + (int)hdr->stp - sizeof(Embryo_Cell)) = 0;
407 ep->hlw = hdr->hea - hdr->dat; /* stack and heap relative to data segment */
408 ep->stp = hdr->stp - hdr->dat - sizeof(Embryo_Cell);
414 embryo_program_vm_push(Embryo_Program *ep)
422 embryo_program_vm_reset(ep);
425 hdr = (Embryo_Header *)ep->code;
426 ep->base = malloc(hdr->stp);
432 embryo_program_vm_reset(ep);
436 embryo_program_vm_pop(Embryo_Program *ep)
438 if ((!ep) || (!ep->base)) return;
440 if (ep->pushes >= 1) return;
447 embryo_swap_16(unsigned short *v
448 #ifndef WORDS_BIGENDIAN
453 #ifdef WORDS_BIGENDIAN
454 _embryo_byte_swap_16(v);
459 embryo_swap_32(unsigned int *v
460 #ifndef WORDS_BIGENDIAN
465 #ifdef WORDS_BIGENDIAN
466 _embryo_byte_swap_32(v);
471 embryo_program_function_find(Embryo_Program *ep, const char *name)
473 int first, last, mid, result;
474 char pname[sNAMEMAX + 1];
477 if (!ep) return EMBRYO_FUNCTION_NONE;
478 hdr = (Embryo_Header *)ep->code;
479 last = NUMENTRIES(hdr, publics, natives) - 1;
482 while (first <= last)
484 mid = (first + last) / 2;
485 if (_embryo_func_get(ep, mid, pname) == EMBRYO_ERROR_NONE)
486 result = strcmp(pname, name);
488 return EMBRYO_FUNCTION_NONE;
490 if (result > 0) last = mid - 1;
491 else if (result < 0) first = mid + 1;
494 return EMBRYO_FUNCTION_NONE;
499 embryo_program_variable_find(Embryo_Program *ep, const char *name)
501 int first, last, mid, result;
502 char pname[sNAMEMAX + 1];
506 if (!ep) return EMBRYO_CELL_NONE;
507 if (!ep->base) return EMBRYO_CELL_NONE;
508 hdr = (Embryo_Header *)ep->base;
509 last = NUMENTRIES(hdr, pubvars, tags) - 1;
512 while (first <= last)
514 mid = (first + last) / 2;
515 if (_embryo_var_get(ep, mid, pname, &paddr) == EMBRYO_ERROR_NONE)
516 result = strcmp(pname, name);
518 return EMBRYO_CELL_NONE;
520 if (result > 0) last = mid - 1;
521 else if (result < 0) first = mid + 1;
524 return EMBRYO_CELL_NONE;
528 embryo_program_variable_count_get(Embryo_Program *ep)
533 if (!ep->base) return 0;
534 hdr = (Embryo_Header *)ep->base;
535 return NUMENTRIES(hdr, pubvars, tags);
539 embryo_program_variable_get(Embryo_Program *ep, int num)
542 char pname[sNAMEMAX + 1];
544 if (!ep) return EMBRYO_CELL_NONE;
545 if (!ep->base) return EMBRYO_CELL_NONE;
546 if (_embryo_var_get(ep, num, pname, &paddr) == EMBRYO_ERROR_NONE)
548 return EMBRYO_CELL_NONE;
553 embryo_program_error_set(Embryo_Program *ep, Embryo_Error error)
560 embryo_program_error_get(Embryo_Program *ep)
562 if (!ep) return EMBRYO_ERROR_NONE;
568 embryo_program_data_set(Embryo_Program *ep, void *data)
575 embryo_program_data_get(Embryo_Program *ep)
577 if (!ep) return NULL;
582 embryo_error_string_get(Embryo_Error error)
584 const char *messages[] =
586 /* EMBRYO_ERROR_NONE */ "(none)",
587 /* EMBRYO_ERROR_EXIT */ "Forced exit",
588 /* EMBRYO_ERROR_ASSERT */ "Assertion failed",
589 /* EMBRYO_ERROR_STACKERR */ "Stack/heap collision (insufficient stack size)",
590 /* EMBRYO_ERROR_BOUNDS */ "Array index out of bounds",
591 /* EMBRYO_ERROR_MEMACCESS */ "Invalid memory access",
592 /* EMBRYO_ERROR_INVINSTR */ "Invalid instruction",
593 /* EMBRYO_ERROR_STACKLOW */ "Stack underflow",
594 /* EMBRYO_ERROR_HEAPLOW */ "Heap underflow",
595 /* EMBRYO_ERROR_CALLBACK */ "No (valid) native function callback",
596 /* EMBRYO_ERROR_NATIVE */ "Native function failed",
597 /* EMBRYO_ERROR_DIVIDE */ "Divide by zero",
598 /* EMBRYO_ERROR_SLEEP */ "(sleep mode)",
599 /* 13 */ "(reserved)",
600 /* 14 */ "(reserved)",
601 /* 15 */ "(reserved)",
602 /* EMBRYO_ERROR_MEMORY */ "Out of memory",
603 /* EMBRYO_ERROR_FORMAT */ "Invalid/unsupported P-code file format",
604 /* EMBRYO_ERROR_VERSION */ "File is for a newer version of the Embryo_Program",
605 /* EMBRYO_ERROR_NOTFOUND */ "Native/Public function is not found",
606 /* EMBRYO_ERROR_INDEX */ "Invalid index parameter (bad entry point)",
607 /* EMBRYO_ERROR_DEBUG */ "Debugger cannot run",
608 /* EMBRYO_ERROR_INIT */ "Embryo_Program not initialized (or doubly initialized)",
609 /* EMBRYO_ERROR_USERDATA */ "Unable to set user data field (table full)",
610 /* EMBRYO_ERROR_INIT_JIT */ "Cannot initialize the JIT",
611 /* EMBRYO_ERROR_PARAMS */ "Parameter error",
613 if (((int)error < 0) ||
614 ((int)error >= (int)(sizeof(messages) / sizeof(messages[0]))))
615 return (const char *)"(unknown)";
616 return messages[error];
621 embryo_data_string_length_get(Embryo_Program *ep, Embryo_Cell *str_cell)
626 if ((!ep) || (!ep->base)) return 0;
627 hdr = (Embryo_Header *)ep->base;
629 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
630 ((void *)str_cell < (void *)ep->base))
632 for (len = 0; str_cell[len] != 0; len++);
637 embryo_data_string_get(Embryo_Program *ep, Embryo_Cell *str_cell, char *dst)
643 if ((!ep) || (!ep->base))
648 hdr = (Embryo_Header *)ep->base;
650 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
651 ((void *)str_cell < (void *)ep->base))
656 for (i = 0; str_cell[i] != 0; i++)
658 #ifdef WORDS_BIGENDIAN
663 _embryo_byte_swap_32(&tmp);
667 dst[i] = str_cell[i];
674 embryo_data_string_set(Embryo_Program *ep, const char *src, Embryo_Cell *str_cell)
680 if (!ep->base) return;
681 hdr = (Embryo_Header *)ep->base;
683 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
684 ((void *)str_cell < (void *)ep->base))
691 for (i = 0; src[i] != 0; i++)
693 if ((void *)(&(str_cell[i])) >= (void *)(ep->base + hdr->stp)) return;
694 else if ((void *)(&(str_cell[i])) == (void *)(ep->base + hdr->stp - 1))
699 #ifdef WORDS_BIGENDIAN
704 _embryo_byte_swap_32(&tmp);
708 str_cell[i] = src[i];
715 embryo_data_address_get(Embryo_Program *ep, Embryo_Cell addr)
720 if ((!ep) || (!ep->base)) return NULL;
721 hdr = (Embryo_Header *)ep->base;
722 data = ep->base + (int)hdr->dat;
723 if ((addr < 0) || (addr >= hdr->stp)) return NULL;
724 return (Embryo_Cell *)(data + (int)addr);
729 embryo_data_heap_push(Embryo_Program *ep, int cells)
734 if ((!ep) || (!ep->base)) return EMBRYO_CELL_NONE;
735 hdr = (Embryo_Header *)ep->base;
736 if (ep->stk - ep->hea - (cells * sizeof(Embryo_Cell)) < STKMARGIN)
737 return EMBRYO_CELL_NONE;
739 ep->hea += (cells * sizeof(Embryo_Cell));
744 embryo_data_heap_pop(Embryo_Program *ep, Embryo_Cell down_to)
747 if (down_to < 0) down_to = 0;
748 if (ep->hea > down_to) ep->hea = down_to;
753 embryo_program_recursion_get(Embryo_Program *ep)
755 return ep->run_count;
760 #define EMBRYO_EXEC_JUMPTABLE
764 /* jump table optimization - only works for gcc though */
765 #ifdef EMBRYO_EXEC_JUMPTABLE
766 #define SWITCH(x) while (1) { goto *switchtable[x];
767 #define SWITCHEND break; }
768 #define CASE(x) SWITCHTABLE_##x:
771 #define SWITCH(x) switch (x) {
773 #define CASE(x) case x:
778 embryo_program_run(Embryo_Program *ep, Embryo_Function fn)
781 Embryo_Func_Stub *func;
782 unsigned char *code, *data;
783 Embryo_Cell pri, alt, stk, frm, hea, hea_start;
784 Embryo_Cell reset_stk, reset_hea, *cip;
785 Embryo_UCell codesize;
792 #ifdef EMBRYO_EXEC_JUMPTABLE
793 /* we limit the jumptable to 256 elements. why? above we forced "op" to be
794 * a unsigned char - that means 256 max values. we limit opcode overflow
795 * here, so eliminating crashes on table lookups with bad/corrupt bytecode.
796 * no need to atuall do compares, branches etc. the datatype does the work
797 * for us. so that means EXCESS elements are all declared as OP_NONE to
798 * keep them innocuous.
800 static const void *switchtable[256] =
802 &&SWITCHTABLE_EMBRYO_OP_NONE,
803 &&SWITCHTABLE_EMBRYO_OP_LOAD_PRI,
804 &&SWITCHTABLE_EMBRYO_OP_LOAD_ALT,
805 &&SWITCHTABLE_EMBRYO_OP_LOAD_S_PRI,
806 &&SWITCHTABLE_EMBRYO_OP_LOAD_S_ALT,
807 &&SWITCHTABLE_EMBRYO_OP_LREF_PRI,
808 &&SWITCHTABLE_EMBRYO_OP_LREF_ALT,
809 &&SWITCHTABLE_EMBRYO_OP_LREF_S_PRI,
810 &&SWITCHTABLE_EMBRYO_OP_LREF_S_ALT,
811 &&SWITCHTABLE_EMBRYO_OP_LOAD_I,
812 &&SWITCHTABLE_EMBRYO_OP_LODB_I,
813 &&SWITCHTABLE_EMBRYO_OP_CONST_PRI,
814 &&SWITCHTABLE_EMBRYO_OP_CONST_ALT,
815 &&SWITCHTABLE_EMBRYO_OP_ADDR_PRI,
816 &&SWITCHTABLE_EMBRYO_OP_ADDR_ALT,
817 &&SWITCHTABLE_EMBRYO_OP_STOR_PRI,
818 &&SWITCHTABLE_EMBRYO_OP_STOR_ALT,
819 &&SWITCHTABLE_EMBRYO_OP_STOR_S_PRI,
820 &&SWITCHTABLE_EMBRYO_OP_STOR_S_ALT,
821 &&SWITCHTABLE_EMBRYO_OP_SREF_PRI,
822 &&SWITCHTABLE_EMBRYO_OP_SREF_ALT,
823 &&SWITCHTABLE_EMBRYO_OP_SREF_S_PRI,
824 &&SWITCHTABLE_EMBRYO_OP_SREF_S_ALT,
825 &&SWITCHTABLE_EMBRYO_OP_STOR_I,
826 &&SWITCHTABLE_EMBRYO_OP_STRB_I,
827 &&SWITCHTABLE_EMBRYO_OP_LIDX,
828 &&SWITCHTABLE_EMBRYO_OP_LIDX_B,
829 &&SWITCHTABLE_EMBRYO_OP_IDXADDR,
830 &&SWITCHTABLE_EMBRYO_OP_IDXADDR_B,
831 &&SWITCHTABLE_EMBRYO_OP_ALIGN_PRI,
832 &&SWITCHTABLE_EMBRYO_OP_ALIGN_ALT,
833 &&SWITCHTABLE_EMBRYO_OP_LCTRL,
834 &&SWITCHTABLE_EMBRYO_OP_SCTRL,
835 &&SWITCHTABLE_EMBRYO_OP_MOVE_PRI,
836 &&SWITCHTABLE_EMBRYO_OP_MOVE_ALT,
837 &&SWITCHTABLE_EMBRYO_OP_XCHG,
838 &&SWITCHTABLE_EMBRYO_OP_PUSH_PRI,
839 &&SWITCHTABLE_EMBRYO_OP_PUSH_ALT,
840 &&SWITCHTABLE_EMBRYO_OP_PUSH_R,
841 &&SWITCHTABLE_EMBRYO_OP_PUSH_C,
842 &&SWITCHTABLE_EMBRYO_OP_PUSH,
843 &&SWITCHTABLE_EMBRYO_OP_PUSH_S,
844 &&SWITCHTABLE_EMBRYO_OP_POP_PRI,
845 &&SWITCHTABLE_EMBRYO_OP_POP_ALT,
846 &&SWITCHTABLE_EMBRYO_OP_STACK,
847 &&SWITCHTABLE_EMBRYO_OP_HEAP,
848 &&SWITCHTABLE_EMBRYO_OP_PROC,
849 &&SWITCHTABLE_EMBRYO_OP_RET,
850 &&SWITCHTABLE_EMBRYO_OP_RETN,
851 &&SWITCHTABLE_EMBRYO_OP_CALL,
852 &&SWITCHTABLE_EMBRYO_OP_CALL_PRI,
853 &&SWITCHTABLE_EMBRYO_OP_JUMP,
854 &&SWITCHTABLE_EMBRYO_OP_JREL,
855 &&SWITCHTABLE_EMBRYO_OP_JZER,
856 &&SWITCHTABLE_EMBRYO_OP_JNZ,
857 &&SWITCHTABLE_EMBRYO_OP_JEQ,
858 &&SWITCHTABLE_EMBRYO_OP_JNEQ,
859 &&SWITCHTABLE_EMBRYO_OP_JLESS,
860 &&SWITCHTABLE_EMBRYO_OP_JLEQ,
861 &&SWITCHTABLE_EMBRYO_OP_JGRTR,
862 &&SWITCHTABLE_EMBRYO_OP_JGEQ,
863 &&SWITCHTABLE_EMBRYO_OP_JSLESS,
864 &&SWITCHTABLE_EMBRYO_OP_JSLEQ,
865 &&SWITCHTABLE_EMBRYO_OP_JSGRTR,
866 &&SWITCHTABLE_EMBRYO_OP_JSGEQ,
867 &&SWITCHTABLE_EMBRYO_OP_SHL,
868 &&SWITCHTABLE_EMBRYO_OP_SHR,
869 &&SWITCHTABLE_EMBRYO_OP_SSHR,
870 &&SWITCHTABLE_EMBRYO_OP_SHL_C_PRI,
871 &&SWITCHTABLE_EMBRYO_OP_SHL_C_ALT,
872 &&SWITCHTABLE_EMBRYO_OP_SHR_C_PRI,
873 &&SWITCHTABLE_EMBRYO_OP_SHR_C_ALT,
874 &&SWITCHTABLE_EMBRYO_OP_SMUL,
875 &&SWITCHTABLE_EMBRYO_OP_SDIV,
876 &&SWITCHTABLE_EMBRYO_OP_SDIV_ALT,
877 &&SWITCHTABLE_EMBRYO_OP_UMUL,
878 &&SWITCHTABLE_EMBRYO_OP_UDIV,
879 &&SWITCHTABLE_EMBRYO_OP_UDIV_ALT,
880 &&SWITCHTABLE_EMBRYO_OP_ADD,
881 &&SWITCHTABLE_EMBRYO_OP_SUB,
882 &&SWITCHTABLE_EMBRYO_OP_SUB_ALT,
883 &&SWITCHTABLE_EMBRYO_OP_AND,
884 &&SWITCHTABLE_EMBRYO_OP_OR,
885 &&SWITCHTABLE_EMBRYO_OP_XOR,
886 &&SWITCHTABLE_EMBRYO_OP_NOT,
887 &&SWITCHTABLE_EMBRYO_OP_NEG,
888 &&SWITCHTABLE_EMBRYO_OP_INVERT,
889 &&SWITCHTABLE_EMBRYO_OP_ADD_C,
890 &&SWITCHTABLE_EMBRYO_OP_SMUL_C,
891 &&SWITCHTABLE_EMBRYO_OP_ZERO_PRI,
892 &&SWITCHTABLE_EMBRYO_OP_ZERO_ALT,
893 &&SWITCHTABLE_EMBRYO_OP_ZERO,
894 &&SWITCHTABLE_EMBRYO_OP_ZERO_S,
895 &&SWITCHTABLE_EMBRYO_OP_SIGN_PRI,
896 &&SWITCHTABLE_EMBRYO_OP_SIGN_ALT,
897 &&SWITCHTABLE_EMBRYO_OP_EQ,
898 &&SWITCHTABLE_EMBRYO_OP_NEQ,
899 &&SWITCHTABLE_EMBRYO_OP_LESS,
900 &&SWITCHTABLE_EMBRYO_OP_LEQ,
901 &&SWITCHTABLE_EMBRYO_OP_GRTR,
902 &&SWITCHTABLE_EMBRYO_OP_GEQ,
903 &&SWITCHTABLE_EMBRYO_OP_SLESS,
904 &&SWITCHTABLE_EMBRYO_OP_SLEQ,
905 &&SWITCHTABLE_EMBRYO_OP_SGRTR,
906 &&SWITCHTABLE_EMBRYO_OP_SGEQ,
907 &&SWITCHTABLE_EMBRYO_OP_EQ_C_PRI,
908 &&SWITCHTABLE_EMBRYO_OP_EQ_C_ALT,
909 &&SWITCHTABLE_EMBRYO_OP_INC_PRI,
910 &&SWITCHTABLE_EMBRYO_OP_INC_ALT,
911 &&SWITCHTABLE_EMBRYO_OP_INC,
912 &&SWITCHTABLE_EMBRYO_OP_INC_S,
913 &&SWITCHTABLE_EMBRYO_OP_INC_I,
914 &&SWITCHTABLE_EMBRYO_OP_DEC_PRI,
915 &&SWITCHTABLE_EMBRYO_OP_DEC_ALT,
916 &&SWITCHTABLE_EMBRYO_OP_DEC,
917 &&SWITCHTABLE_EMBRYO_OP_DEC_S,
918 &&SWITCHTABLE_EMBRYO_OP_DEC_I,
919 &&SWITCHTABLE_EMBRYO_OP_MOVS,
920 &&SWITCHTABLE_EMBRYO_OP_CMPS,
921 &&SWITCHTABLE_EMBRYO_OP_FILL,
922 &&SWITCHTABLE_EMBRYO_OP_HALT,
923 &&SWITCHTABLE_EMBRYO_OP_BOUNDS,
924 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_PRI,
925 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_C,
926 &&SWITCHTABLE_EMBRYO_OP_FILE,
927 &&SWITCHTABLE_EMBRYO_OP_LINE,
928 &&SWITCHTABLE_EMBRYO_OP_SYMBOL,
929 &&SWITCHTABLE_EMBRYO_OP_SRANGE,
930 &&SWITCHTABLE_EMBRYO_OP_JUMP_PRI,
931 &&SWITCHTABLE_EMBRYO_OP_SWITCH,
932 &&SWITCHTABLE_EMBRYO_OP_CASETBL,
933 &&SWITCHTABLE_EMBRYO_OP_SWAP_PRI,
934 &&SWITCHTABLE_EMBRYO_OP_SWAP_ALT,
935 &&SWITCHTABLE_EMBRYO_OP_PUSHADDR,
936 &&SWITCHTABLE_EMBRYO_OP_NOP,
937 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_D,
938 &&SWITCHTABLE_EMBRYO_OP_SYMTAG,
939 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
940 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
941 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
942 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
943 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
944 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
945 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
946 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
947 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
948 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
949 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
950 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
951 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
952 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
953 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
954 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
955 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
956 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
957 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
958 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
959 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
960 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
961 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
962 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE
965 if (!ep) return EMBRYO_PROGRAM_FAIL;
966 if (!(ep->flags & EMBRYO_FLAG_RELOC))
968 ep->error = EMBRYO_ERROR_INIT;
969 return EMBRYO_PROGRAM_FAIL;
973 ep->error = EMBRYO_ERROR_INIT;
974 return EMBRYO_PROGRAM_FAIL;
976 if (ep->run_count > 0)
978 /* return EMBRYO_PROGRAM_BUSY; */
979 /* FIXME: test C->vm->C->vm recursion more fully */
980 /* it seems to work... just fine!!! - strange! */
983 /* set up the registers */
984 hdr = (Embryo_Header *)ep->base;
985 codesize = (Embryo_UCell)(hdr->dat - hdr->cod);
986 code = ep->base + (int)hdr->cod;
987 data = ep->base + (int)hdr->dat;
988 hea_start = hea = ep->hea;
994 /* get the start address */
995 if (fn == EMBRYO_FUNCTION_MAIN)
999 ep->error = EMBRYO_ERROR_INDEX;
1000 return EMBRYO_PROGRAM_FAIL;
1002 cip = (Embryo_Cell *)(code + (int)hdr->cip);
1004 else if (fn == EMBRYO_FUNCTION_CONT)
1006 /* all registers: pri, alt, frm, cip, hea, stk, reset_stk, reset_hea */
1012 reset_stk = ep->reset_stk;
1013 reset_hea = ep->reset_hea;
1014 cip = (Embryo_Cell *)(code + (int)ep->cip);
1018 ep->error = EMBRYO_ERROR_INDEX;
1019 return EMBRYO_PROGRAM_FAIL;
1023 if (fn >= (Embryo_Cell)NUMENTRIES(hdr, publics, natives))
1025 ep->error = EMBRYO_ERROR_INDEX;
1026 return EMBRYO_PROGRAM_FAIL;
1028 func = GETENTRY(hdr, publics, fn);
1029 cip = (Embryo_Cell *)(code + (int)func->address);
1031 /* check values just copied */
1035 if (fn != EMBRYO_FUNCTION_CONT)
1039 for (i = ep->params_size - 1; i >= 0; i--)
1043 pr = &(ep->params[i]);
1047 Embryo_Cell ep_addr, *addr;
1049 len = strlen(pr->string);
1050 ep_addr = embryo_data_heap_push(ep, len + 1);
1051 if (ep_addr == EMBRYO_CELL_NONE)
1053 ep->error = EMBRYO_ERROR_HEAPLOW;
1054 return EMBRYO_PROGRAM_FAIL;
1056 addr = embryo_data_address_get(ep, ep_addr);
1058 embryo_data_string_set(ep, pr->string, addr);
1061 ep->error = EMBRYO_ERROR_HEAPLOW;
1062 return EMBRYO_PROGRAM_FAIL;
1067 else if (pr->cell_array)
1070 Embryo_Cell ep_addr, *addr;
1072 len = pr->cell_array_size;
1073 ep_addr = embryo_data_heap_push(ep, len + 1);
1074 if (ep_addr == EMBRYO_CELL_NONE)
1076 ep->error = EMBRYO_ERROR_HEAPLOW;
1077 return EMBRYO_PROGRAM_FAIL;
1079 addr = embryo_data_address_get(ep, ep_addr);
1081 memcpy(addr, pr->cell_array,
1082 pr->cell_array_size * sizeof(Embryo_Cell));
1085 ep->error = EMBRYO_ERROR_HEAPLOW;
1086 return EMBRYO_PROGRAM_FAIL;
1089 free(pr->cell_array);
1096 PUSH(ep->params_size * sizeof(Embryo_Cell));
1103 ep->params_size = ep->params_alloc = 0;
1105 /* check stack/heap before starting to run */
1108 /* track recursion depth */
1111 max_run_cycles = ep->max_run_cycles;
1113 for (cycle_count = 0;;)
1115 if (max_run_cycles > 0)
1117 if (cycle_count >= max_run_cycles)
1123 op = (Embryo_Opcode)*cip++;
1125 CASE(EMBRYO_OP_LOAD_PRI);
1127 pri = *(Embryo_Cell *)(data + (int)offs);
1129 CASE(EMBRYO_OP_LOAD_ALT);
1131 alt = *(Embryo_Cell *)(data + (int)offs);
1133 CASE(EMBRYO_OP_LOAD_S_PRI);
1135 pri = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1137 CASE(EMBRYO_OP_LOAD_S_ALT);
1139 alt = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1141 CASE(EMBRYO_OP_LREF_PRI);
1143 offs = *(Embryo_Cell *)(data + (int)offs);
1144 pri = *(Embryo_Cell *)(data + (int)offs);
1146 CASE(EMBRYO_OP_LREF_ALT);
1148 offs = *(Embryo_Cell *)(data + (int)offs);
1149 alt = *(Embryo_Cell *)(data + (int)offs);
1151 CASE(EMBRYO_OP_LREF_S_PRI);
1153 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1154 pri = *(Embryo_Cell *)(data + (int)offs);
1156 CASE(EMBRYO_OP_LREF_S_ALT);
1158 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1159 alt = *(Embryo_Cell *)(data + (int)offs);
1161 CASE(EMBRYO_OP_LOAD_I);
1163 pri = *(Embryo_Cell *)(data + (int)pri);
1165 CASE(EMBRYO_OP_LODB_I);
1171 pri = *(data + (int)pri);
1174 pri = *(unsigned short *)(data + (int)pri);
1177 pri = *(unsigned int *)(data + (int)pri);
1180 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1184 CASE(EMBRYO_OP_CONST_PRI);
1187 CASE(EMBRYO_OP_CONST_ALT);
1190 CASE(EMBRYO_OP_ADDR_PRI);
1194 CASE(EMBRYO_OP_ADDR_ALT);
1198 CASE(EMBRYO_OP_STOR_PRI);
1200 *(Embryo_Cell *)(data + (int)offs) = pri;
1202 CASE(EMBRYO_OP_STOR_ALT);
1204 *(Embryo_Cell *)(data + (int)offs) = alt;
1206 CASE(EMBRYO_OP_STOR_S_PRI);
1208 *(Embryo_Cell *)(data + (int)frm + (int)offs) = pri;
1210 CASE(EMBRYO_OP_STOR_S_ALT);
1212 *(Embryo_Cell *)(data + (int)frm + (int)offs) = alt;
1214 CASE(EMBRYO_OP_SREF_PRI);
1216 offs = *(Embryo_Cell *)(data + (int)offs);
1217 *(Embryo_Cell *)(data + (int)offs) = pri;
1219 CASE(EMBRYO_OP_SREF_ALT);
1221 offs = *(Embryo_Cell *)(data + (int)offs);
1222 *(Embryo_Cell *)(data + (int)offs) = alt;
1224 CASE(EMBRYO_OP_SREF_S_PRI);
1226 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1227 *(Embryo_Cell *)(data + (int)offs) = pri;
1229 CASE(EMBRYO_OP_SREF_S_ALT);
1231 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1232 *(Embryo_Cell *)(data + (int)offs) = alt;
1234 CASE(EMBRYO_OP_STOR_I);
1236 *(Embryo_Cell *)(data + (int)alt) = pri;
1238 CASE(EMBRYO_OP_STRB_I);
1244 *(data + (int)alt) = (unsigned char)pri;
1247 *(unsigned short *)(data + (int)alt) = (unsigned short)pri;
1250 *(unsigned int *)(data + (int)alt) = (unsigned int)pri;
1253 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1257 CASE(EMBRYO_OP_LIDX);
1258 offs = (pri * sizeof(Embryo_Cell)) + alt;
1260 pri = *(Embryo_Cell *)(data + (int)offs);
1262 CASE(EMBRYO_OP_LIDX_B);
1264 offs = (pri << (int)offs) + alt;
1266 pri = *(Embryo_Cell *)(data + (int)offs);
1268 CASE(EMBRYO_OP_IDXADDR);
1269 pri = (pri * sizeof(Embryo_Cell)) + alt;
1271 CASE(EMBRYO_OP_IDXADDR_B);
1273 pri = (pri << (int)offs) + alt;
1275 CASE(EMBRYO_OP_ALIGN_PRI);
1277 #ifdef WORDS_BIGENDIAN
1278 if ((size_t)offs < sizeof(Embryo_Cell))
1279 pri ^= sizeof(Embryo_Cell) - offs;
1282 CASE(EMBRYO_OP_ALIGN_ALT);
1284 #ifdef WORDS_BIGENDIAN
1285 if ((size_t)offs < sizeof(Embryo_Cell))
1286 alt ^= sizeof(Embryo_Cell) - offs;
1289 CASE(EMBRYO_OP_LCTRL);
1312 pri = (Embryo_Cell)((unsigned char *)cip - code);
1315 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1319 CASE(EMBRYO_OP_SCTRL);
1329 /* cannot change these parameters */
1338 cip = (Embryo_Cell *)(code + (int)pri);
1341 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1345 CASE(EMBRYO_OP_MOVE_PRI);
1348 CASE(EMBRYO_OP_MOVE_ALT);
1351 CASE(EMBRYO_OP_XCHG);
1352 offs = pri; /* offs is a temporary variable */
1356 CASE(EMBRYO_OP_PUSH_PRI);
1359 CASE(EMBRYO_OP_PUSH_ALT);
1362 CASE(EMBRYO_OP_PUSH_C);
1366 CASE(EMBRYO_OP_PUSH_R);
1368 while (offs--) PUSH(pri);
1370 CASE(EMBRYO_OP_PUSH);
1372 PUSH(*(Embryo_Cell *)(data + (int)offs));
1374 CASE(EMBRYO_OP_PUSH_S);
1376 PUSH(*(Embryo_Cell *)(data + (int)frm + (int)offs));
1378 CASE(EMBRYO_OP_POP_PRI);
1381 CASE(EMBRYO_OP_POP_ALT);
1384 CASE(EMBRYO_OP_STACK);
1391 CASE(EMBRYO_OP_HEAP);
1398 CASE(EMBRYO_OP_PROC);
1403 CASE(EMBRYO_OP_RET);
1406 if ((Embryo_UCell)offs >= codesize)
1407 ABORT(ep, EMBRYO_ERROR_MEMACCESS);
1408 cip = (Embryo_Cell *)(code + (int)offs);
1410 CASE(EMBRYO_OP_RETN);
1413 if ((Embryo_UCell)offs >= codesize)
1414 ABORT(ep, EMBRYO_ERROR_MEMACCESS);
1415 cip = (Embryo_Cell *)(code + (int)offs);
1416 stk += *(Embryo_Cell *)(data + (int)stk) + sizeof(Embryo_Cell); /* remove parameters from the stack */
1419 CASE(EMBRYO_OP_CALL);
1420 PUSH(((unsigned char *)cip - code) + sizeof(Embryo_Cell));/* skip address */
1421 cip = JUMPABS(code, cip); /* jump to the address */
1423 CASE(EMBRYO_OP_CALL_PRI);
1424 PUSH((unsigned char *)cip - code);
1425 cip = (Embryo_Cell *)(code + (int)pri);
1427 CASE(EMBRYO_OP_JUMP);
1428 /* since the GETPARAM() macro modifies cip, you cannot
1429 * do GETPARAM(cip) directly */
1430 cip = JUMPABS(code, cip);
1432 CASE(EMBRYO_OP_JREL);
1434 cip = (Embryo_Cell *)((unsigned char *)cip + (int)offs + sizeof(Embryo_Cell));
1436 CASE(EMBRYO_OP_JZER);
1438 cip = JUMPABS(code, cip);
1440 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1442 CASE(EMBRYO_OP_JNZ);
1444 cip = JUMPABS(code, cip);
1446 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1448 CASE(EMBRYO_OP_JEQ);
1450 cip = JUMPABS(code, cip);
1452 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1454 CASE(EMBRYO_OP_JNEQ);
1456 cip = JUMPABS(code, cip);
1458 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1460 CASE(EMBRYO_OP_JLESS);
1461 if ((Embryo_UCell)pri < (Embryo_UCell)alt)
1462 cip = JUMPABS(code, cip);
1464 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1466 CASE(EMBRYO_OP_JLEQ);
1467 if ((Embryo_UCell)pri <= (Embryo_UCell)alt)
1468 cip = JUMPABS(code, cip);
1470 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1472 CASE(EMBRYO_OP_JGRTR);
1473 if ((Embryo_UCell)pri > (Embryo_UCell)alt)
1474 cip = JUMPABS(code, cip);
1476 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1478 CASE(EMBRYO_OP_JGEQ);
1479 if ((Embryo_UCell)pri >= (Embryo_UCell)alt)
1480 cip = JUMPABS(code, cip);
1482 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1484 CASE(EMBRYO_OP_JSLESS);
1486 cip = JUMPABS(code, cip);
1488 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1490 CASE(EMBRYO_OP_JSLEQ);
1492 cip = JUMPABS(code, cip);
1494 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1496 CASE(EMBRYO_OP_JSGRTR);
1498 cip = JUMPABS(code, cip);
1500 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1502 CASE(EMBRYO_OP_JSGEQ);
1504 cip = JUMPABS(code, cip);
1506 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1508 CASE(EMBRYO_OP_SHL);
1511 CASE(EMBRYO_OP_SHR);
1512 pri = (Embryo_UCell)pri >> (int)alt;
1514 CASE(EMBRYO_OP_SSHR);
1517 CASE(EMBRYO_OP_SHL_C_PRI);
1521 CASE(EMBRYO_OP_SHL_C_ALT);
1525 CASE(EMBRYO_OP_SHR_C_PRI);
1527 pri = (Embryo_UCell)pri >> (int)offs;
1529 CASE(EMBRYO_OP_SHR_C_ALT);
1531 alt = (Embryo_UCell)alt >> (int)offs;
1533 CASE(EMBRYO_OP_SMUL);
1536 CASE(EMBRYO_OP_SDIV);
1537 if (alt == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1538 /* divide must always round down; this is a bit
1539 * involved to do in a machine-independent way.
1541 offs = ((pri % alt) + alt) % alt; /* true modulus */
1542 pri = (pri - offs) / alt; /* division result */
1545 CASE(EMBRYO_OP_SDIV_ALT);
1546 if (pri == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1547 /* divide must always round down; this is a bit
1548 * involved to do in a machine-independent way.
1550 offs = ((alt % pri) + pri) % pri; /* true modulus */
1551 pri = (alt - offs) / pri; /* division result */
1554 CASE(EMBRYO_OP_UMUL);
1555 pri = (Embryo_UCell)pri * (Embryo_UCell)alt;
1557 CASE(EMBRYO_OP_UDIV);
1558 if (alt == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1559 offs = (Embryo_UCell)pri % (Embryo_UCell)alt; /* temporary storage */
1560 pri = (Embryo_UCell)pri / (Embryo_UCell)alt;
1563 CASE(EMBRYO_OP_UDIV_ALT);
1564 if (pri == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1565 offs = (Embryo_UCell)alt % (Embryo_UCell)pri; /* temporary storage */
1566 pri = (Embryo_UCell)alt / (Embryo_UCell)pri;
1569 CASE(EMBRYO_OP_ADD);
1572 CASE(EMBRYO_OP_SUB);
1575 CASE(EMBRYO_OP_SUB_ALT);
1578 CASE(EMBRYO_OP_AND);
1584 CASE(EMBRYO_OP_XOR);
1587 CASE(EMBRYO_OP_NOT);
1590 CASE(EMBRYO_OP_NEG);
1593 CASE(EMBRYO_OP_INVERT);
1596 CASE(EMBRYO_OP_ADD_C);
1600 CASE(EMBRYO_OP_SMUL_C);
1604 CASE(EMBRYO_OP_ZERO_PRI);
1607 CASE(EMBRYO_OP_ZERO_ALT);
1610 CASE(EMBRYO_OP_ZERO);
1612 *(Embryo_Cell *)(data + (int)offs) = 0;
1614 CASE(EMBRYO_OP_ZERO_S);
1616 *(Embryo_Cell *)(data + (int)frm + (int)offs) = 0;
1618 CASE(EMBRYO_OP_SIGN_PRI);
1619 if ((pri & 0xff) >= 0x80) pri |= ~(Embryo_UCell)0xff;
1621 CASE(EMBRYO_OP_SIGN_ALT);
1622 if ((alt & 0xff) >= 0x80) alt |= ~(Embryo_UCell)0xff;
1625 pri = (pri == alt) ? 1 : 0;
1627 CASE(EMBRYO_OP_NEQ);
1628 pri = (pri != alt) ? 1 : 0;
1630 CASE(EMBRYO_OP_LESS);
1631 pri = ((Embryo_UCell)pri < (Embryo_UCell)alt) ? 1 : 0;
1633 CASE(EMBRYO_OP_LEQ);
1634 pri = ((Embryo_UCell)pri <= (Embryo_UCell)alt) ? 1 : 0;
1636 CASE(EMBRYO_OP_GRTR);
1637 pri = ((Embryo_UCell)pri > (Embryo_UCell)alt) ? 1 : 0;
1639 CASE(EMBRYO_OP_GEQ);
1640 pri = ((Embryo_UCell)pri >= (Embryo_UCell)alt) ? 1 : 0;
1642 CASE(EMBRYO_OP_SLESS);
1643 pri = (pri < alt) ? 1 : 0;
1645 CASE(EMBRYO_OP_SLEQ);
1646 pri = (pri <= alt) ? 1 : 0;
1648 CASE(EMBRYO_OP_SGRTR);
1649 pri = (pri > alt) ? 1 : 0;
1651 CASE(EMBRYO_OP_SGEQ);
1652 pri = (pri >= alt) ? 1 : 0;
1654 CASE(EMBRYO_OP_EQ_C_PRI);
1656 pri = (pri == offs) ? 1 : 0;
1658 CASE(EMBRYO_OP_EQ_C_ALT);
1660 pri = (alt == offs) ? 1 : 0;
1662 CASE(EMBRYO_OP_INC_PRI);
1665 CASE(EMBRYO_OP_INC_ALT);
1668 CASE(EMBRYO_OP_INC);
1670 *(Embryo_Cell *)(data + (int)offs) += 1;
1672 CASE(EMBRYO_OP_INC_S);
1674 *(Embryo_Cell *)(data + (int)frm + (int)offs) += 1;
1676 CASE(EMBRYO_OP_INC_I);
1677 *(Embryo_Cell *)(data + (int)pri) += 1;
1679 CASE(EMBRYO_OP_DEC_PRI);
1682 CASE(EMBRYO_OP_DEC_ALT);
1685 CASE(EMBRYO_OP_DEC);
1687 *(Embryo_Cell *)(data + (int)offs) -= 1;
1689 CASE(EMBRYO_OP_DEC_S);
1691 *(Embryo_Cell *)(data + (int)frm + (int)offs) -= 1;
1693 CASE(EMBRYO_OP_DEC_I);
1694 *(Embryo_Cell *)(data + (int)pri) -= 1;
1696 CASE(EMBRYO_OP_MOVS);
1702 memcpy(data+(int)alt, data+(int)pri, (int)offs);
1704 CASE(EMBRYO_OP_CMPS);
1710 pri = memcmp(data + (int)alt, data + (int)pri, (int)offs);
1712 CASE(EMBRYO_OP_FILL);
1717 (size_t)offs >= sizeof(Embryo_Cell);
1718 i += sizeof(Embryo_Cell), offs -= sizeof(Embryo_Cell))
1719 *(Embryo_Cell *)(data + i) = pri;
1721 CASE(EMBRYO_OP_HALT);
1724 /* store complete status */
1730 ep->cip = (Embryo_Cell)((unsigned char*)cip - code);
1731 if (offs == EMBRYO_ERROR_SLEEP)
1733 ep->reset_stk = reset_stk;
1734 ep->reset_hea = reset_hea;
1736 return EMBRYO_PROGRAM_SLEEP;
1739 CASE(EMBRYO_OP_BOUNDS);
1741 if ((Embryo_UCell)pri > (Embryo_UCell)offs)
1742 ABORT(ep, EMBRYO_ERROR_BOUNDS);
1744 CASE(EMBRYO_OP_SYSREQ_PRI);
1745 /* save a few registers */
1746 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
1750 num = _embryo_native_call(ep, pri, &pri, (Embryo_Cell *)(data + (int)stk));
1751 if (num != EMBRYO_ERROR_NONE)
1753 if (num == EMBRYO_ERROR_SLEEP)
1757 ep->reset_stk = reset_stk;
1758 ep->reset_hea = reset_hea;
1760 return EMBRYO_PROGRAM_SLEEP;
1765 CASE(EMBRYO_OP_SYSREQ_C);
1767 /* save a few registers */
1768 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
1772 num = _embryo_native_call(ep, offs, &pri, (Embryo_Cell *)(data + (int)stk));
1773 if (num != EMBRYO_ERROR_NONE)
1775 if (num == EMBRYO_ERROR_SLEEP)
1779 ep->reset_stk = reset_stk;
1780 ep->reset_hea = reset_hea;
1782 return EMBRYO_PROGRAM_SLEEP;
1787 Embryo_Func_Stub *func_entry;
1789 hdr = (Embryo_Header *)ep->code;
1790 num = NUMENTRIES(hdr, natives, libraries);
1791 func_entry = GETENTRY(hdr, natives, 0);
1792 for (i = 0; i < num; i++)
1796 entry_name = GETENTRYNAME(hdr, func_entry);
1798 printf("EMBRYO: CALL [%i] %s() non-existent!\n", i, entry_name);
1800 (Embryo_Func_Stub *)((unsigned char *)func_entry + hdr->defsize);
1806 CASE(EMBRYO_OP_SYSREQ_D);
1808 /* save a few registers */
1809 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
1813 num = _embryo_native_call(ep, offs, &pri, (Embryo_Cell *)(data + (int)stk));
1814 if (num != EMBRYO_ERROR_NONE)
1816 if (num == EMBRYO_ERROR_SLEEP)
1820 ep->reset_stk = reset_stk;
1821 ep->reset_hea = reset_hea;
1823 return EMBRYO_PROGRAM_SLEEP;
1825 ABORT(ep, ep->error);
1828 CASE(EMBRYO_OP_JUMP_PRI);
1829 cip = (Embryo_Cell *)(code + (int)pri);
1831 CASE(EMBRYO_OP_SWITCH);
1835 /* +1, to skip the "casetbl" opcode */
1836 cptr = (Embryo_Cell *)(code + (*cip)) + 1;
1837 /* number of records in the case table */
1839 /* preset to "none-matched" case */
1840 cip = (Embryo_Cell *)(code + *(cptr + 1));
1842 (num > 0) && (*cptr != pri);
1846 cip = (Embryo_Cell *)(code + *(cptr + 1));
1849 CASE(EMBRYO_OP_SWAP_PRI);
1850 offs = *(Embryo_Cell *)(data + (int)stk);
1851 *(Embryo_Cell *)(data + (int)stk) = pri;
1854 CASE(EMBRYO_OP_SWAP_ALT);
1855 offs = *(Embryo_Cell *)(data + (int)stk);
1856 *(Embryo_Cell *)(data + (int)stk) = alt;
1859 CASE(EMBRYO_OP_PUSHADDR);
1863 CASE(EMBRYO_OP_NOP);
1865 CASE(EMBRYO_OP_NONE);
1866 CASE(EMBRYO_OP_FILE);
1867 CASE(EMBRYO_OP_LINE);
1868 CASE(EMBRYO_OP_SYMBOL);
1869 CASE(EMBRYO_OP_SRANGE);
1870 CASE(EMBRYO_OP_CASETBL);
1871 CASE(EMBRYO_OP_SYMTAG);
1873 #ifndef EMBRYO_EXEC_JUMPTABLE
1875 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1879 ep->max_run_cycles = max_run_cycles;
1881 ep->hea = hea_start;
1882 return EMBRYO_PROGRAM_OK;
1886 embryo_program_return_value_get(Embryo_Program *ep)
1893 embryo_program_max_cycle_run_set(Embryo_Program *ep, int max)
1896 if (max < 0) max = 0;
1897 ep->max_run_cycles = max;
1901 embryo_program_max_cycle_run_get(Embryo_Program *ep)
1904 return ep->max_run_cycles;
1909 embryo_parameter_cell_push(Embryo_Program *ep, Embryo_Cell cell)
1914 if (ep->params_size > ep->params_alloc)
1916 ep->params_alloc += 8;
1917 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
1921 pr = &(ep->params[ep->params_size - 1]);
1923 pr->cell_array = NULL;
1924 pr->cell_array_size = 0;
1931 embryo_parameter_string_push(Embryo_Program *ep, const char *str)
1937 return embryo_parameter_string_push(ep, "");
1938 str_dup = strdup(str);
1939 if (!str_dup) return 0;
1941 if (ep->params_size > ep->params_alloc)
1943 ep->params_alloc += 8;
1944 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
1952 pr = &(ep->params[ep->params_size - 1]);
1954 pr->cell_array = NULL;
1955 pr->cell_array_size = 0;
1957 pr->string = str_dup;
1962 embryo_parameter_cell_array_push(Embryo_Program *ep, Embryo_Cell *cells, int num)
1965 Embryo_Cell *cell_array;
1967 if ((!cells) || (num <= 0))
1968 return embryo_parameter_cell_push(ep, 0);
1969 cell_array = malloc(num * sizeof(Embryo_Cell));
1971 if (ep->params_size > ep->params_alloc)
1973 ep->params_alloc += 8;
1974 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
1982 pr = &(ep->params[ep->params_size - 1]);
1984 pr->cell_array = NULL;
1985 pr->cell_array_size = 0;
1987 pr->cell_array = cell_array;
1988 pr->cell_array_size = num;
1989 memcpy(pr->cell_array, cells, num * sizeof(Embryo_Cell));