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