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