-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssembler.cpp
More file actions
193 lines (167 loc) · 5.42 KB
/
Copy pathAssembler.cpp
File metadata and controls
193 lines (167 loc) · 5.42 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
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
#include "Assembler.h"
#include <algorithm>
#include "Constants.h"
CAssembler::CAssembler() :
out( DataSize, NullChar ),
dataPtr( 5 )
{
for( int i = 0; i < WordSize; ++i ) {
out[i] = HeaderChar;
}
getWord( InstructionPtr ) = 0;
getWord( StackPtr ) = DataSize / WordSize - 1;
}
void CAssembler::Compile( const std::string& codeFileName, const std::string& outFileName )
{
codeFile = std::ifstream( codeFileName );
std::string section;
while( !codeFile.eof() ) {
codeFile >> section;
if( section == "var" ) {
loadVariables();
} else if( section == "const" ) {
loadConstants();
} else if( section == "def" ) {
loadFunctions();
} else {
throw std::runtime_error( "unknown section detected" );
}
}
std::ofstream outFile( outFileName );
for( int i = 0; i < out.size(); ++i ) {
outFile << out[i];
}
}
void CAssembler::loadVariables()
{
std::string name, var;
codeFile >> var;
checkSyntax( var, "{" );
codeFile >> name;
while( name != "}" ) {
codeFile >> var;
variables[name] = dataPtr;
getWord( dataPtr ) = std::stoi( var );
dataPtr++;
codeFile >> name;
}
}
void CAssembler::loadConstants()
{
std::string name, cnst;
codeFile >> cnst;
checkSyntax( cnst, "{" );
codeFile >> name;
while( name != "}" ) {
cnst.assign( MaxConstSize, 0 );
codeFile.get();
codeFile.getline( &cnst[0], MaxConstSize );
// Erase redundant null chars
cnst.erase( std::find( cnst.begin(), cnst.end(), 0 ), cnst.end() );
constants[name] = dataPtr;
int currentDataPtr = dataPtr * WordSize; // currentDataPtr is index for 'out' array!
for( int i = 0; i < cnst.length() + 1; ++i, ++currentDataPtr ) {
if( currentDataPtr >= dataPtr * WordSize ) {
dataPtr++;
}
out[currentDataPtr] = (i != cnst.length() ? cnst[i] : 0);
}
codeFile >> name;
}
}
void CAssembler::loadFunctions()
{
std::string name;
codeFile >> name;
checkSyntax( name, "{" );
codeFile >> name;
while( name != "}" ) {
functions[name] = out.size() / WordSize;
if( name == "main" ) { // Fill entrypoint
getWord( EntryPtr ) = functions[name];
}
loadStatic( name );
loadCode( name );
codeFile >> name;
}
}
void CAssembler::loadStatic( const std::string& function )
{
std::string name, var;
codeFile >> var;
checkSyntax( var, "{" );
codeFile >> var;
checkSyntax( var, "static" );
codeFile >> var;
checkSyntax( var, "{" );
codeFile >> name;
while( name != "}" ) {
codeFile >> var;
staticVariables[function]["&" + name] = dataPtr;
getWord( dataPtr++ ) = std::stoi( var );
codeFile >> name;
}
}
void CAssembler::loadCode( const std::string& function )
{
std::string command;
codeFile >> command;
while( command != "}" ) {
loadCommand( function, command );
codeFile >> command;
}
}
void CAssembler::loadCommand( const std::string& function, const std::string& command )
{
out.push_back( CommandCodes.at( command ) );
std::string firstArg, secondArg;
if( command == "push" || command == "pop" || command == "call" || command == "if" ||
command == "print" || command == "printconst" || command == "scan" || command == "exec") {
// Commands with one argument
codeFile >> firstArg;
int address = 0;
if( command == "call" || command == "exec" ) {
address = functions.at( firstArg );
} else if( command == "printconst" ) {
address = constants.at( firstArg );
} else {
address = firstArg[0] == '&' ? staticVariables.at( function ).at( firstArg ) :
variables.at( firstArg );
}
out.push_back( (address & 0x0000ff) );
out.push_back( (address & 0x00ff00) >> 8 );
out.push_back( (address & 0xff0000) >> 16 );
} else if( command == "copy" || command == "load" || command == "add" || command == "sub" ||
command == "isequal" ) {
// Commands with two arguments
codeFile >> firstArg >> secondArg;
int source = firstArg[0] == '&' ? staticVariables.at( function ).at( firstArg ) :
variables.at( firstArg );
int destination = secondArg[0] == '&' ? staticVariables.at( function ).at( secondArg ) :
variables.at( secondArg );
if( command == "load" ) { // in load first arg is large
out.push_back( (source & 0x00ff) );
out.push_back( (source & 0xff00) >> 8 );
out.push_back( destination & 0xff );
} else { // in other commands first arg is small
out.push_back( source & 0xff );
out.push_back( (destination & 0x00ff) );
out.push_back( (destination & 0xff00) >> 8 );
}
} else if( command == "return" || command == "exit" ) {
// Commands without arguments
out.push_back( 0 );
out.push_back( 0 );
out.push_back( 0 );
}
}
int& CAssembler::getWord( int ptr )
{
return *reinterpret_cast<int*>(&out[ptr * WordSize]);
}
void CAssembler::checkSyntax( const std::string& test, const std::string& original )
{
if( test != original ) {
throw std::runtime_error( "syntax error" );
}
}