tree-il.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. ;;;; Copyright (C) 2009-2014, 2017-2020, 2022 Free Software Foundation, Inc.
  2. ;;;;
  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. ;;;;
  17. (define-module (language tree-il)
  18. #:use-module (srfi srfi-1)
  19. #:use-module (srfi srfi-11)
  20. #:use-module (ice-9 match)
  21. #:use-module (system base syntax)
  22. #:export ((tree-il-src/ensure-alist . tree-il-src)
  23. <void> void? make-void void-src
  24. <const> const? make-const const-src const-exp
  25. <primitive-ref> primitive-ref? make-primitive-ref primitive-ref-src primitive-ref-name
  26. <lexical-ref> lexical-ref? make-lexical-ref lexical-ref-src lexical-ref-name lexical-ref-gensym
  27. <lexical-set> lexical-set? make-lexical-set lexical-set-src lexical-set-name lexical-set-gensym lexical-set-exp
  28. <module-ref> module-ref? make-module-ref module-ref-src module-ref-mod module-ref-name module-ref-public?
  29. <module-set> module-set? make-module-set module-set-src module-set-mod module-set-name module-set-public? module-set-exp
  30. <toplevel-ref> toplevel-ref? make-toplevel-ref toplevel-ref-src toplevel-ref-mod toplevel-ref-name
  31. <toplevel-set> toplevel-set? make-toplevel-set toplevel-set-src toplevel-set-mod toplevel-set-name toplevel-set-exp
  32. <toplevel-define> toplevel-define? make-toplevel-define toplevel-define-src toplevel-define-mod toplevel-define-name toplevel-define-exp
  33. <conditional> conditional? make-conditional conditional-src conditional-test conditional-consequent conditional-alternate
  34. <call> call? make-call call-src call-proc call-args
  35. <primcall> primcall? make-primcall primcall-src primcall-name primcall-args
  36. <seq> seq? make-seq seq-src seq-head seq-tail
  37. <lambda> lambda? make-lambda lambda-src lambda-meta lambda-body
  38. <lambda-case> lambda-case? make-lambda-case lambda-case-src
  39. ;; idea: arity
  40. lambda-case-req lambda-case-opt lambda-case-rest lambda-case-kw
  41. lambda-case-inits lambda-case-gensyms
  42. lambda-case-body lambda-case-alternate
  43. <let> let? make-let let-src let-names let-gensyms let-vals let-body
  44. <letrec> letrec? make-letrec letrec-src letrec-in-order? letrec-names letrec-gensyms letrec-vals letrec-body
  45. <fix> fix? make-fix fix-src fix-names fix-gensyms fix-vals fix-body
  46. <let-values> let-values? make-let-values let-values-src let-values-exp let-values-body
  47. <prompt> prompt? make-prompt prompt-src prompt-escape-only? prompt-tag prompt-body prompt-handler
  48. <abort> abort? make-abort abort-src abort-tag abort-args abort-tail
  49. list->seq
  50. parse-tree-il
  51. unparse-tree-il
  52. tree-il->scheme
  53. tree-il-fold
  54. make-tree-il-folder
  55. post-order
  56. pre-order
  57. with-lexicals
  58. tree-il=?
  59. tree-il-hash))
  60. (define (print-tree-il exp port)
  61. (format port "#<tree-il ~S>" (unparse-tree-il exp)))
  62. (define-syntax borrow-core-vtables
  63. (lambda (x)
  64. (syntax-case x ()
  65. ((_)
  66. (let lp ((n 0) (out '()))
  67. (if (< n (vector-length %expanded-vtables))
  68. (lp (1+ n)
  69. (let* ((vtable (vector-ref %expanded-vtables n))
  70. (stem (struct-ref vtable (+ vtable-offset-user 0)))
  71. (fields (struct-ref vtable (+ vtable-offset-user 2)))
  72. (sfields (map
  73. (lambda (f) (datum->syntax x f))
  74. fields))
  75. (type (datum->syntax x (symbol-append '< stem '>)))
  76. (ctor (datum->syntax x (symbol-append 'make- stem)))
  77. (pred (datum->syntax x (symbol-append stem '?))))
  78. (let lp ((n 0) (fields fields)
  79. (out (cons*
  80. #`(define (#,ctor #,@sfields)
  81. (make-struct/simple #,type #,@sfields))
  82. #`(define (#,pred x)
  83. (and (struct? x)
  84. (eq? (struct-vtable x) #,type)))
  85. #`(struct-set! #,type vtable-index-printer
  86. print-tree-il)
  87. #`(define #,type
  88. (vector-ref %expanded-vtables #,n))
  89. out)))
  90. (if (null? fields)
  91. out
  92. (lp (1+ n)
  93. (cdr fields)
  94. (let ((acc (datum->syntax
  95. x (symbol-append stem '- (car fields)))))
  96. (cons #`(define #,acc
  97. (make-procedure-with-setter
  98. (lambda (x) (struct-ref x #,n))
  99. (lambda (x v) (struct-set! x #,n v))))
  100. out)))))))
  101. #`(begin #,@(reverse out))))))))
  102. (borrow-core-vtables)
  103. ;; (<void>)
  104. ;; (<const> exp)
  105. ;; (<primitive-ref> name)
  106. ;; (<lexical-ref> name gensym)
  107. ;; (<lexical-set> name gensym exp)
  108. ;; (<module-ref> mod name public?)
  109. ;; (<module-set> mod name public? exp)
  110. ;; (<toplevel-ref> mod name)
  111. ;; (<toplevel-set> mod name exp)
  112. ;; (<toplevel-define> mod name exp)
  113. ;; (<conditional> test consequent alternate)
  114. ;; (<call> proc args)
  115. ;; (<primcall> name args)
  116. ;; (<seq> head tail)
  117. ;; (<lambda> meta body)
  118. ;; (<lambda-case> req opt rest kw inits gensyms body alternate)
  119. ;; (<let> names gensyms vals body)
  120. ;; (<letrec> in-order? names gensyms vals body)
  121. (define-type (<tree-il> #:common-slots (src) #:printer print-tree-il)
  122. (<fix> names gensyms vals body)
  123. (<let-values> exp body)
  124. (<prompt> escape-only? tag body handler)
  125. (<abort> tag args tail))
  126. (define tree-il-src/ensure-alist
  127. (make-procedure-with-setter
  128. (lambda (tree)
  129. "Return the source location of TREE as a source property alist."
  130. ;; psyntax gives us "source vectors"; convert them lazily to reduce
  131. ;; allocations.
  132. (match (tree-il-src tree)
  133. (#(file line column)
  134. `((filename . ,file) (line . ,line) (column . ,column)))
  135. (src
  136. src)))
  137. (lambda (tree src)
  138. (set! (tree-il-src tree) src))))
  139. ;; A helper.
  140. (define (list->seq loc exps)
  141. (match exps
  142. ((exp . exps)
  143. (let lp ((head exp) (tail exps))
  144. (match tail
  145. (() head)
  146. ((exp . tail) (lp (make-seq loc head exp) tail)))))))
  147. (define (location x)
  148. (and (pair? x)
  149. (let ((props (source-properties x)))
  150. (and (pair? props) props))))
  151. (define (parse-tree-il exp)
  152. (let ((loc (location exp))
  153. (retrans (lambda (x) (parse-tree-il x))))
  154. (match exp
  155. (('void)
  156. (make-void loc))
  157. (('call proc . args)
  158. (make-call loc (retrans proc) (map retrans args)))
  159. (('primcall name . args)
  160. (make-primcall loc name (map retrans args)))
  161. (('if test consequent alternate)
  162. (make-conditional loc (retrans test) (retrans consequent) (retrans alternate)))
  163. (('primitive (and name (? symbol?)))
  164. (make-primitive-ref loc name))
  165. (('lexical (and name (? symbol?)))
  166. (make-lexical-ref loc name name))
  167. (('lexical (and name (? symbol?)) (and sym (? symbol?)))
  168. (make-lexical-ref loc name sym))
  169. (('set! ('lexical (and name (? symbol?))) exp)
  170. (make-lexical-set loc name name (retrans exp)))
  171. (('set! ('lexical (and name (? symbol?)) (and sym (? symbol?))) exp)
  172. (make-lexical-set loc name sym (retrans exp)))
  173. (('@ ((and mod (? symbol?)) ...) (and name (? symbol?)))
  174. (make-module-ref loc mod name #t))
  175. (('set! ('@ ((and mod (? symbol?)) ...) (and name (? symbol?))) exp)
  176. (make-module-set loc mod name #t (retrans exp)))
  177. (('@@ ((and mod (? symbol?)) ...) (and name (? symbol?)))
  178. (make-module-ref loc mod name #f))
  179. (('set! ('@@ ((and mod (? symbol?)) ...) (and name (? symbol?))) exp)
  180. (make-module-set loc mod name #f (retrans exp)))
  181. (('toplevel (and name (? symbol?)))
  182. (make-toplevel-ref loc #f name))
  183. (('set! ('toplevel (and name (? symbol?))) exp)
  184. (make-toplevel-set loc #f name (retrans exp)))
  185. (('define (and name (? symbol?)) exp)
  186. (make-toplevel-define loc #f name (retrans exp)))
  187. (('lambda meta body)
  188. (make-lambda loc meta (retrans body)))
  189. (('lambda-case ((req opt rest kw inits gensyms) body) alternate)
  190. (make-lambda-case loc req opt rest kw
  191. (map retrans inits) gensyms
  192. (retrans body)
  193. (and=> alternate retrans)))
  194. (('lambda-case ((req opt rest kw inits gensyms) body))
  195. (make-lambda-case loc req opt rest kw
  196. (map retrans inits) gensyms
  197. (retrans body)
  198. #f))
  199. (('const exp)
  200. (make-const loc exp))
  201. (('seq head tail)
  202. (make-seq loc (retrans head) (retrans tail)))
  203. ;; Convenience.
  204. (('begin . exps)
  205. (list->seq loc (map retrans exps)))
  206. (('let names gensyms vals body)
  207. (make-let loc names gensyms (map retrans vals) (retrans body)))
  208. (('letrec names gensyms vals body)
  209. (make-letrec loc #f names gensyms (map retrans vals) (retrans body)))
  210. (('letrec* names gensyms vals body)
  211. (make-letrec loc #t names gensyms (map retrans vals) (retrans body)))
  212. (('fix names gensyms vals body)
  213. (make-fix loc names gensyms (map retrans vals) (retrans body)))
  214. (('let-values exp body)
  215. (make-let-values loc (retrans exp) (retrans body)))
  216. (('prompt escape-only? tag body handler)
  217. (make-prompt loc escape-only?
  218. (retrans tag) (retrans body) (retrans handler)))
  219. (('abort tag args tail)
  220. (make-abort loc (retrans tag) (map retrans args) (retrans tail)))
  221. (else
  222. (error "unrecognized tree-il" exp)))))
  223. (define (unparse-tree-il tree-il)
  224. (match tree-il
  225. (($ <void> src)
  226. '(void))
  227. (($ <call> src proc args)
  228. `(call ,(unparse-tree-il proc) ,@(map unparse-tree-il args)))
  229. (($ <primcall> src name args)
  230. `(primcall ,name ,@(map unparse-tree-il args)))
  231. (($ <conditional> src test consequent alternate)
  232. `(if ,(unparse-tree-il test)
  233. ,(unparse-tree-il consequent)
  234. ,(unparse-tree-il alternate)))
  235. (($ <primitive-ref> src name)
  236. `(primitive ,name))
  237. (($ <lexical-ref> src name gensym)
  238. `(lexical ,name ,gensym))
  239. (($ <lexical-set> src name gensym exp)
  240. `(set! (lexical ,name ,gensym) ,(unparse-tree-il exp)))
  241. (($ <module-ref> src mod name public?)
  242. `(,(if public? '@ '@@) ,mod ,name))
  243. (($ <module-set> src mod name public? exp)
  244. `(set! (,(if public? '@ '@@) ,mod ,name) ,(unparse-tree-il exp)))
  245. (($ <toplevel-ref> src mod name)
  246. `(toplevel ,name))
  247. (($ <toplevel-set> src mod name exp)
  248. `(set! (toplevel ,name) ,(unparse-tree-il exp)))
  249. (($ <toplevel-define> src mod name exp)
  250. `(define ,name ,(unparse-tree-il exp)))
  251. (($ <lambda> src meta body)
  252. (if body
  253. `(lambda ,meta ,(unparse-tree-il body))
  254. `(lambda ,meta (lambda-case))))
  255. (($ <lambda-case> src req opt rest kw inits gensyms body alternate)
  256. `(lambda-case ((,req ,opt ,rest ,kw ,(map unparse-tree-il inits) ,gensyms)
  257. ,(unparse-tree-il body))
  258. . ,(if alternate (list (unparse-tree-il alternate)) '())))
  259. (($ <const> src exp)
  260. `(const ,exp))
  261. (($ <seq> src head tail)
  262. `(seq ,(unparse-tree-il head) ,(unparse-tree-il tail)))
  263. (($ <let> src names gensyms vals body)
  264. `(let ,names ,gensyms ,(map unparse-tree-il vals) ,(unparse-tree-il body)))
  265. (($ <letrec> src in-order? names gensyms vals body)
  266. `(,(if in-order? 'letrec* 'letrec) ,names ,gensyms
  267. ,(map unparse-tree-il vals) ,(unparse-tree-il body)))
  268. (($ <fix> src names gensyms vals body)
  269. `(fix ,names ,gensyms ,(map unparse-tree-il vals) ,(unparse-tree-il body)))
  270. (($ <let-values> src exp body)
  271. `(let-values ,(unparse-tree-il exp) ,(unparse-tree-il body)))
  272. (($ <prompt> src escape-only? tag body handler)
  273. `(prompt ,escape-only?
  274. ,(unparse-tree-il tag)
  275. ,(unparse-tree-il body)
  276. ,(unparse-tree-il handler)))
  277. (($ <abort> src tag args tail)
  278. `(abort ,(unparse-tree-il tag) ,(map unparse-tree-il args)
  279. ,(unparse-tree-il tail)))))
  280. (define* (tree-il->scheme e #:optional (env #f) (opts '()))
  281. (values ((@ (language scheme decompile-tree-il)
  282. decompile-tree-il)
  283. e env opts)))
  284. (define-syntax-rule (make-tree-il-folder seed ...)
  285. (lambda (tree down up seed ...)
  286. (define (fold-values proc exps seed ...)
  287. (if (null? exps)
  288. (values seed ...)
  289. (let-values (((seed ...) (proc (car exps) seed ...)))
  290. (fold-values proc (cdr exps) seed ...))))
  291. (let foldts ((tree tree) (seed seed) ...)
  292. (let*-values
  293. (((seed ...) (down tree seed ...))
  294. ((seed ...)
  295. (match tree
  296. (($ <lexical-set> src name gensym exp)
  297. (foldts exp seed ...))
  298. (($ <module-set> src mod name public? exp)
  299. (foldts exp seed ...))
  300. (($ <toplevel-set> src mod name exp)
  301. (foldts exp seed ...))
  302. (($ <toplevel-define> src mod name exp)
  303. (foldts exp seed ...))
  304. (($ <conditional> src test consequent alternate)
  305. (let*-values (((seed ...) (foldts test seed ...))
  306. ((seed ...) (foldts consequent seed ...)))
  307. (foldts alternate seed ...)))
  308. (($ <call> src proc args)
  309. (let-values (((seed ...) (foldts proc seed ...)))
  310. (fold-values foldts args seed ...)))
  311. (($ <primcall> src name args)
  312. (fold-values foldts args seed ...))
  313. (($ <seq> src head tail)
  314. (let-values (((seed ...) (foldts head seed ...)))
  315. (foldts tail seed ...)))
  316. (($ <lambda> src meta body)
  317. (if body
  318. (foldts body seed ...)
  319. (values seed ...)))
  320. (($ <lambda-case> src req opt rest kw inits gensyms body
  321. alternate)
  322. (let-values (((seed ...) (fold-values foldts inits seed ...)))
  323. (if alternate
  324. (let-values (((seed ...) (foldts body seed ...)))
  325. (foldts alternate seed ...))
  326. (foldts body seed ...))))
  327. (($ <let> src names gensyms vals body)
  328. (let*-values (((seed ...) (fold-values foldts vals seed ...)))
  329. (foldts body seed ...)))
  330. (($ <letrec> src in-order? names gensyms vals body)
  331. (let*-values (((seed ...) (fold-values foldts vals seed ...)))
  332. (foldts body seed ...)))
  333. (($ <fix> src names gensyms vals body)
  334. (let*-values (((seed ...) (fold-values foldts vals seed ...)))
  335. (foldts body seed ...)))
  336. (($ <let-values> src exp body)
  337. (let*-values (((seed ...) (foldts exp seed ...)))
  338. (foldts body seed ...)))
  339. (($ <prompt> src escape-only? tag body handler)
  340. (let*-values (((seed ...) (foldts tag seed ...))
  341. ((seed ...) (foldts body seed ...)))
  342. (foldts handler seed ...)))
  343. (($ <abort> src tag args tail)
  344. (let*-values (((seed ...) (foldts tag seed ...))
  345. ((seed ...) (fold-values foldts args seed ...)))
  346. (foldts tail seed ...)))
  347. (_
  348. (values seed ...)))))
  349. (up tree seed ...)))))
  350. (define (tree-il-fold down up seed tree)
  351. "Traverse TREE, calling DOWN before visiting a sub-tree, and UP when
  352. after visiting it. Each of these procedures is invoked as `(PROC TREE
  353. SEED)', where TREE is the sub-tree considered and SEED is the current
  354. result, intially seeded with SEED.
  355. This is an implementation of `foldts' as described by Andy Wingo in
  356. ``Applications of fold to XML transformation''."
  357. ;; Multi-valued fold naturally puts the seeds at the end, whereas
  358. ;; normal fold puts the traversable at the end. Adapt to the expected
  359. ;; argument order.
  360. ((make-tree-il-folder tree) tree down up seed))
  361. (define (pre-post-order pre post x)
  362. (define (elts-eq? a b)
  363. (or (null? a)
  364. (and (eq? (car a) (car b))
  365. (elts-eq? (cdr a) (cdr b)))))
  366. (let lp ((x x))
  367. (post
  368. (let ((x (pre x)))
  369. (match x
  370. ((or ($ <void>)
  371. ($ <const>)
  372. ($ <primitive-ref>)
  373. ($ <lexical-ref>)
  374. ($ <module-ref>)
  375. ($ <toplevel-ref>))
  376. x)
  377. (($ <lexical-set> src name gensym exp)
  378. (let ((exp* (lp exp)))
  379. (if (eq? exp exp*)
  380. x
  381. (make-lexical-set src name gensym exp*))))
  382. (($ <module-set> src mod name public? exp)
  383. (let ((exp* (lp exp)))
  384. (if (eq? exp exp*)
  385. x
  386. (make-module-set src mod name public? exp*))))
  387. (($ <toplevel-set> src mod name exp)
  388. (let ((exp* (lp exp)))
  389. (if (eq? exp exp*)
  390. x
  391. (make-toplevel-set src mod name exp*))))
  392. (($ <toplevel-define> src mod name exp)
  393. (let ((exp* (lp exp)))
  394. (if (eq? exp exp*)
  395. x
  396. (make-toplevel-define src mod name exp*))))
  397. (($ <conditional> src test consequent alternate)
  398. (let ((test* (lp test))
  399. (consequent* (lp consequent))
  400. (alternate* (lp alternate)))
  401. (if (and (eq? test test*)
  402. (eq? consequent consequent*)
  403. (eq? alternate alternate*))
  404. x
  405. (make-conditional src test* consequent* alternate*))))
  406. (($ <call> src proc args)
  407. (let ((proc* (lp proc))
  408. (args* (map lp args)))
  409. (if (and (eq? proc proc*)
  410. (elts-eq? args args*))
  411. x
  412. (make-call src proc* args*))))
  413. (($ <primcall> src name args)
  414. (let ((args* (map lp args)))
  415. (if (elts-eq? args args*)
  416. x
  417. (make-primcall src name args*))))
  418. (($ <seq> src head tail)
  419. (let ((head* (lp head))
  420. (tail* (lp tail)))
  421. (if (and (eq? head head*)
  422. (eq? tail tail*))
  423. x
  424. (make-seq src head* tail*))))
  425. (($ <lambda> src meta body)
  426. (let ((body* (and body (lp body))))
  427. (if (eq? body body*)
  428. x
  429. (make-lambda src meta body*))))
  430. (($ <lambda-case> src req opt rest kw inits gensyms body alternate)
  431. (let ((inits* (map lp inits))
  432. (body* (lp body))
  433. (alternate* (and alternate (lp alternate))))
  434. (if (and (elts-eq? inits inits*)
  435. (eq? body body*)
  436. (eq? alternate alternate*))
  437. x
  438. (make-lambda-case src req opt rest kw inits* gensyms body*
  439. alternate*))))
  440. (($ <let> src names gensyms vals body)
  441. (let ((vals* (map lp vals))
  442. (body* (lp body)))
  443. (if (and (elts-eq? vals vals*)
  444. (eq? body body*))
  445. x
  446. (make-let src names gensyms vals* body*))))
  447. (($ <letrec> src in-order? names gensyms vals body)
  448. (let ((vals* (map lp vals))
  449. (body* (lp body)))
  450. (if (and (elts-eq? vals vals*)
  451. (eq? body body*))
  452. x
  453. (make-letrec src in-order? names gensyms vals* body*))))
  454. (($ <fix> src names gensyms vals body)
  455. (let ((vals* (map lp vals))
  456. (body* (lp body)))
  457. (if (and (elts-eq? vals vals*)
  458. (eq? body body*))
  459. x
  460. (make-fix src names gensyms vals* body*))))
  461. (($ <let-values> src exp body)
  462. (let ((exp* (lp exp))
  463. (body* (lp body)))
  464. (if (and (eq? exp exp*)
  465. (eq? body body*))
  466. x
  467. (make-let-values src exp* body*))))
  468. (($ <prompt> src escape-only? tag body handler)
  469. (let ((tag* (lp tag))
  470. (body* (lp body))
  471. (handler* (lp handler)))
  472. (if (and (eq? tag tag*)
  473. (eq? body body*)
  474. (eq? handler handler*))
  475. x
  476. (make-prompt src escape-only? tag* body* handler*))))
  477. (($ <abort> src tag args tail)
  478. (let ((tag* (lp tag))
  479. (args* (map lp args))
  480. (tail* (lp tail)))
  481. (if (and (eq? tag tag*)
  482. (elts-eq? args args*)
  483. (eq? tail tail*))
  484. x
  485. (make-abort src tag* args* tail*)))))))))
  486. (define (post-order f x)
  487. (pre-post-order (lambda (x) x) f x))
  488. (define (pre-order f x)
  489. (pre-post-order f (lambda (x) x) x))
  490. (define-syntax-rule (with-lexical src id . body)
  491. (let ((k (lambda (id) . body)))
  492. (match id
  493. (($ <lexical-ref>) (k id))
  494. (_
  495. (let ((tmp (gensym "v ")))
  496. (make-let src (list 'id) (list tmp) (list id)
  497. (k (make-lexical-ref src 'id tmp))))))))
  498. (define-syntax with-lexicals
  499. (syntax-rules ()
  500. ((with-lexicals src () . body) (let () . body))
  501. ((with-lexicals src (id . ids) . body)
  502. (with-lexical src id (with-lexicals src ids . body)))))
  503. ;; FIXME: We should have a better primitive than this.
  504. (define (struct-nfields x)
  505. (/ (string-length (symbol->string (struct-layout x))) 2))
  506. (define (tree-il=? a b)
  507. (cond
  508. ((struct? a)
  509. (and (struct? b)
  510. (eq? (struct-vtable a) (struct-vtable b))
  511. ;; Assume that all structs are tree-il, so we skip over the
  512. ;; src slot.
  513. (let lp ((n (1- (struct-nfields a))))
  514. (or (zero? n)
  515. (and (tree-il=? (struct-ref a n) (struct-ref b n))
  516. (lp (1- n)))))))
  517. ((pair? a)
  518. (and (pair? b)
  519. (tree-il=? (car a) (car b))
  520. (tree-il=? (cdr a) (cdr b))))
  521. (else
  522. (equal? a b))))
  523. (define-syntax hash-bits
  524. (make-variable-transformer
  525. (lambda (x)
  526. (syntax-case x ()
  527. (var
  528. (identifier? #'var)
  529. (logcount most-positive-fixnum))))))
  530. (define (tree-il-hash exp)
  531. (let ((hash-depth 4)
  532. (hash-width 3))
  533. (define (hash-exp exp depth)
  534. (define (rotate x bits)
  535. (logior (ash x (- bits))
  536. (ash (logand x (1- (ash 1 bits))) (- hash-bits bits))))
  537. (define (mix h1 h2)
  538. (logxor h1 (rotate h2 8)))
  539. (define (hash-struct s)
  540. (let ((len (struct-nfields s))
  541. (h (hashq (struct-vtable s) most-positive-fixnum)))
  542. (if (zero? depth)
  543. h
  544. (let lp ((i (max (- len hash-width) 1)) (h h))
  545. (if (< i len)
  546. (lp (1+ i) (mix (hash-exp (struct-ref s i) (1+ depth)) h))
  547. h)))))
  548. (define (hash-list l)
  549. (let ((h (hashq 'list most-positive-fixnum)))
  550. (if (zero? depth)
  551. h
  552. (let lp ((l l) (width 0) (h h))
  553. (if (< width hash-width)
  554. (lp (cdr l) (1+ width)
  555. (mix (hash-exp (car l) (1+ depth)) h))
  556. h)))))
  557. (cond
  558. ((struct? exp) (hash-struct exp))
  559. ((list? exp) (hash-list exp))
  560. (else (hash exp most-positive-fixnum))))
  561. (hash-exp exp 0)))