xref: /aosp_15_r20/external/llvm/utils/DSAextract.py (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker#! /usr/bin/python
2*9880d681SAndroid Build Coastguard Worker
3*9880d681SAndroid Build Coastguard Worker#this is a script to extract given named nodes from a dot file, with
4*9880d681SAndroid Build Coastguard Worker#the associated edges.  An edge is kept iff for edge x -> y
5*9880d681SAndroid Build Coastguard Worker# x and y are both nodes specified to be kept.
6*9880d681SAndroid Build Coastguard Worker
7*9880d681SAndroid Build Coastguard Worker#known issues: if a line contains '->' and is not an edge line
8*9880d681SAndroid Build Coastguard Worker#problems will occur.  If node labels do not begin with
9*9880d681SAndroid Build Coastguard Worker#Node this also will not work.  Since this is designed to work
10*9880d681SAndroid Build Coastguard Worker#on DSA dot output and not general dot files this is ok.
11*9880d681SAndroid Build Coastguard Worker#If you want to use this on other files rename the node labels
12*9880d681SAndroid Build Coastguard Worker#to Node[.*] with a script or something.  This also relies on
13*9880d681SAndroid Build Coastguard Worker#the length of a node name being 13 characters (as it is in all
14*9880d681SAndroid Build Coastguard Worker#DSA dot output files)
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker#Note that the name of the node can be any substring of the actual
17*9880d681SAndroid Build Coastguard Worker#name in the dot file.  Thus if you say specify COLLAPSED
18*9880d681SAndroid Build Coastguard Worker#as a parameter this script will pull out all COLLAPSED
19*9880d681SAndroid Build Coastguard Worker#nodes in the file
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard Worker#Specifying escape characters in the name like \n also will not work,
22*9880d681SAndroid Build Coastguard Worker#as Python
23*9880d681SAndroid Build Coastguard Worker#will make it \\n, I'm not really sure how to fix this
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker#currently the script prints the names it is searching for
26*9880d681SAndroid Build Coastguard Worker#to STDOUT, so you can check to see if they are what you intend
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Workerimport re
29*9880d681SAndroid Build Coastguard Workerimport string
30*9880d681SAndroid Build Coastguard Workerimport sys
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Workerif len(sys.argv) < 3:
34*9880d681SAndroid Build Coastguard Worker	print 'usage is ./DSAextract <dot_file_to_modify> \
35*9880d681SAndroid Build Coastguard Worker			<output_file> [list of nodes to extract]'
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker#open the input file
38*9880d681SAndroid Build Coastguard Workerinput = open(sys.argv[1], 'r')
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker#construct a set of node names
41*9880d681SAndroid Build Coastguard Workernode_name_set = set()
42*9880d681SAndroid Build Coastguard Workerfor name in sys.argv[3:]:
43*9880d681SAndroid Build Coastguard Worker	node_name_set |= set([name])
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker#construct a list of compiled regular expressions from the
46*9880d681SAndroid Build Coastguard Worker#node_name_set
47*9880d681SAndroid Build Coastguard Workerregexp_list = []
48*9880d681SAndroid Build Coastguard Workerfor name in node_name_set:
49*9880d681SAndroid Build Coastguard Worker	regexp_list.append(re.compile(name))
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Worker#used to see what kind of line we are on
52*9880d681SAndroid Build Coastguard Workernodeexp = re.compile('Node')
53*9880d681SAndroid Build Coastguard Worker#used to check to see if the current line is an edge line
54*9880d681SAndroid Build Coastguard Workerarrowexp = re.compile('->')
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Workernode_set = set()
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker#read the file one line at a time
59*9880d681SAndroid Build Coastguard Workerbuffer = input.readline()
60*9880d681SAndroid Build Coastguard Workerwhile buffer != '':
61*9880d681SAndroid Build Coastguard Worker	#filter out the unnecessary checks on all the edge lines
62*9880d681SAndroid Build Coastguard Worker	if not arrowexp.search(buffer):
63*9880d681SAndroid Build Coastguard Worker		#check to see if this is a node we are looking for
64*9880d681SAndroid Build Coastguard Worker		for regexp in regexp_list:
65*9880d681SAndroid Build Coastguard Worker			#if this name is for the current node, add the dot variable name
66*9880d681SAndroid Build Coastguard Worker			#for the node (it will be Node(hex number)) to our set of nodes
67*9880d681SAndroid Build Coastguard Worker			if regexp.search(buffer):
68*9880d681SAndroid Build Coastguard Worker				node_set |= set([re.split('\s+',buffer,2)[1]])
69*9880d681SAndroid Build Coastguard Worker				break
70*9880d681SAndroid Build Coastguard Worker	buffer = input.readline()
71*9880d681SAndroid Build Coastguard Worker
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker#test code
74*9880d681SAndroid Build Coastguard Worker#print '\n'
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Workerprint node_name_set
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker#print node_set
79*9880d681SAndroid Build Coastguard Worker
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Worker#open the output file
82*9880d681SAndroid Build Coastguard Workeroutput = open(sys.argv[2], 'w')
83*9880d681SAndroid Build Coastguard Worker#start the second pass over the file
84*9880d681SAndroid Build Coastguard Workerinput = open(sys.argv[1], 'r')
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Workerbuffer = input.readline()
87*9880d681SAndroid Build Coastguard Workerwhile buffer != '':
88*9880d681SAndroid Build Coastguard Worker	#there are three types of lines we are looking for
89*9880d681SAndroid Build Coastguard Worker	#1) node lines, 2) edge lines 3) support lines (like page size, etc)
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker	#is this an edge line?
92*9880d681SAndroid Build Coastguard Worker	#note that this is no completely robust, if a none edge line
93*9880d681SAndroid Build Coastguard Worker	#for some reason contains -> it will be missidentified
94*9880d681SAndroid Build Coastguard Worker	#hand edit the file if this happens
95*9880d681SAndroid Build Coastguard Worker	if arrowexp.search(buffer):
96*9880d681SAndroid Build Coastguard Worker		#check to make sure that both nodes are in the node list
97*9880d681SAndroid Build Coastguard Worker		#if they are print this to output
98*9880d681SAndroid Build Coastguard Worker		nodes = arrowexp.split(buffer)
99*9880d681SAndroid Build Coastguard Worker		nodes[0] = string.strip(nodes[0])
100*9880d681SAndroid Build Coastguard Worker		nodes[1] = string.strip(nodes[1])
101*9880d681SAndroid Build Coastguard Worker		if nodes[0][:13] in node_set and \
102*9880d681SAndroid Build Coastguard Worker				nodes[1][:13] in node_set:
103*9880d681SAndroid Build Coastguard Worker					output.write(buffer)
104*9880d681SAndroid Build Coastguard Worker	elif nodeexp.search(buffer): #this is a node line
105*9880d681SAndroid Build Coastguard Worker		node = re.split('\s+', buffer,2)[1]
106*9880d681SAndroid Build Coastguard Worker		if node in node_set:
107*9880d681SAndroid Build Coastguard Worker			output.write(buffer)
108*9880d681SAndroid Build Coastguard Worker	else: #this is a support line
109*9880d681SAndroid Build Coastguard Worker		output.write(buffer)
110*9880d681SAndroid Build Coastguard Worker	buffer = input.readline()
111*9880d681SAndroid Build Coastguard Worker
112