Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / module / system / base / compile.scm
1 ;;; High-level compiler interface
2
3 ;; Copyright (C) 2001, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4
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.
9 ;;;
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.
14 ;;;
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
18
19 ;;; Code:
20
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
30 compile-file
31 compile-and-load
32 read-and-compile
33 compile
34 decompile))
35
36
37 ;;;
38 ;;; Compiler
39 ;;;
40
41 (define (call-once thunk)
42 (let ((entered #f))
43 (dynamic-wind
44 (lambda ()
45 (if entered
46 (error "thunk may only be entered once: ~a" thunk))
47 (set! entered #t))
48 thunk
49 (lambda () #t))))
50
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)))
55 (call-once
56 (lambda ()
57 (with-throw-handler #t
58 (lambda ()
59 (proc tmp)
60 (chmod tmp (logand #o0666 (lognot (umask))))
61 (close-port tmp)
62 (rename-file template filename))
63 (lambda args
64 (delete-file template)))))))
65
66 (define (ensure-language x)
67 (if (language? x)
68 x
69 (lookup-language x)))
70
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
73 ;; compilation).
74 ;;
75 (define (ensure-directory dir)
76 (catch 'system-error
77 (lambda ()
78 (mkdir dir))
79 (lambda (k subr fmt args rest)
80 (let ((errno (and (pair? rest) (car rest))))
81 (cond
82 ((eqv? errno EEXIST)
83 ;; Assume it's a writable directory, to avoid TOCTOU errors,
84 ;; as well as UID/EUID mismatches that occur with access(2).
85 #t)
86 ((eqv? errno ENOENT)
87 (ensure-directory (dirname dir))
88 (ensure-directory dir))
89 (else
90 (throw k subr fmt args rest)))))))
91
92 ;;; This function is among the trickiest I've ever written. I tried many
93 ;;; variants. In the end, simple is best, of course.
94 ;;;
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.
102 ;;;
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)
108 (cond
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)))
114 (else canon)))
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)
120 ".go")
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)))
128 f))))
129
130 (define* (compile-file file #:key
131 (output-file #f)
132 (from (current-language))
133 (to 'objcode)
134 (env (default-environment from))
135 (opts '())
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"
140 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"))
145
146 (ensure-directory (dirname comp))
147 (call-with-output-file/atomic comp
148 (lambda (port)
149 ((language-printer (ensure-language to))
150 (read-and-compile in #:env env #:from from #:to to #:opts opts)
151 port))
152 file)
153 comp)))
154
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
161 #:env env)))
162
163 \f
164 ;;;
165 ;;; Compiler interface
166 ;;;
167
168 (define (compile-passes from to opts)
169 (map cdr
170 (or (lookup-compilation-order from to)
171 (error "no way to compile" from "to" to))))
172
173 (define (compile-fold passes exp env opts)
174 (let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t))
175 (if (null? passes)
176 (values x e cenv)
177 (receive (x e new-cenv) ((car passes) x e opts)
178 (lp (cdr passes) x e (if first? new-cenv cenv) #f)))))
179
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))))
183 (lang to))
184 (cond ((null? in)
185 (error "don't know how to join expressions" from to))
186 ((language-joiner lang) lang)
187 (else
188 (lp (cdr in) (caar in))))))
189
190 (define* (read-and-compile port #:key
191 (from (current-language))
192 (to 'objcode)
193 (env (default-environment from))
194 (opts '()))
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)))
201 (cond
202 ((eof-object? x)
203 (compile ((language-joiner joint) (reverse exps) env)
204 #:from joint #:to to
205 ;; env can be false if no expressions were read.
206 #:env (or env (default-environment joint))
207 #:opts opts))
208 (else
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)
212 x cenv opts)
213 (lp (cons jexp exps) jenv jcenv))))))))))
214
215 (define* (compile x #:key
216 (from (current-language))
217 (to 'value)
218 (env (default-environment from))
219 (opts '()))
220
221 (let ((warnings (memq #:warnings opts)))
222 (if (pair? warnings)
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)))
228 warnings))))
229
230 (receive (exp env cenv)
231 (compile-fold (compile-passes from to opts) x env opts)
232 exp))
233
234 \f
235 ;;;
236 ;;; Decompiler interface
237 ;;;
238
239 (define (decompile-passes from to opts)
240 (map cdr
241 (or (lookup-decompilation-order from to)
242 (error "no way to decompile" from "to" to))))
243
244 (define (decompile-fold passes exp env opts)
245 (if (null? passes)
246 (values exp env)
247 (receive (exp env) ((car passes) exp env opts)
248 (decompile-fold (cdr passes) exp env opts))))
249
250 (define* (decompile x #:key
251 (env #f)
252 (from 'value)
253 (to 'assembly)
254 (opts '()))
255 (decompile-fold (decompile-passes from to opts)
256 x
257 env
258 opts))