compile-wasm.scm 18 KB

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