1 ;;; High-level compiler interface
3 ;; Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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 ;; emacs: (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))
65 (delete-file template)))))))
67 (define (ensure-language x)
72 ;; Throws an exception if `dir' is not writable. The mkdir occurs
73 ;; before the check, so that we avoid races (possibly due to parallel
76 (define (ensure-directory dir)
80 (lambda (k subr fmt args rest)
81 (let ((errno (and (pair? rest) (car rest))))
84 ;; Assume it's a writable directory, to avoid TOCTOU errors,
85 ;; as well as UID/EUID mismatches that occur with access(2).
88 (ensure-directory (dirname dir))
89 (ensure-directory dir))
91 (throw k subr fmt args rest)))))))
93 ;;; This function is among the trickiest I've ever written. I tried many
94 ;;; variants. In the end, simple is best, of course.
96 ;;; After turning this around a number of times, it seems that the
97 ;;; desired behavior is that .go files should exist in a path, for
98 ;;; searching. That is orthogonal to this function. For writing .go
99 ;;; files, either you know where they should go, in which case you tell
100 ;;; compile-file explicitly, as in the srcdir != builddir case; or you
101 ;;; don't know, in which case this function is called, and we just put
102 ;;; them in your own ccache dir in ~/.cache/guile/ccache.
104 ;;; See also boot-9.scm:load.
105 (define (compiled-file-name file)
106 ;; FIXME: would probably be better just to append SHA1(canon-path)
107 ;; to the %compile-fallback-path, to avoid deep directory stats.
108 (define (canonical->suffix canon)
110 ((string-prefix? "/" canon) canon)
111 ((and (> (string-length canon) 2)
112 (eqv? (string-ref canon 1) #\:))
113 ;; Paths like C:... transform to /C...
114 (string-append "/" (substring canon 0 1) (substring canon 2)))
116 (define (compiled-extension)
117 (cond ((or (null? %load-compiled-extensions)
118 (string-null? (car %load-compiled-extensions)))
119 (warn "invalid %load-compiled-extensions"
120 %load-compiled-extensions)
122 (else (car %load-compiled-extensions))))
123 (and %compile-fallback-path
124 (let ((f (string-append
125 %compile-fallback-path
126 (canonical->suffix (canonicalize-path file))
127 (compiled-extension))))
128 (and (false-if-exception (ensure-directory (dirname f)))
131 (define* (compile-file file #:key
133 (from (current-language))
135 (env (default-environment from))
137 (canonicalization 'relative))
138 (with-fluids ((%file-port-name-canonicalization canonicalization))
139 (let* ((comp (or output-file (compiled-file-name file)
140 (error "failed to create path for auto-compiled file"
142 (in (open-input-file file))
143 (enc (file-encoding in)))
144 ;; Choose the input encoding deterministically.
145 (set-port-encoding! in (or enc "UTF-8"))
147 (ensure-directory (dirname comp))
148 (call-with-output-file/atomic comp
150 ((language-printer (ensure-language to))
151 (read-and-compile in #:env env #:from from #:to to #:opts opts)
156 (define* (compile-and-load file #:key (from (current-language)) (to 'value)
157 (env (current-module)) (opts '())
158 (canonicalization 'relative))
159 (with-fluids ((%file-port-name-canonicalization canonicalization))
160 (read-and-compile (open-input-file file)
161 #:from from #:to to #:opts opts
166 ;;; Compiler interface
169 (define (compile-passes from to opts)
171 (or (lookup-compilation-order from to)
172 (error "no way to compile" from "to" to))))
174 (define (compile-fold passes exp env opts)
175 (let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t))
178 (receive (x e new-cenv) ((car passes) x e opts)
179 (lp (cdr passes) x e (if first? new-cenv cenv) #f)))))
181 (define (find-language-joint from to)
182 (let lp ((in (reverse (or (lookup-compilation-order from to)
183 (error "no way to compile" from "to" to))))
185 (cond ((null? in) to)
186 ((language-joiner lang) lang)
188 (lp (cdr in) (caar in))))))
190 (define (default-language-joiner lang)
192 (if (and (pair? exps) (null? (cdr exps)))
195 "Multiple expressions read and compiled, but language has no joiner"
198 (define (read-and-parse lang port cenv)
199 (let ((exp ((language-reader lang) port cenv)))
201 ((eof-object? exp) exp)
202 ((language-parser lang) => (lambda (parse) (parse exp)))
205 (define* (read-and-compile port #:key
206 (from (current-language))
208 (env (default-environment from))
210 (let ((from (ensure-language from))
211 (to (ensure-language to)))
212 (let ((joint (find-language-joint from to)))
213 (parameterize ((current-language from))
214 (let lp ((exps '()) (env #f) (cenv env))
215 (let ((x (read-and-parse (current-language) port cenv)))
219 (compile ((or (language-joiner joint)
220 (default-language-joiner joint))
224 ;; env can be false if no expressions were read.
225 #:env (or env (default-environment joint))
228 ;; compile-fold instead of compile so we get the env too
229 (receive (jexp jenv jcenv)
230 (compile-fold (compile-passes (current-language) joint opts)
232 (lp (cons jexp exps) jenv jcenv))))))))))
234 (define* (compile x #:key
235 (from (current-language))
237 (env (default-environment from))
240 (let ((warnings (memq #:warnings opts)))
242 (let ((warnings (cadr warnings)))
243 ;; Sanity-check the requested warnings.
244 (for-each (lambda (w)
245 (or (lookup-warning-type w)
246 (warning 'unsupported-warning #f w)))
249 (receive (exp env cenv)
250 (compile-fold (compile-passes from to opts) x env opts)
255 ;;; Decompiler interface
258 (define (decompile-passes from to opts)
260 (or (lookup-decompilation-order from to)
261 (error "no way to decompile" from "to" to))))
263 (define (decompile-fold passes exp env opts)
266 (receive (exp env) ((car passes) exp env opts)
267 (decompile-fold (cdr passes) exp env opts))))
269 (define* (decompile x #:key
274 (decompile-fold (decompile-passes from to opts)