-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstackvm.h
More file actions
56 lines (53 loc) · 2.4 KB
/
stackvm.h
File metadata and controls
56 lines (53 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef STACKVM_H
#define STACKVM_H
#include <stddef.h> /* size_t */
/* flags for vm.status
* bit set means an error.
*/
#define VM_ERROR_INVALID_OPCODE (1 << 1) /* 0x2 */
#define VM_ERROR_STACK_UNDERFLOW (1 << 2) /* 0x4 */
#define VM_ERROR_STACK_OVERFLOW (1 << 3) /* 0x8 */
#define VM_ERROR_SYSCALL (1 << 4) /* 0x10 */
#define VM_ERROR_END_OF_FILE (1 << 5) /* 0x20 */
#define VM_ERROR_OUT_OF_BOUNDS (1 << 6) /* 0x40 */
#define VM_ERROR_MATH_ERROR (1 << 7) /* 0x80 */
#define VM_ERROR_UNALIGNED (1 << 9) /* 0x200 */
#define VM_ERROR_NOT_INITIALIZED (1 << 10) /* 0x400 */
#define VM_ERROR_BAD_ENVIRONMENT (1 << 11) /* 0x800 */
#define VM_ERROR_BAD_SYSCALL (1 << 12) /* 0x1000 */
#define VM_ERROR_ABORT (1 << 13) /* 0x2000 */
#define VM_STATUS_FINISHED (0) /* 0x0 - no bits set means success */
typedef unsigned vmword_t;
typedef float vmsingle_t;
struct vm_env;
struct vm;
struct vm_env *vm_env_new(unsigned nr_syscalls);
int vm_env_register(struct vm_env *env, int syscall_num, void (*sc)(struct vm *vm));
void vm_env_set_break_handler(struct vm_env *env, int (*handler)(struct vm *vm));
void vm_env_set_default_syscall(struct vm_env *env, void (*handler)(struct vm *vm, int syscall_num));
void vm_stacktrace(const struct vm *vm);
void vm_disassemble(const struct vm *vm);
int vm_run_slice(struct vm *vm, unsigned max_steps);
/* vm_call / vm_call_array only set up the call frame; the VM does not run
* until the host drives it with vm_run_slice() in a loop. */
void vm_call(struct vm *vm, vmword_t entry, unsigned nr_args, ...);
void vm_call_array(struct vm *vm, vmword_t entry, unsigned nr_args, const vmword_t args[]);
void vm_free(struct vm *vm);
struct vm *vm_new(const struct vm_env *env);
/* resize the op stack; must be called before any guest pushes (op stack empty).
* returns 0 on success, -1 on bad args / allocation failure / non-empty stack. */
int vm_set_stack_size(struct vm *vm, unsigned words);
int vm_load(struct vm *vm, const char *filename);
int vm_status(const struct vm *vm);
const char *vm_filename(const struct vm *vm);
vmword_t vm_pop(struct vm *vm);
vmsingle_t vm_popf(struct vm *vm);
void vm_push(struct vm *vm, vmword_t n);
void vm_pushf(struct vm *vm, vmsingle_t f);
vmword_t vm_arg(struct vm *vm, int num);
char *vm_string(struct vm *vm, vmword_t addr, size_t *len);
void vm_abort(struct vm *vm);
void *vm_get_extra(struct vm *vm);
void *vm_set_extra(struct vm *vm, void *p);
void vm_yield(struct vm *vm);
#endif