305306949503c8e5bb5d78dbcd00118a35d8001d
[sbcl.git] / src / compiler / ir1-translators.lisp
1 ;;;; the usual place for DEF-IR1-TRANSLATOR forms (and their
2 ;;;; close personal friends)
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!C")
14 \f
15 ;;;; control special forms
16
17 (def-ir1-translator progn ((&rest forms) start cont)
18   #!+sb-doc
19   "Progn Form*
20   Evaluates each Form in order, returning the values of the last form. With no
21   forms, returns NIL."
22   (ir1-convert-progn-body start cont forms))
23
24 (def-ir1-translator if ((test then &optional else) start cont)
25   #!+sb-doc
26   "If Predicate Then [Else]
27   If Predicate evaluates to non-null, evaluate Then and returns its values,
28   otherwise evaluate Else and return its values. Else defaults to NIL."
29   (let* ((pred (make-continuation))
30          (then-cont (make-continuation))
31          (then-block (continuation-starts-block then-cont))
32          (else-cont (make-continuation))
33          (else-block (continuation-starts-block else-cont))
34          (dummy-cont (make-continuation))
35          (node (make-if :test pred
36                         :consequent then-block
37                         :alternative else-block)))
38     (setf (continuation-dest pred) node)
39     (ir1-convert start pred test)
40     (link-node-to-previous-continuation node pred)
41     (use-continuation node dummy-cont)
42
43     (let ((start-block (continuation-block pred)))
44       (setf (block-last start-block) node)
45       (continuation-starts-block cont)
46
47       (link-blocks start-block then-block)
48       (link-blocks start-block else-block))
49
50     (ir1-convert then-cont cont then)
51     (ir1-convert else-cont cont else)))
52 \f
53 ;;;; BLOCK and TAGBODY
54
55 ;;;; We make an ENTRY node to mark the start and a :ENTRY cleanup to
56 ;;;; mark its extent. When doing GO or RETURN-FROM, we emit an EXIT
57 ;;;; node.
58
59 ;;; Make a :ENTRY cleanup and emit an ENTRY node, then convert the
60 ;;; body in the modified environment. We make CONT start a block now,
61 ;;; since if it was done later, the block would be in the wrong
62 ;;; environment.
63 (def-ir1-translator block ((name &rest forms) start cont)
64   #!+sb-doc
65   "Block Name Form*
66   Evaluate the Forms as a PROGN. Within the lexical scope of the body,
67   (RETURN-FROM Name Value-Form) can be used to exit the form, returning the
68   result of Value-Form."
69   (unless (symbolp name)
70     (compiler-error "The block name ~S is not a symbol." name))
71   (continuation-starts-block cont)
72   (let* ((dummy (make-continuation))
73          (entry (make-entry))
74          (cleanup (make-cleanup :kind :block
75                                 :mess-up entry)))
76     (push entry (lambda-entries (lexenv-lambda *lexenv*)))
77     (setf (entry-cleanup entry) cleanup)
78     (link-node-to-previous-continuation entry start)
79     (use-continuation entry dummy)
80     
81     (let* ((env-entry (list entry cont))
82            (*lexenv* (make-lexenv :blocks (list (cons name env-entry))
83                                   :cleanup cleanup)))
84       (push env-entry (continuation-lexenv-uses cont))
85       (ir1-convert-progn-body dummy cont forms))))
86
87
88 (def-ir1-translator return-from ((name &optional value) start cont)
89   #!+sb-doc
90   "Return-From Block-Name Value-Form
91   Evaluate the Value-Form, returning its values from the lexically enclosing
92   BLOCK Block-Name. This is constrained to be used only within the dynamic
93   extent of the BLOCK."
94   ;; CMU CL comment:
95   ;;   We make CONT start a block just so that it will have a block
96   ;;   assigned. People assume that when they pass a continuation into
97   ;;   IR1-CONVERT as CONT, it will have a block when it is done.
98   ;; KLUDGE: Note that this block is basically fictitious. In the code
99   ;;   (BLOCK B (RETURN-FROM B) (SETQ X 3))
100   ;; it's the block which answers the question "which block is
101   ;; the (SETQ X 3) in?" when the right answer is that (SETQ X 3) is
102   ;; dead code and so doesn't really have a block at all. The existence
103   ;; of this block, and that way that it doesn't explicitly say
104   ;; "I'm actually nowhere at all" makes some logic (e.g.
105   ;; BLOCK-HOME-LAMBDA-OR-NULL) more obscure, and it might be better
106   ;; to get rid of it, perhaps using a special placeholder value
107   ;; to indicate the orphanedness of the code.
108   (continuation-starts-block cont)
109   (let* ((found (or (lexenv-find name blocks)
110                     (compiler-error "return for unknown block: ~S" name)))
111          (value-cont (make-continuation))
112          (entry (first found))
113          (exit (make-exit :entry entry
114                           :value value-cont)))
115     (push exit (entry-exits entry))
116     (setf (continuation-dest value-cont) exit)
117     (ir1-convert start value-cont value)
118     (link-node-to-previous-continuation exit value-cont)
119     (let ((home-lambda (continuation-home-lambda-or-null start)))
120       (when home-lambda
121         (push entry (lambda-calls-or-closes home-lambda))))
122     (use-continuation exit (second found))))
123
124 ;;; Return a list of the segments of a TAGBODY. Each segment looks
125 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
126 ;;; tagbody into segments of non-tag statements, and explicitly
127 ;;; represent the drop-through with a GO. The first segment has a
128 ;;; dummy NIL tag, since it represents code before the first tag. The
129 ;;; last segment (which may also be the first segment) ends in NIL
130 ;;; rather than a GO.
131 (defun parse-tagbody (body)
132   (declare (list body))
133   (collect ((segments))
134     (let ((current (cons nil body)))
135       (loop
136         (let ((tag-pos (position-if (complement #'listp) current :start 1)))
137           (unless tag-pos
138             (segments `(,@current nil))
139             (return))
140           (let ((tag (elt current tag-pos)))
141             (when (assoc tag (segments))
142               (compiler-error
143                "The tag ~S appears more than once in the tagbody."
144                tag))
145             (unless (or (symbolp tag) (integerp tag))
146               (compiler-error "~S is not a legal tagbody statement." tag))
147             (segments `(,@(subseq current 0 tag-pos) (go ,tag))))
148           (setq current (nthcdr tag-pos current)))))
149     (segments)))
150
151 ;;; Set up the cleanup, emitting the entry node. Then make a block for
152 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
153 ;;; Finally, convert each segment with the precomputed Start and Cont
154 ;;; values.
155 (def-ir1-translator tagbody ((&rest statements) start cont)
156   #!+sb-doc
157   "Tagbody {Tag | Statement}*
158   Define tags for used with GO. The Statements are evaluated in order
159   (skipping Tags) and NIL is returned. If a statement contains a GO to a
160   defined Tag within the lexical scope of the form, then control is transferred
161   to the next statement following that tag. A Tag must an integer or a
162   symbol. A statement must be a list. Other objects are illegal within the
163   body."
164   (continuation-starts-block cont)
165   (let* ((dummy (make-continuation))
166          (entry (make-entry))
167          (segments (parse-tagbody statements))
168          (cleanup (make-cleanup :kind :tagbody
169                                 :mess-up entry)))
170     (push entry (lambda-entries (lexenv-lambda *lexenv*)))
171     (setf (entry-cleanup entry) cleanup)
172     (link-node-to-previous-continuation entry start)
173     (use-continuation entry dummy)
174
175     (collect ((tags)
176               (starts)
177               (conts))
178       (starts dummy)
179       (dolist (segment (rest segments))
180         (let* ((tag-cont (make-continuation))
181                (tag (list (car segment) entry tag-cont)))          
182           (conts tag-cont)
183           (starts tag-cont)
184           (continuation-starts-block tag-cont)
185           (tags tag)
186           (push (cdr tag) (continuation-lexenv-uses tag-cont))))
187       (conts cont)
188
189       (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
190         (mapc (lambda (segment start cont)
191                 (ir1-convert-progn-body start cont (rest segment)))
192               segments (starts) (conts))))))
193
194 ;;; Emit an EXIT node without any value.
195 (def-ir1-translator go ((tag) start cont)
196   #!+sb-doc
197   "Go Tag
198   Transfer control to the named Tag in the lexically enclosing TAGBODY. This
199   is constrained to be used only within the dynamic extent of the TAGBODY."
200   (continuation-starts-block cont)
201   (let* ((found (or (lexenv-find tag tags :test #'eql)
202                     (compiler-error "attempt to GO to nonexistent tag: ~S"
203                                     tag)))
204          (entry (first found))
205          (exit (make-exit :entry entry)))
206     (push exit (entry-exits entry))
207     (link-node-to-previous-continuation exit start)
208     (let ((home-lambda (continuation-home-lambda-or-null start)))
209       (when home-lambda
210         (push entry (lambda-calls-or-closes home-lambda))))
211     (use-continuation exit (second found))))
212 \f
213 ;;;; translators for compiler-magic special forms
214
215 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in top
216 ;;; level forms are picked off and handled by PROCESS-TOPLEVEL-FORM,
217 ;;; so that they're never seen at this level.)
218 ;;;
219 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
220 ;;; of non-top-level EVAL-WHENs is very simple:
221 ;;;   EVAL-WHEN forms cause compile-time evaluation only at top level.
222 ;;;   Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
223 ;;;   are ignored for non-top-level forms. For non-top-level forms, an
224 ;;;   eval-when specifying the :EXECUTE situation is treated as an
225 ;;;   implicit PROGN including the forms in the body of the EVAL-WHEN
226 ;;;   form; otherwise, the forms in the body are ignored. 
227 (def-ir1-translator eval-when ((situations &rest forms) start cont)
228   #!+sb-doc
229   "EVAL-WHEN (Situation*) Form*
230   Evaluate the Forms in the specified Situations (any of :COMPILE-TOPLEVEL,
231   :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
232   (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
233     (declare (ignore ct lt))
234     (ir1-convert-progn-body start cont (and e forms)))
235   (values))
236
237 ;;; common logic for MACROLET and SYMBOL-MACROLET
238 ;;;
239 ;;; Call DEFINITIONIZE-FUN on each element of DEFINITIONS to find its
240 ;;; in-lexenv representation, stuff the results into *LEXENV*, and
241 ;;; call FUN (with no arguments).
242 (defun %funcall-in-foomacrolet-lexenv (definitionize-fun
243                                        definitionize-keyword
244                                        definitions
245                                        fun)
246   (declare (type function definitionize-fun fun))
247   (declare (type (member :vars :funs) definitionize-keyword))
248   (declare (type list definitions))
249   (unless (= (length definitions)
250              (length (remove-duplicates definitions :key #'first)))
251     (compiler-style-warn "duplicate definitions in ~S" definitions))
252   (let* ((processed-definitions (mapcar definitionize-fun definitions))
253          (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
254     (funcall fun)))
255
256 ;;; Tweak *LEXENV* to include the DEFINITIONS from a MACROLET, then
257 ;;; call FUN (with no arguments).
258 ;;;
259 ;;; This is split off from the IR1 convert method so that it can be
260 ;;; shared by the special-case top level MACROLET processing code.
261 (defun funcall-in-macrolet-lexenv (definitions fun)
262   (%funcall-in-foomacrolet-lexenv
263    (lambda (definition)
264      (unless (list-of-length-at-least-p definition 2)
265        (compiler-error
266         "The list ~S is too short to be a legal local macro definition."
267         definition))
268      (destructuring-bind (name arglist &body body) definition
269        (unless (symbolp name)
270          (compiler-error "The local macro name ~S is not a symbol." name))
271        (let ((whole (gensym "WHOLE"))
272              (environment (gensym "ENVIRONMENT")))
273          (multiple-value-bind (body local-decls)
274              (parse-defmacro arglist whole body name 'macrolet
275                              :environment environment)
276            `(,name macro .
277                    ,(compile nil
278                              `(lambda (,whole ,environment)
279                                 ,@local-decls
280                                 (block ,name ,body))))))))
281    :funs
282    definitions
283    fun))
284
285 (def-ir1-translator macrolet ((definitions &rest body) start cont)
286   #!+sb-doc
287   "MACROLET ({(Name Lambda-List Form*)}*) Body-Form*
288   Evaluate the Body-Forms in an environment with the specified local macros
289   defined. Name is the local macro name, Lambda-List is the DEFMACRO style
290   destructuring lambda list, and the Forms evaluate to the expansion. The
291   Forms are evaluated in the null environment."
292   (funcall-in-macrolet-lexenv definitions
293                               (lambda ()
294                                 (ir1-translate-locally body start cont))))
295
296 (defun funcall-in-symbol-macrolet-lexenv (definitions fun)
297   (%funcall-in-foomacrolet-lexenv
298    (lambda (definition)
299      (unless (proper-list-of-length-p definition 2)
300        (compiler-error "malformed symbol/expansion pair: ~S" definition))
301      (destructuring-bind (name expansion) definition
302        (unless (symbolp name)
303          (compiler-error
304           "The local symbol macro name ~S is not a symbol."
305           name))
306        `(,name . (MACRO . ,expansion))))
307    :vars
308    definitions
309    fun))
310   
311 (def-ir1-translator symbol-macrolet ((macrobindings &body body) start cont)
312   #!+sb-doc
313   "SYMBOL-MACROLET ({(Name Expansion)}*) Decl* Form*
314   Define the Names as symbol macros with the given Expansions. Within the
315   body, references to a Name will effectively be replaced with the Expansion."
316   (funcall-in-symbol-macrolet-lexenv
317    macrobindings
318    (lambda ()
319      (ir1-translate-locally body start cont))))
320
321 ;;; not really a special form, but..
322 (def-ir1-translator declare ((&rest stuff) start cont)
323   (declare (ignore stuff))
324   ;; We ignore START and CONT too, but we can't use DECLARE IGNORE to
325   ;; tell the compiler about it here, because the DEF-IR1-TRANSLATOR
326   ;; macro would put the DECLARE in the wrong place, so..
327   start cont
328   (compiler-error "misplaced declaration"))
329 \f
330 ;;;; %PRIMITIVE
331 ;;;;
332 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
333 ;;;; into a funny function.
334
335 ;;; Carefully evaluate a list of forms, returning a list of the results.
336 (defun eval-info-args (args)
337   (declare (list args))
338   (handler-case (mapcar #'eval args)
339     (error (condition)
340       (compiler-error "Lisp error during evaluation of info args:~%~A"
341                       condition))))
342
343 ;;; Convert to the %%PRIMITIVE funny function. The first argument is
344 ;;; the template, the second is a list of the results of any
345 ;;; codegen-info args, and the remaining arguments are the runtime
346 ;;; arguments.
347 ;;;
348 ;;; We do various error checking now so that we don't bomb out with
349 ;;; a fatal error during IR2 conversion.
350 ;;;
351 ;;; KLUDGE: It's confusing having multiple names floating around for
352 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
353 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
354 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
355 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
356 ;;; VOP or %VOP.. -- WHN 2001-06-11
357 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
358 (def-ir1-translator %primitive ((name &rest args) start cont)
359   (declare (type symbol name))
360   (let* ((template (or (gethash name *backend-template-names*)
361                        (bug "undefined primitive ~A" name)))
362          (required (length (template-arg-types template)))
363          (info (template-info-arg-count template))
364          (min (+ required info))
365          (nargs (length args)))
366     (if (template-more-args-type template)
367         (when (< nargs min)
368           (bug "Primitive ~A was called with ~R argument~:P, ~
369                 but wants at least ~R."
370                name
371                nargs
372                min))
373         (unless (= nargs min)
374           (bug "Primitive ~A was called with ~R argument~:P, ~
375                 but wants exactly ~R."
376                name
377                nargs
378                min)))
379
380     (when (eq (template-result-types template) :conditional)
381       (bug "%PRIMITIVE was used with a conditional template."))
382
383     (when (template-more-results-type template)
384       (bug "%PRIMITIVE was used with an unknown values template."))
385
386     (ir1-convert start
387                  cont
388                  `(%%primitive ',template
389                                ',(eval-info-args
390                                   (subseq args required min))
391                                ,@(subseq args 0 required)
392                                ,@(subseq args min)))))
393 \f
394 ;;;; QUOTE
395
396 (def-ir1-translator quote ((thing) start cont)
397   #!+sb-doc
398   "QUOTE Value
399   Return Value without evaluating it."
400   (reference-constant start cont thing))
401 \f
402 ;;;; FUNCTION and NAMED-LAMBDA
403
404 (def-ir1-translator function ((thing) start cont)
405   #!+sb-doc
406   "FUNCTION Name
407   Return the lexically apparent definition of the function Name. Name may also
408   be a lambda expression."
409   (if (consp thing)
410       (case (car thing)
411         ((lambda)
412          (reference-leaf start
413                          cont
414                          (ir1-convert-lambda thing
415                                              :debug-name (debug-namify
416                                                           "#'~S" thing))))
417         ((setf)
418          (let ((var (find-lexically-apparent-fun
419                      thing "as the argument to FUNCTION")))
420            (reference-leaf start cont var)))
421         ((instance-lambda)
422          (let ((res (ir1-convert-lambda `(lambda ,@(cdr thing))
423                                         :debug-name (debug-namify "#'~S"
424                                                                   thing))))
425            (setf (getf (functional-plist res) :fin-function) t)
426            (reference-leaf start cont res)))
427         (t
428          (compiler-error "~S is not a legal function name." thing)))
429       (let ((var (find-lexically-apparent-fun
430                   thing "as the argument to FUNCTION")))
431         (reference-leaf start cont var))))
432
433 ;;; `(NAMED-LAMBDA ,NAME ,@REST) is like `(FUNCTION (LAMBDA ,@REST)),
434 ;;; except that the value of NAME is passed to the compiler for use in
435 ;;; creation of debug information for the resulting function.
436 ;;;
437 ;;; NAME can be a legal function name or some arbitrary other thing.
438 ;;;
439 ;;; If NAME is a legal function name, then the caller should be
440 ;;; planning to set (FDEFINITION NAME) to the created function.
441 ;;; (Otherwise the debug names will be inconsistent and thus
442 ;;; unnecessarily confusing.)
443 ;;;
444 ;;; Arbitrary other things are appropriate for naming things which are
445 ;;; not the FDEFINITION of NAME. E.g.
446 ;;;   NAME = (:FLET FOO BAR)
447 ;;; for the FLET function in
448 ;;;   (DEFUN BAR (X)
449 ;;;     (FLET ((FOO (Y) (+ X Y)))
450 ;;;       FOO))
451 ;;; or
452 ;;;   NAME = (:METHOD PRINT-OBJECT :AROUND (STARSHIP T))
453 ;;; for the function used to implement
454 ;;;   (DEFMETHOD PRINT-OBJECT :AROUND ((SS STARSHIP) STREAM) ...).
455 (def-ir1-translator named-lambda ((name &rest rest) start cont)
456   (reference-leaf start
457                   cont
458                   (if (legal-fun-name-p name)
459                       (ir1-convert-lambda `(lambda ,@rest)
460                                           :source-name name)
461                       (ir1-convert-lambda `(lambda ,@rest)
462                                           :debug-name name))))
463 \f
464 ;;;; FUNCALL
465
466 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
467 ;;; (not symbols). %FUNCALL is used directly in some places where the
468 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
469 (deftransform funcall ((function &rest args) * * :when :both)
470   (let ((arg-names (make-gensym-list (length args))))
471     `(lambda (function ,@arg-names)
472        (%funcall ,(if (csubtypep (continuation-type function)
473                                  (specifier-type 'function))
474                       'function
475                       '(%coerce-callable-to-fun function))
476                  ,@arg-names))))
477
478 (def-ir1-translator %funcall ((function &rest args) start cont)
479   (let ((fun-cont (make-continuation)))
480     (ir1-convert start fun-cont function)
481     (assert-continuation-type fun-cont (specifier-type 'function))
482     (ir1-convert-combination-args fun-cont cont args)))
483
484 ;;; This source transform exists to reduce the amount of work for the
485 ;;; compiler. If the called function is a FUNCTION form, then convert
486 ;;; directly to %FUNCALL, instead of waiting around for type
487 ;;; inference.
488 (define-source-transform funcall (function &rest args)
489   (if (and (consp function) (eq (car function) 'function))
490       `(%funcall ,function ,@args)
491       (values nil t)))
492
493 (deftransform %coerce-callable-to-fun ((thing) (function) *
494                                        :when :both
495                                        :important t)
496   "optimize away possible call to FDEFINITION at runtime"
497   'thing)
498 \f
499 ;;;; LET and LET*
500 ;;;;
501 ;;;; (LET and LET* can't be implemented as macros due to the fact that
502 ;;;; any pervasive declarations also affect the evaluation of the
503 ;;;; arguments.)
504
505 ;;; Given a list of binding specifiers in the style of LET, return:
506 ;;;  1. The list of var structures for the variables bound.
507 ;;;  2. The initial value form for each variable.
508 ;;;
509 ;;; The variable names are checked for legality and globally special
510 ;;; variables are marked as such. Context is the name of the form, for
511 ;;; error reporting purposes.
512 (declaim (ftype (function (list symbol) (values list list))
513                 extract-let-vars))
514 (defun extract-let-vars (bindings context)
515   (collect ((vars)
516             (vals)
517             (names))
518     (flet ((get-var (name)
519              (varify-lambda-arg name
520                                 (if (eq context 'let*)
521                                     nil
522                                     (names)))))
523       (dolist (spec bindings)
524         (cond ((atom spec)
525                (let ((var (get-var spec)))
526                  (vars var)
527                  (names spec)
528                  (vals nil)))
529               (t
530                (unless (proper-list-of-length-p spec 1 2)
531                  (compiler-error "The ~S binding spec ~S is malformed."
532                                  context
533                                  spec))
534                (let* ((name (first spec))
535                       (var (get-var name)))
536                  (vars var)
537                  (names name)
538                  (vals (second spec)))))))
539
540     (values (vars) (vals))))
541
542 (def-ir1-translator let ((bindings &body body)
543                          start cont)
544   #!+sb-doc
545   "LET ({(Var [Value]) | Var}*) Declaration* Form*
546   During evaluation of the Forms, bind the Vars to the result of evaluating the
547   Value forms. The variables are bound in parallel after all of the Values are
548   evaluated."
549   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
550     (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
551       (let ((fun-cont (make-continuation)))
552         (let* ((*lexenv* (process-decls decls vars nil cont))
553                (fun (ir1-convert-lambda-body
554                      forms vars
555                      :debug-name (debug-namify "LET ~S" bindings))))
556           (reference-leaf start fun-cont fun))
557         (ir1-convert-combination-args fun-cont cont values)))))
558
559 (def-ir1-translator let* ((bindings &body body)
560                           start cont)
561   #!+sb-doc
562   "LET* ({(Var [Value]) | Var}*) Declaration* Form*
563   Similar to LET, but the variables are bound sequentially, allowing each Value
564   form to reference any of the previous Vars."
565   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
566     (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
567       (let ((*lexenv* (process-decls decls vars nil cont)))
568         (ir1-convert-aux-bindings start cont forms vars values)))))
569
570 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
571 ;;; and SYMBOL-MACROLET
572 ;;;
573 ;;; Note that all these things need to preserve toplevel-formness,
574 ;;; but we don't need to worry about that within an IR1 translator,
575 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
576 ;;; forms before we hit the IR1 transform level.
577 (defun ir1-translate-locally (body start cont)
578   (declare (type list body) (type continuation start cont))
579   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
580     (let ((*lexenv* (process-decls decls nil nil cont)))
581       (ir1-convert-aux-bindings start cont forms nil nil))))
582
583 (def-ir1-translator locally ((&body body) start cont)
584   #!+sb-doc
585   "LOCALLY Declaration* Form*
586   Sequentially evaluate the Forms in a lexical environment where the
587   the Declarations have effect. If LOCALLY is a top level form, then
588   the Forms are also processed as top level forms."
589   (ir1-translate-locally body start cont))
590 \f
591 ;;;; FLET and LABELS
592
593 ;;; Given a list of local function specifications in the style of
594 ;;; FLET, return lists of the function names and of the lambdas which
595 ;;; are their definitions.
596 ;;;
597 ;;; The function names are checked for legality. CONTEXT is the name
598 ;;; of the form, for error reporting.
599 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
600 (defun extract-flet-vars (definitions context)
601   (collect ((names)
602             (defs))
603     (dolist (def definitions)
604       (when (or (atom def) (< (length def) 2))
605         (compiler-error "The ~S definition spec ~S is malformed." context def))
606
607       (let ((name (first def)))
608         (check-fun-name name)
609         (names name)
610         (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr def))
611           (defs `(lambda ,(second def)
612                    ,@decls
613                    (block ,(fun-name-block-name name)
614                      . ,forms))))))
615     (values (names) (defs))))
616
617 (def-ir1-translator flet ((definitions &body body)
618                           start cont)
619   #!+sb-doc
620   "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
621   Evaluate the Body-Forms with some local function definitions. The bindings
622   do not enclose the definitions; any use of Name in the Forms will refer to
623   the lexically apparent function definition in the enclosing environment."
624   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
625     (multiple-value-bind (names defs)
626         (extract-flet-vars definitions 'flet)
627       (let* ((fvars (mapcar (lambda (n d)
628                               (ir1-convert-lambda d
629                                                   :source-name n
630                                                   :debug-name (debug-namify
631                                                                "FLET ~S" n)))
632                             names defs))
633              (*lexenv* (make-lexenv
634                         :default (process-decls decls nil fvars cont)
635                         :funs (pairlis names fvars))))
636         (ir1-convert-progn-body start cont forms)))))
637
638 (def-ir1-translator labels ((definitions &body body) start cont)
639   #!+sb-doc
640   "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
641   Evaluate the Body-Forms with some local function definitions. The bindings
642   enclose the new definitions, so the defined functions can call themselves or
643   each other."
644   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
645     (multiple-value-bind (names defs)
646         (extract-flet-vars definitions 'labels)
647       (let* (;; dummy LABELS functions, to be used as placeholders
648              ;; during construction of real LABELS functions
649              (placeholder-funs (mapcar (lambda (name)
650                                          (make-functional
651                                           :%source-name name
652                                           :%debug-name (debug-namify
653                                                         "LABELS placeholder ~S"
654                                                         name)))
655                                        names))
656              ;; (like PAIRLIS but guaranteed to preserve ordering:)
657              (placeholder-fenv (mapcar #'cons names placeholder-funs))
658              ;; the real LABELS functions, compiled in a LEXENV which
659              ;; includes the dummy LABELS functions
660              (real-funs
661               (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
662                 (mapcar (lambda (name def)
663                           (ir1-convert-lambda def
664                                               :source-name name
665                                               :debug-name (debug-namify
666                                                            "LABELS ~S" name)))
667                         names defs))))
668
669         ;; Modify all the references to the dummy function leaves so
670         ;; that they point to the real function leaves.
671         (loop for real-fun in real-funs and
672               placeholder-cons in placeholder-fenv do
673               (substitute-leaf real-fun (cdr placeholder-cons))
674               (setf (cdr placeholder-cons) real-fun))
675
676         ;; Voila.
677         (let ((*lexenv* (make-lexenv
678                          :default (process-decls decls nil real-funs cont)
679                          ;; Use a proper FENV here (not the
680                          ;; placeholder used earlier) so that if the
681                          ;; lexical environment is used for inline
682                          ;; expansion we'll get the right functions.
683                          :funs (pairlis names real-funs))))
684           (ir1-convert-progn-body start cont forms))))))
685 \f
686 ;;;; the THE special operator, and friends
687
688 ;;; Do stuff to recognize a THE or VALUES declaration. CONT is the
689 ;;; continuation that the assertion applies to, TYPE is the type
690 ;;; specifier and LEXENV is the current lexical environment. NAME is
691 ;;; the name of the declaration we are doing, for use in error
692 ;;; messages.
693 ;;;
694 ;;; This is somewhat involved, since a type assertion may only be made
695 ;;; on a continuation, not on a node. We can't just set the
696 ;;; continuation asserted type and let it go at that, since there may
697 ;;; be parallel THE's for the same continuation, i.e.
698 ;;;     (if ...
699 ;;;      (the foo ...)
700 ;;;      (the bar ...))
701 ;;;
702 ;;; In this case, our representation can do no better than the union
703 ;;; of these assertions. And if there is a branch with no assertion,
704 ;;; we have nothing at all. We really need to recognize scoping, since
705 ;;; we need to be able to discern between parallel assertions (which
706 ;;; we union) and nested ones (which we intersect).
707 ;;;
708 ;;; We represent the scoping by throwing our innermost (intersected)
709 ;;; assertion on CONT into the TYPE-RESTRICTIONS. As we go down, we
710 ;;; intersect our assertions together. If CONT has no uses yet, we
711 ;;; have not yet bottomed out on the first COND branch; in this case
712 ;;; we optimistically assume that this type will be the one we end up
713 ;;; with, and set the ASSERTED-TYPE to it. We can never get better
714 ;;; than the type that we have the first time we bottom out. Later
715 ;;; THE's (or the absence thereof) can only weaken this result.
716 ;;;
717 ;;; We make this work by getting USE-CONTINUATION to do the unioning
718 ;;; across COND branches. We can't do it here, since we don't know how
719 ;;; many branches there are going to be.
720 (defun ir1ize-the-or-values (type cont lexenv name)
721   (declare (type continuation cont) (type lexenv lexenv))
722   (let* ((ctype (values-specifier-type type))
723          (old-type (or (lexenv-find cont type-restrictions)
724                        *wild-type*))
725          (intersects (values-types-equal-or-intersect old-type ctype))
726          (int (values-type-intersection old-type ctype))
727          (new (if intersects int old-type)))
728     (when (null (find-uses cont))
729       (setf (continuation-asserted-type cont) new))
730     (when (and (not intersects)
731                (not (policy *lexenv*
732                             (= inhibit-warnings 3)))) ;FIXME: really OK to suppress?
733       (compiler-warn
734        "The type ~S in ~S declaration conflicts with an ~
735         enclosing assertion:~%   ~S"
736        (type-specifier ctype)
737        name
738        (type-specifier old-type)))
739     (make-lexenv :type-restrictions `((,cont . ,new))
740                  :default lexenv)))
741
742 ;;; Assert that FORM evaluates to the specified type (which may be a
743 ;;; VALUES type).
744 ;;;
745 ;;; FIXME: In a version of CMU CL that I used at Cadabra ca. 20000101,
746 ;;; this didn't seem to expand into an assertion, at least for ALIEN
747 ;;; values. Check that SBCL doesn't have this problem.
748 (def-ir1-translator the ((type value) start cont)
749   (let ((*lexenv* (ir1ize-the-or-values type cont *lexenv* 'the)))
750     (ir1-convert start cont value)))
751
752 ;;; This is like the THE special form, except that it believes
753 ;;; whatever you tell it. It will never generate a type check, but
754 ;;; will cause a warning if the compiler can prove the assertion is
755 ;;; wrong.
756 ;;;
757 ;;; Since the CONTINUATION-DERIVED-TYPE is computed as the union of
758 ;;; its uses's types, setting it won't work. Instead we must intersect
759 ;;; the type with the uses's DERIVED-TYPE.
760 (def-ir1-translator truly-the ((type value) start cont)
761   #!+sb-doc
762   (declare (inline member))
763   (let ((type (values-specifier-type type))
764         (old (find-uses cont)))
765     (ir1-convert start cont value)
766     (do-uses (use cont)
767       (unless (member use old :test #'eq)
768         (derive-node-type use type)))))
769 \f
770 ;;;; SETQ
771
772 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
773 ;;; look at the global information. If the name is for a constant,
774 ;;; then error out.
775 (def-ir1-translator setq ((&whole source &rest things) start cont)
776   (let ((len (length things)))
777     (when (oddp len)
778       (compiler-error "odd number of args to SETQ: ~S" source))
779     (if (= len 2)
780         (let* ((name (first things))
781                (leaf (or (lexenv-find name vars)
782                          (find-free-var name))))
783           (etypecase leaf
784             (leaf
785              (when (constant-p leaf)
786                (compiler-error "~S is a constant and thus can't be set." name))
787              (when (lambda-var-p leaf)
788                (let ((home-lambda (continuation-home-lambda-or-null start)))
789                  (when home-lambda
790                    (pushnew leaf (lambda-calls-or-closes home-lambda))))
791                (when (lambda-var-ignorep leaf)
792                  ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
793                  ;; requires that this be a STYLE-WARNING, not a full warning.
794                  (compiler-style-warn
795                   "~S is being set even though it was declared to be ignored."
796                   name)))
797              (setq-var start cont leaf (second things)))
798             (cons
799              (aver (eq (car leaf) 'MACRO))
800              (ir1-convert start cont `(setf ,(cdr leaf) ,(second things))))
801             (heap-alien-info
802              (ir1-convert start cont
803                           `(%set-heap-alien ',leaf ,(second things))))))
804         (collect ((sets))
805           (do ((thing things (cddr thing)))
806               ((endp thing)
807                (ir1-convert-progn-body start cont (sets)))
808             (sets `(setq ,(first thing) ,(second thing))))))))
809
810 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
811 ;;; This should only need to be called in SETQ.
812 (defun setq-var (start cont var value)
813   (declare (type continuation start cont) (type basic-var var))
814   (let ((dest (make-continuation)))
815     (setf (continuation-asserted-type dest) (leaf-type var))
816     (ir1-convert start dest value)
817     (let ((res (make-set :var var :value dest)))
818       (setf (continuation-dest dest) res)
819       (setf (leaf-ever-used var) t)
820       (push res (basic-var-sets var))
821       (link-node-to-previous-continuation res dest)
822       (use-continuation res cont))))
823 \f
824 ;;;; CATCH, THROW and UNWIND-PROTECT
825
826 ;;; We turn THROW into a multiple-value-call of a magical function,
827 ;;; since as as far as IR1 is concerned, it has no interesting
828 ;;; properties other than receiving multiple-values.
829 (def-ir1-translator throw ((tag result) start cont)
830   #!+sb-doc
831   "Throw Tag Form
832   Do a non-local exit, return the values of Form from the CATCH whose tag
833   evaluates to the same thing as Tag."
834   (ir1-convert start cont
835                `(multiple-value-call #'%throw ,tag ,result)))
836
837 ;;; This is a special special form used to instantiate a cleanup as
838 ;;; the current cleanup within the body. KIND is the kind of cleanup
839 ;;; to make, and MESS-UP is a form that does the mess-up action. We
840 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
841 ;;; and introduce the cleanup into the lexical environment. We
842 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
843 ;;; cleanup, since this inner cleanup is the interesting one.
844 (def-ir1-translator %within-cleanup ((kind mess-up &body body) start cont)
845   (let ((dummy (make-continuation))
846         (dummy2 (make-continuation)))
847     (ir1-convert start dummy mess-up)
848     (let* ((mess-node (continuation-use dummy))
849            (cleanup (make-cleanup :kind kind
850                                   :mess-up mess-node))
851            (old-cup (lexenv-cleanup *lexenv*))
852            (*lexenv* (make-lexenv :cleanup cleanup)))
853       (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
854       (ir1-convert dummy dummy2 '(%cleanup-point))
855       (ir1-convert-progn-body dummy2 cont body))))
856
857 ;;; This is a special special form that makes an "escape function"
858 ;;; which returns unknown values from named block. We convert the
859 ;;; function, set its kind to :ESCAPE, and then reference it. The
860 ;;; :ESCAPE kind indicates that this function's purpose is to
861 ;;; represent a non-local control transfer, and that it might not
862 ;;; actually have to be compiled.
863 ;;;
864 ;;; Note that environment analysis replaces references to escape
865 ;;; functions with references to the corresponding NLX-INFO structure.
866 (def-ir1-translator %escape-fun ((tag) start cont)
867   (let ((fun (ir1-convert-lambda
868               `(lambda ()
869                  (return-from ,tag (%unknown-values)))
870               :debug-name (debug-namify "escape function for ~S" tag))))
871     (setf (functional-kind fun) :escape)
872     (reference-leaf start cont fun)))
873
874 ;;; Yet another special special form. This one looks up a local
875 ;;; function and smashes it to a :CLEANUP function, as well as
876 ;;; referencing it.
877 (def-ir1-translator %cleanup-fun ((name) start cont)
878   (let ((fun (lexenv-find name funs)))
879     (aver (lambda-p fun))
880     (setf (functional-kind fun) :cleanup)
881     (reference-leaf start cont fun)))
882
883 ;;; We represent the possibility of the control transfer by making an
884 ;;; "escape function" that does a lexical exit, and instantiate the
885 ;;; cleanup using %WITHIN-CLEANUP.
886 (def-ir1-translator catch ((tag &body body) start cont)
887   #!+sb-doc
888   "Catch Tag Form*
889   Evaluates Tag and instantiates it as a catcher while the body forms are
890   evaluated in an implicit PROGN. If a THROW is done to Tag within the dynamic
891   scope of the body, then control will be transferred to the end of the body
892   and the thrown values will be returned."
893   (ir1-convert
894    start cont
895    (let ((exit-block (gensym "EXIT-BLOCK-")))
896      `(block ,exit-block
897         (%within-cleanup
898             :catch
899             (%catch (%escape-fun ,exit-block) ,tag)
900           ,@body)))))
901
902 ;;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
903 ;;; cleanup forms into a local function so that they can be referenced
904 ;;; both in the case where we are unwound and in any local exits. We
905 ;;; use %CLEANUP-FUN on this to indicate that reference by
906 ;;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
907 ;;; an XEP.
908 (def-ir1-translator unwind-protect ((protected &body cleanup) start cont)
909   #!+sb-doc
910   "Unwind-Protect Protected Cleanup*
911   Evaluate the form Protected, returning its values. The cleanup forms are
912   evaluated whenever the dynamic scope of the Protected form is exited (either
913   due to normal completion or a non-local exit such as THROW)."
914   (ir1-convert
915    start cont
916    (let ((cleanup-fun (gensym "CLEANUP-FUN-"))
917          (drop-thru-tag (gensym "DROP-THRU-TAG-"))
918          (exit-tag (gensym "EXIT-TAG-"))
919          (next (gensym "NEXT"))
920          (start (gensym "START"))
921          (count (gensym "COUNT")))
922      `(flet ((,cleanup-fun () ,@cleanup nil))
923         ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
924         ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
925         ;; and something can be done to make %ESCAPE-FUN have
926         ;; dynamic extent too.
927         (block ,drop-thru-tag
928           (multiple-value-bind (,next ,start ,count)
929               (block ,exit-tag
930                 (%within-cleanup
931                     :unwind-protect
932                     (%unwind-protect (%escape-fun ,exit-tag)
933                                      (%cleanup-fun ,cleanup-fun))
934                   (return-from ,drop-thru-tag ,protected)))
935             (,cleanup-fun)
936             (%continue-unwind ,next ,start ,count)))))))
937 \f
938 ;;;; multiple-value stuff
939
940 ;;; If there are arguments, MULTIPLE-VALUE-CALL turns into an
941 ;;; MV-COMBINATION.
942 ;;;
943 ;;; If there are no arguments, then we convert to a normal
944 ;;; combination, ensuring that a MV-COMBINATION always has at least
945 ;;; one argument. This can be regarded as an optimization, but it is
946 ;;; more important for simplifying compilation of MV-COMBINATIONS.
947 (def-ir1-translator multiple-value-call ((fun &rest args) start cont)
948   #!+sb-doc
949   "MULTIPLE-VALUE-CALL Function Values-Form*
950   Call Function, passing all the values of each Values-Form as arguments,
951   values from the first Values-Form making up the first argument, etc."
952   (let* ((fun-cont (make-continuation))
953          (node (if args
954                    (make-mv-combination fun-cont)
955                    (make-combination fun-cont))))
956     (ir1-convert start fun-cont
957                  (if (and (consp fun) (eq (car fun) 'function))
958                      fun
959                      `(%coerce-callable-to-fun ,fun)))
960     (setf (continuation-dest fun-cont) node)
961     (assert-continuation-type fun-cont
962                               (specifier-type '(or function symbol)))
963     (collect ((arg-conts))
964       (let ((this-start fun-cont))
965         (dolist (arg args)
966           (let ((this-cont (make-continuation node)))
967             (ir1-convert this-start this-cont arg)
968             (setq this-start this-cont)
969             (arg-conts this-cont)))
970         (link-node-to-previous-continuation node this-start)
971         (use-continuation node cont)
972         (setf (basic-combination-args node) (arg-conts))))))
973
974 ;;; MULTIPLE-VALUE-PROG1 is represented implicitly in IR1 by having a
975 ;;; the result code use result continuation (CONT), but transfer
976 ;;; control to the evaluation of the body. In other words, the result
977 ;;; continuation isn't IMMEDIATELY-USED-P by the nodes that compute
978 ;;; the result.
979 ;;;
980 ;;; In order to get the control flow right, we convert the result with
981 ;;; a dummy result continuation, then convert all the uses of the
982 ;;; dummy to be uses of CONT. If a use is an EXIT, then we also
983 ;;; substitute CONT for the dummy in the corresponding ENTRY node so
984 ;;; that they are consistent. Note that this doesn't amount to
985 ;;; changing the exit target, since the control destination of an exit
986 ;;; is determined by the block successor; we are just indicating the
987 ;;; continuation that the result is delivered to.
988 ;;;
989 ;;; We then convert the body, using another dummy continuation in its
990 ;;; own block as the result. After we are done converting the body, we
991 ;;; move all predecessors of the dummy end block to CONT's block.
992 ;;;
993 ;;; Note that we both exploit and maintain the invariant that the CONT
994 ;;; to an IR1 convert method either has no block or starts the block
995 ;;; that control should transfer to after completion for the form.
996 ;;; Nested MV-PROG1's work because during conversion of the result
997 ;;; form, we use dummy continuation whose block is the true control
998 ;;; destination.
999 (def-ir1-translator multiple-value-prog1 ((result &rest forms) start cont)
1000   #!+sb-doc
1001   "MULTIPLE-VALUE-PROG1 Values-Form Form*
1002   Evaluate Values-Form and then the Forms, but return all the values of
1003   Values-Form."
1004   (continuation-starts-block cont)
1005   (let* ((dummy-result (make-continuation))
1006          (dummy-start (make-continuation))
1007          (cont-block (continuation-block cont)))
1008     (continuation-starts-block dummy-start)
1009     (ir1-convert start dummy-start result)
1010
1011     (substitute-continuation-uses cont dummy-start)
1012
1013     (continuation-starts-block dummy-result)
1014     (ir1-convert-progn-body dummy-start dummy-result forms)
1015     (let ((end-block (continuation-block dummy-result)))
1016       (dolist (pred (block-pred end-block))
1017         (unlink-blocks pred end-block)
1018         (link-blocks pred cont-block))
1019       (aver (not (continuation-dest dummy-result)))
1020       (delete-continuation dummy-result)
1021       (remove-from-dfo end-block))))
1022 \f
1023 ;;;; interface to defining macros
1024
1025 ;;;; FIXME:
1026 ;;;;   classic CMU CL comment:
1027 ;;;;     DEFMACRO and DEFUN expand into calls to %DEFxxx functions
1028 ;;;;     so that we get a chance to see what is going on. We define
1029 ;;;;     IR1 translators for these functions which look at the
1030 ;;;;     definition and then generate a call to the %%DEFxxx function.
1031 ;;;; Alas, this implementation doesn't do the right thing for
1032 ;;;; non-toplevel uses of these forms, so this should probably
1033 ;;;; be changed to use EVAL-WHEN instead.
1034
1035 ;;; Return a new source path with any stuff intervening between the
1036 ;;; current path and the first form beginning with NAME stripped off.
1037 ;;; This is used to hide the guts of DEFmumble macros to prevent
1038 ;;; annoying error messages.
1039 (defun revert-source-path (name)
1040   (do ((path *current-path* (cdr path)))
1041       ((null path) *current-path*)
1042     (let ((first (first path)))
1043       (when (or (eq first name)
1044                 (eq first 'original-source-start))
1045         (return path)))))
1046
1047 ;;; Warn about incompatible or illegal definitions and add the macro
1048 ;;; to the compiler environment.
1049 ;;;
1050 ;;; Someday we could check for macro arguments being incompatibly
1051 ;;; redefined. Doing this right will involve finding the old macro
1052 ;;; lambda-list and comparing it with the new one.
1053 (def-ir1-translator %defmacro ((qname qdef lambda-list doc) start cont
1054                                :kind :function)
1055   (let (;; QNAME is typically a quoted name. I think the idea is to
1056         ;; let %DEFMACRO work as an ordinary function when
1057         ;; interpreting. Whatever the reason the quote is there, we
1058         ;; don't want it any more. -- WHN 19990603
1059         (name (eval qname))
1060         ;; QDEF should be a sharp-quoted definition. We don't want to
1061         ;; make a function of it just yet, so we just drop the
1062         ;; sharp-quote.
1063         (def (progn
1064                (aver (eq 'function (first qdef)))
1065                (aver (proper-list-of-length-p qdef 2))
1066                (second qdef))))
1067
1068     (/show "doing IR1 translator for %DEFMACRO" name)
1069
1070     (unless (symbolp name)
1071       (compiler-error "The macro name ~S is not a symbol." name))
1072
1073     (ecase (info :function :kind name)
1074       ((nil))
1075       (:function
1076        (remhash name *free-funs*)
1077        (undefine-fun-name name)
1078        (compiler-warn
1079         "~S is being redefined as a macro when it was ~
1080          previously ~(~A~) to be a function."
1081         name
1082         (info :function :where-from name)))
1083       (:macro)
1084       (:special-form
1085        (compiler-error "The special form ~S can't be redefined as a macro."
1086                        name)))
1087
1088     (setf (info :function :kind name) :macro
1089           (info :function :where-from name) :defined
1090           (info :function :macro-function name) (coerce def 'function))
1091
1092     (let* ((*current-path* (revert-source-path 'defmacro))
1093            (fun (ir1-convert-lambda def 
1094                                     :debug-name (debug-namify "DEFMACRO ~S"
1095                                                               name))))
1096       (setf (functional-arg-documentation fun) (eval lambda-list))
1097
1098       (ir1-convert start cont `(%%defmacro ',name ,fun ,doc)))
1099
1100     (when sb!xc:*compile-print*
1101       ;; FIXME: It would be nice to convert this, and the other places
1102       ;; which create compiler diagnostic output prefixed by
1103       ;; semicolons, to use some common utility which automatically
1104       ;; prefixes all its output with semicolons. (The addition of
1105       ;; semicolon prefixes was introduced ca. sbcl-0.6.8.10 as the
1106       ;; "MNA compiler message patch", and implemented by modifying a
1107       ;; bunch of output statements on a case-by-case basis, which
1108       ;; seems unnecessarily error-prone and unclear, scattering
1109       ;; implicit information about output style throughout the
1110       ;; system.) Starting by rewriting COMPILER-MUMBLE to add
1111       ;; semicolon prefixes would be a good start, and perhaps also:
1112       ;;   * Add semicolon prefixes for "FOO assembled" messages emitted 
1113       ;;     when e.g. src/assembly/x86/assem-rtns.lisp is processed.
1114       ;;   * At least some debugger output messages deserve semicolon
1115       ;;     prefixes too:
1116       ;;     ** restarts table
1117       ;;     ** "Within the debugger, you can type HELP for help."
1118       (compiler-mumble "~&; converted ~S~%" name))))
1119
1120 (def-ir1-translator %define-compiler-macro ((name def lambda-list doc)
1121                                             start cont
1122                                             :kind :function)
1123   (let ((name (eval name))
1124         (def (second def))) ; We don't want to make a function just yet...
1125
1126     (when (eq (info :function :kind name) :special-form)
1127       (compiler-error "attempt to define a compiler-macro for special form ~S"
1128                       name))
1129
1130     (setf (info :function :compiler-macro-function name)
1131           (coerce def 'function))
1132
1133     (let* ((*current-path* (revert-source-path 'define-compiler-macro))
1134            (fun (ir1-convert-lambda def 
1135                                     :debug-name (debug-namify
1136                                                  "DEFINE-COMPILER-MACRO ~S"
1137                                                  name))))
1138       (setf (functional-arg-documentation fun) (eval lambda-list))
1139
1140       (ir1-convert start cont `(%%define-compiler-macro ',name ,fun ,doc)))
1141
1142     (when sb!xc:*compile-print*
1143       (compiler-mumble "~&; converted ~S~%" name))))