189 lines
		
	
	
		
			9.1 KiB
		
	
	
	
		
			Scheme
		
	
	
	
			
		
		
	
	
			189 lines
		
	
	
		
			9.1 KiB
		
	
	
	
		
			Scheme
		
	
	
	
(define size-of-type
 | 
						|
  (lambda (type)
 | 
						|
    (cond ((eq? type 'int8) (c-sizeof int8_t))
 | 
						|
          ((eq? type 'uint8) (c-sizeof uint8_t))
 | 
						|
          ((eq? type 'int16) (c-sizeof int16_t))
 | 
						|
          ((eq? type 'uint16) (c-sizeof uint16_t))
 | 
						|
          ((eq? type 'int32) (c-sizeof int32_t))
 | 
						|
          ((eq? type 'uint32) (c-sizeof uint32_t))
 | 
						|
          ((eq? type 'int64) (c-sizeof int64_t))
 | 
						|
          ((eq? type 'uint64) (c-sizeof uint64_t))
 | 
						|
          ((eq? type 'char) (c-sizeof char))
 | 
						|
          ((eq? type 'unsigned-char) (c-sizeof char))
 | 
						|
          ((eq? type 'short) (c-sizeof short))
 | 
						|
          ((eq? type 'unsigned-short) (c-sizeof unsigned-short))
 | 
						|
          ((eq? type 'int) (c-sizeof int))
 | 
						|
          ((eq? type 'unsigned-int) (c-sizeof unsigned-int))
 | 
						|
          ((eq? type 'long) (c-sizeof long))
 | 
						|
          ((eq? type 'unsigned-long) (c-sizeof unsigned-long))
 | 
						|
          ((eq? type 'float) (c-sizeof float))
 | 
						|
          ((eq? type 'double) (c-sizeof double))
 | 
						|
          ((eq? type 'pointer) (c-sizeof void*))
 | 
						|
          ((eq? type 'struct) (c-sizeof void*))
 | 
						|
          ((eq? type 'callback) (c-sizeof void*))
 | 
						|
          ((eq? type 'void) 0)
 | 
						|
          (else #f))))
 | 
						|
 | 
						|
(define c-bytevector?
 | 
						|
  (lambda (object)
 | 
						|
    (number? object)))
 | 
						|
 | 
						|
(define c-bytevector-u8-set!
 | 
						|
  (lambda (c-bytevector k byte)
 | 
						|
    (bytevector-c-int8-set! (make-bytevector-mapping (+ c-bytevector k)
 | 
						|
                                                     (c-size-of 'uint8))
 | 
						|
                            0
 | 
						|
                            byte)))
 | 
						|
 | 
						|
(define c-bytevector-u8-ref
 | 
						|
  (lambda (c-bytevector k)
 | 
						|
      (bytevector-c-int8-ref (make-bytevector-mapping (+ c-bytevector k)
 | 
						|
                                                      (c-size-of 'uint8))
 | 
						|
                             0)))
 | 
						|
(define c-bytevector-pointer-set!
 | 
						|
  (lambda (c-bytevector k pointer)
 | 
						|
    (let ((bv (make-bytevector-mapping (+ c-bytevector k) (c-size-of 'pointer))))
 | 
						|
      (bytevector-c-void*-set! bv 0 pointer))))
 | 
						|
(define c-bytevector-pointer-ref
 | 
						|
  (lambda (c-bytevector k)
 | 
						|
    (let ((bv (make-bytevector-mapping (+ c-bytevector k) (c-size-of 'pointer))))
 | 
						|
      (bytevector-c-void*-ref bv 0))))
 | 
						|
 | 
						|
#;(define pointer-set!
 | 
						|
  (lambda (pointer type offset value)
 | 
						|
    (let ((bv (make-bytevector-mapping (+ pointer offset) (c-size-of type))))
 | 
						|
      (cond ((equal? type 'int8) (bytevector-c-int8-set! bv 0 value))
 | 
						|
            ((equal? type 'uint8) (bytevector-c-int8-set! bv 0 value))
 | 
						|
            ((equal? type 'int16) (bytevector-c-int16-set! bv 0 value))
 | 
						|
            ((equal? type 'uint16) (bytevector-c-int16-set! bv 0 value))
 | 
						|
            ((equal? type 'int32) (bytevector-c-int32-set! bv 0 value))
 | 
						|
            ((equal? type 'uint32) (bytevector-c-int32-set! bv 0 value))
 | 
						|
            ((equal? type 'int64) (bytevector-c-int64-set! bv 0 value))
 | 
						|
            ((equal? type 'uint64) (bytevector-c-int64-set! bv 0 value))
 | 
						|
            ((equal? type 'char) (bytevector-c-int8-set! bv 0 (char->integer value)))
 | 
						|
            ((equal? type 'short) (bytevector-c-short-set! bv 0 value))
 | 
						|
            ((equal? type 'unsigned-short) (bytevector-c-short-set! bv 0 value))
 | 
						|
            ((equal? type 'int) (bytevector-c-int-set! bv 0 value))
 | 
						|
            ((equal? type 'unsigned-int) (bytevector-c-int-set! bv 0 value))
 | 
						|
            ((equal? type 'long) (bytevector-c-long-set! bv 0 value))
 | 
						|
            ((equal? type 'unsigned-long) (bytevector-c-long-set! bv 0 value))
 | 
						|
            ((equal? type 'float) (bytevector-c-float-set! bv 0 value))
 | 
						|
            ((equal? type 'double) (bytevector-c-double-set! bv 0 value))
 | 
						|
            ((equal? type 'void) (bytevector-c-void*-set! bv 0 value))
 | 
						|
            ((equal? type 'pointer) (bytevector-c-void*-set! bv 0 value))))))
 | 
						|
 | 
						|
#;(define pointer-get
 | 
						|
  (lambda (pointer type offset)
 | 
						|
    (let ((bv (make-bytevector-mapping (+ pointer offset) (c-size-of type))))
 | 
						|
      (cond ((equal? type 'int8) (bytevector-c-int8-ref bv 0))
 | 
						|
            ((equal? type 'uint8) (bytevector-c-uint8-ref bv 0))
 | 
						|
            ((equal? type 'int16) (bytevector-c-int16-ref bv 0))
 | 
						|
            ((equal? type 'uint16) (bytevector-c-uint16-ref bv 0))
 | 
						|
            ((equal? type 'int32) (bytevector-c-int32-ref bv 0))
 | 
						|
            ((equal? type 'uint32) (bytevector-c-uint32-ref bv 0))
 | 
						|
            ((equal? type 'int64) (bytevector-c-int64-ref bv 0))
 | 
						|
            ((equal? type 'uint64) (bytevector-c-uint64-ref bv 0))
 | 
						|
            ((equal? type 'char) (integer->char (bytevector-c-uint8-ref bv 0)))
 | 
						|
            ((equal? type 'short) (bytevector-c-short-ref bv 0))
 | 
						|
            ((equal? type 'unsigned-short) (bytevector-c-unsigned-short-ref bv 0))
 | 
						|
            ((equal? type 'int) (bytevector-c-int-ref bv 0))
 | 
						|
            ((equal? type 'unsigned-int) (bytevector-c-unsigned-int-ref bv 0))
 | 
						|
            ((equal? type 'long) (bytevector-c-long-ref bv 0))
 | 
						|
            ((equal? type 'unsigned-long) (bytevector-c-unsigned-long-ref bv 0))
 | 
						|
            ((equal? type 'float) (bytevector-c-float-ref bv 0))
 | 
						|
            ((equal? type 'double) (bytevector-c-double-ref bv 0))
 | 
						|
            ((equal? type 'void) (bytevector-c-void*-ref bv 0))
 | 
						|
            ((equal? type 'pointer) (bytevector-c-void*-ref bv 0))))))
 | 
						|
 | 
						|
(define shared-object-load
 | 
						|
  (lambda (path options)
 | 
						|
    (load-shared-object path)))
 | 
						|
 | 
						|
#;(define-macro
 | 
						|
  (type->native-type type)
 | 
						|
  `(cond ((equal? ,type 'int8) 'int8_t)
 | 
						|
         ((equal? ,type 'uint8) 'uint8_t)
 | 
						|
         ;((equal? ,type 'int16) 'int16_t)
 | 
						|
         ;((equal? ,type 'uint16) 'uint16_t)
 | 
						|
         ;((equal? ,type 'int32) 'int32_t)
 | 
						|
         ;((equal? ,type 'uint32) 'uint32_t)
 | 
						|
         ;((equal? ,type 'int64) 'int64_t)
 | 
						|
         ;((equal? ,type 'uint64) 'uint64_t)
 | 
						|
         ;((equal? ,type 'char) 'char)
 | 
						|
         ;((equal? ,type 'unsigned-char) 'char)
 | 
						|
         ;((equal? ,type 'short) 'short)
 | 
						|
         ;((equal? ,type 'unsigned-short) 'unsigned-short)
 | 
						|
         ((equal? ,type 'int) 'int)
 | 
						|
         ;((equal? ,type 'unsigned-int) 'unsigned-int)
 | 
						|
         ;((equal? ,type 'long) 'long)
 | 
						|
         ;((equal? ,type 'unsigned-long) 'unsigned-long)
 | 
						|
         ;((equal? ,type 'float) 'float)
 | 
						|
         ;((equal? ,type 'double) 'double)
 | 
						|
         ((equal? ,type 'pointer) 'void*)
 | 
						|
         ((equal? ,type 'void) 'void)
 | 
						|
         ;((equal? ,type 'callback) 'void*)
 | 
						|
         (else (error "type->native-type -- No such type" ,type))))
 | 
						|
 | 
						|
(define-macro
 | 
						|
  (define-c-procedure scheme-name shared-object c-name return-type argument-types)
 | 
						|
  (begin
 | 
						|
    (let ((type->native-type
 | 
						|
            (lambda (type)
 | 
						|
              (cond ((equal? type 'int8) 'int8_t)
 | 
						|
                    ((equal? type 'uint8) 'uint8_t)
 | 
						|
                    ((equal? type 'int16) 'int16_t)
 | 
						|
                    ((equal? type 'uint16) 'uint16_t)
 | 
						|
                    ((equal? type 'int32) 'int32_t)
 | 
						|
                    ((equal? type 'uint32) 'uint32_t)
 | 
						|
                    ((equal? type 'int64) 'int64_t)
 | 
						|
                    ((equal? type 'uint64) 'uint64_t)
 | 
						|
                    ((equal? type 'char) 'char)
 | 
						|
                    ((equal? type 'unsigned-char) 'char)
 | 
						|
                    ((equal? type 'short) 'short)
 | 
						|
                    ((equal? type 'unsigned-short) 'unsigned-short)
 | 
						|
                    ((equal? type 'int) 'int)
 | 
						|
                    ((equal? type 'unsigned-int) 'unsigned-int)
 | 
						|
                    ((equal? type 'long) 'long)
 | 
						|
                    ((equal? type 'unsigned-long) 'unsigned-long)
 | 
						|
                    ((equal? type 'float) 'float)
 | 
						|
                    ((equal? type 'double) 'double)
 | 
						|
                    ((equal? type 'pointer) 'void*)
 | 
						|
                    ((equal? type 'void) 'void)
 | 
						|
                    ((equal? type 'callback) 'void*)
 | 
						|
                    (else (error "type->native-type -- No such type" type))))))
 | 
						|
      `(define ,scheme-name
 | 
						|
         (c-function ,(type->native-type (cadr return-type))
 | 
						|
                     ,(cadr c-name)
 | 
						|
                     ,(map type->native-type (cadr argument-types)))))))
 | 
						|
 | 
						|
(define-macro
 | 
						|
  (define-c-callback scheme-name return-type argument-types procedure)
 | 
						|
  (let* ((type->native-type
 | 
						|
           (lambda (type)
 | 
						|
             (cond ((equal? type 'int8) 'int8_t)
 | 
						|
                   ((equal? type 'uint8) 'uint8_t)
 | 
						|
                   ((equal? type 'int16) 'int16_t)
 | 
						|
                   ((equal? type 'uint16) 'uint16_t)
 | 
						|
                   ((equal? type 'int32) 'int32_t)
 | 
						|
                   ((equal? type 'uint32) 'uint32_t)
 | 
						|
                   ((equal? type 'int64) 'int64_t)
 | 
						|
                   ((equal? type 'uint64) 'uint64_t)
 | 
						|
                   ((equal? type 'char) 'char)
 | 
						|
                   ((equal? type 'unsigned-char) 'char)
 | 
						|
                   ((equal? type 'short) 'short)
 | 
						|
                   ((equal? type 'unsigned-short) 'unsigned-short)
 | 
						|
                   ((equal? type 'int) 'int)
 | 
						|
                   ((equal? type 'unsigned-int) 'unsigned-int)
 | 
						|
                   ((equal? type 'long) 'long)
 | 
						|
                   ((equal? type 'unsigned-long) 'unsigned-long)
 | 
						|
                   ((equal? type 'float) 'float)
 | 
						|
                   ((equal? type 'double) 'double)
 | 
						|
                   ((equal? type 'pointer) 'void*)
 | 
						|
                   ((equal? type 'void) 'void)
 | 
						|
                   ((equal? type 'callback) 'void*)
 | 
						|
                   (else (error "type->native-type -- No such type" type)))))
 | 
						|
         (native-return-type (type->native-type (cadr return-type)))
 | 
						|
         (native-argument-types (map type->native-type (cadr argument-types))))
 | 
						|
    `(define ,scheme-name
 | 
						|
       (c-callback ,native-return-type ,native-argument-types ,procedure))))
 |