compile.scm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ;;; Test script to compile a Scheme expression to wasm
  2. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  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. (use-modules (wasm assemble)
  16. (hoot compile)
  17. (ice-9 binary-ports)
  18. (ice-9 match))
  19. (define* (compile-expr expr out #:key import-abi? export-abi?)
  20. (let ((bytes (assemble-wasm
  21. (compile expr
  22. #:import-abi? import-abi?
  23. #:export-abi? export-abi?
  24. #:dump-cps? #t
  25. #:dump-wasm? #t))))
  26. (call-with-output-file out
  27. (lambda (port) (put-bytevector port bytes)))))
  28. (define (read1 str)
  29. (call-with-input-string
  30. str
  31. (lambda (port)
  32. (let ((expr (read port)))
  33. (when (eof-object? expr)
  34. (error "No expression to evaluate"))
  35. (let ((tail (read port)))
  36. (unless (eof-object? tail)
  37. (error "Unexpected trailing expression" tail)))
  38. expr))))
  39. (when (batch-mode?)
  40. (match (program-arguments)
  41. ((arg0 . args)
  42. (let lp ((args args) (import-abi? #f) (export-abi? #f))
  43. (match args
  44. (("--import-abi" . args) (lp args #t export-abi?))
  45. (("--export-abi" . args) (lp args import-abi? #t))
  46. ((str out) (compile-expr (read1 str) out
  47. #:import-abi? import-abi?
  48. #:export-abi? export-abi?))
  49. (_
  50. (format (current-error-port) "usage: ~a EXPR OUT.WASM\n" arg0)
  51. (exit 1)))))))