-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxann.sex
More file actions
330 lines (273 loc) · 11.2 KB
/
Copy pathxann.sex
File metadata and controls
330 lines (273 loc) · 11.2 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
320
321
322
323
324
325
326
327
328
329
330
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; File: xann.sex ;;
;; Project: the specializer Unmix ;;
;; Author: S.A.Romanenko, the Institute for Applied ;;
;; Mathematics, the USSR Acedemy of Sciences, ;;
;; Moscow. ;;
;; Credits: Some parts of the program have been taken ;;
;; from the specializer Mix made by Peter Sestoft ;;
;; and N.C.Kehler Holst (The Mix Group), ;;
;; mix@diku.UUCP, at the University of Copenhagen. ;;
;; Created: 5 May 1989 ;;
;; Revised: 6 April 1990 ;;
;; July 1990 ;;
;; ;;
;; Contents: The phase of the Annotator ;;
;; that annotates a program. ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Data structures and naming conventions:
;; See the file xsepsd.s ...
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Annotation ;;
;; ;;
;; Global effect: given a division of data into static and ;;
;; dynamic parts, the program is annotated. ;;
;; The program is divided into s-program and d-program. ;;
;; The static functions, which have only static parameters and ;;
;; will produce static results, are put into s-program without ;;
;; being modified. ;;
;; The dynamic functions, which have one or more dynamic ;;
;; parameters and will produce dynamic rusults are put into ;;
;; d-program. ;;
;; The dynamic functions are annotated as follows. ;;
;; The parameter lists are split into lists of static ;;
;; parameters and lists of dynamic parameters. ;;
;; The argument lists are split into lists of static ;;
;; arguments and lists of dynamic arguments. ;;
;; All static subexpressions appearing in dynamic expressions ;;
;; are marked with the construct "(static ... )" without being ;;
;; modified. ;;
;; If the call annotation is made in automatic mode, all ;;
;; function calls are automatically annotated as either ;;
;; unfoldable ("call") or not unfoldable ("rcall"). Otherwise ;;
;; the hand-made call annotations are retained. ;;
;; Then the names of all functions that are called as residual ;;
;; (with "rcall") in one or more places in the d-program are ;;
;; put in the list of "residual function names". ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Returns "mw-prog" annotated according the description "mc".
;; The function "collect-residual-functions" has been defined
;; in another file.
;;
(define (uann:make-annotated-program mw-prog mc)
;;
;; Separates the dynamic functions from the static ones.
;; Annotates the definitions of the dynamic functions.
;;
(define (annotate-fndef* fndef* mc)
(select
(fndef*)
(() => '(() . ()))
((fndef . rest) =>
(with (
( (ann-fndef . ind) (annotate-fndef fndef mc))
( (d-rest . s-rest) (annotate-fndef* rest mc))
)
(if (eq? ind 's)
`(,d-rest . (,ann-fndef . ,s-rest))
`((,ann-fndef . ,d-rest) . ,s-rest)
)))))
;;
;; Returns the annotated definition of a dynamic function.
;;
(define (annotate-fndef fndef mc)
(with*
(( (fn fpars _ fbody) fndef )
( (fargs . fres) (lookup-function-description fn mc) ))
(if (eq? fres 's)
`(,fndef . s)
(with*
(( (s-fpars . d-fpars) (sep-sd fpars fargs) )
( (ann-fbody . ind) (annotate-exp fbody fpars fargs mc) ))
`((,fn ,s-fpars ,d-fpars =
,(protect-static-exp ann-fbody ind))
. d)
))))
;;
;; Returns a pair (res . ind) where "res" is the result of
;; annotating "exp", and "ind" is an indicator with a value
;; s (static) iff "exp" does not depend on dynamic parameters.
;; (in this case "res" equals "exp"). Otherwise the value of "ind"
;; is d (dynamic).
;;
(define (annotate-exp exp vn vv mc)
(select
(exp)
(_
& (symbol? exp) =>
`(,exp . ,(lookup-variable exp vn vv))
)
(('quote s-exp) =>
`(,exp . s))
(('generalize exp1) =>
(with
(( (ann-exp1 . ind1) (annotate-exp exp1 vn vv mc) )
)
`(,(protect-static-exp ann-exp1 ind1) . d)
))
(('if exp1 exp2 exp3) =>
(with* (((aexp1 . ind1) (annotate-exp exp1 vn vv mc))
((aexp2 . ind2) (annotate-exp exp2 vn vv mc))
((aexp3 . ind3) (annotate-exp exp3 vn vv mc))
(ind (lub ind1 (lub ind2 ind3)))
)
(if (eq? ind 's)
`(,exp . s)
(let (
(aif (if (eq? ind1 's) 'ifs 'ifd))
(aexp2 (protect-static-exp aexp2 ind2))
(aexp3 (protect-static-exp aexp3 ind3))
)
`((,aif ,aexp1 ,aexp2 ,aexp3) . d)
))))
((call? . _)
& (memq call? '(call rcall)) =>
(with*
(( (_ fn . exp*) exp )
( (fargs . fres) (lookup-function-description fn mc) ))
(if (eq? fres 's)
`(,exp . s)
(with*
(( (s-exp* . d-exp*) (sep-sd exp* fargs) )
( (ann-d-exp* . ind*) (annotate-exp* d-exp* vn vv mc) )
)
`((,call? ,fn
,s-exp*
,(protect-static-exp* ann-d-exp* ind*))
. d)))))
(('xcall fname . exp*) =>
(with*
(( (ann-exp* . ind*) (annotate-exp* exp* vn vv mc) )
( ind (lub-list ind*))
)
(if (eq? ind 's)
`(,exp . s)
`((xcall ,fname . ,(protect-static-exp* ann-exp* ind*)) . d)
)))
((op . exp*) =>
(with*
(( (ann-exp* . ind*) (annotate-exp* exp* vn vv mc) )
( ind (lub-list ind*) )
)
(if (eq? ind 's)
`(,exp . s)
`((,op . ,(protect-static-exp* ann-exp* ind*)) . d)
)))
(_ =>
(error "Malformed expression: " exp))
))
;;
;; Returns a pair (res* . ind*) where each "res" is the result of
;; annotating corresponding "exp", and each "ind" is an indicators
;; with a value s (static) iff the corresponding "exp" does not
;; depend on dynamic parameters (in this case "res" equals "exp").
;; Otherwise the value of "ind" is d (dynamic).
;;
(define (annotate-exp* exp* vn vv mc)
(let ((res-ind*
(map
(lambda (exp) (annotate-exp exp vn vv mc))
exp*)))
`(,(map car res-ind*) . ,(map cdr res-ind*))
))
;;
;; If "ind" tells that "exp" is a static expression,
;; encloses "exp" in "(static" ...")".
;;
(define (protect-static-exp exp ind)
(if (eq? ind 'd)
exp
(match
(exp)
(('error . exp*) =>
`(error . ,(map (lambda (e) (protect-static-exp e 's))
exp*)))
(_ => `(static ,exp))
)))
(define (protect-static-exp* exp* ind*)
(select
(exp* ind*)
(() () => '())
((exp . rest-exp*) (ind . rest-ind*) =>
(cons (protect-static-exp exp ind)
(protect-static-exp* rest-exp* rest-ind*))
)))
;;
;; Returns a pair (s-lst . d-lst) where "s-lst" is the list
;; that includes all staitc elements of "lst", whereas "d-lst"
;; includes all dynamic elements of "lst" .
;;
(define (sep-sd lst ind*)
(select
(lst ind*)
(() () => '(() . ()))
((first . r-lst) (ind . r-ind*) =>
(with (( (s-lst . d-lst) (sep-sd r-lst r-ind*) )
)
(case ind
((s)
`((,first . ,s-lst) . ,d-lst))
((d)
`(,s-lst . (,first . ,d-lst)))
)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Least Upper Bound Computation ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Returns lub of two indicators.
;;
(define (lub ind1 ind2)
(if (eq? ind1 'd) 'd ind2))
;;
;; Returns the lub of a list of indicators.
;;
(define (lub-list ind*)
(if (memq 'd ind*) 'd 's))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Description Handling ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Returns a pair (fargs . fres) where "fargs" is the parameter
;; description of the function "fname", and "fres" is the result
;; description of the function "fname".
;;
(define (lookup-function-description fname mc)
(cdr (assq fname mc)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Environment Handling ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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)))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; (uann:make-annotated-program mw-prog mc) ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(with* (( (d-fndef* . s-fndef*) (annotate-fndef* mw-prog mc) )
( rf-names (uresfn:collect-residual-functions d-fndef*) )
)
`(,rf-names ,d-fndef* ,s-fndef*)
))