analyze.scm 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. ;;; TREE-IL -> GLIL compiler
  2. ;; Copyright (C) 2001,2008,2009,2010 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (language tree-il analyze)
  18. #:use-module (srfi srfi-1)
  19. #:use-module (srfi srfi-9)
  20. #:use-module (srfi srfi-11)
  21. #:use-module (ice-9 vlist)
  22. #:use-module (system base syntax)
  23. #:use-module (system base message)
  24. #:use-module (system vm program)
  25. #:use-module (language tree-il)
  26. #:use-module (system base pmatch)
  27. #:export (analyze-lexicals
  28. analyze-tree
  29. unused-variable-analysis
  30. unused-toplevel-analysis
  31. unbound-variable-analysis
  32. arity-analysis))
  33. ;; Allocation is the process of assigning storage locations for lexical
  34. ;; variables. A lexical variable has a distinct "address", or storage
  35. ;; location, for each procedure in which it is referenced.
  36. ;;
  37. ;; A variable is "local", i.e., allocated on the stack, if it is
  38. ;; referenced from within the procedure that defined it. Otherwise it is
  39. ;; a "closure" variable. For example:
  40. ;;
  41. ;; (lambda (a) a) ; a will be local
  42. ;; `a' is local to the procedure.
  43. ;;
  44. ;; (lambda (a) (lambda () a))
  45. ;; `a' is local to the outer procedure, but a closure variable with
  46. ;; respect to the inner procedure.
  47. ;;
  48. ;; If a variable is ever assigned, it needs to be heap-allocated
  49. ;; ("boxed"). This is so that closures and continuations capture the
  50. ;; variable's identity, not just one of the values it may have over the
  51. ;; course of program execution. If the variable is never assigned, there
  52. ;; is no distinction between value and identity, so closing over its
  53. ;; identity (whether through closures or continuations) can make a copy
  54. ;; of its value instead.
  55. ;;
  56. ;; Local variables are stored on the stack within a procedure's call
  57. ;; frame. Their index into the stack is determined from their linear
  58. ;; postion within a procedure's binding path:
  59. ;; (let (0 1)
  60. ;; (let (2 3) ...)
  61. ;; (let (2) ...))
  62. ;; (let (2 3 4) ...))
  63. ;; etc.
  64. ;;
  65. ;; This algorithm has the problem that variables are only allocated
  66. ;; indices at the end of the binding path. If variables bound early in
  67. ;; the path are not used in later portions of the path, their indices
  68. ;; will not be recycled. This problem is particularly egregious in the
  69. ;; expansion of `or':
  70. ;;
  71. ;; (or x y z)
  72. ;; -> (let ((a x)) (if a a (let ((b y)) (if b b z))))
  73. ;;
  74. ;; As you can see, the `a' binding is only used in the ephemeral
  75. ;; `consequent' clause of the first `if', but its index would be
  76. ;; reserved for the whole of the `or' expansion. So we have a hack for
  77. ;; this specific case. A proper solution would be some sort of liveness
  78. ;; analysis, and not our linear allocation algorithm.
  79. ;;
  80. ;; Closure variables are captured when a closure is created, and stored in a
  81. ;; vector inline to the closure object itself. Each closure variable has a
  82. ;; unique index into that vector.
  83. ;;
  84. ;; There is one more complication. Procedures bound by <fix> may, in
  85. ;; some cases, be rendered inline to their parent procedure. That is to
  86. ;; say,
  87. ;;
  88. ;; (letrec ((lp (lambda () (lp)))) (lp))
  89. ;; => (fix ((lp (lambda () (lp)))) (lp))
  90. ;; => goto FIX-BODY; LP: goto LP; FIX-BODY: goto LP;
  91. ;; ^ jump over the loop ^ the fixpoint lp ^ starting off the loop
  92. ;;
  93. ;; The upshot is that we don't have to allocate any space for the `lp'
  94. ;; closure at all, as it can be rendered inline as a loop. So there is
  95. ;; another kind of allocation, "label allocation", in which the
  96. ;; procedure is simply a label, placed at the start of the lambda body.
  97. ;; The label is the gensym under which the lambda expression is bound.
  98. ;;
  99. ;; The analyzer checks to see that the label is called with the correct
  100. ;; number of arguments. Calls to labels compile to rename + goto.
  101. ;; Lambda, the ultimate goto!
  102. ;;
  103. ;;
  104. ;; The return value of `analyze-lexicals' is a hash table, the
  105. ;; "allocation".
  106. ;;
  107. ;; The allocation maps gensyms -- recall that each lexically bound
  108. ;; variable has a unique gensym -- to storage locations ("addresses").
  109. ;; Since one gensym may have many storage locations, if it is referenced
  110. ;; in many procedures, it is a two-level map.
  111. ;;
  112. ;; The allocation also stored information on how many local variables
  113. ;; need to be allocated for each procedure, lexicals that have been
  114. ;; translated into labels, and information on what free variables to
  115. ;; capture from its lexical parent procedure.
  116. ;;
  117. ;; In addition, we have a conflation: while we're traversing the code,
  118. ;; recording information to pass to the compiler, we take the
  119. ;; opportunity to generate labels for each lambda-case clause, so that
  120. ;; generated code can skip argument checks at runtime if they match at
  121. ;; compile-time.
  122. ;;
  123. ;; Also, while we're a-traversing and an-allocating, we check prompt
  124. ;; handlers to see if the "continuation" argument is used. If not, we
  125. ;; mark the prompt as being "escape-only". This allows us to implement
  126. ;; `catch' and `throw' using `prompt' and `control', but without causing
  127. ;; a continuation to be reified. Heh heh.
  128. ;;
  129. ;; That is:
  130. ;;
  131. ;; sym -> {lambda -> address}
  132. ;; lambda -> (labels . free-locs)
  133. ;; lambda-case -> (gensym . nlocs)
  134. ;; prompt -> escape-only?
  135. ;;
  136. ;; address ::= (local? boxed? . index)
  137. ;; labels ::= ((sym . lambda) ...)
  138. ;; free-locs ::= ((sym0 . address0) (sym1 . address1) ...)
  139. ;; free variable addresses are relative to parent proc.
  140. (define (make-hashq k v)
  141. (let ((res (make-hash-table)))
  142. (hashq-set! res k v)
  143. res))
  144. (define (analyze-lexicals x)
  145. ;; bound-vars: lambda -> (sym ...)
  146. ;; all identifiers bound within a lambda
  147. (define bound-vars (make-hash-table))
  148. ;; free-vars: lambda -> (sym ...)
  149. ;; all identifiers referenced in a lambda, but not bound
  150. ;; NB, this includes identifiers referenced by contained lambdas
  151. (define free-vars (make-hash-table))
  152. ;; assigned: sym -> #t
  153. ;; variables that are assigned
  154. (define assigned (make-hash-table))
  155. ;; refcounts: sym -> count
  156. ;; allows us to detect the or-expansion in O(1) time
  157. (define refcounts (make-hash-table))
  158. ;; labels: sym -> lambda
  159. ;; for determining if fixed-point procedures can be rendered as
  160. ;; labels.
  161. (define labels (make-hash-table))
  162. ;; returns variables referenced in expr
  163. (define (analyze! x proc labels-in-proc tail? tail-call-args)
  164. (define (step y) (analyze! y proc labels-in-proc #f #f))
  165. (define (step-tail y) (analyze! y proc labels-in-proc tail? #f))
  166. (define (step-tail-call y args) (analyze! y proc labels-in-proc #f
  167. (and tail? args)))
  168. (define (recur/labels x new-proc labels)
  169. (analyze! x new-proc (append labels labels-in-proc) #t #f))
  170. (define (recur x new-proc) (analyze! x new-proc '() tail? #f))
  171. (record-case x
  172. ((<application> proc args)
  173. (apply lset-union eq? (step-tail-call proc args)
  174. (map step args)))
  175. ((<conditional> test consequent alternate)
  176. (lset-union eq? (step test) (step-tail consequent) (step-tail alternate)))
  177. ((<lexical-ref> gensym)
  178. (hashq-set! refcounts gensym (1+ (hashq-ref refcounts gensym 0)))
  179. (if (not (and tail-call-args
  180. (memq gensym labels-in-proc)
  181. (let ((p (hashq-ref labels gensym)))
  182. (and p
  183. (let lp ((c (lambda-body p)))
  184. (and c (lambda-case? c)
  185. (or
  186. ;; for now prohibit optional &
  187. ;; keyword arguments; can relax this
  188. ;; restriction later
  189. (and (= (length (lambda-case-req c))
  190. (length tail-call-args))
  191. (not (lambda-case-opt c))
  192. (not (lambda-case-kw c))
  193. (not (lambda-case-rest c)))
  194. (lp (lambda-case-alternate c)))))))))
  195. (hashq-set! labels gensym #f))
  196. (list gensym))
  197. ((<lexical-set> gensym exp)
  198. (hashq-set! assigned gensym #t)
  199. (hashq-set! labels gensym #f)
  200. (lset-adjoin eq? (step exp) gensym))
  201. ((<module-set> exp)
  202. (step exp))
  203. ((<toplevel-set> exp)
  204. (step exp))
  205. ((<toplevel-define> exp)
  206. (step exp))
  207. ((<sequence> exps)
  208. (let lp ((exps exps) (ret '()))
  209. (cond ((null? exps) '())
  210. ((null? (cdr exps))
  211. (lset-union eq? ret (step-tail (car exps))))
  212. (else
  213. (lp (cdr exps) (lset-union eq? ret (step (car exps))))))))
  214. ((<lambda> body)
  215. ;; order is important here
  216. (hashq-set! bound-vars x '())
  217. (let ((free (recur body x)))
  218. (hashq-set! bound-vars x (reverse! (hashq-ref bound-vars x)))
  219. (hashq-set! free-vars x free)
  220. free))
  221. ((<lambda-case> opt kw inits gensyms body alternate)
  222. (hashq-set! bound-vars proc
  223. (append (reverse gensyms) (hashq-ref bound-vars proc)))
  224. (lset-union
  225. eq?
  226. (lset-difference eq?
  227. (lset-union eq?
  228. (apply lset-union eq? (map step inits))
  229. (step-tail body))
  230. gensyms)
  231. (if alternate (step-tail alternate) '())))
  232. ((<let> gensyms vals body)
  233. (hashq-set! bound-vars proc
  234. (append (reverse gensyms) (hashq-ref bound-vars proc)))
  235. (lset-difference eq?
  236. (apply lset-union eq? (step-tail body) (map step vals))
  237. gensyms))
  238. ((<letrec> gensyms vals body)
  239. (hashq-set! bound-vars proc
  240. (append (reverse gensyms) (hashq-ref bound-vars proc)))
  241. (for-each (lambda (sym) (hashq-set! assigned sym #t)) gensyms)
  242. (lset-difference eq?
  243. (apply lset-union eq? (step-tail body) (map step vals))
  244. gensyms))
  245. ((<fix> gensyms vals body)
  246. ;; Try to allocate these procedures as labels.
  247. (for-each (lambda (sym val) (hashq-set! labels sym val))
  248. gensyms vals)
  249. (hashq-set! bound-vars proc
  250. (append (reverse gensyms) (hashq-ref bound-vars proc)))
  251. ;; Step into subexpressions.
  252. (let* ((var-refs
  253. (map
  254. ;; Since we're trying to label-allocate the lambda,
  255. ;; pretend it's not a closure, and just recurse into its
  256. ;; body directly. (Otherwise, recursing on a closure
  257. ;; that references one of the fix's bound vars would
  258. ;; prevent label allocation.)
  259. (lambda (x)
  260. (record-case x
  261. ((<lambda> body)
  262. ;; just like the closure case, except here we use
  263. ;; recur/labels instead of recur
  264. (hashq-set! bound-vars x '())
  265. (let ((free (recur/labels body x gensyms)))
  266. (hashq-set! bound-vars x (reverse! (hashq-ref bound-vars x)))
  267. (hashq-set! free-vars x free)
  268. free))))
  269. vals))
  270. (vars-with-refs (map cons gensyms var-refs))
  271. (body-refs (recur/labels body proc gensyms)))
  272. (define (delabel-dependents! sym)
  273. (let ((refs (assq-ref vars-with-refs sym)))
  274. (if refs
  275. (for-each (lambda (sym)
  276. (if (hashq-ref labels sym)
  277. (begin
  278. (hashq-set! labels sym #f)
  279. (delabel-dependents! sym))))
  280. refs))))
  281. ;; Stepping into the lambdas and the body might have made some
  282. ;; procedures not label-allocatable -- which might have
  283. ;; knock-on effects. For example:
  284. ;; (fix ((a (lambda () (b)))
  285. ;; (b (lambda () a)))
  286. ;; (a))
  287. ;; As far as `a' is concerned, both `a' and `b' are
  288. ;; label-allocatable. But `b' references `a' not in a proc-tail
  289. ;; position, which makes `a' not label-allocatable. The
  290. ;; knock-on effect is that, when back-propagating this
  291. ;; information to `a', `b' will also become not
  292. ;; label-allocatable, as it is referenced within `a', which is
  293. ;; allocated as a closure. This is a transitive relationship.
  294. (for-each (lambda (sym)
  295. (if (not (hashq-ref labels sym))
  296. (delabel-dependents! sym)))
  297. gensyms)
  298. ;; Now lift bound variables with label-allocated lambdas to the
  299. ;; parent procedure.
  300. (for-each
  301. (lambda (sym val)
  302. (if (hashq-ref labels sym)
  303. ;; Remove traces of the label-bound lambda. The free
  304. ;; vars will propagate up via the return val.
  305. (begin
  306. (hashq-set! bound-vars proc
  307. (append (hashq-ref bound-vars val)
  308. (hashq-ref bound-vars proc)))
  309. (hashq-remove! bound-vars val)
  310. (hashq-remove! free-vars val))))
  311. gensyms vals)
  312. (lset-difference eq?
  313. (apply lset-union eq? body-refs var-refs)
  314. gensyms)))
  315. ((<let-values> exp body)
  316. (lset-union eq? (step exp) (step body)))
  317. ((<dynwind> body winder unwinder)
  318. (lset-union eq? (step body) (step winder) (step unwinder)))
  319. ((<dynlet> fluids vals body)
  320. (apply lset-union eq? (step body) (map step (append fluids vals))))
  321. ((<dynref> fluid)
  322. (step fluid))
  323. ((<dynset> fluid exp)
  324. (lset-union eq? (step fluid) (step exp)))
  325. ((<prompt> tag body handler)
  326. (lset-union eq? (step tag) (step body) (step-tail handler)))
  327. ((<abort> tag args tail)
  328. (apply lset-union eq? (step tag) (step tail) (map step args)))
  329. (else '())))
  330. ;; allocation: sym -> {lambda -> address}
  331. ;; lambda -> (nlocs labels . free-locs)
  332. (define allocation (make-hash-table))
  333. (define (allocate! x proc n)
  334. (define (recur y) (allocate! y proc n))
  335. (record-case x
  336. ((<application> proc args)
  337. (apply max (recur proc) (map recur args)))
  338. ((<conditional> test consequent alternate)
  339. (max (recur test) (recur consequent) (recur alternate)))
  340. ((<lexical-set> exp)
  341. (recur exp))
  342. ((<module-set> exp)
  343. (recur exp))
  344. ((<toplevel-set> exp)
  345. (recur exp))
  346. ((<toplevel-define> exp)
  347. (recur exp))
  348. ((<sequence> exps)
  349. (apply max (map recur exps)))
  350. ((<lambda> body)
  351. ;; allocate closure vars in order
  352. (let lp ((c (hashq-ref free-vars x)) (n 0))
  353. (if (pair? c)
  354. (begin
  355. (hashq-set! (hashq-ref allocation (car c))
  356. x
  357. `(#f ,(hashq-ref assigned (car c)) . ,n))
  358. (lp (cdr c) (1+ n)))))
  359. (let ((nlocs (allocate! body x 0))
  360. (free-addresses
  361. (map (lambda (v)
  362. (hashq-ref (hashq-ref allocation v) proc))
  363. (hashq-ref free-vars x)))
  364. (labels (filter cdr
  365. (map (lambda (sym)
  366. (cons sym (hashq-ref labels sym)))
  367. (hashq-ref bound-vars x)))))
  368. ;; set procedure allocations
  369. (hashq-set! allocation x (cons labels free-addresses)))
  370. n)
  371. ((<lambda-case> opt kw inits gensyms body alternate)
  372. (max
  373. (let lp ((gensyms gensyms) (n n))
  374. (if (null? gensyms)
  375. (let ((nlocs (apply
  376. max
  377. (allocate! body proc n)
  378. ;; inits not logically at the end, but they
  379. ;; are the list...
  380. (map (lambda (x) (allocate! x proc n)) inits))))
  381. ;; label and nlocs for the case
  382. (hashq-set! allocation x (cons (gensym ":LCASE") nlocs))
  383. nlocs)
  384. (begin
  385. (hashq-set! allocation (car gensyms)
  386. (make-hashq
  387. proc `(#t ,(hashq-ref assigned (car gensyms)) . ,n)))
  388. (lp (cdr gensyms) (1+ n)))))
  389. (if alternate (allocate! alternate proc n) n)))
  390. ((<let> gensyms vals body)
  391. (let ((nmax (apply max (map recur vals))))
  392. (cond
  393. ;; the `or' hack
  394. ((and (conditional? body)
  395. (= (length gensyms) 1)
  396. (let ((v (car gensyms)))
  397. (and (not (hashq-ref assigned v))
  398. (= (hashq-ref refcounts v 0) 2)
  399. (lexical-ref? (conditional-test body))
  400. (eq? (lexical-ref-gensym (conditional-test body)) v)
  401. (lexical-ref? (conditional-consequent body))
  402. (eq? (lexical-ref-gensym (conditional-consequent body)) v))))
  403. (hashq-set! allocation (car gensyms)
  404. (make-hashq proc `(#t #f . ,n)))
  405. ;; the 1+ for this var
  406. (max nmax (1+ n) (allocate! (conditional-alternate body) proc n)))
  407. (else
  408. (let lp ((gensyms gensyms) (n n))
  409. (if (null? gensyms)
  410. (max nmax (allocate! body proc n))
  411. (let ((v (car gensyms)))
  412. (hashq-set!
  413. allocation v
  414. (make-hashq proc
  415. `(#t ,(hashq-ref assigned v) . ,n)))
  416. (lp (cdr gensyms) (1+ n)))))))))
  417. ((<letrec> gensyms vals body)
  418. (let lp ((gensyms gensyms) (n n))
  419. (if (null? gensyms)
  420. (let ((nmax (apply max
  421. (map (lambda (x)
  422. (allocate! x proc n))
  423. vals))))
  424. (max nmax (allocate! body proc n)))
  425. (let ((v (car gensyms)))
  426. (hashq-set!
  427. allocation v
  428. (make-hashq proc
  429. `(#t ,(hashq-ref assigned v) . ,n)))
  430. (lp (cdr gensyms) (1+ n))))))
  431. ((<fix> gensyms vals body)
  432. (let lp ((in gensyms) (n n))
  433. (if (null? in)
  434. (let lp ((gensyms gensyms) (vals vals) (nmax n))
  435. (cond
  436. ((null? gensyms)
  437. (max nmax (allocate! body proc n)))
  438. ((hashq-ref labels (car gensyms))
  439. ;; allocate lambda body inline to proc
  440. (lp (cdr gensyms)
  441. (cdr vals)
  442. (record-case (car vals)
  443. ((<lambda> body)
  444. (max nmax (allocate! body proc n))))))
  445. (else
  446. ;; allocate closure
  447. (lp (cdr gensyms)
  448. (cdr vals)
  449. (max nmax (allocate! (car vals) proc n))))))
  450. (let ((v (car in)))
  451. (cond
  452. ((hashq-ref assigned v)
  453. (error "fixpoint procedures may not be assigned" x))
  454. ((hashq-ref labels v)
  455. ;; no binding, it's a label
  456. (lp (cdr in) n))
  457. (else
  458. ;; allocate closure binding
  459. (hashq-set! allocation v (make-hashq proc `(#t #f . ,n)))
  460. (lp (cdr in) (1+ n))))))))
  461. ((<let-values> exp body)
  462. (max (recur exp) (recur body)))
  463. ((<dynwind> body winder unwinder)
  464. (max (recur body) (recur winder) (recur unwinder)))
  465. ((<dynlet> fluids vals body)
  466. (apply max (recur body) (map recur (append fluids vals))))
  467. ((<dynref> fluid)
  468. (recur fluid))
  469. ((<dynset> fluid exp)
  470. (max (recur fluid) (recur exp)))
  471. ((<prompt> tag body handler)
  472. (let ((cont-var (and (lambda-case? handler)
  473. (pair? (lambda-case-gensyms handler))
  474. (car (lambda-case-gensyms handler)))))
  475. (hashq-set! allocation x
  476. (and cont-var (zero? (hashq-ref refcounts cont-var 0))))
  477. (max (recur tag) (recur body) (recur handler))))
  478. ((<abort> tag args tail)
  479. (apply max (recur tag) (recur tail) (map recur args)))
  480. (else n)))
  481. (analyze! x #f '() #t #f)
  482. (allocate! x #f 0)
  483. allocation)
  484. ;;;
  485. ;;; Tree analyses for warnings.
  486. ;;;
  487. (define-record-type <tree-analysis>
  488. (make-tree-analysis leaf down up post init)
  489. tree-analysis?
  490. (leaf tree-analysis-leaf) ;; (lambda (x result env locs) ...)
  491. (down tree-analysis-down) ;; (lambda (x result env locs) ...)
  492. (up tree-analysis-up) ;; (lambda (x result env locs) ...)
  493. (post tree-analysis-post) ;; (lambda (result env) ...)
  494. (init tree-analysis-init)) ;; arbitrary value
  495. (define (analyze-tree analyses tree env)
  496. "Run all tree analyses listed in ANALYSES on TREE for ENV, using
  497. `tree-il-fold'. Return TREE. The leaf/down/up procedures of each analysis are
  498. passed a ``location stack', which is the stack of `tree-il-src' values for each
  499. parent tree (a list); it can be used to approximate source location when
  500. accurate information is missing from a given `tree-il' element."
  501. (define (traverse proc update-locs)
  502. ;; Return a tree traversing procedure that returns a list of analysis
  503. ;; results prepended by the location stack.
  504. (lambda (x results)
  505. (let ((locs (update-locs x (car results))))
  506. (cons locs ;; the location stack
  507. (map (lambda (analysis result)
  508. ((proc analysis) x result env locs))
  509. analyses
  510. (cdr results))))))
  511. ;; Keeping/extending/shrinking the location stack.
  512. (define (keep-locs x locs) locs)
  513. (define (extend-locs x locs) (cons (tree-il-src x) locs))
  514. (define (shrink-locs x locs) (cdr locs))
  515. (let ((results
  516. (tree-il-fold (traverse tree-analysis-leaf keep-locs)
  517. (traverse tree-analysis-down extend-locs)
  518. (traverse tree-analysis-up shrink-locs)
  519. (cons '() ;; empty location stack
  520. (map tree-analysis-init analyses))
  521. tree)))
  522. (for-each (lambda (analysis result)
  523. ((tree-analysis-post analysis) result env))
  524. analyses
  525. (cdr results)))
  526. tree)
  527. ;;;
  528. ;;; Unused variable analysis.
  529. ;;;
  530. ;; <binding-info> records are used during tree traversals in
  531. ;; `unused-variable-analysis'. They contain a list of the local vars
  532. ;; currently in scope, and a list of locals vars that have been referenced.
  533. (define-record-type <binding-info>
  534. (make-binding-info vars refs)
  535. binding-info?
  536. (vars binding-info-vars) ;; ((GENSYM NAME LOCATION) ...)
  537. (refs binding-info-refs)) ;; (GENSYM ...)
  538. (define unused-variable-analysis
  539. ;; Report unused variables in the given tree.
  540. (make-tree-analysis
  541. (lambda (x info env locs)
  542. ;; X is a leaf: extend INFO's refs accordingly.
  543. (let ((refs (binding-info-refs info))
  544. (vars (binding-info-vars info)))
  545. (record-case x
  546. ((<lexical-ref> gensym)
  547. (make-binding-info vars (vhash-consq gensym #t refs)))
  548. (else info))))
  549. (lambda (x info env locs)
  550. ;; Going down into X: extend INFO's variable list
  551. ;; accordingly.
  552. (let ((refs (binding-info-refs info))
  553. (vars (binding-info-vars info))
  554. (src (tree-il-src x)))
  555. (define (extend inner-vars inner-names)
  556. (fold (lambda (var name vars)
  557. (vhash-consq var (list name src) vars))
  558. vars
  559. inner-vars
  560. inner-names))
  561. (record-case x
  562. ((<lexical-set> gensym)
  563. (make-binding-info vars (vhash-consq gensym #t refs)))
  564. ((<lambda-case> req opt inits rest kw gensyms)
  565. (let ((names `(,@req
  566. ,@(or opt '())
  567. ,@(if rest (list rest) '())
  568. ,@(if kw (map cadr (cdr kw)) '()))))
  569. (make-binding-info (extend gensyms names) refs)))
  570. ((<let> gensyms names)
  571. (make-binding-info (extend gensyms names) refs))
  572. ((<letrec> gensyms names)
  573. (make-binding-info (extend gensyms names) refs))
  574. ((<fix> gensyms names)
  575. (make-binding-info (extend gensyms names) refs))
  576. (else info))))
  577. (lambda (x info env locs)
  578. ;; Leaving X's scope: shrink INFO's variable list
  579. ;; accordingly and reported unused nested variables.
  580. (let ((refs (binding-info-refs info))
  581. (vars (binding-info-vars info)))
  582. (define (shrink inner-vars refs)
  583. (vlist-for-each
  584. (lambda (var)
  585. (let ((gensym (car var)))
  586. ;; Don't report lambda parameters as unused.
  587. (if (and (memq gensym inner-vars)
  588. (not (vhash-assq gensym refs))
  589. (not (lambda-case? x)))
  590. (let ((name (cadr var))
  591. ;; We can get approximate source location by going up
  592. ;; the LOCS location stack.
  593. (loc (or (caddr var)
  594. (find pair? locs))))
  595. (warning 'unused-variable loc name)))))
  596. vars)
  597. (vlist-drop vars (length inner-vars)))
  598. ;; For simplicity, we leave REFS untouched, i.e., with
  599. ;; names of variables that are now going out of scope.
  600. ;; It doesn't hurt as these are unique names, it just
  601. ;; makes REFS unnecessarily fat.
  602. (record-case x
  603. ((<lambda-case> gensyms)
  604. (make-binding-info (shrink gensyms refs) refs))
  605. ((<let> gensyms)
  606. (make-binding-info (shrink gensyms refs) refs))
  607. ((<letrec> gensyms)
  608. (make-binding-info (shrink gensyms refs) refs))
  609. ((<fix> gensyms)
  610. (make-binding-info (shrink gensyms refs) refs))
  611. (else info))))
  612. (lambda (result env) #t)
  613. (make-binding-info vlist-null vlist-null)))
  614. ;;;
  615. ;;; Unused top-level variable analysis.
  616. ;;;
  617. ;; <reference-graph> record top-level definitions that are made, references to
  618. ;; top-level definitions and their context (the top-level definition in which
  619. ;; the reference appears), as well as the current context (the top-level
  620. ;; definition we're currently in). The second part (`refs' below) is
  621. ;; effectively a graph from which we can determine unused top-level definitions.
  622. (define-record-type <reference-graph>
  623. (make-reference-graph refs defs toplevel-context)
  624. reference-graph?
  625. (defs reference-graph-defs) ;; ((NAME . LOC) ...)
  626. (refs reference-graph-refs) ;; ((REF-CONTEXT REF ...) ...)
  627. (toplevel-context reference-graph-toplevel-context)) ;; NAME | #f
  628. (define (graph-reachable-nodes root refs reachable)
  629. ;; Add to REACHABLE the nodes reachable from ROOT in graph REFS. REFS is a
  630. ;; vhash mapping nodes to the list of their children: for instance,
  631. ;; ((A -> (B C)) (B -> (A)) (C -> ())) corresponds to
  632. ;;
  633. ;; ,-------.
  634. ;; v |
  635. ;; A ----> B
  636. ;; |
  637. ;; v
  638. ;; C
  639. ;;
  640. ;; REACHABLE is a vhash of nodes known to be otherwise reachable.
  641. (let loop ((root root)
  642. (path vlist-null)
  643. (result reachable))
  644. (if (or (vhash-assq root path)
  645. (vhash-assq root result))
  646. result
  647. (let* ((children (or (and=> (vhash-assq root refs) cdr) '()))
  648. (path (vhash-consq root #t path))
  649. (result (fold (lambda (kid result)
  650. (loop kid path result))
  651. result
  652. children)))
  653. (fold (lambda (kid result)
  654. (vhash-consq kid #t result))
  655. result
  656. children)))))
  657. (define (graph-reachable-nodes* roots refs)
  658. ;; Return the list of nodes in REFS reachable from the nodes listed in ROOTS.
  659. (vlist-fold (lambda (root+true result)
  660. (let* ((root (car root+true))
  661. (reachable (graph-reachable-nodes root refs result)))
  662. (vhash-consq root #t reachable)))
  663. vlist-null
  664. roots))
  665. (define (partition* pred vhash)
  666. ;; Partition VHASH according to PRED. Return the two resulting vhashes.
  667. (let ((result
  668. (vlist-fold (lambda (k+v result)
  669. (let ((k (car k+v))
  670. (v (cdr k+v))
  671. (r1 (car result))
  672. (r2 (cdr result)))
  673. (if (pred k)
  674. (cons (vhash-consq k v r1) r2)
  675. (cons r1 (vhash-consq k v r2)))))
  676. (cons vlist-null vlist-null)
  677. vhash)))
  678. (values (car result) (cdr result))))
  679. (define unused-toplevel-analysis
  680. ;; Report unused top-level definitions that are not exported.
  681. (let ((add-ref-from-context
  682. (lambda (graph name)
  683. ;; Add an edge CTX -> NAME in GRAPH.
  684. (let* ((refs (reference-graph-refs graph))
  685. (defs (reference-graph-defs graph))
  686. (ctx (reference-graph-toplevel-context graph))
  687. (ctx-refs (or (and=> (vhash-assq ctx refs) cdr) '())))
  688. (make-reference-graph (vhash-consq ctx (cons name ctx-refs) refs)
  689. defs ctx)))))
  690. (define (macro-variable? name env)
  691. (and (module? env)
  692. (let ((var (module-variable env name)))
  693. (and var (variable-bound? var)
  694. (macro? (variable-ref var))))))
  695. (make-tree-analysis
  696. (lambda (x graph env locs)
  697. ;; X is a leaf.
  698. (let ((ctx (reference-graph-toplevel-context graph)))
  699. (record-case x
  700. ((<toplevel-ref> name src)
  701. (add-ref-from-context graph name))
  702. (else graph))))
  703. (lambda (x graph env locs)
  704. ;; Going down into X.
  705. (let ((ctx (reference-graph-toplevel-context graph))
  706. (refs (reference-graph-refs graph))
  707. (defs (reference-graph-defs graph)))
  708. (record-case x
  709. ((<toplevel-define> name src)
  710. (let ((refs refs)
  711. (defs (vhash-consq name (or src (find pair? locs))
  712. defs)))
  713. (make-reference-graph refs defs name)))
  714. ((<toplevel-set> name src)
  715. (add-ref-from-context graph name))
  716. (else graph))))
  717. (lambda (x graph env locs)
  718. ;; Leaving X's scope.
  719. (record-case x
  720. ((<toplevel-define>)
  721. (let ((refs (reference-graph-refs graph))
  722. (defs (reference-graph-defs graph)))
  723. (make-reference-graph refs defs #f)))
  724. (else graph)))
  725. (lambda (graph env)
  726. ;; Process the resulting reference graph: determine all private definitions
  727. ;; not reachable from any public definition. Macros
  728. ;; (syntax-transformers), which are globally bound, never considered
  729. ;; unused since we can't tell whether a macro is actually used; in
  730. ;; addition, macros are considered roots of the graph since they may use
  731. ;; private bindings. FIXME: The `make-syntax-transformer' calls don't
  732. ;; contain any literal `toplevel-ref' of the global bindings they use so
  733. ;; this strategy fails.
  734. (define (exported? name)
  735. (if (module? env)
  736. (module-variable (module-public-interface env) name)
  737. #t))
  738. (let-values (((public-defs private-defs)
  739. (partition* (lambda (name)
  740. (or (exported? name)
  741. (macro-variable? name env)))
  742. (reference-graph-defs graph))))
  743. (let* ((roots (vhash-consq #f #t public-defs))
  744. (refs (reference-graph-refs graph))
  745. (reachable (graph-reachable-nodes* roots refs))
  746. (unused (vlist-filter (lambda (name+src)
  747. (not (vhash-assq (car name+src)
  748. reachable)))
  749. private-defs)))
  750. (vlist-for-each (lambda (name+loc)
  751. (let ((name (car name+loc))
  752. (loc (cdr name+loc)))
  753. (warning 'unused-toplevel loc name)))
  754. unused))))
  755. (make-reference-graph vlist-null vlist-null #f))))
  756. ;;;
  757. ;;; Unbound variable analysis.
  758. ;;;
  759. ;; <toplevel-info> records are used during tree traversal in search of
  760. ;; possibly unbound variable. They contain a list of references to
  761. ;; potentially unbound top-level variables, and a list of the top-level
  762. ;; defines that have been encountered.
  763. (define-record-type <toplevel-info>
  764. (make-toplevel-info refs defs)
  765. toplevel-info?
  766. (refs toplevel-info-refs) ;; ((VARIABLE-NAME . LOCATION) ...)
  767. (defs toplevel-info-defs)) ;; (VARIABLE-NAME ...)
  768. (define (goops-toplevel-definition proc args env)
  769. ;; If application of PROC to ARGS is a GOOPS top-level definition, return
  770. ;; the name of the variable being defined; otherwise return #f. This
  771. ;; assumes knowledge of the current implementation of `define-class' et al.
  772. (define (toplevel-define-arg args)
  773. (and (pair? args) (pair? (cdr args)) (null? (cddr args))
  774. (record-case (car args)
  775. ((<const> exp)
  776. (and (symbol? exp) exp))
  777. (else #f))))
  778. (record-case proc
  779. ((<module-ref> mod public? name)
  780. (and (equal? mod '(oop goops))
  781. (not public?)
  782. (eq? name 'toplevel-define!)
  783. (toplevel-define-arg args)))
  784. ((<toplevel-ref> name)
  785. ;; This may be the result of expanding one of the GOOPS macros within
  786. ;; `oop/goops.scm'.
  787. (and (eq? name 'toplevel-define!)
  788. (eq? env (resolve-module '(oop goops)))
  789. (toplevel-define-arg args)))
  790. (else #f)))
  791. (define unbound-variable-analysis
  792. ;; Report possibly unbound variables in the given tree.
  793. (make-tree-analysis
  794. (lambda (x info env locs)
  795. ;; X is a leaf: extend INFO's refs accordingly.
  796. (let ((refs (toplevel-info-refs info))
  797. (defs (toplevel-info-defs info)))
  798. (define (bound? name)
  799. (or (and (module? env)
  800. (module-variable env name))
  801. (vhash-assq name defs)))
  802. (record-case x
  803. ((<toplevel-ref> name src)
  804. (if (bound? name)
  805. info
  806. (let ((src (or src (find pair? locs))))
  807. (make-toplevel-info (vhash-consq name src refs)
  808. defs))))
  809. (else info))))
  810. (lambda (x info env locs)
  811. ;; Going down into X.
  812. (let* ((refs (toplevel-info-refs info))
  813. (defs (toplevel-info-defs info))
  814. (src (tree-il-src x)))
  815. (define (bound? name)
  816. (or (and (module? env)
  817. (module-variable env name))
  818. (vhash-assq name defs)))
  819. (record-case x
  820. ((<toplevel-set> name src)
  821. (if (bound? name)
  822. (make-toplevel-info refs defs)
  823. (let ((src (find pair? locs)))
  824. (make-toplevel-info (vhash-consq name src refs)
  825. defs))))
  826. ((<toplevel-define> name)
  827. (make-toplevel-info (vhash-delete name refs eq?)
  828. (vhash-consq name #t defs)))
  829. ((<application> proc args)
  830. ;; Check for a dynamic top-level definition, as is
  831. ;; done by code expanded from GOOPS macros.
  832. (let ((name (goops-toplevel-definition proc args
  833. env)))
  834. (if (symbol? name)
  835. (make-toplevel-info (vhash-delete name refs
  836. eq?)
  837. (vhash-consq name #t defs))
  838. (make-toplevel-info refs defs))))
  839. (else
  840. (make-toplevel-info refs defs)))))
  841. (lambda (x info env locs)
  842. ;; Leaving X's scope.
  843. info)
  844. (lambda (toplevel env)
  845. ;; Post-process the result.
  846. (vlist-for-each (lambda (name+loc)
  847. (let ((name (car name+loc))
  848. (loc (cdr name+loc)))
  849. (warning 'unbound-variable loc name)))
  850. (vlist-reverse (toplevel-info-refs toplevel))))
  851. (make-toplevel-info vlist-null vlist-null)))
  852. ;;;
  853. ;;; Arity analysis.
  854. ;;;
  855. ;; <arity-info> records contain information about lexical definitions of
  856. ;; procedures currently in scope, top-level procedure definitions that have
  857. ;; been encountered, and calls to top-level procedures that have been
  858. ;; encountered.
  859. (define-record-type <arity-info>
  860. (make-arity-info toplevel-calls lexical-lambdas toplevel-lambdas)
  861. arity-info?
  862. (toplevel-calls toplevel-procedure-calls) ;; ((NAME . APPLICATION) ...)
  863. (lexical-lambdas lexical-lambdas) ;; ((GENSYM . DEFINITION) ...)
  864. (toplevel-lambdas toplevel-lambdas)) ;; ((NAME . DEFINITION) ...)
  865. (define (validate-arity proc application lexical?)
  866. ;; Validate the argument count of APPLICATION, a tree-il application of
  867. ;; PROC, emitting a warning in case of argument count mismatch.
  868. (define (filter-keyword-args keywords allow-other-keys? args)
  869. ;; Filter keyword arguments from ARGS and return the resulting list.
  870. ;; KEYWORDS is the list of allowed keywords, and ALLOW-OTHER-KEYS?
  871. ;; specified whethere keywords not listed in KEYWORDS are allowed.
  872. (let loop ((args args)
  873. (result '()))
  874. (if (null? args)
  875. (reverse result)
  876. (let ((arg (car args)))
  877. (if (and (const? arg)
  878. (or (memq (const-exp arg) keywords)
  879. (and allow-other-keys?
  880. (keyword? (const-exp arg)))))
  881. (loop (if (pair? (cdr args))
  882. (cddr args)
  883. '())
  884. result)
  885. (loop (cdr args)
  886. (cons arg result)))))))
  887. (define (arities proc)
  888. ;; Return the arities of PROC, which can be either a tree-il or a
  889. ;; procedure.
  890. (define (len x)
  891. (or (and (or (null? x) (pair? x))
  892. (length x))
  893. 0))
  894. (cond ((program? proc)
  895. (values (procedure-name proc)
  896. (map (lambda (a)
  897. (list (arity:nreq a) (arity:nopt a) (arity:rest? a)
  898. (map car (arity:kw a))
  899. (arity:allow-other-keys? a)))
  900. (program-arities proc))))
  901. ((procedure? proc)
  902. (let ((arity (procedure-minimum-arity proc)))
  903. (values (procedure-name proc)
  904. (list (list (car arity) (cadr arity) (caddr arity)
  905. #f #f)))))
  906. (else
  907. (let loop ((name #f)
  908. (proc proc)
  909. (arities '()))
  910. (if (not proc)
  911. (values name (reverse arities))
  912. (record-case proc
  913. ((<lambda-case> req opt rest kw alternate)
  914. (loop name alternate
  915. (cons (list (len req) (len opt) rest
  916. (and (pair? kw) (map car (cdr kw)))
  917. (and (pair? kw) (car kw)))
  918. arities)))
  919. ((<lambda> meta body)
  920. (loop (assoc-ref meta 'name) body arities))
  921. (else
  922. (values #f #f))))))))
  923. (let ((args (application-args application))
  924. (src (tree-il-src application)))
  925. (call-with-values (lambda () (arities proc))
  926. (lambda (name arities)
  927. (define matches?
  928. (find (lambda (arity)
  929. (pmatch arity
  930. ((,req ,opt ,rest? ,kw ,aok?)
  931. (let ((args (if (pair? kw)
  932. (filter-keyword-args kw aok? args)
  933. args)))
  934. (if (and req opt)
  935. (let ((count (length args)))
  936. (and (>= count req)
  937. (or rest?
  938. (<= count (+ req opt)))))
  939. #t)))
  940. (else #t)))
  941. arities))
  942. (if (not matches?)
  943. (warning 'arity-mismatch src
  944. (or name (with-output-to-string (lambda () (write proc))))
  945. lexical?)))))
  946. #t)
  947. (define arity-analysis
  948. ;; Report arity mismatches in the given tree.
  949. (make-tree-analysis
  950. (lambda (x info env locs)
  951. ;; X is a leaf.
  952. info)
  953. (lambda (x info env locs)
  954. ;; Down into X.
  955. (define (extend lexical-name val info)
  956. ;; If VAL is a lambda, add NAME to the lexical-lambdas of INFO.
  957. (let ((toplevel-calls (toplevel-procedure-calls info))
  958. (lexical-lambdas (lexical-lambdas info))
  959. (toplevel-lambdas (toplevel-lambdas info)))
  960. (record-case val
  961. ((<lambda> body)
  962. (make-arity-info toplevel-calls
  963. (vhash-consq lexical-name val
  964. lexical-lambdas)
  965. toplevel-lambdas))
  966. ((<lexical-ref> gensym)
  967. ;; lexical alias
  968. (let ((val* (vhash-assq gensym lexical-lambdas)))
  969. (if (pair? val*)
  970. (extend lexical-name (cdr val*) info)
  971. info)))
  972. ((<toplevel-ref> name)
  973. ;; top-level alias
  974. (make-arity-info toplevel-calls
  975. (vhash-consq lexical-name val
  976. lexical-lambdas)
  977. toplevel-lambdas))
  978. (else info))))
  979. (let ((toplevel-calls (toplevel-procedure-calls info))
  980. (lexical-lambdas (lexical-lambdas info))
  981. (toplevel-lambdas (toplevel-lambdas info)))
  982. (record-case x
  983. ((<toplevel-define> name exp)
  984. (record-case exp
  985. ((<lambda> body)
  986. (make-arity-info toplevel-calls
  987. lexical-lambdas
  988. (vhash-consq name exp toplevel-lambdas)))
  989. ((<toplevel-ref> name)
  990. ;; alias for another toplevel
  991. (let ((proc (vhash-assq name toplevel-lambdas)))
  992. (make-arity-info toplevel-calls
  993. lexical-lambdas
  994. (vhash-consq (toplevel-define-name x)
  995. (if (pair? proc)
  996. (cdr proc)
  997. exp)
  998. toplevel-lambdas))))
  999. (else info)))
  1000. ((<let> gensyms vals)
  1001. (fold extend info gensyms vals))
  1002. ((<letrec> gensyms vals)
  1003. (fold extend info gensyms vals))
  1004. ((<fix> gensyms vals)
  1005. (fold extend info gensyms vals))
  1006. ((<application> proc args src)
  1007. (record-case proc
  1008. ((<lambda> body)
  1009. (validate-arity proc x #t)
  1010. info)
  1011. ((<toplevel-ref> name)
  1012. (make-arity-info (vhash-consq name x toplevel-calls)
  1013. lexical-lambdas
  1014. toplevel-lambdas))
  1015. ((<lexical-ref> gensym)
  1016. (let ((proc (vhash-assq gensym lexical-lambdas)))
  1017. (if (pair? proc)
  1018. (record-case (cdr proc)
  1019. ((<toplevel-ref> name)
  1020. ;; alias to toplevel
  1021. (make-arity-info (vhash-consq name x toplevel-calls)
  1022. lexical-lambdas
  1023. toplevel-lambdas))
  1024. (else
  1025. (validate-arity (cdr proc) x #t)
  1026. info))
  1027. ;; If GENSYM wasn't found, it may be because it's an
  1028. ;; argument of the procedure being compiled.
  1029. info)))
  1030. (else info)))
  1031. (else info))))
  1032. (lambda (x info env locs)
  1033. ;; Up from X.
  1034. (define (shrink name val info)
  1035. ;; Remove NAME from the lexical-lambdas of INFO.
  1036. (let ((toplevel-calls (toplevel-procedure-calls info))
  1037. (lexical-lambdas (lexical-lambdas info))
  1038. (toplevel-lambdas (toplevel-lambdas info)))
  1039. (make-arity-info toplevel-calls
  1040. (if (vhash-assq name lexical-lambdas)
  1041. (vlist-tail lexical-lambdas)
  1042. lexical-lambdas)
  1043. toplevel-lambdas)))
  1044. (let ((toplevel-calls (toplevel-procedure-calls info))
  1045. (lexical-lambdas (lexical-lambdas info))
  1046. (toplevel-lambdas (toplevel-lambdas info)))
  1047. (record-case x
  1048. ((<let> gensyms vals)
  1049. (fold shrink info gensyms vals))
  1050. ((<letrec> gensyms vals)
  1051. (fold shrink info gensyms vals))
  1052. ((<fix> gensyms vals)
  1053. (fold shrink info gensyms vals))
  1054. (else info))))
  1055. (lambda (result env)
  1056. ;; Post-processing: check all top-level procedure calls that have been
  1057. ;; encountered.
  1058. (let ((toplevel-calls (toplevel-procedure-calls result))
  1059. (toplevel-lambdas (toplevel-lambdas result)))
  1060. (vlist-for-each
  1061. (lambda (name+application)
  1062. (let* ((name (car name+application))
  1063. (application (cdr name+application))
  1064. (proc
  1065. (or (and=> (vhash-assq name toplevel-lambdas) cdr)
  1066. (and (module? env)
  1067. (false-if-exception
  1068. (module-ref env name)))))
  1069. (proc*
  1070. ;; handle toplevel aliases
  1071. (if (toplevel-ref? proc)
  1072. (let ((name (toplevel-ref-name proc)))
  1073. (and (module? env)
  1074. (false-if-exception
  1075. (module-ref env name))))
  1076. proc)))
  1077. (if (or (lambda? proc*) (procedure? proc*))
  1078. (validate-arity proc* application (lambda? proc*)))))
  1079. toplevel-calls)))
  1080. (make-arity-info vlist-null vlist-null vlist-null)))