1 ;;; High-level compiler interface
3 ;; Copyright (C) 2001 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 (language objcode spec)
26 #:use-module (language value spec)
27 #:use-module (system vm vm) ;; FIXME: there's a reason for this, can't remember why tho
28 #:use-module (ice-9 regex)
29 #:use-module (ice-9 optargs)
30 #:use-module (ice-9 receive)
31 #:export (syntax-error
33 compiled-file-name compile-file compile-and-load
34 compile compile-time-environment)
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 (format (current-error-port)
50 "~A:~A: ~A: ~A~%" (car loc) (cdr loc) msg exp)
51 (format (current-error-port)
52 "unknown location: ~A: ~A~%" msg exp)))))
59 (define *current-language* (make-fluid))
60 (define (current-language)
61 (or (fluid-ref *current-language*)
62 (begin (fluid-set! *current-language* (lookup-language 'scheme))
65 (define (call-once thunk)
70 (error "thunk may only be entered once: ~a" thunk))
75 (define (call-with-output-file/atomic filename proc)
76 (let* ((template (string-append filename ".XXXXXX"))
77 (tmp (mkstemp! template)))
80 (with-throw-handler #t
82 (with-output-to-port tmp
83 (lambda () (proc (current-output-port))))
84 (rename-file template filename))
86 (delete-file template)))))))
88 (define* (compile-file file #:key (to objcode) (opts '()))
89 (let ((comp (compiled-file-name file))
90 (lang (current-language)))
91 (catch 'nothing-at-all
93 (call-with-compile-error-catch
95 (call-with-output-file/atomic comp
97 (let ((print (language-printer to)))
98 (print (compile (read-file-in file lang)
99 #:from lang #:to to #:opts opts)
101 (format #t "wrote `~A'\n" comp))))
103 (format #t "ERROR: during compilation of ~A:\n" file)
105 (apply format #t (cadr args) (caddr args))
107 (format #t "ERROR: ~A ~A ~A\n" key (car args) (cadddr args))
108 (delete-file comp)))))
110 (define* (compile-and-load file #:key (to value) (opts '()))
111 (let ((lang (current-language)))
112 (compile (read-file-in file lang) #:to value #:opts opts)))
114 (define (compiled-file-name file)
115 (let ((base (basename file))
116 (cext (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 (let lp ((exts %load-extensions))
123 (cond ((null? exts) (string-append base cext))
124 ((string-null? (car exts)) (lp (cdr exts)))
125 ((string-suffix? (car exts) base)
128 (- (string-length base) (string-length (car exts))))
130 (else (lp (cdr exts)))))))
134 ;;; Compiler interface
137 (define (read-file-in file lang)
138 (call-with-input-file file
139 (or (language-read-file lang)
140 (error "language has no #:read-file" lang))))
142 (define (compile-passes from to opts)
143 (let lp ((langs (or (lookup-compilation-order from to)
144 (error "no way to compile" (language-name from)
145 "to" (language-name to))))
147 (if (null? (cdr langs))
150 (cons (assq-ref (language-compilers (car langs)) (cadr langs))
153 (define (compile-fold passes exp env opts)
156 (receive (exp env) ((car passes) exp env opts)
157 (compile-fold (cdr passes) exp env opts))))
159 (define (compile-time-environment)
160 "A special function known to the compiler that, when compiled, will
161 return a representation of the lexical environment in place at compile
162 time. Useful for supporting some forms of dynamic compilation. Returns
163 #f if called from the interpreter."
166 (define* (compile x #:key
168 (from (current-language))
171 (compile-fold (compile-passes from to opts)