read-image-util.scm 7.7 KB

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