Fixed a Scheme translation bug; cleaned compilation with GCC 4.
[bpt/guile.git] / module / system / base / compile.scm
1 ;;; High-level compiler interface
2
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
4
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)
8 ;; any later version.
9 ;;
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.
14 ;;
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.
19
20 ;;; Code:
21
22 (define-module (system base compile)
23 :use-syntax (system base syntax)
24 :use-module (system base language)
25 :use-module (system il compile)
26 :use-module (system il glil)
27 :use-module ((system vm core)
28 #:select (the-vm vm-load objcode->u8vector))
29 :use-module (system vm assemble)
30 :use-module (ice-9 regex))
31
32 ;;;
33 ;;; Compiler environment
34 ;;;
35
36 (define-record (<cenv> vm language module))
37
38 (define-public (make-cenv . rest)
39 (apply <cenv> rest))
40
41 (define-public (syntax-error loc msg exp)
42 (throw 'syntax-error loc msg exp))
43
44 (define-public (call-with-compile-error-catch thunk)
45 (catch 'syntax-error
46 (thunk)
47 (lambda (key loc msg exp)
48 (format #t "~A:~A: ~A: ~A" (car loc) (cdr loc) msg exp))))
49
50 \f
51 ;;;
52 ;;; Compiler
53 ;;;
54
55 (define scheme (lookup-language 'scheme))
56
57 (define-public (compile-file file . opts)
58 (let ((comp (compiled-file-name file)))
59 (catch #t
60 (lambda ()
61 ; (call-with-compile-error-catch
62 ; (lambda ()
63 (call-with-output-file comp
64 (lambda (port)
65 (let* ((source (read-file-in file scheme))
66 (objcode (apply compile-in source (current-module)
67 scheme opts)))
68 (if (memq :c opts)
69 (pprint-glil objcode port)
70 (uniform-vector-write (objcode->u8vector objcode) port)))))
71 (format #t "wrote ~A\n" comp))
72 (lambda (key . args)
73 (format #t "ERROR: during compilation of ~A:\n" file)
74 (display "ERROR: ")
75 (apply format #t (cadr args) (caddr args))
76 (newline)
77 (format #t "ERROR: ~A ~A ~A\n" key (car args) (cadddr args))
78 (delete-file comp)))))
79
80 ; (let ((c-f compile-file))
81 ; ;; XXX: Debugging output
82 ; (set! compile-file
83 ; (lambda (file . opts)
84 ; (format #t "compile-file: ~a ~a~%" file opts)
85 ; (let ((result (apply c-f (cons file opts))))
86 ; (format #t "compile-file: returned ~a~%" result)
87 ; result))))
88
89 (define-public (load-source-file file . opts)
90 (let ((source (read-file-in file scheme)))
91 (apply compile-in source (current-module) scheme opts)))
92
93 (define-public (load-file file . opts)
94 (let ((comp (compiled-file-name file)))
95 (if (file-exists? comp)
96 (load-objcode comp)
97 (apply load-source-file file opts))))
98
99 (define-public (compiled-file-name file)
100 (let ((m (string-match "\\.[^.]*$" file)))
101 (string-append (if m (match:prefix m) file) ".go")))
102
103 (define-public (scheme-eval x e)
104 (vm-load (the-vm) (compile-in x e scheme)))
105
106 \f
107 ;;;
108 ;;; Scheme compiler interface
109 ;;;
110
111 (define-public (read-file-in file lang)
112 (call-with-input-file file lang.read-file))
113
114 (define-public (compile-in x e lang . opts)
115 (catch 'result
116 (lambda ()
117 ;; expand
118 (set! x (lang.expander x e))
119 (if (memq :e opts) (throw 'result x))
120 ;; translate
121 (set! x (lang.translator x e))
122 (if (memq :t opts) (throw 'result x))
123 ;; compile
124 (set! x (apply compile x e opts))
125 (if (memq :c opts) (throw 'result x))
126 ;; assemble
127 (apply assemble x e opts))
128 (lambda (key val) val)))
129
130 ;;;
131 ;;;
132 ;;;
133
134 (define (compile-and-load file . opts)
135 (let ((comp (object-file-name file)))
136 (if (or (not (file-exists? comp))
137 (> (stat:mtime (stat file)) (stat:mtime (stat comp))))
138 (compile-file file))
139 (load-compiled-file comp)))
140
141 (define (load/compile file . opts)
142 (let* ((file (file-full-name file))
143 (compiled (object-file-name file)))
144 (if (or (not (file-exists? compiled))
145 (> (stat:mtime (stat file)) (stat:mtime (stat compiled))))
146 (apply compile-file file #f opts))
147 (if (memq #:b opts)
148 (apply vm-trace (the-vm) (load-objcode compiled) opts)
149 ((the-vm) (load-objcode compiled)))))
150
151 (define (file-full-name filename)
152 (let* ((port (current-load-port))
153 (oldname (and port (port-filename port))))
154 (if (and oldname
155 (> (string-length filename) 0)
156 (not (char=? (string-ref filename 0) #\/))
157 (not (string=? (dirname oldname) ".")))
158 (string-append (dirname oldname) "/" filename)
159 filename)))