external.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. (define (fake-it name)
  3. (lambda args
  4. (display "Call to ")
  5. (display (cons name args))
  6. (newline)
  7. 0))
  8. (define extended-vm (fake-it 'extended-vm))
  9. (define external-call (fake-it 'call-external-value))
  10. (define schedule-interrupt (fake-it 'schedule-interrupt))
  11. ;; The following code "implements" bignums using the integers of the
  12. ;; host system. It stores these integers in the byte-vector of the
  13. ;; bignum stob just as the C code does but I'm not sure whether the
  14. ;; representation is the same. Note that the VM passes the address
  15. ;; after the header to the external functions. We re-generate the
  16. ;; bignum stob from this address and use the b-vector operations to
  17. ;; access the data.
  18. (define (bytes->b-vector bytes)
  19. (let ((bv (s48-allocate-bignum (length bytes))))
  20. (let lp ((bytes bytes)
  21. (pos 0))
  22. (if (null? bytes)
  23. bv
  24. (begin
  25. (b-vector-set! bv pos (car bytes))
  26. (lp (cdr bytes)
  27. (+ pos 1)))))))
  28. (define (integer->b-vector int)
  29. (let lp ((i (abs int))
  30. (bytes '()))
  31. (if (< i 256)
  32. (bytes->b-vector
  33. (cons (if (< int 0) 1 0) ; sign
  34. (cons i bytes)))
  35. (lp (arithmetic-shift i -8)
  36. (cons (modulo i 256) bytes)))))
  37. (define (b-vector->integer bv)
  38. (let lp ((index 1)
  39. (sig (- (b-vector-length bv) 2))
  40. (int 0))
  41. (if (= index (b-vector-length bv))
  42. (if (zero? (b-vector-ref bv 0))
  43. int
  44. (- int))
  45. (lp (+ index 1)
  46. (- sig 1)
  47. (+ (arithmetic-shift (b-vector-ref bv index) (* sig 8)) int)))))
  48. ;; no need for cached constants...
  49. (define external-bignum-make-cached-constants
  50. (fake-it 'external-bignum-make-cached-constants))
  51. (define-syntax define-binary-bignum-op/to-bignum
  52. (syntax-rules ()
  53. ((define-binary-bignum-op/to-bignum bignum-op integer-op)
  54. (define (bignum-op x y)
  55. (address-after-header
  56. (integer->b-vector
  57. (integer-op
  58. (b-vector->integer (address->stob-descriptor x))
  59. (b-vector->integer (address->stob-descriptor y)))))))))
  60. (define-syntax define-binary-bignum-op
  61. (syntax-rules ()
  62. ((define-binary-bignum-op bignum-op integer-op)
  63. (define (bignum-op x y)
  64. (integer-op
  65. (b-vector->integer (address->stob-descriptor x))
  66. (b-vector->integer (address->stob-descriptor y)))))))
  67. (define-syntax define-unary-bignum-op
  68. (syntax-rules ()
  69. ((define-unary-bignum-op bignum-op integer-op)
  70. (define (bignum-op x)
  71. (integer-op
  72. (b-vector->integer (address->stob-descriptor x)))))))
  73. (define-syntax define-unary-bignum-op/to-bignum
  74. (syntax-rules ()
  75. ((define-unary-bignum-op/to-bignum bignum-op integer-op)
  76. (define (bignum-op x)
  77. (address-after-header
  78. (integer->b-vector
  79. (integer-op
  80. (b-vector->integer (address->stob-descriptor x)))))))))
  81. (define-binary-bignum-op/to-bignum external-bignum-add +)
  82. (define-binary-bignum-op/to-bignum external-bignum-subtract -)
  83. (define-binary-bignum-op/to-bignum external-bignum-multiply *)
  84. (define-binary-bignum-op/to-bignum external-bignum-quotient quotient)
  85. (define-binary-bignum-op/to-bignum external-bignum-remainder remainder)
  86. (define-binary-bignum-op external-bignum-divide
  87. (lambda (x y)
  88. (if (zero? y)
  89. (values #t 0 0)
  90. (values #f
  91. (address-after-header
  92. (integer->b-vector (quotient x y)))
  93. (address-after-header
  94. (integer->b-vector (remainder x y)))))))
  95. (define-binary-bignum-op external-bignum-equal? =)
  96. (define-binary-bignum-op external-bignum-compare
  97. (lambda (x y)
  98. (if (< x y)
  99. -1
  100. (if (= x y)
  101. 0
  102. 1))))
  103. (define-unary-bignum-op external-bignum-test
  104. (lambda (x)
  105. (if (< x 0) -1
  106. (if (= x 0) 0
  107. 1))))
  108. (define-unary-bignum-op/to-bignum external-bignum-negate
  109. (lambda (x) (- x)))
  110. (define external-bignum-from-long
  111. (lambda (long)
  112. (address-after-header (integer->b-vector long))))
  113. (define (external-bignum-fits-in-word? bignum-addr word-length two-compl?)
  114. (let ((bignum (b-vector->integer (address->stob-descriptor bignum-addr))))
  115. (and (>= bignum least-fixnum-value)
  116. (<= bignum greatest-fixnum-value))))
  117. (define-unary-bignum-op external-bignum->long (lambda (x)
  118. x))
  119. (define-binary-bignum-op/to-bignum external-bignum-bitwise-and bitwise-and)
  120. (define-binary-bignum-op/to-bignum external-bignum-bitwise-xor bitwise-xor)
  121. (define-binary-bignum-op/to-bignum external-bignum-bitwise-ior bitwise-ior)
  122. (define-unary-bignum-op/to-bignum external-bignum-bitwise-not bitwise-not)
  123. (define-unary-bignum-op external-bignum-bit-count bit-count)
  124. (define (external-bignum-arithmetic-shift bignum-addr y)
  125. (let ((bignum (address->stob-descriptor bignum-addr)))
  126. (address-after-header
  127. (integer->b-vector (arithmetic-shift bignum y)))))
  128. (define (real-time) 0)
  129. (define (run-time) 0)
  130. (define (cheap-time) 0)
  131. (define s48-call-native-procedure (fake-it 's48-call-native-code))
  132. (define s48-invoke-native-continuation (fake-it 's48-call-native-code))
  133. (define s48-native-return 0)
  134. (define get-proposal-lock! (fake-it 'get-proposal-lock!))
  135. (define release-proposal-lock! (fake-it 'release-proposal-lock!))
  136. (define (shared-ref x) x)
  137. (define-syntax shared-set!
  138. (syntax-rules ()
  139. ((shared-set! x v)
  140. (set! x v))))
  141. (define (get-os-string-encoding)
  142. "UTF-8")
  143. (define (raise-argument-type-error val)
  144. (fake-it 'raise-argument-type-error))
  145. (define (raise-range-error val min max)
  146. (fake-it 'raise-range-error))