compile-wasm.scm 16 KB

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