summaryrefslogtreecommitdiffhomepage
path: root/vm.h
blob: 0f7b805b2c1751cdc7a6105d92d38574281b7e4a (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef __VM_H_
#define __VM_H_

#include <stdbool.h>
#include <stdarg.h>

#include "chunk.h"
#include "util.h"
#include "lexer.h"
#include "types.h"

#define __insns \
__insn(NOOP) \
__insn(LOAD) \
__insn(LOAD8) \
__insn(LOAD16) \
__insn(LOAD32) \
__insn(LTHIS) \
__insn(LREXP) \
__insn(LNULL) \
__insn(LTRUE) \
__insn(LFALSE) \
__insn(LLOC) \
__insn(LUPV) \
__insn(LVAR) \
__insn(LVAL) \
__insn(CLFN) \
__insn(ARFN) \
__insn(SLOC) \
__insn(SUPV) \
__insn(SVAR) \
__insn(SVAL) \
__insn(ULOC) \
__insn(UUPV) \
__insn(UVAR) \
__insn(UVAL) \
__insn(NARR) \
__insn(PARR) \
__insn(MARR) \
__insn(NOBJ) \
__insn(SOBJ) \
__insn(MOBJ) \
__insn(PLUS) \
__insn(MINUS) \
__insn(ADD) \
__insn(SUB) \
__insn(MUL) \
__insn(DIV) \
__insn(MOD) \
__insn(LSHIFT) \
__insn(RSHIFT) \
__insn(BAND) \
__insn(BXOR) \
__insn(BOR) \
__insn(COMPL) \
__insn(EQ) \
__insn(NE) \
__insn(EQS) \
__insn(NES) \
__insn(LT) \
__insn(GT) \
__insn(IN) \
__insn(NOT) \
__insn(JMP) \
__insn(JMPZ) \
__insn(COPY) \
__insn(POP) \
__insn(CUPV) \
__insn(RETURN) \
__insn(CALL) \
__insn(MCALL) \
__insn(PRINT) \
__insn(NEXTK) \
__insn(NEXTKV)


#undef __insn
#define __insn(_name) I_##_name,

enum insn_type {
	__insns
	__I_MAX
};

typedef struct {
	int8_t stack_pop;
	int8_t stack_push;
	int8_t operand_bytes;
} uc_insn_definition;

typedef enum {
	STATUS_OK,
	ERROR_COMPILE,
	ERROR_RUNTIME
} uc_vm_status_t;

extern uint32_t insns[__I_MAX];

void uc_vm_init(uc_vm *vm, uc_parse_config *config);
void uc_vm_free(uc_vm *vm);

void uc_vm_stack_push(uc_vm *vm, uc_value_t *value);
uc_value_t *uc_vm_stack_pop(uc_vm *vm);
uc_value_t *uc_vm_stack_peek(uc_vm *vm, size_t offset);

uc_exception_type_t uc_vm_call(uc_vm *vm, bool mcall, size_t nargs);

void __attribute__((format(printf, 3, 0)))
uc_vm_raise_exception(uc_vm *vm, uc_exception_type_t type, const char *fmt, ...);

uc_vm_status_t uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t *globals, uc_value_t *modules);

#endif /* __VM_H_ */