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.
37 #include "embryo_private.h"
40 #define JUMPABS(base, ip) ((Embryo_Cell *)(code + (*ip)))
42 #ifdef WORDS_BIGENDIAN
43 static void _embryo_byte_swap_16 (unsigned short *v);
44 static void _embryo_byte_swap_32 (unsigned int *v);
46 static int _embryo_native_call (Embryo_Program *ep, Embryo_Cell idx, Embryo_Cell *result, Embryo_Cell *params);
47 static int _embryo_func_get (Embryo_Program *ep, int idx, char *funcname);
48 static int _embryo_var_get (Embryo_Program *ep, int idx, char *varname, Embryo_Cell *ep_addr);
49 static int _embryo_program_init (Embryo_Program *ep, void *code);
51 #ifdef WORDS_BIGENDIAN
53 _embryo_byte_swap_16(unsigned short *v)
57 s = (unsigned char *)v;
58 t = s[0]; s[0] = s[1]; s[1] = t;
62 _embryo_byte_swap_32(unsigned int *v)
66 s = (unsigned char *)v;
67 t = s[0]; s[0] = s[3]; s[3] = t;
68 t = s[1]; s[1] = s[2]; s[2] = t;
73 _embryo_native_call(Embryo_Program *ep, Embryo_Cell idx, Embryo_Cell *result, Embryo_Cell *params)
76 Embryo_Func_Stub *func_entry;
79 hdr = (Embryo_Header *)ep->base;
80 func_entry = GETENTRY(hdr, natives, idx);
81 if ((func_entry->address <= 0) ||
82 (func_entry->address > ep->native_calls_size))
84 ep->error = EMBRYO_ERROR_CALLBACK;
87 f = ep->native_calls[func_entry->address - 1];
90 ep->error = EMBRYO_ERROR_CALLBACK;
93 ep->error = EMBRYO_ERROR_NONE;
94 *result = f(ep, params);
99 _embryo_func_get(Embryo_Program *ep, int idx, char *funcname)
102 Embryo_Func_Stub *func;
104 hdr = (Embryo_Header *)ep->code;
105 if (idx >= (Embryo_Cell)NUMENTRIES(hdr, publics, natives))
106 return EMBRYO_ERROR_INDEX;
108 func = GETENTRY(hdr, publics, idx);
109 strcpy(funcname, GETENTRYNAME(hdr, func));
110 return EMBRYO_ERROR_NONE;
114 _embryo_var_get(Embryo_Program *ep, int idx, char *varname, Embryo_Cell *ep_addr)
118 Embryo_Func_Stub *var;
120 hdr=(Embryo_Header *)ep->base;
121 if (idx >= (Embryo_Cell)NUMENTRIES(hdr, pubvars, tags))
122 return EMBRYO_ERROR_INDEX;
124 var = GETENTRY(hdr, pubvars, idx);
125 strcpy(varname, GETENTRYNAME(hdr, var));
126 *ep_addr = var->address;
127 return EMBRYO_ERROR_NONE;
131 _embryo_program_init(Embryo_Program *ep, void *code)
135 if ((ep->flags & EMBRYO_FLAG_RELOC)) return 1;
136 ep->code = (unsigned char *)code;
137 hdr = (Embryo_Header *)ep->code;
138 #ifdef WORDS_BIGENDIAN
139 embryo_swap_32((unsigned int *)&hdr->size);
140 embryo_swap_16((unsigned short *)&hdr->magic);
141 embryo_swap_16((unsigned short *)&hdr->flags);
142 embryo_swap_16((unsigned short *)&hdr->defsize);
143 embryo_swap_32((unsigned int *)&hdr->cod);
144 embryo_swap_32((unsigned int *)&hdr->dat);
145 embryo_swap_32((unsigned int *)&hdr->hea);
146 embryo_swap_32((unsigned int *)&hdr->stp);
147 embryo_swap_32((unsigned int *)&hdr->cip);
148 embryo_swap_32((unsigned int *)&hdr->publics);
149 embryo_swap_32((unsigned int *)&hdr->natives);
150 embryo_swap_32((unsigned int *)&hdr->libraries);
151 embryo_swap_32((unsigned int *)&hdr->pubvars);
152 embryo_swap_32((unsigned int *)&hdr->tags);
153 embryo_swap_32((unsigned int *)&hdr->nametable);
156 if (hdr->magic != EMBRYO_MAGIC) return 0;
157 if ((hdr->file_version < MIN_FILE_VERSION) ||
158 (hdr->ep_version > CUR_FILE_VERSION)) return 0;
159 if ((hdr->defsize != sizeof(Embryo_Func_Stub)) &&
160 (hdr->defsize != (2 * sizeof(unsigned int)))) return 0;
161 if (hdr->defsize == (2 * sizeof(unsigned int)))
165 len = (unsigned short*)((unsigned char*)ep->code + hdr->nametable);
166 #ifdef WORDS_BIGENDIAN
167 embryo_swap_16((unsigned short *)len);
169 if (*len > sNAMEMAX) return 0;
171 if (hdr->stp <= 0) return 0;
172 if ((hdr->flags & EMBRYO_FLAG_COMPACT)) return 0;
174 #ifdef WORDS_BIGENDIAN
176 Embryo_Func_Stub *fs;
179 /* also align all addresses in the public function, public variable and */
180 /* public tag tables */
181 fs = GETENTRY(hdr, publics, 0);
182 num = NUMENTRIES(hdr, publics, natives);
183 for (i = 0; i < num; i++)
185 embryo_swap_32(&(fs->address));
186 fs = (Embryo_Func_Stub *)((unsigned char *)fs + hdr->defsize);
189 fs = GETENTRY(hdr, pubvars, 0);
190 num = NUMENTRIES(hdr, pubvars, tags);
191 for (i = 0; i < num; i++)
193 embryo_swap_32(&(fs->address));
194 fs = (Embryo_Func_Stub *)((unsigned char *)fs + hdr->defsize);
197 fs = GETENTRY(hdr, tags, 0);
198 num = NUMENTRIES(hdr, tags, nametable);
199 for (i = 0; i < num; i++)
201 embryo_swap_32(&(fs->address));
202 fs = (Embryo_Func_Stub *)((unsigned char *)fs + hdr->defsize);
206 ep->flags = EMBRYO_FLAG_RELOC;
209 Embryo_Cell cip, code_size, cip_end;
212 code_size = hdr->dat - hdr->cod;
213 code = (Embryo_Cell *)((unsigned char *)ep->code + (int)hdr->cod);
214 cip_end = code_size / sizeof(Embryo_Cell);
215 for (cip = 0; cip < cip_end; cip++)
217 /* move this here - later we probably want something that verifies opcodes
218 * are valid and ok...
220 #ifdef WORDS_BIGENDIAN
221 embryo_swap_32(&(code[cip]));
226 /* init native api for handling floating point - default in embryo */
227 _embryo_args_init(ep);
229 _embryo_rand_init(ep);
230 _embryo_str_init(ep);
231 _embryo_time_init(ep);
235 /*** EXPORTED CALLS ***/
237 EAPI Embryo_Program *
238 embryo_program_new(void *data, int size)
243 if (size < (int)sizeof(Embryo_Header)) return NULL;
245 ep = calloc(1, sizeof(Embryo_Program));
246 if (!ep) return NULL;
248 code_data = malloc(size);
254 memcpy(code_data, data, size);
255 if (_embryo_program_init(ep, code_data)) return ep;
261 EAPI Embryo_Program *
262 embryo_program_const_new(void *data, int size)
266 if (size < (int)sizeof(Embryo_Header)) return NULL;
268 ep = calloc(1, sizeof(Embryo_Program));
269 if (!ep) return NULL;
271 if (_embryo_program_init(ep, data))
273 ep->dont_free_code = 1;
280 EAPI Embryo_Program *
281 embryo_program_load(const char *file)
286 void *program = NULL;
287 int program_size = 0;
289 f = fopen(file, "rb");
291 fseek(f, 0, SEEK_END);
292 program_size = ftell(f);
293 fseek(f, 0L, SEEK_SET);
294 if (program_size < (int)sizeof(Embryo_Header))
299 if (fread(&hdr, sizeof(Embryo_Header), 1, f) != 1)
304 fseek(f, 0L, SEEK_SET);
305 #ifdef WORDS_BIGENDIAN
306 embryo_swap_32((unsigned int *)(&hdr.size));
308 if ((int)hdr.size < program_size) program_size = hdr.size;
309 program = malloc(program_size);
315 if (fread(program, program_size, 1, f) != 1)
321 ep = embryo_program_new(program, program_size);
328 embryo_program_free(Embryo_Program *ep)
332 if (ep->base) free(ep->base);
333 if ((!ep->dont_free_code) && (ep->code)) free(ep->code);
334 if (ep->native_calls) free(ep->native_calls);
335 for (i = 0; i < ep->params_size; i++)
337 if (ep->params[i].string) free(ep->params[i].string);
338 if (ep->params[i].cell_array) free(ep->params[i].cell_array);
340 if (ep->params) free(ep->params);
346 embryo_program_native_call_add(Embryo_Program *ep, const char *name, Embryo_Cell (*func) (Embryo_Program *ep, Embryo_Cell *params))
348 Embryo_Func_Stub *func_entry;
352 if ((!ep ) || (!name) || (!func)) return;
353 if (strlen(name) > sNAMEMAX) return;
355 hdr = (Embryo_Header *)ep->code;
356 if (hdr->defsize < 1) return;
357 num = NUMENTRIES(hdr, natives, libraries);
358 if (num <= 0) return;
360 ep->native_calls_size++;
361 if (ep->native_calls_size > ep->native_calls_alloc)
363 Embryo_Native *calls;
365 ep->native_calls_alloc += 32;
366 calls = realloc(ep->native_calls,
367 ep->native_calls_alloc * sizeof(Embryo_Native));
370 ep->native_calls_size--;
371 ep->native_calls_alloc -= 32;
374 ep->native_calls = calls;
376 ep->native_calls[ep->native_calls_size - 1] = func;
378 func_entry = GETENTRY(hdr, natives, 0);
379 for (i = 0; i < num; i++)
381 if (func_entry->address == 0)
385 entry_name = GETENTRYNAME(hdr, func_entry);
386 if ((entry_name) && (!strcmp(entry_name, name)))
388 func_entry->address = ep->native_calls_size;
389 /* FIXME: embryo_cc is putting in multiple native */
390 /* function call entries - so we need to fill in all */
396 (Embryo_Func_Stub *)((unsigned char *)func_entry + hdr->defsize);
402 embryo_program_vm_reset(Embryo_Program *ep)
406 if ((!ep) || (!ep->base)) return;
407 hdr = (Embryo_Header *)ep->code;
408 memcpy(ep->base, hdr, hdr->size);
409 *(Embryo_Cell *)(ep->base + (int)hdr->stp - sizeof(Embryo_Cell)) = 0;
411 ep->hlw = hdr->hea - hdr->dat; /* stack and heap relative to data segment */
412 ep->stp = hdr->stp - hdr->dat - sizeof(Embryo_Cell);
418 embryo_program_vm_push(Embryo_Program *ep)
426 embryo_program_vm_reset(ep);
429 hdr = (Embryo_Header *)ep->code;
430 ep->base = calloc(1, hdr->stp);
436 embryo_program_vm_reset(ep);
440 embryo_program_vm_pop(Embryo_Program *ep)
442 if ((!ep) || (!ep->base)) return;
444 if (ep->pushes >= 1) return;
451 embryo_swap_16(unsigned short *v
452 #ifndef WORDS_BIGENDIAN
457 #ifdef WORDS_BIGENDIAN
458 _embryo_byte_swap_16(v);
463 embryo_swap_32(unsigned int *v
464 #ifndef WORDS_BIGENDIAN
469 #ifdef WORDS_BIGENDIAN
470 _embryo_byte_swap_32(v);
475 embryo_program_function_find(Embryo_Program *ep, const char *name)
477 int first, last, mid, result;
478 char pname[sNAMEMAX + 1];
481 if (!ep) return EMBRYO_FUNCTION_NONE;
482 hdr = (Embryo_Header *)ep->code;
483 last = NUMENTRIES(hdr, publics, natives) - 1;
486 while (first <= last)
488 mid = (first + last) / 2;
489 if (_embryo_func_get(ep, mid, pname) == EMBRYO_ERROR_NONE)
490 result = strcmp(pname, name);
492 return EMBRYO_FUNCTION_NONE;
494 if (result > 0) last = mid - 1;
495 else if (result < 0) first = mid + 1;
498 return EMBRYO_FUNCTION_NONE;
503 embryo_program_variable_find(Embryo_Program *ep, const char *name)
505 int first, last, mid, result;
506 char pname[sNAMEMAX + 1];
510 if (!ep) return EMBRYO_CELL_NONE;
511 if (!ep->base) return EMBRYO_CELL_NONE;
512 hdr = (Embryo_Header *)ep->base;
513 last = NUMENTRIES(hdr, pubvars, tags) - 1;
516 while (first <= last)
518 mid = (first + last) / 2;
519 if (_embryo_var_get(ep, mid, pname, &paddr) == EMBRYO_ERROR_NONE)
520 result = strcmp(pname, name);
522 return EMBRYO_CELL_NONE;
524 if (result > 0) last = mid - 1;
525 else if (result < 0) first = mid + 1;
528 return EMBRYO_CELL_NONE;
532 embryo_program_variable_count_get(Embryo_Program *ep)
537 if (!ep->base) return 0;
538 hdr = (Embryo_Header *)ep->base;
539 return NUMENTRIES(hdr, pubvars, tags);
543 embryo_program_variable_get(Embryo_Program *ep, int num)
546 char pname[sNAMEMAX + 1];
548 if (!ep) return EMBRYO_CELL_NONE;
549 if (!ep->base) return EMBRYO_CELL_NONE;
550 if (_embryo_var_get(ep, num, pname, &paddr) == EMBRYO_ERROR_NONE)
552 return EMBRYO_CELL_NONE;
557 embryo_program_error_set(Embryo_Program *ep, Embryo_Error error)
564 embryo_program_error_get(Embryo_Program *ep)
566 if (!ep) return EMBRYO_ERROR_NONE;
572 embryo_program_data_set(Embryo_Program *ep, void *data)
579 embryo_program_data_get(Embryo_Program *ep)
581 if (!ep) return NULL;
586 embryo_error_string_get(Embryo_Error error)
588 const char *messages[] =
590 /* EMBRYO_ERROR_NONE */ "(none)",
591 /* EMBRYO_ERROR_EXIT */ "Forced exit",
592 /* EMBRYO_ERROR_ASSERT */ "Assertion failed",
593 /* EMBRYO_ERROR_STACKERR */ "Stack/heap collision (insufficient stack size)",
594 /* EMBRYO_ERROR_BOUNDS */ "Array index out of bounds",
595 /* EMBRYO_ERROR_MEMACCESS */ "Invalid memory access",
596 /* EMBRYO_ERROR_INVINSTR */ "Invalid instruction",
597 /* EMBRYO_ERROR_STACKLOW */ "Stack underflow",
598 /* EMBRYO_ERROR_HEAPLOW */ "Heap underflow",
599 /* EMBRYO_ERROR_CALLBACK */ "No (valid) native function callback",
600 /* EMBRYO_ERROR_NATIVE */ "Native function failed",
601 /* EMBRYO_ERROR_DIVIDE */ "Divide by zero",
602 /* EMBRYO_ERROR_SLEEP */ "(sleep mode)",
603 /* 13 */ "(reserved)",
604 /* 14 */ "(reserved)",
605 /* 15 */ "(reserved)",
606 /* EMBRYO_ERROR_MEMORY */ "Out of memory",
607 /* EMBRYO_ERROR_FORMAT */ "Invalid/unsupported P-code file format",
608 /* EMBRYO_ERROR_VERSION */ "File is for a newer version of the Embryo_Program",
609 /* EMBRYO_ERROR_NOTFOUND */ "Native/Public function is not found",
610 /* EMBRYO_ERROR_INDEX */ "Invalid index parameter (bad entry point)",
611 /* EMBRYO_ERROR_DEBUG */ "Debugger cannot run",
612 /* EMBRYO_ERROR_INIT */ "Embryo_Program not initialized (or doubly initialized)",
613 /* EMBRYO_ERROR_USERDATA */ "Unable to set user data field (table full)",
614 /* EMBRYO_ERROR_INIT_JIT */ "Cannot initialize the JIT",
615 /* EMBRYO_ERROR_PARAMS */ "Parameter error",
617 if (((int)error < 0) ||
618 ((int)error >= (int)(sizeof(messages) / sizeof(messages[0]))))
619 return (const char *)"(unknown)";
620 return messages[error];
625 embryo_data_string_length_get(Embryo_Program *ep, Embryo_Cell *str_cell)
630 if ((!ep) || (!ep->base)) return 0;
631 hdr = (Embryo_Header *)ep->base;
633 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
634 ((void *)str_cell < (void *)ep->base))
636 for (len = 0; str_cell[len] != 0; len++);
641 embryo_data_string_get(Embryo_Program *ep, Embryo_Cell *str_cell, char *dst)
647 if ((!ep) || (!ep->base))
652 hdr = (Embryo_Header *)ep->base;
654 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
655 ((void *)str_cell < (void *)ep->base))
660 for (i = 0; str_cell[i] != 0; i++)
662 #ifdef WORDS_BIGENDIAN
667 _embryo_byte_swap_32(&tmp);
671 dst[i] = str_cell[i];
678 embryo_data_string_set(Embryo_Program *ep, const char *src, Embryo_Cell *str_cell)
684 if (!ep->base) return;
685 hdr = (Embryo_Header *)ep->base;
687 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
688 ((void *)str_cell < (void *)ep->base))
695 for (i = 0; src[i] != 0; i++)
697 if ((void *)(&(str_cell[i])) >= (void *)(ep->base + hdr->stp)) return;
698 else if ((void *)(&(str_cell[i])) == (void *)(ep->base + hdr->stp - 1))
703 #ifdef WORDS_BIGENDIAN
708 _embryo_byte_swap_32(&tmp);
712 str_cell[i] = src[i];
719 embryo_data_address_get(Embryo_Program *ep, Embryo_Cell addr)
724 if ((!ep) || (!ep->base)) return NULL;
725 hdr = (Embryo_Header *)ep->base;
726 data = ep->base + (int)hdr->dat;
727 if ((addr < 0) || (addr >= hdr->stp)) return NULL;
728 return (Embryo_Cell *)(data + (int)addr);
733 embryo_data_heap_push(Embryo_Program *ep, int cells)
738 if ((!ep) || (!ep->base)) return EMBRYO_CELL_NONE;
739 hdr = (Embryo_Header *)ep->base;
740 if (ep->stk - ep->hea - (cells * sizeof(Embryo_Cell)) < STKMARGIN)
741 return EMBRYO_CELL_NONE;
743 ep->hea += (cells * sizeof(Embryo_Cell));
748 embryo_data_heap_pop(Embryo_Program *ep, Embryo_Cell down_to)
751 if (down_to < 0) down_to = 0;
752 if (ep->hea > down_to) ep->hea = down_to;
757 embryo_program_recursion_get(Embryo_Program *ep)
759 return ep->run_count;
764 #define EMBRYO_EXEC_JUMPTABLE
768 /* jump table optimization - only works for gcc though */
769 #ifdef EMBRYO_EXEC_JUMPTABLE
770 #define SWITCH(x) while (1) { goto *switchtable[x];
771 #define SWITCHEND break; }
772 #define CASE(x) SWITCHTABLE_##x:
775 #define SWITCH(x) switch (x) {
777 #define CASE(x) case x:
782 embryo_program_run(Embryo_Program *ep, Embryo_Function fn)
785 Embryo_Func_Stub *func;
786 unsigned char *code, *data;
787 Embryo_Cell pri, alt, stk, frm, hea, hea_start;
788 Embryo_Cell reset_stk, reset_hea, *cip;
789 Embryo_UCell codesize;
796 #ifdef EMBRYO_EXEC_JUMPTABLE
797 /* we limit the jumptable to 256 elements. why? above we forced "op" to be
798 * a unsigned char - that means 256 max values. we limit opcode overflow
799 * here, so eliminating crashes on table lookups with bad/corrupt bytecode.
800 * no need to atuall do compares, branches etc. the datatype does the work
801 * for us. so that means EXCESS elements are all declared as OP_NONE to
802 * keep them innocuous.
804 static const void *switchtable[256] =
806 &&SWITCHTABLE_EMBRYO_OP_NONE,
807 &&SWITCHTABLE_EMBRYO_OP_LOAD_PRI,
808 &&SWITCHTABLE_EMBRYO_OP_LOAD_ALT,
809 &&SWITCHTABLE_EMBRYO_OP_LOAD_S_PRI,
810 &&SWITCHTABLE_EMBRYO_OP_LOAD_S_ALT,
811 &&SWITCHTABLE_EMBRYO_OP_LREF_PRI,
812 &&SWITCHTABLE_EMBRYO_OP_LREF_ALT,
813 &&SWITCHTABLE_EMBRYO_OP_LREF_S_PRI,
814 &&SWITCHTABLE_EMBRYO_OP_LREF_S_ALT,
815 &&SWITCHTABLE_EMBRYO_OP_LOAD_I,
816 &&SWITCHTABLE_EMBRYO_OP_LODB_I,
817 &&SWITCHTABLE_EMBRYO_OP_CONST_PRI,
818 &&SWITCHTABLE_EMBRYO_OP_CONST_ALT,
819 &&SWITCHTABLE_EMBRYO_OP_ADDR_PRI,
820 &&SWITCHTABLE_EMBRYO_OP_ADDR_ALT,
821 &&SWITCHTABLE_EMBRYO_OP_STOR_PRI,
822 &&SWITCHTABLE_EMBRYO_OP_STOR_ALT,
823 &&SWITCHTABLE_EMBRYO_OP_STOR_S_PRI,
824 &&SWITCHTABLE_EMBRYO_OP_STOR_S_ALT,
825 &&SWITCHTABLE_EMBRYO_OP_SREF_PRI,
826 &&SWITCHTABLE_EMBRYO_OP_SREF_ALT,
827 &&SWITCHTABLE_EMBRYO_OP_SREF_S_PRI,
828 &&SWITCHTABLE_EMBRYO_OP_SREF_S_ALT,
829 &&SWITCHTABLE_EMBRYO_OP_STOR_I,
830 &&SWITCHTABLE_EMBRYO_OP_STRB_I,
831 &&SWITCHTABLE_EMBRYO_OP_LIDX,
832 &&SWITCHTABLE_EMBRYO_OP_LIDX_B,
833 &&SWITCHTABLE_EMBRYO_OP_IDXADDR,
834 &&SWITCHTABLE_EMBRYO_OP_IDXADDR_B,
835 &&SWITCHTABLE_EMBRYO_OP_ALIGN_PRI,
836 &&SWITCHTABLE_EMBRYO_OP_ALIGN_ALT,
837 &&SWITCHTABLE_EMBRYO_OP_LCTRL,
838 &&SWITCHTABLE_EMBRYO_OP_SCTRL,
839 &&SWITCHTABLE_EMBRYO_OP_MOVE_PRI,
840 &&SWITCHTABLE_EMBRYO_OP_MOVE_ALT,
841 &&SWITCHTABLE_EMBRYO_OP_XCHG,
842 &&SWITCHTABLE_EMBRYO_OP_PUSH_PRI,
843 &&SWITCHTABLE_EMBRYO_OP_PUSH_ALT,
844 &&SWITCHTABLE_EMBRYO_OP_PUSH_R,
845 &&SWITCHTABLE_EMBRYO_OP_PUSH_C,
846 &&SWITCHTABLE_EMBRYO_OP_PUSH,
847 &&SWITCHTABLE_EMBRYO_OP_PUSH_S,
848 &&SWITCHTABLE_EMBRYO_OP_POP_PRI,
849 &&SWITCHTABLE_EMBRYO_OP_POP_ALT,
850 &&SWITCHTABLE_EMBRYO_OP_STACK,
851 &&SWITCHTABLE_EMBRYO_OP_HEAP,
852 &&SWITCHTABLE_EMBRYO_OP_PROC,
853 &&SWITCHTABLE_EMBRYO_OP_RET,
854 &&SWITCHTABLE_EMBRYO_OP_RETN,
855 &&SWITCHTABLE_EMBRYO_OP_CALL,
856 &&SWITCHTABLE_EMBRYO_OP_CALL_PRI,
857 &&SWITCHTABLE_EMBRYO_OP_JUMP,
858 &&SWITCHTABLE_EMBRYO_OP_JREL,
859 &&SWITCHTABLE_EMBRYO_OP_JZER,
860 &&SWITCHTABLE_EMBRYO_OP_JNZ,
861 &&SWITCHTABLE_EMBRYO_OP_JEQ,
862 &&SWITCHTABLE_EMBRYO_OP_JNEQ,
863 &&SWITCHTABLE_EMBRYO_OP_JLESS,
864 &&SWITCHTABLE_EMBRYO_OP_JLEQ,
865 &&SWITCHTABLE_EMBRYO_OP_JGRTR,
866 &&SWITCHTABLE_EMBRYO_OP_JGEQ,
867 &&SWITCHTABLE_EMBRYO_OP_JSLESS,
868 &&SWITCHTABLE_EMBRYO_OP_JSLEQ,
869 &&SWITCHTABLE_EMBRYO_OP_JSGRTR,
870 &&SWITCHTABLE_EMBRYO_OP_JSGEQ,
871 &&SWITCHTABLE_EMBRYO_OP_SHL,
872 &&SWITCHTABLE_EMBRYO_OP_SHR,
873 &&SWITCHTABLE_EMBRYO_OP_SSHR,
874 &&SWITCHTABLE_EMBRYO_OP_SHL_C_PRI,
875 &&SWITCHTABLE_EMBRYO_OP_SHL_C_ALT,
876 &&SWITCHTABLE_EMBRYO_OP_SHR_C_PRI,
877 &&SWITCHTABLE_EMBRYO_OP_SHR_C_ALT,
878 &&SWITCHTABLE_EMBRYO_OP_SMUL,
879 &&SWITCHTABLE_EMBRYO_OP_SDIV,
880 &&SWITCHTABLE_EMBRYO_OP_SDIV_ALT,
881 &&SWITCHTABLE_EMBRYO_OP_UMUL,
882 &&SWITCHTABLE_EMBRYO_OP_UDIV,
883 &&SWITCHTABLE_EMBRYO_OP_UDIV_ALT,
884 &&SWITCHTABLE_EMBRYO_OP_ADD,
885 &&SWITCHTABLE_EMBRYO_OP_SUB,
886 &&SWITCHTABLE_EMBRYO_OP_SUB_ALT,
887 &&SWITCHTABLE_EMBRYO_OP_AND,
888 &&SWITCHTABLE_EMBRYO_OP_OR,
889 &&SWITCHTABLE_EMBRYO_OP_XOR,
890 &&SWITCHTABLE_EMBRYO_OP_NOT,
891 &&SWITCHTABLE_EMBRYO_OP_NEG,
892 &&SWITCHTABLE_EMBRYO_OP_INVERT,
893 &&SWITCHTABLE_EMBRYO_OP_ADD_C,
894 &&SWITCHTABLE_EMBRYO_OP_SMUL_C,
895 &&SWITCHTABLE_EMBRYO_OP_ZERO_PRI,
896 &&SWITCHTABLE_EMBRYO_OP_ZERO_ALT,
897 &&SWITCHTABLE_EMBRYO_OP_ZERO,
898 &&SWITCHTABLE_EMBRYO_OP_ZERO_S,
899 &&SWITCHTABLE_EMBRYO_OP_SIGN_PRI,
900 &&SWITCHTABLE_EMBRYO_OP_SIGN_ALT,
901 &&SWITCHTABLE_EMBRYO_OP_EQ,
902 &&SWITCHTABLE_EMBRYO_OP_NEQ,
903 &&SWITCHTABLE_EMBRYO_OP_LESS,
904 &&SWITCHTABLE_EMBRYO_OP_LEQ,
905 &&SWITCHTABLE_EMBRYO_OP_GRTR,
906 &&SWITCHTABLE_EMBRYO_OP_GEQ,
907 &&SWITCHTABLE_EMBRYO_OP_SLESS,
908 &&SWITCHTABLE_EMBRYO_OP_SLEQ,
909 &&SWITCHTABLE_EMBRYO_OP_SGRTR,
910 &&SWITCHTABLE_EMBRYO_OP_SGEQ,
911 &&SWITCHTABLE_EMBRYO_OP_EQ_C_PRI,
912 &&SWITCHTABLE_EMBRYO_OP_EQ_C_ALT,
913 &&SWITCHTABLE_EMBRYO_OP_INC_PRI,
914 &&SWITCHTABLE_EMBRYO_OP_INC_ALT,
915 &&SWITCHTABLE_EMBRYO_OP_INC,
916 &&SWITCHTABLE_EMBRYO_OP_INC_S,
917 &&SWITCHTABLE_EMBRYO_OP_INC_I,
918 &&SWITCHTABLE_EMBRYO_OP_DEC_PRI,
919 &&SWITCHTABLE_EMBRYO_OP_DEC_ALT,
920 &&SWITCHTABLE_EMBRYO_OP_DEC,
921 &&SWITCHTABLE_EMBRYO_OP_DEC_S,
922 &&SWITCHTABLE_EMBRYO_OP_DEC_I,
923 &&SWITCHTABLE_EMBRYO_OP_MOVS,
924 &&SWITCHTABLE_EMBRYO_OP_CMPS,
925 &&SWITCHTABLE_EMBRYO_OP_FILL,
926 &&SWITCHTABLE_EMBRYO_OP_HALT,
927 &&SWITCHTABLE_EMBRYO_OP_BOUNDS,
928 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_PRI,
929 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_C,
930 &&SWITCHTABLE_EMBRYO_OP_FILE,
931 &&SWITCHTABLE_EMBRYO_OP_LINE,
932 &&SWITCHTABLE_EMBRYO_OP_SYMBOL,
933 &&SWITCHTABLE_EMBRYO_OP_SRANGE,
934 &&SWITCHTABLE_EMBRYO_OP_JUMP_PRI,
935 &&SWITCHTABLE_EMBRYO_OP_SWITCH,
936 &&SWITCHTABLE_EMBRYO_OP_CASETBL,
937 &&SWITCHTABLE_EMBRYO_OP_SWAP_PRI,
938 &&SWITCHTABLE_EMBRYO_OP_SWAP_ALT,
939 &&SWITCHTABLE_EMBRYO_OP_PUSHADDR,
940 &&SWITCHTABLE_EMBRYO_OP_NOP,
941 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_D,
942 &&SWITCHTABLE_EMBRYO_OP_SYMTAG,
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, &&SWITCHTABLE_EMBRYO_OP_NONE,
963 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
964 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
965 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
966 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE
969 if (!ep) return EMBRYO_PROGRAM_FAIL;
970 if (!(ep->flags & EMBRYO_FLAG_RELOC))
972 ep->error = EMBRYO_ERROR_INIT;
973 return EMBRYO_PROGRAM_FAIL;
977 ep->error = EMBRYO_ERROR_INIT;
978 return EMBRYO_PROGRAM_FAIL;
980 if (ep->run_count > 0)
982 /* return EMBRYO_PROGRAM_BUSY; */
983 /* FIXME: test C->vm->C->vm recursion more fully */
984 /* it seems to work... just fine!!! - strange! */
987 /* set up the registers */
988 hdr = (Embryo_Header *)ep->base;
989 codesize = (Embryo_UCell)(hdr->dat - hdr->cod);
990 code = ep->base + (int)hdr->cod;
991 data = ep->base + (int)hdr->dat;
992 hea_start = hea = ep->hea;
998 /* get the start address */
999 if (fn == EMBRYO_FUNCTION_MAIN)
1003 ep->error = EMBRYO_ERROR_INDEX;
1004 return EMBRYO_PROGRAM_FAIL;
1006 cip = (Embryo_Cell *)(code + (int)hdr->cip);
1008 else if (fn == EMBRYO_FUNCTION_CONT)
1010 /* all registers: pri, alt, frm, cip, hea, stk, reset_stk, reset_hea */
1016 reset_stk = ep->reset_stk;
1017 reset_hea = ep->reset_hea;
1018 cip = (Embryo_Cell *)(code + (int)ep->cip);
1022 ep->error = EMBRYO_ERROR_INDEX;
1023 return EMBRYO_PROGRAM_FAIL;
1027 if (fn >= (Embryo_Cell)NUMENTRIES(hdr, publics, natives))
1029 ep->error = EMBRYO_ERROR_INDEX;
1030 return EMBRYO_PROGRAM_FAIL;
1032 func = GETENTRY(hdr, publics, fn);
1033 cip = (Embryo_Cell *)(code + (int)func->address);
1035 /* check values just copied */
1039 if (fn != EMBRYO_FUNCTION_CONT)
1043 for (i = ep->params_size - 1; i >= 0; i--)
1047 pr = &(ep->params[i]);
1051 Embryo_Cell ep_addr, *addr;
1053 len = strlen(pr->string);
1054 ep_addr = embryo_data_heap_push(ep, len + 1);
1055 if (ep_addr == EMBRYO_CELL_NONE)
1057 ep->error = EMBRYO_ERROR_HEAPLOW;
1058 return EMBRYO_PROGRAM_FAIL;
1060 addr = embryo_data_address_get(ep, ep_addr);
1062 embryo_data_string_set(ep, pr->string, addr);
1065 ep->error = EMBRYO_ERROR_HEAPLOW;
1066 return EMBRYO_PROGRAM_FAIL;
1071 else if (pr->cell_array)
1074 Embryo_Cell ep_addr, *addr;
1076 len = pr->cell_array_size;
1077 ep_addr = embryo_data_heap_push(ep, len + 1);
1078 if (ep_addr == EMBRYO_CELL_NONE)
1080 ep->error = EMBRYO_ERROR_HEAPLOW;
1081 return EMBRYO_PROGRAM_FAIL;
1083 addr = embryo_data_address_get(ep, ep_addr);
1085 memcpy(addr, pr->cell_array,
1086 pr->cell_array_size * sizeof(Embryo_Cell));
1089 ep->error = EMBRYO_ERROR_HEAPLOW;
1090 return EMBRYO_PROGRAM_FAIL;
1093 free(pr->cell_array);
1100 PUSH(ep->params_size * sizeof(Embryo_Cell));
1107 ep->params_size = ep->params_alloc = 0;
1109 /* check stack/heap before starting to run */
1112 /* track recursion depth */
1115 max_run_cycles = ep->max_run_cycles;
1117 for (cycle_count = 0;;)
1119 if (max_run_cycles > 0)
1121 if (cycle_count >= max_run_cycles)
1127 op = (Embryo_Opcode)*cip++;
1129 CASE(EMBRYO_OP_LOAD_PRI);
1131 pri = *(Embryo_Cell *)(data + (int)offs);
1133 CASE(EMBRYO_OP_LOAD_ALT);
1135 alt = *(Embryo_Cell *)(data + (int)offs);
1137 CASE(EMBRYO_OP_LOAD_S_PRI);
1139 pri = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1141 CASE(EMBRYO_OP_LOAD_S_ALT);
1143 alt = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1145 CASE(EMBRYO_OP_LREF_PRI);
1147 offs = *(Embryo_Cell *)(data + (int)offs);
1148 pri = *(Embryo_Cell *)(data + (int)offs);
1150 CASE(EMBRYO_OP_LREF_ALT);
1152 offs = *(Embryo_Cell *)(data + (int)offs);
1153 alt = *(Embryo_Cell *)(data + (int)offs);
1155 CASE(EMBRYO_OP_LREF_S_PRI);
1157 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1158 pri = *(Embryo_Cell *)(data + (int)offs);
1160 CASE(EMBRYO_OP_LREF_S_ALT);
1162 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1163 alt = *(Embryo_Cell *)(data + (int)offs);
1165 CASE(EMBRYO_OP_LOAD_I);
1167 pri = *(Embryo_Cell *)(data + (int)pri);
1169 CASE(EMBRYO_OP_LODB_I);
1175 pri = *(data + (int)pri);
1178 pri = *(unsigned short *)(data + (int)pri);
1181 pri = *(unsigned int *)(data + (int)pri);
1184 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1188 CASE(EMBRYO_OP_CONST_PRI);
1191 CASE(EMBRYO_OP_CONST_ALT);
1194 CASE(EMBRYO_OP_ADDR_PRI);
1198 CASE(EMBRYO_OP_ADDR_ALT);
1202 CASE(EMBRYO_OP_STOR_PRI);
1204 *(Embryo_Cell *)(data + (int)offs) = pri;
1206 CASE(EMBRYO_OP_STOR_ALT);
1208 *(Embryo_Cell *)(data + (int)offs) = alt;
1210 CASE(EMBRYO_OP_STOR_S_PRI);
1212 *(Embryo_Cell *)(data + (int)frm + (int)offs) = pri;
1214 CASE(EMBRYO_OP_STOR_S_ALT);
1216 *(Embryo_Cell *)(data + (int)frm + (int)offs) = alt;
1218 CASE(EMBRYO_OP_SREF_PRI);
1220 offs = *(Embryo_Cell *)(data + (int)offs);
1221 *(Embryo_Cell *)(data + (int)offs) = pri;
1223 CASE(EMBRYO_OP_SREF_ALT);
1225 offs = *(Embryo_Cell *)(data + (int)offs);
1226 *(Embryo_Cell *)(data + (int)offs) = alt;
1228 CASE(EMBRYO_OP_SREF_S_PRI);
1230 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1231 *(Embryo_Cell *)(data + (int)offs) = pri;
1233 CASE(EMBRYO_OP_SREF_S_ALT);
1235 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1236 *(Embryo_Cell *)(data + (int)offs) = alt;
1238 CASE(EMBRYO_OP_STOR_I);
1240 *(Embryo_Cell *)(data + (int)alt) = pri;
1242 CASE(EMBRYO_OP_STRB_I);
1248 *(data + (int)alt) = (unsigned char)pri;
1251 *(unsigned short *)(data + (int)alt) = (unsigned short)pri;
1254 *(unsigned int *)(data + (int)alt) = (unsigned int)pri;
1257 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1261 CASE(EMBRYO_OP_LIDX);
1262 offs = (pri * sizeof(Embryo_Cell)) + alt;
1264 pri = *(Embryo_Cell *)(data + (int)offs);
1266 CASE(EMBRYO_OP_LIDX_B);
1268 offs = (pri << (int)offs) + alt;
1270 pri = *(Embryo_Cell *)(data + (int)offs);
1272 CASE(EMBRYO_OP_IDXADDR);
1273 pri = (pri * sizeof(Embryo_Cell)) + alt;
1275 CASE(EMBRYO_OP_IDXADDR_B);
1277 pri = (pri << (int)offs) + alt;
1279 CASE(EMBRYO_OP_ALIGN_PRI);
1281 #ifdef WORDS_BIGENDIAN
1282 if ((size_t)offs < sizeof(Embryo_Cell))
1283 pri ^= sizeof(Embryo_Cell) - offs;
1286 CASE(EMBRYO_OP_ALIGN_ALT);
1288 #ifdef WORDS_BIGENDIAN
1289 if ((size_t)offs < sizeof(Embryo_Cell))
1290 alt ^= sizeof(Embryo_Cell) - offs;
1293 CASE(EMBRYO_OP_LCTRL);
1316 pri = (Embryo_Cell)((unsigned char *)cip - code);
1319 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1323 CASE(EMBRYO_OP_SCTRL);
1333 /* cannot change these parameters */
1342 cip = (Embryo_Cell *)(code + (int)pri);
1345 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1349 CASE(EMBRYO_OP_MOVE_PRI);
1352 CASE(EMBRYO_OP_MOVE_ALT);
1355 CASE(EMBRYO_OP_XCHG);
1356 offs = pri; /* offs is a temporary variable */
1360 CASE(EMBRYO_OP_PUSH_PRI);
1363 CASE(EMBRYO_OP_PUSH_ALT);
1366 CASE(EMBRYO_OP_PUSH_C);
1370 CASE(EMBRYO_OP_PUSH_R);
1372 while (offs--) PUSH(pri);
1374 CASE(EMBRYO_OP_PUSH);
1376 PUSH(*(Embryo_Cell *)(data + (int)offs));
1378 CASE(EMBRYO_OP_PUSH_S);
1380 PUSH(*(Embryo_Cell *)(data + (int)frm + (int)offs));
1382 CASE(EMBRYO_OP_POP_PRI);
1385 CASE(EMBRYO_OP_POP_ALT);
1388 CASE(EMBRYO_OP_STACK);
1395 CASE(EMBRYO_OP_HEAP);
1402 CASE(EMBRYO_OP_PROC);
1407 CASE(EMBRYO_OP_RET);
1410 if ((Embryo_UCell)offs >= codesize)
1411 ABORT(ep, EMBRYO_ERROR_MEMACCESS);
1412 cip = (Embryo_Cell *)(code + (int)offs);
1414 CASE(EMBRYO_OP_RETN);
1417 if ((Embryo_UCell)offs >= codesize)
1418 ABORT(ep, EMBRYO_ERROR_MEMACCESS);
1419 cip = (Embryo_Cell *)(code + (int)offs);
1420 stk += *(Embryo_Cell *)(data + (int)stk) + sizeof(Embryo_Cell); /* remove parameters from the stack */
1423 CASE(EMBRYO_OP_CALL);
1424 PUSH(((unsigned char *)cip - code) + sizeof(Embryo_Cell));/* skip address */
1425 cip = JUMPABS(code, cip); /* jump to the address */
1427 CASE(EMBRYO_OP_CALL_PRI);
1428 PUSH((unsigned char *)cip - code);
1429 cip = (Embryo_Cell *)(code + (int)pri);
1431 CASE(EMBRYO_OP_JUMP);
1432 /* since the GETPARAM() macro modifies cip, you cannot
1433 * do GETPARAM(cip) directly */
1434 cip = JUMPABS(code, cip);
1436 CASE(EMBRYO_OP_JREL);
1438 cip = (Embryo_Cell *)((unsigned char *)cip + (int)offs + sizeof(Embryo_Cell));
1440 CASE(EMBRYO_OP_JZER);
1442 cip = JUMPABS(code, cip);
1444 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1446 CASE(EMBRYO_OP_JNZ);
1448 cip = JUMPABS(code, cip);
1450 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1452 CASE(EMBRYO_OP_JEQ);
1454 cip = JUMPABS(code, cip);
1456 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1458 CASE(EMBRYO_OP_JNEQ);
1460 cip = JUMPABS(code, cip);
1462 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1464 CASE(EMBRYO_OP_JLESS);
1465 if ((Embryo_UCell)pri < (Embryo_UCell)alt)
1466 cip = JUMPABS(code, cip);
1468 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1470 CASE(EMBRYO_OP_JLEQ);
1471 if ((Embryo_UCell)pri <= (Embryo_UCell)alt)
1472 cip = JUMPABS(code, cip);
1474 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1476 CASE(EMBRYO_OP_JGRTR);
1477 if ((Embryo_UCell)pri > (Embryo_UCell)alt)
1478 cip = JUMPABS(code, cip);
1480 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1482 CASE(EMBRYO_OP_JGEQ);
1483 if ((Embryo_UCell)pri >= (Embryo_UCell)alt)
1484 cip = JUMPABS(code, cip);
1486 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1488 CASE(EMBRYO_OP_JSLESS);
1490 cip = JUMPABS(code, cip);
1492 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1494 CASE(EMBRYO_OP_JSLEQ);
1496 cip = JUMPABS(code, cip);
1498 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1500 CASE(EMBRYO_OP_JSGRTR);
1502 cip = JUMPABS(code, cip);
1504 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1506 CASE(EMBRYO_OP_JSGEQ);
1508 cip = JUMPABS(code, cip);
1510 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1512 CASE(EMBRYO_OP_SHL);
1515 CASE(EMBRYO_OP_SHR);
1516 pri = (Embryo_UCell)pri >> (int)alt;
1518 CASE(EMBRYO_OP_SSHR);
1521 CASE(EMBRYO_OP_SHL_C_PRI);
1525 CASE(EMBRYO_OP_SHL_C_ALT);
1529 CASE(EMBRYO_OP_SHR_C_PRI);
1531 pri = (Embryo_UCell)pri >> (int)offs;
1533 CASE(EMBRYO_OP_SHR_C_ALT);
1535 alt = (Embryo_UCell)alt >> (int)offs;
1537 CASE(EMBRYO_OP_SMUL);
1540 CASE(EMBRYO_OP_SDIV);
1541 if (alt == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1542 /* divide must always round down; this is a bit
1543 * involved to do in a machine-independent way.
1545 offs = ((pri % alt) + alt) % alt; /* true modulus */
1546 pri = (pri - offs) / alt; /* division result */
1549 CASE(EMBRYO_OP_SDIV_ALT);
1550 if (pri == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1551 /* divide must always round down; this is a bit
1552 * involved to do in a machine-independent way.
1554 offs = ((alt % pri) + pri) % pri; /* true modulus */
1555 pri = (alt - offs) / pri; /* division result */
1558 CASE(EMBRYO_OP_UMUL);
1559 pri = (Embryo_UCell)pri * (Embryo_UCell)alt;
1561 CASE(EMBRYO_OP_UDIV);
1562 if (alt == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1563 offs = (Embryo_UCell)pri % (Embryo_UCell)alt; /* temporary storage */
1564 pri = (Embryo_UCell)pri / (Embryo_UCell)alt;
1567 CASE(EMBRYO_OP_UDIV_ALT);
1568 if (pri == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1569 offs = (Embryo_UCell)alt % (Embryo_UCell)pri; /* temporary storage */
1570 pri = (Embryo_UCell)alt / (Embryo_UCell)pri;
1573 CASE(EMBRYO_OP_ADD);
1576 CASE(EMBRYO_OP_SUB);
1579 CASE(EMBRYO_OP_SUB_ALT);
1582 CASE(EMBRYO_OP_AND);
1588 CASE(EMBRYO_OP_XOR);
1591 CASE(EMBRYO_OP_NOT);
1594 CASE(EMBRYO_OP_NEG);
1597 CASE(EMBRYO_OP_INVERT);
1600 CASE(EMBRYO_OP_ADD_C);
1604 CASE(EMBRYO_OP_SMUL_C);
1608 CASE(EMBRYO_OP_ZERO_PRI);
1611 CASE(EMBRYO_OP_ZERO_ALT);
1614 CASE(EMBRYO_OP_ZERO);
1616 *(Embryo_Cell *)(data + (int)offs) = 0;
1618 CASE(EMBRYO_OP_ZERO_S);
1620 *(Embryo_Cell *)(data + (int)frm + (int)offs) = 0;
1622 CASE(EMBRYO_OP_SIGN_PRI);
1623 if ((pri & 0xff) >= 0x80) pri |= ~(Embryo_UCell)0xff;
1625 CASE(EMBRYO_OP_SIGN_ALT);
1626 if ((alt & 0xff) >= 0x80) alt |= ~(Embryo_UCell)0xff;
1629 pri = (pri == alt) ? 1 : 0;
1631 CASE(EMBRYO_OP_NEQ);
1632 pri = (pri != alt) ? 1 : 0;
1634 CASE(EMBRYO_OP_LESS);
1635 pri = ((Embryo_UCell)pri < (Embryo_UCell)alt) ? 1 : 0;
1637 CASE(EMBRYO_OP_LEQ);
1638 pri = ((Embryo_UCell)pri <= (Embryo_UCell)alt) ? 1 : 0;
1640 CASE(EMBRYO_OP_GRTR);
1641 pri = ((Embryo_UCell)pri > (Embryo_UCell)alt) ? 1 : 0;
1643 CASE(EMBRYO_OP_GEQ);
1644 pri = ((Embryo_UCell)pri >= (Embryo_UCell)alt) ? 1 : 0;
1646 CASE(EMBRYO_OP_SLESS);
1647 pri = (pri < alt) ? 1 : 0;
1649 CASE(EMBRYO_OP_SLEQ);
1650 pri = (pri <= alt) ? 1 : 0;
1652 CASE(EMBRYO_OP_SGRTR);
1653 pri = (pri > alt) ? 1 : 0;
1655 CASE(EMBRYO_OP_SGEQ);
1656 pri = (pri >= alt) ? 1 : 0;
1658 CASE(EMBRYO_OP_EQ_C_PRI);
1660 pri = (pri == offs) ? 1 : 0;
1662 CASE(EMBRYO_OP_EQ_C_ALT);
1664 pri = (alt == offs) ? 1 : 0;
1666 CASE(EMBRYO_OP_INC_PRI);
1669 CASE(EMBRYO_OP_INC_ALT);
1672 CASE(EMBRYO_OP_INC);
1674 *(Embryo_Cell *)(data + (int)offs) += 1;
1676 CASE(EMBRYO_OP_INC_S);
1678 *(Embryo_Cell *)(data + (int)frm + (int)offs) += 1;
1680 CASE(EMBRYO_OP_INC_I);
1681 *(Embryo_Cell *)(data + (int)pri) += 1;
1683 CASE(EMBRYO_OP_DEC_PRI);
1686 CASE(EMBRYO_OP_DEC_ALT);
1689 CASE(EMBRYO_OP_DEC);
1691 *(Embryo_Cell *)(data + (int)offs) -= 1;
1693 CASE(EMBRYO_OP_DEC_S);
1695 *(Embryo_Cell *)(data + (int)frm + (int)offs) -= 1;
1697 CASE(EMBRYO_OP_DEC_I);
1698 *(Embryo_Cell *)(data + (int)pri) -= 1;
1700 CASE(EMBRYO_OP_MOVS);
1706 memcpy(data+(int)alt, data+(int)pri, (int)offs);
1708 CASE(EMBRYO_OP_CMPS);
1714 pri = memcmp(data + (int)alt, data + (int)pri, (int)offs);
1716 CASE(EMBRYO_OP_FILL);
1721 (size_t)offs >= sizeof(Embryo_Cell);
1722 i += sizeof(Embryo_Cell), offs -= sizeof(Embryo_Cell))
1723 *(Embryo_Cell *)(data + i) = pri;
1725 CASE(EMBRYO_OP_HALT);
1728 /* store complete status */
1734 ep->cip = (Embryo_Cell)((unsigned char*)cip - code);
1735 if (offs == EMBRYO_ERROR_SLEEP)
1737 ep->reset_stk = reset_stk;
1738 ep->reset_hea = reset_hea;
1740 return EMBRYO_PROGRAM_SLEEP;
1743 CASE(EMBRYO_OP_BOUNDS);
1745 if ((Embryo_UCell)pri > (Embryo_UCell)offs)
1746 ABORT(ep, EMBRYO_ERROR_BOUNDS);
1748 CASE(EMBRYO_OP_SYSREQ_PRI);
1749 /* save a few registers */
1750 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
1754 num = _embryo_native_call(ep, pri, &pri, (Embryo_Cell *)(data + (int)stk));
1755 if (num != EMBRYO_ERROR_NONE)
1757 if (num == EMBRYO_ERROR_SLEEP)
1761 ep->reset_stk = reset_stk;
1762 ep->reset_hea = reset_hea;
1764 return EMBRYO_PROGRAM_SLEEP;
1769 CASE(EMBRYO_OP_SYSREQ_C);
1771 /* save a few registers */
1772 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
1776 num = _embryo_native_call(ep, offs, &pri, (Embryo_Cell *)(data + (int)stk));
1777 if (num != EMBRYO_ERROR_NONE)
1779 if (num == EMBRYO_ERROR_SLEEP)
1783 ep->reset_stk = reset_stk;
1784 ep->reset_hea = reset_hea;
1786 return EMBRYO_PROGRAM_SLEEP;
1791 Embryo_Func_Stub *func_entry;
1793 hdr = (Embryo_Header *)ep->code;
1794 num = NUMENTRIES(hdr, natives, libraries);
1795 func_entry = GETENTRY(hdr, natives, 0);
1796 for (i = 0; i < num; i++)
1800 entry_name = GETENTRYNAME(hdr, func_entry);
1802 printf("EMBRYO: CALL [%i] %s() non-existent!\n", i, entry_name);
1804 (Embryo_Func_Stub *)((unsigned char *)func_entry + hdr->defsize);
1810 CASE(EMBRYO_OP_SYSREQ_D);
1812 /* save a few registers */
1813 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
1817 num = _embryo_native_call(ep, offs, &pri, (Embryo_Cell *)(data + (int)stk));
1818 if (num != EMBRYO_ERROR_NONE)
1820 if (num == EMBRYO_ERROR_SLEEP)
1824 ep->reset_stk = reset_stk;
1825 ep->reset_hea = reset_hea;
1827 return EMBRYO_PROGRAM_SLEEP;
1829 ABORT(ep, ep->error);
1832 CASE(EMBRYO_OP_JUMP_PRI);
1833 cip = (Embryo_Cell *)(code + (int)pri);
1835 CASE(EMBRYO_OP_SWITCH);
1839 /* +1, to skip the "casetbl" opcode */
1840 cptr = (Embryo_Cell *)(code + (*cip)) + 1;
1841 /* number of records in the case table */
1843 /* preset to "none-matched" case */
1844 cip = (Embryo_Cell *)(code + *(cptr + 1));
1846 (num > 0) && (*cptr != pri);
1850 cip = (Embryo_Cell *)(code + *(cptr + 1));
1853 CASE(EMBRYO_OP_SWAP_PRI);
1854 offs = *(Embryo_Cell *)(data + (int)stk);
1855 *(Embryo_Cell *)(data + (int)stk) = pri;
1858 CASE(EMBRYO_OP_SWAP_ALT);
1859 offs = *(Embryo_Cell *)(data + (int)stk);
1860 *(Embryo_Cell *)(data + (int)stk) = alt;
1863 CASE(EMBRYO_OP_PUSHADDR);
1867 CASE(EMBRYO_OP_NOP);
1869 CASE(EMBRYO_OP_NONE);
1870 CASE(EMBRYO_OP_FILE);
1871 CASE(EMBRYO_OP_LINE);
1872 CASE(EMBRYO_OP_SYMBOL);
1873 CASE(EMBRYO_OP_SRANGE);
1874 CASE(EMBRYO_OP_CASETBL);
1875 CASE(EMBRYO_OP_SYMTAG);
1877 #ifndef EMBRYO_EXEC_JUMPTABLE
1879 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1883 ep->max_run_cycles = max_run_cycles;
1885 ep->hea = hea_start;
1886 return EMBRYO_PROGRAM_OK;
1890 embryo_program_return_value_get(Embryo_Program *ep)
1897 embryo_program_max_cycle_run_set(Embryo_Program *ep, int max)
1900 if (max < 0) max = 0;
1901 ep->max_run_cycles = max;
1905 embryo_program_max_cycle_run_get(Embryo_Program *ep)
1908 return ep->max_run_cycles;
1913 embryo_parameter_cell_push(Embryo_Program *ep, Embryo_Cell cell)
1918 if (ep->params_size > ep->params_alloc)
1920 ep->params_alloc += 8;
1921 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
1925 pr = &(ep->params[ep->params_size - 1]);
1927 pr->cell_array = NULL;
1928 pr->cell_array_size = 0;
1935 embryo_parameter_string_push(Embryo_Program *ep, const char *str)
1941 return embryo_parameter_string_push(ep, "");
1942 str_dup = strdup(str);
1943 if (!str_dup) return 0;
1945 if (ep->params_size > ep->params_alloc)
1947 ep->params_alloc += 8;
1948 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
1956 pr = &(ep->params[ep->params_size - 1]);
1958 pr->cell_array = NULL;
1959 pr->cell_array_size = 0;
1961 pr->string = str_dup;
1966 embryo_parameter_cell_array_push(Embryo_Program *ep, Embryo_Cell *cells, int num)
1969 Embryo_Cell *cell_array;
1971 if ((!cells) || (num <= 0))
1972 return embryo_parameter_cell_push(ep, 0);
1973 cell_array = malloc(num * sizeof(Embryo_Cell));
1975 if (ep->params_size > ep->params_alloc)
1977 ep->params_alloc += 8;
1978 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
1986 pr = &(ep->params[ep->params_size - 1]);
1988 pr->cell_array = NULL;
1989 pr->cell_array_size = 0;
1991 pr->cell_array = cell_array;
1992 pr->cell_array_size = num;
1993 memcpy(pr->cell_array, cells, num * sizeof(Embryo_Cell));