compile-wasm.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. ;;; Compile --- Command-line Guile Scheme compiler -*- coding: iso-8859-1 -*-
  2. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  3. ;;; Copyright 2005,2008-2011,2013-2015,2017-2020 Free Software Foundation, Inc.
  4. ;;;
  5. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  6. ;;; you may not use this file except in compliance with the License.
  7. ;;; You may obtain a copy of the License at
  8. ;;;
  9. ;;; http://www.apache.org/licenses/LICENSE-2.0
  10. ;;;
  11. ;;; Unless required by applicable law or agreed to in writing, software
  12. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  13. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ;;; See the License for the specific language governing permissions and
  15. ;;; limitations under the License.
  16. ;;; Commentary:
  17. ;;;
  18. ;;; Usage: compile-wasm [ARGS]
  19. ;;;
  20. ;;; A command-line interface to the Guile-to-WebAssembly compiler.
  21. ;;;
  22. ;;; Code:
  23. (define-module (scripts compile-wasm)
  24. #:use-module ((system base compile) #:select (default-warning-level
  25. default-optimization-level))
  26. #:use-module (ice-9 format)
  27. #:use-module (ice-9 match)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-37)
  30. #:use-module (system base message)
  31. #:use-module (system base optimize)
  32. #:use-module (hoot compile)
  33. #:use-module (hoot library-group)
  34. #:export (compile-wasm))
  35. (define %summary "Compile a file to WebAssembly.")
  36. (define (fail message . args)
  37. (format (current-error-port) "error: ~?~%" message args)
  38. (exit 1))
  39. (define %options
  40. ;; Specifications of the command-line options.
  41. (list (option '(#\h "help") #f #f
  42. (lambda (opt name arg result)
  43. (alist-cons 'help? #t result)))
  44. (option '("version") #f #f
  45. (lambda (opt name arg result)
  46. (show-version)
  47. (exit 0)))
  48. (option '(#\L "load-path") #t #f
  49. (lambda (opt name arg result)
  50. (let ((load-path (assoc-ref result 'load-path)))
  51. (alist-cons 'load-path (cons arg load-path)
  52. result))))
  53. (option '(#\o "output") #t #f
  54. (lambda (opt name arg result)
  55. (if (assoc-ref result 'output-file)
  56. (fail "`-o' option cannot be specified more than once")
  57. (alist-cons 'output-file arg result))))
  58. (option '("r6rs") #f #f
  59. (lambda (opt name arg result)
  60. (alist-cons 'install-r6rs? #t result)))
  61. (option '("dump-wasm") #f #f
  62. (lambda (opt name arg result)
  63. (alist-cons 'dump-wasm? #t result)))
  64. (option '("dump-cps") #f #f
  65. (lambda (opt name arg result)
  66. (alist-cons 'dump-cps? #t result)))
  67. (option '("r7rs") #f #f
  68. (lambda (opt name arg result)
  69. (alist-cons 'install-r7rs? #t result)))
  70. (option '(#\x) #t #f
  71. (lambda (opt name arg result)
  72. (set! %load-extensions (cons arg %load-extensions))
  73. result))
  74. (option '(#\W "warn") #t #f
  75. (lambda (opt name arg result)
  76. (match arg
  77. ("help"
  78. (show-warning-help)
  79. (exit 0))
  80. ((? string->number)
  81. (let ((n (string->number arg)))
  82. (unless (and (exact-integer? n) (<= 0 n))
  83. (fail "Bad warning level `~a'" n))
  84. (alist-cons 'warning-level n
  85. (alist-delete 'warning-level result))))
  86. (_
  87. (let ((warnings (assoc-ref result 'warnings)))
  88. (alist-cons 'warnings
  89. (cons (string->symbol arg) warnings)
  90. (alist-delete 'warnings result)))))))
  91. (option '(#\O "optimize") #t #f
  92. (lambda (opt name arg result)
  93. (define (return val)
  94. (alist-cons 'optimizations val result))
  95. (define (return-option name val)
  96. (let ((kw (symbol->keyword
  97. (string->symbol (string-append name "?")))))
  98. (unless (assq kw (available-optimizations))
  99. (fail "Unknown optimization pass `~a'" name))
  100. (return (list kw val))))
  101. (cond
  102. ((string=? arg "help")
  103. (show-optimization-help)
  104. (exit 0))
  105. ((string->number arg)
  106. => (lambda (level)
  107. (unless (and (exact-integer? level) (<= 0 level 9))
  108. (fail "Bad optimization level `~a'" level))
  109. (alist-cons 'optimization-level level
  110. (alist-delete 'optimization-level result))))
  111. ((string-prefix? "no-" arg)
  112. (return-option (substring arg 3) #f))
  113. (else
  114. (return-option arg #t)))))))
  115. (define (parse-args args)
  116. "Parse argument list @var{args} and return an alist with all the relevant
  117. options."
  118. (args-fold args %options
  119. (lambda (opt name arg result)
  120. (format (current-error-port) "~A: unrecognized option~%" name)
  121. (exit 1))
  122. (lambda (file result)
  123. (let ((input-files (assoc-ref result 'input-files)))
  124. (alist-cons 'input-files (cons file input-files)
  125. result)))
  126. ;; default option values
  127. `((input-files)
  128. (load-path)
  129. (warning-level . ,(default-warning-level))
  130. (optimization-level . ,(default-optimization-level))
  131. (warnings unsupported-warning))))
  132. (define (show-version)
  133. (format #t "compile-wasm ~A~%" (version))
  134. (format #t "Copyright (C) 2023 Spritely Institute, Igalia.
  135. Part of guile-hoot:
  136. https://gitlab.com/spritely/guile-hoot
  137. Licensed under the Apache License, Version 2.0:
  138. http://www.apache.org/licenses/LICENSE-2.0
  139. This is free software: you are free to change and redistribute it.
  140. There is NO WARRANTY, to the extent permitted by law.~%"))
  141. (define (show-warning-help)
  142. (format #t "The available warning types are:~%~%")
  143. (for-each (lambda (wt)
  144. (format #t " ~22A ~A~%"
  145. (format #f "`~A'" (warning-type-name wt))
  146. (warning-type-description wt)))
  147. %warning-types)
  148. (format #t "~%")
  149. (format #t "You may also specify warning levels as `-W0`, `-W1',~%")
  150. (format #t "`-W2', or `-W3'. The default is `-W1'.~%"))
  151. (define (show-optimization-help)
  152. (format #t "The available optimizations are:~%~%")
  153. (let lp ((options (available-optimizations)))
  154. (match options
  155. (() #t)
  156. (((kw level) . options)
  157. (let ((name (string-trim-right (symbol->string (keyword->symbol kw))
  158. #\?)))
  159. (format #t " -O~a~%" name)
  160. (lp options)))))
  161. (format #t "~%")
  162. (format #t "To disable an optimization, prepend it with `no-', for example~%")
  163. (format #t "`-Ono-cse.'~%~%")
  164. (format #t "You may also specify optimization levels as `-O0', `-O1',~%")
  165. (format #t "`-O2', or `-O3'. Currently `-O0' turns off all optimizations,~%")
  166. (format #t "`-O1' turns on partial evaluation, and `-O2' and `-O3' turn on~%")
  167. (format #t "everything. The default is equivalent to `-O2'.")
  168. (format #t "~%"))
  169. (define (compile-wasm . args)
  170. (let* ((options (parse-args args))
  171. (help? (assoc-ref options 'help?))
  172. (warning-level (assoc-ref options 'warning-level))
  173. (optimization-level (assoc-ref options 'optimization-level))
  174. (dump-wasm? (assoc-ref options 'dump-wasm?))
  175. (dump-cps? (assoc-ref options 'dump-cps?))
  176. (compile-opts `(#:warnings
  177. ,(assoc-ref options 'warnings)
  178. ,@(append-map
  179. (lambda (opt)
  180. (match opt
  181. (('optimizations . opts) opts)
  182. (_ '())))
  183. options)))
  184. (input-files (assoc-ref options 'input-files))
  185. (output-file (assoc-ref options 'output-file))
  186. (load-path (assoc-ref options 'load-path)))
  187. (when (or help? (null? input-files))
  188. (format #t "Usage: compile-wasm [OPTION] FILE
  189. Compile the Guile source file FILE into a WebAssembly module file.
  190. -h, --help print this help message
  191. -L, --load-path=DIR add DIR to the front of the module load path
  192. -o, --output=OFILE write output to OFILE
  193. -x EXTENSION add EXTENSION to the set of source file extensions
  194. -W, --warn=WARNING emit warnings of type WARNING; use `--warn=help'
  195. for a list of available warnings
  196. -O, --optimize=OPT specify optimization passes to run; use `-Ohelp'
  197. for a list of available optimizations
  198. --r6rs, --r7rs compile in an environment whose default bindings,
  199. reader options, and load paths are adapted for
  200. specific Scheme standards; see \"R6RS Support\"
  201. and \"R7RS Support\" in the manual, for full details
  202. --dump-cps print a debugging representation of the low-level
  203. CPS code, before generating WebAssembly
  204. --dump-wasm print a debugging representation of the generated
  205. WebAssembly code
  206. Report bugs to <~A>.~%"
  207. %guile-bug-report-address)
  208. (exit 0))
  209. (when (assoc-ref options 'install-r6rs?)
  210. (install-r6rs!))
  211. (when (assoc-ref options 'install-r7rs?)
  212. (install-r7rs!))
  213. (unless output-file
  214. (fail "missing output file (pass `-o FILE')"))
  215. ;; Install a SIGINT handler. As a side effect, this gives unwind
  216. ;; handlers an opportunity to run upon SIGINT; this includes that of
  217. ;; 'call-with-output-file/atomic', called by 'compile-file', which
  218. ;; removes the temporary output file.
  219. (sigaction SIGINT
  220. (lambda args
  221. (fail "interrupted by the user")))
  222. (define (read-forms-from-file filename)
  223. (call-with-include-port
  224. (datum->syntax #f (canonicalize-path filename))
  225. (lambda (p)
  226. (let lp ()
  227. (match (read-syntax p)
  228. ((? eof-object?) #'())
  229. (x (cons x (lp))))))))
  230. (define (load-library-from-file filename)
  231. (define trusted? #f)
  232. (match (read-forms-from-file filename)
  233. ((form) (parse-r6rs-library form trusted?))
  234. (forms (error "expected exactly one form" forms))))
  235. (define (library-name->file-name name)
  236. (string-join (map symbol->string name) file-name-separator-string))
  237. (define (locate-library name)
  238. (search-path load-path (library-name->file-name name) %load-extensions))
  239. (match input-files
  240. (() (fail "missing input file"))
  241. ((input-file)
  242. (with-fluids ((*current-warning-prefix* ""))
  243. (compile-file input-file
  244. #:extend-load-library
  245. (lambda (load-library)
  246. (lambda (name)
  247. (cond
  248. ((load-library name))
  249. ((locate-library name) => load-library-from-file)
  250. (else #f))))
  251. #:output-file output-file
  252. #:warning-level warning-level
  253. #:optimization-level optimization-level
  254. #:dump-cps? dump-cps?
  255. #:dump-wasm? dump-wasm?
  256. #:opts compile-opts))
  257. (format #t "wrote `~A'\n" output-file))
  258. (_ (fail "multiple input files not supported")))))
  259. (define main compile-wasm)