foreign.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ;;;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
  2. ;;;;
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 2.1 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;;;
  17. (define-module (system foreign)
  18. #:use-module (rnrs bytevectors)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (srfi srfi-9)
  21. #:use-module (srfi srfi-9 gnu)
  22. #:export (void
  23. float double
  24. complex-float complex-double
  25. short
  26. unsigned-short
  27. int unsigned-int long unsigned-long size_t ssize_t ptrdiff_t
  28. int8 uint8
  29. uint16 int16
  30. uint32 int32
  31. uint64 int64
  32. intptr_t uintptr_t
  33. sizeof alignof
  34. %null-pointer
  35. null-pointer?
  36. pointer?
  37. make-pointer
  38. pointer->scm
  39. scm->pointer
  40. pointer-address
  41. pointer->bytevector
  42. bytevector->pointer
  43. set-pointer-finalizer!
  44. dereference-pointer
  45. string->pointer
  46. pointer->string
  47. pointer->procedure
  48. ;; procedure->pointer (see below)
  49. make-c-struct parse-c-struct
  50. define-wrapped-pointer-type))
  51. (eval-when (expand load eval)
  52. (load-extension (string-append "libguile-" (effective-version))
  53. "scm_init_foreign"))
  54. ;;;
  55. ;;; Pointers.
  56. ;;;
  57. (define (null-pointer? pointer)
  58. "Return true if POINTER is the null pointer."
  59. (= (pointer-address pointer) 0))
  60. (if (defined? 'procedure->pointer)
  61. (export procedure->pointer))
  62. ;;;
  63. ;;; Structures.
  64. ;;;
  65. (define bytevector-pointer-ref
  66. (case (sizeof '*)
  67. ((8) (lambda (bv offset)
  68. (make-pointer (bytevector-u64-native-ref bv offset))))
  69. ((4) (lambda (bv offset)
  70. (make-pointer (bytevector-u32-native-ref bv offset))))
  71. (else (error "what machine is this?"))))
  72. (define bytevector-pointer-set!
  73. (case (sizeof '*)
  74. ((8) (lambda (bv offset ptr)
  75. (bytevector-u64-native-set! bv offset (pointer-address ptr))))
  76. ((4) (lambda (bv offset ptr)
  77. (bytevector-u32-native-set! bv offset (pointer-address ptr))))
  78. (else (error "what machine is this?"))))
  79. (define (writer-complex set size)
  80. (lambda (bv i val)
  81. (set bv i (real-part val))
  82. (set bv (+ i size) (imag-part val))))
  83. (define (reader-complex ref size)
  84. (lambda (bv i)
  85. (make-rectangular
  86. (ref bv i)
  87. (ref bv (+ i size)))))
  88. (define *writers*
  89. `((,float . ,bytevector-ieee-single-native-set!)
  90. (,double . ,bytevector-ieee-double-native-set!)
  91. ,@(if (defined? 'complex-float)
  92. `((,complex-float
  93. . ,(writer-complex bytevector-ieee-single-native-set! (sizeof float)))
  94. (,complex-double
  95. . ,(writer-complex bytevector-ieee-double-native-set! (sizeof double))))
  96. '())
  97. (,int8 . ,bytevector-s8-set!)
  98. (,uint8 . ,bytevector-u8-set!)
  99. (,int16 . ,bytevector-s16-native-set!)
  100. (,uint16 . ,bytevector-u16-native-set!)
  101. (,int32 . ,bytevector-s32-native-set!)
  102. (,uint32 . ,bytevector-u32-native-set!)
  103. (,int64 . ,bytevector-s64-native-set!)
  104. (,uint64 . ,bytevector-u64-native-set!)
  105. (* . ,bytevector-pointer-set!)))
  106. (define *readers*
  107. `((,float . ,bytevector-ieee-single-native-ref)
  108. (,double . ,bytevector-ieee-double-native-ref)
  109. ,@(if (defined? 'complex-float)
  110. `((,complex-float
  111. . ,(reader-complex bytevector-ieee-single-native-ref (sizeof float)))
  112. (,complex-double
  113. . ,(reader-complex bytevector-ieee-double-native-ref (sizeof double))))
  114. '())
  115. (,int8 . ,bytevector-s8-ref)
  116. (,uint8 . ,bytevector-u8-ref)
  117. (,int16 . ,bytevector-s16-native-ref)
  118. (,uint16 . ,bytevector-u16-native-ref)
  119. (,int32 . ,bytevector-s32-native-ref)
  120. (,uint32 . ,bytevector-u32-native-ref)
  121. (,int64 . ,bytevector-s64-native-ref)
  122. (,uint64 . ,bytevector-u64-native-ref)
  123. (* . ,bytevector-pointer-ref)))
  124. (define (align off alignment)
  125. (1+ (logior (1- off) (1- alignment))))
  126. (define (write-c-struct bv offset types vals)
  127. (let lp ((offset offset) (types types) (vals vals))
  128. (cond
  129. ((not (pair? types))
  130. (or (null? vals)
  131. (error "too many values" vals)))
  132. ((not (pair? vals))
  133. (error "too few values" types))
  134. (else
  135. ;; alignof will error-check
  136. (let* ((type (car types))
  137. (offset (align offset (alignof type))))
  138. (if (pair? type)
  139. (write-c-struct bv offset (car types) (car vals))
  140. ((assv-ref *writers* type) bv offset (car vals)))
  141. (lp (+ offset (sizeof type)) (cdr types) (cdr vals)))))))
  142. (define (read-c-struct bv offset types)
  143. (let lp ((offset offset) (types types) (vals '()))
  144. (cond
  145. ((not (pair? types))
  146. (reverse vals))
  147. (else
  148. ;; alignof will error-check
  149. (let* ((type (car types))
  150. (offset (align offset (alignof type))))
  151. (lp (+ offset (sizeof type)) (cdr types)
  152. (cons (if (pair? type)
  153. (read-c-struct bv offset (car types))
  154. ((assv-ref *readers* type) bv offset))
  155. vals)))))))
  156. (define (make-c-struct types vals)
  157. (let ((bv (make-bytevector (sizeof types) 0)))
  158. (write-c-struct bv 0 types vals)
  159. (bytevector->pointer bv)))
  160. (define (parse-c-struct foreign types)
  161. (let ((size (fold (lambda (type total)
  162. (+ (sizeof type)
  163. (align total (alignof type))))
  164. 0
  165. types)))
  166. (read-c-struct (pointer->bytevector foreign size) 0 types)))
  167. ;;;
  168. ;;; Wrapped pointer types.
  169. ;;;
  170. (define-syntax define-wrapped-pointer-type
  171. (lambda (stx)
  172. "Define helper procedures to wrap pointer objects into Scheme
  173. objects with a disjoint type. Specifically, this macro defines PRED, a
  174. predicate for the new Scheme type, WRAP, a procedure that takes a
  175. pointer object and returns an object that satisfies PRED, and UNWRAP
  176. which does the reverse. PRINT must name a user-defined object printer."
  177. (syntax-case stx ()
  178. ((_ type-name pred wrap unwrap print)
  179. (with-syntax ((%wrap (datum->syntax #'wrap (gensym "wrap"))))
  180. #'(begin
  181. (define-record-type type-name
  182. (%wrap pointer)
  183. pred
  184. (pointer unwrap))
  185. (define wrap
  186. ;; Use a weak hash table to preserve pointer identity, i.e.,
  187. ;; PTR1 == PTR2 <-> (eq? (wrap PTR1) (wrap PTR2)).
  188. (let ((ptr->obj (make-weak-value-hash-table 3000)))
  189. (lambda (ptr)
  190. (or (hash-ref ptr->obj ptr)
  191. (let ((o (%wrap ptr)))
  192. (hash-set! ptr->obj ptr o)
  193. o)))))
  194. (set-record-type-printer! type-name print)))))))