node-util.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Mike Sperber
  3. ; This file contains miscellaneous utilities for accessing and modifying the
  4. ; node tree.
  5. ; Get the root of the tree containing node.
  6. (define (node-base node)
  7. (do ((p node (node-parent p)))
  8. ((not (node? (node-parent p)))
  9. p)))
  10. ; Find the procedure node that contains NODE. Go up one parent at a time
  11. ; until a lambda node is found, then go up two at a time, skipping the
  12. ; intervening call nodes.
  13. (define (containing-procedure node)
  14. (do ((node (node-parent node) (node-parent node)))
  15. ((lambda-node? node)
  16. (do ((node node (node-parent (node-parent node))))
  17. ((proc-lambda? node) node)))))
  18. ; Trivial calls are those whose parents are call nodes.
  19. (define (trivial? call)
  20. (call-node? (node-parent call)))
  21. (define (nontrivial? call)
  22. (lambda-node? (node-parent call)))
  23. (define (nontrivial-ancestor call)
  24. (let loop ((call call))
  25. (if (or (not (node? (node-parent call)))
  26. (nontrivial? call))
  27. call
  28. (loop (node-parent call)))))
  29. (define (calls-this-primop? call id)
  30. (eq? id (primop-id (call-primop call))))
  31. ; Return the variable to which a value is bound by LET or LETREC.
  32. (define (bound-to-variable node)
  33. (let ((parent (node-parent node)))
  34. (case (primop-id (call-primop parent))
  35. ((let)
  36. (if (n= 0 (node-index node))
  37. (list-ref (lambda-variables (call-arg parent 0))
  38. (- (node-index node) 1))
  39. #f))
  40. ((letrec2)
  41. (if (< 1 (node-index node))
  42. (list-ref (lambda-variables
  43. (variable-binder
  44. (reference-variable (call-arg parent 1))))
  45. (- (node-index node) 1))
  46. #f))
  47. (else #f))))
  48. ; Return a list of all the reference to lambda-node L's value that call it.
  49. ; If not all can be identified then #F is returned.
  50. (define (find-calls l)
  51. (let ((refs (cond ((bound-to-variable l)
  52. => variable-refs)
  53. ((called-node? l)
  54. (list l))
  55. (else
  56. #f))))
  57. (cond ((and refs (every? called-node? refs))
  58. refs)
  59. ((calls-known? l)
  60. (bug "cannot find calls for known lambda ~S" l))
  61. (else #f))))
  62. ; Walk (or map) a tree-modifying procedure down a variable's references.
  63. (define (walk-refs-safely proc var)
  64. (for-each proc (copy-list (variable-refs var))))
  65. ; Return #t if the total primop-cost of NODE is less than SIZE.
  66. (define (small-node? node size)
  67. (let label ((call (lambda-body node)))
  68. (set! size (- size (primop-cost call)))
  69. (if (>= size 0)
  70. (walk-vector (lambda (n)
  71. (cond ((lambda-node? n)
  72. (label (lambda-body n)))
  73. ((call-node? n)
  74. (label n))))
  75. (call-args call))))
  76. (>= size 0))
  77. ; True if executing NODE involves side-effects.
  78. (define (side-effects? node . permissible)
  79. (let ((permissible (cons #f permissible)))
  80. (let label ((node node))
  81. (cond ((not (call-node? node))
  82. #f)
  83. ((and (= 0 (call-exits node))
  84. (memq (primop-side-effects (call-primop node))
  85. permissible))
  86. (let loop ((i (- (call-arg-count node) 1)))
  87. (cond ((< i 0) #f)
  88. ((label (call-arg node i)) #t)
  89. (else (loop (- i 1))))))
  90. (else
  91. #t)))))
  92. ; A conservative check - is there only one SET-CONTENTS call for the owner and
  93. ; are all calls between CALL and the LETREC call that binds the owner calls to
  94. ; SET-CONTENTS?
  95. ;(define (single-letrec-set? call)
  96. ; (let ((owner (call-arg call set/owner)))
  97. ; (and (reference-node? owner)
  98. ; (every? (lambda (ref)
  99. ; (or (eq? (node-parent ref) call)
  100. ; (not (set-reference? ref))))
  101. ; (variable-refs (reference-variable owner))))))
  102. ;(define (set-reference? node)
  103. ; (and (eq? 'set-contents
  104. ; (primop-id (call-primop (node-parent node))))
  105. ; (= (node-index node) set/owner)))
  106. ;-------------------------------------------------------------------------------
  107. (define the-undefined-value (list '*undefined-value*))
  108. (define (undefined-value? x)
  109. (eq? x the-undefined-value))
  110. (define (undefined-value-node? x)
  111. (and (literal-node? x)
  112. (undefined-value? (literal-value x))))
  113. (define (make-undefined-literal)
  114. (make-literal-node the-undefined-value #f))
  115. ;-------------------------------------------------------------------------------
  116. ; Finding the lambda node called by CALL, JUMP, or RETURN
  117. (define (called-node? node)
  118. (and (node? (node-parent node))
  119. (eq? node (called-node (node-parent node)))))
  120. (define (called-node call)
  121. (cond ((and (primop-procedure? (call-primop call))
  122. (primop-call-index (call-primop call)))
  123. => (lambda (i)
  124. (call-arg call i)))
  125. (else '#f)))
  126. (define (called-lambda call)
  127. (get-lambda-value (call-arg call (primop-call-index (call-primop call)))))
  128. (define (get-lambda-value value)
  129. (cond ((lambda-node? value)
  130. value)
  131. ((reference-node? value)
  132. (get-variable-lambda (reference-variable value)))
  133. (else
  134. (error "peculiar procedure in ~S" value))))
  135. (define (get-variable-lambda variable)
  136. (if (global-variable? variable)
  137. (or (variable-known-lambda variable)
  138. (error "peculiar procedure variable ~S" variable))
  139. (let* ((binder (variable-binder variable))
  140. (index (node-index binder))
  141. (call (node-parent binder))
  142. (lose (lambda ()
  143. (error "peculiar procedure variable ~S" variable))))
  144. (case (primop-id (call-primop call))
  145. ((let)
  146. (if (= 0 index)
  147. (get-lambda-value (call-arg call (+ 1 (variable-index variable))))
  148. (lose)))
  149. ((letrec1)
  150. (if (= 0 index)
  151. (get-letrec-variable-lambda variable)
  152. (lose)))
  153. ((call)
  154. (if (and (= 1 index)
  155. (= 0 (variable-index variable))) ; var is a continuation var
  156. (get-lambda-value (call-arg call 0))
  157. (lose)))
  158. (else
  159. (lose))))))
  160. ; Some of the checking can be removed once I know the LETREC code works.
  161. (define (get-letrec-variable-lambda variable)
  162. (let* ((binder (variable-binder variable))
  163. (call (lambda-body binder)))
  164. (if (and (eq? 'letrec2 (primop-id (call-primop call)))
  165. (reference-node? (call-arg call 1))
  166. (eq? (car (lambda-variables binder))
  167. (reference-variable (call-arg call 1))))
  168. (call-arg call (+ 1 (variable-index variable)))
  169. (error "LETREC is incorrectly organized ~S" (node-parent binder)))))
  170. ;(define (get-cell-variable-lambda variable)
  171. ; (let ((ref (first set-reference? (variable-refs variable))))
  172. ; (if (and ref
  173. ; (eq? 'letrec
  174. ; (literal-value (call-arg (node-parent ref) set/type))))
  175. ; (get-lambda-value (call-arg (node-parent ref) set/value))
  176. ; (error "peculiar lambda cell ~S" variable))))
  177. ;-------------------------------------------------------------------------------
  178. ; Attaching and detaching arguments to calls
  179. ; Make ARGS the arguments of call node PARENT. ARGS may contain #f.
  180. (define (attach-call-args parent args)
  181. (let ((len (call-arg-count parent)))
  182. (let loop ((args args) (i 0))
  183. (cond ((null? args)
  184. (if (< i (- len 1))
  185. (bug '"too few arguments added to node ~S" parent))
  186. (values))
  187. ((>= i len)
  188. (bug '"too many arguments added to node ~S" parent))
  189. (else
  190. (if (car args)
  191. (attach parent i (car args)))
  192. (loop (cdr args) (+ 1 i)))))))
  193. ; Remove all of the arguments of NODE.
  194. (define (remove-call-args node)
  195. (let ((len (call-arg-count node)))
  196. (do ((i 1 (+ i 1)))
  197. ((>= i len))
  198. (if (not (empty? (call-arg node i)))
  199. (erase (detach (call-arg node i)))))
  200. (values)))
  201. ; Replace the arguments of call node NODE with NEW-ARGS.
  202. (define (replace-call-args node new-args)
  203. (let ((len (length new-args)))
  204. (remove-call-args node)
  205. (if (n= len (call-arg-count node))
  206. (let ((new (make-vector len empty))
  207. (old (call-args node)))
  208. (set-call-args! node new)))
  209. (attach-call-args node new-args)))
  210. ; Remove all arguments to CALL that are EMPTY?. COUNT is the number of
  211. ; non-EMPTY? arguments.
  212. (define (remove-null-arguments call count)
  213. (let ((old (call-args call))
  214. (new (make-vector count empty)))
  215. (let loop ((i 0) (j 0))
  216. (cond ((>= j count)
  217. (values))
  218. ((not (empty? (vector-ref old i)))
  219. (set-node-index! (vector-ref old i) j)
  220. (vector-set! new j (vector-ref old i))
  221. (loop (+ i 1) (+ j 1)))
  222. (else
  223. (loop (+ i 1) j))))
  224. (set-call-args! call new)
  225. (values)))
  226. ; Remove all but the first COUNT arguments from CALL.
  227. (define (shorten-call-args call count)
  228. (let ((old (call-args call))
  229. (new (make-vector count empty)))
  230. (vector-replace new old count)
  231. (do ((i (+ count 1) (+ i 1)))
  232. ((>= i (vector-length old)))
  233. (erase (vector-ref old i)))
  234. (set-call-args! call new)
  235. (values)))
  236. ; Insert ARG as the INDEXth argument to CALL.
  237. (define (insert-call-arg call index arg)
  238. (let* ((old (call-args call))
  239. (len (vector-length old))
  240. (new (make-vector (+ 1 len) empty)))
  241. (vector-replace new old index)
  242. (do ((i index (+ i 1)))
  243. ((>= i len))
  244. (vector-set! new (+ i 1) (vector-ref old i))
  245. (set-node-index! (vector-ref old i) (+ i 1)))
  246. (set-call-args! call new)
  247. (attach call index arg)
  248. (values)))
  249. ; Remove the INDEXth argument to CALL.
  250. (define (remove-call-arg call index)
  251. (let* ((old (call-args call))
  252. (len (- (vector-length old) 1))
  253. (new (make-vector len)))
  254. (vector-replace new old index)
  255. (if (node? (vector-ref old index))
  256. (erase (detach (vector-ref old index))))
  257. (do ((i index (+ i 1)))
  258. ((>= i len))
  259. (vector-set! new i (vector-ref old (+ i 1)))
  260. (set-node-index! (vector-ref new i) i))
  261. (set-call-args! call new)
  262. (if (< index (call-exits call))
  263. (set-call-exits! call (- (call-exits call) 1)))
  264. (values)))
  265. ; Add ARG to the end of CALL's arguments.
  266. (define (append-call-arg call arg)
  267. (insert-call-arg call (call-arg-count call) arg))
  268. ; Replace CALL with the body of its continuation.
  269. (define (remove-body call)
  270. (if (n= 1 (call-exits call))
  271. (bug "removing a call with ~D exits" (call-exits call))
  272. (replace-body call (detach-body (lambda-body (call-arg call 0))))))
  273. ; Avoiding N-Ary Procedures
  274. ; These are used in the expansion of the LET-NODES macro.
  275. (define (attach-two-call-args node a0 a1)
  276. (attach node 0 a0)
  277. (attach node 1 a1))
  278. (define (attach-three-call-args node a0 a1 a2)
  279. (attach node 0 a0)
  280. (attach node 1 a1)
  281. (attach node 2 a2))
  282. (define (attach-four-call-args node a0 a1 a2 a3)
  283. (attach node 0 a0)
  284. (attach node 1 a1)
  285. (attach node 2 a2)
  286. (attach node 3 a3))
  287. (define (attach-five-call-args node a0 a1 a2 a3 a4)
  288. (attach node 0 a0)
  289. (attach node 1 a1)
  290. (attach node 2 a2)
  291. (attach node 3 a3)
  292. (attach node 4 a4))
  293. ;-------------------------------------------------------------------------------
  294. ; Bind VARS to VALUES using letrec at CALL. If CALL is already a letrec
  295. ; call, just add to it, otherwise make a new one.
  296. (define (put-in-letrec vars values call)
  297. (cond ((eq? 'letrec2 (primop-id (call-primop call)))
  298. (let ((binder (node-parent call)))
  299. (mark-changed call)
  300. (for-each (lambda (var)
  301. (set-variable-binder! var binder))
  302. vars)
  303. (set-lambda-variables! binder
  304. (append (lambda-variables binder) vars))
  305. (for-each (lambda (value)
  306. (append-call-arg call value))
  307. values)))
  308. (else
  309. (move-body
  310. call
  311. (lambda (call)
  312. (receive (letrec-call letrec-cont)
  313. (make-letrec vars values)
  314. (attach-body letrec-cont call)
  315. letrec-call))))))
  316. (define (make-letrec vars vals)
  317. (let ((cont (make-lambda-node 'c 'cont '())))
  318. (let-nodes ((call (letrec1 1 l2))
  319. (l2 ((x #f) . vars) (letrec2 1 cont (* x) . vals)))
  320. (values call cont))))
  321. ;-------------------------------------------------------------------------------
  322. ; Changing lambda-nodes' variable lists
  323. (define (remove-lambda-variable l-node index)
  324. (remove-variable l-node (list-ref (lambda-variables l-node) index)))
  325. (define (remove-variable l-node var)
  326. (if (used? var)
  327. (bug '"cannot remove referenced variable ~s" var))
  328. (erase-variable var)
  329. (let ((vars (lambda-variables l-node)))
  330. (if (eq? (car vars) var)
  331. (set-lambda-variables! l-node (cdr vars))
  332. (do ((vars vars (cdr vars)))
  333. ((eq? (cadr vars) var)
  334. (set-cdr! vars (cddr vars)))))))
  335. ; Remove all of L-NODES' unused variables.
  336. (define (remove-unused-variables l-node)
  337. (set-lambda-variables! l-node
  338. (filter! (lambda (v)
  339. (cond ((used? v)
  340. #t)
  341. (else
  342. (erase-variable v)
  343. #f)))
  344. (lambda-variables l-node))))
  345. ;------------------------------------------------------------------------------
  346. ; Substituting Values For Variables
  347. ; Substitute VAL for VAR. If DETACH? is true then VAL should be detached
  348. ; and so can be used instead of a copy for the first substitution.
  349. ;
  350. ; If VAL is a reference to a variable named V, it was probably introduced by
  351. ; the CPS conversion code. In that case, the variable is renamed with the
  352. ; name of VAR. This helps considerably when debugging the compiler.
  353. (define (substitute var val detach?)
  354. (if (and (reference-node? val)
  355. (eq? 'v (variable-name (reference-variable val)))
  356. (not (global-variable? (reference-variable val))))
  357. (set-variable-name! (reference-variable val)
  358. (variable-name var)))
  359. (let ((refs (variable-refs var)))
  360. (set-variable-refs! var '())
  361. (cond ((not (null? refs))
  362. (for-each (lambda (ref)
  363. (replace ref (copy-node-tree val)))
  364. (if detach? (cdr refs) refs))
  365. (if detach? (replace (car refs) (detach val))))
  366. (detach?
  367. (erase (detach val))))))
  368. ; Walk the tree NODE replacing references to variables in OLD-VARS with
  369. ; the corresponding variables in NEW-VARS. Uses VARIABLE-FLAG to mark
  370. ; the variables being replaced.
  371. (define (substitute-vars-in-node-tree node old-vars new-vars)
  372. (for-each (lambda (old new)
  373. (set-variable-flag! old new))
  374. old-vars
  375. new-vars)
  376. (let tree-walk ((node node))
  377. (cond ((lambda-node? node)
  378. (walk-vector tree-walk (call-args (lambda-body node))))
  379. ((call-node? node)
  380. (walk-vector tree-walk (call-args node)))
  381. ((and (reference-node? node)
  382. (variable-flag (reference-variable node)))
  383. => (lambda (new)
  384. (replace node (make-reference-node new))))))
  385. (for-each (lambda (old)
  386. (set-variable-flag! old #f))
  387. old-vars))
  388. ; Replaces the call node CALL with VALUE.
  389. ; (<proc> <exit> . <args>) => (<exit> <value>)
  390. (define (replace-call-with-value call value)
  391. (cond ((n= 1 (call-exits call))
  392. (bug '"can only substitute for call with one exit ~s" call))
  393. (else
  394. (let ((cont (detach (call-arg call 0))))
  395. (set-call-exits! call 0)
  396. (replace-call-args call (if value (list cont value) (list cont)))
  397. (set-call-primop! call (get-primop (enum primop let)))))))
  398. ;------------------------------------------------------------------------------
  399. ; Copying Node Trees
  400. ; Copy the node-tree NODE. This dispatches on the type of NODE.
  401. ; Variables which have been copied have the copy in the node-flag field.
  402. (define (copy-node-tree node)
  403. (let ((new (cond ((lambda-node? node)
  404. (copy-lambda node))
  405. ((reference-node? node)
  406. (let ((var (reference-variable node)))
  407. (cond ((and (variable-binder var)
  408. (variable-flag var))
  409. => make-reference-node)
  410. (else
  411. (make-reference-node var)))))
  412. ((call-node? node)
  413. (copy-call node))
  414. ((literal-node? node)
  415. (copy-literal-node node)))))
  416. new))
  417. ; Copy a lambda node and its variables. The variables' copies are put in
  418. ; their VARIABLE-FLAG while the lambda's body is being copied.
  419. (define (copy-lambda node)
  420. (let* ((vars (map (lambda (var)
  421. (if var
  422. (let ((new (copy-variable var)))
  423. (set-variable-flag! var new)
  424. new)
  425. #f))
  426. (lambda-variables node)))
  427. (new-node (make-lambda-node (lambda-name node)
  428. (lambda-type node)
  429. vars)))
  430. (attach-body new-node (copy-call (lambda-body node)))
  431. (set-lambda-protocol! new-node (lambda-protocol node))
  432. (set-lambda-source! new-node (lambda-source node))
  433. (for-each (lambda (var)
  434. (if var (set-variable-flag! var #f)))
  435. (lambda-variables node))
  436. new-node))
  437. (define (copy-call node)
  438. (let ((new-node (make-call-node (call-primop node)
  439. (call-arg-count node)
  440. (call-exits node))))
  441. (do ((i 0 (+ i 1)))
  442. ((>= i (call-arg-count node)))
  443. (attach new-node i (copy-node-tree (call-arg node i))))
  444. (set-call-source! new-node (call-source node))
  445. new-node))
  446. ;------------------------------------------------------------------------------
  447. ; Checking the scoping of identifers
  448. ; Mark all ancestors of N with FLAG
  449. (define (mark-ancestors n flag)
  450. (do ((n n (node-parent n)))
  451. ((not (node? n)) (values))
  452. (set-node-flag! n flag)))
  453. ; Does N have an ancestor with a non-#f flag?
  454. (define (marked-ancestor? n)
  455. (do ((n n (node-parent n)))
  456. ((or (not (node? n))
  457. (node-flag n))
  458. (node? n))))
  459. ; Does N have an ancestor with a #f flag?
  460. (define (unmarked-ancestor? n)
  461. (do ((n n (node-parent n)))
  462. ((or (not (node? n))
  463. (not (node-flag n)))
  464. (node? n))))
  465. ; Is ANC? an ancestor of NODE?
  466. (define (node-ancestor? anc? node)
  467. (set-node-flag! anc? #t)
  468. (let ((okay? (marked-ancestor? node)))
  469. (set-node-flag! anc? #f)
  470. okay?))
  471. ; Find the lowest ancestor of N that has a non-#f flag
  472. (define (marked-ancestor n)
  473. (do ((n n (node-parent n)))
  474. ((or (not (node? n))
  475. (node-flag n))
  476. (if (node? n) n #f))))
  477. ; Mark the ancestors of START with #f, stopping when END is reached
  478. (define (unmark-ancestors-to start end)
  479. (do ((node start (node-parent node)))
  480. ((eq? node end))
  481. (set-node-flag! node #f)))
  482. ; Return the lowest node that is above all NODES
  483. (define (least-common-ancestor nodes)
  484. (mark-ancestors (car nodes) #t)
  485. (let loop ((nodes (cdr nodes)) (top (car nodes)))
  486. (cond ((null? nodes)
  487. (mark-ancestors top #f)
  488. top)
  489. (else
  490. (let ((new (marked-ancestor (car nodes))))
  491. (unmark-ancestors-to top new)
  492. (loop (cdr nodes) new))))))
  493. ; Can TO be moved to FROM without taking variables out of scope.
  494. ; This first marks all of the ancestors of FROM, and then unmarks all of the
  495. ; ancestors of TO. The net result is to mark every node that is above FROM but
  496. ; not above TO. Then if any reference-node below FROM references a variable
  497. ; with a marked binder, that node, and thus FROM itself, cannot legally be
  498. ; moved to TO.
  499. ; This is not currently used anywhere, and it doesn't know about trivial
  500. ; calls.
  501. (define (hoistable-node? from to)
  502. (let ((from (if (call-node? from)
  503. (node-parent (nontrivial-ancestor from))
  504. from)))
  505. (mark-ancestors (node-parent from) #t)
  506. (mark-ancestors to #f)
  507. (let ((okay? (let label ((n from))
  508. (cond ((lambda-node? n)
  509. (let* ((vec (call-args (lambda-body n)))
  510. (c (vector-length vec)))
  511. (let loop ((i 0))
  512. (cond ((>= i c) #t)
  513. ((label (vector-ref vec i))
  514. (loop (+ i 1)))
  515. (else #f)))))
  516. ((reference-node? n)
  517. (let ((b (variable-binder (reference-variable n))))
  518. (or (not b) (not (node-flag b)))))
  519. (else #t)))))
  520. (mark-ancestors (node-parent from) #f)
  521. okay?)))
  522. ; Mark all of the lambda nodes which bind variables referenced below NODE.
  523. (define (mark-binders node)
  524. (let label ((n node))
  525. (cond ((lambda-node? n)
  526. (walk-vector label (call-args (lambda-body n))))
  527. ((reference-node? n)
  528. (let ((b (variable-binder (reference-variable n))))
  529. (if b (set-node-flag! b #f))))))
  530. (values))
  531. ;------------------------------------------------------------------------------
  532. ; For each lambda-node L this sets (PARENT L) to be the enclosing PROC node
  533. ; of L and, if L is a PROC node, sets (KIDS L) to be the lambda nodes it
  534. ; encloses.
  535. (define (find-scoping lambdas parent set-parent! kids set-kids!)
  536. (receive (procs others)
  537. (partition-list proc-lambda? lambdas)
  538. (for-each (lambda (l)
  539. (set-parent! l #f)
  540. (set-kids! l '()))
  541. procs)
  542. (for-each (lambda (l)
  543. (set-parent! l #f))
  544. others)
  545. (letrec ((set-lambda-parent!
  546. (lambda (l)
  547. (cond ((parent l)
  548. => identity)
  549. ((proc-ancestor l)
  550. => (lambda (p)
  551. (let ((p (if (proc-lambda? p)
  552. p
  553. (set-lambda-parent! p))))
  554. (set-kids! p (cons l (kids p)))
  555. (set-parent! l p)
  556. p)))
  557. (else #f)))))
  558. (for-each set-lambda-parent! lambdas))
  559. (values procs others)))
  560. (define (proc-ancestor node)
  561. (let ((p (node-parent node)))
  562. (if (not (node? p))
  563. #f
  564. (let ((node (do ((p p (node-parent p)))
  565. ((lambda-node? p)
  566. p))))
  567. (do ((node node (node-parent (node-parent node))))
  568. ((proc-lambda? node)
  569. node))))))
  570. (define (no-free-references? node)
  571. (if (call-node? node)
  572. (error "NO-FREE-REFERENCES only works on value nodes: ~S" node))
  573. (let label ((node node))
  574. (cond ((reference-node? node)
  575. (let ((b (variable-binder (reference-variable node))))
  576. (or (not b)
  577. (node-flag b))))
  578. ((lambda-node? node)
  579. (set-node-flag! node #t)
  580. (let ((res (label (lambda-body node))))
  581. (set-node-flag! node #f)
  582. res))
  583. ((call-node? node)
  584. (let ((vec (call-args node)))
  585. (let loop ((i (- (vector-length vec) 1)))
  586. (cond ((< i 0) #t)
  587. ((not (label (vector-ref vec i))) #f)
  588. (else (loop (- i 1)))))))
  589. (else #t))))
  590. (define (node-type node)
  591. (cond ((literal-node? node)
  592. (literal-type node))
  593. ((reference-node? node)
  594. (variable-type (reference-variable node)))
  595. ((lambda-node? node)
  596. (lambda-node-type node))
  597. ((and (call-node? node)
  598. (primop-trivial? (call-primop node)))
  599. (trivial-call-return-type node))
  600. (else
  601. (error "node ~S does not represent a value" node))))
  602. ;----------------------------------------------------------------
  603. ; Debugging utilities
  604. (define (show-simplified node)
  605. (let loop ((n node) (r '()))
  606. (if (node? n)
  607. (loop (node-parent n) (cons (node-simplified? n) r))
  608. (reverse r))))
  609. (define (show-flag node)
  610. (let loop ((n node) (r '()))
  611. (if (node? n)
  612. (loop (node-parent n) (cons (node-flag n) r))
  613. (reverse r))))
  614. (define (reset-simplified node)
  615. (let loop ((n node))
  616. (cond ((node? n)
  617. (set-node-simplified?! n #f)
  618. (loop (node-parent n))))))