-
Notifications
You must be signed in to change notification settings - Fork 1
/
xpiu.sex
531 lines (421 loc) · 16.5 KB
/
xpiu.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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; File: xpiu.s ;;
;; Project: the specializer Unmix ;;
;; Author: S.A.Romanenko, the Institute for Applied ;;
;; Mathematics, the USSR Academy of Sciences, ;;
;; Moscow. ;;
;; Created: 20 May 1989 ;;
;; Revised: 6 April 1990 ;;
;; August 1990 ;;
;; ;;
;; Contents: The first phase of the Call Annotator ;;
;; that prevents infinite unfolding ;;
;; of function calls. ;;
;; ;;
;; Synopsis: ;;
;; (prevent-infinite-unfolding prog) ;;
;; ;;
;; prog - an annotated Mixwell program ;;
;; ;;
;; Description: ;;
;; The program makes call annotations that guarantee ;;
;; absence of infinite unfolding. It finds all ;;
;; loops in the program and marks as residual ;;
;; all calls that do not satisfy the structural ;;
;; induction condition. ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data structures:
;; ===============
;;
;; Loop Description List:
;;
;; <LoopDescriptionList> ::= ( <LoopDescription>* )
;; <LoopDescription> ::= ( <CallDescription> . <Path> )
;; <Path> ::= ( <CallDescription>* )
;; <CallDescription> ::= ( <Fname> . <Trace> )
;; <Trace> ::= ( <Integer>* )
;;
;;
;; A loop description list is a list of loop descriptions, where
;; each loop's description contains a call description and a path.
;; The path records a sequence of calls that leads from the loop's
;; head function to the same function. The call description is equal
;; to the last call description in the path. Each call's description
;; consists of a function name and a trace that describes the position
;; of the call in the function's definition.
;;
;; Size Description:
;;
;; <SizeDescription> ::= 'any
;; | ('le . <Vname> )
;; | ('lt . <Vname> )
;;
;; A size description is an abstract value that gives information
;; about the size that the corresponding static value may have during
;; partial evaluation.
;; A size description 'any tells that there is no information
;; about the value.
;; A size description ('le . Vname ) tells that the size of the
;; value will be less or equal than the size of the value
;; the variable "Vname" was bound to at the head of a loop.
;; A size description ('lt . Vname ) tells that the size of the
;; value will be less than the size of the value the variable "Vname"
;; was bound to at the head of a loop.
;; Naming conventions:
;; ==================
;;
;; prog - the Mixwell program to analyze.
;; loops - a loop description list.
;; path - a path in the program.
;; trace - a trace in a function definition.
;; vn - a list of variable names.
;; vv - a list of abstract values (size descriptions)
;; corresponding to the variables found in "vn".
;; A note on the algorithm used:
;; ============================
;;
;; A call to a function with only static variables is always
;; made eliminable.
;; The decision for a call to a function with at least one
;; dynamic variable makes use of the concept of an "inductive
;; variable". A variable in a call to the head of a loop is
;; called inductive if the corresponding argument expression
;; produces a value which is a proper substructure of the value
;; of the variable it was boun to when the head function was
;; callled.
;; A call to the head of a loop is made eliminable if there
;; is at least one inductive static variable in the call and
;; the remaining static variables are either unchanged or
;; inductive.
;;
;; The program contains tree phases:
;;
;; 1) Finding all loops in the program.
;;
;; 2) Finding all "dangerous" calls.
;;
;; 3) Annotating all "dangerous" calls as residual.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Main Function ;;
;; ;;
;; Global effect: Replaces some keywords "call" with "rcall" ;;
;; in a program in order to avoid infinite unfolding. ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (upiu:prevent-infinite-unfolding! prog)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Finding Loops ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Finds all loops in a program.
;;
(define (find-loops-prog rf prog)
(define loops #f)
(define (find-loops-func fname)
(with (( (_ _ _ _ body) (assq fname prog) ))
(find-loops
body fname '() (list fname) '())))
;;
;; Returns a list of loops found by traversing
;; the expression "exp".
;; "fn-path" is only used to speed up loop detection.
;;
(define (find-loops exp fn trace fn-path path)
(select
(exp)
(_ & (symbol? exp) => #f)
(('static exp1) => #f)
(('ifs exp0 . exp*) =>
(find-loops* exp* 1 fn trace fn-path path))
(('ifd . exp*) =>
(find-loops* exp* 0 fn trace fn-path path))
(('call fname s-exp* d-exp*) =>
(find-loops* d-exp* 0 fn trace fn-path path)
(if (memq fname fn-path)
(record-loop fname fn trace path)
(visit-function fname fn trace fn-path path)))
(('rcall fname s-exp* d-exp*) =>
(find-loops* d-exp* 0 fn trace fn-path path))
(('xcall fname . exp*) =>
(find-loops* exp* 0 fn trace fn-path path))
((op . exp*) =>
(find-loops* exp* 0 fn trace fn-path path))
(_ =>
(error "Malformed expression: " exp))
))
(define (find-loops* exp* num fn trace fn-path path)
(select
(exp*)
(() => #f)
((exp . rest) =>
(find-loops exp fn `(,num . ,trace) fn-path path)
(find-loops* rest (+ num 1) fn trace fn-path path))
))
;;
;; Records a loop found
;;
(define (record-loop fname fn trace path)
(let ((new-loop
`( (,fn . ,(reverse trace))
. ,(extract-loop fname `((,fn . ,trace) . ,path) '()))
))
(when (not (member new-loop loops))
(set! loops `(,new-loop . ,loops)))))
;;
;; Extracts a loop from a path.
;;
(define (extract-loop fname path loop)
(with* (( ((fname1 . trace1) . path-rest) path )
( loop `((,fname1 . ,(reverse trace1)) . ,loop) )
)
(if (eq? fname fname1)
loop
(extract-loop fname path-rest loop))))
(define (visit-function fname fn trace fn-path path)
(with (( (_ _ _ _ body) (assq fname prog) )
)
(find-loops
body fname '()
`(,fname . ,fn-path)
`((,fn . ,trace) . ,path)
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (find-loops-prog rf prog) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(set! loops '())
(for-each (lambda (fname) (find-loops-func fname)) rf)
loops)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Collecting Dangerous Calls ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Returns a list of dangerous calls unfolding of which
;; could lead to generating an infinite residual program.
;;
(define (collect-dangerous-calls loops prog)
(define dangerous-calls #f)
(define (collect-in-fundef loop)
(with (( (back-call . path) loop )
)
(cond
((member back-call dangerous-calls) #f)
((dangerous-path? path)
(set! dangerous-calls `(,back-call . ,dangerous-calls)))
(else #f))))
;;
;; Tests whether a path is dangerous.
;;
(define (dangerous-path? path)
(with* (( ((fname . trace) . path-rest) path )
( (_ svn dvn _ body) (assq fname prog) )
( new-svv (go-through-path
body trace path-rest svn (make-le svn)) )
)
(or (not (all-non-increasing? svn new-svv))
(not (some-decreasing? new-svv)))))
;;
;; Goes through a path and computes the size descriptions
;; of variables at the end of the path.
;;
(define (go-through-path exp trace path vn vv)
(select
(exp)
(_ & (symbol? exp) =>
(error "No way in the expression" exp))
(('static _) =>
(error "No way in the expression" exp))
(('call fname s-exp* d-exp*) =>
(select (trace)
(() =>
(go-through-call
fname (decr-eval* s-exp* vn vv) path))
((num . trace-rest) =>
(go-through-path
(list-ref d-exp* num) trace-rest path vn vv))
))
(('rcall fname s-exp* d-exp*) =>
(with (( (num . trace-rest) trace )
)
(go-through-path
(list-ref d-exp* num) trace-rest path vn vv)))
(('xcall fname . exp*) =>
(with (( (num . trace-rest) trace )
)
(go-through-path
(list-ref exp* num) trace-rest path vn vv)))
((op . exp*) =>
(with (( (num . trace-rest) trace )
)
(go-through-path
(list-ref exp* num) trace-rest path vn vv)))
(_ =>
(error "Malformed expression: " exp))
))
(define (go-through-call fname svv path)
(select
(path)
(() => svv)
((path-head . path-rest) =>
(with (( (_ svn dvn _ body) (assq fname prog) )
( (_ . trace) path-head )
)
(go-through-path body trace path-rest svn svv)))
))
;;
;; Abstract evaluation of the expression "exp"
;; in the environment (vn,vv) to get information
;; about decreasing variable values.
;;
(define (decr-eval exp vn vv)
(select
(exp)
(_ & (symbol? exp) =>
(lookup-variable exp vn vv))
(('quote _) => 'any)
((op . exp*)
& (memq op '(car cdr)) =>
(decr-eval-sel (decr-eval (car exp*) vn vv)))
((_ . _) => 'any)
(_ =>
(error "Malformed expression: " exp))
))
(define (decr-eval* exp* vn vv)
(map (lambda (exp) (decr-eval exp vn vv)) exp*))
(define (decr-eval-sel a-value)
(select
(a-value)
('any => 'any)
(('lt . vname) => a-value)
(('le . vname) => `(lt . ,vname))
))
;;
;; Makes a list of abstract variable values.
;;
(define (make-le vn)
(map (lambda (vname) `(le . ,vname)) vn))
;;
;; Tests whether there is no variable value that
;; might have increased.
;;
(define (all-non-increasing? vn vv)
(select
(vn vv)
(() () => #t)
((vname . vn-rest) (vvalue . vv-rest) =>
(select
(vvalue)
('any => #f)
(('lt . vname1) =>
(and (eq? vname vname1)
(all-non-increasing? vn-rest vv-rest)))
(('le . vname1) =>
(and (eq? vname vname1)
(all-non-increasing? vn-rest vv-rest)))
))
))
;;
;; Tests whether there is a variable value that has decreased
;; (they are all assumed to be non-increasing because
;; this function is called after "all-non-increasing").
;;
(define (some-decreasing? vv)
(or-map (lambda (vvalue)
(select (vvalue)
(('lt _) => #t)
(('le _) => #f)))
vv))
;;
;; Returns the value of the variable "vname" in the environment
;; (vn,vv).
;;
(define (lookup-variable vname vn vv)
(select
(vn vv)
(() () => (error "Undefined variable: " vname))
((vn . nrest) (vv . vrest) =>
(if (eq? vname vn)
vv
(lookup-variable vname nrest vrest)))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (collect-dangerous-calls loops prog) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(set! dangerous-calls '())
(for-each collect-in-fundef loops)
dangerous-calls)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Annotating Dangerous Calls ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Replaces "call" with "rcall" for all function calls
;; recorded in "dangerous calls".
;;
(define (mark-dangerous-calls! prog dangerous-calls)
(define (mark-dc-fundef! fname trace prog)
(with (( (_ _ _ _ body) (assq fname prog) )
)
(mark-dc! body trace)))
(define (mark-dc! exp trace)
(select
(exp)
(_ & (symbol? exp) =>
(error "No way in the expression: " exp))
(('static exp1) =>
(error "No way in the expression: " exp))
((&call . _)
& (memq &call '(call rcall)) =>
(with (( (_ fname s-exp* d-exp*) exp ))
(select
(trace)
(()
=> (set-car! exp 'rcall))
((num . trace-rest)
=> (mark-dc! (list-ref d-exp* num) trace-rest))
)))
(('xcall fname . exp*) =>
(with (( (num . trace-rest) trace )
)
(mark-dc! (list-ref exp* num) trace-rest)
))
((op . exp*) =>
(with (( (num . trace-rest) trace )
)
(mark-dc! (list-ref exp* num) trace-rest)
))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (mark-dangerous-calls! prog dangerous-calls) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(for-each
(lambda (back-call)
(with* (( (fname . trace) back-call )
)
(mark-dc-fundef! fname trace prog)
))
dangerous-calls))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (upiu:prevent-infinite-unfolding! prog) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(display "Preventing Infinite Unfolding") (newline)
(with (( (rf d-fundef* s-fundef*) prog ))
(display "Finding Loops") (newline)
(let* ((loops (find-loops-prog rf d-fundef*))
(dangerous-calls
(collect-dangerous-calls loops d-fundef*)))
(display "Dangerous calls:") (newline)
(write dangerous-calls) (newline)
(display "Cutting Dangerous Loops") (newline)
(mark-dangerous-calls! d-fundef* dangerous-calls)
(let* ((rf (uresfn:collect-residual-functions d-fundef*)))
(display "-- Done --") (newline)
`(,rf ,d-fundef* ,s-fundef*)))))