1.0.21.14: fix CHECK-FASL-HEADER buglet
[sbcl.git] / src / compiler / fopcompile.lisp
1 ;;;; A compiler from simple top-level forms to FASL operations.
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!C")
13
14 ;;; SBCL has no proper byte compiler (having ditched the rather
15 ;;; ambitious and slightly flaky byte compiler inherited from CMU CL)
16 ;;; but its FOPs are a sort of byte code which is expressive enough
17 ;;; that we can compile some simple toplevel forms directly to them,
18 ;;; including very common operations like the forms that DEFVARs and
19 ;;; DECLAIMs macroexpand into.
20 (defun fopcompilable-p (form)
21   ;; We'd like to be able to handle
22   ;;   -- simple funcalls, nested recursively, e.g.
23   ;;      (SET '*PACKAGE* (FIND-PACKAGE "CL-USER"))
24   ;;   -- common self-evaluating forms like strings and keywords and
25   ;;      fixnums, which are important for terminating
26   ;;      the recursion of the simple funcalls above
27   ;;   -- quoted lists (which are important for PROCLAIMs, which are
28   ;;      common toplevel forms)
29   ;;   -- fopcompilable stuff wrapped around non-fopcompilable expressions,
30   ;;      e.g.
31   ;;        (%DEFUN 'FOO (LAMBDA () ...) ...)
32   ;;   -- the IF special form, to support things like (DEFVAR *X* 0)
33   ;;      expanding into (UNLESS (BOUNDP '*X*) (SET '*X* 0))
34   ;;
35   ;; Special forms which we don't currently handle, but might consider
36   ;; supporting in the future are LOCALLY (with declarations),
37   ;; MACROLET, SYMBOL-MACROLET and THE.
38   #+sb-xc-host
39   nil
40   #-sb-xc-host
41   (or (and (self-evaluating-p form)
42            (constant-fopcompilable-p form))
43       (and (symbolp form)
44            (multiple-value-bind (macroexpansion macroexpanded-p)
45                (macroexpand form *lexenv*)
46              (if macroexpanded-p
47                  (fopcompilable-p macroexpansion)
48                  ;; Punt on :ALIEN variables
49                  (let ((kind (info :variable :kind form)))
50                    (or (eq kind :special)
51                        ;; Not really a global, but a variable for
52                        ;; which no information exists.
53                        (eq kind :global)
54                        (eq kind :constant))))))
55       (and (listp form)
56            (ignore-errors (list-length form))
57            (multiple-value-bind (macroexpansion macroexpanded-p)
58                (macroexpand form *lexenv*)
59              (if macroexpanded-p
60                  (fopcompilable-p macroexpansion)
61                  (destructuring-bind (operator &rest args) form
62                    (case operator
63                      ;; Special operators that we know how to cope with
64                      ((progn)
65                       (every #'fopcompilable-p args))
66                      ((quote)
67                       (and (= (length args) 1)
68                            (constant-fopcompilable-p (car args))))
69                      ((function)
70                       (and (= (length args) 1)
71                            ;; #'(LAMBDA ...), #'(NAMED-LAMBDA ...), etc. These
72                            ;; are not fopcompileable as such, but we can compile
73                            ;; the lambdas with the real compiler, and the rest
74                            ;; of the expression with the fop-compiler.
75                            (or (and (lambda-form-p (car args))
76                                     ;; The lambda might be closing over some
77                                     ;; variable, punt. As a further improvement,
78                                     ;; we could analyze the lambda body to
79                                     ;; see whether it really closes over any
80                                     ;; variables. One place where even simple
81                                     ;; analysis would be useful are the PCL
82                                     ;; slot-definition type-check-functions
83                                     ;;   -- JES, 2007-01-13
84                                     (notany (lambda (binding)
85                                               (lambda-var-p (cdr binding)))
86                                             (lexenv-vars *lexenv*)))
87                                ;; #'FOO, #'(SETF FOO), etc
88                                (legal-fun-name-p (car args)))))
89                      ((if)
90                       (and (<= 2 (length args) 3)
91                            (every #'fopcompilable-p args)))
92                      ;; Allow SETQ only on special variables
93                      ((setq)
94                       (loop for (name value) on args by #'cddr
95                             unless (and (symbolp name)
96                                         (let ((kind (info :variable :kind name)))
97                                           (eq kind :special))
98                                         (fopcompilable-p value))
99                             return nil
100                             finally (return t)))
101                      ;; The real toplevel form processing has already been
102                      ;; done, so EVAL-WHEN handling will be easy.
103                      ((eval-when)
104                       (and (>= (length args) 1)
105                            (eq (set-difference (car args)
106                                                '(:compile-toplevel
107                                                  compile
108                                                  :load-toplevel
109                                                  load
110                                                  :execute
111                                                  eval))
112                                nil)
113                            (every #'fopcompilable-p (cdr args))))
114                      ;; A LET or LET* that introduces only lexical
115                      ;; bindings might be fopcompilable, depending on
116                      ;; whether something closes over the bindings.
117                      ;; (And whether there are declarations in the body,
118                      ;; see below)
119                      ((let let*)
120                       (let-fopcompilable-p operator args))
121                      ((locally)
122                       (every #'fopcompilable-p args))
123                      (otherwise
124                       ;; ordinary function calls
125                       (and (symbolp operator)
126                            ;; If a LET/LOCALLY tries to introduce
127                            ;; declarations, we'll detect it here, and
128                            ;; disallow fopcompilation.  This is safe,
129                            ;; since defining a function/macro named
130                            ;; DECLARE would violate a package lock.
131                            (not (eq operator 'declare))
132                            (not (special-operator-p operator))
133                            (not (macro-function operator))
134                            ;; We can't FOP-FUNCALL with more than 255
135                            ;; parameters. (We could theoretically use
136                            ;; APPLY, but then we'd need to construct
137                            ;; the parameter list for APPLY without
138                            ;; calling LIST, which is probably more
139                            ;; trouble than it's worth).
140                            (<= (length args) 255)
141                            (every #'fopcompilable-p args))))))))))
142
143 (defun let-fopcompilable-p (operator args)
144   (when (>= (length args) 1)
145     (multiple-value-bind (body decls)
146         (parse-body (cdr args) :doc-string-allowed nil)
147       (declare (ignore body))
148       (let* ((orig-lexenv *lexenv*)
149              (*lexenv* (make-lexenv)))
150         ;; We need to check for declarations
151         ;; first. Otherwise the fake lexenv we're
152         ;; constructing might be invalid.
153         (and (null decls)
154              (loop for binding in (car args)
155                    for name = (if (consp binding)
156                                   (first binding)
157                                   binding)
158                    for value = (if (consp binding)
159                                    (second binding)
160                                    nil)
161                    ;; Only allow binding lexicals,
162                    ;; since special bindings can't be
163                    ;; easily expressed with fops.
164                    always (and (eq (info :variable :kind name)
165                                    :global)
166                                (let ((*lexenv* (ecase operator
167                                                  (let orig-lexenv)
168                                                  (let* *lexenv*))))
169                                  (fopcompilable-p value)))
170                    do (progn
171                         (setf *lexenv* (make-lexenv))
172                         (push (cons name
173                                     (make-lambda-var :%source-name name))
174                               (lexenv-vars *lexenv*))))
175              (every #'fopcompilable-p (cdr args)))))))
176
177 (defun lambda-form-p (form)
178   (and (consp form)
179        (member (car form)
180                '(lambda named-lambda instance-lambda lambda-with-lexenv))))
181
182 ;;; Check that a literal form is fopcompilable. It would not for example
183 ;;; when the form contains structures with funny MAKE-LOAD-FORMS.
184 (defun constant-fopcompilable-p (constant)
185   (let ((xset (alloc-xset)))
186     (labels ((grovel (value)
187                ;; Unless VALUE is an object which which obviously
188                ;; can't contain other objects
189                (unless (typep value
190                               '(or unboxed-array
191                                 symbol
192                                 number
193                                 character
194                                 string))
195                  (if (xset-member-p value xset)
196                      (return-from grovel nil)
197                      (add-to-xset value xset))
198                  (typecase value
199                    (cons
200                     (grovel (car value))
201                     (grovel (cdr value)))
202                    (simple-vector
203                     (dotimes (i (length value))
204                       (grovel (svref value i))))
205                    ((vector t)
206                     (dotimes (i (length value))
207                       (grovel (aref value i))))
208                    ((simple-array t)
209                     ;; Even though the (ARRAY T) branch does the exact
210                     ;; same thing as this branch we do this separately
211                     ;; so that the compiler can use faster versions of
212                     ;; array-total-size and row-major-aref.
213                     (dotimes (i (array-total-size value))
214                       (grovel (row-major-aref value i))))
215                    ((array t)
216                     (dotimes (i (array-total-size value))
217                       (grovel (row-major-aref value i))))
218                    (instance
219                     (multiple-value-bind (creation-form init-form)
220                         (handler-case
221                             (sb!xc:make-load-form value (make-null-lexenv))
222                           (error (condition)
223                             (compiler-error condition)))
224                       (declare (ignore init-form))
225                       (case creation-form
226                         (:sb-just-dump-it-normally
227                          ;; FIXME: Why is this needed? If the constant
228                          ;; is deemed fopcompilable, then when we dump
229                          ;; it we bind *dump-only-valid-structures* to
230                          ;; NIL.
231                          (fasl-validate-structure value *compile-object*)
232                          (dotimes (i (- (%instance-length value)
233                                         (layout-n-untagged-slots
234                                          (%instance-ref value 0))))
235                            (grovel (%instance-ref value i))))
236                         (:ignore-it)
237                         (t
238                          (return-from constant-fopcompilable-p nil)))))
239                    (t
240                     (return-from constant-fopcompilable-p nil))))))
241       (grovel constant))
242     t))
243
244 ;;; FOR-VALUE-P is true if the value will be used (i.e., pushed onto
245 ;;; FOP stack), or NIL if any value will be discarded. FOPCOMPILABLE-P
246 ;;; has already ensured that the form can be fopcompiled.
247 (defun fopcompile (form path for-value-p)
248   (cond ((self-evaluating-p form)
249          (fopcompile-constant form for-value-p))
250         ((symbolp form)
251          (multiple-value-bind (macroexpansion macroexpanded-p)
252              (sb!xc:macroexpand form *lexenv*)
253            (if macroexpanded-p
254                ;; Symbol macro
255                (fopcompile macroexpansion path for-value-p)
256                (let ((kind (info :variable :kind form)))
257                  (if (member kind '(:special :constant))
258                      ;; Special variable
259                      (fopcompile `(symbol-value ',form) path for-value-p)
260                      ;; Lexical
261                      (when for-value-p
262                        (let* ((lambda-var (cdr (assoc form (lexenv-vars *lexenv*))))
263                               (handle (when lambda-var
264                                         (lambda-var-fop-value lambda-var))))
265                          (if handle
266                              (sb!fasl::dump-push handle
267                                                  *compile-object*)
268                              (progn
269                                ;; Undefined variable. Signal a warning, and
270                                ;; treat it as a special variable reference,
271                                ;; like the real compiler does.
272                                (note-undefined-reference form :variable)
273                                (fopcompile `(symbol-value ',form)
274                                            path
275                                            for-value-p))))))))))
276         ((listp form)
277          (multiple-value-bind (macroexpansion macroexpanded-p)
278              (sb!xc:macroexpand form *lexenv*)
279            (if macroexpanded-p
280                (fopcompile macroexpansion path for-value-p)
281                (destructuring-bind (operator &rest args) form
282                  (case operator
283                    ;; The QUOTE special operator is worth handling: very
284                    ;; easy and very common at toplevel.
285                    ((quote)
286                     (fopcompile-constant (second form) for-value-p))
287                    ;; A FUNCTION needs to be compiled properly, but doesn't
288                    ;; need to prevent the fopcompilation of the whole form.
289                    ;; We just compile it, and emit an instruction for pushing
290                    ;; the function handle on the FOP stack.
291                    ((function)
292                     (fopcompile-function (second form) path for-value-p))
293                    ;; KLUDGE! SB!C:SOURCE-LOCATION calls are normally handled
294                    ;; by a compiler-macro. Doing general compiler-macro
295                    ;; expansion in the fopcompiler is probably not sensible,
296                    ;; so we'll just special-case it.
297                    ((source-location)
298                     (if (policy *policy* (and (> space 1)
299                                               (> space debug)))
300                         (fopcompile-constant nil for-value-p)
301                         (fopcompile (let ((*current-path* path))
302                                       (make-definition-source-location))
303                                     path
304                                     for-value-p)))
305                    ((if)
306                     (fopcompile-if args path for-value-p))
307                    ((progn locally)
308                     (loop for (arg . next) on args
309                           do (fopcompile arg
310                                          path (if next
311                                                   nil
312                                                   for-value-p))))
313                    ((setq)
314                     (loop for (name value . next) on args by #'cddr
315                           do (fopcompile `(set ',name ,value) path
316                                          (if next
317                                              nil
318                                              for-value-p))))
319                    ((eval-when)
320                     (destructuring-bind (situations &body body) args
321                       (if (or (member :execute situations)
322                               (member 'eval situations))
323                           (fopcompile (cons 'progn body) path for-value-p)
324                           (fopcompile nil path for-value-p))))
325                    ((let let*)
326                     (let ((orig-lexenv *lexenv*)
327                           (*lexenv* (make-lexenv :default *lexenv*)))
328                       (loop for binding in (car args)
329                             for name = (if (consp binding)
330                                            (first binding)
331                                            binding)
332                             for value = (if (consp binding)
333                                             (second binding)
334                                             nil)
335                             do (let ((*lexenv* (if (eql operator 'let)
336                                                    orig-lexenv
337                                                    *lexenv*)))
338                                  (fopcompile value path t))
339                             do (let ((obj (sb!fasl::dump-pop *compile-object*)))
340                                  (setf *lexenv*
341                                        (make-lexenv
342                                         :vars (list (cons name
343                                                           (make-lambda-var
344                                                            :%source-name name
345                                                            :fop-value obj)))))))
346                       (fopcompile (cons 'progn (cdr args)) path for-value-p)))
347                    ;; Otherwise it must be an ordinary funcall.
348                    (otherwise
349                     (cond
350                       ;; Special hack: there's already a fop for
351                       ;; find-undeleted-package-or-lose, so use it.
352                       ;; (We could theoretically do the same for
353                       ;; other operations, but I don't see any good
354                       ;; candidates in a quick read-through of
355                       ;; src/code/fop.lisp.)
356                       ((and (eq operator
357                                 'sb!int:find-undeleted-package-or-lose)
358                             (= 1 (length args))
359                             for-value-p)
360                        (fopcompile (first args) path t)
361                        (sb!fasl::dump-fop 'sb!fasl::fop-package
362                                           *compile-object*))
363                       (t
364                        (fopcompile-constant operator t)
365                        (dolist (arg args)
366                          (fopcompile arg path t))
367                        (if for-value-p
368                            (sb!fasl::dump-fop 'sb!fasl::fop-funcall
369                                               *compile-object*)
370                            (sb!fasl::dump-fop 'sb!fasl::fop-funcall-for-effect
371                                               *compile-object*))
372                        (let ((n-args (length args)))
373                          ;; stub: FOP-FUNCALL isn't going to be usable
374                          ;; to compile more than this, since its count
375                          ;; is a single byte. Maybe we should just punt
376                          ;; to the ordinary compiler in that case?
377                          (aver (<= n-args 255))
378                          (sb!fasl::dump-byte n-args *compile-object*))))))))))
379         (t
380          (bug "looks unFOPCOMPILEable: ~S" form))))
381
382 (defun fopcompile-function (form path for-value-p)
383   (flet ((dump-fdefinition (name)
384            (fopcompile `(fdefinition ',name) path for-value-p)))
385     (if (consp form)
386         (cond
387           ;; Lambda forms are compiled with the real compiler
388           ((lambda-form-p form)
389            (let* ((handle (%compile form
390                                     *compile-object*
391                                     :path path)))
392              (when for-value-p
393                (sb!fasl::dump-push handle *compile-object*))))
394           ;; While function names are translated to a call to FDEFINITION.
395           ((legal-fun-name-p form)
396            (dump-fdefinition form))
397           (t
398            (compiler-error "~S is not a legal function name." form)))
399         (dump-fdefinition form))))
400
401 (defun fopcompile-if (args path for-value-p)
402   (destructuring-bind (condition then &optional else)
403       args
404     (let ((else-label (incf *fopcompile-label-counter*))
405           (end-label (incf *fopcompile-label-counter*)))
406       (sb!fasl::dump-integer else-label *compile-object*)
407       (fopcompile condition path t)
408       ;; If condition was false, skip to the ELSE
409       (sb!fasl::dump-fop 'sb!fasl::fop-skip-if-false *compile-object*)
410       (fopcompile then path for-value-p)
411       ;; The THEN branch will have produced a value even if we were
412       ;; currently skipping to the ELSE branch (or over this whole
413       ;; IF). This is done to ensure that the stack effects are
414       ;; balanced properly when dealing with operations that are
415       ;; executed even when skipping over code. But this particular
416       ;; value will be bogus, so we drop it.
417       (when for-value-p
418         (sb!fasl::dump-fop 'sb!fasl::fop-drop-if-skipping *compile-object*))
419       ;; Now skip to the END
420       (sb!fasl::dump-integer end-label *compile-object*)
421       (sb!fasl::dump-fop 'sb!fasl::fop-skip *compile-object*)
422       ;; Start of the ELSE branch
423       (sb!fasl::dump-integer else-label *compile-object*)
424       (sb!fasl::dump-fop 'sb!fasl::fop-maybe-stop-skipping *compile-object*)
425       (fopcompile else path for-value-p)
426       ;; As before
427       (when for-value-p
428         (sb!fasl::dump-fop 'sb!fasl::fop-drop-if-skipping *compile-object*))
429       ;; End of IF
430       (sb!fasl::dump-integer end-label *compile-object*)
431       (sb!fasl::dump-fop 'sb!fasl::fop-maybe-stop-skipping *compile-object*)
432       ;; If we're still skipping, we must've triggered both of the
433       ;; drop-if-skipping fops. To keep the stack balanced, push a
434       ;; dummy value if needed.
435       (when for-value-p
436         (sb!fasl::dump-fop 'sb!fasl::fop-push-nil-if-skipping
437                            *compile-object*)))))
438
439 (defun fopcompile-constant (form for-value-p)
440   (when for-value-p
441     ;; FIXME: Without this binding the dumper chokes on unvalidated
442     ;; structures: CONSTANT-FOPCOMPILABLE-P validates the structure
443     ;; about to be dumped, not its load-form. Compare and contrast
444     ;; with EMIT-MAKE-LOAD-FORM.
445     (let ((sb!fasl::*dump-only-valid-structures* nil))
446       (dump-object form *compile-object*))))