-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstrFactory.cpp
More file actions
485 lines (366 loc) · 20.9 KB
/
InstrFactory.cpp
File metadata and controls
485 lines (366 loc) · 20.9 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
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
#include "InstrFactory.h"
InstrFactory::InstrFactory(int dataMemorySize, int instructionMemorySize) :dataMemorySize(dataMemorySize), instructionMemorySize(instructionMemorySize)
{
}
InstrFactory::~InstrFactory()
{
}
opcode InstrFactory::decideInstrType(std::string str_opcode)
{
opcode int_opcode;
if (str_opcode == "ADD") {
int_opcode = ADD;
}
else {
if (str_opcode == "NEG") {
int_opcode = NEG;
}
else {
if (str_opcode == "MUL") {
int_opcode = MUL;
}
else {
if (str_opcode == "JMP") {
int_opcode = JMP;
}
else {
if (str_opcode == "JMP0") {
int_opcode = JMP0;
}
else {
if (str_opcode == "ASS") {
int_opcode = ASS;
}
else {
if (str_opcode == "LE") {
int_opcode = LE;
}
else {
if (str_opcode == "READ") {
int_opcode = READ;
}
else {
if (str_opcode == "WRITE") {
int_opcode = WRITE;
}
else {
if (str_opcode == "HALT") {
int_opcode = HALT;
}
else {
int_opcode = UNDEFINED;
}
}
}
}
}
}
}
}
}
}
return int_opcode;
}
Instr* InstrFactory::StrtoInstr(std::string instr, int instruction_num) // need to parse the line and extract useful fields from it
{
int space_index = instr.find(" "); // to allocate the opcode
std::string str_opcode;
opcode int_opcode;
if (space_index != std::string::npos) // if the white space is found
{
// extract the opcode
str_opcode = instr.substr(0, space_index);
int_opcode = decideInstrType(str_opcode);
int in1, in2, out;
int in, in_type;
int in1_type, in2_type;
int addr;
switch (int_opcode) {
case ADD:
parseThreeOperandsInstr(instr.substr(space_index + 1, instr.length() - space_index - 1), in1, in1_type, in2, in2_type, out, instruction_num); // consider the operands string only after the whitespace
return new AddInstr(in1_type, in1, in2_type, in2, out);
case MUL:
parseThreeOperandsInstr(instr.substr(space_index + 1, instr.length() - space_index - 1), in1, in1_type, in2, in2_type, out, instruction_num); // consider the operands string only after the whitespace
return new MulInstr(in1_type, in1, in2_type, in2, out);
case LE:
parseThreeOperandsInstr(instr.substr(space_index + 1, instr.length() - space_index - 1), in1, in1_type, in2, in2_type, out, instruction_num); // consider the operands string only after the whitespace
return new LeInstr(in1_type, in1, in2_type, in2, out);
case NEG:
parseTwoOperandInstr(instr.substr(space_index + 1, instr.length() - space_index - 1), in, in_type, out, instruction_num); // consider the operands string only after the whitespace
return new NegInstr(in_type, in, out);
case ASS:
parseTwoOperandInstr(instr.substr(space_index + 1, instr.length() - space_index - 1), in, in_type, out, instruction_num); // consider the operands string only after the whitespace
return new AssInstr(in_type, in, out);
case JMP:
parseJmpInstr(instr.substr(space_index + 1, instr.length() - space_index - 1), addr, instruction_num); // consider the operands string only after the whitespace
return new JmpInstr(addr);
case JMP0:
parseJmp_0Instr(instr.substr(space_index + 1, instr.length() - space_index - 1), in, in_type, out, instruction_num); // consider the operands string only after the whitespace
return new Jmp_0Instr(in_type, in, out);
case READ:
parseReadInstr(instr.substr(space_index + 1, instr.length() - space_index - 1), addr, instruction_num);
return new ReadInstr(addr);
case WRITE:
parseWriteInstr(instr.substr(space_index + 1, instr.length() - space_index - 1), in, in_type, instruction_num);
return new WriteInstr(in_type, in);
case HALT:
// has no argument so needs no parsing!
return new HaltInstr();
case UNDEFINED:
throw UnsupportedOpcodeException("Instruction number " + std::to_string(instruction_num) + " is an unsupported Instruction!"); // !!!!!! raise exception!!!! unsupported instruction opcode !!!
}
}
else
{
// wrong format
throw WrongInstrFormatException("Opcode in instruction number " + std::to_string(instruction_num) + " must be followed by a white space!"); // !!!!!! raise exception!!!! wrong format in the instruction, opcode must be followed by an empty space !!!
}
}
// parses all instructions that take 2 input values and a single output value
void InstrFactory::parseThreeOperandsInstr(std::string str_operands, int& in1, int& in1_type, int& in2, int& in2_type, int& out, int instruction_num) {
std::string str_in1, str_in2, str_out;
// Extract the 3 strings representing each of the 2 inputs as well as the output string
int index = str_operands.find(",");
if (index != std::string::npos) { // if the ',' is found
if (index == 0 || index == str_operands.length() - 1) { // this indicates that at least 1 of the 3 required operands is missing!!
throw WrongInstrFormatException("Missing Operands for instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! instruction is missing 1 or more operands!!!
}
// Extracting the 1st operand appearing before the ','
str_in1 = str_operands.substr(0, index);
// Checking if in1 address or value
int dollar_index = str_in1.find("$");
if (dollar_index == std::string::npos) { // didn't find it so it's a value not address
in1 = stoi(str_in1);
in1_type = 0; // indicating that it's a value not an address
}
else { // found it so it's an address
int temp_address = stoi(str_in1.substr(dollar_index + 1, str_in1.length() - dollar_index - 1));
if (temp_address < 0 || temp_address >= dataMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Data Memory Address must be > 0 and < " + std::to_string(dataMemorySize) + "in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the data memory !!!
}
in1 = temp_address;
in1_type = 1; // indicating that it's an address not a value
}
// searching for the next ',' preceding th 2nd operand
str_operands = str_operands.substr(index + 1, str_operands.length() - index - 1);
index = str_operands.find(",");
if (index != std::string::npos) { // if the ',' is found
if (index == 0 || index == str_operands.length() - 1) { // this indicates that at least 1 of the 3 required operands is missing!!
throw WrongInstrFormatException("Missing Operands for instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! instruction is missing 1 or more operands!!!
}
// Extracting the 2nd operand appearing before the ','
str_in2 = str_operands.substr(0, index);
// checking if in2 is address or value
dollar_index = str_in2.find("$");
if (dollar_index == std::string::npos) { // didn't find it so it's a value not address
in2 = stoi(str_in2);
in2_type = 0; // indicating that it's a value not an address
}
else { // found it so it's an address
int temp_address = stoi(str_in2.substr(dollar_index + 1, str_in2.length() - dollar_index - 1));
if (temp_address < 0 || temp_address >= dataMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Data Memory Address must be > 0 and < " + std::to_string(dataMemorySize) + "in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the data memory !!!
}
in2 = temp_address;
in2_type = 1; // indicating that it's an address not a value
}
// extracting the last operand after the 2nd ','
str_out = str_operands.substr(index + 1, str_operands.length() - index - 1);
dollar_index = str_out.find("$");
if (dollar_index == std::string::npos) { // didn't find it error!!
throw WrongInstrFormatException("Output Operand must be sent as an address in the Data Memory not a constant in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! out operand must be an address!!!
}
else { // it's an address, need to check that it's within the proper range
int temp_address = stoi(str_out.substr(dollar_index + 1, str_out.length() - dollar_index - 1));
if (temp_address < 0 || temp_address >= dataMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Data Memory Address must be > 0 and < " + std::to_string(dataMemorySize) + "in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the data memory !!!
}
out = temp_address;
}
}
else {
throw WrongInstrFormatException("Operands in instruction number " + std::to_string(instruction_num) + " must be separated by a ','!"); // !!!!!! raise exception!!!! wrong format in the instruction, missing a ',' !!!
}
}
else {
throw WrongInstrFormatException("Operands in instruction number " + std::to_string(instruction_num) + " must be separated by a ','!"); // !!!!!! raise exception!!!! wrong format in the instruction, missing a ',' !!!
}
}
void InstrFactory::parseTwoOperandInstr(std::string str_operands, int& in, int& in_type, int& out, int instruction_num) {
std::string str_in, str_out;
// extract the 2 strings representing each of the input and output
int index = str_operands.find(",");
if (index != std::string::npos) { // if the , is found
if (index == 0 || index == str_operands.length() - 1) { // this indicates that at least 1 of the 2 required operands is missing!!
throw WrongInstrFormatException("Missing Operands for instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! instruction is missing 1 or more operands!!!
}
// if it passed up to here then we are sure that there are 2 operands
// reading the first operand existing before the ,
str_in = str_operands.substr(0, index);
// checking if the operand is address or value
int dollar_index = str_in.find("$");
if (dollar_index == std::string::npos) { // didn't find it so it's a value not address
in = stoi(str_in);
in_type = 0; // indicating that it's a value not an address
}
else { // found $ so it's an address
// Extract the address coming after the $
int temp_address = stoi(str_in.substr(dollar_index + 1, str_in.length() - dollar_index - 1));
if (temp_address < 0 || temp_address >= dataMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Data Memory Address must be > 0 and < " + std::to_string(dataMemorySize) + "in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the data memory !!!
}
in = temp_address;
in_type = 1; // indicating that it's an address not a value
}
// Extract the operand coming after the ,
str_out = str_operands.substr(index + 1, str_operands.length() - index - 1);
dollar_index = str_out.find("$");
if (dollar_index == std::string::npos) { // the out operand isn't passed as an address, which isn't allowed
throw WrongInstrFormatException(" Output Operand must be sent as an address in the Data Memory not a constant in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! out operand must be an address!!!
}
else { // it's an address, need to check that it's within the proper range
// Extract the address coming after the $
int temp_address = stoi(str_out.substr(dollar_index + 1, str_out.length() - dollar_index - 1));
if (temp_address < 0 || temp_address >= dataMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Data Memory Address must be > 0 and < " + std::to_string(dataMemorySize) + "in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the data memory !!!
}
out = temp_address;
}
}
else { // missing ','
throw WrongInstrFormatException(" Operands in instruction number " + std::to_string(instruction_num) + " must be separated by a ','!"); // !!!!!! raise exception!!!! wrong format in the instruction, missing a ',' !!!
}
}
void InstrFactory::parseJmpInstr(std::string str_operands, int& addr, int instruction_num) {
// must make sure that the passed operand is an address and that it's within the instruction memory range
int temp_addr;
// extract the string representing the instr_mem address to move to
int index = str_operands.find("$");
if (index == 0) { // if the $ is found in the beginning, then it's an address
temp_addr = stoi(str_operands.substr(index + 1, str_operands.length() - index - 1));
// Check that this address is within the size of the instruction Memory!
if (temp_addr < 0 || temp_addr >= instructionMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Can't Jump to an address that is not within the range of the Instruction Memory in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the Instruction memory !!!
}
addr = temp_addr;
}
else { // it's not an address
throw WrongInstrFormatException(" Jump Instr Operand must be sent as an address in the Instruction Memory not a constant in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! out operand must be an address!!!
}
}
void InstrFactory::parseJmp_0Instr(std::string str_operands, int& in, int& in_type, int& out, int instruction_num) {
std::string str_in, str_out;
// extract the 2 strings representing each of the input and output
int index = str_operands.find(",");
if (index != std::string::npos) { // if the , is found
if (index == 0 || index == str_operands.length() - 1) { // this indicates that at least 1 of the 2 required operands is missing!!
throw WrongInstrFormatException("Missing Operands for instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! instruction is missing 1 or more operands!!!
}
// if it passed up to here then we are sure that there are 2 operands
// reading the first operand existing before the ,
str_in = str_operands.substr(0, index);
// checking if the operand is address or value
int dollar_index = str_in.find("$");
if (dollar_index == std::string::npos) { // didn't find it so it's a value not address
in = stoi(str_in);
in_type = 0; // indicating that it's a value not an address
}
else { // found $ so it's an address
// Extract the address coming after the $
int temp_address = stoi(str_in.substr(dollar_index + 1, str_in.length() - dollar_index - 1));
if (temp_address < 0 || temp_address >= dataMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Data Memory Address must be > 0 and < " + std::to_string(dataMemorySize) + "in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the data memory !!!
}
in = temp_address;
in_type = 1; // indicating that it's an address not a value
}
// Extract the operand coming after the ,
str_out = str_operands.substr(index + 1, str_operands.length() - index - 1);
dollar_index = str_out.find("$");
if (dollar_index == std::string::npos) { // the out operand isn't passed as an address, which isn't allowed
throw WrongInstrFormatException("Output Operand must be sent as an address from the Instruction Memory not a constant in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! out operand must be an address!!!
}
else { // it's an address, need to check that it's within the proper range
// Extract the address coming after the $
int temp_address = stoi(str_out.substr(dollar_index + 1, str_out.length() - dollar_index - 1));
// Check that this address is within the size of the instruction Memory!
if (temp_address < 0 || temp_address >= instructionMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Can't Jump to an address that is not within the range of the Instruction Memory in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the Instruction memory !!!
}
out = temp_address;
}
}
else { // missing ','
throw WrongInstrFormatException(" Operands in instruction number " + std::to_string(instruction_num) + " must be separated by a ','!"); // !!!!!! raise exception!!!! wrong format in the instruction, missing a ',' !!!
}
}
void InstrFactory::parseReadInstr(std::string str_operands, int& addr, int instruction_num) {
// must make sure that the passed operand is an address and that it's within the data memory range
int temp_addr;
// extract the string representing the data_mem address to move to
int index = str_operands.find("$");
if (index == 0) { // if the $ is found in the beginning, then it's an address
temp_addr = stoi(str_operands.substr(index + 1, str_operands.length() - index - 1));
// Check that this address is within the size of the instruction Memory!
if (temp_addr < 0 || temp_addr >= dataMemorySize) { // invalid address index
throw WrongMemoryAddressException("Can't Read from an address that is not within the range of the Data Memory in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the data memory !!!
}
addr = temp_addr;
}
else { // it's not an address
throw WrongInstrFormatException("Read Instr Operand must be sent as an address in the Data Memory not a constant in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! out operand must be an address!!!
}
}
void InstrFactory::parseWriteInstr(std::string str_operands, int& in, int& in_type, int instruction_num) {
// checking if the operand is address or value
int dollar_index = str_operands.find("$");
if (dollar_index == std::string::npos) { // didn't find it so it's a value not address
in = stoi(str_operands);
in_type = 0; // indicating that it's a value not an address
}
else { // found $ so it's an address
// Extract the address coming after the $
int temp_address = stoi(str_operands.substr(dollar_index + 1, str_operands.length() - dollar_index - 1));
if (temp_address < 0 || temp_address >= dataMemorySize) { // invalid address index
throw WrongMemoryAddressException(" Read Instruction must read from a valid Data Memory Address in instruction number " + std::to_string(instruction_num) + "!"); // !!!!!! raise exception!!!! passed address is out of range of the data memory !!!
}
in = temp_address;
in_type = 1; // indicating that it's an address not a value
}
}
int InstrFactory::ParseFile(std::string file_name, Instr* instruction_memory[]) {
std::ifstream input_file_stream;
std::string instruction_line;
std::string last_line;
int instruction_num = 0;
// Open the file
input_file_stream.open(file_name);
// check if the file wasn't successfully opened, raise exception
if (input_file_stream.is_open()) { // the file was successfully opened
while (getline(input_file_stream, instruction_line) && (instruction_num < instructionMemorySize)) { // read a single instruction as a string
last_line = instruction_line;
if (!instruction_line.empty()) { // if the parsed line isn't empty
instruction_memory[instruction_num] = StrtoInstr(instruction_line, instruction_num);
instruction_num += 1;
}
// ignore empty lines
}
input_file_stream.close(); // closing the file
if (instruction_num == instructionMemorySize) {
throw WrongMemoryAddressException(" Exceeded the size of the Instruction Memory! Can't load instructions anymore! "); //!!!!!!!! need to raise an exception indicating that the size of instruction memory is exceeded!!!
}
// You might need to use this output just for debugging
//std::cout << "The last instruction is " << last_line << std::endl;
// If you didn't exceed the size of the instruction memory, you are good,
// check if the last instruction is a Halt or else raise an exception
int space_index = last_line.find(" "); // to allocate the opcode
std::string str_opcode = last_line.substr(0, space_index);
if (str_opcode != "HALT") { // checking on the opcode of the last instruction
throw HaltException();
}
}
else {
throw FileAccessException(); //!!!!!!!!!!!! need to raise an exception indicating that the file wasn't read!!!
}
return (instruction_num);
}