-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructiontable.cpp
48 lines (39 loc) · 1013 Bytes
/
instructiontable.cpp
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
#include "instructiontable.h"
#include <iostream>
InstructionTable::InstructionTable() :
instructionAddress(1),
table(),
jumpStack()
{}
InstructionTable::~InstructionTable() {}
void InstructionTable::generateInstruction(OP op, int oprand)
{
Entry entry;
entry.address = getInstructionAddress();
entry.op = op;
entry.oprand = oprand;
table.push_back(entry);
instructionAddress++;
}
int InstructionTable::getInstructionAddress() const
{
return instructionAddress;
}
void InstructionTable::addJumpAddress(int jumpAddress)
{
jumpStack.push(jumpAddress);
}
void InstructionTable::backPatch(int jumpAddress)
{
int address = jumpStack.top() - 1;
jumpStack.pop();
table.at(address).oprand = jumpAddress;
}
void InstructionTable::printTable(std::ofstream& file)
{
file << "Address\tOp code\tOperand" << std::endl;
for (auto &entry : table)
{
file << entry.address << "\t" << entry.enumVal() << "\t" << entry.oprand << std::endl;
}
}