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;
212 code_size = hdr->dat - hdr->cod;
213 code = (Embryo_Cell *)((unsigned char *)ep->code + (int)hdr->cod);
214 for (cip = 0; cip < (code_size / sizeof(Embryo_Cell)); cip++)
216 /* move this here - later we probably want something that verifies opcodes
217 * are valid and ok...
219 #ifdef WORDS_BIGENDIAN
220 embryo_swap_32(&(code[cip]));
225 /* init native api for handling floating point - default in embryo */
226 _embryo_args_init(ep);
228 _embryo_rand_init(ep);
229 _embryo_str_init(ep);
230 _embryo_time_init(ep);
234 /*** EXPORTED CALLS ***/
237 * @defgroup Embryo_Program_Creation_Group Program Creation and Destruction Functions
239 * Functions that set up programs, and destroy them.
243 * Creates a new Embryo program, with bytecode data that can be freed.
244 * @param data Pointer to the bytecode of the program.
245 * @param size Number of bytes of bytecode.
246 * @return A new Embryo program.
247 * @ingroup Embryo_Program_Creation_Group
249 EAPI Embryo_Program *
250 embryo_program_new(void *data, int size)
255 if (size < (int)sizeof(Embryo_Header)) return NULL;
257 ep = calloc(1, sizeof(Embryo_Program));
258 if (!ep) return NULL;
260 code_data = malloc(size);
266 memcpy(code_data, data, size);
267 if (_embryo_program_init(ep, code_data)) return ep;
274 * Creates a new Embryo program, with bytecode data that cannot be
276 * @param data Pointer to the bytecode of the program.
277 * @param size Number of bytes of bytecode.
278 * @return A new Embryo program.
279 * @ingroup Embryo_Program_Creation_Group
281 EAPI Embryo_Program *
282 embryo_program_const_new(void *data, int size)
286 if (size < (int)sizeof(Embryo_Header)) return NULL;
288 ep = calloc(1, sizeof(Embryo_Program));
289 if (!ep) return NULL;
291 if (_embryo_program_init(ep, data))
293 ep->dont_free_code = 1;
301 * Creates a new Embryo program based on the bytecode data stored in the
303 * @param file Filename of the given file.
304 * @return A new Embryo program.
305 * @ingroup Embryo_Program_Creation_Group
307 EAPI Embryo_Program *
308 embryo_program_load(char *file)
313 void *program = NULL;
314 int program_size = 0;
316 f = fopen(file, "rb");
318 fseek(f, 0, SEEK_END);
319 program_size = ftell(f);
321 if (program_size < (int)sizeof(Embryo_Header))
326 if (fread(&hdr, sizeof(Embryo_Header), 1, f) != 1)
332 #ifdef WORDS_BIGENDIAN
333 embryo_swap_32((unsigned int *)(&hdr.size));
335 if ((int)hdr.size < program_size) program_size = hdr.size;
336 program = malloc(program_size);
342 if (fread(program, program_size, 1, f) != 1)
348 ep = embryo_program_new(program, program_size);
355 * Frees the given Embryo program.
356 * @param ep The given program.
357 * @ingroup Embryo_Program_Creation_Group
360 embryo_program_free(Embryo_Program *ep)
364 if (ep->base) free(ep->base);
365 if ((!ep->dont_free_code) && (ep->code)) free(ep->code);
366 if (ep->native_calls) free(ep->native_calls);
367 for (i = 0; i < ep->params_size; i++)
369 if (ep->params[i].string) free(ep->params[i].string);
370 if (ep->params[i].cell_array) free(ep->params[i].cell_array);
372 if (ep->params) free(ep->params);
377 * @defgroup Embryo_Func_Group Function Functions
379 * Functions that deal with Embryo program functions.
383 * Adds a native program call to the given Embryo program.
384 * @param ep The given Embryo program.
385 * @param name The name for the call used in the script.
386 * @param func The function to use when the call is made.
387 * @ingroup Embryo_Func_Group
390 embryo_program_native_call_add(Embryo_Program *ep, const char *name, Embryo_Cell (*func) (Embryo_Program *ep, Embryo_Cell *params))
392 Embryo_Func_Stub *func_entry;
396 if ((!ep ) || (!name) || (!func)) return;
397 if (strlen(name) > sNAMEMAX) return;
399 hdr = (Embryo_Header *)ep->code;
400 if (hdr->defsize < 1) return;
401 num = NUMENTRIES(hdr, natives, libraries);
402 if (num <= 0) return;
404 ep->native_calls_size++;
405 if (ep->native_calls_size > ep->native_calls_alloc)
407 Embryo_Native *calls;
409 ep->native_calls_alloc += 32;
410 calls = realloc(ep->native_calls,
411 ep->native_calls_alloc * sizeof(Embryo_Native));
414 ep->native_calls_size--;
415 ep->native_calls_alloc -= 32;
418 ep->native_calls = calls;
420 ep->native_calls[ep->native_calls_size - 1] = func;
422 func_entry = GETENTRY(hdr, natives, 0);
423 for (i = 0; i < num; i++)
425 if (func_entry->address == 0)
429 entry_name = GETENTRYNAME(hdr, func_entry);
430 if ((entry_name) && (!strcmp(entry_name, name)))
432 func_entry->address = ep->native_calls_size;
433 /* FIXME: embryo_cc is putting in multiple native */
434 /* function call entries - so we need to fill in all */
440 (Embryo_Func_Stub *)((unsigned char *)func_entry + hdr->defsize);
445 * @defgroup Embryo_Program_VM_Group Virtual Machine Functions
447 * Functions that deal with creating and destroying virtual machine sessions
448 * for a given program.
450 * A given embryo program can have multiple virtual machine sessions running.
451 * This is useful when you have a native call that in turn calls a function in
452 * the embryo program. The native call can start a new virtual machine
453 * session to run the function it needs. Once completed, the session can be
454 * popped off the program's stack, and the native call can return its value
455 * to the old session.
457 * A new virtual machine session is created by pushing a new virtual machine
458 * onto the session stack of a program using @ref embryo_program_vm_push.
459 * The current virtual machine session can be destroyed by calling
460 * @ref embryo_program_vm_pop.
464 * Resets the current virtual machine session of the given program.
465 * @param ep The given program.
466 * @ingroup Embryo_Program_VM_Group
469 embryo_program_vm_reset(Embryo_Program *ep)
473 if ((!ep) || (!ep->base)) return;
474 hdr = (Embryo_Header *)ep->code;
475 memcpy(ep->base, hdr, hdr->size);
476 *(Embryo_Cell *)(ep->base + (int)hdr->stp - sizeof(Embryo_Cell)) = 0;
478 ep->hlw = hdr->hea - hdr->dat; /* stack and heap relative to data segment */
479 ep->stp = hdr->stp - hdr->dat - sizeof(Embryo_Cell);
485 * Starts a new virtual machine session for the given program.
487 * See @ref Embryo_Program_VM_Group for more information about how this works.
489 * @param ep The given program.
490 * @ingroup Embryo_Program_VM_Group
493 embryo_program_vm_push(Embryo_Program *ep)
501 embryo_program_vm_reset(ep);
504 hdr = (Embryo_Header *)ep->code;
505 ep->base = malloc(hdr->stp);
511 embryo_program_vm_reset(ep);
515 * Frees the current virtual machine session associated with the given program.
517 * See @ref Embryo_Program_VM_Group for more information about how this works.
518 * Note that you will need to retrieve any return data or data on the stack
521 * @param ep The given program.
522 * @ingroup Embryo_Program_VM_Group
525 embryo_program_vm_pop(Embryo_Program *ep)
527 if ((!ep) || (!ep->base)) return;
529 if (ep->pushes >= 1) return;
535 * @defgroup Embryo_Swap_Group Byte Swapping Functions
537 * Functions that are used to ensure that integers passed to the
538 * virtual machine are in small endian format. These functions are
539 * used to ensure that the virtual machine operates correctly on big
544 * Ensures that the given unsigned short integer is in the small
546 * @param v Pointer to the given integer.
547 * @ingroup Embryo_Swap_Group
550 embryo_swap_16(unsigned short *v)
552 #ifdef WORDS_BIGENDIAN
553 _embryo_byte_swap_16(v);
558 * Ensures that the given unsigned integer is in the small endian
560 * @param v Pointer to the given integer.
561 * @ingroup Embryo_Swap_Group
564 embryo_swap_32(unsigned int *v)
566 #ifdef WORDS_BIGENDIAN
567 _embryo_byte_swap_32(v);
572 * Returns the function in the given program with the given name.
573 * @param ep The given program.
574 * @param name The given function name.
575 * @return The function if successful. Otherwise, @c EMBRYO_FUNCTION_NONE.
576 * @ingroup Embryo_Func_Group
579 embryo_program_function_find(Embryo_Program *ep, const char *name)
581 int first, last, mid, result;
582 char pname[sNAMEMAX + 1];
585 if (!ep) return EMBRYO_FUNCTION_NONE;
586 hdr = (Embryo_Header *)ep->code;
587 last = NUMENTRIES(hdr, publics, natives) - 1;
590 while (first <= last)
592 mid = (first + last) / 2;
593 if (_embryo_func_get(ep, mid, pname) == EMBRYO_ERROR_NONE)
594 result = strcmp(pname, name);
596 return EMBRYO_FUNCTION_NONE;
598 if (result > 0) last = mid - 1;
599 else if (result < 0) first = mid + 1;
602 return EMBRYO_FUNCTION_NONE;
606 * @defgroup Embryo_Public_Variable_Group Public Variable Access Functions
608 * In an Embryo program, a global variable can be declared public, as
609 * described in @ref Small_Scope_Subsection. The functions here allow
610 * the host program to access these public variables.
614 * Retrieves the location of the public variable in the given program
615 * with the given name.
616 * @param ep The given program.
617 * @param name The given name.
618 * @return The address of the variable if found. @c EMBRYO_CELL_NONE
620 * @ingroup Embryo_Public_Variable_Group
623 embryo_program_variable_find(Embryo_Program *ep, const char *name)
625 int first, last, mid, result;
626 char pname[sNAMEMAX + 1];
630 if (!ep) return EMBRYO_CELL_NONE;
631 if (!ep->base) return EMBRYO_CELL_NONE;
632 hdr = (Embryo_Header *)ep->base;
633 last = NUMENTRIES(hdr, pubvars, tags) - 1;
636 while (first <= last)
638 mid = (first + last) / 2;
639 if (_embryo_var_get(ep, mid, pname, &paddr) == EMBRYO_ERROR_NONE)
640 result = strcmp(pname, name);
642 return EMBRYO_CELL_NONE;
644 if (result > 0) last = mid - 1;
645 else if (result < 0) first = mid + 1;
648 return EMBRYO_CELL_NONE;
652 * Retrieves the number of public variables in the given program.
653 * @param ep The given program.
654 * @return The number of public variables.
655 * @ingroup Embryo_Public_Variable_Group
658 embryo_program_variable_count_get(Embryo_Program *ep)
663 if (!ep->base) return 0;
664 hdr = (Embryo_Header *)ep->base;
665 return NUMENTRIES(hdr, pubvars, tags);
669 * Retrieves the location of the public variable in the given program
670 * with the given identifier.
671 * @param ep The given program.
672 * @param num The identifier of the public variable.
673 * @return The virtual machine address of the variable if found.
674 * @c EMBRYO_CELL_NONE otherwise.
675 * @ingroup Embryo_Public_Variable_Group
678 embryo_program_variable_get(Embryo_Program *ep, int num)
681 char pname[sNAMEMAX + 1];
683 if (!ep) return EMBRYO_CELL_NONE;
684 if (!ep->base) return EMBRYO_CELL_NONE;
685 if (_embryo_var_get(ep, num, pname, &paddr) == EMBRYO_ERROR_NONE)
687 return EMBRYO_CELL_NONE;
691 * @defgroup Embryo_Error_Group Error Functions
693 * Functions that set and retrieve error codes in Embryo programs.
697 * Sets the error code for the given program to the given code.
698 * @param ep The given program.
699 * @param error The given error code.
700 * @ingroup Embryo_Error_Group
703 embryo_program_error_set(Embryo_Program *ep, int error)
710 * Retrieves the current error code for the given program.
711 * @param ep The given program.
712 * @return The current error code.
713 * @ingroup Embryo_Error_Group
716 embryo_program_error_get(Embryo_Program *ep)
718 if (!ep) return EMBRYO_ERROR_NONE;
723 * @defgroup Embryo_Program_Data_Group Program Data Functions
725 * Functions that set and retrieve data associated with the given
730 * Sets the data associated to the given program.
731 * @param ep The given program.
732 * @param data New bytecode data.
733 * @ingroup Embryo_Program_Data_Group
736 embryo_program_data_set(Embryo_Program *ep, void *data)
743 * Retrieves the data associated to the given program.
744 * @param ep The given program.
745 * @ingroup Embryo_Program_Data_Group
748 embryo_program_data_get(Embryo_Program *ep)
750 if (!ep) return NULL;
755 * Retrieves a string describing the given error code.
756 * @param error The given error code.
757 * @return String describing the given error code. If the given code is not
758 * known, the string "(unknown)" is returned.
759 * @ingroup Embryo_Error_Group
762 embryo_error_string_get(int error)
764 const char *messages[] =
766 /* EMBRYO_ERROR_NONE */ "(none)",
767 /* EMBRYO_ERROR_EXIT */ "Forced exit",
768 /* EMBRYO_ERROR_ASSERT */ "Assertion failed",
769 /* EMBRYO_ERROR_STACKERR */ "Stack/heap collision (insufficient stack size)",
770 /* EMBRYO_ERROR_BOUNDS */ "Array index out of bounds",
771 /* EMBRYO_ERROR_MEMACCESS */ "Invalid memory access",
772 /* EMBRYO_ERROR_INVINSTR */ "Invalid instruction",
773 /* EMBRYO_ERROR_STACKLOW */ "Stack underflow",
774 /* EMBRYO_ERROR_HEAPLOW */ "Heap underflow",
775 /* EMBRYO_ERROR_CALLBACK */ "No (valid) native function callback",
776 /* EMBRYO_ERROR_NATIVE */ "Native function failed",
777 /* EMBRYO_ERROR_DIVIDE */ "Divide by zero",
778 /* EMBRYO_ERROR_SLEEP */ "(sleep mode)",
779 /* 13 */ "(reserved)",
780 /* 14 */ "(reserved)",
781 /* 15 */ "(reserved)",
782 /* EMBRYO_ERROR_MEMORY */ "Out of memory",
783 /* EMBRYO_ERROR_FORMAT */ "Invalid/unsupported P-code file format",
784 /* EMBRYO_ERROR_VERSION */ "File is for a newer version of the Embryo_Program",
785 /* EMBRYO_ERROR_NOTFOUND */ "Native/Public function is not found",
786 /* EMBRYO_ERROR_INDEX */ "Invalid index parameter (bad entry point)",
787 /* EMBRYO_ERROR_DEBUG */ "Debugger cannot run",
788 /* EMBRYO_ERROR_INIT */ "Embryo_Program not initialized (or doubly initialized)",
789 /* EMBRYO_ERROR_USERDATA */ "Unable to set user data field (table full)",
790 /* EMBRYO_ERROR_INIT_JIT */ "Cannot initialize the JIT",
791 /* EMBRYO_ERROR_PARAMS */ "Parameter error",
793 if ((error < 0) || (error >= (int)(sizeof(messages) / sizeof(messages[0]))))
794 return (const char *)"(unknown)";
795 return messages[error];
799 * @defgroup Embryo_Data_String_Group Embryo Data String Functions
801 * Functions that operate on strings in the memory of a virtual machine.
805 * Retrieves the length of the string starting at the given cell.
806 * @param ep The program the cell is part of.
807 * @param str_cell Pointer to the first cell of the string.
808 * @return The length of the string. @c 0 is returned if there is an error.
809 * @ingroup Embryo_Data_String_Group
812 embryo_data_string_length_get(Embryo_Program *ep, Embryo_Cell *str_cell)
817 if ((!ep) || (!ep->base)) return 0;
818 hdr = (Embryo_Header *)ep->base;
820 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
821 ((void *)str_cell < (void *)ep->base))
823 for (len = 0; str_cell[len] != 0; len++);
828 * Copies the string starting at the given cell to the given buffer.
829 * @param ep The program the cell is part of.
830 * @param str_cell Pointer to the first cell of the string.
831 * @param dst The given buffer.
832 * @ingroup Embryo_Data_String_Group
835 embryo_data_string_get(Embryo_Program *ep, Embryo_Cell *str_cell, char *dst)
841 if ((!ep) || (!ep->base))
846 hdr = (Embryo_Header *)ep->base;
848 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
849 ((void *)str_cell < (void *)ep->base))
854 for (i = 0; str_cell[i] != 0; i++)
856 #ifdef WORDS_BIGENDIAN
861 _embryo_byte_swap_32(&tmp);
865 dst[i] = str_cell[i];
872 * Copies string in the given buffer into the virtual machine memory
873 * starting at the given cell.
874 * @param ep The program the cell is part of.
875 * @param src The given buffer.
876 * @param str_cell Pointer to the first cell to copy the string to.
877 * @ingroup Embryo_Data_String_Group
880 embryo_data_string_set(Embryo_Program *ep, const char *src, Embryo_Cell *str_cell)
886 if (!ep->base) return;
887 hdr = (Embryo_Header *)ep->base;
889 ((void *)str_cell >= (void *)(ep->base + hdr->stp)) ||
890 ((void *)str_cell < (void *)ep->base))
897 for (i = 0; src[i] != 0; i++)
899 if ((void *)(&(str_cell[i])) >= (void *)(ep->base + hdr->stp)) return;
900 else if ((void *)(&(str_cell[i])) == (void *)(ep->base + hdr->stp - 1))
905 #ifdef WORDS_BIGENDIAN
910 _embryo_byte_swap_32(&tmp);
914 str_cell[i] = src[i];
921 * Retreives a pointer to the address in the virtual machine given by the
923 * @param ep The program whose virtual machine address is being queried.
924 * @param addr The given cell.
925 * @return A pointer to the cell at the given address.
926 * @ingroup Embryo_Data_String_Group
929 embryo_data_address_get(Embryo_Program *ep, Embryo_Cell addr)
934 if ((!ep) || (!ep->base)) return NULL;
935 hdr = (Embryo_Header *)ep->base;
936 data = ep->base + (int)hdr->dat;
937 if ((addr < 0) || (addr >= hdr->stp)) return NULL;
938 return (Embryo_Cell *)(data + (int)addr);
942 * @defgroup Embryo_Heap_Group Heap Functions
944 * The heap is an area of memory that can be allocated for program
945 * use at runtime. The heap functions here change the amount of heap
950 * Increases the size of the heap of the given virtual machine by the given
951 * number of Embryo_Cells.
952 * @param ep The program with the given virtual machine.
953 * @param cells The given number of Embryo_Cells.
954 * @return The address of the new memory region on success.
955 * @c EMBRYO_CELL_NONE otherwise.
956 * @ingroup Embryo_Heap_Group
959 embryo_data_heap_push(Embryo_Program *ep, int cells)
964 if ((!ep) || (!ep->base)) return EMBRYO_CELL_NONE;
965 hdr = (Embryo_Header *)ep->base;
966 if (ep->stk - ep->hea - (cells * sizeof(Embryo_Cell)) < STKMARGIN)
967 return EMBRYO_CELL_NONE;
969 ep->hea += (cells * sizeof(Embryo_Cell));
974 * Decreases the size of the heap of the given virtual machine down to the
976 * @param ep The program with the given virtual machine.
977 * @param down_to The given size.
978 * @ingroup Embryo_Heap_Group
981 embryo_data_heap_pop(Embryo_Program *ep, Embryo_Cell down_to)
984 if (down_to < 0) down_to = 0;
985 if (ep->hea > down_to) ep->hea = down_to;
989 * @defgroup Embryo_Run_Group Program Run Functions
991 * Functions that are involved in actually running functions in an
996 * Returns the number of virtual machines are running for the given program.
997 * @param ep The given program.
998 * @return The number of virtual machines running.
999 * @ingroup Embryo_Run_Group
1002 embryo_program_recursion_get(Embryo_Program *ep)
1004 return ep->run_count;
1009 #define EMBRYO_EXEC_JUMPTABLE
1013 /* jump table optimization - only works for gcc though */
1014 #ifdef EMBRYO_EXEC_JUMPTABLE
1015 #define SWITCH(x) while (1) { goto *switchtable[x];
1016 #define SWITCHEND break; }
1017 #define CASE(x) SWITCHTABLE_##x:
1018 #define BREAK break;
1020 #define SWITCH(x) switch (x) {
1022 #define CASE(x) case x:
1027 * Runs the given function of the given Embryo program in the current
1028 * virtual machine. The parameter @p fn can be found using
1029 * @ref embryo_program_function_find.
1031 * @note For Embryo to be able to run a function, it must have been
1032 * declared @c public in the Small source code.
1034 * @param ep The given program.
1035 * @param fn The given function. Normally "main", in which case the
1036 * constant @c EMBRYO_FUNCTION_MAIN can be used.
1037 * @return @c EMBRYO_PROGRAM_OK on success. @c EMBRYO_PROGRAM_SLEEP if the
1038 * program is halted by the Small @c sleep call.
1039 * @c EMBRYO_PROGRAM_FAIL if there is an error.
1040 * @c EMBRYO_PROGRAM_TOOLONG if the program executes for longer than
1041 * it is allowed to in abstract machine instruction count.
1042 * @ingroup Embryo_Run_Group
1045 embryo_program_run(Embryo_Program *ep, Embryo_Function fn)
1048 Embryo_Func_Stub *func;
1049 unsigned char *code, *data;
1050 Embryo_Cell pri, alt, stk, frm, hea, hea_start;
1051 Embryo_Cell reset_stk, reset_hea, *cip;
1052 Embryo_UCell codesize;
1059 #ifdef EMBRYO_EXEC_JUMPTABLE
1060 /* we limit the jumptable to 256 elements. why? above we forced "op" to be
1061 * a unsigned char - that means 256 max values. we limit opcode overflow
1062 * here, so eliminating crashes on table lookups with bad/corrupt bytecode.
1063 * no need to atuall do compares, branches etc. the datatype does the work
1064 * for us. so that means EXCESS elements are all declared as OP_NONE to
1065 * keep them innocuous.
1067 static const void *switchtable[256] =
1069 &&SWITCHTABLE_EMBRYO_OP_NONE,
1070 &&SWITCHTABLE_EMBRYO_OP_LOAD_PRI,
1071 &&SWITCHTABLE_EMBRYO_OP_LOAD_ALT,
1072 &&SWITCHTABLE_EMBRYO_OP_LOAD_S_PRI,
1073 &&SWITCHTABLE_EMBRYO_OP_LOAD_S_ALT,
1074 &&SWITCHTABLE_EMBRYO_OP_LREF_PRI,
1075 &&SWITCHTABLE_EMBRYO_OP_LREF_ALT,
1076 &&SWITCHTABLE_EMBRYO_OP_LREF_S_PRI,
1077 &&SWITCHTABLE_EMBRYO_OP_LREF_S_ALT,
1078 &&SWITCHTABLE_EMBRYO_OP_LOAD_I,
1079 &&SWITCHTABLE_EMBRYO_OP_LODB_I,
1080 &&SWITCHTABLE_EMBRYO_OP_CONST_PRI,
1081 &&SWITCHTABLE_EMBRYO_OP_CONST_ALT,
1082 &&SWITCHTABLE_EMBRYO_OP_ADDR_PRI,
1083 &&SWITCHTABLE_EMBRYO_OP_ADDR_ALT,
1084 &&SWITCHTABLE_EMBRYO_OP_STOR_PRI,
1085 &&SWITCHTABLE_EMBRYO_OP_STOR_ALT,
1086 &&SWITCHTABLE_EMBRYO_OP_STOR_S_PRI,
1087 &&SWITCHTABLE_EMBRYO_OP_STOR_S_ALT,
1088 &&SWITCHTABLE_EMBRYO_OP_SREF_PRI,
1089 &&SWITCHTABLE_EMBRYO_OP_SREF_ALT,
1090 &&SWITCHTABLE_EMBRYO_OP_SREF_S_PRI,
1091 &&SWITCHTABLE_EMBRYO_OP_SREF_S_ALT,
1092 &&SWITCHTABLE_EMBRYO_OP_STOR_I,
1093 &&SWITCHTABLE_EMBRYO_OP_STRB_I,
1094 &&SWITCHTABLE_EMBRYO_OP_LIDX,
1095 &&SWITCHTABLE_EMBRYO_OP_LIDX_B,
1096 &&SWITCHTABLE_EMBRYO_OP_IDXADDR,
1097 &&SWITCHTABLE_EMBRYO_OP_IDXADDR_B,
1098 &&SWITCHTABLE_EMBRYO_OP_ALIGN_PRI,
1099 &&SWITCHTABLE_EMBRYO_OP_ALIGN_ALT,
1100 &&SWITCHTABLE_EMBRYO_OP_LCTRL,
1101 &&SWITCHTABLE_EMBRYO_OP_SCTRL,
1102 &&SWITCHTABLE_EMBRYO_OP_MOVE_PRI,
1103 &&SWITCHTABLE_EMBRYO_OP_MOVE_ALT,
1104 &&SWITCHTABLE_EMBRYO_OP_XCHG,
1105 &&SWITCHTABLE_EMBRYO_OP_PUSH_PRI,
1106 &&SWITCHTABLE_EMBRYO_OP_PUSH_ALT,
1107 &&SWITCHTABLE_EMBRYO_OP_PUSH_R,
1108 &&SWITCHTABLE_EMBRYO_OP_PUSH_C,
1109 &&SWITCHTABLE_EMBRYO_OP_PUSH,
1110 &&SWITCHTABLE_EMBRYO_OP_PUSH_S,
1111 &&SWITCHTABLE_EMBRYO_OP_POP_PRI,
1112 &&SWITCHTABLE_EMBRYO_OP_POP_ALT,
1113 &&SWITCHTABLE_EMBRYO_OP_STACK,
1114 &&SWITCHTABLE_EMBRYO_OP_HEAP,
1115 &&SWITCHTABLE_EMBRYO_OP_PROC,
1116 &&SWITCHTABLE_EMBRYO_OP_RET,
1117 &&SWITCHTABLE_EMBRYO_OP_RETN,
1118 &&SWITCHTABLE_EMBRYO_OP_CALL,
1119 &&SWITCHTABLE_EMBRYO_OP_CALL_PRI,
1120 &&SWITCHTABLE_EMBRYO_OP_JUMP,
1121 &&SWITCHTABLE_EMBRYO_OP_JREL,
1122 &&SWITCHTABLE_EMBRYO_OP_JZER,
1123 &&SWITCHTABLE_EMBRYO_OP_JNZ,
1124 &&SWITCHTABLE_EMBRYO_OP_JEQ,
1125 &&SWITCHTABLE_EMBRYO_OP_JNEQ,
1126 &&SWITCHTABLE_EMBRYO_OP_JLESS,
1127 &&SWITCHTABLE_EMBRYO_OP_JLEQ,
1128 &&SWITCHTABLE_EMBRYO_OP_JGRTR,
1129 &&SWITCHTABLE_EMBRYO_OP_JGEQ,
1130 &&SWITCHTABLE_EMBRYO_OP_JSLESS,
1131 &&SWITCHTABLE_EMBRYO_OP_JSLEQ,
1132 &&SWITCHTABLE_EMBRYO_OP_JSGRTR,
1133 &&SWITCHTABLE_EMBRYO_OP_JSGEQ,
1134 &&SWITCHTABLE_EMBRYO_OP_SHL,
1135 &&SWITCHTABLE_EMBRYO_OP_SHR,
1136 &&SWITCHTABLE_EMBRYO_OP_SSHR,
1137 &&SWITCHTABLE_EMBRYO_OP_SHL_C_PRI,
1138 &&SWITCHTABLE_EMBRYO_OP_SHL_C_ALT,
1139 &&SWITCHTABLE_EMBRYO_OP_SHR_C_PRI,
1140 &&SWITCHTABLE_EMBRYO_OP_SHR_C_ALT,
1141 &&SWITCHTABLE_EMBRYO_OP_SMUL,
1142 &&SWITCHTABLE_EMBRYO_OP_SDIV,
1143 &&SWITCHTABLE_EMBRYO_OP_SDIV_ALT,
1144 &&SWITCHTABLE_EMBRYO_OP_UMUL,
1145 &&SWITCHTABLE_EMBRYO_OP_UDIV,
1146 &&SWITCHTABLE_EMBRYO_OP_UDIV_ALT,
1147 &&SWITCHTABLE_EMBRYO_OP_ADD,
1148 &&SWITCHTABLE_EMBRYO_OP_SUB,
1149 &&SWITCHTABLE_EMBRYO_OP_SUB_ALT,
1150 &&SWITCHTABLE_EMBRYO_OP_AND,
1151 &&SWITCHTABLE_EMBRYO_OP_OR,
1152 &&SWITCHTABLE_EMBRYO_OP_XOR,
1153 &&SWITCHTABLE_EMBRYO_OP_NOT,
1154 &&SWITCHTABLE_EMBRYO_OP_NEG,
1155 &&SWITCHTABLE_EMBRYO_OP_INVERT,
1156 &&SWITCHTABLE_EMBRYO_OP_ADD_C,
1157 &&SWITCHTABLE_EMBRYO_OP_SMUL_C,
1158 &&SWITCHTABLE_EMBRYO_OP_ZERO_PRI,
1159 &&SWITCHTABLE_EMBRYO_OP_ZERO_ALT,
1160 &&SWITCHTABLE_EMBRYO_OP_ZERO,
1161 &&SWITCHTABLE_EMBRYO_OP_ZERO_S,
1162 &&SWITCHTABLE_EMBRYO_OP_SIGN_PRI,
1163 &&SWITCHTABLE_EMBRYO_OP_SIGN_ALT,
1164 &&SWITCHTABLE_EMBRYO_OP_EQ,
1165 &&SWITCHTABLE_EMBRYO_OP_NEQ,
1166 &&SWITCHTABLE_EMBRYO_OP_LESS,
1167 &&SWITCHTABLE_EMBRYO_OP_LEQ,
1168 &&SWITCHTABLE_EMBRYO_OP_GRTR,
1169 &&SWITCHTABLE_EMBRYO_OP_GEQ,
1170 &&SWITCHTABLE_EMBRYO_OP_SLESS,
1171 &&SWITCHTABLE_EMBRYO_OP_SLEQ,
1172 &&SWITCHTABLE_EMBRYO_OP_SGRTR,
1173 &&SWITCHTABLE_EMBRYO_OP_SGEQ,
1174 &&SWITCHTABLE_EMBRYO_OP_EQ_C_PRI,
1175 &&SWITCHTABLE_EMBRYO_OP_EQ_C_ALT,
1176 &&SWITCHTABLE_EMBRYO_OP_INC_PRI,
1177 &&SWITCHTABLE_EMBRYO_OP_INC_ALT,
1178 &&SWITCHTABLE_EMBRYO_OP_INC,
1179 &&SWITCHTABLE_EMBRYO_OP_INC_S,
1180 &&SWITCHTABLE_EMBRYO_OP_INC_I,
1181 &&SWITCHTABLE_EMBRYO_OP_DEC_PRI,
1182 &&SWITCHTABLE_EMBRYO_OP_DEC_ALT,
1183 &&SWITCHTABLE_EMBRYO_OP_DEC,
1184 &&SWITCHTABLE_EMBRYO_OP_DEC_S,
1185 &&SWITCHTABLE_EMBRYO_OP_DEC_I,
1186 &&SWITCHTABLE_EMBRYO_OP_MOVS,
1187 &&SWITCHTABLE_EMBRYO_OP_CMPS,
1188 &&SWITCHTABLE_EMBRYO_OP_FILL,
1189 &&SWITCHTABLE_EMBRYO_OP_HALT,
1190 &&SWITCHTABLE_EMBRYO_OP_BOUNDS,
1191 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_PRI,
1192 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_C,
1193 &&SWITCHTABLE_EMBRYO_OP_FILE,
1194 &&SWITCHTABLE_EMBRYO_OP_LINE,
1195 &&SWITCHTABLE_EMBRYO_OP_SYMBOL,
1196 &&SWITCHTABLE_EMBRYO_OP_SRANGE,
1197 &&SWITCHTABLE_EMBRYO_OP_JUMP_PRI,
1198 &&SWITCHTABLE_EMBRYO_OP_SWITCH,
1199 &&SWITCHTABLE_EMBRYO_OP_CASETBL,
1200 &&SWITCHTABLE_EMBRYO_OP_SWAP_PRI,
1201 &&SWITCHTABLE_EMBRYO_OP_SWAP_ALT,
1202 &&SWITCHTABLE_EMBRYO_OP_PUSHADDR,
1203 &&SWITCHTABLE_EMBRYO_OP_NOP,
1204 &&SWITCHTABLE_EMBRYO_OP_SYSREQ_D,
1205 &&SWITCHTABLE_EMBRYO_OP_SYMTAG,
1206 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1207 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1208 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1209 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1210 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1211 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1212 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1213 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
1214 &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE, &&SWITCHTABLE_EMBRYO_OP_NONE,
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
1232 if (!ep) return EMBRYO_PROGRAM_FAIL;
1233 if (!(ep->flags & EMBRYO_FLAG_RELOC))
1235 ep->error = EMBRYO_ERROR_INIT;
1236 return EMBRYO_PROGRAM_FAIL;
1240 ep->error = EMBRYO_ERROR_INIT;
1241 return EMBRYO_PROGRAM_FAIL;
1243 if (ep->run_count > 0)
1245 /* return EMBRYO_PROGRAM_BUSY; */
1246 /* FIXME: test C->vm->C->vm recursion more fully */
1247 /* it seems to work... just fine!!! - strange! */
1250 /* set up the registers */
1251 hdr = (Embryo_Header *)ep->base;
1252 codesize = (Embryo_UCell)(hdr->dat - hdr->cod);
1253 code = ep->base + (int)hdr->cod;
1254 data = ep->base + (int)hdr->dat;
1255 hea_start = hea = ep->hea;
1259 frm = alt = pri = 0;
1261 /* get the start address */
1262 if (fn == EMBRYO_FUNCTION_MAIN)
1266 ep->error = EMBRYO_ERROR_INDEX;
1267 return EMBRYO_PROGRAM_FAIL;
1269 cip = (Embryo_Cell *)(code + (int)hdr->cip);
1271 else if (fn == EMBRYO_FUNCTION_CONT)
1273 /* all registers: pri, alt, frm, cip, hea, stk, reset_stk, reset_hea */
1279 reset_stk = ep->reset_stk;
1280 reset_hea = ep->reset_hea;
1281 cip = (Embryo_Cell *)(code + (int)ep->cip);
1285 ep->error = EMBRYO_ERROR_INDEX;
1286 return EMBRYO_PROGRAM_FAIL;
1290 if (fn >= (Embryo_Cell)NUMENTRIES(hdr, publics, natives))
1292 ep->error = EMBRYO_ERROR_INDEX;
1293 return EMBRYO_PROGRAM_FAIL;
1295 func = GETENTRY(hdr, publics, fn);
1296 cip = (Embryo_Cell *)(code + (int)func->address);
1298 /* check values just copied */
1302 if (fn != EMBRYO_FUNCTION_CONT)
1306 for (i = ep->params_size - 1; i >= 0; i--)
1310 pr = &(ep->params[i]);
1314 Embryo_Cell ep_addr, *addr;
1316 len = strlen(pr->string);
1317 ep_addr = embryo_data_heap_push(ep, len + 1);
1318 if (ep_addr == EMBRYO_CELL_NONE)
1320 ep->error = EMBRYO_ERROR_HEAPLOW;
1321 return EMBRYO_PROGRAM_FAIL;
1323 addr = embryo_data_address_get(ep, ep_addr);
1325 embryo_data_string_set(ep, pr->string, addr);
1328 ep->error = EMBRYO_ERROR_HEAPLOW;
1329 return EMBRYO_PROGRAM_FAIL;
1334 else if (pr->cell_array)
1337 Embryo_Cell ep_addr, *addr;
1339 len = pr->cell_array_size;
1340 ep_addr = embryo_data_heap_push(ep, len + 1);
1341 if (ep_addr == EMBRYO_CELL_NONE)
1343 ep->error = EMBRYO_ERROR_HEAPLOW;
1344 return EMBRYO_PROGRAM_FAIL;
1346 addr = embryo_data_address_get(ep, ep_addr);
1348 memcpy(addr, pr->cell_array,
1349 pr->cell_array_size * sizeof(Embryo_Cell));
1352 ep->error = EMBRYO_ERROR_HEAPLOW;
1353 return EMBRYO_PROGRAM_FAIL;
1356 free(pr->cell_array);
1363 PUSH(ep->params_size * sizeof(Embryo_Cell));
1370 ep->params_size = ep->params_alloc = 0;
1372 /* check stack/heap before starting to run */
1375 /* track recursion depth */
1378 max_run_cycles = ep->max_run_cycles;
1380 for (cycle_count = 0;;)
1382 if (max_run_cycles > 0)
1384 if (cycle_count >= max_run_cycles)
1390 op = (Embryo_Opcode)*cip++;
1392 CASE(EMBRYO_OP_LOAD_PRI);
1394 pri = *(Embryo_Cell *)(data + (int)offs);
1396 CASE(EMBRYO_OP_LOAD_ALT);
1398 alt = *(Embryo_Cell *)(data + (int)offs);
1400 CASE(EMBRYO_OP_LOAD_S_PRI);
1402 pri = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1404 CASE(EMBRYO_OP_LOAD_S_ALT);
1406 alt = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1408 CASE(EMBRYO_OP_LREF_PRI);
1410 offs = *(Embryo_Cell *)(data + (int)offs);
1411 pri = *(Embryo_Cell *)(data + (int)offs);
1413 CASE(EMBRYO_OP_LREF_ALT);
1415 offs = *(Embryo_Cell *)(data + (int)offs);
1416 alt = *(Embryo_Cell *)(data + (int)offs);
1418 CASE(EMBRYO_OP_LREF_S_PRI);
1420 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1421 pri = *(Embryo_Cell *)(data + (int)offs);
1423 CASE(EMBRYO_OP_LREF_S_ALT);
1425 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1426 alt = *(Embryo_Cell *)(data + (int)offs);
1428 CASE(EMBRYO_OP_LOAD_I);
1430 pri = *(Embryo_Cell *)(data + (int)pri);
1432 CASE(EMBRYO_OP_LODB_I);
1438 pri = *(data + (int)pri);
1441 pri = *(unsigned short *)(data + (int)pri);
1444 pri = *(unsigned int *)(data + (int)pri);
1447 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1451 CASE(EMBRYO_OP_CONST_PRI);
1454 CASE(EMBRYO_OP_CONST_ALT);
1457 CASE(EMBRYO_OP_ADDR_PRI);
1461 CASE(EMBRYO_OP_ADDR_ALT);
1465 CASE(EMBRYO_OP_STOR_PRI);
1467 *(Embryo_Cell *)(data + (int)offs) = pri;
1469 CASE(EMBRYO_OP_STOR_ALT);
1471 *(Embryo_Cell *)(data + (int)offs) = alt;
1473 CASE(EMBRYO_OP_STOR_S_PRI);
1475 *(Embryo_Cell *)(data + (int)frm + (int)offs) = pri;
1477 CASE(EMBRYO_OP_STOR_S_ALT);
1479 *(Embryo_Cell *)(data + (int)frm + (int)offs) = alt;
1481 CASE(EMBRYO_OP_SREF_PRI);
1483 offs = *(Embryo_Cell *)(data + (int)offs);
1484 *(Embryo_Cell *)(data + (int)offs) = pri;
1486 CASE(EMBRYO_OP_SREF_ALT);
1488 offs = *(Embryo_Cell *)(data + (int)offs);
1489 *(Embryo_Cell *)(data + (int)offs) = alt;
1491 CASE(EMBRYO_OP_SREF_S_PRI);
1493 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1494 *(Embryo_Cell *)(data + (int)offs) = pri;
1496 CASE(EMBRYO_OP_SREF_S_ALT);
1498 offs = *(Embryo_Cell *)(data + (int)frm + (int)offs);
1499 *(Embryo_Cell *)(data + (int)offs) = alt;
1501 CASE(EMBRYO_OP_STOR_I);
1503 *(Embryo_Cell *)(data + (int)alt) = pri;
1505 CASE(EMBRYO_OP_STRB_I);
1511 *(data + (int)alt) = (unsigned char)pri;
1514 *(unsigned short *)(data + (int)alt) = (unsigned short)pri;
1517 *(unsigned int *)(data + (int)alt) = (unsigned int)pri;
1520 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1524 CASE(EMBRYO_OP_LIDX);
1525 offs = (pri * sizeof(Embryo_Cell)) + alt;
1527 pri = *(Embryo_Cell *)(data + (int)offs);
1529 CASE(EMBRYO_OP_LIDX_B);
1531 offs = (pri << (int)offs) + alt;
1533 pri = *(Embryo_Cell *)(data + (int)offs);
1535 CASE(EMBRYO_OP_IDXADDR);
1536 pri = (pri * sizeof(Embryo_Cell)) + alt;
1538 CASE(EMBRYO_OP_IDXADDR_B);
1540 pri = (pri << (int)offs) + alt;
1542 CASE(EMBRYO_OP_ALIGN_PRI);
1544 #ifdef WORDS_BIGENDIAN
1545 if ((size_t)offs < sizeof(Embryo_Cell))
1546 pri ^= sizeof(Embryo_Cell) - offs;
1549 CASE(EMBRYO_OP_ALIGN_ALT);
1551 #ifdef WORDS_BIGENDIAN
1552 if ((size_t)offs < sizeof(Embryo_Cell))
1553 alt ^= sizeof(Embryo_Cell) - offs;
1556 CASE(EMBRYO_OP_LCTRL);
1579 pri = (Embryo_Cell)((unsigned char *)cip - code);
1582 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1586 CASE(EMBRYO_OP_SCTRL);
1596 /* cannot change these parameters */
1605 cip = (Embryo_Cell *)(code + (int)pri);
1608 ABORT(ep, EMBRYO_ERROR_INVINSTR);
1612 CASE(EMBRYO_OP_MOVE_PRI);
1615 CASE(EMBRYO_OP_MOVE_ALT);
1618 CASE(EMBRYO_OP_XCHG);
1619 offs = pri; /* offs is a temporary variable */
1623 CASE(EMBRYO_OP_PUSH_PRI);
1626 CASE(EMBRYO_OP_PUSH_ALT);
1629 CASE(EMBRYO_OP_PUSH_C);
1633 CASE(EMBRYO_OP_PUSH_R);
1635 while (offs--) PUSH(pri);
1637 CASE(EMBRYO_OP_PUSH);
1639 PUSH(*(Embryo_Cell *)(data + (int)offs));
1641 CASE(EMBRYO_OP_PUSH_S);
1643 PUSH(*(Embryo_Cell *)(data + (int)frm + (int)offs));
1645 CASE(EMBRYO_OP_POP_PRI);
1648 CASE(EMBRYO_OP_POP_ALT);
1651 CASE(EMBRYO_OP_STACK);
1658 CASE(EMBRYO_OP_HEAP);
1665 CASE(EMBRYO_OP_PROC);
1670 CASE(EMBRYO_OP_RET);
1673 if ((Embryo_UCell)offs >= codesize)
1674 ABORT(ep, EMBRYO_ERROR_MEMACCESS);
1675 cip = (Embryo_Cell *)(code + (int)offs);
1677 CASE(EMBRYO_OP_RETN);
1680 if ((Embryo_UCell)offs >= codesize)
1681 ABORT(ep, EMBRYO_ERROR_MEMACCESS);
1682 cip = (Embryo_Cell *)(code + (int)offs);
1683 stk += *(Embryo_Cell *)(data + (int)stk) + sizeof(Embryo_Cell); /* remove parameters from the stack */
1686 CASE(EMBRYO_OP_CALL);
1687 PUSH(((unsigned char *)cip - code) + sizeof(Embryo_Cell));/* skip address */
1688 cip = JUMPABS(code, cip); /* jump to the address */
1690 CASE(EMBRYO_OP_CALL_PRI);
1691 PUSH((unsigned char *)cip - code);
1692 cip = (Embryo_Cell *)(code + (int)pri);
1694 CASE(EMBRYO_OP_JUMP);
1695 /* since the GETPARAM() macro modifies cip, you cannot
1696 * do GETPARAM(cip) directly */
1697 cip = JUMPABS(code, cip);
1699 CASE(EMBRYO_OP_JREL);
1701 cip = (Embryo_Cell *)((unsigned char *)cip + (int)offs + sizeof(Embryo_Cell));
1703 CASE(EMBRYO_OP_JZER);
1705 cip = JUMPABS(code, cip);
1707 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1709 CASE(EMBRYO_OP_JNZ);
1711 cip = JUMPABS(code, cip);
1713 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1715 CASE(EMBRYO_OP_JEQ);
1717 cip = JUMPABS(code, cip);
1719 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1721 CASE(EMBRYO_OP_JNEQ);
1723 cip = JUMPABS(code, cip);
1725 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1727 CASE(EMBRYO_OP_JLESS);
1728 if ((Embryo_UCell)pri < (Embryo_UCell)alt)
1729 cip = JUMPABS(code, cip);
1731 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1733 CASE(EMBRYO_OP_JLEQ);
1734 if ((Embryo_UCell)pri <= (Embryo_UCell)alt)
1735 cip = JUMPABS(code, cip);
1737 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1739 CASE(EMBRYO_OP_JGRTR);
1740 if ((Embryo_UCell)pri > (Embryo_UCell)alt)
1741 cip = JUMPABS(code, cip);
1743 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1745 CASE(EMBRYO_OP_JGEQ);
1746 if ((Embryo_UCell)pri >= (Embryo_UCell)alt)
1747 cip = JUMPABS(code, cip);
1749 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1751 CASE(EMBRYO_OP_JSLESS);
1753 cip = JUMPABS(code, cip);
1755 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1757 CASE(EMBRYO_OP_JSLEQ);
1759 cip = JUMPABS(code, cip);
1761 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1763 CASE(EMBRYO_OP_JSGRTR);
1765 cip = JUMPABS(code, cip);
1767 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1769 CASE(EMBRYO_OP_JSGEQ);
1771 cip = JUMPABS(code, cip);
1773 cip = (Embryo_Cell *)((unsigned char *)cip + sizeof(Embryo_Cell));
1775 CASE(EMBRYO_OP_SHL);
1778 CASE(EMBRYO_OP_SHR);
1779 pri = (Embryo_UCell)pri >> (int)alt;
1781 CASE(EMBRYO_OP_SSHR);
1784 CASE(EMBRYO_OP_SHL_C_PRI);
1788 CASE(EMBRYO_OP_SHL_C_ALT);
1792 CASE(EMBRYO_OP_SHR_C_PRI);
1794 pri = (Embryo_UCell)pri >> (int)offs;
1796 CASE(EMBRYO_OP_SHR_C_ALT);
1798 alt = (Embryo_UCell)alt >> (int)offs;
1800 CASE(EMBRYO_OP_SMUL);
1803 CASE(EMBRYO_OP_SDIV);
1804 if (alt == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1805 /* divide must always round down; this is a bit
1806 * involved to do in a machine-independent way.
1808 offs = ((pri % alt) + alt) % alt; /* true modulus */
1809 pri = (pri - offs) / alt; /* division result */
1812 CASE(EMBRYO_OP_SDIV_ALT);
1813 if (pri == 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 = ((alt % pri) + pri) % pri; /* true modulus */
1818 pri = (alt - offs) / pri; /* division result */
1821 CASE(EMBRYO_OP_UMUL);
1822 pri = (Embryo_UCell)pri * (Embryo_UCell)alt;
1824 CASE(EMBRYO_OP_UDIV);
1825 if (alt == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1826 offs = (Embryo_UCell)pri % (Embryo_UCell)alt; /* temporary storage */
1827 pri = (Embryo_UCell)pri / (Embryo_UCell)alt;
1830 CASE(EMBRYO_OP_UDIV_ALT);
1831 if (pri == 0) ABORT(ep, EMBRYO_ERROR_DIVIDE);
1832 offs = (Embryo_UCell)alt % (Embryo_UCell)pri; /* temporary storage */
1833 pri = (Embryo_UCell)alt / (Embryo_UCell)pri;
1836 CASE(EMBRYO_OP_ADD);
1839 CASE(EMBRYO_OP_SUB);
1842 CASE(EMBRYO_OP_SUB_ALT);
1845 CASE(EMBRYO_OP_AND);
1851 CASE(EMBRYO_OP_XOR);
1854 CASE(EMBRYO_OP_NOT);
1857 CASE(EMBRYO_OP_NEG);
1860 CASE(EMBRYO_OP_INVERT);
1863 CASE(EMBRYO_OP_ADD_C);
1867 CASE(EMBRYO_OP_SMUL_C);
1871 CASE(EMBRYO_OP_ZERO_PRI);
1874 CASE(EMBRYO_OP_ZERO_ALT);
1877 CASE(EMBRYO_OP_ZERO);
1879 *(Embryo_Cell *)(data + (int)offs) = 0;
1881 CASE(EMBRYO_OP_ZERO_S);
1883 *(Embryo_Cell *)(data + (int)frm + (int)offs) = 0;
1885 CASE(EMBRYO_OP_SIGN_PRI);
1886 if ((pri & 0xff) >= 0x80) pri |= ~(Embryo_UCell)0xff;
1888 CASE(EMBRYO_OP_SIGN_ALT);
1889 if ((alt & 0xff) >= 0x80) alt |= ~(Embryo_UCell)0xff;
1892 pri = (pri == alt) ? 1 : 0;
1894 CASE(EMBRYO_OP_NEQ);
1895 pri = (pri != alt) ? 1 : 0;
1897 CASE(EMBRYO_OP_LESS);
1898 pri = ((Embryo_UCell)pri < (Embryo_UCell)alt) ? 1 : 0;
1900 CASE(EMBRYO_OP_LEQ);
1901 pri = ((Embryo_UCell)pri <= (Embryo_UCell)alt) ? 1 : 0;
1903 CASE(EMBRYO_OP_GRTR);
1904 pri = ((Embryo_UCell)pri > (Embryo_UCell)alt) ? 1 : 0;
1906 CASE(EMBRYO_OP_GEQ);
1907 pri = ((Embryo_UCell)pri >= (Embryo_UCell)alt) ? 1 : 0;
1909 CASE(EMBRYO_OP_SLESS);
1910 pri = (pri < alt) ? 1 : 0;
1912 CASE(EMBRYO_OP_SLEQ);
1913 pri = (pri <= alt) ? 1 : 0;
1915 CASE(EMBRYO_OP_SGRTR);
1916 pri = (pri > alt) ? 1 : 0;
1918 CASE(EMBRYO_OP_SGEQ);
1919 pri = (pri >= alt) ? 1 : 0;
1921 CASE(EMBRYO_OP_EQ_C_PRI);
1923 pri = (pri == offs) ? 1 : 0;
1925 CASE(EMBRYO_OP_EQ_C_ALT);
1927 pri = (alt == offs) ? 1 : 0;
1929 CASE(EMBRYO_OP_INC_PRI);
1932 CASE(EMBRYO_OP_INC_ALT);
1935 CASE(EMBRYO_OP_INC);
1937 *(Embryo_Cell *)(data + (int)offs) += 1;
1939 CASE(EMBRYO_OP_INC_S);
1941 *(Embryo_Cell *)(data + (int)frm + (int)offs) += 1;
1943 CASE(EMBRYO_OP_INC_I);
1944 *(Embryo_Cell *)(data + (int)pri) += 1;
1946 CASE(EMBRYO_OP_DEC_PRI);
1949 CASE(EMBRYO_OP_DEC_ALT);
1952 CASE(EMBRYO_OP_DEC);
1954 *(Embryo_Cell *)(data + (int)offs) -= 1;
1956 CASE(EMBRYO_OP_DEC_S);
1958 *(Embryo_Cell *)(data + (int)frm + (int)offs) -= 1;
1960 CASE(EMBRYO_OP_DEC_I);
1961 *(Embryo_Cell *)(data + (int)pri) -= 1;
1963 CASE(EMBRYO_OP_MOVS);
1969 memcpy(data+(int)alt, data+(int)pri, (int)offs);
1971 CASE(EMBRYO_OP_CMPS);
1977 pri = memcmp(data + (int)alt, data + (int)pri, (int)offs);
1979 CASE(EMBRYO_OP_FILL);
1984 (size_t)offs >= sizeof(Embryo_Cell);
1985 i += sizeof(Embryo_Cell), offs -= sizeof(Embryo_Cell))
1986 *(Embryo_Cell *)(data + i) = pri;
1988 CASE(EMBRYO_OP_HALT);
1991 /* store complete status */
1997 ep->cip = (Embryo_Cell)((unsigned char*)cip - code);
1998 if (offs == EMBRYO_ERROR_SLEEP)
2000 ep->reset_stk = reset_stk;
2001 ep->reset_hea = reset_hea;
2003 return EMBRYO_PROGRAM_SLEEP;
2006 CASE(EMBRYO_OP_BOUNDS);
2008 if ((Embryo_UCell)pri > (Embryo_UCell)offs)
2009 ABORT(ep, EMBRYO_ERROR_BOUNDS);
2011 CASE(EMBRYO_OP_SYSREQ_PRI);
2012 /* save a few registers */
2013 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
2017 num = _embryo_native_call(ep, pri, &pri, (Embryo_Cell *)(data + (int)stk));
2018 if (num != EMBRYO_ERROR_NONE)
2020 if (num == EMBRYO_ERROR_SLEEP)
2024 ep->reset_stk = reset_stk;
2025 ep->reset_hea = reset_hea;
2027 return EMBRYO_PROGRAM_SLEEP;
2032 CASE(EMBRYO_OP_SYSREQ_C);
2034 /* save a few registers */
2035 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
2039 num = _embryo_native_call(ep, offs, &pri, (Embryo_Cell *)(data + (int)stk));
2040 if (num != EMBRYO_ERROR_NONE)
2042 if (num == EMBRYO_ERROR_SLEEP)
2046 ep->reset_stk = reset_stk;
2047 ep->reset_hea = reset_hea;
2049 return EMBRYO_PROGRAM_SLEEP;
2054 Embryo_Func_Stub *func_entry;
2056 hdr = (Embryo_Header *)ep->code;
2057 num = NUMENTRIES(hdr, natives, libraries);
2058 func_entry = GETENTRY(hdr, natives, 0);
2059 for (i = 0; i < num; i++)
2063 entry_name = GETENTRYNAME(hdr, func_entry);
2065 printf("EMBRYO: CALL [%i] %s() non-existant!\n", i, entry_name);
2067 (Embryo_Func_Stub *)((unsigned char *)func_entry + hdr->defsize);
2073 CASE(EMBRYO_OP_SYSREQ_D);
2075 /* save a few registers */
2076 ep->cip = (Embryo_Cell)((unsigned char *)cip - code);
2080 num = _embryo_native_call(ep, offs, &pri, (Embryo_Cell *)(data + (int)stk));
2081 if (num != EMBRYO_ERROR_NONE)
2083 if (num == EMBRYO_ERROR_SLEEP)
2087 ep->reset_stk = reset_stk;
2088 ep->reset_hea = reset_hea;
2090 return EMBRYO_PROGRAM_SLEEP;
2092 ABORT(ep, ep->error);
2095 CASE(EMBRYO_OP_JUMP_PRI);
2096 cip = (Embryo_Cell *)(code + (int)pri);
2098 CASE(EMBRYO_OP_SWITCH);
2102 /* +1, to skip the "casetbl" opcode */
2103 cptr = (Embryo_Cell *)(code + (*cip)) + 1;
2104 /* number of records in the case table */
2106 /* preset to "none-matched" case */
2107 cip = (Embryo_Cell *)(code + *(cptr + 1));
2109 (num > 0) && (*cptr != pri);
2113 cip = (Embryo_Cell *)(code + *(cptr + 1));
2116 CASE(EMBRYO_OP_SWAP_PRI);
2117 offs = *(Embryo_Cell *)(data + (int)stk);
2118 *(Embryo_Cell *)(data + (int)stk) = pri;
2121 CASE(EMBRYO_OP_SWAP_ALT);
2122 offs = *(Embryo_Cell *)(data + (int)stk);
2123 *(Embryo_Cell *)(data + (int)stk) = alt;
2126 CASE(EMBRYO_OP_PUSHADDR);
2130 CASE(EMBRYO_OP_NOP);
2132 CASE(EMBRYO_OP_NONE);
2133 CASE(EMBRYO_OP_FILE);
2134 CASE(EMBRYO_OP_LINE);
2135 CASE(EMBRYO_OP_SYMBOL);
2136 CASE(EMBRYO_OP_SRANGE);
2137 CASE(EMBRYO_OP_CASETBL);
2138 CASE(EMBRYO_OP_SYMTAG);
2140 #ifndef EMBRYO_EXEC_JUMPTABLE
2142 ABORT(ep, EMBRYO_ERROR_INVINSTR);
2146 ep->max_run_cycles = max_run_cycles;
2148 ep->hea = hea_start;
2149 return EMBRYO_PROGRAM_OK;
2153 * Retreives the return value of the last called function of the given
2155 * @param ep The given program.
2156 * @return An Embryo_Cell representing the return value of the function
2157 * that was last called.
2158 * @ingroup Embryo_Run_Group
2161 embryo_program_return_value_get(Embryo_Program *ep)
2168 * Sets the maximum number of abstract machine cycles any given program run
2169 * can execute before being put to sleep and returning.
2171 * @param ep The given program.
2172 * @param max The number of machine cycles as a limit.
2174 * This sets the maximum number of abstract machine (virtual machine)
2175 * instructions that a single run of an embryo function (even if its main)
2176 * can use before embryo embryo_program_run() reutrns with the value
2177 * EMBRYO_PROGRAM_TOOLONG. If the function fully executes within this number
2178 * of cycles, embryo_program_run() will return as normal with either
2179 * EMBRYO_PROGRAM_OK, EMBRYO_PROGRAM_FAIL or EMBRYO_PROGRAM_SLEEP. If the
2180 * run exceeds this instruction count, then EMBRYO_PROGRAM_TOOLONG will be
2181 * returned indicating the program exceeded its run count. If the app wishes
2182 * to continue running this anyway - it is free to process its own events or
2183 * whatever it wants and continue the function by calling
2184 * embryo_program_run(program, EMBRYO_FUNCTION_CONT); which will start the
2185 * run again until the instruction count is reached. This can keep being done
2186 * to allow the calling program to still be able to control things outside the
2187 * embryo function being called. If the maximum run cycle count is 0 then the
2188 * program is allowed to run forever only returning when it is done.
2190 * It is important to note that abstract machine cycles are NOT the same as
2191 * the host machine cpu cycles. They are not fixed in runtime per cycle, so
2192 * this is more of a helper tool than a way to HARD-FORCE a script to only
2193 * run for a specific period of time. If the cycle count is set to something
2194 * low like 5000 or 1000, then every 1000 (or 5000) cycles control will be
2195 * returned to the calling process where it can check a timer to see if a
2196 * physical runtime limit has been elapsed and then abort runing further
2197 * assuming a "runaway script" or keep continuing the script run. This
2198 * limits resolution to only that many cycles which do not take a determined
2199 * amount of time to execute, as this varies from cpu to cpu and also depends
2200 * on how loaded the system is. Making the max cycle run too low will
2201 * impact performance requiring the abstract machine to do setup and teardown
2202 * cycles too often comapred to cycles actually executed.
2204 * Also note it does NOT include nested abstract machines. IF this abstract
2205 * machine run calls embryo script that calls a native function that in turn
2206 * calls more embryo script, then the 2nd (and so on) levels are not included
2207 * in this run count. They can set their own max instruction count values
2210 * The default max cycle run value is 0 in any program until set with this
2213 * @ingroup Embryo_Run_Group
2216 embryo_program_max_cycle_run_set(Embryo_Program *ep, int max)
2219 if (max < 0) max = 0;
2220 ep->max_run_cycles = max;
2224 * Retreives the maximum number of abstract machine cycles a program is allowed
2226 * @param ep The given program.
2227 * @return The number of cycles a run cycle is allowed to run for this
2230 * This returns the value set by embryo_program_max_cycle_run_set(). See
2231 * embryo_program_max_cycle_run_set() for more information.
2233 * @ingroup Embryo_Run_Group
2236 embryo_program_max_cycle_run_get(Embryo_Program *ep)
2239 return ep->max_run_cycles;
2243 * @defgroup Embryo_Parameter_Group Function Parameter Functions
2245 * Functions that set parameters for the next function that is called.
2249 * Pushes an Embryo_Cell onto the function stack to use as a parameter for
2250 * the next function that is called in the given program.
2251 * @param ep The given program.
2252 * @param cell The Embryo_Cell to push onto the stack.
2253 * @return @c 1 if successful. @c 0 otherwise.
2254 * @ingroup Embryo_Parameter_Group
2257 embryo_parameter_cell_push(Embryo_Program *ep, Embryo_Cell cell)
2262 if (ep->params_size > ep->params_alloc)
2264 ep->params_alloc += 8;
2265 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
2269 pr = &(ep->params[ep->params_size - 1]);
2271 pr->cell_array = NULL;
2272 pr->cell_array_size = 0;
2279 * Pushes a string onto the function stack to use as a parameter for the
2280 * next function that is called in the given program.
2281 * @param ep The given program.
2282 * @param str The string to push onto the stack.
2283 * @return @c 1 if successful. @c 0 otherwise.
2284 * @ingroup Embryo_Parameter_Group
2287 embryo_parameter_string_push(Embryo_Program *ep, const char *str)
2293 return embryo_parameter_string_push(ep, "");
2294 str_dup = strdup(str);
2295 if (!str_dup) return 0;
2297 if (ep->params_size > ep->params_alloc)
2299 ep->params_alloc += 8;
2300 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
2308 pr = &(ep->params[ep->params_size - 1]);
2310 pr->cell_array = NULL;
2311 pr->cell_array_size = 0;
2313 pr->string = str_dup;
2318 * Pushes an array of Embryo_Cells onto the function stack to be used as
2319 * parameters for the next function that is called in the given program.
2320 * @param ep The given program.
2321 * @param cells The array of Embryo_Cells.
2322 * @param num The number of cells in @p cells.
2323 * @return @c 1 if successful. @c 0 otherwise.
2324 * @ingroup Embryo_Parameter_Group
2327 embryo_parameter_cell_array_push(Embryo_Program *ep, Embryo_Cell *cells, int num)
2330 Embryo_Cell *cell_array;
2332 if ((!cells) || (num <= 0))
2333 return embryo_parameter_cell_push(ep, 0);
2334 cell_array = malloc(num * sizeof(Embryo_Cell));
2336 if (ep->params_size > ep->params_alloc)
2338 ep->params_alloc += 8;
2339 pr = realloc(ep->params, ep->params_alloc * sizeof(Embryo_Param));
2347 pr = &(ep->params[ep->params_size - 1]);
2349 pr->cell_array = NULL;
2350 pr->cell_array_size = 0;
2352 pr->cell_array = cell_array;
2353 pr->cell_array_size = num;
2354 memcpy(pr->cell_array, cells, num * sizeof(Embryo_Cell));