-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphDrawer.cpp
147 lines (119 loc) · 3.71 KB
/
GraphDrawer.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
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
//
// Created by tmpark on 2/2/16.
//
#include "GraphDrawer.h"
GraphDrawer *GraphDrawer::_graphDrawer = 0;
GraphDrawer* GraphDrawer::instance() {
if(!_graphDrawer)
_graphDrawer = new GraphDrawer();
return _graphDrawer;
}
RC GraphDrawer::createFile(const string &fileName)
{
destroyFile(fileName);
const char* fileName_char = fileName.c_str();
fstream file_to_create;
file_to_create.open(fileName_char, fstream::out | fstream:: binary); //Create a file (do not use in when creating a file)
if(file_to_create.is_open())
{
file_to_create.close();
return 1;
}
return -1;
}
RC GraphDrawer::openFile(const std::string &fileName) {
fileStream.open(fileName.c_str(), std::fstream::in | std::fstream::out);
if(!fileStream.is_open())
{
return -1;
}
return 1;
}
RC GraphDrawer::destroyFile(const string &fileName)
{
const char* fileName_char = fileName.c_str();
RC success = remove(fileName_char); //delete file
if(success == 0)
{
return 0; //successful
}
return -1; //A file still exists
}
RC GraphDrawer::closeFile() {
fileStream.close();
return 1;
}
void GraphDrawer :: writePreliminary(GRAPHTYPE graphType,string functionName){
if(!fileStream.is_open())
return;
string graphTypeString;
string edgeType;
if(graphType == graph_CFG) {
edgeType = string("digraph \"");
graphTypeString = "Control-flow Graph";
}
else if(graphType == graph_DT) {
edgeType = string("digraph \"");
graphTypeString = "Dominator Tree";
}
else if(graphType == graph_IG) {
edgeType = string("graph \"");
graphTypeString = "Interference Graph";
}
string prelimi = edgeType + graphTypeString + string(" for \'" + functionName + "\" {\n")
+ string("label=\"")+ graphTypeString + string(" for \'") + functionName + "\' function\";\n\n";
fileStream.write(prelimi.c_str(),prelimi.size());
return;
}
void GraphDrawer :: writeNodeStart(int blockNum,string blockName){
if(!fileStream.is_open())
return;
string nodeStart = string("Node") + to_string(blockNum) + string(" [shape=record, label=\"{") +
"[" + to_string(blockNum) + "]" + blockName + string(":\\l");
fileStream.write(nodeStart.c_str(),nodeStart.size());
}
void GraphDrawer :: writeCode(string codeString)
{
if(!fileStream.is_open())
return;
codeString = " " + codeString + "\\l";
fileStream.write(codeString.c_str(),codeString.size());
}
void GraphDrawer :: writeCodeForCond()
{
if(!fileStream.is_open())
return;
string codeString = "|{<s0>T|<s1>F}";
fileStream.write(codeString.c_str(),codeString.size());
}
void GraphDrawer :: writeNodeEnd(){
if(!fileStream.is_open())
return;
string endString = "}\"];\n";
fileStream.write(endString.c_str(),endString.size());
}
void GraphDrawer :: writeEdge(int sourceNum, int targetNum, EDGETYPE edgeType, GRAPHTYPE graphType)
{
if(!fileStream.is_open())
return;
string edge = string("\t") + string("Node") + to_string(sourceNum);
if(edgeType == edge_true)
edge = edge + string(":s0");
else if(edgeType == edge_false)
{
edge = edge + string(":s1");
}
string edgeShape;
if(graphType == graph_CFG || graphType == graph_DT)
edgeShape = string(" -> ");
else if(graphType == graph_IG)
edgeShape = string(" -- ");
edge = edge + edgeShape + string("Node") + to_string(targetNum) + string(";\n");
fileStream.write(edge.c_str(),edge.size());
}
void GraphDrawer :: writeEnd(){
if(!fileStream.is_open())
return;
string endString = "}\n";
fileStream.write(endString.c_str(),endString.size());
}