primitives.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Alternate implementation of PRIMITIVES module.
  3. (define underlying-error error)
  4. (define (unspecific) (if #f #f))
  5. ; Records
  6. (define-record-type new-record :new-record
  7. (make-new-record fields)
  8. record?
  9. (fields new-record-fields))
  10. (define (make-record size init)
  11. (make-new-record (make-vector size init)))
  12. (define (record-ref r i)
  13. (vector-ref (new-record-fields r) i))
  14. (define (record-set! r i value)
  15. (vector-set! (new-record-fields r) i value))
  16. (define (record-length r)
  17. (vector-length (new-record-fields r)))
  18. ; Extended numbers
  19. (define-record-type new-extended-number :new-extended-number
  20. (make-new-extended-number fields)
  21. extended-number?
  22. (fields new-extended-number-fields))
  23. (define-record-discloser :new-extended-number
  24. (lambda (n) `(extended-number ,(new-extended-number-fields n))))
  25. (define (make-extended-number size init)
  26. (make-new-extended-number (make-vector size init)))
  27. (define (extended-number-ref n i)
  28. (vector-ref (new-extended-number-fields n) i))
  29. (define (extended-number-set! n i value)
  30. (vector-set! (new-extended-number-fields n) i value))
  31. (define (extended-number-length n)
  32. (vector-length (new-extended-number-fields n)))
  33. ; Current thread
  34. (define *current-thread* 'uninitialized-current-thread)
  35. (define (current-thread) *current-thread*)
  36. (define (set-current-thread! thread)
  37. (if (not (and (record? thread)
  38. (list? (record-ref thread 1))))
  39. (underlying-error "invalid current thread" thread))
  40. (set! *current-thread* thread))
  41. ; Etc.
  42. (define (close-port port)
  43. ((if (input-port? port) close-input-port close-output-port)
  44. port))
  45. (define (write-string s port)
  46. (display s port))
  47. (define (write-string-tail s start port)
  48. (display (substring s start (port))
  49. (define (schedule-interrupt interval)
  50. (if (not (= interval 0))
  51. (warn "ignoring schedule-interrupt" interval)))
  52. (define *pseudo-enabled-interrupts* 0)
  53. (define (set-enabled-interrupts! ei)
  54. (let ((previous *pseudo-enabled-interrupts*))
  55. (set! *pseudo-enabled-interrupts* ei)
  56. ;; (if (bitwise-and *pseudo-pending-interrupts* ei) ...)
  57. previous))
  58. (define *pseudo-pending-interrupts* 0)
  59. (define *pseudo-exception-handlers* #f)
  60. (define (set-exception-handlers! h)
  61. (let ((old *pseudo-exception-handlers*))
  62. (set! *pseudo-exception-handlers* h)
  63. old))
  64. (define *pseudo-interrupt-handlers* #f)
  65. (define (set-interrupt-handlers! v)
  66. (let ((old *pseudo-interrupt-handlers*))
  67. (set! *pseudo-interrupt-handlers* v)
  68. old))
  69. (define (unimplemented name)
  70. (lambda args (underlying-error "unimplemented primitive" name args)))
  71. (define collect (unimplemented 'collect))
  72. (define call-external-value (unimplemented 'call-external-value))
  73. (define lookup-shared-binding (unimplemented 'lookup-shared-binding))
  74. (define define-shared-binding (unimplemented 'define-shared-binding))
  75. (define undefine-shared-binding (unimplemented 'undefine-shared-binding))
  76. (define (shared-binding? x) #f)
  77. (define make-shared-binding (unimplemented 'make-shared-binding))
  78. (define shared-binding-name (unimplemented 'shared-binding-name))
  79. (define shared-binding-is-import? (unimplemented 'shared-binding-is-import?))
  80. (define shared-binding-ref (unimplemented 'shared-binding-ref))
  81. (define shared-binding-set! (unimplemented 'shared-binding-set!))
  82. (define find-undefined-imported-bindings (unimplemented 'find-undefined-imported-bindings))
  83. (define find-all (unimplemented 'find-all))
  84. (define vm-extension (unimplemented 'vm-extension))
  85. (define (memory-status which arg)
  86. (case which
  87. ((2) 100)
  88. ((3) (display "(Ignoring set-minimum-recovered-space!)") (newline))
  89. (else (underlying-error "unimplemented memory-status" which arg))))
  90. (define (time which arg)
  91. (case which
  92. ((0) 1000)
  93. (else (underlying-error "unimplemented time" which arg))))
  94. ; end of definitions implementing PRIMITIVES structure
  95. ; --------------------
  96. ; Auxiliary crud.
  97. (define (maybe-handle-interrupt which)
  98. ;; Should actually do (get-highest-priority-interrupt!) ...
  99. (let ((bit (arithmetic-shift 1 which)))
  100. (cond ((= (bitwise-and *pseudo-enabled-interrupts* bit) 0)
  101. (set! *pseudo-pending-interrupts*
  102. (bitwise-ior *pseudo-pending-interrupts* bit))
  103. (display "(Interrupt deferred)")
  104. (newline)
  105. #f)
  106. (else
  107. (set! *pseudo-pending-interrupts*
  108. (bitwise-and *pseudo-pending-interrupts*
  109. (bitwise-not bit)))
  110. (display "(Handling interrupt)")
  111. (newline)
  112. ((vector-ref *pseudo-interrupt-handlers* which)
  113. (set-enabled-interrupts! 0))
  114. #t))))
  115. (define (raise-exception opcode exception arguments)
  116. (apply (vector-ref (get-exception-handlers) opcode)
  117. (cons exception arguments)))
  118. (define (get-exception-handlers)
  119. *pseudo-exception-handlers*)
  120. (define (clear-registers!)
  121. (set! *current-thread* 'uninitialized-current-thread)
  122. (set! *pseudo-enabled-interrupts* 0)
  123. (set! *pseudo-interrupt-handlers* #f)
  124. (set! *pseudo-exception-handlers* #f))
  125. (define *vm-return* #f)
  126. (define (vm-return . rest)
  127. (if *vm-return*
  128. (apply *vm-return* rest)
  129. (underlying-error "vm-return" rest)))
  130. (define (?start entry-point arg) ;E.g. (?start (usual-resumer bare #t) 0)
  131. (clear-registers!)
  132. (call-with-current-continuation
  133. (lambda (k)
  134. (set! *vm-return* k)
  135. (entry-point arg
  136. (current-input-port)
  137. (current-output-port)))))