compile.scm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. ;;; High-level compiler interface
  2. ;; Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;;; This library is free software; you can redistribute it and/or
  4. ;;; modify it under the terms of the GNU Lesser General Public
  5. ;;; License as published by the Free Software Foundation; either
  6. ;;; version 3 of the License, or (at your option) any later version.
  7. ;;;
  8. ;;; This library is distributed in the hope that it will be useful,
  9. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;; Lesser General Public License for more details.
  12. ;;;
  13. ;;; You should have received a copy of the GNU Lesser General Public
  14. ;;; License along with this library; if not, write to the Free Software
  15. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (system base compile)
  18. #:use-module (system base syntax)
  19. #:use-module (system base language)
  20. #:use-module (system base message)
  21. #:use-module (system vm vm) ;; FIXME: there's a reason for this, can't remember why tho
  22. #:use-module (ice-9 regex)
  23. #:use-module (ice-9 optargs)
  24. #:use-module (ice-9 receive)
  25. #:export (compiled-file-name
  26. compile-file
  27. compile-and-load
  28. read-and-compile
  29. compile
  30. decompile))
  31. ;;;
  32. ;;; Compiler
  33. ;;;
  34. (define (call-once thunk)
  35. (let ((entered #f))
  36. (dynamic-wind
  37. (lambda ()
  38. (if entered
  39. (error "thunk may only be entered once: ~a" thunk))
  40. (set! entered #t))
  41. thunk
  42. (lambda () #t))))
  43. ;; (put 'call-with-output-file/atomic 'scheme-indent-function 1)
  44. (define* (call-with-output-file/atomic filename proc #:optional reference)
  45. (let* ((template (string-append filename ".XXXXXX"))
  46. (tmp (mkstemp! template)))
  47. (call-once
  48. (lambda ()
  49. (with-throw-handler #t
  50. (lambda ()
  51. (proc tmp)
  52. (chmod tmp (logand #o0666 (lognot (umask))))
  53. (close-port tmp)
  54. (rename-file template filename))
  55. (lambda args
  56. (delete-file template)))))))
  57. (define (ensure-language x)
  58. (if (language? x)
  59. x
  60. (lookup-language x)))
  61. ;; Throws an exception if `dir' is not writable. The mkdir occurs
  62. ;; before the check, so that we avoid races (possibly due to parallel
  63. ;; compilation).
  64. ;;
  65. (define (ensure-writable-dir dir)
  66. (catch 'system-error
  67. (lambda ()
  68. (mkdir dir))
  69. (lambda (k subr fmt args rest)
  70. (let ((errno (and (pair? rest) (car rest))))
  71. (cond
  72. ((eqv? errno EEXIST)
  73. (let ((st (stat dir)))
  74. (if (or (not (eq? (stat:type st) 'directory))
  75. (not (access? dir W_OK)))
  76. (error "directory not writable" dir))))
  77. ((eqv? errno ENOENT)
  78. (ensure-writable-dir (dirname dir))
  79. (ensure-writable-dir dir))
  80. (else
  81. (throw k subr fmt args rest)))))))
  82. ;;; This function is among the trickiest I've ever written. I tried many
  83. ;;; variants. In the end, simple is best, of course.
  84. ;;;
  85. ;;; After turning this around a number of times, it seems that the the
  86. ;;; desired behavior is that .go files should exist in a path, for
  87. ;;; searching. That is orthogonal to this function. For writing .go
  88. ;;; files, either you know where they should go, in which case you tell
  89. ;;; compile-file explicitly, as in the srcdir != builddir case; or you
  90. ;;; don't know, in which case this function is called, and we just put
  91. ;;; them in your own ccache dir in ~/.guile-ccache.
  92. ;;;
  93. ;;; See also boot-9.scm:load.
  94. (define (compiled-file-name file)
  95. ;; FIXME: would probably be better just to append SHA1(canon-path)
  96. ;; to the %compile-fallback-path, to avoid deep directory stats.
  97. (define (canonical->suffix canon)
  98. (cond
  99. ((string-prefix? "/" canon) canon)
  100. ((and (> (string-length canon) 2)
  101. (eqv? (string-ref canon 1) #\:))
  102. ;; Paths like C:... transform to /C...
  103. (string-append "/" (substring canon 0 1) (substring canon 2)))
  104. (else canon)))
  105. (define (compiled-extension)
  106. (cond ((or (null? %load-compiled-extensions)
  107. (string-null? (car %load-compiled-extensions)))
  108. (warn "invalid %load-compiled-extensions"
  109. %load-compiled-extensions)
  110. ".go")
  111. (else (car %load-compiled-extensions))))
  112. (and %compile-fallback-path
  113. (let ((f (string-append
  114. %compile-fallback-path
  115. (canonical->suffix (canonicalize-path file))
  116. (compiled-extension))))
  117. (and (false-if-exception (ensure-writable-dir (dirname f)))
  118. f))))
  119. (define* (compile-file file #:key
  120. (output-file #f)
  121. (from (current-language))
  122. (to 'objcode)
  123. (env (default-environment from))
  124. (opts '())
  125. (canonicalization 'relative))
  126. (with-fluids ((%file-port-name-canonicalization canonicalization))
  127. (let* ((comp (or output-file (compiled-file-name file)
  128. (error "failed to create path for auto-compiled file"
  129. file)))
  130. (in (open-input-file file))
  131. (enc (file-encoding in)))
  132. ;; Choose the input encoding deterministically.
  133. (set-port-encoding! in (or enc "UTF-8"))
  134. (ensure-writable-dir (dirname comp))
  135. (call-with-output-file/atomic comp
  136. (lambda (port)
  137. ((language-printer (ensure-language to))
  138. (read-and-compile in #:env env #:from from #:to to #:opts opts)
  139. port))
  140. file)
  141. comp)))
  142. (define* (compile-and-load file #:key (from 'scheme) (to 'value)
  143. (env (current-module)) (opts '())
  144. (canonicalization 'relative))
  145. (with-fluids ((%file-port-name-canonicalization canonicalization))
  146. (read-and-compile (open-input-file file)
  147. #:from from #:to to #:opts opts
  148. #:env env)))
  149. ;;;
  150. ;;; Compiler interface
  151. ;;;
  152. (define (compile-passes from to opts)
  153. (map cdr
  154. (or (lookup-compilation-order from to)
  155. (error "no way to compile" from "to" to))))
  156. (define (compile-fold passes exp env opts)
  157. (let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t))
  158. (if (null? passes)
  159. (values x e cenv)
  160. (receive (x e new-cenv) ((car passes) x e opts)
  161. (lp (cdr passes) x e (if first? new-cenv cenv) #f)))))
  162. (define (find-language-joint from to)
  163. (let lp ((in (reverse (or (lookup-compilation-order from to)
  164. (error "no way to compile" from "to" to))))
  165. (lang to))
  166. (cond ((null? in)
  167. (error "don't know how to join expressions" from to))
  168. ((language-joiner lang) lang)
  169. (else
  170. (lp (cdr in) (caar in))))))
  171. (define* (read-and-compile port #:key
  172. (from (current-language))
  173. (to 'objcode)
  174. (env (default-environment from))
  175. (opts '()))
  176. (let ((from (ensure-language from))
  177. (to (ensure-language to)))
  178. (let ((joint (find-language-joint from to)))
  179. (with-fluids ((*current-language* from))
  180. (let lp ((exps '()) (env #f) (cenv env))
  181. (let ((x ((language-reader (current-language)) port cenv)))
  182. (cond
  183. ((eof-object? x)
  184. (compile ((language-joiner joint) (reverse exps) env)
  185. #:from joint #:to to
  186. ;; env can be false if no expressions were read.
  187. #:env (or env (default-environment joint))
  188. #:opts opts))
  189. (else
  190. ;; compile-fold instead of compile so we get the env too
  191. (receive (jexp jenv jcenv)
  192. (compile-fold (compile-passes (current-language) joint opts)
  193. x cenv opts)
  194. (lp (cons jexp exps) jenv jcenv))))))))))
  195. (define* (compile x #:key
  196. (from (current-language))
  197. (to 'value)
  198. (env (default-environment from))
  199. (opts '()))
  200. (let ((warnings (memq #:warnings opts)))
  201. (if (pair? warnings)
  202. (let ((warnings (cadr warnings)))
  203. ;; Sanity-check the requested warnings.
  204. (for-each (lambda (w)
  205. (or (lookup-warning-type w)
  206. (warning 'unsupported-warning #f w)))
  207. warnings))))
  208. (receive (exp env cenv)
  209. (compile-fold (compile-passes from to opts) x env opts)
  210. exp))
  211. ;;;
  212. ;;; Decompiler interface
  213. ;;;
  214. (define (decompile-passes from to opts)
  215. (map cdr
  216. (or (lookup-decompilation-order from to)
  217. (error "no way to decompile" from "to" to))))
  218. (define (decompile-fold passes exp env opts)
  219. (if (null? passes)
  220. (values exp env)
  221. (receive (exp env) ((car passes) exp env opts)
  222. (decompile-fold (cdr passes) exp env opts))))
  223. (define* (decompile x #:key
  224. (env #f)
  225. (from 'value)
  226. (to 'assembly)
  227. (opts '()))
  228. (decompile-fold (decompile-passes from to opts)
  229. x
  230. env
  231. opts))