4307d78ac2a065d6e6acfaff7118a309f1197897
[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           (with-world-lock ()
97             (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
98               (clear-stuff)
99               (unless source-paths
100                 (find-source-paths form tlf))
101               (let ((*compiler-error-bailout*
102                       (lambda (e)
103                         (setf oops e)
104                         ;; Unwind the compiler frames: users want the know where
105                         ;; the error came from, not how the compiler got there.
106                         (go :error))))
107                 (return (%compile form (make-core-object)
108                                   :name name
109                                   :path `(original-source-start 0 ,tlf))))))
110         :error
111           ;; Either signal the error right away, or return a function that
112           ;; will signal the corresponding COMPILED-PROGRAM-ERROR. This is so
113           ;; that we retain our earlier behaviour when called with erronous
114           ;; lambdas via %SIMPLE-EVAL. We could legally do just either one
115           ;; always, but right now keeping the old behaviour seems like less
116           ;; painful option: compiler.pure.lisp is full of tests that make all
117           ;; sort of assumptions about when which things are signalled. FIXME,
118           ;; probably.
119           (if errorp
120               (error oops)
121               (let ((message (princ-to-string oops))
122                     (source (source-to-string form)))
123                 (return
124                   (lambda (&rest arguments)
125                     (declare (ignore arguments))
126                     (error 'compiled-program-error
127                            :message message
128                            :source source))))))))))
129
130 (defun compile-in-lexenv (name definition lexenv
131                           &optional source-info tlf errorp)
132   (multiple-value-bind (compiled-definition warnings-p failure-p)
133       (cond
134         #!+sb-eval
135         ((sb!eval:interpreted-function-p definition)
136          (multiple-value-bind (definition lexenv)
137              (sb!eval:prepare-for-compile definition)
138            (actually-compile name definition lexenv source-info tlf errorp)))
139         ((compiled-function-p definition)
140          (values definition nil nil))
141         (t
142          (actually-compile name definition lexenv source-info tlf errorp)))
143     (check-type compiled-definition compiled-function)
144     (cond (name
145            (if (and (symbolp name)
146                     (macro-function name))
147                (setf (macro-function name) compiled-definition)
148                (setf (fdefinition name) compiled-definition))
149            (values name warnings-p failure-p))
150           (t
151            (values compiled-definition warnings-p failure-p)))))
152
153 (defun compile (name &optional (definition (or (macro-function name)
154                                                (fdefinition name))))
155   #!+sb-doc
156   "Produce a compiled function from DEFINITION. If DEFINITION is a
157 lambda-expression, it is coerced to a function. If DEFINITION is an
158 interpreted function, it is compiled. If DEFINITION is already a compiled
159 function, it is used as-is. (Future versions of SBCL might try to
160 recompile the existing definition, but this is not currently supported.)
161
162 If NAME is NIL, the compiled function is returned as the primary value.
163 Otherwise the resulting compiled function replaces existing function
164 definition of NAME, and NAME is returned as primary value; if NAME is a symbol
165 tha names a macro, its macro function is replaced and NAME is returned as
166 primary value.
167
168 Also returns secondary value which is true if any conditions of type WARNING
169 occur during the compilation, and NIL otherwise.
170
171 Tertiary value is true if any conditions of type ERROR, or WARNING that are
172 not STYLE-WARNINGs occur during compilation, and NIL otherwise.
173 "
174   (compile-in-lexenv name definition (make-null-lexenv)))