save.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 2000,2001,2002, 2006, 2009, 2010, 2013, 2015 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (oop goops save)
  19. :use-module (oop goops internal)
  20. :export (make-unbound save-objects load-objects restore
  21. enumerate! enumerate-component!
  22. write-readably write-component write-component-procedure
  23. literal? readable make-readable))
  24. (define (make-unbound)
  25. *unbound*)
  26. ;;;
  27. ;;; save-objects ALIST PORT [EXCLUDED] [USES]
  28. ;;;
  29. ;;; ALIST ::= ((NAME . OBJECT) ...)
  30. ;;;
  31. ;;; Save OBJECT ... to PORT so that when the data is read and evaluated
  32. ;;; OBJECT ... are re-created under names NAME ... .
  33. ;;; Exclude any references to objects in the list EXCLUDED.
  34. ;;; Add a (use-modules . USES) line to the top of the saved text.
  35. ;;;
  36. ;;; In some instances, when `save-object' doesn't know how to produce
  37. ;;; readable syntax for an object, you can explicitly register read
  38. ;;; syntax for an object using the special form `readable'.
  39. ;;;
  40. ;;; Example:
  41. ;;;
  42. ;;; The function `foo' produces an object of obscure structure.
  43. ;;; Only `foo' can construct such objects. Because of this, an
  44. ;;; object such as
  45. ;;;
  46. ;;; (define x (vector 1 (foo)))
  47. ;;;
  48. ;;; cannot be saved by `save-objects'. But if you instead write
  49. ;;;
  50. ;;; (define x (vector 1 (readable (foo))))
  51. ;;;
  52. ;;; `save-objects' will happily produce the necessary read syntax.
  53. ;;;
  54. ;;; To add new read syntax, hang methods on `enumerate!' and
  55. ;;; `write-readably'.
  56. ;;;
  57. ;;; enumerate! OBJECT ENV
  58. ;;; Should call `enumerate-component!' (which takes same args) on
  59. ;;; each component object. Should return #t if the composite object
  60. ;;; can be written as a literal. (`enumerate-component!' returns #t
  61. ;;; if the component is a literal.
  62. ;;;
  63. ;;; write-readably OBJECT PORT ENV
  64. ;;; Should write a readable representation of OBJECT to PORT.
  65. ;;; Should use `write-component' to print each component object.
  66. ;;; Use `literal?' to decide if a component is a literal.
  67. ;;;
  68. ;;; Utilities:
  69. ;;;
  70. ;;; enumerate-component! OBJECT ENV
  71. ;;;
  72. ;;; write-component OBJECT PATCHER PORT ENV
  73. ;;; PATCHER is an expression which, when evaluated, stores OBJECT
  74. ;;; into its current location.
  75. ;;;
  76. ;;; Example:
  77. ;;;
  78. ;;; (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
  79. ;;;
  80. ;;; write-component is a macro.
  81. ;;;
  82. ;;; literal? COMPONENT ENV
  83. ;;;
  84. (define-method (immediate? (o <top>)) #f)
  85. (define-method (immediate? (o <null>)) #t)
  86. (define-method (immediate? (o <number>)) #t)
  87. (define-method (immediate? (o <boolean>)) #t)
  88. (define-method (immediate? (o <symbol>)) #t)
  89. (define-method (immediate? (o <char>)) #t)
  90. (define-method (immediate? (o <keyword>)) #t)
  91. ;;; enumerate! OBJECT ENVIRONMENT
  92. ;;;
  93. ;;; Return #t if object is a literal.
  94. ;;;
  95. (define-method (enumerate! (o <top>) env) #t)
  96. (define-method (write-readably (o <top>) file env)
  97. ;;(goops-error "No read-syntax defined for object `~S'" o)
  98. (write o file) ;doesn't catch bugs, but is much more flexible
  99. )
  100. ;;;
  101. ;;; Readables
  102. ;;;
  103. (define readables (make-weak-key-hash-table 61))
  104. (define-macro (readable exp)
  105. `(make-readable ,exp ',(copy-tree exp)))
  106. (define (make-readable obj expr)
  107. (hashq-set! readables obj expr)
  108. obj)
  109. (define (readable-expression obj)
  110. `(readable ,(hashq-ref readables obj)))
  111. ;; FIXME: if obj is nil or false, this can return a false value. OTOH
  112. ;; usually this is only for non-immediates.
  113. (define (readable? obj)
  114. (hashq-ref readables obj))
  115. ;;;
  116. ;;; Writer helpers
  117. ;;;
  118. (define (write-component-procedure o file env)
  119. "Return #f if circular reference"
  120. (cond ((immediate? o) (write o file) #t)
  121. ((readable? o) (write (readable-expression o) file) #t)
  122. ((excluded? o env) (display #f file) #t)
  123. (else
  124. (let ((info (object-info o env)))
  125. (cond ((not (binding? info)) (write-readably o file env) #t)
  126. ((not (eq? (visiting info) #:defined)) #f) ;forward reference
  127. (else (display (binding info) file) #t))))))
  128. ;;; write-component OBJECT PATCHER FILE ENV
  129. ;;;
  130. (define-macro (write-component object patcher file env)
  131. `(or (write-component-procedure ,object ,file ,env)
  132. (begin
  133. (display #f ,file)
  134. (add-patcher! ,patcher ,env))))
  135. ;;;
  136. ;;; Strings
  137. ;;;
  138. (define-method (enumerate! (o <string>) env) #f)
  139. ;;;
  140. ;;; Vectors
  141. ;;;
  142. (define-method (enumerate! (o <vector>) env)
  143. (or (not (vector? o))
  144. (let ((literal? #t))
  145. (array-for-each (lambda (o)
  146. (if (not (enumerate-component! o env))
  147. (set! literal? #f)))
  148. o)
  149. literal?)))
  150. (define-method (write-readably (o <vector>) file env)
  151. (if (not (vector? o))
  152. (write o file)
  153. (let ((n (vector-length o)))
  154. (if (zero? n)
  155. (display "#()" file)
  156. (let ((not-literal? (not (literal? o env))))
  157. (display (if not-literal?
  158. "(vector "
  159. "#(")
  160. file)
  161. (if (and not-literal?
  162. (literal? (vector-ref o 0) env))
  163. (display #\' file))
  164. (write-component (vector-ref o 0)
  165. `(vector-set! ,o 0 ,(vector-ref o 0))
  166. file
  167. env)
  168. (do ((i 1 (+ 1 i)))
  169. ((= i n))
  170. (display #\space file)
  171. (if (and not-literal?
  172. (literal? (vector-ref o i) env))
  173. (display #\' file))
  174. (write-component (vector-ref o i)
  175. `(vector-set! ,o ,i ,(vector-ref o i))
  176. file
  177. env))
  178. (display #\) file))))))
  179. ;;;
  180. ;;; Arrays
  181. ;;;
  182. (define-method (enumerate! (o <array>) env)
  183. (enumerate-component! (shared-array-root o) env))
  184. (define (make-mapper array)
  185. (let* ((n (array-rank array))
  186. (indices (reverse (if (<= n 11)
  187. (list-tail '(t s r q p n m l k j i) (- 11 n))
  188. (let loop ((n n)
  189. (ls '()))
  190. (if (zero? n)
  191. ls
  192. (loop (- n 1)
  193. (cons (gensym "i") ls))))))))
  194. `(lambda ,indices
  195. (+ ,(shared-array-offset array)
  196. ,@(map (lambda (ind dim inc)
  197. `(* ,inc ,(if (pair? dim) `(- ,ind ,(car dim)) ind)))
  198. indices
  199. (array-dimensions array)
  200. (shared-array-increments array))))))
  201. (define (write-array prefix o not-literal? file env)
  202. (letrec ((inner (lambda (n indices)
  203. (if (not (zero? n))
  204. (let ((el (apply array-ref o
  205. (reverse (cons 0 indices)))))
  206. (if (and not-literal?
  207. (literal? el env))
  208. (display #\' file))
  209. (write-component
  210. el
  211. `(array-set! ,o ,el ,@indices)
  212. file
  213. env)))
  214. (do ((i 1 (+ 1 i)))
  215. ((= i n))
  216. (display #\space file)
  217. (let ((el (apply array-ref o
  218. (reverse (cons i indices)))))
  219. (if (and not-literal?
  220. (literal? el env))
  221. (display #\' file))
  222. (write-component
  223. el
  224. `(array-set! ,o ,el ,@indices)
  225. file
  226. env))))))
  227. (display prefix file)
  228. (let loop ((dims (array-dimensions o))
  229. (indices '()))
  230. (cond ((null? (cdr dims))
  231. (inner (car dims) indices))
  232. (else
  233. (let ((n (car dims)))
  234. (do ((i 0 (+ 1 i)))
  235. ((= i n))
  236. (if (> i 0)
  237. (display #\space file))
  238. (display prefix file)
  239. (loop (cdr dims) (cons i indices))
  240. (display #\) file))))))
  241. (display #\) file)))
  242. (define-method (write-readably (o <array>) file env)
  243. (let ((root (shared-array-root o)))
  244. (cond ((literal? o env)
  245. (if (not (vector? root))
  246. (write o file)
  247. (begin
  248. (display #\# file)
  249. (display (array-rank o) file)
  250. (write-array #\( o #f file env))))
  251. ((binding? root env)
  252. (display "(make-shared-array " file)
  253. (if (literal? root env)
  254. (display #\' file))
  255. (write-component root
  256. (goops-error "write-readably(<array>): internal error")
  257. file
  258. env)
  259. (display #\space file)
  260. (display (make-mapper o) file)
  261. (for-each (lambda (dim)
  262. (display #\space file)
  263. (display dim file))
  264. (array-dimensions o))
  265. (display #\) file))
  266. (else
  267. (display "(list->uniform-array " file)
  268. (display (array-rank o) file)
  269. (display " '() " file)
  270. (write-array "(list " o #f file env)))))
  271. ;;;
  272. ;;; Pairs
  273. ;;;
  274. ;;; These methods have more complex structure than is required for
  275. ;;; most objects, since they take over some of the logic of
  276. ;;; `write-component'.
  277. ;;;
  278. (define-method (enumerate! (o <pair>) env)
  279. (let ((literal? (enumerate-component! (car o) env)))
  280. (and (enumerate-component! (cdr o) env)
  281. literal?)))
  282. (define-method (write-readably (o <pair>) file env)
  283. (let ((proper? (let loop ((ls o))
  284. (or (null? ls)
  285. (and (pair? ls)
  286. (not (binding? (cdr ls) env))
  287. (loop (cdr ls))))))
  288. (1? (or (not (pair? (cdr o)))
  289. (binding? (cdr o) env)))
  290. (not-literal? (not (literal? o env)))
  291. (infos '())
  292. (refs (ref-stack env)))
  293. (display (cond ((not not-literal?) #\()
  294. (proper? "(list ")
  295. (1? "(cons ")
  296. (else "(cons* "))
  297. file)
  298. (if (and not-literal?
  299. (literal? (car o) env))
  300. (display #\' file))
  301. (write-component (car o) `(set-car! ,o ,(car o)) file env)
  302. (do ((ls (cdr o) (cdr ls))
  303. (prev o ls))
  304. ((or (not (pair? ls))
  305. (binding? ls env))
  306. (if (not (null? ls))
  307. (begin
  308. (if (not not-literal?)
  309. (display " ." file))
  310. (display #\space file)
  311. (if (and not-literal?
  312. (literal? ls env))
  313. (display #\' file))
  314. (write-component ls `(set-cdr! ,prev ,ls) file env)))
  315. (display #\) file))
  316. (display #\space file)
  317. (set! infos (cons (object-info ls env) infos))
  318. (push-ref! ls env) ;*fixme* optimize
  319. (set! (visiting? (car infos)) #t)
  320. (if (and not-literal?
  321. (literal? (car ls) env))
  322. (display #\' file))
  323. (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
  324. )
  325. (for-each (lambda (info)
  326. (set! (visiting? info) #f))
  327. infos)
  328. (set! (ref-stack env) refs)
  329. ))
  330. ;;;
  331. ;;; Objects
  332. ;;;
  333. ;;; Doesn't yet handle unbound slots
  334. ;; Don't export this function! This is all very temporary.
  335. ;;
  336. (define (get-set-for-each proc class)
  337. (for-each (lambda (slot)
  338. (unless (memq (slot-definition-allocation slot)
  339. '(#:class #:each-subclass))
  340. (let ((ref (slot-definition-slot-ref slot))
  341. (set (slot-definition-slot-set! slot))
  342. (index (slot-definition-index slot)))
  343. (if ref
  344. (proc ref set)
  345. (proc (standard-get index) (standard-set index))))))
  346. (class-slots class)))
  347. (define (access-for-each proc class)
  348. (for-each (lambda (slot)
  349. (unless (memq (slot-definition-allocation slot)
  350. '(#:class #:each-subclass))
  351. (let ((name (slot-definition-name slot))
  352. (accessor (and=> (slot-definition-accessor slot)
  353. generic-function-name))
  354. (ref (slot-definition-slot-ref slot))
  355. (set (slot-definition-slot-set! slot))
  356. (index (slot-definition-index slot)))
  357. (if ref
  358. (proc name accessor ref set)
  359. (proc name accessor
  360. (standard-get index) (standard-set index))))))
  361. (class-slots class)))
  362. (define-macro (restore class slots . exps)
  363. "(restore CLASS (SLOT-NAME1 ...) EXP1 ...)"
  364. `(let ((o ((@@ (oop goops) %allocate-instance) ,class '())))
  365. (for-each (lambda (name val)
  366. (slot-set! o name val))
  367. ',slots
  368. (list ,@exps))
  369. o))
  370. (define-method (enumerate! (o <object>) env)
  371. (get-set-for-each (lambda (get set)
  372. (let ((val (get o)))
  373. (if (not (unbound? val))
  374. (enumerate-component! val env))))
  375. (class-of o))
  376. #f)
  377. (define-method (write-readably (o <object>) file env)
  378. (let ((class (class-of o)))
  379. (display "(restore " file)
  380. (display (class-name class) file)
  381. (display " (" file)
  382. (let ((slotdefs
  383. (filter (lambda (slotdef)
  384. (not (or (memq (slot-definition-allocation slotdef)
  385. '(#:class #:each-subclass))
  386. (and (slot-bound? o (slot-definition-name slotdef))
  387. (excluded?
  388. (slot-ref o (slot-definition-name slotdef))
  389. env)))))
  390. (class-slots class))))
  391. (if (not (null? slotdefs))
  392. (begin
  393. (display (slot-definition-name (car slotdefs)) file)
  394. (for-each (lambda (slotdef)
  395. (display #\space file)
  396. (display (slot-definition-name slotdef) file))
  397. (cdr slotdefs)))))
  398. (display #\) file)
  399. (access-for-each (lambda (name aname get set)
  400. (display #\space file)
  401. (let ((val (get o)))
  402. (cond ((unbound? val)
  403. (display '(make-unbound) file))
  404. ((excluded? val env))
  405. (else
  406. (if (literal? val env)
  407. (display #\' file))
  408. (write-component val
  409. (if aname
  410. `(set! (,aname ,o) ,val)
  411. `(slot-set! ,o ',name ,val))
  412. file env)))))
  413. class)
  414. (display #\) file)))
  415. ;;;
  416. ;;; Classes
  417. ;;;
  418. ;;; Currently, we don't support reading in class objects
  419. ;;;
  420. (define-method (enumerate! (o <class>) env) #f)
  421. (define-method (write-readably (o <class>) file env)
  422. (display (class-name o) file))
  423. ;;;
  424. ;;; Generics
  425. ;;;
  426. ;;; Currently, we don't support reading in generic functions
  427. ;;;
  428. (define-method (enumerate! (o <generic>) env) #f)
  429. (define-method (write-readably (o <generic>) file env)
  430. (display (generic-function-name o) file))
  431. ;;;
  432. ;;; Method
  433. ;;;
  434. ;;; Currently, we don't support reading in methods
  435. ;;;
  436. (define-method (enumerate! (o <method>) env) #f)
  437. (define-method (write-readably (o <method>) file env)
  438. (goops-error "No read-syntax for <method> defined"))
  439. ;;;
  440. ;;; Environments
  441. ;;;
  442. (define-class <environment> ()
  443. (object-info #:accessor object-info
  444. #:init-form (make-hash-table 61))
  445. (excluded #:accessor excluded
  446. #:init-form (make-hash-table 61))
  447. (pass-2? #:accessor pass-2?
  448. #:init-value #f)
  449. (ref-stack #:accessor ref-stack
  450. #:init-value '())
  451. (objects #:accessor objects
  452. #:init-value '())
  453. (pre-defines #:accessor pre-defines
  454. #:init-value '())
  455. (locals #:accessor locals
  456. #:init-value '())
  457. (stand-ins #:accessor stand-ins
  458. #:init-value '())
  459. (post-defines #:accessor post-defines
  460. #:init-value '())
  461. (patchers #:accessor patchers
  462. #:init-value '())
  463. (multiple-bound #:accessor multiple-bound
  464. #:init-value '())
  465. )
  466. (define-method (initialize (env <environment>) initargs)
  467. (next-method)
  468. (cond ((get-keyword #:excluded initargs #f)
  469. => (lambda (excludees)
  470. (for-each (lambda (e)
  471. (hashq-create-handle! (excluded env) e #f))
  472. excludees)))))
  473. (define-method (object-info o env)
  474. (hashq-ref (object-info env) o))
  475. (define-method ((setter object-info) o env x)
  476. (hashq-set! (object-info env) o x))
  477. (define (excluded? o env)
  478. (hashq-get-handle (excluded env) o))
  479. (define (add-patcher! patcher env)
  480. (set! (patchers env) (cons patcher (patchers env))))
  481. (define (push-ref! o env)
  482. (set! (ref-stack env) (cons o (ref-stack env))))
  483. (define (pop-ref! env)
  484. (set! (ref-stack env) (cdr (ref-stack env))))
  485. (define (container env)
  486. (car (ref-stack env)))
  487. (define-class <object-info> ()
  488. (visiting #:accessor visiting
  489. #:init-value #f)
  490. (binding #:accessor binding
  491. #:init-value #f)
  492. (literal? #:accessor literal?
  493. #:init-value #f)
  494. )
  495. (define visiting? visiting)
  496. (define-method (binding (info <boolean>))
  497. #f)
  498. (define-method (binding o env)
  499. (binding (object-info o env)))
  500. (define binding? binding)
  501. (define-method (literal? (info <boolean>))
  502. #t)
  503. ;;; Note that this method is intended to be used only during the
  504. ;;; writing pass
  505. ;;;
  506. (define-method (literal? o env)
  507. (or (immediate? o)
  508. (excluded? o env)
  509. (let ((info (object-info o env)))
  510. ;; write-component sets all bindings first to #:defining,
  511. ;; then to #:defined
  512. (and (or (not (binding? info))
  513. ;; we might be using `literal?' in a write-readably method
  514. ;; to query about the object being defined
  515. (and (eq? (visiting info) #:defining)
  516. (null? (cdr (ref-stack env)))))
  517. (literal? info)))))
  518. ;;;
  519. ;;; Enumeration
  520. ;;;
  521. ;;; Enumeration has two passes.
  522. ;;;
  523. ;;; Pass 1: Detect common substructure, circular references and order
  524. ;;;
  525. ;;; Pass 2: Detect literals
  526. (define (enumerate-component! o env)
  527. (cond ((immediate? o) #t)
  528. ((readable? o) #f)
  529. ((excluded? o env) #t)
  530. ((pass-2? env)
  531. (let ((info (object-info o env)))
  532. (if (binding? info)
  533. ;; if circular reference, we print as a literal
  534. ;; (note that during pass-2, circular references are
  535. ;; forward references, i.e. *not* yet marked with #:pass-2
  536. (not (eq? (visiting? info) #:pass-2))
  537. (and (enumerate! o env)
  538. (begin
  539. (set! (literal? info) #t)
  540. #t)))))
  541. ((object-info o env)
  542. => (lambda (info)
  543. (set! (binding info) #t)
  544. (if (visiting? info)
  545. ;; circular reference--mark container
  546. (set! (binding (object-info (container env) env)) #t))))
  547. (else
  548. (let ((info (make <object-info>)))
  549. (set! (object-info o env) info)
  550. (push-ref! o env)
  551. (set! (visiting? info) #t)
  552. (enumerate! o env)
  553. (set! (visiting? info) #f)
  554. (pop-ref! env)
  555. (set! (objects env) (cons o (objects env)))))))
  556. ;;;
  557. ;;; Main engine
  558. ;;;
  559. (define binding-name car)
  560. (define binding-object cdr)
  561. (define (pass-1! alist env)
  562. ;; Determine object order and necessary bindings
  563. (for-each (lambda (binding)
  564. (enumerate-component! (binding-object binding) env))
  565. alist))
  566. (define (make-local i)
  567. (string->symbol (string-append "%o" (number->string i))))
  568. (define (name-bindings! alist env)
  569. ;; Name top-level bindings
  570. (for-each (lambda (b)
  571. (let ((o (binding-object b)))
  572. (if (not (or (immediate? o)
  573. (readable? o)
  574. (excluded? o env)))
  575. (let ((info (object-info o env)))
  576. (if (symbol? (binding info))
  577. ;; already bound to a variable
  578. (set! (multiple-bound env)
  579. (acons (binding info)
  580. (binding-name b)
  581. (multiple-bound env)))
  582. (set! (binding info)
  583. (binding-name b)))))))
  584. alist)
  585. ;; Name rest of bindings and create stand-in and definition lists
  586. (let post-loop ((ls (objects env))
  587. (post-defs '()))
  588. (cond ((or (null? ls)
  589. (eq? (binding (car ls) env) #t))
  590. (set! (post-defines env) post-defs)
  591. (set! (objects env) ls))
  592. ((not (binding (car ls) env))
  593. (post-loop (cdr ls) post-defs))
  594. (else
  595. (post-loop (cdr ls) (cons (car ls) post-defs)))))
  596. (let pre-loop ((ls (reverse (objects env)))
  597. (i 0)
  598. (pre-defs '())
  599. (locs '())
  600. (sins '()))
  601. (if (null? ls)
  602. (begin
  603. (set! (pre-defines env) (reverse pre-defs))
  604. (set! (locals env) (reverse locs))
  605. (set! (stand-ins env) (reverse sins)))
  606. (let ((info (object-info (car ls) env)))
  607. (cond ((not (binding? info))
  608. (pre-loop (cdr ls) i pre-defs locs sins))
  609. ((boolean? (binding info))
  610. ;; local
  611. (set! (binding info) (make-local i))
  612. (pre-loop (cdr ls)
  613. (+ 1 i)
  614. pre-defs
  615. (cons (car ls) locs)
  616. sins))
  617. ((null? locs)
  618. (pre-loop (cdr ls)
  619. i
  620. (cons (car ls) pre-defs)
  621. locs
  622. sins))
  623. (else
  624. (let ((real-name (binding info)))
  625. (set! (binding info) (make-local i))
  626. (pre-loop (cdr ls)
  627. (+ 1 i)
  628. pre-defs
  629. (cons (car ls) locs)
  630. (acons (binding info) real-name sins)))))))))
  631. (define (pass-2! env)
  632. (set! (pass-2? env) #t)
  633. (for-each (lambda (o)
  634. (let ((info (object-info o env)))
  635. (set! (literal? info) (enumerate! o env))
  636. (set! (visiting info) #:pass-2)))
  637. (append (pre-defines env)
  638. (locals env)
  639. (post-defines env))))
  640. (define (write-define! name val literal? file)
  641. (display "(define " file)
  642. (display name file)
  643. (display #\space file)
  644. (if literal? (display #\' file))
  645. (write val file)
  646. (display ")\n" file))
  647. (define (write-empty-defines! file env)
  648. (for-each (lambda (stand-in)
  649. (write-define! (cdr stand-in) #f #f file))
  650. (stand-ins env))
  651. (for-each (lambda (o)
  652. (write-define! (binding o env) #f #f file))
  653. (post-defines env)))
  654. (define (write-definition! prefix o file env)
  655. (display prefix file)
  656. (let ((info (object-info o env)))
  657. (display (binding info) file)
  658. (display #\space file)
  659. (if (literal? info)
  660. (display #\' file))
  661. (push-ref! o env)
  662. (set! (visiting info) #:defining)
  663. (write-readably o file env)
  664. (set! (visiting info) #:defined)
  665. (pop-ref! env)
  666. (display #\) file)))
  667. (define (write-let*-head! file env)
  668. (display "(let* (" file)
  669. (write-definition! "(" (car (locals env)) file env)
  670. (for-each (lambda (o)
  671. (write-definition! "\n (" o file env))
  672. (cdr (locals env)))
  673. (display ")\n" file))
  674. (define (write-rebindings! prefix bindings file env)
  675. (for-each (lambda (patch)
  676. (display prefix file)
  677. (display (cdr patch) file)
  678. (display #\space file)
  679. (display (car patch) file)
  680. (display ")\n" file))
  681. bindings))
  682. (define (write-definitions! selector prefix file env)
  683. (for-each (lambda (o)
  684. (write-definition! prefix o file env)
  685. (newline file))
  686. (selector env)))
  687. (define (write-patches! prefix file env)
  688. (for-each (lambda (patch)
  689. (display prefix file)
  690. (display (let name-objects ((patcher patch))
  691. (cond ((binding patcher env)
  692. => (lambda (name)
  693. (cond ((assq name (stand-ins env))
  694. => cdr)
  695. (else name))))
  696. ((pair? patcher)
  697. (cons (name-objects (car patcher))
  698. (name-objects (cdr patcher))))
  699. (else patcher)))
  700. file)
  701. (newline file))
  702. (reverse (patchers env))))
  703. (define (write-immediates! alist file)
  704. (for-each (lambda (b)
  705. (if (immediate? (binding-object b))
  706. (write-define! (binding-name b)
  707. (binding-object b)
  708. #t
  709. file)))
  710. alist))
  711. (define (write-readables! alist file env)
  712. (let ((written '()))
  713. (for-each (lambda (b)
  714. (cond ((not (readable? (binding-object b))))
  715. ((assq (binding-object b) written)
  716. => (lambda (p)
  717. (set! (multiple-bound env)
  718. (acons (cdr p)
  719. (binding-name b)
  720. (multiple-bound env)))))
  721. (else
  722. (write-define! (binding-name b)
  723. (readable-expression (binding-object b))
  724. #f
  725. file)
  726. (set! written (acons (binding-object b)
  727. (binding-name b)
  728. written)))))
  729. alist)))
  730. (define-method (save-objects (alist <pair>) (file <string>) . rest)
  731. (let ((port (open-output-file file)))
  732. (apply save-objects alist port rest)
  733. (close-port port)
  734. *unspecified*))
  735. (define-method (save-objects (alist <pair>) (file <output-port>) . rest)
  736. (let ((excluded (if (>= (length rest) 1) (car rest) '()))
  737. (uses (if (>= (length rest) 2) (cadr rest) '())))
  738. (let ((env (make <environment> #:excluded excluded)))
  739. (pass-1! alist env)
  740. (name-bindings! alist env)
  741. (pass-2! env)
  742. (if (not (null? uses))
  743. (begin
  744. (write `(use-modules ,@uses) file)
  745. (newline file)))
  746. (write-immediates! alist file)
  747. (if (null? (locals env))
  748. (begin
  749. (write-definitions! post-defines "(define " file env)
  750. (write-patches! "" file env))
  751. (begin
  752. (write-definitions! pre-defines "(define " file env)
  753. (write-empty-defines! file env)
  754. (write-let*-head! file env)
  755. (write-rebindings! " (set! " (stand-ins env) file env)
  756. (write-definitions! post-defines " (set! " file env)
  757. (write-patches! " " file env)
  758. (display " )\n" file)))
  759. (write-readables! alist file env)
  760. (write-rebindings! "(define " (reverse (multiple-bound env)) file env))))
  761. (define-method (load-objects (file <string>))
  762. (let* ((port (open-input-file file))
  763. (objects (load-objects port)))
  764. (close-port port)
  765. objects))
  766. (define iface (module-public-interface (current-module)))
  767. (define-method (load-objects (file <input-port>))
  768. (let ((m (make-module)))
  769. (module-use! m the-scm-module)
  770. (module-use! m iface)
  771. (save-module-excursion
  772. (lambda ()
  773. (set-current-module m)
  774. (let loop ((sexp (read file)))
  775. (if (not (eof-object? sexp))
  776. (begin
  777. (eval sexp m)
  778. (loop (read file)))))))
  779. (module-map (lambda (name var)
  780. (cons name (variable-ref var)))
  781. m)))