Fix handling of the *-gnux32 target.
[bpt/guile.git] / module / system / base / compile.scm
1 ;;; High-level compiler interface
2
3 ;; Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013 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 ;; 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)))
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 (close-port tmp)
65 (delete-file template)))))))
66
67 (define (ensure-language x)
68 (if (language? x)
69 x
70 (lookup-language x)))
71
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
74 ;; compilation).
75 ;;
76 (define (ensure-directory dir)
77 (catch 'system-error
78 (lambda ()
79 (mkdir dir))
80 (lambda (k subr fmt args rest)
81 (let ((errno (and (pair? rest) (car rest))))
82 (cond
83 ((eqv? errno EEXIST)
84 ;; Assume it's a writable directory, to avoid TOCTOU errors,
85 ;; as well as UID/EUID mismatches that occur with access(2).
86 #t)
87 ((eqv? errno ENOENT)
88 (ensure-directory (dirname dir))
89 (ensure-directory dir))
90 (else
91 (throw k subr fmt args rest)))))))
92
93 ;;; This function is among the trickiest I've ever written. I tried many
94 ;;; variants. In the end, simple is best, of course.
95 ;;;
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.
103 ;;;
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)
109 (cond
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)))
115 (else canon)))
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)
121 ".go")
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)))
129 f))))
130
131 (define* (compile-file file #:key
132 (output-file #f)
133 (from (current-language))
134 (to 'objcode)
135 (env (default-environment from))
136 (opts '())
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"
141 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"))
146
147 (ensure-directory (dirname comp))
148 (call-with-output-file/atomic comp
149 (lambda (port)
150 ((language-printer (ensure-language to))
151 (read-and-compile in #:env env #:from from #:to to #:opts opts)
152 port))
153 file)
154 comp)))
155
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
162 #:env env)))
163
164 \f
165 ;;;
166 ;;; Compiler interface
167 ;;;
168
169 (define (compile-passes from to opts)
170 (map cdr
171 (or (lookup-compilation-order from to)
172 (error "no way to compile" from "to" to))))
173
174 (define (compile-fold passes exp env opts)
175 (let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t))
176 (if (null? passes)
177 (values x e cenv)
178 (receive (x e new-cenv) ((car passes) x e opts)
179 (lp (cdr passes) x e (if first? new-cenv cenv) #f)))))
180
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))))
184 (lang to))
185 (cond ((null? in) to)
186 ((language-joiner lang) lang)
187 (else
188 (lp (cdr in) (caar in))))))
189
190 (define (default-language-joiner lang)
191 (lambda (exps env)
192 (if (and (pair? exps) (null? (cdr exps)))
193 (car exps)
194 (error
195 "Multiple expressions read and compiled, but language has no joiner"
196 lang))))
197
198 (define (read-and-parse lang port cenv)
199 (let ((exp ((language-reader lang) port cenv)))
200 (cond
201 ((eof-object? exp) exp)
202 ((language-parser lang) => (lambda (parse) (parse exp)))
203 (else exp))))
204
205 (define* (read-and-compile port #:key
206 (from (current-language))
207 (to 'objcode)
208 (env (default-environment from))
209 (opts '()))
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)))
216 (cond
217 ((eof-object? x)
218 (close-port port)
219 (compile ((or (language-joiner joint)
220 (default-language-joiner joint))
221 (reverse exps)
222 env)
223 #:from joint #:to to
224 ;; env can be false if no expressions were read.
225 #:env (or env (default-environment joint))
226 #:opts opts))
227 (else
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)
231 x cenv opts)
232 (lp (cons jexp exps) jenv jcenv))))))))))
233
234 (define* (compile x #:key
235 (from (current-language))
236 (to 'value)
237 (env (default-environment from))
238 (opts '()))
239
240 (let ((warnings (memq #:warnings opts)))
241 (if (pair? warnings)
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)))
247 warnings))))
248
249 (receive (exp env cenv)
250 (compile-fold (compile-passes from to opts) x env opts)
251 exp))
252
253 \f
254 ;;;
255 ;;; Decompiler interface
256 ;;;
257
258 (define (decompile-passes from to opts)
259 (map cdr
260 (or (lookup-decompilation-order from to)
261 (error "no way to decompile" from "to" to))))
262
263 (define (decompile-fold passes exp env opts)
264 (if (null? passes)
265 (values exp env)
266 (receive (exp env) ((car passes) exp env opts)
267 (decompile-fold (cdr passes) exp env opts))))
268
269 (define* (decompile x #:key
270 (env #f)
271 (from 'value)
272 (to 'assembly)
273 (opts '()))
274 (decompile-fold (decompile-passes from to opts)
275 x
276 env
277 opts))