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