1 ;;; High-level compiler interface
3 ;; Copyright (C) 2001, 2009 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program 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
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
22 (define-module (system base compile)
23 #:use-module (system base syntax)
24 #:use-module (system base language)
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 (syntax-error
31 compiled-file-name compile-file compile-and-load
32 load-ensuring-compiled
35 #:export-syntax (call-with-compile-error-catch))
38 ;;; Compiler environment
41 (define (syntax-error loc msg exp)
42 (throw 'syntax-error-compile-time loc msg exp))
44 (define-macro (call-with-compile-error-catch thunk)
45 `(catch 'syntax-error-compile-time
47 (lambda (key loc msg exp)
49 (let ((file (or (assq-ref loc 'filename) "unknown file"))
50 (line (assq-ref loc 'line))
51 (col (assq-ref loc 'column)))
52 (format (current-error-port)
53 "~A:~A:~A: ~A: ~A~%" file line col msg exp))
54 (format (current-error-port)
55 "unknown location: ~A: ~S~%" msg exp)))))
62 (define *current-language* (make-fluid))
63 (fluid-set! *current-language* 'scheme)
64 (define (current-language)
65 (fluid-ref *current-language*))
67 (define (call-once thunk)
72 (error "thunk may only be entered once: ~a" thunk))
77 (define (call-with-output-file/atomic filename proc)
78 (let* ((template (string-append filename ".XXXXXX"))
79 (tmp (mkstemp! template)))
82 (with-throw-handler #t
85 (chmod tmp (logand #o0666 (lognot (umask))))
87 (rename-file template filename))
89 (delete-file template)))))))
91 (define (ensure-language x)
96 (define (ensure-directory dir)
97 (or (file-exists? dir)
99 (ensure-directory (dirname dir))
102 (define* (compile-file file #:key
105 (from (current-language))
108 (let ((comp (or output-file (compiled-file-name file)))
109 (in (open-input-file file)))
110 (ensure-directory (dirname comp))
111 (call-with-output-file/atomic comp
113 ((language-printer (ensure-language to))
114 (read-and-compile in #:env env #:from from #:to to #:opts opts)
118 (define* (compile-and-load file #:key (from 'scheme) (to 'value) (opts '()))
119 (read-and-compile (open-input-file file)
120 #:from from #:to to #:opts opts))
122 (define* (load-ensuring-compiled source #:key (from 'scheme)
123 (to 'value) (opts '()))
124 (let ((compiled (compiled-file-name source #:readable #t)))
127 (>= (stat:mtime (stat compiled)) (stat:mtime (stat source))))
129 (let ((to-compile (compiled-file-name source #:writable #t)))
131 (warn "source file" source "newer than" compiled))
133 (not (string-equal? compiled to-compile))
134 (file-exists? to-compile)
135 (>= (stat:mtime (stat to-compile))
136 (stat:mtime (stat compiled))))
137 (warn "using local compiled copy" to-compile)
139 (format (current-error-port) ";;; Compiling ~s\n" source)
140 (compile-file source #:output-file to-compile)
141 (format (current-error-port) ";;; Success: ~s\n" to-compile)))
144 (define (ensure-fallback-path)
145 (let ((home (or (getenv "HOME")
147 (passwd:dir (getpwuid (getuid)))))))
149 (let ((cache (in-vicinity home ".guile-ccache")))
151 ((and (access? cache (logior W_OK X_OK))
152 (file-is-directory? cache))
154 ((not (file-exists? cache))
155 (and (false-if-exception (mkdir cache))
159 (define load-compiled-path
160 (let ((fallback-path #f))
162 (if (not fallback-path)
163 (let ((cache-path (ensure-fallback-path)))
168 (append %load-path fallback-path))))
170 (define* (compiled-file-name file #:key (writable #f) (readable #f))
171 (let ((base (basename file))
172 (cext (cond ((or (null? %load-compiled-extensions)
173 (string-null? (car %load-compiled-extensions)))
174 (warn "invalid %load-compiled-extensions"
175 %load-compiled-extensions)
177 (else (car %load-compiled-extensions)))))
178 (define (strip-source-extension base)
179 (let lp ((exts %load-extensions))
180 (cond ((null? exts) (string-append file cext))
181 ((string-null? (car exts)) (lp (cdr exts)))
182 ((string-suffix? (car exts) base)
184 (- (string-length source)
185 (string-length (car exts)))))
186 (else (lp (cdr exts))))))
187 (define (strip-path file paths)
188 (let lp ((paths paths))
189 (cond ((null? paths) file)
190 ((string-prefix? (car paths) file)
191 (substring file (1+ (string-length (car paths)))))
192 (else (lp (cdr paths))))))
193 (let ((sibling (string-append (strip-source-extension file) cext)))
196 ;; either put it right beside the original file, or in our
197 ;; ccache. other things wind up not making sense.
199 ((or (not (file-exists? sibling)) (access? sibling W_OK))
201 ((ensure-fallback-path)
203 (string-append p "/" (strip-path sibling))))
206 (if (access? sibling R_OK)
208 (search-path (load-compiled-path)
209 (strip-path (strip-source-extension file))
210 %load-compiled-extensions #t)))
217 ;;; Compiler interface
220 (define (compile-passes from to opts)
222 (or (lookup-compilation-order from to)
223 (error "no way to compile" from "to" to))))
225 (define (compile-fold passes exp env opts)
226 (let lp ((passes passes) (x exp) (e env) (cenv env) (first? #t))
229 (receive (x e new-cenv) ((car passes) x e opts)
230 (lp (cdr passes) x e (if first? new-cenv cenv) #f)))))
232 (define (find-language-joint from to)
233 (let lp ((in (reverse (or (lookup-compilation-order from to)
234 (error "no way to compile" from "to" to))))
237 (error "don't know how to join expressions" from to))
238 ((language-joiner lang) lang)
240 (lp (cdr in) (caar in))))))
242 (define* (read-and-compile port #:key
244 (from (current-language))
247 (let ((from (ensure-language from))
248 (to (ensure-language to)))
249 (let ((joint (find-language-joint from to)))
250 (with-fluids ((*current-language* from))
251 (let lp ((exps '()) (env #f) (cenv env))
252 (let ((x ((language-reader (current-language)) port)))
255 (compile ((language-joiner joint) (reverse exps) env)
256 #:from joint #:to to #:env env #:opts opts))
258 ;; compile-fold instead of compile so we get the env too
259 (receive (jexp jenv jcenv)
260 (compile-fold (compile-passes (current-language) joint opts)
262 (lp (cons jexp exps) jenv jcenv))))))))))
264 (define* (compile x #:key
266 (from (current-language))
269 (receive (exp env cenv)
270 (compile-fold (compile-passes from to opts) x env opts)
275 ;;; Decompiler interface
278 (define (decompile-passes from to opts)
280 (or (lookup-decompilation-order from to)
281 (error "no way to decompile" from "to" to))))
283 (define (decompile-fold passes exp env opts)
286 (receive (exp env) ((car passes) exp env opts)
287 (decompile-fold (cdr passes) exp env opts))))
289 (define* (decompile x #:key
294 (decompile-fold (decompile-passes from to opts)