read-image-util.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: David Frese, Marcus Crestani, Mike Sperber
  3. ; Utilities for reading images.
  4. (define (init-read-image!)
  5. (set! *status* (enum errors no-errors))
  6. (set! *eof?* #f))
  7. (define *status* (enum errors no-errors))
  8. (define *eof?* #f)
  9. (define (get-status) *status*)
  10. (define (is-eof?) *eof?*)
  11. ;; Setters
  12. (define (set-status! status)
  13. (set! *status* status))
  14. (define-syntax read-check
  15. (syntax-rules ()
  16. ((read-check exp)
  17. (read-check exp -1))
  18. ((read-check exp losing-value)
  19. (let ((lose losing-value))
  20. (if (or (error? (get-status))
  21. (is-eof?))
  22. lose
  23. (receive (thing eof? status)
  24. exp
  25. (cond (eof?
  26. (set! *eof?* #t)
  27. lose)
  28. ((eq? status (enum errors no-errors))
  29. thing)
  30. (else
  31. (set! *eof?* #t)
  32. (if (eq? *status* (enum errors no-errors))
  33. (set! *status* status))
  34. lose))))))))
  35. (define (got-error? expr)
  36. (not (and (= expr 0)
  37. (not (error? *status*)))))
  38. (define (image-read-block port address need)
  39. (receive (got eof? status)
  40. (read-block port address need)
  41. (cond ((error? status)
  42. (set! *status* status)
  43. (values #f "Error reading from image file"))
  44. (eof?
  45. (values #f "Premature EOF when reading image file"))
  46. ((< got need)
  47. (values #f "Read returned too few bytes"))
  48. (else
  49. (values #t "")))))
  50. ;; Reader exception handler
  51. (define (read-lost message port)
  52. (error-message message)
  53. (if (error? (get-status))
  54. (begin
  55. (error-message (error-string (get-status)))
  56. (unspecific)))
  57. (if (error? (close-input-port port))
  58. (begin
  59. (error-message "Error closing image file")
  60. (unspecific)))
  61. -1)
  62. ; The page character is used to mark the ends of the user and prelude sections
  63. ; of image files.
  64. (define page-character (ascii->char 12))
  65. (define (read-page port)
  66. (read-this-character page-character port))
  67. (define (read-newline port)
  68. (read-this-character #\newline port))
  69. (define (read-this-character char port)
  70. (let loop ()
  71. (receive (ch eof? status)
  72. (read-char port)
  73. (cond ((or eof? (error? status))
  74. (values -1 eof? status))
  75. ((char=? char ch)
  76. (values -1 #f status))
  77. (else
  78. (loop))))))
  79. ; ABCD => DCBA
  80. ; memory intensive, but independent of Scheme's integer size
  81. (define (reverse-descriptor-byte-order! addr)
  82. (do ((i 0 (+ i 1))
  83. (j (- bytes-per-cell 1) (- j 1)))
  84. ((>= i j))
  85. (let* ((addr-a (address+ addr i))
  86. (addr-b (address+ addr j))
  87. (byte-a (fetch-byte addr-a))
  88. (byte-b (fetch-byte addr-b)))
  89. (store-byte! addr-a byte-b)
  90. (store-byte! addr-b byte-a))))
  91. (define (reverse-byte-order! start end)
  92. (error-message "Correcting byte order of resumed image.")
  93. (let loop ((ptr start))
  94. (if (address< ptr end)
  95. (begin
  96. (reverse-descriptor-byte-order! ptr)
  97. (loop (let ((value (fetch ptr))
  98. (next (address1+ ptr)))
  99. (if (b-vector-header? value)
  100. (address+ next (header-length-in-a-units value))
  101. next)))))))
  102. (define (adjust descriptor delta)
  103. (if (stob? descriptor)
  104. (address->stob-descriptor
  105. (address+ (address-after-header descriptor) delta))
  106. descriptor))
  107. (define (relocate-image delta start end)
  108. (let loop ((ptr start))
  109. (if (address< ptr end)
  110. (let ((d (adjust (fetch ptr) delta)))
  111. (store! ptr d)
  112. (if (b-vector-header? d)
  113. (loop (address+ (address1+ ptr) (header-length-in-a-units d)))
  114. (loop (address1+ ptr)))))))
  115. (define (alloc-space size-in-byte)
  116. (s48-allocate-traced+gc size-in-byte))
  117. ; Add DELTA to all hidden pointers.
  118. (define (table-relocator-two-space foo-next set-foo-next!)
  119. (lambda (table delta)
  120. (relocate-table table
  121. (lambda (address)
  122. (+ address delta))
  123. foo-next
  124. set-foo-next!)))
  125. (define relocate-symbol-table-two-space!
  126. (table-relocator-two-space vm-symbol-next
  127. vm-set-symbol-next!))
  128. (define relocate-binding-table-two-space!
  129. (table-relocator-two-space shared-binding-next
  130. set-shared-binding-next!))
  131. ; Variables
  132. ;; Common (from image)
  133. (define *startup-procedure* 0) ; 0 is for the type checker
  134. (define *symbols* 0)
  135. (define *imported-bindings* 0)
  136. (define *exported-bindings* 0)
  137. (define *resumer-records* 0)
  138. (define (get-startup-procedure) *startup-procedure*)
  139. (define (get-symbols) *symbols*)
  140. (define (get-imported-bindings) *imported-bindings*)
  141. (define (get-exported-bindings) *exported-bindings*)
  142. (define (get-resumer-records) *resumer-records*)
  143. (define (set-startup-procedure! expr)
  144. (set! *startup-procedure* expr))
  145. (define (set-symbols! expr)
  146. (set! *symbols* expr))
  147. (define (set-imported-bindings! expr)
  148. (set! *imported-bindings* expr))
  149. (define (set-exported-bindings! expr)
  150. (set! *exported-bindings* expr))
  151. (define (set-resumer-records! expr)
  152. (set! *resumer-records* expr))
  153. ; Two-space format
  154. (define *img-start-addr* null-address) ; image start address
  155. (define *img-end-addr* null-address) ; image end address
  156. (define *img-heap-size* 0) ; heap size from the image
  157. (define (get-img-start-addr) *img-start-addr*)
  158. (define (get-img-end-addr) *img-end-addr*)
  159. (define (get-img-heap-size) *img-heap-size*)
  160. (define (set-img-start-addr! value)
  161. (set! *img-start-addr* value))
  162. (define (set-img-end-addr! value)
  163. (set! *img-end-addr* value))
  164. (define (set-img-heap-size! value)
  165. (set! *img-heap-size* value))
  166. ; BIBOP format
  167. (define *small-img-start-addr* null-address) ; small-image start address
  168. (define *small-img-hp-addr* null-address) ; small-image hp address
  169. (define *small-img-end-addr* null-address) ; small-image end address
  170. (define *small-img-heap-size* 0) ; small-heap size from the image
  171. (define (get-small-img-start-addr) *small-img-start-addr*)
  172. (define (get-small-img-hp-addr) *small-img-hp-addr*)
  173. (define (get-small-img-end-addr) *small-img-end-addr*)
  174. (define (get-small-img-heap-size) *small-img-heap-size*)
  175. (define (set-small-img-start-addr! value)
  176. (set! *small-img-start-addr* value))
  177. (define (set-small-img-hp-addr! value)
  178. (set! *small-img-hp-addr* value))
  179. (define (set-small-img-end-addr! value)
  180. (set! *small-img-end-addr* value))
  181. (define (set-small-img-heap-size! value)
  182. (set! *small-img-heap-size* value))
  183. (define *large-img-start-addr* null-address) ; lagre-image start address
  184. (define *large-img-hp-addr* null-address) ; large-image hp address
  185. (define *large-img-end-addr* null-address) ; large-image end address
  186. (define *large-img-heap-size* 0) ; large-heap size from the image
  187. (define (get-large-img-start-addr) *large-img-start-addr*)
  188. (define (get-large-img-hp-addr) *large-img-hp-addr*)
  189. (define (get-large-img-end-addr) *large-img-end-addr*)
  190. (define (get-large-img-heap-size) *large-img-heap-size*)
  191. (define (set-large-img-start-addr! value)
  192. (set! *large-img-start-addr* value))
  193. (define (set-large-img-hp-addr! value)
  194. (set! *large-img-hp-addr* value))
  195. (define (set-large-img-end-addr! value)
  196. (set! *large-img-end-addr* value))
  197. (define (set-large-img-heap-size! value)
  198. (set! *large-img-heap-size* value))
  199. (define *weaks-img-start-addr* null-address) ; weaks-image start address
  200. (define *weaks-img-hp-addr* null-address) ; weaks-image hp address
  201. (define *weaks-img-end-addr* null-address) ; weaks-image end address
  202. (define *weaks-img-heap-size* 0) ; weaks-heap size from the image
  203. (define (get-weaks-img-start-addr) *weaks-img-start-addr*)
  204. (define (get-weaks-img-hp-addr) *weaks-img-hp-addr*)
  205. (define (get-weaks-img-end-addr) *weaks-img-end-addr*)
  206. (define (get-weaks-img-heap-size) *weaks-img-heap-size*)
  207. (define (set-weaks-img-start-addr! value)
  208. (set! *weaks-img-start-addr* value))
  209. (define (set-weaks-img-hp-addr! value)
  210. (set! *weaks-img-hp-addr* value))
  211. (define (set-weaks-img-end-addr! value)
  212. (set! *weaks-img-end-addr* value))
  213. (define (set-weaks-img-heap-size! value)
  214. (set! *weaks-img-heap-size* value))