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