node-util.scm 25 KB

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