-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_machine.hpp
168 lines (140 loc) · 2.36 KB
/
stack_machine.hpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef STACK_MACHINE_HPP
#define STACK_MACHINE_HPP
#include <array>
#include <boost/variant.hpp>
#include <map>
#include <stack>
#include <vector>
namespace j5 {
enum class op_t {
ADD,
SUB,
INC,
DEC,
AND,
OR,
NOT,
XOR,
SHR,
SHL,
TGT,
TLT,
TEQ,
TSZ,
SSET,
SET,
LOAD,
STORE,
BRANCH,
BRZERO,
IBRANCH,
CALL,
RETURN,
STOP,
OUT,
DROP,
DUP,
SWAP,
RSD3,
RSU3,
TUCK2,
TUCK3,
COPY3,
PUSH,
POP,
NUM_OPS,
};
static const std::array<std::string, (size_t)op_t::NUM_OPS> OP_T_STR{{
"ADD",
"SUB",
"INC",
"DEC",
"AND",
"OR",
"NOT",
"XOR",
"SHR",
"SHL",
"TGT",
"TLT",
"TEQ",
"TSZ",
"SSET",
"SET",
"LOAD",
"STORE",
"BRANCH",
"BRZERO",
"IBRANCH",
"CALL",
"RETURN",
"STOP",
"OUT",
"DROP",
"DUP",
"SWAP",
"RSD3",
"RSU3",
"TUCK2",
"TUCK3",
"COPY3",
"PUSH",
"POP",
}};
using operand_t = boost::variant<boost::blank, uint16_t, std::string>;
struct instruction {
std::string label;
op_t code;
operand_t op;
};
std::ostream& operator<<(std::ostream& os, const instruction& ins);
inline instruction make_instruction(op_t code, operand_t op = boost::blank(), std::string label = "")
{
return {label, code, op};
}
using program = std::vector<instruction>;
program tokenise_source(const std::string &source);
class machine {
public:
void run(const program &prog, bool speedlimit);
std::string register_dump();
static const std::map<op_t, int> STACK_DIFF;
protected:
uint16_t pc;
std::stack<uint16_t> stack;
std::array<uint16_t, 0x10000> mem;
// Registers. In a stack machine. Go figure.
uint8_t flags;
uint8_t lbr;
uint8_t gbr;
uint8_t vba;
enum class flagbit : uint8_t {
CARRY = 0,
ZERO = 1,
IMODE = 6,
INTER = 7,
};
bool terminate;
program cur_prog;
uint16_t run_instruction(const instruction &ins);
uint16_t run_branch_instruction(const instruction &ins);
virtual uint16_t find_label(const std::string &l);
using opfunc_t = std::function<void(machine*)>;
using op_map = std::map<op_t, opfunc_t>;
static const op_map OPERATIONS;
void binop_func(std::function<uint16_t(uint16_t, uint16_t)> op);
void comp_func(std::function<bool(uint16_t, uint16_t)> op);
void testzero_func();
void set_func(uint16_t v);
void load_func();
void store_func();
void swap_func();
void out_func();
void tuck2_func();
void tuck3_func();
void rsu3_func();
void rsd3_func();
void copy3_func();
};
}
#endif /* STACK_MACHINE_HPP */