write-image.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; BIBOP-specific part of the dumper
  3. (define small-image-start-address 0)
  4. (define large-image-start-address 0)
  5. (define weaks-image-start-address 0)
  6. (define (write-header resumer-records resume-proc image-descriptor port)
  7. ;; debugging
  8. ;;(display-small-image)
  9. ;;(display-large-image)
  10. ;;(display-weaks-image)
  11. (write-check (write-char #\newline port))
  12. (write-page)
  13. (write-check (write-char #\newline port))
  14. (write-check (write-string architecture-version port))
  15. (write-check (write-char #\newline port))
  16. (write-check (write-integer (enum image-format bibop) port))
  17. (write-check (write-char #\newline port))
  18. (write-header-integer bytes-per-cell)
  19. ;; small area addresses
  20. (write-header-integer (a-units->cells (address->integer *small-image-begin*)))
  21. (write-header-integer (a-units->cells (address->integer *small-image-hp*)))
  22. ;; (write-header-integer (a-units->cells (address->integer *small-image-end*)))
  23. ;; large area addresses
  24. (write-header-integer (a-units->cells (address->integer *large-image-begin*)))
  25. (write-header-integer (a-units->cells (address->integer *large-image-hp*)))
  26. ;; (write-header-integer (a-units->cells (address->integer *large-image-end*)))
  27. ;; weaks area addresses
  28. (write-header-integer (a-units->cells (address->integer *weaks-image-begin*)))
  29. (write-header-integer (a-units->cells (address->integer *weaks-image-hp*)))
  30. (write-header-integer (a-units->cells (address->integer *weaks-image-end*)))
  31. (write-header-integer (image-descriptor (s48-symbol-table)))
  32. (write-header-integer (image-descriptor (s48-imported-bindings)))
  33. (write-header-integer (image-descriptor (s48-exported-bindings)))
  34. (write-header-integer resumer-records)
  35. (write-header-integer resume-proc)
  36. (write-page))
  37. (define (begin-making-image/gc-specific)
  38. ; (let ((cand-addr (get-candidate-address)))
  39. ; (if (not (null-address? cand-addr))
  40. ; (set! small-image-start-address (address->integer cand-addr))))
  41. ;; There seems to be no problem with a possible null-address - David
  42. (set! small-image-start-address (address->integer (get-candidate-address)))
  43. (set! *small-image-begin* (integer->address small-image-start-address))
  44. (set! *small-image-hp* (integer->address small-image-start-address))
  45. (set! *small-image-end* null-address)
  46. (set! *large-image-begin* (integer->address large-image-start-address))
  47. (set! *large-image-hp* (integer->address large-image-start-address))
  48. (set! *large-image-end* null-address)
  49. (set! *weaks-image-begin* (integer->address weaks-image-start-address))
  50. (set! *weaks-image-hp* (integer->address weaks-image-start-address))
  51. (set! *weaks-image-end* null-address)
  52. ;; Initialize the areas
  53. (set! *bibop-areas* (make-bibop-areas))
  54. (set! *traced-last-stob?* #f))
  55. (define (note-traced-last-stob!)
  56. (set! *traced-last-stob?* #t))
  57. (define (allocate-new-image-object stob)
  58. (let ((new-descriptor (image-alloc
  59. (s48-area-type-size stob)
  60. (header-length-in-a-units
  61. (stob-header stob)))))
  62. (values new-descriptor
  63. (make-image-location new-descriptor))))
  64. (define (finalize-new-image-object stob)
  65. (set! *stob-table-obj-nr* (+ *stob-table-obj-nr* 1))
  66. (update-bibop-areas! (s48-area-type-size stob) stob))
  67. (define (small-image-size)
  68. (address-difference *small-image-hp* *small-image-begin*))
  69. (define (large-image-size)
  70. (address-difference *large-image-hp* *large-image-begin*))
  71. (define (weaks-image-size)
  72. (address-difference *weaks-image-hp* *weaks-image-begin*))
  73. (define (get-candidate-address)
  74. (s48-get-new-small-start-addr))
  75. (define *small-image-begin*) ; Starting address (small part image)
  76. (define *small-image-hp*) ; Current ending address (small part image)
  77. (define *small-image-end*) ; Ending address of the small area (BIBOP)
  78. (define *large-image-begin*) ; Starting address (large part image)
  79. (define *large-image-hp*) ; Current ending address (large part image)
  80. (define *large-image-end*) ; Ending address of the large area (BIBOP)
  81. (define *weaks-image-begin*) ; Starting address (weaks part image)
  82. (define *weaks-image-hp*) ; Current ending address (weaks part image)
  83. (define *weaks-image-end*) ; Ending address of the weaks area (BIBOP)
  84. (define *bibop-areas*) ; Record to keep the stobs in 3 areas (small, large, weaks)
  85. (define *traced-last-stob?* #f) ; Flag to mark the last traced stob in bibop-areas
  86. ; Each STOB's new-descriptor becomes a descriptor-address from its own
  87. ; image part (small, large, weaks). Each stob will be written in
  88. ; *bibop-areas* container for the last scan to write the image
  89. ; properly.
  90. (define s48-area-type-size
  91. (external "s48_area_type_size" (=> (integer) integer)))
  92. (define (write-image-areas first-stob stob-table write-stob)
  93. (write-weaks-area write-stob)
  94. (write-large-area write-stob)
  95. (write-small-area write-stob)
  96. (unspecific))
  97. ;; --------------------------------------------------------------------
  98. ;; Bibop Areas
  99. ;; --------------------------------------------------------------------
  100. ;; In this container are collected the stobs - before they 'll
  101. ;; inserted into the *stob-table* - categorized among the heap area
  102. ;; they belong to: small, large and weaks.
  103. ;; - During 'make-image', this container will be scanned to adjust the
  104. ;; descriptors in *stob-table*
  105. ;; - During 'write-image', this container will be scanned to write the
  106. ;; stobs from *stob-table* for each area separate
  107. ;; --------------------------------------------------------------------
  108. (define-record-type bibop-areas :bibop-areas
  109. (really-make-bibop-areas small small-index
  110. large large-index
  111. weaks weaks-index)
  112. (small (^ integer) bibop-areas-small set-bibop-areas-small!)
  113. (small-index integer bibop-areas-small-index set-bibop-areas-small-index!)
  114. (large (^ integer) bibop-areas-large set-bibop-areas-large!)
  115. (large-index integer bibop-areas-large-index set-bibop-areas-large-index!)
  116. (weaks (^ integer) bibop-areas-weaks set-bibop-areas-weaks!)
  117. (weaks-index integer bibop-areas-weaks-index set-bibop-areas-weaks-index!))
  118. ;; How big are the vectors (each area) wich hold the stobs
  119. (define *initial-stob-obj-nr* (shift-left 1 20))
  120. (define (make-bibop-areas)
  121. (let ((small (make-vector *initial-stob-obj-nr* 0))
  122. (large (make-vector *initial-stob-obj-nr* 0))
  123. (weaks (make-vector *initial-stob-obj-nr* 0)))
  124. (really-make-bibop-areas small 0
  125. large 0
  126. weaks 0)))
  127. (define (deallocate-bibop-areas)
  128. (deallocate (bibop-areas-small (get-bibop-areas)))
  129. (deallocate (bibop-areas-large (get-bibop-areas)))
  130. (deallocate (bibop-areas-weaks (get-bibop-areas)))
  131. (deallocate (get-bibop-areas)))
  132. (define (get-bibop-areas)
  133. *bibop-areas*)
  134. (define (update-bibop-areas! type-size stob)
  135. (enum-case area-type-size type-size
  136. ((small)
  137. (insert-small-area! stob))
  138. ((large)
  139. (insert-large-area! stob))
  140. ((weaks)
  141. (insert-weaks-area! stob))
  142. (else (error "Unexpected area type size!"))))
  143. (define (insert-small-area! stob)
  144. (if *traced-last-stob?*
  145. ;; Mark this index as the last traced (means not to be written)
  146. ;; But adjustment must be done to all of them
  147. (let ((i (bibop-areas-small-index (get-bibop-areas))))
  148. (assert (not (= i *initial-stob-obj-nr*)))
  149. (vector-set! (bibop-areas-small (get-bibop-areas)) i false)
  150. (set-bibop-areas-small-index!
  151. (get-bibop-areas)
  152. (+ (bibop-areas-small-index (get-bibop-areas)) 1))))
  153. ;; Now insert the stob
  154. (let ((i (bibop-areas-small-index (get-bibop-areas))))
  155. (assert (not (= i *initial-stob-obj-nr*)))
  156. (vector-set! (bibop-areas-small (get-bibop-areas)) i stob)
  157. (set-bibop-areas-small-index!
  158. (get-bibop-areas)
  159. (+ (bibop-areas-small-index (get-bibop-areas)) 1))))
  160. (define (insert-large-area! stob)
  161. (if *traced-last-stob?*
  162. ;; Mark this index as the last traced (means not to be written)
  163. ;; But adjustment must be done to all of them
  164. (let ((i (bibop-areas-large-index (get-bibop-areas))))
  165. (assert (not (= i *initial-stob-obj-nr*)))
  166. (vector-set! (bibop-areas-large (get-bibop-areas)) i false)
  167. (set-bibop-areas-large-index!
  168. (get-bibop-areas)
  169. (+ (bibop-areas-large-index (get-bibop-areas)) 1))))
  170. ;; Now insert the stob
  171. (let ((i (bibop-areas-large-index (get-bibop-areas))))
  172. (assert (not (= i *initial-stob-obj-nr*)))
  173. (vector-set! (bibop-areas-large (get-bibop-areas)) i stob)
  174. (set-bibop-areas-large-index!
  175. (get-bibop-areas)
  176. (+ (bibop-areas-large-index (get-bibop-areas)) 1))))
  177. (define (insert-weaks-area! stob)
  178. (if *traced-last-stob?*
  179. ;; Mark this index as the last traced (means not to be written)
  180. ;; But adjustment must be done to all of them
  181. (let ((i (bibop-areas-weaks-index (get-bibop-areas))))
  182. (assert (not (= i *initial-stob-obj-nr*)))
  183. (vector-set! (bibop-areas-weaks (get-bibop-areas)) i false)
  184. (set-bibop-areas-weaks-index!
  185. (get-bibop-areas)
  186. (+ (bibop-areas-weaks-index (get-bibop-areas)) 1))))
  187. ;; Now insert the stob
  188. (let ((i (bibop-areas-weaks-index (get-bibop-areas))))
  189. (assert (not (= i *initial-stob-obj-nr*)))
  190. (vector-set! (bibop-areas-weaks (get-bibop-areas)) i stob)
  191. (set-bibop-areas-weaks-index!
  192. (get-bibop-areas)
  193. (+ (bibop-areas-weaks-index (get-bibop-areas)) 1))))
  194. ; - The descriptors of the small objects have already been adjusted
  195. ; by small-image-start-address
  196. ; - The descriptors of the large and weaks objects will be adjusted
  197. ; by the last address of the future bibop area (rounded pages)
  198. (define (adjust-descriptors! stob-table)
  199. (calculate-bibop-small-image-end!)
  200. (calculate-bibop-large-image-end!)
  201. (calculate-bibop-weaks-image-end!)
  202. (adjust-large-area-descriptors! stob-table)
  203. (adjust-weaks-area-descriptors! stob-table))
  204. (define log-bytes-per-page 12)
  205. (define bytes-per-page (shift-left 1 log-bytes-per-page))
  206. (define (bytes->pages n)
  207. (arithmetic-shift-right (+ n (- bytes-per-page 1)) log-bytes-per-page))
  208. (define (pages->bytes n)
  209. (shift-left n log-bytes-per-page))
  210. (define (calculate-bibop-small-image-end!)
  211. (set! *small-image-end*
  212. (address+ *small-image-begin*
  213. (pages->bytes (bytes->pages (if (= 0 (small-image-size))
  214. 1
  215. (small-image-size)))))))
  216. (define (calculate-bibop-large-image-end!)
  217. (set! *large-image-end*
  218. (address+ *large-image-begin*
  219. (pages->bytes (bytes->pages (if (= 0 (large-image-size))
  220. 1
  221. (large-image-size)))))))
  222. (define (calculate-bibop-weaks-image-end!)
  223. (set! *weaks-image-end*
  224. (address+ *weaks-image-begin*
  225. (pages->bytes (bytes->pages (if (= 0 (weaks-image-size))
  226. 1
  227. (weaks-image-size)))))))
  228. (define (adjust-large-area-descriptors! stob-table)
  229. (do ((i 0 (+ i 1)))
  230. ((= i (bibop-areas-large-index (get-bibop-areas))))
  231. (let ((stob (vector-ref (bibop-areas-large (get-bibop-areas)) i)))
  232. ;; If this is 'false'(= the marked index as last stob) jump it
  233. (if (and (stob? stob)
  234. (not (= stob false)))
  235. (let ((image-location (table-ref stob-table stob)))
  236. (set-image-location-new-descriptor!
  237. image-location
  238. (address->stob-descriptor
  239. (address+ *small-image-end*
  240. (address->integer
  241. (address-after-header
  242. (image-location-new-descriptor image-location))))))))))
  243. ;; Move the pointers of the large area
  244. (set! *large-image-begin* (address+ *small-image-end*
  245. (address->integer *large-image-begin*)))
  246. (set! *large-image-hp* (address+ *small-image-end*
  247. (address->integer *large-image-hp*)))
  248. (set! *large-image-end* (address+ *small-image-end*
  249. (address->integer *large-image-end*))))
  250. (define (adjust-weaks-area-descriptors! stob-table)
  251. (do ((i 0 (+ i 1)))
  252. ((= i (bibop-areas-weaks-index (get-bibop-areas))))
  253. (let ((stob (vector-ref (bibop-areas-weaks (get-bibop-areas)) i)))
  254. ;; If this is 'false'(= the marked index as last stob) jump it
  255. (if (and (stob? stob)
  256. (not (= stob false)))
  257. (let ((image-location (table-ref stob-table stob)))
  258. (set-image-location-new-descriptor!
  259. image-location
  260. (address->stob-descriptor
  261. (address+ *large-image-end*
  262. (address->integer
  263. (address-after-header
  264. (image-location-new-descriptor image-location))))))))))
  265. ;; Move the pointers of the weaks area
  266. (set! *weaks-image-begin* (address+ *large-image-end*
  267. (address->integer *weaks-image-begin*)))
  268. (set! *weaks-image-hp* (address+ *large-image-end*
  269. (address->integer *weaks-image-hp*)))
  270. (set! *weaks-image-end* (address+ *large-image-end*
  271. (address->integer *weaks-image-end*))))
  272. ;; Write everything till last traced stob (marked index 'false')
  273. (define (write-small-area write-stob)
  274. (do ((i 0 (+ i 1)))
  275. ((or (= i (bibop-areas-small-index (get-bibop-areas)))
  276. (= false (vector-ref (bibop-areas-small (get-bibop-areas)) i))))
  277. (write-stob (vector-ref (bibop-areas-small (get-bibop-areas)) i))))
  278. ;; Write everything till last traced stob (marked index 'false')
  279. (define (write-large-area write-stob)
  280. (do ((i 0 (+ i 1)))
  281. ((or (= i (bibop-areas-large-index (get-bibop-areas)))
  282. (= false (vector-ref (bibop-areas-large (get-bibop-areas)) i))))
  283. (write-stob (vector-ref (bibop-areas-large (get-bibop-areas)) i))))
  284. ;; Write everything till last traced stob (marked index 'false')
  285. (define (write-weaks-area write-stob)
  286. (do ((i 0 (+ i 1)))
  287. ((or (= i (bibop-areas-weaks-index (get-bibop-areas)))
  288. (= false (vector-ref (bibop-areas-weaks (get-bibop-areas)) i))))
  289. (write-stob (vector-ref (bibop-areas-weaks (get-bibop-areas)) i))))
  290. (define (image-alloc type-size length-in-a-units)
  291. (let ((image-hp
  292. (enum-case area-type-size type-size
  293. ((small) *small-image-hp*)
  294. ((large) *large-image-hp*)
  295. ((weaks) *weaks-image-hp*)
  296. (else
  297. (error "invalid area tag")
  298. *weaks-image-hp*)))) ; don't confuse the PreScheme compiler
  299. (let ((data-addr (address+ image-hp (cells->a-units stob-overhead))))
  300. (enum-case area-type-size type-size
  301. ((small)
  302. (set! *small-image-hp* (address+ data-addr length-in-a-units)))
  303. ((large)
  304. (set! *large-image-hp* (address+ data-addr length-in-a-units)))
  305. ((weaks)
  306. (set! *weaks-image-hp* (address+ data-addr length-in-a-units))))
  307. (address->stob-descriptor data-addr))))
  308. (define (deallocate-areas)
  309. (unspecific))
  310. ;; Debugging stuff
  311. (define *stob-table-obj-nr* 0)
  312. (define (*bibop-areas-obj-nr*)
  313. (+ (bibop-areas-small-index (get-bibop-areas))
  314. (bibop-areas-large-index (get-bibop-areas))
  315. (bibop-areas-weaks-index (get-bibop-areas))))
  316. (define (compare-stobs-nr)
  317. (write-out-newline)
  318. (write-out-string "STOB-TABLE has : ")
  319. (write-out-integer *stob-table-obj-nr*)
  320. (write-out-newline)
  321. (write-out-string "SMALL IMAGE-SIZE : ")
  322. (write-out-integer (small-image-size))
  323. (write-out-newline)
  324. (write-out-string "LARGE IMAGE-SIZE : ")
  325. (write-out-integer (large-image-size))
  326. (write-out-newline)
  327. (write-out-string "WEAKS IMAGE-SIZE : ")
  328. (write-out-integer (weaks-image-size))
  329. (write-out-newline)
  330. (write-out-newline)
  331. (write-out-string "BIBOP-AREAS has : ")
  332. (write-out-integer (*bibop-areas-obj-nr*))
  333. (write-out-newline))
  334. (define (show-descriptors stob-table)
  335. (show-small-descriptors stob-table)
  336. (show-large-descriptors stob-table)
  337. (show-weaks-descriptors stob-table))
  338. (define (show-small-descriptors stob-table)
  339. (do-show-descriptors "SMALL"
  340. (bibop-areas-small-index (get-bibop-areas))
  341. (bibop-areas-small (get-bibop-areas))
  342. stob-table))
  343. (define (show-large-descriptors stob-table)
  344. (do-show-descriptors "LARGE"
  345. (bibop-areas-large-index (get-bibop-areas))
  346. (bibop-areas-large (get-bibop-areas))
  347. stob-table))
  348. (define (show-weaks-descriptors stob-table)
  349. (do-show-descriptors "WEAKS"
  350. (bibop-areas-weaks-index (get-bibop-areas))
  351. (bibop-areas-weaks (get-bibop-areas))
  352. stob-table))
  353. (define (do-show-descriptors area-size-str area-index area stob-table)
  354. (write-out-newline)
  355. (write-out-string area-size-str)
  356. (write-out-newline)
  357. (do ((i 0 (+ i 1)))
  358. ((= i area-index))
  359. (let* ((stob (vector-ref area i))
  360. (image-location (table-ref stob-table stob)))
  361. (write-out-newline)
  362. (write-out-integer stob)
  363. (write-out-string " : ")
  364. (write-out-integer (image-location-new-descriptor image-location))
  365. (write-out-newline))))
  366. (define (display-small-image)
  367. (write-out-newline)
  368. (write-out-string "SMALL-IMG-DATA")
  369. (write-out-newline)
  370. (write-out-string "*small-image-begin* : ")
  371. (write-out-integer (address->integer *small-image-begin*))
  372. (write-out-newline)
  373. (write-out-string "*small-image-hp* : ")
  374. (write-out-integer (address->integer *small-image-hp*))
  375. (write-out-newline)
  376. (write-out-string "*small-image-end* : ")
  377. (write-out-integer (address->integer *small-image-end*))
  378. (write-out-newline)
  379. (write-out-string "PAGES : ")
  380. (write-out-integer (quotient (address-difference *small-image-end*
  381. *small-image-begin*)
  382. 4096))
  383. (write-out-newline))
  384. (define (display-large-image)
  385. (write-out-newline)
  386. (write-out-string "LARGE-IMG-DATA")
  387. (write-out-newline)
  388. (write-out-string "*large-image-begin* : ")
  389. (write-out-integer (address->integer *large-image-begin*))
  390. (write-out-newline)
  391. (write-out-string "*large-image-hp* : ")
  392. (write-out-integer (address->integer *large-image-hp*))
  393. (write-out-newline)
  394. (write-out-string "*large-image-end* : ")
  395. (write-out-integer (address->integer *large-image-end*))
  396. (write-out-newline)
  397. (write-out-string "PAGES : ")
  398. (write-out-integer (quotient (address-difference *large-image-end*
  399. *large-image-begin*)
  400. 4096))
  401. (write-out-newline))
  402. (define (display-weaks-image)
  403. (write-out-newline)
  404. (write-out-string "WEAKS-IMG-DATA")
  405. (write-out-newline)
  406. (write-out-string "*weaks-image-begin* : ")
  407. (write-out-integer (address->integer *weaks-image-begin*))
  408. (write-out-newline)
  409. (write-out-string "*weaks-image-hp* : ")
  410. (write-out-integer (address->integer *weaks-image-hp*))
  411. (write-out-newline)
  412. (write-out-string "*weaks-image-end* : ")
  413. (write-out-integer (address->integer *weaks-image-end*))
  414. (write-out-newline)
  415. (write-out-string "PAGES : ")
  416. (write-out-integer (quotient (address-difference *weaks-image-end*
  417. *weaks-image-begin*)
  418. 4096))
  419. (write-out-newline))