compile-wasm.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 (ice-9 binary-ports)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-37)
  31. #:use-module (system base message)
  32. #:use-module (system base optimize)
  33. #:use-module (hoot config)
  34. #:use-module (hoot compile)
  35. #:use-module (hoot frontend)
  36. #:use-module (hoot reflect)
  37. #:use-module (wasm assemble)
  38. #:export (compile-wasm))
  39. (define %summary "Compile a file to WebAssembly.")
  40. (define (fail message . args)
  41. (format (current-error-port) "error: ~?~%" message args)
  42. (exit 1))
  43. (define %options
  44. ;; Specifications of the command-line options.
  45. (list (option '(#\h "help") #f #f
  46. (lambda (opt name arg result)
  47. (alist-cons 'help? #t result)))
  48. (option '("version") #f #f
  49. (lambda (opt name arg result)
  50. (show-version)
  51. (exit 0)))
  52. (option '(#\L "load-path") #t #f
  53. (lambda (opt name arg result)
  54. (hoot-load-path (cons arg (hoot-load-path)))
  55. result))
  56. (option '(#\o "output") #t #f
  57. (lambda (opt name arg result)
  58. (if (assoc-ref result 'output-file)
  59. (fail "`-o' option cannot be specified more than once")
  60. (alist-cons 'output-file arg result))))
  61. (option '("r6rs") #f #f
  62. (lambda (opt name arg result)
  63. (alist-cons 'install-r6rs? #t result)))
  64. (option '("dump-wasm") #f #f
  65. (lambda (opt name arg result)
  66. (alist-cons 'dump-wasm? #t result)))
  67. (option '("dump-cps") #f #f
  68. (lambda (opt name arg result)
  69. (alist-cons 'dump-cps? #t result)))
  70. (option '("dump-tree-il") #f #f
  71. (lambda (opt name arg result)
  72. (alist-cons 'dump-tree-il? #t result)))
  73. (option '("run") #f #t
  74. (lambda (opt name arg result)
  75. (alist-cons 'vm
  76. (match arg (#f 'hoot) ("hoot" 'hoot) (arg arg))
  77. result)))
  78. (option '("async") #f #f
  79. (lambda (opt name arg result)
  80. (alist-cons 'async? #t result)))
  81. (option '("mode") #t #f
  82. (lambda (opt name arg result)
  83. (acons 'mode
  84. (cond
  85. ((string=? arg "primary") 'primary)
  86. ((string=? arg "standalone") 'standalone)
  87. ((string=? arg "secondary") 'secondary)
  88. (else (fail "unexpected `--mode' argument")))
  89. result)))
  90. (option '("emit-names") #f #f
  91. (lambda (opt name arg result)
  92. (alist-cons 'emit-names? #t result)))
  93. (option '("r7rs") #f #f
  94. (lambda (opt name arg result)
  95. (alist-cons 'install-r7rs? #t result)))
  96. (option '(#\x) #t #f
  97. (lambda (opt name arg result)
  98. (hoot-load-extensions (cons arg (hoot-load-extensions)))
  99. result))
  100. (option '(#\W "warn") #t #f
  101. (lambda (opt name arg result)
  102. (match arg
  103. ("help"
  104. (show-warning-help)
  105. (exit 0))
  106. ((? string->number)
  107. (let ((n (string->number arg)))
  108. (unless (and (exact-integer? n) (<= 0 n))
  109. (fail "Bad warning level `~a'" n))
  110. (alist-cons 'warning-level n
  111. (alist-delete 'warning-level result))))
  112. (_
  113. (let ((warnings (assoc-ref result 'warnings)))
  114. (alist-cons 'warnings
  115. (cons (string->symbol arg) warnings)
  116. (alist-delete 'warnings result)))))))
  117. (option '(#\O "optimize") #t #f
  118. (lambda (opt name arg result)
  119. (define (return val)
  120. (alist-cons 'optimizations val result))
  121. (define (return-option name val)
  122. (let ((kw (symbol->keyword
  123. (string->symbol (string-append name "?")))))
  124. (unless (assq kw (available-optimizations))
  125. (fail "Unknown optimization pass `~a'" name))
  126. (return (list kw val))))
  127. (cond
  128. ((string=? arg "help")
  129. (show-optimization-help)
  130. (exit 0))
  131. ((string->number arg)
  132. => (lambda (level)
  133. (unless (and (exact-integer? level) (<= 0 level 9))
  134. (fail "Bad optimization level `~a'" level))
  135. (alist-cons 'optimization-level level
  136. (alist-delete 'optimization-level result))))
  137. ((string-prefix? "no-" arg)
  138. (return-option (substring arg 3) #f))
  139. (else
  140. (return-option arg #t)))))))
  141. (define (parse-args args)
  142. "Parse argument list @var{args} and return an alist with all the relevant
  143. options."
  144. (args-fold args %options
  145. (lambda (opt name arg result)
  146. (format (current-error-port) "~A: unrecognized option~%" name)
  147. (exit 1))
  148. (lambda (file result)
  149. (let ((input-files (assoc-ref result 'input-files)))
  150. (alist-cons 'input-files (cons file input-files)
  151. result)))
  152. ;; default option values
  153. `((input-files)
  154. (warning-level . ,(default-warning-level))
  155. (optimization-level . ,(default-optimization-level))
  156. (warnings unsupported-warning)
  157. (mode . primary))))
  158. (define (show-version)
  159. (format #t "compile-wasm ~A~%" (version))
  160. (format #t "Copyright (C) 2023 Spritely Institute, Igalia.
  161. Part of guile-hoot:
  162. https://gitlab.com/spritely/guile-hoot
  163. Licensed under the Apache License, Version 2.0:
  164. http://www.apache.org/licenses/LICENSE-2.0
  165. This is free software: you are free to change and redistribute it.
  166. There is NO WARRANTY, to the extent permitted by law.~%"))
  167. (define (show-warning-help)
  168. (format #t "The available warning types are:~%~%")
  169. (for-each (lambda (wt)
  170. (format #t " ~22A ~A~%"
  171. (format #f "`~A'" (warning-type-name wt))
  172. (warning-type-description wt)))
  173. %warning-types)
  174. (format #t "~%")
  175. (format #t "You may also specify warning levels as `-W0`, `-W1',~%")
  176. (format #t "`-W2', or `-W3'. The default is `-W1'.~%"))
  177. (define (show-optimization-help)
  178. (format #t "The available optimizations are:~%~%")
  179. (let lp ((options (available-optimizations)))
  180. (match options
  181. (() #t)
  182. (((kw level) . options)
  183. (let ((name (string-trim-right (symbol->string (keyword->symbol kw))
  184. #\?)))
  185. (format #t " -O~a~%" name)
  186. (lp options)))))
  187. (format #t "~%")
  188. (format #t "To disable an optimization, prepend it with `no-', for example~%")
  189. (format #t "`-Ono-cse.'~%~%")
  190. (format #t "You may also specify optimization levels as `-O0', `-O1',~%")
  191. (format #t "`-O2', or `-O3'. Currently `-O0' turns off all optimizations,~%")
  192. (format #t "`-O1' turns on partial evaluation, and `-O2' and `-O3' turn on~%")
  193. (format #t "everything. The default is equivalent to `-O2'.")
  194. (format #t "~%"))
  195. (define (unwind-protect body unwind)
  196. (call-with-values
  197. (lambda ()
  198. (with-exception-handler
  199. (lambda (exn)
  200. (unwind)
  201. (raise-exception exn))
  202. body))
  203. (lambda vals
  204. (unwind)
  205. (apply values vals))))
  206. (define (call-with-named-output-file output-file f k)
  207. (call-with-values (lambda () (call-with-output-file output-file f))
  208. (lambda vals
  209. (format #t "wrote `~A'\n" output-file)
  210. (apply k output-file vals))))
  211. (define (call-with-temp-output-file f k)
  212. (let* ((wasm-port (mkstemp "/tmp/tmp-wasm-XXXXXX"))
  213. (wasm-file-name (port-filename wasm-port)))
  214. (call-with-values (lambda () (f wasm-port))
  215. (lambda vals
  216. (close-port wasm-port)
  217. (unwind-protect
  218. (lambda () (apply k wasm-file-name vals))
  219. (lambda () (delete-file wasm-file-name)))))))
  220. (define (call-with-no-output-file f k)
  221. (call-with-values (lambda () (f #f))
  222. (lambda vals
  223. (apply k #f vals))))
  224. (define* (call-with-output-file-as-needed f k
  225. #:key named-output-file needs-file?)
  226. (cond
  227. (named-output-file (call-with-named-output-file named-output-file f k))
  228. (needs-file? (call-with-temp-output-file f k))
  229. (else (call-with-no-output-file f k))))
  230. (define (run/hoot wasm async?)
  231. (call-with-values (lambda ()
  232. (if async?
  233. (hoot-apply-async (hoot-load (hoot-instantiate wasm)))
  234. (hoot-load (hoot-instantiate wasm))))
  235. (lambda vals
  236. (for-each (lambda (x)
  237. (hoot-print x (current-output-port))
  238. (newline))
  239. vals))))
  240. (define (run/js wasm-file js async?)
  241. ;; system* rather than execlp, to give us a chance to remove any
  242. ;; temporary output file.
  243. (define runner (in-vicinity %js-runner-dir
  244. (if async? "load-async.js" "load.js")))
  245. (exit (status:exit-val
  246. (system* js runner "--" %reflect-js-dir %reflect-wasm-dir
  247. wasm-file))))
  248. (define (compile-wasm . args)
  249. (let* ((options (parse-args args))
  250. (help? (assoc-ref options 'help?))
  251. (warning-level (assoc-ref options 'warning-level))
  252. (optimization-level (assoc-ref options 'optimization-level))
  253. (import-abi? (match (assq-ref options 'mode)
  254. ((or 'standalone 'primary) #f)
  255. ('secondary #t)))
  256. (export-abi? (match (assq-ref options 'mode)
  257. ('primary #t)
  258. ((or 'standalone 'secondary) #f)))
  259. (dump-wasm? (assoc-ref options 'dump-wasm?))
  260. (dump-cps? (assoc-ref options 'dump-cps?))
  261. (dump-tree-il? (assoc-ref options 'dump-tree-il?))
  262. (emit-names? (assoc-ref options 'emit-names?))
  263. (compile-opts `(#:warnings
  264. ,(assoc-ref options 'warnings)
  265. ,@(append-map
  266. (lambda (opt)
  267. (match opt
  268. (('optimizations . opts) opts)
  269. (_ '())))
  270. options)))
  271. (input-files (assoc-ref options 'input-files))
  272. (output-file (assoc-ref options 'output-file))
  273. (vm (or (assoc-ref options 'vm)
  274. (and (not output-file)
  275. 'hoot)))
  276. (async? (assoc-ref options 'async?)))
  277. (when (or help? (null? input-files))
  278. (format #t "Usage: compile-wasm [OPTION] FILE
  279. Compile the Guile source file FILE into a WebAssembly module file.
  280. -h, --help print this help message
  281. -L, --load-path=DIR add DIR to the front of the module load path
  282. -o, --output=OFILE write output to OFILE
  283. -x EXTENSION add EXTENSION to the set of source file extensions
  284. -W, --warn=WARNING emit warnings of type WARNING; use `--warn=help'
  285. for a list of available warnings
  286. -O, --optimize=OPT specify optimization passes to run; use `-Ohelp'
  287. for a list of available optimizations
  288. --r6rs, --r7rs compile in an environment whose default bindings,
  289. reader options, and load paths are adapted for
  290. specific Scheme standards; see \"R6RS Support\"
  291. and \"R7RS Support\" in the manual, for full details
  292. --run, --run=JS run the compiled wasm; by default, in Hoot
  293. virtual machine, otherwise using a JavaScript shell
  294. --async when combined with `--run', run program in async
  295. context.
  296. --mode=primary compile a main module: one which defines run-time
  297. facilities and which by default makes them
  298. available to secondary modules. The default mode.
  299. --mode=standalone like `--mode=primary', but without the
  300. possibility of sharing run-time facilities with
  301. secondary modules
  302. --mode=secondary compile an auxiliary module: one which imports
  303. runtime facilities instead of defining and exporting
  304. them.
  305. --dump-tree-il print a debugging representation of the high-level
  306. expanded and optimized Scheme code
  307. --dump-cps print a debugging representation of the low-level
  308. CPS code, before generating WebAssembly
  309. --dump-wasm print a debugging representation of the generated
  310. WebAssembly code
  311. --emit-names emit WebAssembly name section for debugging
  312. Report bugs to <~A>.~%"
  313. %guile-bug-report-address)
  314. (exit 0))
  315. (when (assoc-ref options 'install-r6rs?)
  316. (install-r6rs!))
  317. (when (assoc-ref options 'install-r7rs?)
  318. (install-r7rs!))
  319. ;; Install a SIGINT handler. As a side effect, this gives unwind
  320. ;; handlers an opportunity to run upon SIGINT; this includes that of
  321. ;; 'call-with-output-file/atomic', called by 'compile-file', which
  322. ;; removes the temporary output file.
  323. (sigaction SIGINT
  324. (lambda args
  325. (fail "interrupted by the user")))
  326. (match input-files
  327. (() (fail "missing input file"))
  328. ((input-file)
  329. (let ((wasm (with-fluids ((*current-warning-prefix* ""))
  330. (compile-file input-file
  331. #:extend-load-library
  332. (library-load-path-extension (hoot-load-path))
  333. #:warning-level warning-level
  334. #:optimization-level optimization-level
  335. #:import-abi? import-abi?
  336. #:export-abi? export-abi?
  337. #:dump-tree-il? dump-tree-il?
  338. #:dump-cps? dump-cps?
  339. #:dump-wasm? dump-wasm?
  340. #:emit-names? emit-names?
  341. #:opts compile-opts))))
  342. (call-with-output-file-as-needed
  343. (lambda (out)
  344. (when out
  345. (put-bytevector out (assemble-wasm wasm)))
  346. wasm)
  347. (lambda (output-file wasm)
  348. (match vm
  349. (#f (values))
  350. ('hoot (run/hoot wasm async?))
  351. (js (run/js output-file js async?))))
  352. #:named-output-file output-file
  353. #:needs-file? (not (eq? vm 'hoot))))
  354. #t)
  355. (_ (fail "multiple input files not supported")))))
  356. (define main compile-wasm)