ffi.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ;;; Hoot foreign function interface
  2. ;;; Copyright (C) 2023, 2024 David Thompson <dave@spritely.institute>
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Foreign function interface for declaring Wasm host imports and
  18. ;;; handling (ref extern) values.
  19. ;;;
  20. ;;; Code:
  21. (library (hoot ffi)
  22. (export external?
  23. external-null?
  24. external-non-null?
  25. procedure->external
  26. define-foreign)
  27. (import (hoot cond-expand)
  28. (hoot primitives)
  29. (hoot not)
  30. (hoot procedures)
  31. (hoot strings)
  32. (hoot errors)
  33. (hoot pairs)
  34. (only (hoot lists) map)
  35. (hoot numbers))
  36. (define (external? obj)
  37. (%inline-wasm
  38. '(func (param $obj (ref eq)) (result (ref eq))
  39. (ref.i31
  40. (if i32
  41. (ref.test $extern-ref (local.get $obj))
  42. (then (i32.const 17))
  43. (else (i32.const 1)))))
  44. obj))
  45. (define (external-null? extern)
  46. (check-type extern external? 'external-null?)
  47. (%inline-wasm
  48. '(func (param $extern (ref $extern-ref)) (result (ref eq))
  49. (if (ref eq)
  50. (ref.is_null
  51. (struct.get $extern-ref $val (local.get $extern)))
  52. (then (ref.i31 (i32.const 17)))
  53. (else (ref.i31 (i32.const 1)))))
  54. extern))
  55. (define (external-non-null? extern)
  56. (not (external-null? extern)))
  57. (define (procedure->external proc)
  58. (check-type proc procedure? 'procedure->external)
  59. (%inline-wasm
  60. '(func (param $f (ref $proc)) (result (ref eq))
  61. (struct.new $extern-ref
  62. (i32.const 0)
  63. (call $procedure->extern (local.get $f))))
  64. proc))
  65. (define-syntax define-foreign
  66. (lambda (x)
  67. (define (type-check exp proc-name)
  68. (define (check param predicate)
  69. #`(check-type #,param #,predicate '#,proc-name))
  70. (syntax-case exp (i32 i64 f32 f64 ref null eq string extern)
  71. ((x i32) (check #'x #'exact-integer?))
  72. ((x i64) (check #'x #'exact-integer?))
  73. ((x f32) (check #'x #'real?))
  74. ((x f64) (check #'x #'real?))
  75. ((x (ref eq)) #'#t)
  76. ((x (ref extern)) (check #'x #'external-non-null?))
  77. ((x (ref null extern)) (check #'x #'external?))
  78. ((x (ref string)) (check #'x #'string?))
  79. ((x type) (%error "unsupported param type" #'type))))
  80. (define (import-result-types exp)
  81. (syntax-case exp (none)
  82. (none #'())
  83. (type #'((result type)))))
  84. (define (result-types exp)
  85. (syntax-case exp (none i32 i64 f32 f64 ref null string extern)
  86. (none #'())
  87. (i32 #'((result i64)))
  88. (i64 #'((result i64)))
  89. (f32 #'((result f64)))
  90. (f64 #'((result f64)))
  91. ((ref string) #'((result (ref eq))))
  92. ((ref extern) #'((result (ref eq))))
  93. ((ref null extern) #'((result (ref eq))))
  94. ((ref eq) #'((result (ref eq))))
  95. (type (%error "unsupported result type" #'type))))
  96. (define (lift-result exp)
  97. (syntax-case exp (none i32 i64 f32 f64 ref null string extern)
  98. ((x none) #'x)
  99. ((x i32) #'(i64.extend_i32_s x))
  100. ((x i64) #'x)
  101. ((x f32) #'(f64.promote_f32 x))
  102. ((x f64) #'x)
  103. ((x (ref string)) #'(struct.new $string (i32.const 0) x))
  104. ((x (ref extern)) #'(struct.new $extern-ref (i32.const 0) x))
  105. ((x (ref null extern)) #'(struct.new $extern-ref (i32.const 0) x))
  106. ((x (ref eq)) #'(ref.cast $heap-object x))
  107. (type (%error "unsupported result type" #'type))))
  108. (define (fresh-wasm-id prefix)
  109. (datum->syntax x (gensym prefix)))
  110. (define (fresh-wasm-ids prefix lst)
  111. (map (lambda (_) (fresh-wasm-id prefix)) lst))
  112. (syntax-case x (->)
  113. ((_ proc-name mod name ptype ... -> rtype)
  114. (and (string? (syntax->datum #'mod)) (string? (syntax->datum #'name)))
  115. (with-syntax ((iname (fresh-wasm-id "$import-"))
  116. ((pname ...) (fresh-wasm-ids "$param-" #'(ptype ...))))
  117. #`(begin
  118. (cond-expand
  119. (guile-vm)
  120. (hoot
  121. (%wasm-import
  122. '(func iname (import mod name)
  123. (param ptype) ...
  124. #,@(import-result-types #'rtype)))))
  125. (define (proc-name pname ...)
  126. #,@(map (lambda (exp) (type-check exp #'proc-name))
  127. #'((pname ptype) ...))
  128. (%inline-wasm
  129. '(func (param pname ptype) ...
  130. #,@(result-types #'rtype)
  131. #,(lift-result
  132. #'((call iname (local.get pname) ...) rtype)))
  133. pname ...)))))))))