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 index, Embryo_Cell *result, Embryo_Cell *params);
47 static int _embryo_func_get (Embryo_Program *ep, int index, char *funcname);
48 static int _embryo_var_get (Embryo_Program *ep, int index, 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 index, Embryo_Cell *result, Embryo_Cell *params)
76 Embryo_Func_Stub *func_entry;
79 hdr = (Embryo_Header *)ep->base;
80 func_entry = GETENTRY(hdr, natives, index);
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 index, char *funcname)
102 Embryo_Func_Stub *func;
104 hdr = (Embryo_Header *)ep->code;
105 if (index >= (Embryo_Cell)NUMENTRIES(hdr, publics, natives))
106 return EMBRYO_ERROR_INDEX;
108 func = GETENTRY(hdr, publics, index);
109 strcpy(funcname, GETENTRYNAME(hdr, func));
110 return EMBRYO_ERROR_NONE;
114 _embryo_var_get(Embryo_Program *ep, int index, char *varname, Embryo_Cell *ep_addr)
118 Embryo_Func_Stub *var;
120 hdr=(Embryo_Header *)ep->base;
121 if (index >= (Embryo_Cell)NUMENTRIES(hdr, pubvars, tags))
122 return EMBRYO_ERROR_INDEX;
124 var = GETENTRY(hdr, pubvars, index);
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 ***/
238 * @defgroup Embryo_Program_Creation_Group Program Creation and Destruction Functions
240 * Functions that set up programs, and destroy them.
244 * Creates a new Embryo program, with bytecode data that can be freed.
245 * @param data Pointer to the bytecode of the program.
246 * @param size Number of bytes of bytecode.
247 * @return A new Embryo program.
248 * @ingroup Embryo_Program_Creation_Group
250 EAPI Embryo_Program *
251 embryo_program_new(void *data, int size)
256 if (size < (int)sizeof(Embryo_Header)) return NULL;
258 ep = calloc(1, sizeof(Embryo_Program));
259 if (!ep) return NULL;
261 code_data = malloc(size);
267 memcpy(code_data, data, size);
268 if (_embryo_program_init(ep, code_data)) return ep;
275 * Creates a new Embryo program, with bytecode data that cannot be
277 * @param data Pointer to the bytecode of the program.
278 * @param size Number of bytes of bytecode.
279 * @return A new Embryo program.
280 * @ingroup Embryo_Program_Creation_Group
282 EAPI Embryo_Program *
283 embryo_program_const_new(void *data, int size)
287 if (size < (int)sizeof(Embryo_Header)) return NULL;
289 ep = calloc(1, sizeof(Embryo_Program));
290 if (!ep) return NULL;
292 if (_embryo_program_init(ep, data))
294 ep->dont_free_code = 1;
302 * Creates a new Embryo program based on the bytecode data stored in the
304 * @param file Filename of the given file.
305 * @return A new Embryo program.
306 * @ingroup Embryo_Program_Creation_Group
308 EAPI Embryo_Program *
309 embryo_program_load(char *file)
314 void *program = NULL;
315 int program_size = 0;
317 f = fopen(file, "rb");
319 fseek(f, 0, SEEK_END);
320 program_size = ftell(f);
322 if (program_size < (int)sizeof(Embryo_Header))
327 if (fread(&hdr, sizeof(Embryo_Header), 1, f) != 1)
333 #ifdef WORDS_BIGENDIAN
334 embryo_swap_32((unsigned int *)(&hdr.size));
336 if ((int)hdr.size < program_size) program_size = hdr.size;
337 program = malloc(program_size);
343 if (fread(program, program_size, 1, f) != 1)
349 ep = embryo_program_new(program, program_size);
356 * Frees the given Embryo program.
357 * @param ep The given program.
358 * @ingroup Embryo_Program_Creation_Group
361 embryo_program_free(Embryo_Program *ep)
365 if (ep->base) free(ep->base);
366 if ((!ep->dont_free_code) && (ep->code)) free(ep->code);
367 if (ep->native_calls) free(ep->native_calls);
368 for (i = 0; i < ep->params_size; i++)
370 if (ep->params[i].string) free(ep->params[i].string);
371 if (ep->params[i].cell_array) free(ep->params[i].cell_array);
373 if (ep->params) free(ep->params);
378 * @defgroup Embryo_Func_Group Function Functions
380 * Functions that deal with Embryo program functions.
384 * Adds a native program call to the given Embryo program.
385 * @param ep The given Embryo program.
386 * @param name The name for the call used in the script.
387 * @param func The function to use when the call is made.
388 * @ingroup Embryo_Func_Group
391 embryo_program_native_call_add(Embryo_Program *ep, const char *name, Embryo_Cell (*func) (Embryo_Program *ep, Embryo_Cell *params))
393 Embryo_Func_Stub *func_entry;
397 if ((!ep ) || (!name) || (!func)) return;
398 if (strlen(name) > sNAMEMAX) return;
400 hdr = (Embryo_Header *)ep->code;
401 if (hdr->defsize < 1) return;
402 num = NUMENTRIES(hdr, natives, libraries);
403 if (num <= 0) return;
405 ep->native_calls_size++;
406 if (ep->native_calls_size > ep->native_calls_alloc)
408 Embryo_Native *calls;
410 ep->native_calls_alloc += 32;
411 calls = realloc(ep->native_calls,
412 ep->native_calls_alloc * sizeof(Embryo_Native));
415 ep->native_calls_size--;
416 ep->native_calls_alloc -= 32;
419 ep->native_calls = calls;
421 ep->native_calls[ep->native_calls_size - 1] = func;
423 func_entry = GETENTRY(hdr, natives, 0);
424 for (i = 0; i < num; i++)
426 if (func_entry->address == 0)
430 entry_name = GETENTRYNAME(hdr, func_entry);
431 if ((entry_name) && (!strcmp(entry_name, name)))
433 func_entry->address = ep->native_calls_size;
434 /* FIXME: embryo_cc is putting in multiple native */
435 /* function call entries - so we need to fill in all */
441 (Embryo_Func_Stub *)((unsigned char *)func_entry + hdr->defsize);
446 * @defgroup Embryo_Program_VM_Group Virtual Machine Functions
448 * Functions that deal with creating and destroying virtual machine sessions
449 * for a given program.
451 * A given embryo program can have multiple virtual machine sessions running.
452 * This is useful when you have a native call that in turn calls a function in
453 * the embryo program. The native call can start a new virtual machine
454 * session to run the function it needs. Once completed, the session can be
455 * popped off the program's stack, and the native call can return its value
456 * to the old session.
458 * A new virtual machine session is created by pushing a new virtual machine
459 * onto the session stack of a program using @ref embryo_program_vm_push.
460 * The current virtual machine session can be destroyed by calling
461 * @ref embryo_program_vm_pop.
465 * Resets the current virtual machine session of the given program.
466 * @param ep The given program.
467 * @ingroup Embryo_Program_VM_Group
470 embryo_program_vm_reset(Embryo_Program *ep)
474 if ((!ep) || (!ep->base)) return;
475 hdr = (Embryo_Header *)ep->code;
476 memcpy(ep->base, hdr, hdr->size);
477 *(Embryo_Cell *)(ep->base + (int)hdr->stp - sizeof(Embryo_Cell)) = 0;
479 ep->hlw = hdr->hea - hdr->dat; /* stack and heap relative to data segment */
480 ep->stp = hdr->stp - hdr->dat - sizeof(Embryo_Cell);
486 * Starts a new virtual machine session for the given program.
488 * See @ref Embryo_Program_VM_Group for more information about how this works.
490 * @param ep The given program.
491 * @ingroup Embryo_Program_VM_Group
494 embryo_program_vm_push(Embryo_Program *ep)
502 embryo_program_vm_reset(ep);
505 hdr = (Embryo_Header *)ep->code;
506 ep->base = malloc(hdr->stp);
512 embryo_program_vm_reset(ep);
516 * Frees the current virtual machine session associated with the given program.
518 * See @ref Embryo_Program_VM_Group for more information about how this works.
519 * Note that you will need to retrieve any return data or data on the stack
522 * @param ep The given program.
523 * @ingroup Embryo_Program_VM_Group
526 embryo_program_vm_pop(Embryo_Program *ep)
528 if ((!ep) || (!ep->base)) return;
530 if (ep->pushes >= 1) return;
536 * @defgroup Embryo_Swap_Group Byte Swapping Functions
538 * Functions that are used to ensure that integers passed to the
539 * virtual machine are in small endian format. These functions are
540 * used to ensure that the virtual machine operates correctly on big
545 * Ensures that the given unsigned short integer is in the small
547 * @param v Pointer to the given integer.
548 * @ingroup Embryo_Swap_Group
551 embryo_swap_16(unsigned short *v
552 #ifndef WORDS_BIGENDIAN
557 #ifdef WORDS_BIGENDIAN
558 _embryo_byte_swap_16(v);
563 * Ensures that the given unsigned integer is in the small endian
565 * @param v Pointer to the given integer.
566 * @ingroup Embryo_Swap_Group
569 embryo_swap_32(unsigned int *v
570 #ifndef WORDS_BIGENDIAN
575 #ifdef WORDS_BIGENDIAN
576 _embryo_byte_swap_32(v);
581 * Returns the function in the given program with the given name.
582 * @param ep The given program.
583 * @param name The given function name.
584 * @return The function if successful. Otherwise, @c EMBRYO_FUNCTION_NONE.
585 * @ingroup Embryo_Func_Group
588 embryo_program_function_find(Embryo_Program *ep, const char *name)
590 int first, last, mid, result;
591 char pname[sNAMEMAX + 1];
594 if (!ep) return EMBRYO_FUNCTION_NONE;
595 hdr = (Embryo_Header *)ep->code;
596 last = NUMENTRIES(hdr, publics, natives) - 1;
599 while (first <= last)
601 mid = (first + last) / 2;
602 if (_embryo_func_get(ep, mid, pname) == EMBRYO_ERROR_NONE)
603 result = strcmp(pname, name);
605 return EMBRYO_FUNCTION_NONE;
607 if (result > 0) last = mid - 1;
608 else if (result < 0) first = mid + 1;
611 return EMBRYO_FUNCTION_NONE;
615 * @defgroup Embryo_Public_Variable_Group Public Variable Access Functions
617 * In an Embryo program, a global variable can be declared public, as
618 * described in @ref Small_Scope_Subsection. The functions here allow
619 * the host program to access these public variables.
623 * Retrieves the location of the public variable in the given program
624 * with the given name.
625 * @param ep The given program.
626 * @param name The given name.
627 * @return The address of the variable if found. @c EMBRYO_CELL_NONE
629 * @ingroup Embryo_Public_Variable_Group
632 embryo_program_variable_find(Embryo_Program *ep, const char *name)
634 int first, last, mid, result;
635 char pname[sNAMEMAX + 1];
639 if (!ep) return EMBRYO_CELL_NONE;
640 if (!ep->base) return EMBRYO_CELL_NONE;
641 hdr = (Embryo_Header *)ep->base;
642 last = NUMENTRIES(hdr, pubvars, tags) - 1;
645 while (first <= last)
647 mid = (first + last) / 2;
648 if (_embryo_var_get(ep, mid, pname, &paddr) == EMBRYO_ERROR_NONE)
649 result = strcmp(pname, name);
651 return EMBRYO_CELL_NONE;
653 if (result > 0) last = mid - 1;
654 else if (result < 0) first = mid + 1;
657 return EMBRYO_CELL_NONE;
661 * Retrieves the number of public variables in the given program.
662 * @param ep The given program.
663 * @return The number of public variables.
664 * @ingroup Embryo_Public_Variable_Group
667 embryo_program_variable_count_get(Embryo_Program *ep)
672 if (!ep->base) return 0;
673 hdr = (Embryo_Header *)ep->base;
674 return NUMENTRIES(hdr, pubvars, tags);
678 * Retrieves the location of the public variable in the given program
679 * with the given identifier.
680 * @param ep The given program.
681 * @param num The identifier of the public variable.
682 * @return The virtual machine address of the variable if found.
683 * @c EMBRYO_CELL_NONE otherwise.
684 * @ingroup Embryo_Public_Variable_Group
687 embryo_program_variable_get(Embryo_Program *ep, int num)
690 char pname[sNAMEMAX + 1];
692 if (!ep) return EMBRYO_CELL_NONE;
693 if (!ep->base) return EMBRYO_CELL_NONE;
694 if (_embryo_var_get(ep, num, pname, &paddr) == EMBRYO_ERROR_NONE)
696 return EMBRYO_CELL_NONE;
700 * @defgroup Embryo_Error_Group Error Functions
702 * Functions that set and retrieve error codes in Embryo programs.
706 * Sets the error code for the given program to the given code.
707 * @param ep The given program.
708 * @param error The given error code.
709 * @ingroup Embryo_Error_Group
712 embryo_program_error_set(Embryo_Program *ep, int error)
719 * Retrieves the current error code for the given program.
720 * @param ep The given program.
721 * @return The current error code.
722 * @ingroup Embryo_Error_Group
725 embryo_program_error_get(Embryo_Program *ep)
727 if (!ep) return EMBRYO_ERROR_NONE;
732 * @defgroup Embryo_Program_Data_Group Program Data Functions
734 * Functions that set and retrieve data associated with the given
739 * Sets the data associated to the given program.
740 * @param ep The given program.
741 * @param data New bytecode data.
742 * @ingroup Embryo_Program_Data_Group
745 embryo_program_data_set(Embryo_Program *ep, void *data)
752 * Retrieves the data associated to the given program.
753 * @param ep The given program.
754 * @ingroup Embryo_Program_Data_Group
757 embryo_program_data_get(Embryo_Program *ep)
759 if (!ep) return NULL;
764 * Retrieves a string describing the given error code.
765 * @param error The given error code.
766 * @return String describing the given error code. If the given code is not
767 * known, the string "(unknown)" is returned.
768 * @ingroup Embryo_Error_Group
771 embryo_error_string_get(int error)
773 const char *messages[] =
775 /* EMBRYO_ERROR_NONE */ "(none)",
776 /* EMBRYO_ERROR_EXIT */ "Forced exit",
777 /* EMBRYO_ERROR_ASSERT */ "Assertion failed",
778 /* EMBRYO_ERROR_STACKERR */ "Stack/heap collision (insufficient stack size)",
779 /* EMBRYO_ERROR_BOUNDS */ "Array index out of bounds",
780 /* EMBRYO_ERROR_MEMACCESS */ "Invalid memory access",
781 /* EMBRYO_ERROR_INVINSTR */ "Invalid instruction",
782 /* EMBRYO_ERROR_STACKLOW */ "Stack underflow",
783 /* EMBRYO_ERROR_HEAPLOW */ "Heap underflow",
784 /* EMBRYO_ERROR_CALLBACK */ "No (valid) native function callback",
785 /* EMBRYO_ERROR_NATIVE */ "Native function failed",
786 /* EMBRYO_ERROR_DIVIDE */ "Divide by zero",
787 /* EMBRYO_ERROR_SLEEP */ "(sleep mode)",
788 /* 13 */ "(reserved)",
789 /* 14 */ "(reserved)",
790 /* 15 */ "(reserved)",
791 /* EMBRYO_ERROR_MEMORY */ "Out of memory",
792 /* EMBRYO_ERROR_FORMAT */ "Invalid/unsupported P-code file format",
793 /* EMBRYO_ERROR_VERSION */ "File is for a newer version of the Embryo_Program",
794 /* EMBRYO_ERROR_NOTFOUND */ "Native/Public function is not found",
795 /* EMBRYO_ERROR_INDEX */ "Invalid index parameter (bad entry point)",
796 /* EMBRYO_ERROR_DEBUG */ "Debugger cannot run",
797 /* EMBRYO_ERROR_INIT */ "Embryo_Program not initialized (or doubly initialized)",
798 /* EMBRYO_ERROR_USERDATA */ "Unable to set user data field (table full)",
799 /* EMBRYO_ERROR_INIT_JIT */ "Cannot initialize the JIT",
800 /* EMBRYO_ERROR_PARAMS */ "Parameter error",
802 if ((error < 0) || (error >= (int)(sizeof(messages) / sizeof(messages[0]))))
803 return (const char *)"(unknown)";
804 return messages[error];
808 * @defgroup Embryo_Data_String_Group Embryo Data String Functions
810 * Functions that operate on strings in the memory of a virtual machine.
814 * Retrieves the length of the string starting at the given cell.
815 * @param ep The program the cell is part of.
816 * @param str_cell Pointer to the first cell of the string.
817 * @return The length of the string. @c 0 is returned if there is an error.
818 * @ingroup Embryo_Data_String_Group
821 embryo_data_string_length_get(Embryo_Program *ep, Embryo_Cell *str_cell)
826 if ((!ep) || (!ep->base)) return 0;
827 hdr = (Embryo_Header *)ep->base;
829 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
830 ((void *)str_cell < (void *)ep->base))
832 for (len = 0; str_cell[len] != 0; len++);
837 * Copies the string starting at the given cell to the given buffer.
838 * @param ep The program the cell is part of.
839 * @param str_cell Pointer to the first cell of the string.
840 * @param dst The given buffer.
841 * @ingroup Embryo_Data_String_Group
844 embryo_data_string_get(Embryo_Program *ep, Embryo_Cell *str_cell, char *dst)
850 if ((!ep) || (!ep->base))
855 hdr = (Embryo_Header *)ep->base;
857 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
858 ((void *)str_cell < (void *)ep->base))
863 for (i = 0; str_cell[i] != 0; i++)
865 #ifdef WORDS_BIGENDIAN
870 _embryo_byte_swap_32(&tmp);
874 dst[i] = str_cell[i];
881 * Copies string in the given buffer into the virtual machine memory
882 * starting at the given cell.
883 * @param ep The program the cell is part of.
884 * @param src The given buffer.
885 * @param str_cell Pointer to the first cell to copy the string to.
886 * @ingroup Embryo_Data_String_Group
889 embryo_data_string_set(Embryo_Program *ep, const char *src, Embryo_Cell *str_cell)
895 if (!ep->base) return;
896 hdr = (Embryo_Header *)ep->base;
898 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
899 ((void *)str_cell < (void *)ep->base))
906 for (i = 0; src[i] != 0; i++)
908 if ((void *)(&(str_cell[i])) >= (void *)(ep->base + hdr->stp)) return;
909 else if ((void *)(&(str_cell[i])) == (void *)(ep->base + hdr->stp - 1))
914 #ifdef WORDS_BIGENDIAN
919 _embryo_byte_swap_32(&tmp);
923 str_cell[i] = src[i];
930 * Retreives a pointer to the address in the virtual machine given by the
932 * @param ep The program whose virtual machine address is being queried.
933 * @param addr The given cell.
934 * @return A pointer to the cell at the given address.
935 * @ingroup Embryo_Data_String_Group
938 embryo_data_address_get(Embryo_Program *ep, Embryo_Cell addr)
943 if ((!ep) || (!ep->base)) return NULL;
944 hdr = (Embryo_Header *)ep->base;
945 data = ep->base + (int)hdr->dat;
946 if ((addr < 0) || (addr >= hdr->stp)) return NULL;
947 return (Embryo_Cell *)(data + (int)addr);
951 * @defgroup Embryo_Heap_Group Heap Functions
953 * The heap is an area of memory that can be allocated for program
954 * use at runtime. The heap functions here change the amount of heap
959 * Increases the size of the heap of the given virtual machine by the given
960 * number of Embryo_Cells.
961 * @param ep The program with the given virtual machine.
962 * @param cells The given number of Embryo_Cells.
963 * @return The address of the new memory region on success.
964 * @c EMBRYO_CELL_NONE otherwise.
965 * @ingroup Embryo_Heap_Group
968 embryo_data_heap_push(Embryo_Program *ep, int cells)
973 if ((!ep) || (!ep->base)) return EMBRYO_CELL_NONE;
974 hdr = (Embryo_Header *)ep->base;
975 if (ep->stk - ep->hea - (cells * sizeof(Embryo_Cell)) < STKMARGIN)
976 return EMBRYO_CELL_NONE;
978 ep->hea += (cells * sizeof(Embryo_Cell));
983 * Decreases the size of the heap of the given virtual machine down to the
985 * @param ep The program with the given virtual machine.
986 * @param down_to The given size.
987 * @ingroup Embryo_Heap_Group
990 embryo_data_heap_pop(Embryo_Program *ep, Embryo_Cell down_to)
993 if (down_to < 0) down_to = 0;
994 if (ep->hea > down_to) ep->hea = down_to;
998 * @defgroup Embryo_Run_Group Program Run Functions
1000 * Functions that are involved in actually running functions in an
1005 * Returns the number of virtual machines are running for the given program.
1006 * @param ep The given program.
1007 * @return The number of virtual machines running.
1008 * @ingroup Embryo_Run_Group
1011 embryo_program_recursion_get(Embryo_Program *ep)
1013 return ep->run_count;
1018 #define EMBRYO_EXEC_JUMPTABLE
1022 /* jump table optimization - only works for gcc though */
1023 #ifdef EMBRYO_EXEC_JUMPTABLE
1024 #define SWITCH(x) while (1) { goto *switchtable[x];
1025 #define SWITCHEND break; }
1026 #define CASE(x) SWITCHTABLE_##x:
1027 #define BREAK break;
1029 #define SWITCH(x) switch (x) {
1031 #define CASE(x) case x:
1036 * Runs the given function of the given Embryo program in the current
1037 * virtual machine. The parameter @p fn can be found using
1038 * @ref embryo_program_function_find.
1040 * @note For Embryo to be able to run a function, it must have been
1041 * declared @c public in the Small source code.
1043 * @param ep The given program.
1044 * @param fn The given function. Normally "main", in which case the
1045 * constant @c EMBRYO_FUNCTION_MAIN can be used.
1046 * @return @c EMBRYO_PROGRAM_OK on success. @c EMBRYO_PROGRAM_SLEEP if the
1047 * program is halted by the Small @c sleep call.
1048 * @c EMBRYO_PROGRAM_FAIL if there is an error.
1049 * @c EMBRYO_PROGRAM_TOOLONG if the program executes for longer than
1050 * it is allowed to in abstract machine instruction count.
1051 * @ingroup Embryo_Run_Group
1054 embryo_program_run(Embryo_Program *ep, Embryo_Function fn)
1057 Embryo_Func_Stub *func;
1058 unsigned char *code, *data;
1059 Embryo_Cell pri, alt, stk, frm, hea, hea_start;
1060 Embryo_Cell reset_stk, reset_hea, *cip;
1061 Embryo_UCell codesize;
1068 #ifdef EMBRYO_EXEC_JUMPTABLE
1069 /* we limit the jumptable to 256 elements. why? above we forced "op" to be
1070 * a unsigned char - that means 256 max values. we limit opcode overflow
1071 * here, so eliminating crashes on table lookups with bad/corrupt bytecode.
1072 * no need to atuall do compares, branches etc. the datatype does the work
1073 * for us. so that means EXCESS elements are all declared as OP_NONE to
1074 * keep them innocuous.
1076 static const void *switchtable[256] =
1078 &&SWITCHTABLE_EMBRYO_OP_NONE,
1079 &&SWITCHTABLE_EMBRYO_OP_LOAD_PRI,
1080 &&SWITCHTABLE_EMBRYO_OP_LOAD_ALT,
1081 &&SWITCHTABLE_EMBRYO_OP_LOAD_S_PRI,
1082 &&SWITCHTABLE_EMBRYO_OP_LOAD_S_ALT,
1083 &&SWITCHTABLE_EMBRYO_OP_LREF_PRI,
1084 &&SWITCHTABLE_EMBRYO_OP_LREF_ALT,
1085 &&SWITCHTABLE_EMBRYO_OP_LREF_S_PRI,
1086 &&SWITCHTABLE_EMBRYO_OP_LREF_S_ALT,
1087 &&SWITCHTABLE_EMBRYO_OP_LOAD_I,
1088 &&SWITCHTABLE_EMBRYO_OP_LODB_I,
1089 &&SWITCHTABLE_EMBRYO_OP_CONST_PRI,
1090 &&SWITCHTABLE_EMBRYO_OP_CONST_ALT,
1091 &&SWITCHTABLE_EMBRYO_OP_ADDR_PRI,
1092 &&SWITCHTABLE_EMBRYO_OP_ADDR_ALT,
1093 &&SWITCHTABLE_EMBRYO_OP_STOR_PRI,
1094 &&SWITCHTABLE_EMBRYO_OP_STOR_ALT,
1095 &&SWITCHTABLE_EMBRYO_OP_STOR_S_PRI,
1096 &&SWITCHTABLE_EMBRYO_OP_STOR_S_ALT,
1097 &&SWITCHTABLE_EMBRYO_OP_SREF_PRI,
1098 &&SWITCHTABLE_EMBRYO_OP_SREF_ALT,
1099 &&SWITCHTABLE_EMBRYO_OP_SREF_S_PRI,
1100 &&SWITCHTABLE_EMBRYO_OP_SREF_S_ALT,
1101 &&SWITCHTABLE_EMBRYO_OP_STOR_I,
1102 &&SWITCHTABLE_EMBRYO_OP_STRB_I,
1103 &&SWITCHTABLE_EMBRYO_OP_LIDX,
1104 &&SWITCHTABLE_EMBRYO_OP_LIDX_B,
1105 &&SWITCHTABLE_EMBRYO_OP_IDXADDR,
1106 &&SWITCHTABLE_EMBRYO_OP_IDXADDR_B,
1107 &&SWITCHTABLE_EMBRYO_OP_ALIGN_PRI,
1108 &&SWITCHTABLE_EMBRYO_OP_ALIGN_ALT,
1109 &&SWITCHTABLE_EMBRYO_OP_LCTRL,
1110 &&SWITCHTABLE_EMBRYO_OP_SCTRL,
1111 &&SWITCHTABLE_EMBRYO_OP_MOVE_PRI,
1112 &&SWITCHTABLE_EMBRYO_OP_MOVE_ALT,
1113 &&SWITCHTABLE_EMBRYO_OP_XCHG,
1114 &&SWITCHTABLE_EMBRYO_OP_PUSH_PRI,
1115 &&SWITCHTABLE_EMBRYO_OP_PUSH_ALT,
1116 &&SWITCHTABLE_EMBRYO_OP_PUSH_R,
1117 &&SWITCHTABLE_EMBRYO_OP_PUSH_C,
1118 &&SWITCHTABLE_EMBRYO_OP_PUSH,
1119 &&SWITCHTABLE_EMBRYO_OP_PUSH_S,
1120 &&SWITCHTABLE_EMBRYO_OP_POP_PRI,
1121 &&SWITCHTABLE_EMBRYO_OP_POP_ALT,
1122 &&SWITCHTABLE_EMBRYO_OP_STACK,
1123 &&SWITCHTABLE_EMBRYO_OP_HEAP,
1124 &&SWITCHTABLE_EMBRYO_OP_PROC,
1125 &&SWITCHTABLE_EMBRYO_OP_RET,
1126 &&SWITCHTABLE_EMBRYO_OP_RETN,
1127 &&SWITCHTABLE_EMBRYO_OP_CALL,
1128 &&SWITCHTABLE_EMBRYO_OP_CALL_PRI,
1129 &&SWITCHTABLE_EMBRYO_OP_JUMP,
1130 &&SWITCHTABLE_EMBRYO_OP_JREL,
1131 &&SWITCHTABLE_EMBRYO_OP_JZER,
1132 &&SWITCHTABLE_EMBRYO_OP_JNZ,
1133 &&SWITCHTABLE_EMBRYO_OP_JEQ,
1134 &&SWITCHTABLE_EMBRYO_OP_JNEQ,
1135 &&SWITCHTABLE_EMBRYO_OP_JLESS,
1136 &&SWITCHTABLE_EMBRYO_OP_JLEQ,
1137 &&SWITCHTABLE_EMBRYO_OP_JGRTR,
1138 &&SWITCHTABLE_EMBRYO_OP_JGEQ,
1139 &&SWITCHTABLE_EMBRYO_OP_JSLESS,
1140 &&SWITCHTABLE_EMBRYO_OP_JSLEQ,
1141 &&SWITCHTABLE_EMBRYO_OP_JSGRTR,
1142 &&SWITCHTABLE_EMBRYO_OP_JSGEQ,
1143 &&SWITCHTABLE_EMBRYO_OP_SHL,
1144 &&SWITCHTABLE_EMBRYO_OP_SHR,
1145 &&SWITCHTABLE_EMBRYO_OP_SSHR,
1146 &&SWITCHTABLE_EMBRYO_OP_SHL_C_PRI,
1147 &&SWITCHTABLE_EMBRYO_OP_SHL_C_ALT,
1148 &&SWITCHTABLE_EMBRYO_OP_SHR_C_PRI,
1149 &&SWITCHTABLE_EMBRYO_OP_SHR_C_ALT,
1150 &&SWITCHTABLE_EMBRYO_OP_SMUL,
1151 &&SWITCHTABLE_EMBRYO_OP_SDIV,
1152 &&SWITCHTABLE_EMBRYO_OP_SDIV_ALT,
1153 &&SWITCHTABLE_EMBRYO_OP_UMUL,
1154 &&SWITCHTABLE_EMBRYO_OP_UDIV,
1155 &&SWITCHTABLE_EMBRYO_OP_UDIV_ALT,
1156 &&SWITCHTABLE_EMBRYO_OP_ADD,
1157 &&SWITCHTABLE_EMBRYO_OP_SUB,
1158 &&SWITCHTABLE_EMBRYO_OP_SUB_ALT,
1159 &&SWITCHTABLE_EMBRYO_OP_AND,
1160 &&SWITCHTABLE_EMBRYO_OP_OR,
1161 &&SWITCHTABLE_EMBRYO_OP_XOR,
1162 &&SWITCHTABLE_EMBRYO_OP_NOT,
1163 &&SWITCHTABLE_EMBRYO_OP_NEG,
1164 &&SWITCHTABLE_EMBRYO_OP_INVERT,
1165 &&SWITCHTABLE_EMBRYO_OP_ADD_C,
1166 &&SWITCHTABLE_EMBRYO_OP_SMUL_C,
1167 &&SWITCHTABLE_EMBRYO_OP_ZERO_PRI,
1168 &&SWITCHTABLE_EMBRYO_OP_ZERO_ALT,
1169 &&SWITCHTABLE_EMBRYO_OP_ZERO,
1170 &&SWITCHTABLE_EMBRYO_OP_ZERO_S,
1171 &&SWITCHTABLE_EMBRYO_OP_SIGN_PRI,
1172 &&SWITCHTABLE_EMBRYO_OP_SIGN_ALT,
1173 &&SWITCHTABLE_EMBRYO_OP_EQ,
1174 &&SWITCHTABLE_EMBRYO_OP_NEQ,
1175 &&SWITCHTABLE_EMBRYO_OP_LESS,
1176 &&SWITCHTABLE_EMBRYO_OP_LEQ,
1177 &&SWITCHTABLE_EMBRYO_OP_GRTR,
1178 &&SWITCHTABLE_EMBRYO_OP_GEQ,
1179 &&SWITCHTABLE_EMBRYO_OP_SLESS,
1180 &&SWITCHTABLE_EMBRYO_OP_SLEQ,
1181 &&SWITCHTABLE_EMBRYO_OP_SGRTR,
1182 &&SWITCHTABLE_EMBRYO_OP_SGEQ,
1183 &&SWITCHTABLE_EMBRYO_OP_EQ_C_PRI,
1184 &&SWITCHTABLE_EMBRYO_OP_EQ_C_ALT,
1185 &&SWITCHTABLE_EMBRYO_OP_INC_PRI,
1186 &&SWITCHTABLE_EMBRYO_OP_INC_ALT,
1187 &&SWITCHTABLE_EMBRYO_OP_INC,
1188 &&SWITCHTABLE_EMBRYO_OP_INC_S,
1189 &&SWITCHTABLE_EMBRYO_OP_INC_I,
1190 &&SWITCHTABLE_EMBRYO_OP_DEC_PRI,
1191 &&SWITCHTABLE_EMBRYO_OP_DEC_ALT,
1192 &&SWITCHTABLE_EMBRYO_OP_DEC,
1193 &&SWITCHTABLE_EMBRYO_OP_DEC_S,
1194 &&SWITCHTABLE_EMBRYO_OP_DEC_I,
1195 &&SWITCHTABLE_EMBRYO_OP_MOVS,
1196 &&SWITCHTABLE_EMBRYO_OP_CMPS,
1197 &&SWITCHTABLE_EMBRYO_OP_FILL,
1198 &&SWITCHTABLE_EMBRYO_OP_HALT,
1199 &&SWITCHTABLE_EMBRYO_OP_BOUNDS,
1200 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_PRI,
1201 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_C,
1202 &&SWITCHTABLE_EMBRYO_OP_FILE,
1203 &&SWITCHTABLE_EMBRYO_OP_LINE,
1204 &&SWITCHTABLE_EMBRYO_OP_SYMBOL,
1205 &&SWITCHTABLE_EMBRYO_OP_SRANGE,
1206 &&SWITCHTABLE_EMBRYO_OP_JUMP_PRI,
1207 &&SWITCHTABLE_EMBRYO_OP_SWITCH,
1208 &&SWITCHTABLE_EMBRYO_OP_CASETBL,
1209 &&SWITCHTABLE_EMBRYO_OP_SWAP_PRI,
1210 &&SWITCHTABLE_EMBRYO_OP_SWAP_ALT,
1211 &&SWITCHTABLE_EMBRYO_OP_PUSHADDR,
1212 &&SWITCHTABLE_EMBRYO_OP_NOP,
1213 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_D,
1214 &&SWITCHTABLE_EMBRYO_OP_SYMTAG,
1215 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1216 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1217 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1218 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1219 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1220 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1221 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1222 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1223 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1224 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1225 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1226 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1227 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1228 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1229 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1230 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1231 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1232 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1233 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1234 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1235 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1236 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1237 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1238 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE
1241 if (!ep) return EMBRYO_PROGRAM_FAIL;
1242 if (!(ep->flags & EMBRYO_FLAG_RELOC))
1244 ep->error = EMBRYO_ERROR_INIT;
1245 return EMBRYO_PROGRAM_FAIL;
1249 ep->error = EMBRYO_ERROR_INIT;
1250 return EMBRYO_PROGRAM_FAIL;
1252 if (ep->run_count > 0)
1254 /* return EMBRYO_PROGRAM_BUSY; */
1255 /* FIXME: test C->vm->C->vm recursion more fully */
1256 /* it seems to work... just fine!!! - strange! */
1259 /* set up the registers */
1260 hdr = (Embryo_Header *)ep->base;
1261 codesize = (Embryo_UCell)(hdr->dat - hdr->cod);
1262 code = ep->base + (int)hdr->cod;
1263 data = ep->base + (int)hdr->dat;
1264 hea_start = hea = ep->hea;
1268 frm = alt = pri = 0;
1270 /* get the start address */
1271 if (fn == EMBRYO_FUNCTION_MAIN)
1275 ep->error = EMBRYO_ERROR_INDEX;
1276 return EMBRYO_PROGRAM_FAIL;
1278 cip = (Embryo_Cell *)(code + (int)hdr->cip);
1280 else if (fn == EMBRYO_FUNCTION_CONT)
1282 /* all registers: pri, alt, frm, cip, hea, stk, reset_stk, reset_hea */
1288 reset_stk = ep->reset_stk;
1289 reset_hea = ep->reset_hea;
1290 cip = (Embryo_Cell *)(code + (int)ep->cip);
1294 ep->error = EMBRYO_ERROR_INDEX;
1295 return EMBRYO_PROGRAM_FAIL;
1299 if (fn >= (Embryo_Cell)NUMENTRIES(hdr, publics, natives))
1301 ep->error = EMBRYO_ERROR_INDEX;
1302 return EMBRYO_PROGRAM_FAIL;
1304 func = GETENTRY(hdr, publics, fn);
1305 cip = (Embryo_Cell *)(code + (int)func->address);
1307 /* check values just copied */
1311 if (fn != EMBRYO_FUNCTION_CONT)
1315 for (i = ep->params_size - 1; i >= 0; i--)
1319 pr = &(ep->params[i]);
1323 Embryo_Cell ep_addr, *addr;
1325 len = strlen(pr->string);
1326 ep_addr = embryo_data_heap_push(ep, len + 1);
1327 if (ep_addr == EMBRYO_CELL_NONE)
1329 ep->error = EMBRYO_ERROR_HEAPLOW;
1330 return EMBRYO_PROGRAM_FAIL;
1332 addr = embryo_data_address_get(ep, ep_addr);
1334 embryo_data_string_set(ep, pr->string, addr);
1337 ep->error = EMBRYO_ERROR_HEAPLOW;
1338 return EMBRYO_PROGRAM_FAIL;
1343 else if (pr->cell_array)
1346 Embryo_Cell ep_addr, *addr;
1348 len = pr->cell_array_size;
1349 ep_addr = embryo_data_heap_push(ep, len + 1);
1350 if (ep_addr == EMBRYO_CELL_NONE)
1352 ep->error = EMBRYO_ERROR_HEAPLOW;
1353 return EMBRYO_PROGRAM_FAIL;
1355 addr = embryo_data_address_get(ep, ep_addr);
1357 memcpy(addr, pr->cell_array,
1358 pr->cell_array_size * sizeof(Embryo_Cell));
1361 ep->error = EMBRYO_ERROR_HEAPLOW;
1362 return EMBRYO_PROGRAM_FAIL;
1365 free(pr->cell_array);
1372 PUSH(ep->params_size * sizeof(Embryo_Cell));
1379 ep->params_size = ep->params_alloc = 0;
1381 /* check stack/heap before starting to run */
1384 /* track recursion depth */
1387 max_run_cycles = ep->max_run_cycles;
1389 for (cycle_count = 0;;)
1391 if (max_run_cycles > 0)
1393 if (cycle_count >= max_run_cycles)
1399 op = (Embryo_Opcode)*cip++;
1401 CASE(EMBRYO_OP_LOAD_PRI);
1403 pri = *(Embryo_Cell *)(data + (int)offs);
1405 CASE(EMBRYO_OP_LOAD_ALT);
1407 alt = *(Embryo_Cell *)(data + (int)offs);
1409 CASE(EMBRYO_OP_LOAD_S_PRI);
1411 pri = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1413 CASE(EMBRYO_OP_LOAD_S_ALT);
1415 alt = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1417 CASE(EMBRYO_OP_LREF_PRI);
1419 offs = *(Embryo_Cell *)(data + (int)offs);
1420 pri = *(Embryo_Cell *)(data + (int)offs);
1422 CASE(EMBRYO_OP_LREF_ALT);
1424 offs = *(Embryo_Cell *)(data + (int)offs);
1425 alt = *(Embryo_Cell *)(data + (int)offs);
1427 CASE(EMBRYO_OP_LREF_S_PRI);
1429 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1430 pri = *(Embryo_Cell *)(data + (int)offs);
1432 CASE(EMBRYO_OP_LREF_S_ALT);
1434 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1435 alt = *(Embryo_Cell *)(data + (int)offs);
1437 CASE(EMBRYO_OP_LOAD_I);
1439 pri = *(Embryo_Cell *)(data + (int)pri);
1441 CASE(EMBRYO_OP_LODB_I);
1447 pri = *(data + (int)pri);
1450 pri = *(unsigned short *)(data + (int)pri);
1453 pri = *(unsigned int *)(data + (int)pri);
1456 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1460 CASE(EMBRYO_OP_CONST_PRI);
1463 CASE(EMBRYO_OP_CONST_ALT);
1466 CASE(EMBRYO_OP_ADDR_PRI);
1470 CASE(EMBRYO_OP_ADDR_ALT);
1474 CASE(EMBRYO_OP_STOR_PRI);
1476 *(Embryo_Cell *)(data + (int)offs) = pri;
1478 CASE(EMBRYO_OP_STOR_ALT);
1480 *(Embryo_Cell *)(data + (int)offs) = alt;
1482 CASE(EMBRYO_OP_STOR_S_PRI);
1484 *(Embryo_Cell *)(data + (int)frm + (int)offs) = pri;
1486 CASE(EMBRYO_OP_STOR_S_ALT);
1488 *(Embryo_Cell *)(data + (int)frm + (int)offs) = alt;
1490 CASE(EMBRYO_OP_SREF_PRI);
1492 offs = *(Embryo_Cell *)(data + (int)offs);
1493 *(Embryo_Cell *)(data + (int)offs) = pri;
1495 CASE(EMBRYO_OP_SREF_ALT);
1497 offs = *(Embryo_Cell *)(data + (int)offs);
1498 *(Embryo_Cell *)(data + (int)offs) = alt;
1500 CASE(EMBRYO_OP_SREF_S_PRI);
1502 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1503 *(Embryo_Cell *)(data + (int)offs) = pri;
1505 CASE(EMBRYO_OP_SREF_S_ALT);
1507 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1508 *(Embryo_Cell *)(data + (int)offs) = alt;
1510 CASE(EMBRYO_OP_STOR_I);
1512 *(Embryo_Cell *)(data + (int)alt) = pri;
1514 CASE(EMBRYO_OP_STRB_I);
1520 *(data + (int)alt) = (unsigned char)pri;
1523 *(unsigned short *)(data + (int)alt) = (unsigned short)pri;
1526 *(unsigned int *)(data + (int)alt) = (unsigned int)pri;
1529 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1533 CASE(EMBRYO_OP_LIDX);
1534 offs = (pri * sizeof(Embryo_Cell)) + alt;
1536 pri = *(Embryo_Cell *)(data + (int)offs);
1538 CASE(EMBRYO_OP_LIDX_B);
1540 offs = (pri << (int)offs) + alt;
1542 pri = *(Embryo_Cell *)(data + (int)offs);
1544 CASE(EMBRYO_OP_IDXADDR);
1545 pri = (pri * sizeof(Embryo_Cell)) + alt;
1547 CASE(EMBRYO_OP_IDXADDR_B);
1549 pri = (pri << (int)offs) + alt;
1551 CASE(EMBRYO_OP_ALIGN_PRI);
1553 #ifdef WORDS_BIGENDIAN
1554 if ((size_t)offs < sizeof(Embryo_Cell))
1555 pri ^= sizeof(Embryo_Cell) - offs;
1558 CASE(EMBRYO_OP_ALIGN_ALT);
1560 #ifdef WORDS_BIGENDIAN
1561 if ((size_t)offs < sizeof(Embryo_Cell))
1562 alt ^= sizeof(Embryo_Cell) - offs;
1565 CASE(EMBRYO_OP_LCTRL);
1588 pri = (Embryo_Cell)((unsigned char *)cip - code);
1591 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1595 CASE(EMBRYO_OP_SCTRL);
1605 /* cannot change these parameters */
1614 cip = (Embryo_Cell *)(code + (int)pri);
1617 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1621 CASE(EMBRYO_OP_MOVE_PRI);
1624 CASE(EMBRYO_OP_MOVE_ALT);
1627 CASE(EMBRYO_OP_XCHG);
1628 offs = pri; /* offs is a temporary variable */
1632 CASE(EMBRYO_OP_PUSH_PRI);
1635 CASE(EMBRYO_OP_PUSH_ALT);
1638 CASE(EMBRYO_OP_PUSH_C);
1642 CASE(EMBRYO_OP_PUSH_R);
1644 while (offs--) PUSH(pri);
1646 CASE(EMBRYO_OP_PUSH);
1648 PUSH(*(Embryo_Cell *)(data + (int)offs));
1650 CASE(EMBRYO_OP_PUSH_S);
1652 PUSH(*(Embryo_Cell *)(data + (int)frm + (int)offs));
1654 CASE(EMBRYO_OP_POP_PRI);
1657 CASE(EMBRYO_OP_POP_ALT);
1660 CASE(EMBRYO_OP_STACK);
1667 CASE(EMBRYO_OP_HEAP);
1674 CASE(EMBRYO_OP_PROC);
1679 CASE(EMBRYO_OP_RET);
1682 if ((Embryo_UCell)offs >= codesize)
1683 ABORT(ep, EMBRYO_ERROR_MEMACCESS);
1684 cip = (Embryo_Cell *)(code + (int)offs);
1686 CASE(EMBRYO_OP_RETN);
1689 if ((Embryo_UCell)offs >= codesize)
1690 ABORT(ep, EMBRYO_ERROR_MEMACCESS);
1691 cip = (Embryo_Cell *)(code + (int)offs);
1692 stk += *(Embryo_Cell *)(data + (int)stk) + sizeof(Embryo_Cell); /* remove parameters from the stack */
1695 CASE(EMBRYO_OP_CALL);
1696 PUSH(((unsigned char *)cip - code) + sizeof(Embryo_Cell));/* skip address */
1697 cip = JUMPABS(code, cip); /* jump to the address */
1699 CASE(EMBRYO_OP_CALL_PRI);
1700 PUSH((unsigned char *)cip - code);
1701 cip = (Embryo_Cell *)(code + (int)pri);
1703 CASE(EMBRYO_OP_JUMP);
1704 /* since the GETPARAM() macro modifies cip, you cannot
1705 * do GETPARAM(cip) directly */
1706 cip = JUMPABS(code, cip);
1708 CASE(EMBRYO_OP_JREL);
1710 cip = (Embryo_Cell *)((unsigned char *)cip + (int)offs + sizeof(Embryo_Cell));
1712 CASE(EMBRYO_OP_JZER);
1714 cip = JUMPABS(code, cip);
1716 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1718 CASE(EMBRYO_OP_JNZ);
1720 cip = JUMPABS(code, cip);
1722 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1724 CASE(EMBRYO_OP_JEQ);
1726 cip = JUMPABS(code, cip);
1728 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1730 CASE(EMBRYO_OP_JNEQ);
1732 cip = JUMPABS(code, cip);
1734 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1736 CASE(EMBRYO_OP_JLESS);
1737 if ((Embryo_UCell)pri < (Embryo_UCell)alt)
1738 cip = JUMPABS(code, cip);
1740 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1742 CASE(EMBRYO_OP_JLEQ);
1743 if ((Embryo_UCell)pri <= (Embryo_UCell)alt)
1744 cip = JUMPABS(code, cip);
1746 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1748 CASE(EMBRYO_OP_JGRTR);
1749 if ((Embryo_UCell)pri > (Embryo_UCell)alt)
1750 cip = JUMPABS(code, cip);
1752 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1754 CASE(EMBRYO_OP_JGEQ);
1755 if ((Embryo_UCell)pri >= (Embryo_UCell)alt)
1756 cip = JUMPABS(code, cip);
1758 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1760 CASE(EMBRYO_OP_JSLESS);
1762 cip = JUMPABS(code, cip);
1764 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1766 CASE(EMBRYO_OP_JSLEQ);
1768 cip = JUMPABS(code, cip);
1770 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1772 CASE(EMBRYO_OP_JSGRTR);
1774 cip = JUMPABS(code, cip);
1776 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1778 CASE(EMBRYO_OP_JSGEQ);
1780 cip = JUMPABS(code, cip);
1782 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1784 CASE(EMBRYO_OP_SHL);
1787 CASE(EMBRYO_OP_SHR);
1788 pri = (Embryo_UCell)pri >> (int)alt;
1790 CASE(EMBRYO_OP_SSHR);
1793 CASE(EMBRYO_OP_SHL_C_PRI);
1797 CASE(EMBRYO_OP_SHL_C_ALT);
1801 CASE(EMBRYO_OP_SHR_C_PRI);
1803 pri = (Embryo_UCell)pri >> (int)offs;
1805 CASE(EMBRYO_OP_SHR_C_ALT);
1807 alt = (Embryo_UCell)alt >> (int)offs;
1809 CASE(EMBRYO_OP_SMUL);
1812 CASE(EMBRYO_OP_SDIV);
1813 if (alt == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1814 /* divide must always round down; this is a bit
1815 * involved to do in a machine-independent way.
1817 offs = ((pri % alt) + alt) % alt; /* true modulus */
1818 pri = (pri - offs) / alt; /* division result */
1821 CASE(EMBRYO_OP_SDIV_ALT);
1822 if (pri == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1823 /* divide must always round down; this is a bit
1824 * involved to do in a machine-independent way.
1826 offs = ((alt % pri) + pri) % pri; /* true modulus */
1827 pri = (alt - offs) / pri; /* division result */
1830 CASE(EMBRYO_OP_UMUL);
1831 pri = (Embryo_UCell)pri * (Embryo_UCell)alt;
1833 CASE(EMBRYO_OP_UDIV);
1834 if (alt == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1835 offs = (Embryo_UCell)pri % (Embryo_UCell)alt; /* temporary storage */
1836 pri = (Embryo_UCell)pri / (Embryo_UCell)alt;
1839 CASE(EMBRYO_OP_UDIV_ALT);
1840 if (pri == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1841 offs = (Embryo_UCell)alt % (Embryo_UCell)pri; /* temporary storage */
1842 pri = (Embryo_UCell)alt / (Embryo_UCell)pri;
1845 CASE(EMBRYO_OP_ADD);
1848 CASE(EMBRYO_OP_SUB);
1851 CASE(EMBRYO_OP_SUB_ALT);
1854 CASE(EMBRYO_OP_AND);
1860 CASE(EMBRYO_OP_XOR);
1863 CASE(EMBRYO_OP_NOT);
1866 CASE(EMBRYO_OP_NEG);
1869 CASE(EMBRYO_OP_INVERT);
1872 CASE(EMBRYO_OP_ADD_C);
1876 CASE(EMBRYO_OP_SMUL_C);
1880 CASE(EMBRYO_OP_ZERO_PRI);
1883 CASE(EMBRYO_OP_ZERO_ALT);
1886 CASE(EMBRYO_OP_ZERO);
1888 *(Embryo_Cell *)(data + (int)offs) = 0;
1890 CASE(EMBRYO_OP_ZERO_S);
1892 *(Embryo_Cell *)(data + (int)frm + (int)offs) = 0;
1894 CASE(EMBRYO_OP_SIGN_PRI);
1895 if ((pri & 0xff) >= 0x80) pri |= ~(Embryo_UCell)0xff;
1897 CASE(EMBRYO_OP_SIGN_ALT);
1898 if ((alt & 0xff) >= 0x80) alt |= ~(Embryo_UCell)0xff;
1901 pri = (pri == alt) ? 1 : 0;
1903 CASE(EMBRYO_OP_NEQ);
1904 pri = (pri != alt) ? 1 : 0;
1906 CASE(EMBRYO_OP_LESS);
1907 pri = ((Embryo_UCell)pri < (Embryo_UCell)alt) ? 1 : 0;
1909 CASE(EMBRYO_OP_LEQ);
1910 pri = ((Embryo_UCell)pri <= (Embryo_UCell)alt) ? 1 : 0;
1912 CASE(EMBRYO_OP_GRTR);
1913 pri = ((Embryo_UCell)pri > (Embryo_UCell)alt) ? 1 : 0;
1915 CASE(EMBRYO_OP_GEQ);
1916 pri = ((Embryo_UCell)pri >= (Embryo_UCell)alt) ? 1 : 0;
1918 CASE(EMBRYO_OP_SLESS);
1919 pri = (pri < alt) ? 1 : 0;
1921 CASE(EMBRYO_OP_SLEQ);
1922 pri = (pri <= alt) ? 1 : 0;
1924 CASE(EMBRYO_OP_SGRTR);
1925 pri = (pri > alt) ? 1 : 0;
1927 CASE(EMBRYO_OP_SGEQ);
1928 pri = (pri >= alt) ? 1 : 0;
1930 CASE(EMBRYO_OP_EQ_C_PRI);
1932 pri = (pri == offs) ? 1 : 0;
1934 CASE(EMBRYO_OP_EQ_C_ALT);
1936 pri = (alt == offs) ? 1 : 0;
1938 CASE(EMBRYO_OP_INC_PRI);
1941 CASE(EMBRYO_OP_INC_ALT);
1944 CASE(EMBRYO_OP_INC);
1946 *(Embryo_Cell *)(data + (int)offs) += 1;
1948 CASE(EMBRYO_OP_INC_S);
1950 *(Embryo_Cell *)(data + (int)frm + (int)offs) += 1;
1952 CASE(EMBRYO_OP_INC_I);
1953 *(Embryo_Cell *)(data + (int)pri) += 1;
1955 CASE(EMBRYO_OP_DEC_PRI);
1958 CASE(EMBRYO_OP_DEC_ALT);
1961 CASE(EMBRYO_OP_DEC);
1963 *(Embryo_Cell *)(data + (int)offs) -= 1;
1965 CASE(EMBRYO_OP_DEC_S);
1967 *(Embryo_Cell *)(data + (int)frm + (int)offs) -= 1;
1969 CASE(EMBRYO_OP_DEC_I);
1970 *(Embryo_Cell *)(data + (int)pri) -= 1;
1972 CASE(EMBRYO_OP_MOVS);
1978 memcpy(data+(int)alt, data+(int)pri, (int)offs);
1980 CASE(EMBRYO_OP_CMPS);
1986 pri = memcmp(data + (int)alt, data + (int)pri, (int)offs);
1988 CASE(EMBRYO_OP_FILL);
1993 (size_t)offs >= sizeof(Embryo_Cell);
1994 i += sizeof(Embryo_Cell), offs -= sizeof(Embryo_Cell))
1995 *(Embryo_Cell *)(data + i) = pri;
1997 CASE(EMBRYO_OP_HALT);
2000 /* store complete status */
2006 ep->cip = (Embryo_Cell)((unsigned char*)cip - code);
2007 if (offs == EMBRYO_ERROR_SLEEP)
2009 ep->reset_stk = reset_stk;
2010 ep->reset_hea = reset_hea;
2012 return EMBRYO_PROGRAM_SLEEP;
2015 CASE(EMBRYO_OP_BOUNDS);
2017 if ((Embryo_UCell)pri > (Embryo_UCell)offs)
2018 ABORT(ep, EMBRYO_ERROR_BOUNDS);
2020 CASE(EMBRYO_OP_SYSREQ_PRI);
2021 /* save a few registers */
2022 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
2026 num = _embryo_native_call(ep, pri, &pri, (Embryo_Cell *)(data + (int)stk));
2027 if (num != EMBRYO_ERROR_NONE)
2029 if (num == EMBRYO_ERROR_SLEEP)
2033 ep->reset_stk = reset_stk;
2034 ep->reset_hea = reset_hea;
2036 return EMBRYO_PROGRAM_SLEEP;
2041 CASE(EMBRYO_OP_SYSREQ_C);
2043 /* save a few registers */
2044 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
2048 num = _embryo_native_call(ep, offs, &pri, (Embryo_Cell *)(data + (int)stk));
2049 if (num != EMBRYO_ERROR_NONE)
2051 if (num == EMBRYO_ERROR_SLEEP)
2055 ep->reset_stk = reset_stk;
2056 ep->reset_hea = reset_hea;
2058 return EMBRYO_PROGRAM_SLEEP;
2063 Embryo_Func_Stub *func_entry;
2065 hdr = (Embryo_Header *)ep->code;
2066 num = NUMENTRIES(hdr, natives, libraries);
2067 func_entry = GETENTRY(hdr, natives, 0);
2068 for (i = 0; i < num; i++)
2072 entry_name = GETENTRYNAME(hdr, func_entry);
2074 printf("EMBRYO: CALL [%i] %s() non-existent!\n", i, entry_name);
2076 (Embryo_Func_Stub *)((unsigned char *)func_entry + hdr->defsize);
2082 CASE(EMBRYO_OP_SYSREQ_D);
2084 /* save a few registers */
2085 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
2089 num = _embryo_native_call(ep, offs, &pri, (Embryo_Cell *)(data + (int)stk));
2090 if (num != EMBRYO_ERROR_NONE)
2092 if (num == EMBRYO_ERROR_SLEEP)
2096 ep->reset_stk = reset_stk;
2097 ep->reset_hea = reset_hea;
2099 return EMBRYO_PROGRAM_SLEEP;
2101 ABORT(ep, ep->error);
2104 CASE(EMBRYO_OP_JUMP_PRI);
2105 cip = (Embryo_Cell *)(code + (int)pri);
2107 CASE(EMBRYO_OP_SWITCH);
2111 /* +1, to skip the "casetbl" opcode */
2112 cptr = (Embryo_Cell *)(code + (*cip)) + 1;
2113 /* number of records in the case table */
2115 /* preset to "none-matched" case */
2116 cip = (Embryo_Cell *)(code + *(cptr + 1));
2118 (num > 0) && (*cptr != pri);
2122 cip = (Embryo_Cell *)(code + *(cptr + 1));
2125 CASE(EMBRYO_OP_SWAP_PRI);
2126 offs = *(Embryo_Cell *)(data + (int)stk);
2127 *(Embryo_Cell *)(data + (int)stk) = pri;
2130 CASE(EMBRYO_OP_SWAP_ALT);
2131 offs = *(Embryo_Cell *)(data + (int)stk);
2132 *(Embryo_Cell *)(data + (int)stk) = alt;
2135 CASE(EMBRYO_OP_PUSHADDR);
2139 CASE(EMBRYO_OP_NOP);
2141 CASE(EMBRYO_OP_NONE);
2142 CASE(EMBRYO_OP_FILE);
2143 CASE(EMBRYO_OP_LINE);
2144 CASE(EMBRYO_OP_SYMBOL);
2145 CASE(EMBRYO_OP_SRANGE);
2146 CASE(EMBRYO_OP_CASETBL);
2147 CASE(EMBRYO_OP_SYMTAG);
2149 #ifndef EMBRYO_EXEC_JUMPTABLE
2151 ABORT(ep, EMBRYO_ERROR_INVINSTR);
2155 ep->max_run_cycles = max_run_cycles;
2157 ep->hea = hea_start;
2158 return EMBRYO_PROGRAM_OK;
2162 * Retreives the return value of the last called function of the given
2164 * @param ep The given program.
2165 * @return An Embryo_Cell representing the return value of the function
2166 * that was last called.
2167 * @ingroup Embryo_Run_Group
2170 embryo_program_return_value_get(Embryo_Program *ep)
2177 * Sets the maximum number of abstract machine cycles any given program run
2178 * can execute before being put to sleep and returning.
2180 * @param ep The given program.
2181 * @param max The number of machine cycles as a limit.
2183 * This sets the maximum number of abstract machine (virtual machine)
2184 * instructions that a single run of an embryo function (even if its main)
2185 * can use before embryo embryo_program_run() reutrns with the value
2186 * EMBRYO_PROGRAM_TOOLONG. If the function fully executes within this number
2187 * of cycles, embryo_program_run() will return as normal with either
2188 * EMBRYO_PROGRAM_OK, EMBRYO_PROGRAM_FAIL or EMBRYO_PROGRAM_SLEEP. If the
2189 * run exceeds this instruction count, then EMBRYO_PROGRAM_TOOLONG will be
2190 * returned indicating the program exceeded its run count. If the app wishes
2191 * to continue running this anyway - it is free to process its own events or
2192 * whatever it wants and continue the function by calling
2193 * embryo_program_run(program, EMBRYO_FUNCTION_CONT); which will start the
2194 * run again until the instruction count is reached. This can keep being done
2195 * to allow the calling program to still be able to control things outside the
2196 * embryo function being called. If the maximum run cycle count is 0 then the
2197 * program is allowed to run forever only returning when it is done.
2199 * It is important to note that abstract machine cycles are NOT the same as
2200 * the host machine cpu cycles. They are not fixed in runtime per cycle, so
2201 * this is more of a helper tool than a way to HARD-FORCE a script to only
2202 * run for a specific period of time. If the cycle count is set to something
2203 * low like 5000 or 1000, then every 1000 (or 5000) cycles control will be
2204 * returned to the calling process where it can check a timer to see if a
2205 * physical runtime limit has been elapsed and then abort running further
2206 * assuming a "runaway script" or keep continuing the script run. This
2207 * limits resolution to only that many cycles which do not take a determined
2208 * amount of time to execute, as this varies from cpu to cpu and also depends
2209 * on how loaded the system is. Making the max cycle run too low will
2210 * impact performance requiring the abstract machine to do setup and teardown
2211 * cycles too often comapred to cycles actually executed.
2213 * Also note it does NOT include nested abstract machines. IF this abstract
2214 * machine run calls embryo script that calls a native function that in turn
2215 * calls more embryo script, then the 2nd (and so on) levels are not included
2216 * in this run count. They can set their own max instruction count values
2219 * The default max cycle run value is 0 in any program until set with this
2222 * @ingroup Embryo_Run_Group
2225 embryo_program_max_cycle_run_set(Embryo_Program *ep, int max)
2228 if (max < 0) max = 0;
2229 ep->max_run_cycles = max;
2233 * Retreives the maximum number of abstract machine cycles a program is allowed
2235 * @param ep The given program.
2236 * @return The number of cycles a run cycle is allowed to run for this
2239 * This returns the value set by embryo_program_max_cycle_run_set(). See
2240 * embryo_program_max_cycle_run_set() for more information.
2242 * @ingroup Embryo_Run_Group
2245 embryo_program_max_cycle_run_get(Embryo_Program *ep)
2248 return ep->max_run_cycles;
2252 * @defgroup Embryo_Parameter_Group Function Parameter Functions
2254 * Functions that set parameters for the next function that is called.
2258 * Pushes an Embryo_Cell onto the function stack to use as a parameter for
2259 * the next function that is called in the given program.
2260 * @param ep The given program.
2261 * @param cell The Embryo_Cell to push onto the stack.
2262 * @return @c 1 if successful. @c 0 otherwise.
2263 * @ingroup Embryo_Parameter_Group
2266 embryo_parameter_cell_push(Embryo_Program *ep, Embryo_Cell cell)
2271 if (ep->params_size > ep->params_alloc)
2273 ep->params_alloc += 8;
2274 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
2278 pr = &(ep->params[ep->params_size - 1]);
2280 pr->cell_array = NULL;
2281 pr->cell_array_size = 0;
2288 * Pushes a string onto the function stack to use as a parameter for the
2289 * next function that is called in the given program.
2290 * @param ep The given program.
2291 * @param str The string to push onto the stack.
2292 * @return @c 1 if successful. @c 0 otherwise.
2293 * @ingroup Embryo_Parameter_Group
2296 embryo_parameter_string_push(Embryo_Program *ep, const char *str)
2302 return embryo_parameter_string_push(ep, "");
2303 str_dup = strdup(str);
2304 if (!str_dup) return 0;
2306 if (ep->params_size > ep->params_alloc)
2308 ep->params_alloc += 8;
2309 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
2317 pr = &(ep->params[ep->params_size - 1]);
2319 pr->cell_array = NULL;
2320 pr->cell_array_size = 0;
2322 pr->string = str_dup;
2327 * Pushes an array of Embryo_Cells onto the function stack to be used as
2328 * parameters for the next function that is called in the given program.
2329 * @param ep The given program.
2330 * @param cells The array of Embryo_Cells.
2331 * @param num The number of cells in @p cells.
2332 * @return @c 1 if successful. @c 0 otherwise.
2333 * @ingroup Embryo_Parameter_Group
2336 embryo_parameter_cell_array_push(Embryo_Program *ep, Embryo_Cell *cells, int num)
2339 Embryo_Cell *cell_array;
2341 if ((!cells) || (num <= 0))
2342 return embryo_parameter_cell_push(ep, 0);
2343 cell_array = malloc(num * sizeof(Embryo_Cell));
2345 if (ep->params_size > ep->params_alloc)
2347 ep->params_alloc += 8;
2348 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
2356 pr = &(ep->params[ep->params_size - 1]);
2358 pr->cell_array = NULL;
2359 pr->cell_array_size = 0;
2361 pr->cell_array = cell_array;
2362 pr->cell_array_size = num;
2363 memcpy(pr->cell_array, cells, num * sizeof(Embryo_Cell));