peval.scm 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. ;;; Tree-IL partial evaluator
  2. ;; Copyright (C) 2011-2014, 2017, 2019, 2020, 2021, 2022 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. (define-module (language tree-il peval)
  17. #:use-module (language tree-il)
  18. #:use-module (language tree-il primitives)
  19. #:use-module (language tree-il effects)
  20. #:use-module (ice-9 vlist)
  21. #:use-module (ice-9 match)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-9)
  24. #:use-module (srfi srfi-11)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (system base target)
  27. #:use-module (ice-9 control)
  28. #:export (peval))
  29. ;;;
  30. ;;; Partial evaluation is Guile's most important source-to-source
  31. ;;; optimization pass. It performs copy propagation, dead code
  32. ;;; elimination, inlining, and constant folding, all while preserving
  33. ;;; the order of effects in the residual program.
  34. ;;;
  35. ;;; For more on partial evaluation, see William Cook’s excellent
  36. ;;; tutorial on partial evaluation at DSL 2011, called “Build your own
  37. ;;; partial evaluator in 90 minutes”[0].
  38. ;;;
  39. ;;; Our implementation of this algorithm was heavily influenced by
  40. ;;; Waddell and Dybvig's paper, "Fast and Effective Procedure Inlining",
  41. ;;; IU CS Dept. TR 484.
  42. ;;;
  43. ;;; [0] http://www.cs.utexas.edu/~wcook/tutorial/.
  44. ;;;
  45. ;; First, some helpers.
  46. ;;
  47. (define-syntax *logging* (identifier-syntax #f))
  48. ;; For efficiency we define *logging* to inline to #f, so that the call
  49. ;; to log* gets optimized out. If you want to log, uncomment these
  50. ;; lines:
  51. ;;
  52. ;; (define %logging #f)
  53. ;; (define-syntax *logging* (identifier-syntax %logging))
  54. ;;
  55. ;; Then you can change %logging at runtime.
  56. (define-syntax log
  57. (syntax-rules (quote)
  58. ((log 'event arg ...)
  59. (if (and *logging*
  60. (or (eq? *logging* #t)
  61. (memq 'event *logging*)))
  62. (log* 'event arg ...)))))
  63. (define (log* event . args)
  64. (let ((pp (module-ref (resolve-interface '(ice-9 pretty-print))
  65. 'pretty-print)))
  66. (pp `(log ,event . ,args))
  67. (newline)
  68. (values)))
  69. (define (tree-il-any proc exp)
  70. (let/ec k
  71. (tree-il-fold (lambda (exp res)
  72. (let ((res (proc exp)))
  73. (if res (k res) #f)))
  74. (lambda (exp res) #f)
  75. #f exp)))
  76. (define (vlist-any proc vlist)
  77. (let ((len (vlist-length vlist)))
  78. (let lp ((i 0))
  79. (and (< i len)
  80. (or (proc (vlist-ref vlist i))
  81. (lp (1+ i)))))))
  82. (define (singly-valued-expression? exp)
  83. (match exp
  84. (($ <const>) #t)
  85. (($ <void>) #t)
  86. (($ <lexical-ref>) #t)
  87. (($ <primitive-ref>) #t)
  88. (($ <module-ref>) #t)
  89. (($ <toplevel-ref>) #t)
  90. (($ <primcall> _ (? singly-valued-primitive?)) #t)
  91. (($ <primcall> _ 'values (val)) #t)
  92. (($ <lambda>) #t)
  93. (($ <conditional> _ test consequent alternate)
  94. (and (singly-valued-expression? consequent)
  95. (singly-valued-expression? alternate)))
  96. (else #f)))
  97. (define (truncate-values x)
  98. "Discard all but the first value of X."
  99. (if (singly-valued-expression? x)
  100. x
  101. (make-primcall (tree-il-src x) 'values (list x))))
  102. ;; Peval will do a one-pass analysis on the source program to determine
  103. ;; the set of assigned lexicals, and to identify unreferenced and
  104. ;; singly-referenced lexicals.
  105. ;;
  106. (define-record-type <var>
  107. (make-var name gensym refcount set?)
  108. var?
  109. (name var-name)
  110. (gensym var-gensym)
  111. (refcount var-refcount set-var-refcount!)
  112. (set? var-set? set-var-set?!))
  113. (define* (build-var-table exp #:optional (table vlist-null))
  114. (tree-il-fold
  115. (lambda (exp res)
  116. (match exp
  117. (($ <lexical-ref> src name gensym)
  118. (let ((var (cdr (vhash-assq gensym res))))
  119. (set-var-refcount! var (1+ (var-refcount var)))
  120. res))
  121. (($ <lambda-case> src req opt rest kw init gensyms body alt)
  122. (fold (lambda (name sym res)
  123. (vhash-consq sym (make-var name sym 0 #f) res))
  124. res
  125. (append req (or opt '()) (if rest (list rest) '())
  126. (match kw
  127. ((aok? (kw name sym) ...) name)
  128. (_ '())))
  129. gensyms))
  130. (($ <let> src names gensyms vals body)
  131. (fold (lambda (name sym res)
  132. (vhash-consq sym (make-var name sym 0 #f) res))
  133. res names gensyms))
  134. (($ <letrec>)
  135. (error "unexpected letrec"))
  136. (($ <fix> src names gensyms vals body)
  137. (fold (lambda (name sym res)
  138. (vhash-consq sym (make-var name sym 0 #f) res))
  139. res names gensyms))
  140. (($ <lexical-set> src name gensym exp)
  141. (set-var-set?! (cdr (vhash-assq gensym res)) #t)
  142. res)
  143. (_ res)))
  144. (lambda (exp res) res)
  145. table exp))
  146. (define (augment-var-table-with-externally-introduced-lexicals exp table)
  147. "Take the previously computed var table TABLE and the term EXP and
  148. return a table augmented with the lexicals bound in EXP which are not
  149. present in TABLE. This is used for the result of `expand-primcalls`,
  150. which may introduce new lexicals if a subexpression needs to be
  151. referenced multiple times."
  152. (define (maybe-add-var name sym table)
  153. ;; Use a refcount of 2 to prevent the copy-single optimization.
  154. (define refcount 2)
  155. (define assigned? #f)
  156. (if (vhash-assq sym table)
  157. table
  158. (vhash-consq sym (make-var name sym refcount assigned?) table)))
  159. (tree-il-fold
  160. (lambda (exp table)
  161. (match exp
  162. (($ <lambda-case> src req opt rest kw init gensyms body alt)
  163. (fold maybe-add-var table
  164. (append req (or opt '()) (if rest (list rest) '())
  165. (match kw
  166. ((aok? (kw name sym) ...) name)
  167. (_ '())))
  168. gensyms))
  169. (($ <let> src names gensyms vals body)
  170. (fold maybe-add-var table names gensyms))
  171. (($ <letrec>)
  172. (error "unexpected letrec"))
  173. (($ <fix> src names gensyms vals body)
  174. (fold maybe-add-var table names gensyms))
  175. (_ table)))
  176. (lambda (exp table) table)
  177. table exp))
  178. ;; Counters are data structures used to limit the effort that peval
  179. ;; spends on particular inlining attempts. Each call site in the source
  180. ;; program is allocated some amount of effort. If peval exceeds the
  181. ;; effort counter while attempting to inline a call site, it aborts the
  182. ;; inlining attempt and residualizes a call instead.
  183. ;;
  184. ;; As there is a fixed number of call sites, that makes `peval' O(N) in
  185. ;; the number of call sites in the source program.
  186. ;;
  187. ;; Counters should limit the size of the residual program as well, but
  188. ;; currently this is not implemented.
  189. ;;
  190. ;; At the top level, before seeing any peval call, there is no counter,
  191. ;; because inlining will terminate as there is no recursion. When peval
  192. ;; sees a call at the top level, it will make a new counter, allocating
  193. ;; it some amount of effort and size.
  194. ;;
  195. ;; This top-level effort counter effectively "prints money". Within a
  196. ;; toplevel counter, no more effort is printed ex nihilo; for a nested
  197. ;; inlining attempt to proceed, effort must be transferred from the
  198. ;; toplevel counter to the nested counter.
  199. ;;
  200. ;; Via `data' and `prev', counters form a linked list, terminating in a
  201. ;; toplevel counter. In practice `data' will be the a pointer to the
  202. ;; source expression of the procedure being inlined.
  203. ;;
  204. ;; In this way peval can detect a recursive inlining attempt, by walking
  205. ;; back on the `prev' links looking for matching `data'. Recursive
  206. ;; counters receive a more limited effort allocation, as we don't want
  207. ;; to spend all of the effort for a toplevel inlining site on loops.
  208. ;; Also, recursive counters don't need a prompt at each inlining site:
  209. ;; either the call chain folds entirely, or it will be residualized at
  210. ;; its original call.
  211. ;;
  212. (define-record-type <counter>
  213. (%make-counter effort size continuation recursive? data prev)
  214. counter?
  215. (effort effort-counter)
  216. (size size-counter)
  217. (continuation counter-continuation)
  218. (recursive? counter-recursive? set-counter-recursive?!)
  219. (data counter-data)
  220. (prev counter-prev))
  221. (define (abort-counter c)
  222. ((counter-continuation c)))
  223. (define (record-effort! c)
  224. (let ((e (effort-counter c)))
  225. (if (zero? (variable-ref e))
  226. (abort-counter c)
  227. (variable-set! e (1- (variable-ref e))))))
  228. (define (record-size! c)
  229. (let ((s (size-counter c)))
  230. (if (zero? (variable-ref s))
  231. (abort-counter c)
  232. (variable-set! s (1- (variable-ref s))))))
  233. (define (find-counter data counter)
  234. (and counter
  235. (if (eq? data (counter-data counter))
  236. counter
  237. (find-counter data (counter-prev counter)))))
  238. (define* (transfer! from to #:optional
  239. (effort (variable-ref (effort-counter from)))
  240. (size (variable-ref (size-counter from))))
  241. (define (transfer-counter! from-v to-v amount)
  242. (let* ((from-balance (variable-ref from-v))
  243. (to-balance (variable-ref to-v))
  244. (amount (min amount from-balance)))
  245. (variable-set! from-v (- from-balance amount))
  246. (variable-set! to-v (+ to-balance amount))))
  247. (transfer-counter! (effort-counter from) (effort-counter to) effort)
  248. (transfer-counter! (size-counter from) (size-counter to) size))
  249. (define (make-top-counter effort-limit size-limit continuation data)
  250. (%make-counter (make-variable effort-limit)
  251. (make-variable size-limit)
  252. continuation
  253. #t
  254. data
  255. #f))
  256. (define (make-nested-counter continuation data current)
  257. (let ((c (%make-counter (make-variable 0)
  258. (make-variable 0)
  259. continuation
  260. #f
  261. data
  262. current)))
  263. (transfer! current c)
  264. c))
  265. (define (make-recursive-counter effort-limit size-limit orig current)
  266. (let ((c (%make-counter (make-variable 0)
  267. (make-variable 0)
  268. (counter-continuation orig)
  269. #t
  270. (counter-data orig)
  271. current)))
  272. (transfer! current c effort-limit size-limit)
  273. c))
  274. ;; Operand structures allow bindings to be processed lazily instead of
  275. ;; eagerly. By doing so, hopefully we can get process them in a way
  276. ;; appropriate to their use contexts. Operands also prevent values from
  277. ;; being visited multiple times, wasting effort.
  278. ;;
  279. ;; TODO: Record value size in operand structure?
  280. ;;
  281. (define-record-type <operand>
  282. (%make-operand var sym visit source visit-count use-count
  283. copyable? residual-value constant-value alias)
  284. operand?
  285. (var operand-var)
  286. (sym operand-sym)
  287. (visit %operand-visit)
  288. (source operand-source)
  289. (visit-count operand-visit-count set-operand-visit-count!)
  290. (use-count operand-use-count set-operand-use-count!)
  291. (copyable? operand-copyable? set-operand-copyable?!)
  292. (residual-value operand-residual-value %set-operand-residual-value!)
  293. (constant-value operand-constant-value set-operand-constant-value!)
  294. (alias operand-alias set-operand-alias!))
  295. (define* (make-operand var sym #:optional source visit alias)
  296. ;; Bind SYM to VAR, with value SOURCE. Unassigned bound operands are
  297. ;; considered copyable until we prove otherwise. If we have a source
  298. ;; expression, truncate it to one value. Copy propagation does not
  299. ;; work on multiply-valued expressions.
  300. (let ((source (and=> source truncate-values)))
  301. (%make-operand var sym visit source 0 0
  302. (and source (not (var-set? var))) #f #f
  303. (and (not (var-set? var)) alias))))
  304. (define* (make-bound-operands vars syms sources visit #:optional aliases)
  305. (if aliases
  306. (map (lambda (name sym source alias)
  307. (make-operand name sym source visit alias))
  308. vars syms sources aliases)
  309. (map (lambda (name sym source)
  310. (make-operand name sym source visit #f))
  311. vars syms sources)))
  312. (define (make-unbound-operands vars syms)
  313. (map make-operand vars syms))
  314. (define (set-operand-residual-value! op val)
  315. (%set-operand-residual-value!
  316. op
  317. (match val
  318. (($ <primcall> src 'values (first))
  319. ;; The continuation of a residualized binding does not need the
  320. ;; introduced `values' node, so undo the effects of truncation.
  321. first)
  322. (else
  323. val))))
  324. (define* (visit-operand op counter ctx #:optional effort-limit size-limit)
  325. ;; Peval is O(N) in call sites of the source program. However,
  326. ;; visiting an operand can introduce new call sites. If we visit an
  327. ;; operand outside a counter -- i.e., outside an inlining attempt --
  328. ;; this can lead to divergence. So, if we are visiting an operand to
  329. ;; try to copy it, and there is no counter, make a new one.
  330. ;;
  331. ;; This will only happen at most as many times as there are lexical
  332. ;; references in the source program.
  333. (and (zero? (operand-visit-count op))
  334. (dynamic-wind
  335. (lambda ()
  336. (set-operand-visit-count! op (1+ (operand-visit-count op))))
  337. (lambda ()
  338. (and (operand-source op)
  339. (if (or counter (and (not effort-limit) (not size-limit)))
  340. ((%operand-visit op) (operand-source op) counter ctx)
  341. (let/ec k
  342. (define (abort)
  343. ;; If we abort when visiting the value in a
  344. ;; fresh context, we won't succeed in any future
  345. ;; attempt, so don't try to copy it again.
  346. (set-operand-copyable?! op #f)
  347. (k #f))
  348. ((%operand-visit op)
  349. (operand-source op)
  350. (make-top-counter effort-limit size-limit abort op)
  351. ctx)))))
  352. (lambda ()
  353. (set-operand-visit-count! op (1- (operand-visit-count op)))))))
  354. ;; A helper for constant folding.
  355. ;;
  356. (define (types-check? primitive-name args)
  357. (case primitive-name
  358. ((values) #t)
  359. ((not pair? null? list? symbol? vector? struct?)
  360. (= (length args) 1))
  361. ((eq? eqv? equal?)
  362. (= (length args) 2))
  363. ;; FIXME: add more cases?
  364. (else #f)))
  365. (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
  366. #:key
  367. (operator-size-limit 40)
  368. (operand-size-limit 20)
  369. (value-size-limit 10)
  370. (effort-limit 500)
  371. (recursive-effort-limit 100)
  372. (cross-module-inlining? #f))
  373. "Partially evaluate EXP in compilation environment CENV, with
  374. top-level bindings from ENV and return the resulting expression."
  375. ;; This is a simple partial evaluator. It effectively performs
  376. ;; constant folding, copy propagation, dead code elimination, and
  377. ;; inlining.
  378. ;; TODO:
  379. ;;
  380. ;; Propagate copies across toplevel bindings, if we can prove the
  381. ;; bindings to be immutable.
  382. ;;
  383. ;; Specialize lambda expressions with invariant arguments.
  384. (define local-toplevel-env
  385. ;; The top-level environment of the module being compiled.
  386. (let ()
  387. (define (env-folder x env)
  388. (match x
  389. (($ <toplevel-define> _ _ name)
  390. (vhash-consq name #t env))
  391. (($ <seq> _ head tail)
  392. (env-folder tail (env-folder head env)))
  393. (_ env)))
  394. (env-folder exp vlist-null)))
  395. (define (local-toplevel? name)
  396. (vhash-assq name local-toplevel-env))
  397. ;; gensym -> <var>
  398. ;; renamed-term -> original-term
  399. ;;
  400. (define store (build-var-table exp))
  401. (define (record-new-temporary! name sym refcount)
  402. (set! store (vhash-consq sym (make-var name sym refcount #f) store)))
  403. (define (lookup-var sym)
  404. (let ((v (vhash-assq sym store)))
  405. (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
  406. (define (fresh-gensyms vars)
  407. (map (lambda (var)
  408. (let ((new (gensym (string-append (symbol->string (var-name var))
  409. " "))))
  410. (set! store (vhash-consq new var store))
  411. new))
  412. vars))
  413. (define (fresh-temporaries ls)
  414. (map (lambda (elt)
  415. (let ((new (gensym "tmp ")))
  416. (record-new-temporary! 'tmp new 1)
  417. new))
  418. ls))
  419. (define (assigned-lexical? sym)
  420. (var-set? (lookup-var sym)))
  421. (define (lexical-refcount sym)
  422. (var-refcount (lookup-var sym)))
  423. (define (splice-expression exp)
  424. (define vars (make-hash-table))
  425. (define (rename! old*)
  426. (match old*
  427. (() '())
  428. ((old . old*)
  429. (cons (let ((new (gensym "t")))
  430. (hashq-set! vars old new)
  431. new)
  432. (rename! old*)))))
  433. (define (new-name old) (hashq-ref vars old))
  434. (define renamed
  435. (pre-order
  436. (match-lambda
  437. (($ <lexical-ref> src name gensym)
  438. (make-lexical-ref src name (new-name gensym)))
  439. (($ <lexical-set> src name gensym exp)
  440. (make-lexical-set src name (new-name gensym) exp))
  441. (($ <lambda-case> src req opt rest kw init gensyms body alt)
  442. (let ((gensyms (rename! gensyms)))
  443. (make-lambda-case src req opt rest
  444. (match kw
  445. ((aok? (kw name sym) ...)
  446. (cons aok?
  447. (map (lambda (kw name sym)
  448. (list kw name (new-name sym)))
  449. kw name sym)))
  450. (#f #f))
  451. init gensyms body alt)))
  452. (($ <let> src names gensyms vals body)
  453. (make-let src names (rename! gensyms) vals body))
  454. (($ <letrec>)
  455. (error "unexpected letrec"))
  456. (($ <fix> src names gensyms vals body)
  457. (make-fix src names (rename! gensyms) vals body))
  458. (exp exp))
  459. exp))
  460. (set! store (build-var-table renamed store))
  461. renamed)
  462. (define (with-temporaries src exps refcount can-copy? k)
  463. (let* ((pairs (map (match-lambda
  464. ((and exp (? can-copy?))
  465. (cons #f exp))
  466. (exp
  467. (let ((sym (gensym "tmp ")))
  468. (record-new-temporary! 'tmp sym refcount)
  469. (cons sym exp))))
  470. exps))
  471. (tmps (filter car pairs)))
  472. (match tmps
  473. (() (k exps))
  474. (tmps
  475. (make-let src
  476. (make-list (length tmps) 'tmp)
  477. (map car tmps)
  478. (map cdr tmps)
  479. (k (map (match-lambda
  480. ((#f . val) val)
  481. ((sym . _)
  482. (make-lexical-ref #f 'tmp sym)))
  483. pairs)))))))
  484. (define (make-begin0 src first second)
  485. (make-let-values
  486. src
  487. first
  488. (let ((vals (gensym "vals ")))
  489. (record-new-temporary! 'vals vals 1)
  490. (make-lambda-case
  491. #f
  492. '() #f 'vals #f '() (list vals)
  493. (make-seq
  494. src
  495. second
  496. (make-primcall #f 'apply
  497. (list
  498. (make-primitive-ref #f 'values)
  499. (make-lexical-ref #f 'vals vals))))
  500. #f))))
  501. ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
  502. ;; from it to ORIG.
  503. ;;
  504. (define (record-source-expression! orig new)
  505. (set! store (vhash-consq new (source-expression orig) store))
  506. new)
  507. ;; Find the source expression corresponding to NEW. Used to detect
  508. ;; recursive inlining attempts.
  509. ;;
  510. (define (source-expression new)
  511. (let ((x (vhash-assq new store)))
  512. (if x (cdr x) new)))
  513. (define (record-operand-use op)
  514. (set-operand-use-count! op (1+ (operand-use-count op))))
  515. (define (unrecord-operand-uses op n)
  516. (let ((count (- (operand-use-count op) n)))
  517. (when (zero? count)
  518. (set-operand-residual-value! op #f))
  519. (set-operand-use-count! op count)))
  520. (define* (residualize-lexical op #:optional ctx val)
  521. (log 'residualize op)
  522. (record-operand-use op)
  523. (if (memq ctx '(value values))
  524. (set-operand-residual-value! op val))
  525. (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
  526. (define (fold-constants src name args ctx)
  527. (define (apply-primitive name args)
  528. ;; todo: further optimize commutative primitives
  529. (catch #t
  530. (lambda ()
  531. (call-with-values
  532. (lambda ()
  533. (apply (module-ref the-scm-module name) args))
  534. (lambda results
  535. (values #t results))))
  536. (lambda _
  537. (values #f '()))))
  538. (define (make-values src values)
  539. (match values
  540. ((single) single) ; 1 value
  541. ((_ ...) ; 0, or 2 or more values
  542. (make-primcall src 'values values))))
  543. (define (residualize-call)
  544. (make-primcall src name args))
  545. (cond
  546. ((every const? args)
  547. (let-values (((success? values)
  548. (apply-primitive name (map const-exp args))))
  549. (log 'fold success? values name args)
  550. (if success?
  551. (case ctx
  552. ((effect) (make-void src))
  553. ((test)
  554. ;; Values truncation: only take the first
  555. ;; value.
  556. (if (pair? values)
  557. (make-const src (car values))
  558. (make-values src '())))
  559. (else
  560. (make-values src (map (cut make-const src <>) values))))
  561. (residualize-call))))
  562. ((and (eq? ctx 'effect) (types-check? name args))
  563. (make-void #f))
  564. (else
  565. (residualize-call))))
  566. (define (inline-values src exp nmin nmax consumer)
  567. (let loop ((exp exp))
  568. (match exp
  569. ;; Some expression types are always singly-valued.
  570. ((or ($ <const>)
  571. ($ <void>)
  572. ($ <lambda>)
  573. ($ <lexical-ref>)
  574. ($ <toplevel-ref>)
  575. ($ <module-ref>)
  576. ($ <primitive-ref>)
  577. ($ <lexical-set>) ; FIXME: these set! expressions
  578. ($ <toplevel-set>) ; could return zero values in
  579. ($ <toplevel-define>) ; the future
  580. ($ <module-set>) ;
  581. ($ <primcall> src (? singly-valued-primitive?)))
  582. (and (<= nmin 1) (or (not nmax) (>= nmax 1))
  583. (make-call src (make-lambda #f '() consumer) (list exp))))
  584. ;; Statically-known number of values.
  585. (($ <primcall> src 'values vals)
  586. (and (<= nmin (length vals)) (or (not nmax) (>= nmax (length vals)))
  587. (make-call src (make-lambda #f '() consumer) vals)))
  588. ;; Not going to copy code into both branches.
  589. (($ <conditional>) #f)
  590. ;; Bail on other applications.
  591. (($ <call>) #f)
  592. (($ <primcall>) #f)
  593. ;; Bail on prompt and abort.
  594. (($ <prompt>) #f)
  595. (($ <abort>) #f)
  596. ;; Propagate to tail positions.
  597. (($ <let> src names gensyms vals body)
  598. (let ((body (loop body)))
  599. (and body
  600. (make-let src names gensyms vals body))))
  601. (($ <fix> src names gensyms vals body)
  602. (let ((body (loop body)))
  603. (and body
  604. (make-fix src names gensyms vals body))))
  605. (($ <let-values> src exp
  606. ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
  607. (let ((body (loop body)))
  608. (and body
  609. (make-let-values src exp
  610. (make-lambda-case src2 req opt rest kw
  611. inits gensyms body #f)))))
  612. (($ <seq> src head tail)
  613. (let ((tail (loop tail)))
  614. (and tail (make-seq src head tail)))))))
  615. (define compute-effects
  616. (make-effects-analyzer assigned-lexical?))
  617. (define (constant-expression? x)
  618. ;; Return true if X is constant, for the purposes of copying or
  619. ;; elision---i.e., if it is known to have no effects, does not
  620. ;; allocate storage for a mutable object, and does not access
  621. ;; mutable data (like `car' or toplevel references).
  622. (constant? (compute-effects x)))
  623. (define (prune-bindings ops in-order? body counter ctx build-result)
  624. ;; This helper handles both `let' and `letrec'/`fix'. In the latter
  625. ;; cases we need to make sure that if referenced binding A needs
  626. ;; as-yet-unreferenced binding B, that B is processed for value.
  627. ;; Likewise if C, when processed for effect, needs otherwise
  628. ;; unreferenced D, then D needs to be processed for value too.
  629. ;;
  630. (define (referenced? op)
  631. ;; When we visit lambdas in operator context, we just copy them,
  632. ;; as we will process their body later. However this does have
  633. ;; the problem that any free var referenced by the lambda is not
  634. ;; marked as needing residualization. Here we hack around this
  635. ;; and treat all bindings as referenced if we are in operator
  636. ;; context.
  637. (or (eq? ctx 'operator)
  638. (not (zero? (operand-use-count op)))))
  639. ;; values := (op ...)
  640. ;; effects := (op ...)
  641. (define (residualize values effects)
  642. ;; Note, values and effects are reversed.
  643. (cond
  644. (in-order?
  645. (let ((values (filter operand-residual-value ops)))
  646. (if (null? values)
  647. body
  648. (build-result (map (compose var-name operand-var) values)
  649. (map operand-sym values)
  650. (map operand-residual-value values)
  651. body))))
  652. (else
  653. (let ((body
  654. (if (null? effects)
  655. body
  656. (let ((effect-vals (map operand-residual-value effects)))
  657. (list->seq #f (reverse (cons body effect-vals)))))))
  658. (if (null? values)
  659. body
  660. (let ((values (reverse values)))
  661. (build-result (map (compose var-name operand-var) values)
  662. (map operand-sym values)
  663. (map operand-residual-value values)
  664. body)))))))
  665. ;; old := (bool ...)
  666. ;; values := (op ...)
  667. ;; effects := ((op . value) ...)
  668. (let prune ((old (map referenced? ops)) (values '()) (effects '()))
  669. (let lp ((ops* ops) (values values) (effects effects))
  670. (cond
  671. ((null? ops*)
  672. (let ((new (map referenced? ops)))
  673. (if (not (equal? new old))
  674. (prune new values '())
  675. (residualize values
  676. (map (lambda (op val)
  677. (set-operand-residual-value! op val)
  678. op)
  679. (map car effects) (map cdr effects))))))
  680. (else
  681. (let ((op (car ops*)))
  682. (cond
  683. ((memq op values)
  684. (lp (cdr ops*) values effects))
  685. ((operand-residual-value op)
  686. (lp (cdr ops*) (cons op values) effects))
  687. ((referenced? op)
  688. (set-operand-residual-value! op (visit-operand op counter 'value))
  689. (lp (cdr ops*) (cons op values) effects))
  690. (else
  691. (lp (cdr ops*)
  692. values
  693. (let ((effect (visit-operand op counter 'effect)))
  694. (if (void? effect)
  695. effects
  696. (acons op effect effects))))))))))))
  697. (define (small-expression? x limit)
  698. (let/ec k
  699. (tree-il-fold
  700. (lambda (x res) ; down
  701. (1+ res))
  702. (lambda (x res) ; up
  703. (if (< res limit)
  704. res
  705. (k #f)))
  706. 0 x)
  707. #t))
  708. (define (extend-env sym op env)
  709. (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
  710. (let loop ((exp exp)
  711. (env vlist-null) ; vhash of gensym -> <operand>
  712. (counter #f) ; inlined call stack
  713. (ctx 'values)) ; effect, value, values, test, operator, or call
  714. (define (lookup var)
  715. (cond
  716. ((vhash-assq var env) => cdr)
  717. (else (error "unbound var" var))))
  718. ;; Find a value referenced a specific number of times. This is a hack
  719. ;; that's used for propagating fresh data structures like rest lists and
  720. ;; prompt tags. Usually we wouldn't copy consed data, but we can do so in
  721. ;; some special cases like `apply' or prompts if we can account
  722. ;; for all of its uses.
  723. ;;
  724. ;; You don't want to use this in general because it introduces a slight
  725. ;; nonlinearity by running peval again (though with a small effort and size
  726. ;; counter).
  727. ;;
  728. (define (find-definition x n-aliases)
  729. (cond
  730. ((lexical-ref? x)
  731. (cond
  732. ((lookup (lexical-ref-gensym x))
  733. => (lambda (op)
  734. (if (var-set? (operand-var op))
  735. (values #f #f)
  736. (let ((y (or (operand-residual-value op)
  737. (visit-operand op counter 'value 10 10)
  738. (operand-source op))))
  739. (cond
  740. ((and (lexical-ref? y)
  741. (= (lexical-refcount (lexical-ref-gensym x)) 1))
  742. ;; X is a simple alias for Y. Recurse, regardless of
  743. ;; the number of aliases we were expecting.
  744. (find-definition y n-aliases))
  745. ((= (lexical-refcount (lexical-ref-gensym x)) n-aliases)
  746. ;; We found a definition that is aliased the right
  747. ;; number of times. We still recurse in case it is a
  748. ;; lexical.
  749. (values (find-definition y 1)
  750. op))
  751. (else
  752. ;; We can't account for our aliases.
  753. (values #f #f)))))))
  754. (else
  755. ;; A formal parameter. Can't say anything about that.
  756. (values #f #f))))
  757. ((= n-aliases 1)
  758. ;; Not a lexical: success, but only if we are looking for an
  759. ;; unaliased value.
  760. (values x #f))
  761. (else (values #f #f))))
  762. (define (visit exp ctx)
  763. (loop exp env counter ctx))
  764. (define (for-value exp) (visit exp 'value))
  765. (define (for-values exp) (visit exp 'values))
  766. (define (for-test exp) (visit exp 'test))
  767. (define (for-effect exp) (visit exp 'effect))
  768. (define (for-call exp) (visit exp 'call))
  769. (define (for-tail exp) (visit exp ctx))
  770. (if counter
  771. (record-effort! counter))
  772. (log 'visit ctx (and=> counter effort-counter)
  773. (unparse-tree-il exp))
  774. (match exp
  775. (($ <const>)
  776. (case ctx
  777. ((effect) (make-void #f))
  778. (else exp)))
  779. (($ <void>)
  780. (case ctx
  781. ((test) (make-const #f #t))
  782. (else exp)))
  783. (($ <lexical-ref> _ _ gensym)
  784. (log 'begin-copy gensym)
  785. (let lp ((op (lookup gensym)))
  786. (cond
  787. ((eq? ctx 'effect)
  788. (log 'lexical-for-effect gensym)
  789. (make-void #f))
  790. ((operand-alias op)
  791. ;; This is an unassigned operand that simply aliases some
  792. ;; other operand. Recurse to avoid residualizing the leaf
  793. ;; binding.
  794. => lp)
  795. ((eq? ctx 'call)
  796. ;; Don't propagate copies if we are residualizing a call.
  797. (log 'residualize-lexical-call gensym op)
  798. (residualize-lexical op))
  799. ((var-set? (operand-var op))
  800. ;; Assigned lexicals don't copy-propagate.
  801. (log 'assigned-var gensym op)
  802. (residualize-lexical op))
  803. ((not (operand-copyable? op))
  804. ;; We already know that this operand is not copyable.
  805. (log 'not-copyable gensym op)
  806. (residualize-lexical op))
  807. ((and=> (operand-constant-value op)
  808. (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
  809. ;; A cache hit.
  810. (let ((val (operand-constant-value op)))
  811. (log 'memoized-constant gensym val)
  812. (for-tail val)))
  813. ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
  814. recursive-effort-limit operand-size-limit)
  815. =>
  816. ;; If we end up deciding to residualize this value instead of
  817. ;; copying it, save that residualized value.
  818. (lambda (val)
  819. (cond
  820. ((not (constant-expression? val))
  821. (log 'not-constant gensym op)
  822. ;; At this point, ctx is operator, test, or value. A
  823. ;; value that is non-constant in one context will be
  824. ;; non-constant in the others, so it's safe to record
  825. ;; that here, and avoid future visits.
  826. (set-operand-copyable?! op #f)
  827. (residualize-lexical op ctx val))
  828. ((or (const? val)
  829. (void? val)
  830. (primitive-ref? val))
  831. ;; Always propagate simple values that cannot lead to
  832. ;; code bloat.
  833. (log 'copy-simple gensym val)
  834. ;; It could be this constant is the result of folding.
  835. ;; If that is the case, cache it. This helps loop
  836. ;; unrolling get farther.
  837. (if (or (eq? ctx 'value) (eq? ctx 'values))
  838. (begin
  839. (log 'memoize-constant gensym val)
  840. (set-operand-constant-value! op val)))
  841. val)
  842. ((= 1 (var-refcount (operand-var op)))
  843. ;; Always propagate values referenced only once.
  844. (log 'copy-single gensym val)
  845. val)
  846. ;; FIXME: do demand-driven size accounting rather than
  847. ;; these heuristics.
  848. ((eq? ctx 'operator)
  849. ;; A pure expression in the operator position. Inline
  850. ;; if it's a lambda that's small enough.
  851. (if (and (lambda? val)
  852. (small-expression? val operator-size-limit))
  853. (begin
  854. (log 'copy-operator gensym val)
  855. val)
  856. (begin
  857. (log 'too-big-for-operator gensym val)
  858. (residualize-lexical op ctx val))))
  859. (else
  860. ;; A pure expression, processed for call or for value.
  861. ;; Don't inline lambdas, because they will probably won't
  862. ;; fold because we don't know the operator.
  863. (if (and (small-expression? val value-size-limit)
  864. (not (tree-il-any lambda? val)))
  865. (begin
  866. (log 'copy-value gensym val)
  867. val)
  868. (begin
  869. (log 'too-big-or-has-lambda gensym val)
  870. (residualize-lexical op ctx val)))))))
  871. (else
  872. ;; Visit failed. Either the operand isn't bound, as in
  873. ;; lambda formal parameters, or the copy was aborted.
  874. (log 'unbound-or-aborted gensym op)
  875. (residualize-lexical op)))))
  876. (($ <lexical-set> src name gensym exp)
  877. (let ((op (lookup gensym)))
  878. (if (zero? (var-refcount (operand-var op)))
  879. (let ((exp (for-effect exp)))
  880. (if (void? exp)
  881. exp
  882. (make-seq src exp (make-void #f))))
  883. (begin
  884. (record-operand-use op)
  885. (make-lexical-set src name (operand-sym op) (for-value exp))))))
  886. (($ <let> src
  887. (names ... rest)
  888. (gensyms ... rest-sym)
  889. (vals ... ($ <primcall> _ 'list rest-args))
  890. ($ <primcall> asrc 'apply
  891. (proc args ...
  892. ($ <lexical-ref> _
  893. (? (cut eq? <> rest))
  894. (? (lambda (sym)
  895. (and (eq? sym rest-sym)
  896. (= (lexical-refcount sym) 1))))))))
  897. (let* ((tmps (make-list (length rest-args) 'tmp))
  898. (tmp-syms (fresh-temporaries tmps)))
  899. (for-tail
  900. (make-let src
  901. (append names tmps)
  902. (append gensyms tmp-syms)
  903. (append vals rest-args)
  904. (make-call
  905. asrc
  906. proc
  907. (append args
  908. (map (cut make-lexical-ref #f <> <>)
  909. tmps tmp-syms)))))))
  910. (($ <let> src names gensyms vals body)
  911. (define (lookup-alias exp)
  912. ;; It's very common for macros to introduce something like:
  913. ;;
  914. ;; ((lambda (x y) ...) x-exp y-exp)
  915. ;;
  916. ;; In that case you might end up trying to inline something like:
  917. ;;
  918. ;; (let ((x x-exp) (y y-exp)) ...)
  919. ;;
  920. ;; But if x-exp is itself a lexical-ref that aliases some much
  921. ;; larger expression, perhaps it will fail to inline due to
  922. ;; size. However we don't want to introduce a useless alias
  923. ;; (in this case, x). So if the RHS of a let expression is a
  924. ;; lexical-ref, we record that expression. If we end up having
  925. ;; to residualize X, then instead we residualize X-EXP, as long
  926. ;; as it isn't assigned.
  927. ;;
  928. (match exp
  929. (($ <lexical-ref> _ _ sym)
  930. (let ((op (lookup sym)))
  931. (and (not (var-set? (operand-var op))) op)))
  932. (_ #f)))
  933. (let* ((vars (map lookup-var gensyms))
  934. (new (fresh-gensyms vars))
  935. (ops (make-bound-operands vars new vals
  936. (lambda (exp counter ctx)
  937. (loop exp env counter ctx))
  938. (map lookup-alias vals)))
  939. (env (fold extend-env env gensyms ops))
  940. (body (loop body env counter ctx)))
  941. (match body
  942. (($ <const>)
  943. (for-tail (list->seq src (append vals (list body)))))
  944. (($ <lexical-ref> _ _ (? (lambda (sym) (memq sym new)) sym))
  945. (let ((pairs (map cons new vals)))
  946. ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
  947. (for-tail
  948. (list->seq
  949. src
  950. (append (map cdr (alist-delete sym pairs eq?))
  951. (list (assq-ref pairs sym)))))))
  952. ((and ($ <conditional> src*
  953. ($ <lexical-ref> _ _ sym) ($ <lexical-ref> _ _ sym) alt)
  954. (? (lambda (_)
  955. (case ctx
  956. ((test effect)
  957. (and (equal? (list sym) new)
  958. (= (lexical-refcount sym) 2)))
  959. (else #f)))))
  960. ;; (let ((x EXP)) (if x x ALT)) -> (if EXP #t ALT) in test context
  961. (make-conditional src* (visit-operand (car ops) counter 'test)
  962. (make-const src* #t) alt))
  963. (_
  964. ;; Only include bindings for which lexical references
  965. ;; have been residualized.
  966. (prune-bindings ops #f body counter ctx
  967. (lambda (names gensyms vals body)
  968. (if (null? names) (error "what!" names))
  969. (make-let src names gensyms vals body)))))))
  970. (($ <fix> src names gensyms vals body)
  971. ;; Note the difference from the `let' case: here we use letrec*
  972. ;; so that the `visit' procedure for the new operands closes over
  973. ;; an environment that includes the operands. Also we don't try
  974. ;; to elide aliases, because we can't sensibly reduce something
  975. ;; like (letrec ((a b) (b a)) a).
  976. (letrec* ((visit (lambda (exp counter ctx)
  977. (loop exp env* counter ctx)))
  978. (vars (map lookup-var gensyms))
  979. (new (fresh-gensyms vars))
  980. (ops (make-bound-operands vars new vals visit))
  981. (env* (fold extend-env env gensyms ops))
  982. (body* (visit body counter ctx)))
  983. (if (const? body*)
  984. body*
  985. (prune-bindings ops #f body* counter ctx
  986. (lambda (names gensyms vals body)
  987. (make-fix src names gensyms vals body))))))
  988. (($ <let-values> lv-src producer consumer)
  989. ;; Peval the producer, then try to inline the consumer into
  990. ;; the producer. If that succeeds, peval again. Otherwise
  991. ;; reconstruct the let-values, pevaling the consumer.
  992. (let ((producer (for-values producer)))
  993. (or (match consumer
  994. ((and ($ <lambda-case> src () #f rest #f () (rest-sym) body #f)
  995. (? (lambda _ (singly-valued-expression? producer))))
  996. (let ((tmp (gensym "tmp ")))
  997. (record-new-temporary! 'tmp tmp 1)
  998. (for-tail
  999. (make-let
  1000. src (list 'tmp) (list tmp) (list producer)
  1001. (make-let
  1002. src (list rest) (list rest-sym)
  1003. (list
  1004. (make-primcall #f 'list
  1005. (list (make-lexical-ref #f 'tmp tmp))))
  1006. body)))))
  1007. (($ <lambda-case> src req opt rest #f inits gensyms body #f)
  1008. (let* ((nmin (length req))
  1009. (nmax (and (not rest) (+ nmin (if opt (length opt) 0)))))
  1010. (cond
  1011. ((inline-values lv-src producer nmin nmax consumer)
  1012. => for-tail)
  1013. (else #f))))
  1014. (_ #f))
  1015. (make-let-values lv-src producer (for-tail consumer)))))
  1016. (($ <toplevel-ref> src mod (? effect-free-primitive? name))
  1017. exp)
  1018. (($ <toplevel-ref>)
  1019. ;; todo: open private local bindings.
  1020. exp)
  1021. (($ <module-ref> src module (? effect-free-primitive? name) #f)
  1022. (let ((module (false-if-exception
  1023. (resolve-module module #:ensure #f))))
  1024. (if (module? module)
  1025. (let ((var (module-variable module name)))
  1026. (if (eq? var (module-variable the-scm-module name))
  1027. (make-primitive-ref src name)
  1028. exp))
  1029. exp)))
  1030. (($ <module-ref> src module name public?)
  1031. (cond
  1032. ((and cross-module-inlining?
  1033. public?
  1034. (and=> (resolve-module module #:ensure #f)
  1035. (lambda (module)
  1036. (and=> (module-public-interface module)
  1037. (lambda (iface)
  1038. (and=> (module-inlinable-exports iface)
  1039. (lambda (proc) (proc name))))))))
  1040. => (lambda (inlined)
  1041. ;; Similar logic to lexical-ref, but we can't enumerate
  1042. ;; uses, and don't know about aliases.
  1043. (log 'begin-xm-copy exp inlined)
  1044. (cond
  1045. ((eq? ctx 'effect)
  1046. (log 'xm-effect)
  1047. (make-void #f))
  1048. ((eq? ctx 'call)
  1049. ;; Don't propagate copies if we are residualizing a call.
  1050. (log 'residualize-xm-call exp)
  1051. exp)
  1052. ((or (const? inlined) (void? inlined) (primitive-ref? inlined))
  1053. ;; Always propagate simple values that cannot lead to
  1054. ;; code bloat.
  1055. (log 'copy-xm-const)
  1056. (for-tail inlined))
  1057. ;; Inline in operator position if it's a lambda that's
  1058. ;; small enough. Normally the inlinable-exports pass
  1059. ;; will only make small lambdas available for inlining,
  1060. ;; but you never know.
  1061. ((and (eq? ctx 'operator) (lambda? inlined)
  1062. (small-expression? inlined operator-size-limit))
  1063. (log 'copy-xm-operator exp inlined)
  1064. (splice-expression inlined))
  1065. (else
  1066. (log 'xm-copy-failed)
  1067. ;; Could copy small lambdas in value context. Something
  1068. ;; to revisit.
  1069. exp))))
  1070. (else exp)))
  1071. (($ <module-set> src mod name public? exp)
  1072. (make-module-set src mod name public? (for-value exp)))
  1073. (($ <toplevel-define> src mod name exp)
  1074. (make-toplevel-define src mod name (for-value exp)))
  1075. (($ <toplevel-set> src mod name exp)
  1076. (make-toplevel-set src mod name (for-value exp)))
  1077. (($ <primitive-ref>)
  1078. (case ctx
  1079. ((effect) (make-void #f))
  1080. ((test) (make-const #f #t))
  1081. (else exp)))
  1082. (($ <conditional> src condition subsequent alternate)
  1083. (define (call-with-failure-thunk exp proc)
  1084. (match exp
  1085. (($ <call> _ _ ()) (proc exp))
  1086. (($ <primcall> _ _ ()) (proc exp))
  1087. (($ <const>) (proc exp))
  1088. (($ <void>) (proc exp))
  1089. (($ <lexical-ref>) (proc exp))
  1090. (_
  1091. (let ((t (gensym "failure-")))
  1092. (record-new-temporary! 'failure t 2)
  1093. (make-let
  1094. src (list 'failure) (list t)
  1095. (list
  1096. (make-lambda
  1097. #f '()
  1098. (make-lambda-case #f '() #f #f #f '() '() exp #f)))
  1099. (proc (make-call #f (make-lexical-ref #f 'failure t)
  1100. '())))))))
  1101. (define (simplify-conditional c)
  1102. (match c
  1103. ;; Swap the arms of (if (not FOO) A B), to simplify.
  1104. (($ <conditional> src ($ <primcall> _ 'not (pred))
  1105. subsequent alternate)
  1106. (simplify-conditional
  1107. (make-conditional src pred alternate subsequent)))
  1108. ;; In the following four cases, we try to expose the test to
  1109. ;; the conditional. This will let the CPS conversion avoid
  1110. ;; reifying boolean literals in some cases.
  1111. (($ <conditional> src ($ <let> src* names vars vals body)
  1112. subsequent alternate)
  1113. (make-let src* names vars vals
  1114. (simplify-conditional
  1115. (make-conditional src body subsequent alternate))))
  1116. (($ <conditional> src ($ <fix> src* names vars vals body)
  1117. subsequent alternate)
  1118. (make-fix src* names vars vals
  1119. (simplify-conditional
  1120. (make-conditional src body subsequent alternate))))
  1121. (($ <conditional> src ($ <seq> src* head tail)
  1122. subsequent alternate)
  1123. (make-seq src* head
  1124. (simplify-conditional
  1125. (make-conditional src tail subsequent alternate))))
  1126. ;; Special cases for common tests in the predicates of chains
  1127. ;; of if expressions.
  1128. (($ <conditional> src
  1129. ($ <conditional> src* outer-test inner-test ($ <const> _ #f))
  1130. inner-subsequent
  1131. alternate)
  1132. (let lp ((alternate alternate))
  1133. (match alternate
  1134. ;; Lift a common repeated test out of a chain of if
  1135. ;; expressions.
  1136. (($ <conditional> _ (? (cut tree-il=? outer-test <>))
  1137. other-subsequent alternate)
  1138. (make-conditional
  1139. src outer-test
  1140. (simplify-conditional
  1141. (make-conditional src* inner-test inner-subsequent
  1142. other-subsequent))
  1143. alternate))
  1144. ;; Likewise, but punching through any surrounding
  1145. ;; failure continuations.
  1146. (($ <let> let-src (name) (sym) ((and thunk ($ <lambda>))) body)
  1147. (make-let
  1148. let-src (list name) (list sym) (list thunk)
  1149. (lp body)))
  1150. ;; Otherwise, rotate AND tests to expose a simple
  1151. ;; condition in the front. Although this may result in
  1152. ;; lexically binding failure thunks, the thunks will be
  1153. ;; compiled to labels allocation, so there's no actual
  1154. ;; code growth.
  1155. (_
  1156. (call-with-failure-thunk
  1157. alternate
  1158. (lambda (failure)
  1159. (make-conditional
  1160. src outer-test
  1161. (simplify-conditional
  1162. (make-conditional src* inner-test inner-subsequent failure))
  1163. failure)))))))
  1164. (_ c)))
  1165. (match (for-test condition)
  1166. (($ <const> _ val)
  1167. (if val
  1168. (for-tail subsequent)
  1169. (for-tail alternate)))
  1170. (c
  1171. (simplify-conditional
  1172. (make-conditional src c (for-tail subsequent)
  1173. (for-tail alternate))))))
  1174. (($ <primcall> src 'call-with-values
  1175. (producer
  1176. ($ <lambda> _ _
  1177. (and consumer
  1178. ;; No optional or kwargs.
  1179. ($ <lambda-case>
  1180. _ req #f rest #f () gensyms body #f)))))
  1181. (for-tail (make-let-values src (make-call src producer '())
  1182. consumer)))
  1183. (($ <primcall> src 'dynamic-wind (w thunk u))
  1184. (for-tail
  1185. (with-temporaries
  1186. src (list w u) 2 constant-expression?
  1187. (match-lambda
  1188. ((w u)
  1189. (make-seq
  1190. src
  1191. (make-seq
  1192. src
  1193. (make-conditional
  1194. src
  1195. ;; fixme: introduce logic to fold thunk?
  1196. (make-primcall src 'thunk? (list u))
  1197. (make-call src w '())
  1198. (make-primcall
  1199. src 'throw
  1200. (list
  1201. (make-const #f 'wrong-type-arg)
  1202. (make-const #f "dynamic-wind")
  1203. (make-const #f "Wrong type (expecting thunk): ~S")
  1204. (make-primcall #f 'list (list u))
  1205. (make-primcall #f 'list (list u)))))
  1206. (make-primcall src 'wind (list w u)))
  1207. (make-begin0 src
  1208. (make-call src thunk '())
  1209. (make-seq src
  1210. (make-primcall src 'unwind '())
  1211. (make-call src u '())))))))))
  1212. (($ <primcall> src 'with-fluid* (f v thunk))
  1213. (for-tail
  1214. (with-temporaries
  1215. src (list f v thunk) 1 constant-expression?
  1216. (match-lambda
  1217. ((f v thunk)
  1218. (make-seq src
  1219. (make-primcall src 'push-fluid (list f v))
  1220. (make-begin0 src
  1221. (make-call src thunk '())
  1222. (make-primcall src 'pop-fluid '()))))))))
  1223. (($ <primcall> src 'with-dynamic-state (state thunk))
  1224. (for-tail
  1225. (with-temporaries
  1226. src (list state thunk) 1 constant-expression?
  1227. (match-lambda
  1228. ((state thunk)
  1229. (make-seq src
  1230. (make-primcall src 'push-dynamic-state (list state))
  1231. (make-begin0 src
  1232. (make-call src thunk '())
  1233. (make-primcall src 'pop-dynamic-state
  1234. '()))))))))
  1235. (($ <primcall> src 'values exps)
  1236. (cond
  1237. ((null? exps)
  1238. (if (eq? ctx 'effect)
  1239. (make-void #f)
  1240. exp))
  1241. (else
  1242. (let ((vals (map for-value exps)))
  1243. (if (and (case ctx
  1244. ((value test effect) #t)
  1245. (else (null? (cdr vals))))
  1246. (every singly-valued-expression? vals))
  1247. (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
  1248. (make-primcall src 'values vals))))))
  1249. (($ <primcall> src 'apply (proc args ... tail))
  1250. (let lp ((tail* (find-definition tail 1)) (speculative? #t))
  1251. (define (copyable? x)
  1252. ;; Inlining a result from find-definition effectively copies it,
  1253. ;; relying on the let-pruning to remove its original binding. We
  1254. ;; shouldn't copy non-constant expressions.
  1255. (or (not speculative?) (constant-expression? x)))
  1256. (match tail*
  1257. (($ <const> _ (args* ...))
  1258. (let ((args* (map (cut make-const #f <>) args*)))
  1259. (for-tail (make-call src proc (append args args*)))))
  1260. (($ <primcall> _ 'cons
  1261. ((and head (? copyable?)) (and tail (? copyable?))))
  1262. (for-tail (make-primcall src 'apply
  1263. (cons proc
  1264. (append args (list head tail))))))
  1265. (($ <primcall> _ 'list
  1266. (and args* ((? copyable?) ...)))
  1267. (for-tail (make-call src proc (append args args*))))
  1268. (tail*
  1269. (if speculative?
  1270. (lp (for-value tail) #f)
  1271. (let ((args (append (map for-value args) (list tail*))))
  1272. (make-primcall src 'apply
  1273. (cons (for-value proc) args))))))))
  1274. (($ <primcall> src (? constructor-primitive? name) args)
  1275. (cond
  1276. ((and (memq ctx '(effect test))
  1277. (match (cons name args)
  1278. ((or ('cons _ _)
  1279. ('list . _)
  1280. ('vector . _)
  1281. ('make-prompt-tag)
  1282. ('make-prompt-tag ($ <const> _ (? string?))))
  1283. #t)
  1284. (_ #f)))
  1285. ;; Some expressions can be folded without visiting the
  1286. ;; arguments for value.
  1287. (let ((res (if (eq? ctx 'effect)
  1288. (make-void #f)
  1289. (make-const #f #t))))
  1290. (for-tail (list->seq src (append args (list res))))))
  1291. (else
  1292. (match (cons name (map for-value args))
  1293. (('cons x ($ <const> _ (? (cut eq? <> '()))))
  1294. (make-primcall src 'list (list x)))
  1295. (('cons x ($ <primcall> _ 'list elts))
  1296. (make-primcall src 'list (cons x elts)))
  1297. (('list)
  1298. (make-const src '()))
  1299. (('vector)
  1300. (make-const src '#()))
  1301. ((name . args)
  1302. (make-primcall src name args))))))
  1303. (($ <primcall> src 'thunk? (proc))
  1304. (case ctx
  1305. ((effect)
  1306. (for-tail (make-seq src proc (make-void src))))
  1307. (else
  1308. (match (for-value proc)
  1309. (($ <lambda> _ _ ($ <lambda-case> _ req))
  1310. (for-tail (make-const src (null? req))))
  1311. (proc
  1312. (match (find-definition proc 2)
  1313. (($ <lambda> _ _ ($ <lambda-case> _ req))
  1314. (for-tail (make-const src (null? req))))
  1315. (_
  1316. (make-primcall src 'thunk? (list proc)))))))))
  1317. (($ <primcall> src name args)
  1318. (match (cons name (map for-value args))
  1319. ;; FIXME: these for-tail recursions could take place outside
  1320. ;; an effort counter.
  1321. (('car ($ <primcall> src 'cons (head tail)))
  1322. (for-tail (make-seq src tail head)))
  1323. (('cdr ($ <primcall> src 'cons (head tail)))
  1324. (for-tail (make-seq src head tail)))
  1325. (('car ($ <primcall> src 'list (head . tail)))
  1326. (for-tail (list->seq src (append tail (list head)))))
  1327. (('cdr ($ <primcall> src 'list (head . tail)))
  1328. (for-tail (make-seq src head (make-primcall #f 'list tail))))
  1329. (('car ($ <const> src (head . tail)))
  1330. (for-tail (make-const src head)))
  1331. (('cdr ($ <const> src (head . tail)))
  1332. (for-tail (make-const src tail)))
  1333. (((or 'memq 'memv) k ($ <const> _ (elts ...)))
  1334. ;; FIXME: factor
  1335. (case ctx
  1336. ((effect)
  1337. (for-tail
  1338. (make-seq src k (make-void #f))))
  1339. ((test)
  1340. (cond
  1341. ((const? k)
  1342. ;; A shortcut. The `else' case would handle it, but
  1343. ;; this way is faster.
  1344. (let ((member (case name ((memq) memq) ((memv) memv))))
  1345. (make-const #f (and (member (const-exp k) elts) #t))))
  1346. ((null? elts)
  1347. (for-tail
  1348. (make-seq src k (make-const #f #f))))
  1349. (else
  1350. (let ((t (gensym "t "))
  1351. (eq (if (eq? name 'memq) 'eq? 'eqv?)))
  1352. (record-new-temporary! 't t (length elts))
  1353. (for-tail
  1354. (make-let
  1355. src (list 't) (list t) (list k)
  1356. (let lp ((elts elts))
  1357. (define test
  1358. (make-primcall #f eq
  1359. (list (make-lexical-ref #f 't t)
  1360. (make-const #f (car elts)))))
  1361. (if (null? (cdr elts))
  1362. test
  1363. (make-conditional src test
  1364. (make-const #f #t)
  1365. (lp (cdr elts)))))))))))
  1366. (else
  1367. (cond
  1368. ((const? k)
  1369. (let ((member (case name ((memq) memq) ((memv) memv))))
  1370. (make-const #f (member (const-exp k) elts))))
  1371. ((null? elts)
  1372. (for-tail (make-seq src k (make-const #f #f))))
  1373. (else
  1374. (make-primcall src name (list k (make-const #f elts))))))))
  1375. (((? equality-primitive?) a (and b ($ <const> _ v)))
  1376. (cond
  1377. ((const? a)
  1378. ;; Constants will be deduplicated later, but eq? folding can
  1379. ;; happen now. Anticipate the deduplication by using equal?
  1380. ;; instead of eq? or eqv?.
  1381. (for-tail (make-const src (equal? (const-exp a) v))))
  1382. ((eq? name 'eq?)
  1383. ;; Already in a reduced state.
  1384. (make-primcall src 'eq? (list a b)))
  1385. ((or (memq v '(#f #t () #nil)) (symbol? v) (char? v)
  1386. ;; Only fold to eq? value is a fixnum on target and
  1387. ;; host, as constant folding may have us compare on host
  1388. ;; as well.
  1389. (and (exact-integer? v)
  1390. (<= (max (target-most-negative-fixnum)
  1391. most-negative-fixnum)
  1392. v
  1393. (min (target-most-positive-fixnum)
  1394. most-positive-fixnum))))
  1395. ;; Reduce to eq?. Note that in Guile, characters are
  1396. ;; comparable with eq?.
  1397. (make-primcall src 'eq? (list a b)))
  1398. ((number? v)
  1399. ;; equal? and eqv? on non-fixnum numbers is the same as
  1400. ;; eqv?, and can't be reduced beyond that.
  1401. (make-primcall src 'eqv? (list a b)))
  1402. ((eq? name 'eqv?)
  1403. ;; eqv? on anything else is the same as eq?.
  1404. (make-primcall src 'eq? (list a b)))
  1405. (else
  1406. ;; FIXME: inline a specialized implementation of equal? for
  1407. ;; V here.
  1408. (make-primcall src name (list a b)))))
  1409. (((? equality-primitive?) (and a ($ <const>)) b)
  1410. (for-tail (make-primcall src name (list b a))))
  1411. (((? equality-primitive?) ($ <lexical-ref> _ _ sym)
  1412. ($ <lexical-ref> _ _ sym))
  1413. (for-tail (make-const src #t)))
  1414. (('logbit? ($ <const> src2
  1415. (? (lambda (bit)
  1416. (and (exact-integer? bit)
  1417. (<= 0 bit (logcount most-positive-fixnum))))
  1418. bit))
  1419. val)
  1420. (for-tail
  1421. (make-primcall src 'logtest
  1422. (list (make-const src2 (ash 1 bit)) val))))
  1423. (('logtest a b)
  1424. (for-tail
  1425. (make-primcall
  1426. src
  1427. 'not
  1428. (list
  1429. (make-primcall src 'eq?
  1430. (list (make-primcall src 'logand (list a b))
  1431. (make-const src 0)))))))
  1432. (((? effect-free-primitive?) . args)
  1433. (fold-constants src name args ctx))
  1434. ((name . args)
  1435. (make-primcall src name args))))
  1436. (($ <call> src orig-proc orig-args)
  1437. ;; todo: augment the global env with specialized functions
  1438. (let revisit-proc ((proc (visit orig-proc 'operator)))
  1439. (match proc
  1440. (($ <primitive-ref> _ name)
  1441. (let ((exp (expand-primcall (make-primcall src name orig-args))))
  1442. (set! store
  1443. (augment-var-table-with-externally-introduced-lexicals
  1444. exp store))
  1445. (for-tail exp)))
  1446. (($ <lambda> _ _
  1447. ($ <lambda-case> _ req opt rest #f inits gensyms body #f))
  1448. ;; Simple case: no keyword arguments.
  1449. ;; todo: handle the more complex cases
  1450. (let* ((nargs (length orig-args))
  1451. (nreq (length req))
  1452. (opt (or opt '()))
  1453. (rest (if rest (list rest) '()))
  1454. (nopt (length opt))
  1455. (key (source-expression proc)))
  1456. (define (singly-referenced-lambda? orig-proc)
  1457. (match orig-proc
  1458. (($ <lambda>) #t)
  1459. (($ <lexical-ref> _ _ sym)
  1460. (and (not (assigned-lexical? sym))
  1461. (= (lexical-refcount sym) 1)
  1462. (singly-referenced-lambda?
  1463. (operand-source (lookup sym)))))
  1464. (_ #f)))
  1465. (define (inlined-call)
  1466. (let ((req-vals (list-head orig-args nreq))
  1467. (opt-vals (let lp ((args (drop orig-args nreq))
  1468. (inits inits)
  1469. (out '()))
  1470. (match inits
  1471. (() (reverse out))
  1472. ((init . inits)
  1473. (match args
  1474. (()
  1475. (lp '() inits (cons init out)))
  1476. ((arg . args)
  1477. (lp args inits (cons arg out))))))))
  1478. (rest-vals (cond
  1479. ((> nargs (+ nreq nopt))
  1480. (list (make-primcall
  1481. #f 'list
  1482. (drop orig-args (+ nreq nopt)))))
  1483. ((null? rest) '())
  1484. (else (list (make-const #f '()))))))
  1485. (if (>= nargs (+ nreq nopt))
  1486. (make-let src
  1487. (append req opt rest)
  1488. gensyms
  1489. (append req-vals opt-vals rest-vals)
  1490. body)
  1491. ;; The default initializers of optional arguments
  1492. ;; may refer to earlier arguments, so in the general
  1493. ;; case we must expand into a series of nested let
  1494. ;; expressions.
  1495. ;;
  1496. ;; In the generated code, the outermost let
  1497. ;; expression will bind all required arguments, as
  1498. ;; well as the empty rest argument, if any. Each
  1499. ;; optional argument will be bound within an inner
  1500. ;; let.
  1501. (make-let src
  1502. (append req rest)
  1503. (append (list-head gensyms nreq)
  1504. (last-pair gensyms))
  1505. (append req-vals rest-vals)
  1506. (fold-right (lambda (var gensym val body)
  1507. (make-let src
  1508. (list var)
  1509. (list gensym)
  1510. (list val)
  1511. body))
  1512. body
  1513. opt
  1514. (list-head (drop gensyms nreq) nopt)
  1515. opt-vals)))))
  1516. (cond
  1517. ((or (< nargs nreq) (and (null? rest) (> nargs (+ nreq nopt))))
  1518. ;; An error, or effecting arguments.
  1519. (make-call src (for-call orig-proc) (map for-value orig-args)))
  1520. ((or (and=> (find-counter key counter) counter-recursive?)
  1521. (singly-referenced-lambda? orig-proc))
  1522. ;; A recursive call, or a lambda in the operator
  1523. ;; position of the source expression. Process again in
  1524. ;; tail context.
  1525. ;;
  1526. ;; In the recursive case, mark intervening counters as
  1527. ;; recursive, so we can handle a toplevel counter that
  1528. ;; recurses mutually with some other procedure.
  1529. ;; Otherwise, the next time we see the other procedure,
  1530. ;; the effort limit would be clamped to 100.
  1531. ;;
  1532. (let ((found (find-counter key counter)))
  1533. (if (and found (counter-recursive? found))
  1534. (let lp ((counter counter))
  1535. (if (not (eq? counter found))
  1536. (begin
  1537. (set-counter-recursive?! counter #t)
  1538. (lp (counter-prev counter)))))))
  1539. (log 'inline-recurse key)
  1540. (loop (inlined-call) env counter ctx))
  1541. (else
  1542. ;; An integration at the top-level, the first
  1543. ;; recursion of a recursive procedure, or a nested
  1544. ;; integration of a procedure that hasn't been seen
  1545. ;; yet.
  1546. (log 'inline-begin exp)
  1547. (let/ec k
  1548. (define (abort)
  1549. (log 'inline-abort exp)
  1550. (k (make-call src (for-call orig-proc)
  1551. (map for-value orig-args))))
  1552. (define new-counter
  1553. (cond
  1554. ;; These first two cases will transfer effort
  1555. ;; from the current counter into the new
  1556. ;; counter.
  1557. ((find-counter key counter)
  1558. => (lambda (prev)
  1559. (make-recursive-counter recursive-effort-limit
  1560. operand-size-limit
  1561. prev counter)))
  1562. (counter
  1563. (make-nested-counter abort key counter))
  1564. ;; This case opens a new account, effectively
  1565. ;; printing money. It should only do so once
  1566. ;; for each call site in the source program.
  1567. (else
  1568. (make-top-counter effort-limit operand-size-limit
  1569. abort key))))
  1570. (define result
  1571. (loop (inlined-call) env new-counter ctx))
  1572. (if counter
  1573. ;; The nested inlining attempt succeeded.
  1574. ;; Deposit the unspent effort and size back
  1575. ;; into the current counter.
  1576. (transfer! new-counter counter))
  1577. (log 'inline-end result exp)
  1578. result)))))
  1579. (($ <let> _ _ _ vals _)
  1580. ;; Attempt to inline `let' in the operator position.
  1581. ;;
  1582. ;; We have to re-visit the proc in value mode, since the
  1583. ;; `let' bindings might have been introduced or renamed,
  1584. ;; whereas the lambda (if any) in operator position has not
  1585. ;; been renamed.
  1586. (if (or (and-map constant-expression? vals)
  1587. (and-map constant-expression? orig-args))
  1588. ;; The arguments and the let-bound values commute.
  1589. (match (for-value orig-proc)
  1590. (($ <let> lsrc names syms vals body)
  1591. (log 'inline-let orig-proc)
  1592. (for-tail
  1593. (make-let lsrc names syms vals
  1594. (make-call src body orig-args))))
  1595. ;; It's possible for a `let' to go away after the
  1596. ;; visit due to the fact that visiting a procedure in
  1597. ;; value context will prune unused bindings, whereas
  1598. ;; visiting in operator mode can't because it doesn't
  1599. ;; traverse through lambdas. In that case re-visit
  1600. ;; the procedure.
  1601. (proc (revisit-proc proc)))
  1602. (make-call src (for-call orig-proc)
  1603. (map for-value orig-args))))
  1604. (_
  1605. (make-call src (for-call orig-proc) (map for-value orig-args))))))
  1606. (($ <lambda> src meta body)
  1607. (case ctx
  1608. ((effect) (make-void #f))
  1609. ((test) (make-const #f #t))
  1610. ((operator) exp)
  1611. (else (record-source-expression!
  1612. exp
  1613. (make-lambda src meta (and body (for-values body)))))))
  1614. (($ <lambda-case> src req opt rest kw inits gensyms body alt)
  1615. (define (lift-applied-lambda body gensyms)
  1616. (and (not opt) rest (not kw)
  1617. (match body
  1618. (($ <primcall> _ 'apply
  1619. (($ <lambda> _ _ (and lcase ($ <lambda-case> _ req1)))
  1620. ($ <lexical-ref> _ _ sym)
  1621. ...))
  1622. (and (equal? sym gensyms)
  1623. (not (lambda-case-alternate lcase))
  1624. (<= (length req) (length req1))
  1625. (every (lambda (s)
  1626. (= (lexical-refcount s) 1))
  1627. sym)
  1628. lcase))
  1629. (_ #f))))
  1630. (let* ((vars (map lookup-var gensyms))
  1631. (new (fresh-gensyms vars))
  1632. (env (fold extend-env env gensyms
  1633. (make-unbound-operands vars new)))
  1634. (new-sym (lambda (old)
  1635. (operand-sym (cdr (vhash-assq old env)))))
  1636. (body (loop body env counter ctx)))
  1637. (or
  1638. ;; (lambda args (apply (lambda ...) args)) => (lambda ...)
  1639. (lift-applied-lambda body new)
  1640. (make-lambda-case src req opt rest
  1641. (match kw
  1642. ((aok? (kw name old) ...)
  1643. (cons aok? (map list kw name (map new-sym old))))
  1644. (_ #f))
  1645. (map (cut loop <> env counter 'value) inits)
  1646. new
  1647. body
  1648. (and alt (for-tail alt))))))
  1649. (($ <seq> src head tail)
  1650. (let ((head (for-effect head))
  1651. (tail (for-tail tail)))
  1652. (if (void? head)
  1653. tail
  1654. (make-seq src
  1655. (if (and (seq? head)
  1656. (void? (seq-tail head)))
  1657. (seq-head head)
  1658. head)
  1659. tail))))
  1660. (($ <prompt> src escape-only? tag body handler)
  1661. (define (make-prompt-tag? x)
  1662. (match x
  1663. (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
  1664. #t)
  1665. (_ #f)))
  1666. (let ((tag (for-value tag))
  1667. (body (if escape-only? (for-tail body) (for-value body))))
  1668. (cond
  1669. ((find-definition tag 1)
  1670. (lambda (val op)
  1671. (make-prompt-tag? val))
  1672. => (lambda (val op)
  1673. ;; There is no way that an <abort> could know the tag
  1674. ;; for this <prompt>, so we can elide the <prompt>
  1675. ;; entirely.
  1676. (when op (unrecord-operand-uses op 1))
  1677. (for-tail (if escape-only? body (make-call src body '())))))
  1678. (else
  1679. (let ((handler (for-value handler)))
  1680. (define (escape-only-handler? handler)
  1681. (match handler
  1682. (($ <lambda> _ _
  1683. ($ <lambda-case> _ (_ . _) _ _ _ _ (k . _) body #f))
  1684. (not (tree-il-any
  1685. (match-lambda
  1686. (($ <lexical-ref> _ _ (? (cut eq? <> k))) #t)
  1687. (_ #f))
  1688. body)))
  1689. (else #f)))
  1690. (if (and (not escape-only?) (escape-only-handler? handler))
  1691. ;; Prompt transitioning to escape-only; transition body
  1692. ;; to be an expression.
  1693. (for-tail
  1694. (make-prompt src #t tag (make-call #f body '()) handler))
  1695. (make-prompt src escape-only? tag body handler)))))))
  1696. (($ <abort> src tag args tail)
  1697. (make-abort src (for-value tag) (map for-value args)
  1698. (for-value tail))))))