package-defs.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. ; The VM
  2. (define-structure vm vm-interface
  3. (open prescheme ps-receive vm-architecture vm-utilities
  4. external
  5. bignum-low
  6. integer-arithmetic
  7. flonum-arithmetic
  8. data struct stob
  9. text-encodings
  10. interpreter interpreter-internal
  11. stack gc interpreter-gc gc-util
  12. vmio
  13. arithmetic-opcodes
  14. external-opcodes
  15. external-events
  16. shared-bindings shared-bindings-access
  17. symbols
  18. io-opcodes
  19. external-gc-roots
  20. proposal-opcodes
  21. read-image
  22. return-codes
  23. ;; For debugging
  24. memory ;; fetch
  25. )
  26. (files (interp resume)
  27. (interp vm-external)))
  28. ; Byte code architecture.
  29. (define-structure vm-architecture vm-architecture-interface
  30. (open prescheme ps-platform)
  31. (files (interp arch)))
  32. ;----------------------------------------------------------------
  33. ; The interpreter.
  34. (define-structures ((interpreter interpreter-interface)
  35. (interpreter-internal interpreter-internal-interface))
  36. (open prescheme ps-receive vm-utilities vm-architecture enum-case
  37. events
  38. pending-interrupts
  39. memory data stob struct allocation vmio
  40. text-encodings
  41. return-codes
  42. gc-roots gc gc-util
  43. heap stack external external-events)
  44. (for-syntax (open scheme destructuring signals))
  45. (files (interp interp)
  46. (interp call)
  47. (interp define-primitive)
  48. (interp prim)
  49. (interp interrupt)
  50. )
  51. ;(optimize auto-integrate)
  52. )
  53. (define-structure pending-interrupts (export pending-interrupts-empty?
  54. pending-interrupts-remove!
  55. pending-interrupts-add!
  56. pending-interrupts-clear!
  57. pending-interrupts-mask
  58. interrupt-bit)
  59. (open prescheme)
  60. (begin
  61. (define *pending-interrupts*) ; bitmask of pending interrupts
  62. (define (pending-interrupts-add! interrupt-bit)
  63. (set! *pending-interrupts*
  64. (bitwise-ior *pending-interrupts* interrupt-bit)))
  65. (define (pending-interrupts-remove! interrupt-bit)
  66. (set! *pending-interrupts*
  67. (bitwise-and *pending-interrupts*
  68. (bitwise-not interrupt-bit))))
  69. (define (pending-interrupts-clear!)
  70. (set! *pending-interrupts* 0))
  71. (define (pending-interrupts-empty?)
  72. (= *pending-interrupts* 0))
  73. (define (pending-interrupts-mask)
  74. *pending-interrupts*)
  75. ; Return a bitmask for INTERRUPT.
  76. (define (interrupt-bit interrupt)
  77. (shift-left 1 interrupt))
  78. ))
  79. ; Assorted additional opcodes
  80. (define-structure arithmetic-opcodes (export)
  81. (open prescheme interpreter-internal
  82. interpreter-gc
  83. data struct
  84. fixnum-arithmetic
  85. vm-architecture
  86. bignum-arithmetic
  87. flonum-arithmetic
  88. integer-arithmetic)
  89. (files (arith integer-op)))
  90. (define-structure external-opcodes external-call-interface
  91. (open prescheme vm-architecture ps-receive
  92. interpreter-internal stack
  93. memory data struct
  94. gc gc-roots gc-util
  95. heap ; S48-GATHER-OBJECTS
  96. string-tables
  97. external
  98. shared-bindings shared-bindings-access)
  99. (files (interp external-call)))
  100. (define-structure external-events external-events-interface
  101. (open prescheme ps-record-types ps-memory
  102. data struct
  103. vm-utilities
  104. shared-bindings)
  105. (files (interp external-event)))
  106. (define-structures ((shared-bindings shared-bindings-interface)
  107. (shared-bindings-access shared-bindings-access-interface))
  108. (open prescheme
  109. vm-architecture data struct
  110. string-tables
  111. gc gc-roots gc-util)
  112. (files (interp shared-binding)))
  113. (define-structure io-opcodes (export)
  114. (open prescheme vm-utilities vm-architecture ps-receive enum-case
  115. interpreter-internal
  116. channel-io vmio
  117. memory data struct
  118. read-image write-image
  119. gc-roots
  120. symbols external-opcodes
  121. stack ;pop
  122. stob ;immutable
  123. text-encodings)
  124. (files (interp prim-io)))
  125. (define-structure proposal-opcodes (export initialize-proposals!+gc)
  126. (open prescheme vm-utilities vm-architecture ps-receive
  127. interpreter-internal
  128. memory data struct
  129. gc-util
  130. stob
  131. external ;get-proposal-lock! release-proposal-lock!
  132. gc ;s48-trace-value
  133. gc-roots) ;add-gc-root!
  134. (files (interp proposal)))
  135. (define-structures ((stack stack-interface)
  136. (initialize-stack (export initialize-stack+gc)))
  137. (open prescheme vm-utilities ps-receive ps-memory
  138. vm-architecture memory data stob struct
  139. return-codes
  140. allocation
  141. gc-roots gc
  142. heap) ; for debugging function STACK-CHECK
  143. ;(optimize auto-integrate)
  144. (files (interp stack)
  145. (interp stack-gc)))
  146. (define-structure vmio vmio-interface
  147. (open prescheme ps-receive channel-io vm-utilities
  148. data stob struct allocation memory
  149. pending-interrupts
  150. vm-architecture) ;port-status
  151. ;(optimize auto-integrate)
  152. (files (interp vmio)))
  153. ; The VM needs return pointers for a few special continuations (bottom-of-stack,
  154. ; exceptions frame, and interrupt frames). These have to have the correct data
  155. ; format.
  156. (define-structure return-codes (export make-return-code
  157. s48-make-blank-return-code
  158. return-code-size
  159. return-code-pc)
  160. (open prescheme vm-architecture struct)
  161. (begin
  162. (define return-code-pc 13)
  163. ;; Number of entries of the code vector
  164. (define blank-return-code-count 15)
  165. (define (make-return-code-count opcode-count)
  166. (+ blank-return-code-count opcode-count))
  167. (define first-opcode-index 15)
  168. ;; value for VM
  169. (define return-code-count (make-return-code-count 1))
  170. ;; Size in bytes of the return code frame
  171. (define (make-return-code-size return-code-count)
  172. (code-vector-size return-code-count))
  173. ;; value for VM
  174. (define return-code-size (make-return-code-size return-code-count))
  175. ;; procedure for VM
  176. (define (make-return-code protocol template opcode frame-size key)
  177. (let ((blank-return-code (make-blank-return-code protocol template frame-size 1 key)))
  178. (code-vector-set! blank-return-code first-opcode-index opcode)
  179. blank-return-code))
  180. (define (make-blank-return-code protocol template frame-size opcode-count key)
  181. (let ((code (make-code-vector (make-return-code-count opcode-count) key)))
  182. ; A whole lot of stuff to make the GC and disassembler happy.
  183. (code-vector-set! code 0 (enum op protocol))
  184. (code-vector-set! code 1 protocol)
  185. (code-vector-set! code 2 #b00) ; no env or template - for disassembler
  186. (code-vector-set! code 3 (enum op cont-data)) ; - etc.
  187. (code-vector-set! code 4 0) ; high byte of size
  188. (code-vector-set! code 5 8) ; low byte of size
  189. ; no mask
  190. (code-vector-set! code 6 (high-byte template))
  191. (code-vector-set! code 7 (low-byte template))
  192. (code-vector-set! code 8 0) ; high byte of offset
  193. (code-vector-set! code 9 return-code-pc); low byte of offset
  194. (code-vector-set! code 10 0) ; GC mask size
  195. (code-vector-set! code 11 (high-byte frame-size))
  196. (code-vector-set! code 12 (low-byte frame-size))
  197. (code-vector-set! code 13 (enum op protocol))
  198. (code-vector-set! code 14 protocol)
  199. code))
  200. (define (s48-make-blank-return-code protocol template frame-size opcode-count)
  201. (make-blank-return-code protocol
  202. template
  203. frame-size
  204. opcode-count
  205. (ensure-space (make-return-code-size
  206. (make-return-code-count opcode-count)))))
  207. (define (high-byte n)
  208. (low-byte (arithmetic-shift-right n 8)))
  209. (define (low-byte n)
  210. (bitwise-and n #xFF))))
  211. ;----------------------------------------------------------------
  212. ; GC and allocation utilities for the interpreter.
  213. (define-structures ((interpreter-gc interpreter-gc-interface)
  214. (gc-roots gc-roots-interface))
  215. (open prescheme)
  216. (begin
  217. ; GC-ROOT and POST-GC-CLEANUP are defined incrementally.
  218. ;
  219. ; (ADD-GC-ROOT! <thunk>) ; call <thunk> when tracing the GC roots
  220. ; (ADD-POST-GC-CLEANUP! <thunk>) ; call <thunk> when a GC has finished
  221. ;
  222. ; (S48-GC-ROOT) ; call all the root thunks
  223. ; (S48-POST-GC-CLEANUP) ; call all the cleanup thunks
  224. (define-syntax define-extensible-proc
  225. (syntax-rules ()
  226. ((define-extensible-proc (proc arg ...) body-form extender temp)
  227. (begin
  228. (define (temp arg ...)
  229. body-form
  230. (unspecific))
  231. (define (proc arg ...) (temp arg ...))
  232. (define (extender more)
  233. (let ((old temp))
  234. (set! temp (lambda (arg ...)
  235. (more arg ...)
  236. (old arg ...)))))))))
  237. (define-extensible-proc (s48-gc-root)
  238. (unspecific)
  239. add-gc-root!
  240. *gc-root-proc*)
  241. (define-extensible-proc (s48-post-gc-cleanup major? in-trouble?)
  242. (begin
  243. (eq? major? #t)
  244. (eq? in-trouble? #t)) ; for the type checker
  245. add-post-gc-cleanup!
  246. *post-gc-cleanup*)))
  247. (define-structure gc-util gc-util-interface
  248. (open prescheme data gc gc-roots)
  249. (begin
  250. (define *temp0* false)
  251. (define *temp1* false)
  252. (add-gc-root! (lambda ()
  253. (set! *temp0* (s48-trace-value *temp0*))
  254. (set! *temp1* (s48-trace-value *temp1*))))
  255. (define (save-temp0! value)
  256. (set! *temp0* value))
  257. (define (recover-temp0!)
  258. (let ((value *temp0*))
  259. (set! *temp0* false)
  260. value))
  261. (define (save-temp1! value)
  262. (set! *temp1* value))
  263. (define (recover-temp1!)
  264. (let ((value *temp1*))
  265. (set! *temp1* false)
  266. value))))
  267. ; Registering and tracing external GC roots.
  268. (define-structure external-gc-roots external-gc-roots-interface
  269. (open prescheme ps-memory
  270. memory data
  271. gc gc-roots)
  272. (files (heap gc-root)))
  273. ;----------------------------------------------------------------
  274. ; Data structures
  275. (define-structure data vm-data-interface
  276. (open prescheme ps-unsigned-integers vm-utilities
  277. vm-architecture
  278. ps-platform)
  279. ;(optimize auto-integrate)
  280. (files (data data)))
  281. (define-structure memory memory-interface
  282. (open prescheme ps-memory vm-utilities data)
  283. ;(optimize auto-integrate)
  284. (files (data memory)))
  285. (define-structure stob stob-interface
  286. (open prescheme ps-receive vm-utilities vm-architecture
  287. memory heap data allocation debugging)
  288. ;(optimize auto-integrate)
  289. (files (data stob)))
  290. (define-structure struct struct-interface
  291. (open prescheme vm-utilities
  292. vm-architecture memory data stob allocation)
  293. (for-syntax (open scheme vm-architecture destructuring))
  294. ;(optimize auto-integrate)
  295. (files (data defdata)
  296. (data struct)))
  297. (define-structure string-tables string-table-interface
  298. (open prescheme vm-utilities vm-architecture
  299. data struct stob
  300. ps-memory ; address->integer - BIBOP
  301. memory ; address->stob-descriptor - BIBOP
  302. image-table ; image-location-new-descriptor - BIBOP
  303. )
  304. (files (data vm-tables)))
  305. (define-structure symbols (export s48-symbol-table
  306. install-symbols!+gc)
  307. (open prescheme vm-utilities vm-architecture
  308. interpreter-internal
  309. memory heap data struct string-tables
  310. gc gc-roots)
  311. (files (data symbol)))
  312. (define-structure text-encodings text-encodings-interface
  313. (open prescheme ps-memory enum-case
  314. (subset vm-architecture (text-encoding-option)))
  315. (files (data text-encoding)))
  316. ;----------------------------------------------------------------
  317. ;; DUMPER
  318. ;----------------------------------------------------------------
  319. ;; Reading and writing images
  320. ;; The new READ-IMAGE uses a helper structure READ-IMAGE-KERNEL
  321. (define-structure read-image read-image-interface
  322. (open prescheme enum-case ps-receive ps-memory
  323. debugging
  324. vm-utilities
  325. (subset vm-architecture (architecture-version))
  326. image-util
  327. read-image-gc-specific
  328. read-image-util
  329. data
  330. (subset memory (fetch))
  331. heap-init
  332. (subset gc (s48-trace-value)))
  333. (files (heap read-image)))
  334. (define-structure read-image-portable read-image-portable-interface
  335. (open prescheme ps-receive enum-case
  336. vm-utilities vm-architecture
  337. memory
  338. data struct
  339. (subset string-tables (relocate-table))
  340. ps-memory ;allocate/deallocate-memory
  341. heap ;s48-heap-size
  342. image-table ;make-table
  343. image-util
  344. heap-init
  345. read-image-util
  346. read-image-util-gc-specific
  347. )
  348. (files (heap read-image-portable)))
  349. (define-structure write-image write-image-interface
  350. (open prescheme ps-receive enum-case
  351. vm-utilities vm-architecture
  352. memory data struct
  353. ps-platform
  354. heap
  355. image-table
  356. image-util
  357. write-image-util
  358. string-tables
  359. symbols ;s48-symbol-table
  360. shared-bindings-access
  361. ps-record-types ;define-record-type
  362. write-image-gc-specific
  363. )
  364. (files (heap write-image)))
  365. (define-structure image-table image-table-interface
  366. (open prescheme ps-memory ps-record-types
  367. vm-utilities)
  368. (files (heap image-table)))
  369. (define-structure image-util image-util-interface
  370. (open prescheme enum-case)
  371. (files (heap image-util)))
  372. (define-structure read-image-util read-image-util-interface
  373. (open prescheme ps-receive
  374. data
  375. memory
  376. (subset ps-memory (read-block address+ address<))
  377. (subset data (bytes->a-units b-vector-header? header-length-in-a-units stob?))
  378. vm-utilities
  379. (subset allocation (s48-allocate-traced+gc))
  380. (subset struct (vm-symbol-next
  381. vm-set-symbol-next!
  382. shared-binding-next
  383. set-shared-binding-next!))
  384. string-tables)
  385. (files (heap read-image-util)))
  386. (define-structure write-image-util write-image-util-interface
  387. (open prescheme ps-memory
  388. (subset memory (address1+)))
  389. (files (heap write-image-util)))
  390. ;----------------------------------------------------------------
  391. ; Arithmetic
  392. (define-structure fixnum-arithmetic fixnum-arithmetic-interface
  393. (open prescheme vm-utilities data
  394. memory) ; bits-per-cell
  395. ;(optimize auto-integrate)
  396. (files (arith arith)))
  397. (define-structure bignum-low bignum-low-interface
  398. (open prescheme
  399. vm-utilities
  400. stob
  401. ps-platform
  402. gc
  403. struct memory
  404. vm-architecture
  405. external
  406. interpreter-gc
  407. data)
  408. (files (arith bignum-low)))
  409. (define-structure bignum-arithmetic bignum-arithmetic-interface
  410. (open prescheme
  411. vm-utilities
  412. external
  413. struct
  414. ps-receive
  415. interpreter-internal
  416. data
  417. gc-util
  418. bignum-low)
  419. (files (arith bignum-arith)))
  420. (define-structure integer-arithmetic integer-arithmetic-interface
  421. (open prescheme ps-unsigned-integers
  422. fixnum-arithmetic
  423. bignum-arithmetic
  424. external
  425. bignum-low
  426. struct
  427. data)
  428. (files (arith integer)))
  429. (define-structure flonum-arithmetic (export flonum-add
  430. flonum-subtract
  431. flonum-multiply
  432. flonum-divide
  433. flonum= flonum< flonum>
  434. flonum<= flonum>=)
  435. (open prescheme
  436. ps-memory
  437. ps-flonums
  438. gc-util
  439. data ; false
  440. struct)
  441. (files (arith flonum-arith)))
  442. ;----------------------------------------------------------------
  443. ; Random utility
  444. (define-structure enum-case (export (enum-case :syntax))
  445. (open prescheme)
  446. (begin
  447. (define-syntax enum-case
  448. (syntax-rules (else)
  449. ((enum-case enumeration (x ...) clause ...)
  450. (let ((temp (x ...)))
  451. (enum-case enumeration temp clause ...)))
  452. ((enum-case enumeration value ((name ...) body ...) rest ...)
  453. (if (or (= value (enum enumeration name)) ...)
  454. (begin body ...)
  455. (enum-case enumeration value rest ...)))
  456. ((enum-case enumeration value (else body ...))
  457. (begin body ...))
  458. ((enum-case enumeration value)
  459. (unspecific))))))
  460. ; Memory management
  461. ;
  462. ; These are dummies to avoid warnings during compilation.
  463. ; The real modules are in each GC subdirectory (gc-twospace and gc-bibop)
  464. ; and will be loaded after this file.
  465. ;----------------------------------------------------------------
  466. (define-structures ((heap heap-interface)
  467. (heap-gc-util heap-gc-util-interface)
  468. (heap-init heap-init-interface)
  469. (gc gc-interface)
  470. (allocation allocation-interface)
  471. (read-image-gc-specific read-image-gc-specific-interface)
  472. (read-image-util-gc-specific read-image-util-gc-specific-interface)
  473. (write-image-gc-specific write-image-gc-specific-interface))
  474. (open)
  475. (files))
  476. ;; JUST FOR DEBUGGING:
  477. ;; To activate/deactivate it, the flag 'debug-mode?' must be set in
  478. ;; debugging.scm
  479. (define-structure debugging debugging-interface
  480. (open prescheme vm-utilities)
  481. (files debugging))