-
Notifications
You must be signed in to change notification settings - Fork 3
/
vmachine.cpp
200 lines (162 loc) · 5.63 KB
/
vmachine.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*=============================================================================
Copyright (c) 2001-2009 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include "vmachine.hpp"
#include "byte_code.hpp"
#include <boost/assert.hpp>
#ifdef _MSC_VER
# pragma warning(disable: 4800) // forcing value to bool 'true' or 'false'
// (performance warning)
#endif // _MSC_VER
//!
//! \brief simple virtual machine using a stack pointer and a program counter
//! The program counter points to the next instruction to execute while
//! the stack stores our local variables
//!
//! \precondition Stack pointer points one beyond the last used element
//! Program counter points to the next instruction to execute
//!
//! \note If an instruction doesn't pop or push exactly the expected number
//! of values the stack gets corrupted
//!
//!
int vmachine::execute(
std::vector<int> const& code
, std::vector<int>::const_iterator pc
, std::vector<int>::iterator frame_ptr
)
{
std::vector<int>::iterator stack_ptr = frame_ptr;
while (true)
{
BOOST_ASSERT(pc != code.end());
switch (*pc++)
{
case op_neg:
stack_ptr[-1] = -stack_ptr[-1];
break;
case op_not:
stack_ptr[-1] = !bool(stack_ptr[-1]);
break;
case op_add:
--stack_ptr;
stack_ptr[-1] += stack_ptr[0];
break;
case op_sub:
--stack_ptr;
stack_ptr[-1] -= stack_ptr[0];
break;
case op_mul:
--stack_ptr;
stack_ptr[-1] *= stack_ptr[0];
break;
case op_div:
--stack_ptr;
stack_ptr[-1] /= stack_ptr[0];
break;
case op_eq:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] == stack_ptr[0]);
break;
case op_neq:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] != stack_ptr[0]);
break;
case op_lt:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] < stack_ptr[0]);
break;
case op_lte:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] <= stack_ptr[0]);
break;
case op_gt:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] > stack_ptr[0]);
break;
case op_gte:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] >= stack_ptr[0]);
break;
case op_and:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1]) && bool(stack_ptr[0]);
break;
case op_or:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1]) || bool(stack_ptr[0]);
break;
case op_load_32:
*stack_ptr++ = frame_ptr[*pc++];
break;
case op_load_16:
*stack_ptr++ = 0xFFFF & (frame_ptr[*pc++]);
break;
case op_load_8:
*stack_ptr++ = 0xFF & (frame_ptr[*pc++]);
break;
case op_store_32:
--stack_ptr;
frame_ptr[*pc++] = stack_ptr[0];
break;
case op_int:
*stack_ptr++ = *pc++;
break;
case op_short:
*stack_ptr++ = 0xFFFF & (*pc++);
break;
case op_byte:
*stack_ptr++ = 0xFF & (*pc++);
break;
case op_true:
*stack_ptr++ = true;
break;
case op_false:
*stack_ptr++ = false;
break;
case op_jump:
pc = code.begin() + *pc;
break;
case op_jump_if:
if (!bool(stack_ptr[-1]))
pc = code.begin() + *pc;
else
++pc;
--stack_ptr;
break;
case op_stk_adj:
stack_ptr += *pc++;
break;
case op_call:
{
int nargs = *pc++;
int jump = *pc++;
// a function call is a recursive call to execute
int r = execute(
code
, code.begin() + jump
, stack_ptr - nargs
);
// cleanup after return from function
stack_ptr[-nargs] = r; // get return value
stack_ptr -= (nargs - 1); // the stack will now contain
// the return value
}
break;
case op_return:
return stack_ptr[-1];
case op_print:
std::cout << *(--stack_ptr);
break;
case op_dump:
std::cout << "dump: " << *(--stack_ptr) << std::endl;
break;
case op_debug:
std::cout << "debug: " << *(--stack_ptr) << std::endl;
break;
}
}
return 0; // Should never reach this...
}