-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinker.cc
81 lines (73 loc) · 2.29 KB
/
linker.cc
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
#include "linker.h"
#include "bytes.h"
void Linker::link()
{
if (!is_silent)
printEveryLabelLink();
insertIndexInsideEveryInstruction();
for (auto origin : labelOriginMap)
{
std::string label = origin.first;
for (BranchLabel *label_dest : origin.second)
{
int destinationAddress =
labelDestMap[label]->relativeAddress -
label_dest->branch->relativeAddress;
Bytes number = Bytes(destinationAddress);
label_dest->firstByte->debugAddress = labelDestMap[label]->relativeAddress;
label_dest->firstByte->immediate = number.getNthByte(2);
label_dest->secondByte->immediate = number.getNthByte(3);
// Prints
if (!is_silent)
{
std::cout << "LINKER PRINTS\n";
std::cout << label << " -> " << label_dest->to_string() << "\n";
std::cout << "destinationAddress: " << destinationAddress << "\n";
std::cout << "first Byte : " << label_dest->firstByte->to_string() << "\n";
std::cout << "second Byte: " << label_dest->secondByte->to_string() << "\n";
std::cout << "branch position: " << label_dest->branch->relativeAddress + offset << "\n";
std::cout << "absolute position: " << labelDestMap[label]->relativeAddress + offset << "\n";
}
}
}
}
void Linker::printEveryLabelLink()
{
for (auto item : labelDestMap)
{
std::string label = item.first;
Instruction *label_dest = item.second;
std::cout << label << " -> " << label_dest->name << "\n";
}
}
void Linker::insertIndexInsideEveryInstruction()
{
int i = 0;
for (Instruction *inst : code)
inst->relativeAddress = i++;
}
Linker *Linker::withCode(std::vector<Instruction *> _code)
{
code = _code;
return this;
}
Linker *Linker::withDestMap(std::map<std::string, Instruction *> map)
{
labelDestMap = map;
return this;
}
Linker *Linker::withOriginMap(std::map<std::string, std::vector<BranchLabel *>> map)
{
labelOriginMap = map;
return this;
}
Linker *Linker::withOffset(int value)
{
offset = value;
return this;
}
Linker *Linker::setDebugMode(bool isSilent)
{
is_silent = isSilent;
return this;
}