-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
82 lines (61 loc) · 2.54 KB
/
graph.py
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
class graph(object):
def __init__(self, basic_blocks):
self.root = node(basic_blocks[0])
self.block_list = []
for basic_block in basic_blocks:
self.block_list.append( node(basic_block) )
for basic_block in self.block_list:
basic_block.setSuccs(self.block_list)
basic_block.setPreds(self.block_list)
self.blocks = len(self.block_list)
self.edges = 0
self.size = 0
for block in self.block_list:
self.edges += block.getCountOfInEdge()
self.edges += block.getCountOfOutEdge()
self.size += block.getBlockSize()
def getGraphBlocks(self):
return self.blocks
def getGraphEdges(self):
return self.edges
def getGraphSize(self):
return self.size
class node(object):
def __init__(self, block_info):
self.number = block_info['number']
self.addr = block_info['addr']
self.succsAddr = block_info['succs']
self.predsAddr = block_info['preds']
self.size = block_info['size']
#self.mnemonics = block_info['mnemonics']
self.succs = []
self.preds = []
def setSuccs(self, block_list):
for basic_block in block_list:
for succAddr in self.succsAddr:
if succAddr == basic_block.addr:
self.succs.append(basic_block.number)
#self.succs.append(basic_block)
def setPreds(self, block_list):
for basic_block in block_list:
for predAddr in self.predsAddr:
if predAddr == basic_block.addr:
self.preds.append(basic_block.number)
#self.preds.append(basic_block)
def getCountOfOutEdge(self):
return len(self.succs)
def getCountOfInEdge(self):
return len(self.preds)
def getBlockSize(self):
return self.size
def getDeleteCost(self):
cost = self.getCountOfInEdge() + self.getCountOfOutEdge() + 1
return cost
def getMatchingCost(self, node):
gapOutEdge = abs(self.getCountOfOutEdge() - node.getCountOfOutEdge())
gapInEdge = abs(self.getCountOfInEdge() - node.getCountOfInEdge())
gapSize = abs(self.getBlockSize() - node.getBlockSize())
matchingCost = gapOutEdge + gapInEdge + gapSize
return matchingCost
def __str__(self):
return 'number : {} addr : {} succsAddr : {} predsAddr : {} succsNode : {} predsNode : {}'.format(self.number, self.addr, self.succsAddr, self.predsAddr, self.succs, self.preds)