[go: up one dir, main page]

Menu

[390b88]: / ir / cfg.py  Maximize  Restore  History

Download this file

320 lines (252 with data), 5.3 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'''Control Flow Graph (CFG) generation.'''
import aterm
import transf
import box
from transf import util
from transf import lib
from transf import parse
import ir.pprint
import dot
renderBox = (
util.Adaptor(lambda term: term.factory.makeStr(box.stringify(term))) +
lib.build.Str("???")
)
parse.Transfs(r'''
#######################################################################
# Graph Generation
shared stmtid
shared this
shared next
shared retn
shared brek
shared cont
shared nodes
getNodeId =
Label(label) -> label & box.escape +
Count()
makeNodeLabel =
( If(cond,_,_)
-> <ir.pprint.ppExpr cond>
| While(cond,_)
-> <ir.pprint.ppExpr cond>
| DoWhile(cond,_)
-> <ir.pprint.ppExpr cond>
| _
-> <ir.pprint.stmtKern>
| n(*)
-> n
) ;
renderBox ;
box.escape
makeNodeShape =
If
-> "diamond"
| While
-> "diamond"
| DoWhile
-> "diamond"
| Module
-> "point"
| Block
-> "point"
| NoStmt
-> "point"
| _
-> "box"
makeNodeUrl =
(path.get & box.reprz + !"" ) ;
box.escape
makeNodeAttrs =
![
!Attr("label", <makeNodeLabel>),
!Attr("shape", <makeNodeShape>),
!Attr("URL", <makeNodeUrl>)
]
MakeEdge(dst) =
!Edge(<dst>, [])
MakeLabelledEdge(dst, label) =
!Edge(<dst>, [Attr("label", <label ; box.escape>)])
AddNode(nodeid, attrs, edges) =
Where(
!Node(
<nodeid>,
<attrs>,
<edges>
) ;
nodes := ![_,*nodes]
)
GetTerminalNodeId(nodeid) =
NegInt(nodeid)
hasTerminalNode =
?Function
addTerminalNode =
AddNode(
!retn,
![
Attr("label", "\"\""),
Attr("shape", "doublecircle"),
Attr("style", "filled"),
Attr("fillcolor", "black")
],
![]
)
#######################################################################
# Flow Traversal
doStmts =
MapR(doStmt)
MakeNode(edges) =
AddNode(!this, makeNodeAttrs, edges)
doDefault =
MakeNode(![<MakeEdge(!next)>]) ;
next := !this
doIf =
with true, false, oldnext in
oldnext := !next ;
?If(_,
<with next in next := !oldnext; doStmt; true := !next end>,
<with next in next := !oldnext; doStmt; false := !next end>
) ;
MakeNode(![
<MakeLabelledEdge(!true, !"True")>,
<MakeLabelledEdge(!false, !"False")>,
]) ;
next := !this
end
doNoStmt =
id
doWhile =
with true in
?While(_, <with next in next := !this; doStmt; true := !next end> );
MakeNode(![
<MakeLabelledEdge(!true, !"True")>,
<MakeLabelledEdge(!next, !"False")>,
]) ;
next := !this
end
doDoWhile =
with false in
false := !next ;
next := !this ;
?DoWhile(_, <doStmt> );
MakeNode(![
<MakeLabelledEdge(!next, !"True")>,
<MakeLabelledEdge(!false, !"False")>,
])
end
doBreak =
MakeNode(![<MakeEdge(!brek)>]) ;
next := !this
doContinue =
MakeNode(![<MakeEdge(!cont)>]) ;
next := !this
doRet =
MakeNode(![<MakeEdge(!retn)>]) ;
next := !this
doGoTo =
with
label
in
?GoTo(Sym(label)) &
MakeNode(![<MakeEdge(!label ; box.escape)>]) +
MakeNode(![])
end ;
next := !this
doBlock =
?Block(<doStmts>)
doFunction =
oldnext := !next ;
with next, retn, brek, cont in
next := !oldnext ;
retn := GetTerminalNodeId(!this) ;
brek := !0 ;
cont := !0 ;
?Function(_, _, _, <doStmts>) ;
MakeNode(![<MakeEdge(!next)>]) ;
addTerminalNode
end
doModule =
with next, retn, brek, cont in
next := !0 ;
retn := !0 ;
brek := !0 ;
cont := !0 ;
?Module(<doStmts>) ;
addTerminalNode
end
doStmt =
with this in
this := getNodeId ;
switch project.name
case "If": doIf
case "While": doWhile
case "DoWhile": doDoWhile
case "Break": doBreak
case "GoTo": doGoTo
case "Continue": doContinue
case "NoStmt": doNoStmt
case "Ret": doRet
case "Block": doBlock
case "Function": doFunction
case "Module": doModule
else doDefault
end
end
makeGraph =
with nodes, stmtid in
nodes := ![] ;
stmtid := !0 ;
doModule ;
AddNode(!"edge",![Attr("fontname","Arial")],![]) ;
AddNode(!"node",![Attr("fontname","Arial")],![]) ;
!Graph(nodes)
end
#######################################################################
# Graph Simplification
shared point as table
matchPointShapeAttr =
?Attr("shape", "point")
findPointNode =
?Node(src, <One(matchPointShapeAttr)>, [Edge(dst, _)]) ;
point.set [src, dst]
findPointNodes =
Map(Try(findPointNode))
replaceEdge =
~Edge(<~point>, _)
removePointNode =
~Node(<Not(?point)>, _, <Map(Try(replaceEdge))>)
removePointNodes =
Filter(removePointNode)
simplifyPoints =
with point in
~Graph(<findPointNodes; removePointNodes>)
end
simplifyGraph = simplifyPoints
#######################################################################
render = makeGraph ; simplifyGraph
''')
#######################################################################
# Example
def main():
import aterm.factory
import sys
factory = aterm.factory.factory
for arg in sys.argv[1:]:
sys.stderr.write("* Reading aterm\n")
term = factory.readFromTextFile(file(arg, 'rt'))
#print ( ir.pprint.module * renderBox )(term)
#print term
#print
sys.stderr.write("* Making Graph\n")
term = makeGraph(term)
#print term
#print
sys.stderr.write("* Simplifying Graph\n")
term = simplifyGraph (term)
#print term
#print
sys.stderr.write("* Generating DOT\n")
term = simplifyGraph (term)
dotcode = dot.stringify(term)
sys.stdout.write(dotcode)
if __name__ == '__main__':
main()