tree-il.scm 24 KB

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