46ec5eb6e26d799f670a529d78919b3e12278d05
[sbcl.git] / src / compiler / target-main.lisp
1 ;;;; functions from classic CMU CL src/compiler/main.lisp which are
2 ;;;; needed only (and which may make sense only) on the
3 ;;;; cross-compilation target, not the cross-compilation host
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!C")
15 \f
16 ;;;; CL:COMPILE
17
18 (defun get-lambda-to-compile (definition-designator)
19   (if (consp definition-designator)
20       definition-designator
21       (multiple-value-bind (definition env-p)
22                            (function-lambda-expression definition-designator)
23         (when env-p
24           (error "~S was defined in a non-null environment."
25                  definition-designator))
26         (unless definition
27           (error "can't find a definition for ~S" definition-designator))
28         definition)))
29
30 ;;; Handle the nontrivial case of CL:COMPILE.
31 ;;;
32 ;;; If ERRORP is true signals an error immediately -- otherwise returns
33 ;;; a function that will signal the error.
34 (defun actually-compile (name definition *lexenv* source-info tlf errorp)
35   (let ((source-paths (when source-info *source-paths*)))
36     (with-compilation-values
37      (sb!xc:with-compilation-unit ()
38        ;; FIXME: These bindings were copied from SUB-COMPILE-FILE with
39        ;; few changes. Once things are stable, the shared bindings
40        ;; probably be merged back together into some shared utility
41        ;; macro, or perhaps both merged into one of the existing utility
42        ;; macros SB-C::WITH-COMPILATION-VALUES or
43        ;; CL:WITH-COMPILATION-UNIT.
44        (prog* ((tlf (or tlf 0))
45                ;; If we have a source-info from LOAD, we will
46                ;; also have a source-paths already set up -- so drop
47                ;; the ones from WITH-COMPILATION-VALUES.
48                (*source-paths* (or source-paths *source-paths*))
49                ;; FIXME: Do we need the *INFO-ENVIRONMENT* rebinding
50                ;; here? It's a literal translation of the old CMU CL
51                ;; rebinding to (OR *BACKEND-INFO-ENVIRONMENT*
52                ;; *INFO-ENVIRONMENT*), and it's not obvious whether the
53                ;; rebinding to itself is needed now that SBCL doesn't
54                ;; need *BACKEND-INFO-ENVIRONMENT*.
55                (*info-environment* *info-environment*)
56                (form (get-lambda-to-compile definition))
57                (*source-info* (or source-info
58                                (make-lisp-source-info
59                                 form :parent *source-info*)))
60                (*toplevel-lambdas* ())
61                (*block-compile* nil)
62                (*allow-instrumenting* nil)
63                (*code-coverage-records* nil)
64                (*code-coverage-blocks* nil)
65                (*current-path* nil)
66                (*last-source-context* nil)
67                (*last-original-source* nil)
68                (*last-source-form* nil)
69                (*last-format-string* nil)
70                (*last-format-args* nil)
71                (*last-message-count* 0)
72                (*last-error-context* nil)
73                (*gensym-counter* 0)
74                ;; KLUDGE: This rebinding of policy is necessary so that
75                ;; forms such as LOCALLY at the REPL actually extend the
76                ;; compilation policy correctly.  However, there is an
77                ;; invariant that is potentially violated: future
78                ;; refactoring must not allow this to be done in the file
79                ;; compiler.  At the moment we're clearly alright, as we
80                ;; call %COMPILE with a core-object, not a fasl-stream,
81                ;; but caveat future maintainers. -- CSR, 2002-10-27
82                (*policy* (lexenv-policy *lexenv*))
83                ;; see above
84                (*handled-conditions* (lexenv-handled-conditions *lexenv*))
85                ;; ditto
86                (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*))
87                ;; FIXME: ANSI doesn't say anything about CL:COMPILE
88                ;; interacting with these variables, so we shouldn't. As
89                ;; of SBCL 0.6.7, COMPILE-FILE controls its verbosity by
90                ;; binding these variables, so as a quick hack we do so
91                ;; too. But a proper implementation would have verbosity
92                ;; controlled by function arguments and lexical variables.
93                (*compile-verbose* nil)
94                (*compile-print* nil)
95                (oops nil))
96           (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
97             (clear-stuff)
98             (unless source-paths
99               (find-source-paths form tlf))
100             (let ((*compiler-error-bailout*
101                     (lambda (e)
102                       (setf oops e)
103                       ;; Unwind the compiler frames: users want the know where
104                       ;; the error came from, not how the compiler got there.
105                       (go :error))))
106               (return (%compile form (make-core-object)
107                                 :name name
108                                 :path `(original-source-start 0 ,tlf)))))
109         :error
110           ;; Either signal the error right away, or return a function that
111           ;; will signal the corresponding COMPILED-PROGRAM-ERROR. This is so
112           ;; that we retain our earlier behaviour when called with erronous
113           ;; lambdas via %SIMPLE-EVAL. We could legally do just either one
114           ;; always, but right now keeping the old behaviour seems like less
115           ;; painful option: compiler.pure.lisp is full of tests that make all
116           ;; sort of assumptions about when which things are signalled. FIXME,
117           ;; probably.
118           (if errorp
119               (error oops)
120               (let ((message (princ-to-string oops))
121                     (source (source-to-string form)))
122                 (return
123                   (lambda (&rest arguments)
124                     (declare (ignore arguments))
125                     (error 'compiled-program-error
126                            :message message
127                            :source source))))))))))
128
129 (defun compile-in-lexenv (name definition lexenv
130                           &optional source-info tlf errorp)
131   (multiple-value-bind (compiled-definition warnings-p failure-p)
132       (cond
133         #!+sb-eval
134         ((sb!eval:interpreted-function-p definition)
135          (multiple-value-bind (definition lexenv)
136              (sb!eval:prepare-for-compile definition)
137            (actually-compile name definition lexenv source-info tlf errorp)))
138         ((compiled-function-p definition)
139          (values definition nil nil))
140         (t
141          (actually-compile name definition lexenv source-info tlf errorp)))
142     (check-type compiled-definition compiled-function)
143     (cond (name
144            (if (and (symbolp name)
145                     (macro-function name))
146                (setf (macro-function name) compiled-definition)
147                (setf (fdefinition name) compiled-definition))
148            (values name warnings-p failure-p))
149           (t
150            (values compiled-definition warnings-p failure-p)))))
151
152 (defun compile (name &optional (definition (or (macro-function name)
153                                                (fdefinition name))))
154   #!+sb-doc
155   "Produce a compiled function from DEFINITION. If DEFINITION is a
156 lambda-expression, it is coerced to a function. If DEFINITION is an
157 interpreted function, it is compiled. If DEFINITION is already a compiled
158 function, it is used as-is. (Future versions of SBCL might try to
159 recompile the existing definition, but this is not currently supported.)
160
161 If NAME is NIL, the compiled function is returned as the primary value.
162 Otherwise the resulting compiled function replaces existing function
163 definition of NAME, and NAME is returned as primary value; if NAME is a symbol
164 tha names a macro, its macro function is replaced and NAME is returned as
165 primary value.
166
167 Also returns secondary value which is true if any conditions of type WARNING
168 occur during the compilation, and NIL otherwise.
169
170 Tertiary value is true if any conditions of type ERROR, or WARNING that are
171 not STYLE-WARNINGs occur during compilation, and NIL otherwise.
172 "
173   (compile-in-lexenv name definition (make-null-lexenv)))