-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
156 lines (133 loc) · 3.74 KB
/
Node.cpp
File metadata and controls
156 lines (133 loc) · 3.74 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
#include "Node.h"
#include "RocketShip.h"
#include "llvm/Instructions.h"
#include "llvm/Support/raw_ostream.h"
Node::Node(int identifier, Type type) :
_nodeId(identifier),
_nodeType(type),
_nodeName(""),
_instruction(NULL)
{
}
Node::~Node()
{
}
int
Node::getNodeId()
{
return _nodeId;
}
Node::Type
Node::getNodeType()
{
return _nodeType;
}
std::vector<Edge*>
Node::getNodeEdges()
{
return _edges;
}
std::string
Node::getNodeName()
{
return _nodeName;
}
std::string
Node::getNodeLabel()
{
return _nodeLabel;
}
void
Node::setNodeId(int value)
{
_nodeId = value;
}
void
Node::setNodeType(Type value)
{
_nodeType = value;
}
void
Node::setNodeName(std::string value)
{
_nodeName = value;
}
void
Node::setNodeLabel(std::string value)
{
_nodeLabel = value;
}
void
Node::setInstruction(llvm::Instruction* instruction)
{
_instruction = instruction;
}
void
Node::addNodeEdge(Edge* edge)
{
// Edges pointing to the same location are not allowed. Each edge
// must have a distinct id.
bool found = false;
for (std::vector<Edge*>::iterator it = _edges.begin();
it != _edges.end();
it++) {
if ((*it)->getId() == edge->getId()) {
found = true;
break;
}
}
if (!found) {
_edges.push_back(edge);
}
}
void
Node::removeNodeEdge(Edge* edge)
{
for (std::vector<Edge*>::iterator it = _edges.begin();
it != _edges.end();
it++) {
if ((*it)->getId() == edge->getId()) {
_edges.erase(it);
break;
}
}
}
std::map<std::string, llvm::BasicBlock*>
Node::getBlockEdges()
{
std::map<std::string, llvm::BasicBlock*> result;
if (_instruction) {
// Get Branch Instruction edges
if (llvm::BranchInst* instruction = llvm::dyn_cast<llvm::BranchInst>(&*_instruction)) {
if (instruction->isConditional()) {
setNodeType(Node::DECISION);
llvm::BasicBlock *Dest1 = llvm::dyn_cast<llvm::BasicBlock>(instruction->getOperand(1));
llvm::BasicBlock *Dest2 = llvm::dyn_cast<llvm::BasicBlock>(instruction->getOperand(2));
result.insert(std::pair<std::string, llvm::BasicBlock*>("false", Dest1));
result.insert(std::pair<std::string, llvm::BasicBlock*>("true", Dest2));
} else {
llvm::BasicBlock *Dest = llvm::dyn_cast<llvm::BasicBlock>(instruction->getOperand(0));
result.insert(std::pair<std::string, llvm::BasicBlock*>("x", Dest));
}
}
// Get Switch instruction edges
else if (llvm::SwitchInst* instruction = llvm::dyn_cast<llvm::SwitchInst>(&*_instruction)) {
setNodeType(Node::DECISION);
llvm::BasicBlock *defaultDest = instruction->getDefaultDest();
result.insert(std::pair<std::string, llvm::BasicBlock*>("default",defaultDest));
for (unsigned int i = instruction->getNumSuccessors() - 1; i > 0; i--) {
llvm::BasicBlock* destination = instruction->getSuccessor(i);
std::string label = rocketship::RocketShip::getValueName(instruction->getCaseValue(i));
result.insert(std::pair<std::string, llvm::BasicBlock*>(label, destination));
}
}
// Get Invoke instruction edges
else if (llvm::InvokeInst* instruction = llvm::dyn_cast<llvm::InvokeInst>(&*_instruction)) {
llvm::BasicBlock *normal = instruction->getNormalDest();
llvm::BasicBlock *unwind = instruction->getUnwindDest();
result.insert(std::pair<std::string, llvm::BasicBlock*>("unwind", unwind));
result.insert(std::pair<std::string, llvm::BasicBlock*>("", normal));
}
}
return result;
}