1 ;;; High-level compiler interface
3 ;; Copyright (C) 2001, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (system base compile)
22 #:use-module (system base syntax)
23 #:use-module (system base language)
24 #:use-module (system base message)
25 #:use-module (system vm vm) ;; FIXME: there's a reason for this, can't remember why tho
26 #:use-module (ice-9 regex)
27 #:use-module (ice-9 optargs)
28 #:use-module (ice-9 receive)
29 #:export (compiled-file-name
41 (define (call-once thunk)
46 (error "thunk may only be entered once: ~a" thunk))
51 ;; (put 'call-with-output-file/atomic 'scheme-indent-function 1)
52 (define* (call-with-output-file/atomic filename proc #:optional reference)
53 (let* ((template (string-append filename ".XXXXXX"))
54 (tmp (mkstemp! template)))
57 (with-throw-handler #t
60 (chmod tmp (logand #o0666 (lognot (umask))))
62 (rename-file template filename))
64 (delete-file template)))))))
66 (define (ensure-language x)
71 ;; Throws an exception if `dir' is not writable. The mkdir occurs
72 ;; before the check, so that we avoid races (possibly due to parallel
75 (define (ensure-directory dir)
79 (lambda (k subr fmt args rest)
80 (let ((errno (and (pair? rest) (car rest))))
83 ;; Assume it's a writable directory, to avoid TOCTOU errors,
84 ;; as well as UID/EUID mismatches that occur with access(2).
87 (ensure-directory (dirname dir))
88 (ensure-directory dir))
90 (throw k subr fmt args rest)))))))
92 ;;; This function is among the trickiest I've ever written. I tried many
93 ;;; variants. In the end, simple is best, of course.
95 ;;; After turning this around a number of times, it seems that the
96 ;;; desired behavior is that .go files should exist in a path, for
97 ;;; searching. That is orthogonal to this function. For writing .go
98 ;;; files, either you know where they should go, in which case you tell
99 ;;; compile-file explicitly, as in the srcdir != builddir case; or you
100 ;;; don't know, in which case this function is called, and we just put
101 ;;; them in your own ccache dir in ~/.cache/guile/ccache.
103 ;;; See also boot-9.scm:load.
104 (define (compiled-file-name file)
105 ;; FIXME: would probably be better just to append SHA1(canon-path)
106 ;; to the %compile-fallback-path, to avoid deep directory stats.
107 (define (canonical->suffix canon)
109 ((string-prefix? "/" canon) canon)
110 ((and (> (string-length canon) 2)
111 (eqv? (string-ref canon 1) #\:))
112 ;; Paths like C:... transform to /C...
113 (string-append "/" (substring canon 0 1) (substring canon 2)))
115 (define (compiled-extension)
116 (cond ((or (null? %load-compiled-extensions)
117 (string-null? (car %load-compiled-extensions)))
118 (warn "invalid %load-compiled-extensions"
119 %load-compiled-extensions)
121 (else (car %load-compiled-extensions))))
122 (and %compile-fallback-path
123 (let ((f (string-append
124 %compile-fallback-path
125 (canonical->suffix (canonicalize-path file))
126 (compiled-extension))))
127 (and (false-if-exception (ensure-directory (dirname f)))
130 (define* (compile-file file #:key
132 (from (current-language))
134 (env (default-environment from))
136 (canonicalization 'relative))
137 (with-fluids ((%file-port-name-canonicalization canonicalization))
138 (let* ((comp (or output-file (compiled-file-name file)
139 (error "failed to create path for auto-compiled file"
141 (in (open-input-file file))
142 (enc (file-encoding in)))
143 ;; Choose the input encoding deterministically.
144 (set-port-encoding! in (or enc "UTF-8"))
146 (ensure-directory (dirname comp))
147 (call-with-output-file/atomic comp
149 ((language-printer (ensure-language to))
150 (read-and-compile in #:env env #:from from #:to to #:opts opts)
155 (define* (compile-and-load file #:key (from 'scheme) (to 'value)
156 (env (current-module)) (opts '())
157 (canonicalization 'relative))
158 (with-fluids ((%file-port-name-canonicalization canonicalization))
159 (read-and-compile (open-input-file file)
160 #:from from #:to to #:opts opts
165 ;;; Compiler interface
168 (define (compile-passes from to opts)
170 (or (lookup-compilation-order from to)
171 (error "no way to compile" from "to" to))))
173 (define (compile-fold passes exp env opts)
174 (let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t))
177 (receive (x e new-cenv) ((car passes) x e opts)
178 (lp (cdr passes) x e (if first? new-cenv cenv) #f)))))
180 (define (find-language-joint from to)
181 (let lp ((in (reverse (or (lookup-compilation-order from to)
182 (error "no way to compile" from "to" to))))
185 (error "don't know how to join expressions" from to))
186 ((language-joiner lang) lang)
188 (lp (cdr in) (caar in))))))
190 (define* (read-and-compile port #:key
191 (from (current-language))
193 (env (default-environment from))
195 (let ((from (ensure-language from))
196 (to (ensure-language to)))
197 (let ((joint (find-language-joint from to)))
198 (with-fluids ((*current-language* from))
199 (let lp ((exps '()) (env #f) (cenv env))
200 (let ((x ((language-reader (current-language)) port cenv)))
203 (compile ((language-joiner joint) (reverse exps) env)
205 ;; env can be false if no expressions were read.
206 #:env (or env (default-environment joint))
209 ;; compile-fold instead of compile so we get the env too
210 (receive (jexp jenv jcenv)
211 (compile-fold (compile-passes (current-language) joint opts)
213 (lp (cons jexp exps) jenv jcenv))))))))))
215 (define* (compile x #:key
216 (from (current-language))
218 (env (default-environment from))
221 (let ((warnings (memq #:warnings opts)))
223 (let ((warnings (cadr warnings)))
224 ;; Sanity-check the requested warnings.
225 (for-each (lambda (w)
226 (or (lookup-warning-type w)
227 (warning 'unsupported-warning #f w)))
230 (receive (exp env cenv)
231 (compile-fold (compile-passes from to opts) x env opts)
236 ;;; Decompiler interface
239 (define (decompile-passes from to opts)
241 (or (lookup-decompilation-order from to)
242 (error "no way to decompile" from "to" to))))
244 (define (decompile-fold passes exp env opts)
247 (receive (exp env) ((car passes) exp env opts)
248 (decompile-fold (cdr passes) exp env opts))))
250 (define* (decompile x #:key
255 (decompile-fold (decompile-passes from to opts)