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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
#include <algorithm>
#include <cassert>
#include "spasm.hpp"
namespace SpasmImpl
{
Spasm::Spasm() {}
/*!
** Constructs new Spasm object
**
** \param _bc_size - size/length of the bytecode
** \param _bytecode - the bytecode of the program
** \param _istr - input stream for the machine
** \param _ostr - output stream for the machine
*/
void Spasm::Initialize(PC_t _bc_size,
const byte* _bytecode,
std::istream& _istr,
std::ostream& _ostr)
{
m_PC = 0;
m_ByteCode.assign(_bytecode, _bytecode + _bc_size);
istr = &_istr;
ostr = &_ostr;
data_stack.resize(1024);
m_SP = &data_stack[0];
m_FP = &data_stack[0];
}
Spasm::~Spasm() {}
/*!
** Runs the machine. The machine stops if it reaches an invalid opcode
** or opcode 0 or the pc reaches beyond the end of the bytecode.
** @return error code for success or failure
*/
Spasm::RunResult Spasm::run()
{
const auto codeSize = m_ByteCode.size();
while (m_PC < codeSize)
{
const auto instruction = m_ByteCode[m_PC++];
const auto opcode = OpCodes(instruction & 0x3f);
const auto size = instruction >> 6;
switch (opcode)
{
case OpCodes::Halt:
return RunResult::Success;
case OpCodes::Dup:
dup();
break;
case OpCodes::Pop:
{
const auto count = PC_t(read_reg(size));
assert(m_SP - count >= m_FP);
m_SP -= count;
break;
}
case OpCodes::PopTo:
{
const auto arg0 = read_reg(size);
popto(arg0);
break;
}
case OpCodes::PushFrom:
{
const auto arg0 = read_reg(size);
push(arg0);
break;
}
case OpCodes::Push:
{
const auto count = PC_t(read_reg(size));
assert(m_SP + count <= &data_stack[data_stack.size() - 1]);
std::fill(m_SP, m_SP + count, data_t{});
m_SP += count;
break;
}
case OpCodes::Print:
{
const auto arg0 = read_reg(size);
print(arg0);
break;
}
case OpCodes::Read:
{
const auto arg0 = read_reg(size);
read(arg0);
break;
}
case OpCodes::Call:
{
const auto arg0 = read_reg(size);
call(arg0);
break;
}
case OpCodes::Ret:
{
const auto arg0 = read_reg(size);
ret(arg0);
break;
}
case OpCodes::Jump:
{
const auto arg0 = read_reg(size);
go(arg0);
break;
}
case OpCodes::JumpT:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
gotrue(arg0, arg1);
break;
}
case OpCodes::JumpF:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
gofalse(arg0, arg1);
break;
}
case OpCodes::Const:
{
const auto reg = read_reg(size);
const auto value = read_number(size);
set_local(reg, value);
break;
}
case OpCodes::String:
{
const auto reg = read_reg(size);
const auto value = read_string(size);
set_local(reg, value);
break;
}
case OpCodes::Add:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
plus(arg0, arg1, arg2);
break;
}
case OpCodes::Sub:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
minus(arg0, arg1, arg2);
break;
}
case OpCodes::Mul:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
multiply(arg0, arg1, arg2);
break;
}
case OpCodes::Div:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
divide(arg0, arg1, arg2);
break;
}
case OpCodes::Mod:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
modulus(arg0, arg1, arg2);
break;
}
case OpCodes::Less:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
less(arg0, arg1, arg2);
break;
}
case OpCodes::LessEq:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
lesseq(arg0, arg1, arg2);
break;
}
case OpCodes::Greater:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
greater(arg0, arg1, arg2);
break;
}
case OpCodes::GreaterEq:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
greatereq(arg0, arg1, arg2);
break;
}
case OpCodes::Equal:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
equal(arg0, arg1, arg2);
break;
}
case OpCodes::NotEqual:
{
const auto arg0 = read_reg(size);
const auto arg1 = read_reg(size);
const auto arg2 = read_reg(size);
not_equal(arg0, arg1, arg2);
break;
}
default:
{
std::cerr << opcode << ": not implemented" << std::endl;
return RunResult::NotImplemented;
}
}
}
return RunResult::Success;
}
/*!
** Pushes the next data_t object on the data stack
*/
void Spasm::push(reg_t reg)
{
push_data(get_local(reg));
}
void Spasm::popto(reg_t reg)
{
m_FP[reg] = *(--m_SP);
}
/*!
** Dups the data_t object on top of the data stack.
*/
void Spasm::dup()
{
push_data(*(m_SP - 1));
}
/*!
** Reads a data_t object from the input stream and pushes it on the data
** stack.
*/
void Spasm::read(reg_t reg)
{
data_t x;
*istr >> x;
set_local(reg, x);
}
/*!
** Pops a data_t object from the data stack and prints it on the output
** stream.
*/
void Spasm::print(reg_t reg)
{
*ostr << get_local(reg);
}
/*!
** Pops two data objects from the data stack and pushes their sum on the
** data stack.
*/
void Spasm::plus(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) + get_local(a2));
}
/*!
** Pops two data objects from the data stack and pushes their difference
** on the data stack.
*/
void Spasm::minus(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) - get_local(a2));
}
/*!
** Pops two data objects from the data stack and pushes their product
** on the data stack.
*/
void Spasm::multiply(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) * get_local(a2));
}
/*!
** Pops two data objects from the data stack and pushes their division
** on the data stack.
*/
void Spasm::divide(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) / get_local(a2));
}
/*!
** Pops two data objects from the data stack and pushes their modulus
** on the data stack.
*/
void Spasm::modulus(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) % get_local(a2));
}
/*!
** Conditional execution.
** If the data_t object on top of the data stack evaluates to true
** execution continues from the place pointed by number in the next bytes
*/
void Spasm::gotrue(reg_t a0, reg_t a1)
{
if (get_local(a0))
{
go(a1);
}
}
/*!
** Conditional execution.
** If the data_t object on top of the data stack evaluates to false
** execution continues from the place pointed by number in the next bytes
*/
void Spasm::gofalse(reg_t a0, reg_t a1)
{
if (!get_local(a0))
{
go(a1);
}
}
/*!
** Unconditional execution.
** Execution continues from the opcode pointed by the next bytes
*/
void Spasm::go(reg_t a0)
{
m_PC = PC_t(a0);
}
/*!
** Function call. A new frame with the specified size is created, the
** return address is saved in the return stack and the new pc is loaded.
*/
void Spasm::call(reg_t a0)
{
Frame call{m_PC, PC_t(m_FP - &data_stack[0]),
m_SP - &data_stack[0] - PC_t((*(m_SP - 1)).get_double()) - 1};
m_Frames.push(std::move(call));
m_FP = m_SP - 1;
go(a0);
}
/*!
** Function return. The frame of the current function is destroyed and the
** saved return address is loaded in the pc
*/
void Spasm::ret(reg_t reg)
{
Frame parent = m_Frames.top();
m_Frames.pop();
m_SP = &data_stack[parent.StackPointer];
*(m_SP - 1) = m_FP[reg];
m_FP = &data_stack[parent.FramePointer];
m_PC = parent.ReturnAddress;
}
/*!
** Local variable with index top of the data stack is pushed on top of the
** data stack.
*/
void Spasm::load()
{
assert(false && "not-implemented");
}
/*!
** Local variable is assigned value
*/
void Spasm::store()
{
assert(false && "not-implemented");
}
/*!
** Compares the values on top of the stack. Pushes 1 if the value before
** the top of the stack is less than that on top of the stack.
*/
void Spasm::less(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) < get_local(a2));
}
/*!
** Compares the values on top of the stack. Pushes 1 if the value before
** the top of the stack is less than or equal to that on top of the stack.
*/
void Spasm::lesseq(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) <= get_local(a2));
}
void Spasm::greater(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) > get_local(a2));
}
void Spasm::greatereq(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) >= get_local(a2));
}
void Spasm::equal(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) == get_local(a2));
}
void Spasm::not_equal(reg_t a0, reg_t a1, reg_t a2)
{
set_local(a0, get_local(a1) != get_local(a2));
}
data_t Spasm::get_local(reg_t reg)
{
assert(&data_stack[0] <= (m_FP + reg));
assert((m_FP + reg) < &data_stack[data_stack.size() - 1]);
return m_FP[reg];
}
void Spasm::set_local(reg_t reg, data_t data)
{
assert(&data_stack[0] <= (m_FP + reg));
assert((m_FP + reg) < &data_stack[data_stack.size() - 1]);
m_FP[reg] = data;
}
data_t Spasm::pop_data()
{
auto result = data_stack.back();
data_stack.pop_back();
return result;
}
void Spasm::push_data(data_t data)
{
assert(m_SP < &data_stack[data_stack.size() - 1]);
*(m_SP++) = data;
}
reg_t Spasm::read_reg(size_t size)
{
return read_integer(size);
}
data_t Spasm::read_number(size_t size)
{
return data_t(double(read_integer(size)));
}
int64_t Spasm::read_integer(size_t size)
{
switch (size)
{
case 0:
return m_ByteCode[m_PC++];
case 1:
{
auto result = *reinterpret_cast<int16_t*>(&m_ByteCode[0] + m_PC);
m_PC += 2;
return result;
}
case 2:
{
auto result = *reinterpret_cast<int32_t*>(&m_ByteCode[0] + m_PC);
m_PC += 4;
return result;
}
case 3:
{
auto result = *reinterpret_cast<int64_t*>(&m_ByteCode[0] + m_PC);
m_PC += 8;
return result;
}
}
assert(false && "not reached");
return 0;
}
data_t Spasm::read_string(size_t size)
{
const auto length = size_t(read_integer(size));
const auto s = reinterpret_cast<const char*>(&m_ByteCode[m_PC]);
m_PC += length;
const auto value = m_Strings.Get(s, length);
return data_t(::Spasm::ValueType::String, (void*)value);
}
} // namespace SpasmImpl
|