-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualMachine.cpp
More file actions
143 lines (109 loc) · 4.31 KB
/
Copy pathVirtualMachine.cpp
File metadata and controls
143 lines (109 loc) · 4.31 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
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
#include "VirtualMachine.h"
#include <fstream>
#include "Constants.h"
void CVirtualMachine::Load( const std::string& binFileName )
{
bin.assign( 0, 0 );
std::ifstream binFile( binFileName );
int c = 0;
while( !binFile.eof() ) {
c = binFile.get();
bin.push_back( static_cast<char>(c) );
}
}
int CVirtualMachine::Exec()
{
for( int i = 0; i < WordSize; ++i ) { // Check header
if( bin[i] != HeaderChar ) {
throw std::runtime_error( "file is not binary file!" );
}
}
// Initialise instruction ptr
getWord( InstructionPtr ) = getWord( EntryPtr );
while( doStep() ) {}
//
return getWord( InstructionPtr );
}
bool CVirtualMachine::doStep()
{
int& instructionPtrCell = getWord( InstructionPtr );
int& stackPtrCell = getWord( StackPtr );
int& instruction = getWord( instructionPtrCell );
// Remember that int reverses bytes
char commandCode = instruction & 0xffU;
int source = 0;
int destination = 0;
if( commandCode == CommandCodes.at( "push" ) ) {
source = (instruction & 0xffffff00U) >> 8;
getWord( stackPtrCell-- ) = getWord( source ); // Programm stack grows to the left
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "pop" ) ) {
destination = (instruction & 0xffffff00U) >> 8;
getWord( destination ) = getWord( ++stackPtrCell );
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "call" ) ) {
source = (instruction & 0xffffff00U) >> 8;
getWord( stackPtrCell-- ) = instructionPtrCell + 1;
instructionPtrCell = source;
} else if( commandCode == CommandCodes.at( "copy" ) ) {
source = (instruction & 0x0000ff00U) >> 8;
destination = (instruction & 0xffff0000U) >> 16;
getWord( destination ) = getWord( source );
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "load" ) ) {
source = (instruction & 0x00ffff00U) >> 8;
destination = (instruction & 0xff000000U) >> 24;
getWord( destination ) = getWord( source );
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "add" ) ) {
source = (instruction & 0x0000ff00U) >> 8;
destination = (instruction & 0xffff0000U) >> 16;
getWord( destination ) += getWord( source );
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "sub" ) ) {
source = (instruction & 0x0000ff00U) >> 8;
destination = (instruction & 0xffff0000U) >> 16;
getWord( destination ) -= getWord( source );
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "isequal" ) ) {
source = (instruction & 0x0000ff00U) >> 8;
destination = (instruction & 0xffff0000U) >> 16;
getWord( stackPtrCell-- ) = getWord( source ) == getWord( destination ) ? 1 : 0;
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "if" ) ) {
source = (instruction & 0xffffff00U) >> 8;
if( getWord( source ) ) {
++instructionPtrCell;
} else {
instructionPtrCell += 2;
}
} else if( commandCode == CommandCodes.at( "return" ) ) {
instructionPtrCell = getWord( ++stackPtrCell );
} else if( commandCode == CommandCodes.at( "exit" ) ) {
instructionPtrCell = 0; // Successfully exit
return false;
} else if( commandCode == CommandCodes.at( "print" ) ) {
source = (instruction & 0xffffff00U) >> 8;
std::cout << getWord( source ) << "\n";
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "printconst" ) ) {
source = (instruction & 0xffffff00U) >> 8;
std::cout << &bin[source * WordSize] << "\n";
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "scan" ) ) {
destination = (instruction & 0xffffff00U) >> 8;
std::cin >> getWord( destination );
++instructionPtrCell;
} else if( commandCode == CommandCodes.at( "exec" ) ) {
source = (instruction & 0xffffff00U) >> 8;
instructionPtrCell = source;
} else {
instructionPtrCell = 42; // Bad exit
return false;
}
return true;
}
int& CVirtualMachine::getWord( int ptr )
{
return *reinterpret_cast<int*>(&bin[ptr * WordSize]);
}