-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryParser.cpp
More file actions
418 lines (350 loc) · 11.8 KB
/
BinaryParser.cpp
File metadata and controls
418 lines (350 loc) · 11.8 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
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
//Name: Maxine Xin
#include "BinaryParser.h"
#include "OpcodeTable.h"
#include <iomanip>
// filename: string type name of the file
// reads in a file containing lines of 32 bit binary string;
// check the syntax correctness of each binary string and whether it is an
// valid encoding of an ASM instruction;
// if valid, decode the binary and store the corresponding instruction.
BinaryParser::BinaryParser(string filename) {
myFormatCorrect = true;
Instruction i;
ifstream in;
in.open(filename.c_str());
if (in.bad())
myFormatCorrect = false;
else {
string line;
while (getline(in, line)) {
if (!checkSyntax(line)) {
//if there is something wrong with the length or there are
//other characters other than '0' and '1'
myFormatCorrect = false;
break;
}
//if the syntax is correct, check whether is a valid binary representation
//of an instruction, and decode and store instruction if valid
if (!decode2Inst(i, line)) {
myFormatCorrect = false;
break;
}
string asmStr = cvt2ASM(i);
i.setAssembly(asmStr);
i.setEncoding(line);
myInstructions.push_back(i);
}
}
in.close();
myIndex = 0;
return;
}
// Iterator that returns the next Instruction in the list of Instructions.
Instruction BinaryParser::getNextInstruction() {
if (myIndex < (int)(myInstructions.size())) {
myIndex++;
return myInstructions[myIndex - 1];
}
Instruction i;
return i;
}
// binaryLine: the line of the binary string to be checked
// returns true if that binary representation is correct in
// syntax(length, composed of 0's and 1's)
bool BinaryParser::checkSyntax(string binaryLine) {
if (binaryLine.length() != ENCODELEN)
return false;
//if the length is correct, check whether only composed of 1 or 0
for (int i = 0; i < ENCODELEN; i++) {
if (binaryLine.at(i) != '0' && binaryLine.at(i) != '1')
return false;
}
//length correct, and composed of only '0' or '1'
return true;
}
// binaryLine: the line of the binary string to be encoded
// returns true if that binary representation is a valid encoding of an ASM
// instruction, and stores the corresponding instruction to the instruction list;
// returns false if binaryLine is not a valid encoding.
bool BinaryParser::decode2Inst(Instruction &i, string binaryLine) {
//get Opcode
Opcode opcode = getOpcodeFromBinary(binaryLine);
// pass in only the opfield and func filed
//if opcode is UNDEFINED
if (opcode == UNDEFINED) {
return false;
}
InstType instType = myOpTable.getInstType(opcode);
if (instType == RTYPE)
return decode2InstR(binaryLine, opcode, i);
else if (instType == ITYPE)
return decode2InstI(binaryLine, opcode, i);
else
return decode2InstJ(binaryLine, opcode, i);
}
// binaryLine: the line of the binary string to be encoded
// op: the opcode of the instruction to be decoded into
// i: the instruction that will hold the newly created instruction
// returns false if encoding is invalid; otherwise, returns true and
// creates an R type instruction based on the binary encoding passed in
// (might not be an valid R instruction at this time)
bool BinaryParser::decode2InstR(string binary, Opcode op, Instruction &i) {
// get positions of each operand for the purpose of error checking
int rs_p = myOpTable.RSposition(op);
int rt_p = myOpTable.RTposition(op);
int rd_p = myOpTable.RDposition(op);
int imm_p = myOpTable.IMMposition(op);
int rs, rt, rd, imm;
int pos = OPLEN;
rs = cvtBinaryStr2Decimal(binary.substr(pos, RGSTLEN));
pos += RGSTLEN;
rt = cvtBinaryStr2Decimal(binary.substr(pos, RGSTLEN));
pos += RGSTLEN;
rd = cvtBinaryStr2Decimal(binary.substr(pos, RGSTLEN));
pos += RGSTLEN;
imm = cvtBinaryStr2Decimal(binary.substr(pos, IMMRLEN));
if (rs_p == -1) {
if (rs != 0)
return false;
else
rs = NUMREGISTERS;
}
if (rt_p == -1) {
if (rt != 0)
return false;
else
rt = NUMREGISTERS;
}
if (rd_p == -1) {
if (rd != 0)
return false;
else
rd = NUMREGISTERS;
}
if (imm_p == -1) {
if (imm != 0)
return false;
else
imm = 0;
}
i.setValues(op, rs, rt, rd, imm);
return true;
}
// binaryLine: the line of the binary string to be encoded
// op: the opcode of the instruction to be decoded into
// i: the instruction that will hold the newly created instruction
// returns false if encoding is invalid; otherwise, returns true and
// creates an I type instruction based on the binary encoding passed in
// (might not be an valid I instruction at this time)
bool BinaryParser::decode2InstI(string binary, Opcode op, Instruction &i) {
// get positions of each operand for the purpose of error checking
int rs_p = myOpTable.RSposition(op);
int rt_p = myOpTable.RTposition(op);
int imm_p = myOpTable.IMMposition(op);
int pos = OPLEN;
int rs, rt, rd, imm;
rs = cvtBinaryStr2Decimal(binary.substr(pos, RGSTLEN));
pos += RGSTLEN;
rt = cvtBinaryStr2Decimal(binary.substr(pos, RGSTLEN));
pos += RGSTLEN;
rd = NUMREGISTERS;
string immStr = binary.substr(pos, IMMILEN);
if (myOpTable.isIMMLabel(op)) {
//if imm is a label
immStr += MULT_4; //*4
}
signExtendImm(immStr);
imm = cvtIMMBinary2Decimal(immStr);
if (rs_p == -1) {
if (rs != 0)
return false;
else
rs = NUMREGISTERS;
}
if (rt_p == -1) {
if (rt != 0)
return false;
else
rt = NUMREGISTERS;
}
if (imm_p == -1) {
if (imm != 0)
return false;
else
imm = 0;
}
i.setValues(op, rs, rt, rd, imm);
return true;
}
// binaryLine: the line of the binary string to be encoded
// op: the opcode of the instruction to be decoded into
// i: the instruction that will hold the newly created instruction
// returns false if encoding is invalid; otherwise, returns true and
// creates an J type instruction based on the binary encoding passed in
// (might not be an valid J instruction at this time)
bool BinaryParser::decode2InstJ(string binary, Opcode op, Instruction &i) {
int imm_p = myOpTable.IMMposition(op);
int pos = OPLEN;
int rs, rt, rd, imm;
rs = rt = rd = NUMREGISTERS;
string immStr = binary.substr(pos, IMMJLEN);
immStr += MULT_4; // *4
immStr = FOREMOST_4_BITS_PC + immStr; // add 4 '0's to the front(the most significant 4 bits of PC)
imm = cvtIMMBinary2Decimal(immStr);
if (imm_p == -1) {
if (imm != 0)
return false;
else
imm = 0;
}
i.setValues(op, rs, rt, rd, imm);
return true;
}
// binaryStr: the binary string to be converted
// returns an integer representing the decimal value of the binary string
int BinaryParser::cvtBinaryStr2Decimal(string binaryStr) {
int decimal = 0;
for (unsigned int i = 0; i < binaryStr.length(); i++) {
decimal *= 2;
if (binaryStr.at(i) == '1')
decimal += 1;
}
return decimal;
}
// immBinary: the binary string of an immediate value
// returns a positive/negative integer representing the decimal value
// of the binary string
int BinaryParser::cvtIMMBinary2Decimal(string immBinary) {
bool isNeg = false;
string zero = "0";
string one = "1";
if (immBinary.at(0) == '1') //if negative
isNeg = true;
if (isNeg) {
for (unsigned int i = 0; i < immBinary.length(); i++) {
if (immBinary.at(i) == '0')
immBinary.replace(i, 1, one); //convert '0' to '1'
else
immBinary.replace(i, 1, zero); //convert '1' to '0'
}
}
int immDecimal = cvtBinaryStr2Decimal(immBinary);
if (isNeg) {
// plus 1 and then convert to negative
immDecimal++;
immDecimal -= 2 * immDecimal;
}
return immDecimal;
}
// i: instruction to be converted into ASM
// returns the string of ASM that represents the instruction
string BinaryParser::cvt2ASM(Instruction &i) {
Opcode op = i.getOpcode();
InstType type = myOpTable.getInstType(op);
if (type == RTYPE)
return convert2RTypeASM(op, i);
else if (type == ITYPE)
return convert2ITypeASM(op, i);
else //type == JTYPE
return convert2JTypeASM(op, i);
}
// operands: string array storing all operands based on their position
// name: name of the instruction
// isIMMLabel: boolean flag representing whether the imm field is an add label
// returns the string representation of the R type ASM instruction
string BinaryParser::convert2RTypeASM(Opcode op, Instruction &i) {
string strASM = "";
int numOperands = myOpTable.numOperands(op);
vector<string> operands (numOperands); // stores the string representation
// for each operand based on their position
string name = myOpTable.getInstName(op);
int rs_p = myOpTable.RSposition(op);
int rt_p = myOpTable.RTposition(op);
int rd_p = myOpTable.RDposition(op);
int imm_p = myOpTable.IMMposition(op);
if (rs_p != -1)
operands[rs_p] = myRgstTable.getName(i.getRS());
if (rt_p != -1)
operands[rt_p] = myRgstTable.getName(i.getRT());
if (rd_p != -1)
operands[rd_p] = myRgstTable.getName(i.getRD());
if (imm_p != -1) {
stringstream ss;
ss << dec << i.getImmediate();
operands[imm_p] = ss.str();
}
strASM += (name + " ");
for (unsigned int j = 0; j < operands.size() - 1; j++)
strASM += (operands[j] + ", ");
strASM += operands[operands.size() - 1];
return strASM;
}
// operands: string array storing all operands based on their position
// name: name of the instruction
// returns the string representation of the I type ASM instruction
string BinaryParser::convert2ITypeASM(Opcode op, Instruction &i) {
string strASM = "";
int numOperands = myOpTable.numOperands(op);
vector<string> operands (numOperands); // stores the string representation
// for each operand based on their position
string name = myOpTable.getInstName(op);
int rs_p = myOpTable.RSposition(op);
int rt_p = myOpTable.RTposition(op);
int imm_p = myOpTable.IMMposition(op);
if (rs_p != -1)
operands[rs_p] = myRgstTable.getName(i.getRS());
if (rt_p != -1)
operands[rt_p] = myRgstTable.getName(i.getRT());
if (imm_p != -1) {
stringstream ss;
if (myOpTable.isIMMLabel(op)) {
ss << "0x" << hex << i.getImmediate();
} else {
ss << dec << i.getImmediate();
}
operands[imm_p] = ss.str();
}
strASM += (name + " ");
if (myOpTable.isIMMMemory(op)) {
// if expects memory address
strASM += (operands[0] + ", " + operands[1] + "(" + operands[2] + ")");
} else {
for (unsigned int j = 0; j < operands.size() - 1; j++) {
strASM += (operands[j] + ", ");
}
strASM += operands[operands.size() - 1];
}
return strASM;
}
// operands: string array storing all operands based on their position
// name: name of the instruction
// returns the string representation of the J type ASM instruction
string BinaryParser::convert2JTypeASM(Opcode op, Instruction &i) {
string strASM = "";
string name = myOpTable.getInstName(op);
string immStr = "";
int imm_p = myOpTable.IMMposition(op);
if (imm_p != -1) {
stringstream ss;
ss << "0x" << hex << i.getImmediate();
immStr = ss.str();
}
strASM += (name + " " + immStr);
return strASM;
}
// string: the immediate value to be sign extended
// returns the string of the immediate value after sign extension
string BinaryParser::signExtendImm(string immStr) {
// append the first char of the string to the front until
// the length reaches 32
while (immStr.length() < ENCODELEN)
immStr = immStr.at(0) + immStr;
return immStr;
}
// string binaryLine: 32-bit encoding
// returns the Opcode representing the encoding
Opcode BinaryParser::getOpcodeFromBinary(string binaryLine) {
string opStr = binaryLine.substr(0, OPLEN);
string funcStr = binaryLine.substr(OPLEN + RGSTLEN * 3 + IMMRLEN, FUNCLEN);
return myOpTable.getOpcode(opStr, funcStr);
}