tests: Avoid nonsensical classes and methods in deprecation.impure.lisp
[sbcl.git] / src / compiler / target-main.lisp
blob8ed64d07d814a66470c703f69229ec33cc57a195
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
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.
14 (in-package "SB!C")
16 ;;;; CL:COMPILE
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)))
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 (with-source-paths
45 (prog* ((tlf (or tlf 0))
46 ;; If we have a source-info from LOAD, we will
47 ;; also have a source-paths already set up -- so drop
48 ;; the ones from WITH-COMPILATION-VALUES.
49 (*source-paths* (or source-paths *source-paths*))
50 (form (get-lambda-to-compile definition))
51 (*source-info* (or source-info
52 (make-lisp-source-info
53 form :parent *source-info*)))
54 (*toplevel-lambdas* ())
55 (*block-compile* nil)
56 (*allow-instrumenting* nil)
57 (*code-coverage-records* nil)
58 (*code-coverage-blocks* nil)
59 (*current-path* nil)
60 (*last-format-string* nil)
61 (*last-format-args* nil)
62 (*last-message-count* 0)
63 (*last-error-context* nil)
64 (*gensym-counter* 0)
65 ;; KLUDGE: This rebinding of policy is necessary so that
66 ;; forms such as LOCALLY at the REPL actually extend the
67 ;; compilation policy correctly. However, there is an
68 ;; invariant that is potentially violated: future
69 ;; refactoring must not allow this to be done in the file
70 ;; compiler. At the moment we're clearly alright, as we
71 ;; call %COMPILE with a core-object, not a fasl-stream,
72 ;; but caveat future maintainers. -- CSR, 2002-10-27
73 (*policy* (lexenv-policy *lexenv*))
74 ;; see above
75 (*handled-conditions* (lexenv-handled-conditions *lexenv*))
76 ;; ditto
77 (*disabled-package-locks* (lexenv-disabled-package-locks *lexenv*))
78 ;; FIXME: ANSI doesn't say anything about CL:COMPILE
79 ;; interacting with these variables, so we shouldn't. As
80 ;; of SBCL 0.6.7, COMPILE-FILE controls its verbosity by
81 ;; binding these variables, so as a quick hack we do so
82 ;; too. But a proper implementation would have verbosity
83 ;; controlled by function arguments and lexical variables.
84 (*compile-verbose* nil)
85 (*compile-print* nil)
86 (oops nil))
87 (handler-bind (((satisfies handle-condition-p) #'handle-condition-handler))
88 (unless source-paths
89 (find-source-paths form tlf))
90 (let ((*compiler-error-bailout*
91 (lambda (e)
92 (setf oops e)
93 ;; Unwind the compiler frames: users want the know where
94 ;; the error came from, not how the compiler got there.
95 (go :error))))
96 (return
97 (with-world-lock ()
98 (%compile form (make-core-object)
99 :name name
100 :path `(original-source-start 0 ,tlf))))))
101 :error
102 ;; Either signal the error right away, or return a function that
103 ;; will signal the corresponding COMPILED-PROGRAM-ERROR. This is so
104 ;; that we retain our earlier behaviour when called with erronous
105 ;; lambdas via %SIMPLE-EVAL. We could legally do just either one
106 ;; always, but right now keeping the old behaviour seems like less
107 ;; painful option: compiler.pure.lisp is full of tests that make all
108 ;; sort of assumptions about when which things are signalled. FIXME,
109 ;; probably.
110 (if errorp
111 (error oops)
112 (let ((message (princ-to-string oops))
113 (source (source-to-string form)))
114 (return
115 (lambda (&rest arguments)
116 (declare (ignore arguments))
117 (error 'compiled-program-error
118 :message message
119 :source source)))))))))))
121 (defun compile-in-lexenv (name definition lexenv
122 &optional source-info tlf errorp)
123 (dx-flet ((really-compile (definition lexenv)
124 (actually-compile
125 name definition lexenv source-info tlf errorp)))
126 (multiple-value-bind (compiled-definition warnings-p failure-p)
127 (typecase definition
128 #!+sb-fasteval
129 (sb!interpreter:interpreted-function
130 (multiple-value-call #'really-compile
131 (sb!interpreter:prepare-for-compile definition)))
132 #!+sb-eval
133 (sb!eval:interpreted-function
134 (multiple-value-call #'really-compile
135 (sb!eval:prepare-for-compile definition)))
136 (function
137 (values definition nil nil))
139 (really-compile definition lexenv)))
140 (aver (typep compiled-definition 'compiled-function))
141 (let ((result (if (not name)
142 compiled-definition
143 (progn
144 (if (and (symbolp name) (macro-function name))
145 (setf (macro-function name) compiled-definition)
146 (setf (fdefinition name) compiled-definition))
147 name))))
148 (values result warnings-p failure-p)))))
150 (defun compile (name &optional (definition (or (and (symbolp name)
151 (macro-function name))
152 (fdefinition name))))
153 "Produce a compiled function from DEFINITION. If DEFINITION is a
154 lambda-expression, it is coerced to a function. If DEFINITION is an
155 interpreted function, it is compiled. If DEFINITION is already a compiled
156 function, it is used as-is. (Future versions of SBCL might try to
157 recompile the existing definition, but this is not currently supported.)
159 If NAME is NIL, the compiled function is returned as the primary value.
160 Otherwise the resulting compiled function replaces existing function
161 definition of NAME, and NAME is returned as primary value; if NAME is a symbol
162 that names a macro, its macro function is replaced and NAME is returned as
163 primary value.
165 Also returns a secondary value which is true if any conditions of type
166 WARNING occur during the compilation, and NIL otherwise.
168 Tertiary value is true if any conditions of type ERROR, or WARNING that are
169 not STYLE-WARNINGs occur during compilation, and NIL otherwise.
171 (compile-in-lexenv name definition (make-null-lexenv)))
173 (defun make-form-tracking-stream-observer (file-info)
174 (lambda (arg1 arg2 arg3)
175 ;; Log some kind of reader event into FILE-INFO.
176 (case arg1
177 (:reset ; a char macro returned zero values - "virtual whitespace".
178 ;; I think this would be an ideal place at which to inquire and stash
179 ;; the FILE-POSITION in bytes so that DEBUG-SOURCE-START-POSITIONS
180 ;; are obtained _after_ having skipped virtual whitespace, not before.
181 (setf (fill-pointer (file-info-subforms file-info)) 0))
183 (let ((subforms (file-info-subforms file-info)))
184 ;; (ARG1 ARG2 ARG3) = (start-pos end-pos form)
185 (vector-push-extend arg1 subforms)
186 (vector-push-extend arg2 subforms)
187 (vector-push-extend arg3 subforms))))))
189 ;;; COMPILE-FILE-POSITION macro
191 ;; Macros and inline functions report the original-source position. e.g.:
192 ;; 01: (declaim (inline foo))
193 ;; 02: (defun foo (x) (if x (warn "fail @line ~d" (compile-file-position))))
194 ;; 03: (defun bar (y) (foo y))
195 ;; 04: (defun baz (y) (foo y))
196 ;; will cause BAR to print 3 and BAZ to print 4 in the warning message.
198 ;; For macros this seems fair enough, but for inline functions it could
199 ;; be considered undesirable on the grounds that enabling/disabling inlining
200 ;; should not change visible behavior. Other than working harder to figure
201 ;; out where we are in inlined code (which may not even be feasible),
202 ;; a viable remedy is that all inlineable functions should have their stored
203 ;; representation not contain any macros, i.e. macros could be pre-expanded,
204 ;; which in this case means stuffing in the literal integers.
205 ;; I feel that that would be a general improvement to semantics, because
206 ;; as things are, an inline function's macros are expanded at least as many
207 ;; times as there are calls to the function - not very defensible
208 ;; as a design choice, but just an accident of the particular implementation.
210 (let ()
211 (defmacro compile-file-position (&whole this-form)
212 "Return character position of this macro invocation or NIL if unavailable."
213 ;; Counting characters is intuitive because the transfer element size is 1
214 ;; measurement unit. The standard allows counting in something other than
215 ;; characters (namely bytes) for character streams, which is basically
216 ;; irrelevant here, as we don't need random access to the file.
217 (compute-compile-file-position this-form nil))
219 (defmacro compile-file-line (&whole this-form)
220 "Return line# and column# of this macro invocation as multiple values."
221 (compute-compile-file-position this-form t))
224 (defun compute-compile-file-position (this-form as-line/col-p)
225 (let (file-info stream charpos)
226 (flet ((find-form-eq (form &optional fallback-path)
227 (with-array-data ((vect (file-info-subforms file-info))
228 (start) (end) :check-fill-pointer t)
229 (declare (ignore start))
230 (do ((i (1- end) (- i 3)))
231 ((< i 0))
232 (declare (index-or-minus-1 i))
233 (when (eq form (svref vect i))
234 (if charpos ; ambiguous
235 (return
236 (setq charpos
237 (and fallback-path
238 (compile-file-position-helper
239 file-info fallback-path))))
240 (setq charpos (svref vect (- i 2)))))))))
241 (let ((source-info *source-info*))
242 (when (and source-info (boundp '*current-path*))
243 (setq file-info (source-info-file-info source-info)
244 stream (source-info-stream source-info))
245 (cond
246 ((not *current-path*)
247 ;; probably a read-time eval
248 (find-form-eq this-form))
249 ;; Hmm, would a &WHOLE argument would work better or worse in general?
251 (let* ((original-source-path (source-path-original-source *current-path*))
252 (path (reverse original-source-path)))
253 (when (file-info-subforms file-info)
254 (let ((form (elt (file-info-forms file-info) (car path))))
255 (dolist (p (cdr path))
256 (setq form (nth p form)))
257 (find-form-eq form (cdr path))))
258 (unless charpos
259 (let ((parent (source-info-parent *source-info*)))
260 ;; probably in a local macro executing COMPILE-FILE-POSITION,
261 ;; not producing a sexpr containing an invocation of C-F-P.
262 (when parent
263 (setq file-info (source-info-file-info parent)
264 stream (source-info-stream parent))
265 (find-form-eq this-form))))))))))
266 (if as-line/col-p
267 (if (and charpos (form-tracking-stream-p stream))
268 (let ((line/col (line/col-from-charpos stream charpos)))
269 `(values ,(car line/col) ,(cdr line/col)))
270 '(values 0 -1))
271 charpos)))
273 ;; Find FORM's character position in FILE-INFO by looking for PATH-TO-FIND.
274 ;; This is done by imparting tree structure to the annotations
275 ;; more-or-less paralleling construction of the original sexpr.
276 ;; Unfortunately, though this was a nice idea, it is not terribly useful.
277 ;; FIND-SOURCE-PATHS can not supply the correct path because it assumes
278 ;; that a form determines a path, whereas the opposite is more accurate.
279 ;; e.g. below are two functions that cause misbehavior.
281 * (defun example1 (x)
282 (if x
283 #1=(format t "Err#2 @~D~%" (compile-file-position))
284 (progn #1#)))
285 * (defconstant +foo+ '(format t "Err#1 @~D~%" (compile-file-position))))
286 * (defun example2 (x) (if x #.+foo+ (progn #.+foo+)))
288 ;; In each case the compiler assigns the same source path to two logically
289 ;; different paths that it takes as it IR1-translates each half of the IF
290 ;; expression, though the ELSE branch obviously has a longer path.
291 ;; However, if you _could_ supply correct paths, this would compute correct
292 ;; answers. (Modulo any bugs due to near-total lack of testing)
294 (defun compile-file-position-helper (file-info path-to-find)
295 (let (found-form start-char)
296 (labels
297 ((recurse (subpath upper-bound queue)
298 (let ((index -1))
299 (declare (type index-or-minus-1 index))
300 (loop
301 (let* ((item (car queue))
302 (end (cdar item)))
303 (when (> end upper-bound)
304 (return))
305 (pop queue)
306 (incf index)
307 (when (and (eql index (car subpath)) (not (cdr subpath)))
308 ;; This does not eagerly declare victory, because we want
309 ;; to find the rightmost match. In "#1=(FOO)" there are two
310 ;; different annotations pointing to (FOO).
311 (setq found-form (cdr item)
312 start-char (caar item)))
313 (unless queue (return))
314 (let* ((next (car queue))
315 (next-end (cdar next)))
316 (cond ((< next-end end) ; could descend
317 ;; only scan children if we're on the correct path
318 (if (eql index (car subpath))
319 (setf queue (recurse (cdr subpath) end queue))
320 ;; else skip quickly by finding the next sibling
321 (loop
322 (pop queue)
323 (when (or (endp queue) (>= (caaar queue) end))
324 (return))))
325 (unless queue (return)))
326 ((= next-end end) ; probably because of "#n="
327 (decf (truly-the (integer 0) index))))))))
328 queue))
329 (let ((list
330 (with-array-data ((v (file-info-subforms file-info))
331 (start) (end) :check-fill-pointer t)
332 (declare (ignore start))
333 (sort (loop for i from 0 below end by 3
334 collect (acons (aref v i)
335 (aref v (+ i 1))
336 (aref v (+ i 2))))
337 #'< :key 'caar))))
338 (recurse path-to-find (cdaar list) (cdr list))))
339 start-char))