-
Notifications
You must be signed in to change notification settings - Fork 1
/
xcgr.sex
551 lines (415 loc) · 15.1 KB
/
xcgr.sex
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; File: xcgr.sex ;;
;; Project: the specializer Unmix ;;
;; Author: S.A.Romanenko, the Institute for Applied ;;
;; Mathematics, the USSR Acedemy of Sciences, ;;
;; Moscow. ;;
;; Credits: The program is based on the corresponding ;;
;; phase of the specializer Mix made by ;;
;; Peter Sestoft and Kim H|glund ;;
;; (The Mix Group), mix@diku.UUCP, ;;
;; at the University of Copenhagen. ;;
;; Created: 5 May 1989 ;;
;; Revised: 7 December 1989 ;;
;; 9 April 1990 ;;
;; August 1990 ;;
;; ;;
;; Contents: Call Graph Reducer ;;
;; ;;
;; Synopsis: ;;
;; (ucgr:main src dst prog) ;;
;; ;;
;; src - source program name ;;
;; dst - destination program name ;;
;; prog - a Mixwell program ;;
;; ;;
;; Description: ;;
;; Reduces the call graph of the Mixwell program ;;
;; "prog". ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Call Graph Reduction ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Structures used by the Call Graph Reducer
;; ==============================================
;;
;; CycleHeadList:
;;
;; <CycleHeadList> ::= ( <Fname>* )
;;
;; A <CycleHeadList> is a list of function names, where
;; each element is the name of a function which is a head
;; of a cycle in the program's call graph.
;;
;; Naming Conventions:
;; ==================
;;
;; prog - the residual Mixwell program.
;; path - a path in the call graph as a list of
;; the functions called.
;; cutpoints - a CycleHeadList as described above.
;; pending - a list of names of functions pending to
;; be unfolded.
;; out - a list of unfolded functions.
;; suspend - a CycleHeadList as described above.
;; vn - a list of variable names
;; vv - a list of variable values
(define (ucgr:main src dst src-prog)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Call Graph Analysis ;;
;; ------------------- ;;
;; Global effect: Produces a list of names of functions which ;;
;; will be unfolded under no circumstances. A function will ;;
;; appear in the list if it is the head of a cycle in the call ;;
;; graph of the program. ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Perform the call graph analysis on the (residual) Mixwell
;; program "prog". A list of cycle-heads is returned.
;;
(define (find-cutpoints prog)
(define visited-funcs #f)
(define cutpoints #f)
;;
;; Updates "cutpoints" with the functions met during
;; analysis of the function "fcn".
;;
(define (visit! fcn path)
(with (( (_ _ _ body) (assq fcn prog) )
)
(when (not (memq fcn visited-funcs))
(set! visited-funcs `(,fcn . ,visited-funcs)))
(find-cycles! body `(,fcn . ,path))))
;;
;; Updates "cutpoints" with the functions met during
;; the analysis of the function calls in "exp".
;;
(define (find-cycles! exp path)
(select
(exp)
(_ & (symbol? exp) => #f)
(('quote _) => #f)
(('call fname . exp*) =>
(cond
((memq fname path)
(when (not (memq fname cutpoints))
(set! cutpoints `(,fname . ,cutpoints)))
(find-cycles*! exp* path))
((memq fname visited-funcs)
(find-cycles*! exp* path))
(else
(visit! fname path)
(find-cycles*! exp* path))))
(('xcall fname . exp*) =>
(find-cycles*! exp* path))
((op . exp*) =>
(find-cycles*! exp* path))
))
;;
;; Updates "cutpoints" with the functions met during
;; analysis of the function calls in the expressions "exp*".
;;
(define (find-cycles*! exp* path)
(for-each (lambda (exp) (find-cycles! exp path)) exp*))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (find-cutpoints prog) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(set! visited-funcs '())
(set! cutpoints '())
(with (( ((goal _ _ _) . _) prog )
)
(visit! goal '())
cutpoints))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Function Call Unfolding and Reduction of Expressions ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Returns the program "prog" unfolded according to the information
;; provided in "suspend".
;;
(define (unfold suspend prog)
(define pending #f) ;; Names of functions to be generated.
(define current #f) ;; The name of function being generated.
(define out #f) ;; Generated function definitions.
;;
;; Returns "out" updated with the functions in "pending"
;; and any functions on which they depend.
;;
(define (choose-next!)
(when (not (null? pending))
(set! current (car pending))
(set! pending (cdr pending))
(set! out `(,(make-fundef!) . ,out))
(choose-next!)))
;;
;; Returns a new function definition and updates "pending"
;; with the names of functions called in this function
;; definition.
;;
(define (make-fundef!)
(with* (( (_ parlist _ body) (assq current prog) ))
`(,current ,parlist = ,(reduce-exp! body parlist parlist))
))
;;
;; Returns "exp" with function calls unfolded if found feasible.
;; Functions recorded in "suspend" are not unfolded. If unfolding
;; would lead to call duplication unfolding is not done.
;;
(define (reduce-exp! exp vn vv)
(select
(exp)
(vname
& (symbol? vname) =>
(lookup-value vname vn vv))
(('quote const) => exp)
(('car exp1) =>
(reduce-car (reduce-exp! exp1 vn vv)))
(('cdr exp1) =>
(reduce-cdr (reduce-exp! exp1 vn vv)))
(('pair? exp1) =>
(reduce-pair? (reduce-exp! exp1 vn vv)))
(('null? exp1) =>
(reduce-1-op 'null? null?
(reduce-exp! exp1 vn vv)))
(('not exp1) =>
(reduce-1-op 'not not
(reduce-exp! exp1 vn vv)))
(('cons exp1 exp2) =>
(reduce-cons (reduce-exp! exp1 vn vv)
(reduce-exp! exp2 vn vv)))
(('equal? exp1 exp2) =>
(reduce-2-op 'equal? equal?
(reduce-exp! exp1 vn vv)
(reduce-exp! exp2 vn vv)))
(('eq? exp1 exp2) =>
(reduce-2-op 'eq? eq?
(reduce-exp! exp1 vn vv)
(reduce-exp! exp2 vn vv)))
(('eqv? exp1 exp2) =>
(reduce-2-op 'eqv? eqv?
(reduce-exp! exp1 vn vv)
(reduce-exp! exp2 vn vv)))
(('if exp1 exp2 exp3) =>
(reduce-if! (reduce-exp! exp1 vn vv)
exp2 exp3 vn vv))
(('call fname . exp*) =>
(let ((arg* (reduce-exp*! exp* vn vv))
)
(if (memq fname suspend)
(reduce-call! fname arg*)
(with (( (_ parlist _ body) (assq fname prog) )
)
(if (suspend? parlist arg* body)
(reduce-call! fname arg*)
(reduce-exp! body parlist arg*))))))
(('xcall fname . exp*) =>
`(xcall ,fname . ,(reduce-exp*! exp* vn vv)))
(('xapply . exp*) =>
(reduce-xapply (reduce-exp*! exp* vn vv)))
((fname . exp*) =>
`(,fname . ,(reduce-exp*! exp* vn vv)))
))
(define (reduce-exp*! exp* vn vv)
(map (lambda (exp) (reduce-exp! exp vn vv)) exp*))
(define (reduce-call! fname exp*)
(when (and (not (assq fname out))
(not (eq? fname current))
(not (memq fname pending)))
(set! pending `(,fname . ,pending)))
`(call ,fname . ,exp*))
(define (reduce-if! cnd exp1 exp2 vn vv)
(match
(cnd)
(('quote const) =>
(if const
(reduce-exp! exp1 vn vv)
(reduce-exp! exp2 vn vv)))
(('cons . _) =>
(reduce-exp! exp1 vn vv))
(_ =>
`(if ,cnd
,(reduce-exp! exp1 vn vv)
,(reduce-exp! exp2 vn vv)))
))
(define (reduce-xapply args)
(match
(args)
((('quote fname) exp)
& (and (symbol? fname)
(splittable-exp? exp)) =>
(make-xcall fname (split-exp exp)))
(_ => `(xapply . ,args))
))
(define (splittable-exp? exp)
(select
(exp)
(('quote c) =>
(list? c))
(('cons exp1 exp2) =>
(splittable-exp? exp2))
))
(define (split-exp exp)
(select
(exp)
(('quote c) => (split-quote c))
(('cons exp1 exp2) => `(,exp1 . ,(split-exp exp2)))
))
(define (split-quote lst)
(select
(lst)
(() => '())
((c1 . c2) => `((quote ,c1) . ,(split-quote c2)))
))
(define (make-xcall fname exp*)
(if (memq fname '(static ifs ifd call xcall))
`(xcall ,fname . ,exp*)
`(,fname . ,exp*)))
(define (reduce-car exp)
(match
(exp)
(('quote (c1 . c2)) => `(quote ,c1))
(('cons e1 e2) => e1)
(_ => `(car ,exp))
))
(define (reduce-cdr exp)
(match
(exp)
(('quote (c1 . c2)) => `(quote ,c2))
(('cons e1 e2) => e2)
(_ => `(cdr ,exp))
))
(define (reduce-pair? exp)
(match
(exp)
(('quote c1) => `(quote ,(pair? c1)))
(('cons e1 e2) => ''#t)
(_ => `(pair? ,exp))
))
(define (reduce-cons exp1 exp2)
(match
(exp1 exp2)
(('quote c1) ('quote c2) =>
`(quote ,(cons c1 c2)))
(_ _ => `(cons ,exp1 ,exp2))
))
(define (reduce-1-op name proc exp)
(match
(exp)
(('quote c) => `(quote ,(proc c)))
(_ => `(,name ,exp))
))
(define (reduce-2-op name proc exp1 exp2)
(match
(exp1 exp2)
(('quote c1) ('quote c2) =>
`(quote ,(proc c1 c2)))
(_ _ => `(,name ,exp1 ,exp2))
))
;;
;; Returns #t iff there is an expression in "vv" which
;; contains a call or is a large expression, and the variable
;; in the corresponding place in "vn" appears at least twice
;; in a single branch of "exp".
;;
(define (suspend? vn vv exp)
(select
(vn vv)
(() () => #f)
((vnhd . vntl) (vvhd . vvtl) =>
(or (and (too-large? vvhd)
(> (max-occurrences vnhd exp) 1))
(suspend? vntl vvtl exp)))
))
;;
;; Tests whether the expression "exp" is too large
;; to be duplicated.
;;
(define (too-large? exp)
(select
(exp)
(_ & (symbol? exp) => #f)
(('quote c) =>
(too-large-constant? c))
(_ => #t)
))
(define (too-large-constant? c)
(match
(c)
((_ . _) => #t)
(_ => #f)
))
;;
;; Returns the maximum number of occurrences of "vname"
;; in any branch of "exp".
;;
(define (max-occurrences vname exp)
(select
(exp)
(_
& (symbol? exp) =>
(if (eq? vname exp) 1 0))
(('quote _) => 0)
(('if exp1 exp2 exp3) =>
(let ((n1 (max-occurrences vname exp1))
(n2 (max-occurrences vname exp2))
(n3 (max-occurrences vname exp3)))
(max (+ n1 n2) (+ n1 n3))))
(('call _ . exp*) =>
(max-occurrences* vname exp*))
(('xcall _ . exp*) =>
(max-occurrences* vname exp*))
((_ . exp*) =>
(max-occurrences* vname exp*))
))
(define (max-occurrences* vname exp*)
(foldl-map + 0 (lambda (exp) (max-occurrences vname exp)) exp*))
;;
;; Search for "vname" in "vn" and return the corresponding value
;; from "vv".
;;
(define (lookup-value vname vn vv)
(select
(vn vv)
((vnhd . vntl) (vvhd . vvtl) =>
(if (eq? vnhd vname)
vvhd
(lookup-value vname vntl vvtl)))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (unfold suspend prog) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(with (( ((goal _ _ _) . _) prog )
)
(set! pending `(,goal))
(set! out '())
(choose-next!)
(reverse out)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (ucgr:main src dst src-prog) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This function calls the Call Graph Analyzer and gets a list
;; of cutpoints (i.e. functions that are not to be unfolded).
;; Then it calls the Call Unfolder, which produces the call
;; reduced program.
;;
(newline)
(display "-- Call Graph Reduction: ")
(display src) (display " -> ") (display dst)
(newline) (newline)
(display "Call Graph Analysis") (newline)
(let ((DontUnfold (find-cutpoints src-prog)))
(display "Cut Points: ") (newline)
(write DontUnfold) (newline)
(display "Call Unfolding") (newline)
(let ((dst-prog (unfold DontUnfold src-prog)))
(display "-- Done --") (newline)
dst-prog)))