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