compile.scm 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ;;; Test script to compile a Scheme expression to wasm
  2. ;;; Copyright (C) 2023 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 d8 (or (getenv "D8") "d8"))
  20. (define srcdir (or (getenv "SRCDIR") (getcwd)))
  21. (define (scope-file file-name)
  22. (in-vicinity srcdir file-name))
  23. (define* (compile-expr expr out #:key import-abi? export-abi?)
  24. (let ((bytes (assemble-wasm
  25. (compile expr
  26. #:import-abi? import-abi?
  27. #:export-abi? export-abi?
  28. #:dump-cps? #t
  29. #:dump-wasm? #t))))
  30. (call-with-output-file out
  31. (lambda (port) (put-bytevector port bytes)))))
  32. (define (read1 str)
  33. (call-with-input-string
  34. str
  35. (lambda (port)
  36. (let ((expr (read port)))
  37. (when (eof-object? expr)
  38. (error "No expression to evaluate"))
  39. (let ((tail (read port)))
  40. (unless (eof-object? tail)
  41. (error "Unexpected trailing expression" tail)))
  42. expr))))
  43. (when (batch-mode?)
  44. (match (program-arguments)
  45. ((arg0 . args)
  46. (let lp ((args args) (import-abi? #f) (export-abi? #f))
  47. (match args
  48. (("--import-abi" . args) (lp args #t export-abi?))
  49. (("--export-abi" . args) (lp args import-abi? #t))
  50. ((str out) (compile-expr (read1 str) out
  51. #:import-abi? import-abi?
  52. #:export-abi? export-abi?))
  53. (_
  54. (format (current-error-port) "usage: ~a EXPR OUT.WASM\n" arg0)
  55. (exit 1)))))))