read-image.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ;; Image reader that reads images into the BIBOP GC
  3. (define (really-read-image format reverse-byte-order? port)
  4. (enum-case image-format format
  5. ((two-space)
  6. (really-read-image-portable format reverse-byte-order? port))
  7. ((bibop)
  8. (really-read-image-bibop-native reverse-byte-order? port))
  9. (else
  10. (read-lost "invalid image format" port))))
  11. (define (really-read-image-bibop-native reverse-byte-order? port)
  12. ;; debugging
  13. ;;(describe-small)
  14. ;;(describe-large)
  15. ;;(describe-weaks)
  16. (let* ((small-delta (address-difference (s48-get-new-small-start-addr)
  17. (get-small-img-start-addr)))
  18. (small-new-hp (address+ (get-small-img-hp-addr) small-delta))
  19. (large-delta (address-difference (s48-get-new-large-start-addr)
  20. (get-large-img-start-addr)))
  21. (large-new-hp (address+ (get-large-img-hp-addr) large-delta))
  22. (weaks-delta (address-difference (s48-get-new-weaks-start-addr)
  23. (get-weaks-img-start-addr)))
  24. (weaks-new-hp (address+ (get-weaks-img-hp-addr) weaks-delta)))
  25. (cond
  26. ((got-error? (really-read-weaks-image port))
  27. (read-lost "error reading weaks area from image" port))
  28. ((got-error? (really-read-large-image port))
  29. (read-lost "error reading large area from image" port))
  30. ((got-error? (really-read-small-image port))
  31. (read-lost "error reading small area from image" port))
  32. (else
  33. (receive (ch eof? status)
  34. (read-char port)
  35. (cond ((error? status)
  36. (read-lost "Error reading from image file" port))
  37. ((not eof?)
  38. (read-lost "Image file has extraneous data after image" port))
  39. ((error? (close-input-port port))
  40. (read-lost "Error closing image file" port))
  41. (else
  42. (if reverse-byte-order?
  43. (begin
  44. (reverse-byte-order! (s48-get-new-small-start-addr) small-new-hp)
  45. (reverse-byte-order! (s48-get-new-large-start-addr) large-new-hp)
  46. (reverse-byte-order! (s48-get-new-weaks-start-addr) weaks-new-hp)))
  47. ;; here we have to adjust along the new sizes of the areas
  48. ;; TODO: the adjustment must depend on where the
  49. ;; addresses point to (small, large, weaks). If the
  50. ;; delta's differ, it won't work like it is, so for now
  51. ;; we make sure they are equal
  52. (if (not (and (= small-delta large-delta)
  53. (= large-delta weaks-delta)))
  54. (error "Bug: Cannot load image, because the deltas of all parts aren't equal. Notify the authors."))
  55. (if (not (= small-delta 0))
  56. (begin
  57. (set-startup-procedure! (adjust (get-startup-procedure) small-delta))
  58. (set-symbols! (adjust (get-symbols) small-delta))
  59. (set-imported-bindings! (adjust (get-imported-bindings) small-delta))
  60. (set-exported-bindings! (adjust (get-exported-bindings) small-delta))
  61. (set-resumer-records! (adjust (get-resumer-records) small-delta))
  62. (relocate-symbol-table-two-space! (get-symbols) small-delta)
  63. (relocate-binding-table-two-space! (get-imported-bindings) small-delta)
  64. (relocate-binding-table-two-space! (get-exported-bindings) small-delta)
  65. (relocate-image small-delta (s48-get-new-small-start-addr) small-new-hp)))
  66. (if (not (= large-delta 0))
  67. (begin
  68. ;; debugging
  69. ;;(notify-large-delta)
  70. (relocate-image large-delta (s48-get-new-large-start-addr) large-new-hp)
  71. (unspecific)))
  72. (if (not (= weaks-delta 0))
  73. (begin
  74. ;; debugging
  75. ;;(notify-weaks-delta)
  76. (relocate-image weaks-delta (s48-get-new-weaks-start-addr) weaks-new-hp)
  77. (unspecific)))
  78. 0)))))))
  79. (define s48-get-new-small-end-addr
  80. (external "s48_get_new_small_end_addr" (=> () address)))
  81. (define s48-get-new-large-end-addr
  82. (external "s48_get_new_large_end_addr" (=> () address)))
  83. (define s48-get-new-weaks-end-addr
  84. (external "s48_get_new_weaks_end_addr" (=> () address)))
  85. (define s48-get-new-small-size
  86. (external "s48_get_new_small_size" (=> () integer)))
  87. (define s48-get-new-large-size
  88. (external "s48_get_new_large_size" (=> () integer)))
  89. (define s48-get-new-weaks-size
  90. (external "s48_get_new_weaks_size" (=> () integer)))
  91. (define (get-small-img-whole-size)
  92. (address-difference (get-small-img-end-addr)
  93. (get-small-img-start-addr)))
  94. (define (get-large-img-whole-size)
  95. (address-difference (get-large-img-end-addr)
  96. (get-large-img-start-addr)))
  97. (define (get-weaks-img-whole-size)
  98. (address-difference (get-weaks-img-end-addr)
  99. (get-weaks-img-start-addr)))
  100. (define (initialize-image-areas!)
  101. (s48-initialize-image-areas (get-small-img-whole-size)
  102. (address-difference (get-small-img-hp-addr)
  103. (get-small-img-start-addr))
  104. (get-large-img-whole-size)
  105. (address-difference (get-large-img-hp-addr)
  106. (get-large-img-start-addr))
  107. (get-weaks-img-whole-size)
  108. (address-difference (get-weaks-img-hp-addr)
  109. (get-weaks-img-start-addr))))
  110. ; debugging
  111. (define (describe-small)
  112. (write-out-newline)
  113. (write-out-string "-----------")
  114. (write-out-newline)
  115. (write-out-string "SMALL DATA ")
  116. (write-out-newline)
  117. (write-out-string "-----------")
  118. (write-out-newline)
  119. (write-out-string "(get-small-img-start-addr): ")
  120. (write-out-integer (address->integer (get-small-img-start-addr)))
  121. (write-out-newline)
  122. (write-out-string "(get-small-img-hp-addr): ")
  123. (write-out-integer (address->integer (get-small-img-hp-addr)))
  124. (write-out-newline)
  125. (write-out-string "(get-small-img-end-addr): ")
  126. (write-out-integer (address->integer (get-small-img-end-addr)))
  127. (write-out-newline)
  128. (write-out-string "PAGES: ")
  129. (write-out-integer (quotient (address-difference (get-small-img-end-addr)
  130. (get-small-img-start-addr))
  131. 4096))
  132. (write-out-newline)
  133. (write-out-string "(get-new-small-start-addr): ")
  134. (write-out-integer (address->integer (s48-get-new-small-start-addr)))
  135. (write-out-newline)
  136. (write-out-string "(get-new-small-end-addr): ")
  137. (write-out-integer (address->integer (s48-get-new-small-end-addr)))
  138. (write-out-newline)
  139. (write-out-string "PAGES: : ")
  140. (write-out-integer (quotient (address-difference (s48-get-new-small-end-addr)
  141. (s48-get-new-small-start-addr))
  142. 4096))
  143. (write-out-newline)
  144. (write-out-string "(get-new-small-size): ")
  145. (write-out-integer (s48-get-new-small-size))
  146. (write-out-newline)
  147. (write-out-string "(get-small-img-heap-size): ")
  148. (write-out-integer (cells->a-units (get-small-img-heap-size)))
  149. (write-out-newline)
  150. )
  151. (define (describe-large)
  152. (write-out-newline)
  153. (write-out-string "-----------")
  154. (write-out-newline)
  155. (write-out-string "LARGE DATA ")
  156. (write-out-newline)
  157. (write-out-string "-----------")
  158. (write-out-newline)
  159. (write-out-newline)
  160. (write-out-string "(get-large-img-start-addr): ")
  161. (write-out-integer (address->integer (get-large-img-start-addr)))
  162. (write-out-newline)
  163. (write-out-string "(get-large-img-end-addr): ")
  164. (write-out-integer (address->integer (get-large-img-end-addr)))
  165. (write-out-newline)
  166. (write-out-string "PAGES: ")
  167. (write-out-integer (quotient (address-difference (get-large-img-end-addr)
  168. (get-large-img-start-addr))
  169. 4096))
  170. (write-out-newline)
  171. (write-out-string "(get-new-large-start-addr): ")
  172. (write-out-integer (address->integer (s48-get-new-large-start-addr)))
  173. (write-out-newline)
  174. (write-out-string "(get-new-large-end-addr): ")
  175. (write-out-integer (address->integer (s48-get-new-large-end-addr)))
  176. (write-out-newline)
  177. (write-out-string "PAGES: : ")
  178. (write-out-integer (quotient (s48-get-new-large-size) 4096))
  179. (write-out-newline)
  180. (write-out-string "(get-new-large-size): ")
  181. (write-out-integer (s48-get-new-large-size))
  182. (write-out-newline)
  183. (write-out-string "(get-large-img-heap-size): ")
  184. (write-out-integer (cells->a-units (get-large-img-heap-size)))
  185. (write-out-newline))
  186. (define (describe-weaks)
  187. (write-out-newline)
  188. (write-out-string "-----------")
  189. (write-out-newline)
  190. (write-out-string "WEAKS DATA ")
  191. (write-out-newline)
  192. (write-out-string "-----------")
  193. (write-out-newline)
  194. (write-out-newline)
  195. (write-out-string "(get-weaks-img-start-addr): ")
  196. (write-out-integer (address->integer (get-weaks-img-start-addr)))
  197. (write-out-newline)
  198. (write-out-string "(get-weaks-img-end-addr): ")
  199. (write-out-integer (address->integer (get-weaks-img-end-addr)))
  200. (write-out-newline)
  201. (write-out-string "PAGES: ")
  202. (write-out-integer (quotient (address-difference (get-weaks-img-end-addr)
  203. (get-weaks-img-start-addr))
  204. 4096))
  205. (write-out-newline)
  206. (write-out-string "(get-new-weaks-start-addr): ")
  207. (write-out-integer (address->integer (s48-get-new-weaks-start-addr)))
  208. (write-out-newline)
  209. (write-out-string "(get-new-weaks-end-addr): ")
  210. (write-out-integer (address->integer (s48-get-new-weaks-end-addr)))
  211. (write-out-newline)
  212. (write-out-string "PAGES: : ")
  213. (write-out-integer (quotient (s48-get-new-weaks-size) 4096))
  214. (write-out-newline)
  215. (write-out-string "(get-new-weaks-size): ")
  216. (write-out-integer (s48-get-new-weaks-size))
  217. (write-out-newline)
  218. (write-out-string "(get-weaks-img-heap-size): ")
  219. (write-out-integer (cells->a-units (get-weaks-img-heap-size)))
  220. (write-out-newline))
  221. (define (notify-small-delta)
  222. (break "ADJUST SMALL DELTA"))
  223. (define (notify-large-delta)
  224. (break "ADJUST LARGE DELTA"))
  225. (define (notify-weaks-delta)
  226. (break "ADJUST WEAKS DELTA"))
  227. (define (break str)
  228. (write-out-newline)
  229. (write-out-string str)
  230. (write-out-newline))
  231. ; Utilities for portable reader
  232. (define (read-image-area new-start-addr img-heap-size port)
  233. (receive (okay? string)
  234. (image-read-block port
  235. new-start-addr
  236. (cells->a-units img-heap-size))
  237. (cond ((not okay?)
  238. (read-lost string port))
  239. (else 0))))
  240. (define (really-read-small-image port)
  241. (read-image-area (s48-get-new-small-start-addr)
  242. (get-small-img-heap-size)
  243. port))
  244. (define (really-read-large-image port)
  245. (read-image-area (s48-get-new-large-start-addr)
  246. (get-large-img-heap-size)
  247. port))
  248. (define (really-read-weaks-image port)
  249. (read-image-area (s48-get-new-weaks-start-addr)
  250. (get-weaks-img-heap-size)
  251. port))