(I seem to've screwed up during the checkin of 0.pre7.131 before, so
[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   (unless (symbolp name)
360     (compiler-error "internal error: Primitive name ~S is not a symbol." name))
361   (let* ((template (or (gethash name *backend-template-names*)
362                        (compiler-error
363                         "internal error: Primitive name ~A is not defined."
364                         name)))
365          (required (length (template-arg-types template)))
366          (info (template-info-arg-count template))
367          (min (+ required info))
368          (nargs (length args)))
369     (if (template-more-args-type template)
370         (when (< nargs min)
371           (compiler-error "internal error: Primitive ~A was called ~
372                            with ~R argument~:P, ~
373                            but wants at least ~R."
374                           name
375                           nargs
376                           min))
377         (unless (= nargs min)
378           (compiler-error "internal error: Primitive ~A was called ~
379                            with ~R argument~:P, ~
380                            but wants exactly ~R."
381                           name
382                           nargs
383                           min)))
384
385     (when (eq (template-result-types template) :conditional)
386       (compiler-error
387        "%PRIMITIVE was used with a conditional template."))
388
389     (when (template-more-results-type template)
390       (compiler-error
391        "%PRIMITIVE was used with an unknown values template."))
392
393     (ir1-convert start
394                  cont
395                  `(%%primitive ',template
396                                ',(eval-info-args
397                                   (subseq args required min))
398                                ,@(subseq args 0 required)
399                                ,@(subseq args min)))))
400 \f
401 ;;;; QUOTE
402
403 (def-ir1-translator quote ((thing) start cont)
404   #!+sb-doc
405   "QUOTE Value
406   Return Value without evaluating it."
407   (reference-constant start cont thing))
408 \f
409 ;;;; FUNCTION and NAMED-LAMBDA
410
411 (def-ir1-translator function ((thing) start cont)
412   #!+sb-doc
413   "FUNCTION Name
414   Return the lexically apparent definition of the function Name. Name may also
415   be a lambda expression."
416   (if (consp thing)
417       (case (car thing)
418         ((lambda)
419          (reference-leaf start
420                          cont
421                          (ir1-convert-lambda thing
422                                              :debug-name (debug-namify
423                                                           "#'~S" thing))))
424         ((setf)
425          (let ((var (find-lexically-apparent-fun
426                      thing "as the argument to FUNCTION")))
427            (reference-leaf start cont var)))
428         ((instance-lambda)
429          (let ((res (ir1-convert-lambda `(lambda ,@(cdr thing))
430                                         :debug-name (debug-namify "#'~S"
431                                                                   thing))))
432            (setf (getf (functional-plist res) :fin-function) t)
433            (reference-leaf start cont res)))
434         (t
435          (compiler-error "~S is not a legal function name." thing)))
436       (let ((var (find-lexically-apparent-fun
437                   thing "as the argument to FUNCTION")))
438         (reference-leaf start cont var))))
439
440 ;;; `(NAMED-LAMBDA ,NAME ,@REST) is like `(FUNCTION (LAMBDA ,@REST)),
441 ;;; except that the value of NAME is passed to the compiler for use in
442 ;;; creation of debug information for the resulting function.
443 ;;;
444 ;;; Eventually we might use this for NAME values other than legal
445 ;;; function names, 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 (STARSHIP T))
453 ;;; for the function used to implement
454 ;;;   (DEFMETHOD PRINT-OBJECT ((SS STARSHIP) STREAM) ...).
455 ;;; However, as of this writing (while defining/implementing it in
456 ;;; sbcl-0.pre7.108) NAME is always a legal function name.
457 ;;;
458 ;;; If NAME is a legal function name, then the caller should be
459 ;;; planning to set (FDEFINITION NAME) to the created function.
460 ;;; (Otherwise the debug names will be inconsistent and thus
461 ;;; unnecessarily confusing.)
462 (def-ir1-translator named-lambda ((name &rest rest) start cont)
463   (reference-leaf start
464                   cont
465                   (ir1-convert-lambda `(lambda ,@rest)
466                                       :source-name name)))
467 \f
468 ;;;; FUNCALL
469
470 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
471 ;;; (not symbols). %FUNCALL is used directly in some places where the
472 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
473 (deftransform funcall ((function &rest args) * * :when :both)
474   (let ((arg-names (make-gensym-list (length args))))
475     `(lambda (function ,@arg-names)
476        (%funcall ,(if (csubtypep (continuation-type function)
477                                  (specifier-type 'function))
478                       'function
479                       '(%coerce-callable-to-fun function))
480                  ,@arg-names))))
481
482 (def-ir1-translator %funcall ((function &rest args) start cont)
483   (let ((fun-cont (make-continuation)))
484     (ir1-convert start fun-cont function)
485     (assert-continuation-type fun-cont (specifier-type 'function))
486     (ir1-convert-combination-args fun-cont cont args)))
487
488 ;;; This source transform exists to reduce the amount of work for the
489 ;;; compiler. If the called function is a FUNCTION form, then convert
490 ;;; directly to %FUNCALL, instead of waiting around for type
491 ;;; inference.
492 (define-source-transform funcall (function &rest args)
493   (if (and (consp function) (eq (car function) 'function))
494       `(%funcall ,function ,@args)
495       (values nil t)))
496
497 (deftransform %coerce-callable-to-fun ((thing) (function) *
498                                        :when :both
499                                        :important t)
500   "optimize away possible call to FDEFINITION at runtime"
501   'thing)
502 \f
503 ;;;; LET and LET*
504 ;;;;
505 ;;;; (LET and LET* can't be implemented as macros due to the fact that
506 ;;;; any pervasive declarations also affect the evaluation of the
507 ;;;; arguments.)
508
509 ;;; Given a list of binding specifiers in the style of Let, return:
510 ;;;  1. The list of var structures for the variables bound.
511 ;;;  2. The initial value form for each variable.
512 ;;;
513 ;;; The variable names are checked for legality and globally special
514 ;;; variables are marked as such. Context is the name of the form, for
515 ;;; error reporting purposes.
516 (declaim (ftype (function (list symbol) (values list list list))
517                 extract-let-vars))
518 (defun extract-let-vars (bindings context)
519   (collect ((vars)
520             (vals)
521             (names))
522     (flet ((get-var (name)
523              (varify-lambda-arg name
524                                 (if (eq context 'let*)
525                                     nil
526                                     (names)))))
527       (dolist (spec bindings)
528         (cond ((atom spec)
529                (let ((var (get-var spec)))
530                  (vars var)
531                  (names (cons spec var))
532                  (vals nil)))
533               (t
534                (unless (proper-list-of-length-p spec 1 2)
535                  (compiler-error "The ~S binding spec ~S is malformed."
536                                  context
537                                  spec))
538                (let* ((name (first spec))
539                       (var (get-var name)))
540                  (vars var)
541                  (names name)
542                  (vals (second spec)))))))
543
544     (values (vars) (vals) (names))))
545
546 (def-ir1-translator let ((bindings &body body)
547                          start cont)
548   #!+sb-doc
549   "LET ({(Var [Value]) | Var}*) Declaration* Form*
550   During evaluation of the Forms, bind the Vars to the result of evaluating the
551   Value forms. The variables are bound in parallel after all of the Values are
552   evaluated."
553   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
554     (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
555       (let* ((*lexenv* (process-decls decls vars nil cont))
556              (fun-cont (make-continuation))
557              (fun (ir1-convert-lambda-body
558                    forms vars :debug-name (debug-namify "LET ~S" bindings))))
559         (reference-leaf start fun-cont fun)
560         (ir1-convert-combination-args fun-cont cont values)))))
561
562 (def-ir1-translator let* ((bindings &body body)
563                           start cont)
564   #!+sb-doc
565   "LET* ({(Var [Value]) | Var}*) Declaration* Form*
566   Similar to LET, but the variables are bound sequentially, allowing each Value
567   form to reference any of the previous Vars."
568   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
569     (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
570       (let ((*lexenv* (process-decls decls vars nil cont)))
571         (ir1-convert-aux-bindings start cont forms vars values)))))
572
573 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
574 ;;; and SYMBOL-MACROLET
575 ;;;
576 ;;; Note that all these things need to preserve toplevel-formness,
577 ;;; but we don't need to worry about that within an IR1 translator,
578 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
579 ;;; forms before we hit the IR1 transform level.
580 (defun ir1-translate-locally (body start cont)
581   (declare (type list body) (type continuation start cont))
582   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
583     (let ((*lexenv* (process-decls decls nil nil cont)))
584       (ir1-convert-aux-bindings start cont forms nil nil))))
585
586 (def-ir1-translator locally ((&body body) start cont)
587   #!+sb-doc
588   "LOCALLY Declaration* Form*
589   Sequentially evaluate the Forms in a lexical environment where the
590   the Declarations have effect. If LOCALLY is a top level form, then
591   the Forms are also processed as top level forms."
592   (ir1-translate-locally body start cont))
593 \f
594 ;;;; FLET and LABELS
595
596 ;;; Given a list of local function specifications in the style of
597 ;;; FLET, return lists of the function names and of the lambdas which
598 ;;; are their definitions.
599 ;;;
600 ;;; The function names are checked for legality. CONTEXT is the name
601 ;;; of the form, for error reporting.
602 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
603 (defun extract-flet-vars (definitions context)
604   (collect ((names)
605             (defs))
606     (dolist (def definitions)
607       (when (or (atom def) (< (length def) 2))
608         (compiler-error "The ~S definition spec ~S is malformed." context def))
609
610       (let ((name (first def)))
611         (check-fun-name name)
612         (names name)
613         (multiple-value-bind (forms decls) (sb!sys:parse-body (cddr def))
614           (defs `(lambda ,(second def)
615                    ,@decls
616                    (block ,(fun-name-block-name name)
617                      . ,forms))))))
618     (values (names) (defs))))
619
620 (def-ir1-translator flet ((definitions &body body)
621                           start cont)
622   #!+sb-doc
623   "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
624   Evaluate the Body-Forms with some local function definitions. The bindings
625   do not enclose the definitions; any use of Name in the Forms will refer to
626   the lexically apparent function definition in the enclosing environment."
627   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
628     (multiple-value-bind (names defs)
629         (extract-flet-vars definitions 'flet)
630       (let* ((fvars (mapcar (lambda (n d)
631                               (ir1-convert-lambda d
632                                                   :source-name n
633                                                   :debug-name (debug-namify
634                                                                "FLET ~S" n)))
635                             names defs))
636              (*lexenv* (make-lexenv
637                         :default (process-decls decls nil fvars cont)
638                         :funs (pairlis names fvars))))
639         (ir1-convert-progn-body start cont forms)))))
640
641 (def-ir1-translator labels ((definitions &body body) start cont)
642   #!+sb-doc
643   "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
644   Evaluate the Body-Forms with some local function definitions. The bindings
645   enclose the new definitions, so the defined functions can call themselves or
646   each other."
647   (multiple-value-bind (forms decls) (sb!sys:parse-body body nil)
648     (multiple-value-bind (names defs)
649         (extract-flet-vars definitions 'labels)
650       (let* (;; dummy LABELS functions, to be used as placeholders
651              ;; during construction of real LABELS functions
652              (placeholder-funs (mapcar (lambda (name)
653                                          (make-functional
654                                           :%source-name name
655                                           :%debug-name (debug-namify
656                                                         "LABELS placeholder ~S"
657                                                         name)))
658                                        names))
659              ;; (like PAIRLIS but guaranteed to preserve ordering:)
660              (placeholder-fenv (mapcar #'cons names placeholder-funs))
661              ;; the real LABELS functions, compiled in a LEXENV which
662              ;; includes the dummy LABELS functions
663              (real-funs
664               (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
665                 (mapcar (lambda (name def)
666                           (ir1-convert-lambda def
667                                               :source-name name
668                                               :debug-name (debug-namify
669                                                            "LABELS ~S" name)))
670                         names defs))))
671
672         ;; Modify all the references to the dummy function leaves so
673         ;; that they point to the real function leaves.
674         (loop for real-fun in real-funs and
675               placeholder-cons in placeholder-fenv do
676               (substitute-leaf real-fun (cdr placeholder-cons))
677               (setf (cdr placeholder-cons) real-fun))
678
679         ;; Voila.
680         (let ((*lexenv* (make-lexenv
681                          :default (process-decls decls nil real-funs cont)
682                          ;; Use a proper FENV here (not the
683                          ;; placeholder used earlier) so that if the
684                          ;; lexical environment is used for inline
685                          ;; expansion we'll get the right functions.
686                          :funs (pairlis names real-funs))))
687           (ir1-convert-progn-body start cont forms))))))
688 \f
689 ;;;; the THE special operator, and friends
690
691 ;;; Do stuff to recognize a THE or VALUES declaration. CONT is the
692 ;;; continuation that the assertion applies to, TYPE is the type
693 ;;; specifier and LEXENV is the current lexical environment. NAME is
694 ;;; the name of the declaration we are doing, for use in error
695 ;;; messages.
696 ;;;
697 ;;; This is somewhat involved, since a type assertion may only be made
698 ;;; on a continuation, not on a node. We can't just set the
699 ;;; continuation asserted type and let it go at that, since there may
700 ;;; be parallel THE's for the same continuation, i.e.
701 ;;;     (if ...
702 ;;;      (the foo ...)
703 ;;;      (the bar ...))
704 ;;;
705 ;;; In this case, our representation can do no better than the union
706 ;;; of these assertions. And if there is a branch with no assertion,
707 ;;; we have nothing at all. We really need to recognize scoping, since
708 ;;; we need to be able to discern between parallel assertions (which
709 ;;; we union) and nested ones (which we intersect).
710 ;;;
711 ;;; We represent the scoping by throwing our innermost (intersected)
712 ;;; assertion on CONT into the TYPE-RESTRICTIONS. As we go down, we
713 ;;; intersect our assertions together. If CONT has no uses yet, we
714 ;;; have not yet bottomed out on the first COND branch; in this case
715 ;;; we optimistically assume that this type will be the one we end up
716 ;;; with, and set the ASSERTED-TYPE to it. We can never get better
717 ;;; than the type that we have the first time we bottom out. Later
718 ;;; THE's (or the absence thereof) can only weaken this result.
719 ;;;
720 ;;; We make this work by getting USE-CONTINUATION to do the unioning
721 ;;; across COND branches. We can't do it here, since we don't know how
722 ;;; many branches there are going to be.
723 (defun do-the-stuff (type cont lexenv name)
724   (declare (type continuation cont) (type lexenv lexenv))
725   (let* ((ctype (values-specifier-type type))
726          (old-type (or (lexenv-find cont type-restrictions)
727                        *wild-type*))
728          (intersects (values-types-equal-or-intersect old-type ctype))
729          (int (values-type-intersection old-type ctype))
730          (new (if intersects int old-type)))
731     (when (null (find-uses cont))
732       (setf (continuation-asserted-type cont) new))
733     (when (and (not intersects)
734                (not (policy *lexenv*
735                             (= inhibit-warnings 3)))) ;FIXME: really OK to suppress?
736       (compiler-warn
737        "The type ~S in ~S declaration conflicts with an ~
738         enclosing assertion:~%   ~S"
739        (type-specifier ctype)
740        name
741        (type-specifier old-type)))
742     (make-lexenv :type-restrictions `((,cont . ,new))
743                  :default lexenv)))
744
745 ;;; Assert that FORM evaluates to the specified type (which may be a
746 ;;; VALUES type).
747 ;;;
748 ;;; FIXME: In a version of CMU CL that I used at Cadabra ca. 20000101,
749 ;;; this didn't seem to expand into an assertion, at least for ALIEN
750 ;;; values. Check that SBCL doesn't have this problem.
751 (def-ir1-translator the ((type value) start cont)
752   (let ((*lexenv* (do-the-stuff type cont *lexenv* 'the)))
753     (ir1-convert start cont value)))
754
755 ;;; This is like the THE special form, except that it believes
756 ;;; whatever you tell it. It will never generate a type check, but
757 ;;; will cause a warning if the compiler can prove the assertion is
758 ;;; wrong.
759 ;;;
760 ;;; Since the CONTINUATION-DERIVED-TYPE is computed as the union of
761 ;;; its uses's types, setting it won't work. Instead we must intersect
762 ;;; the type with the uses's DERIVED-TYPE.
763 (def-ir1-translator truly-the ((type value) start cont)
764   #!+sb-doc
765   (declare (inline member))
766   (let ((type (values-specifier-type type))
767         (old (find-uses cont)))
768     (ir1-convert start cont value)
769     (do-uses (use cont)
770       (unless (member use old :test #'eq)
771         (derive-node-type use type)))))
772 \f
773 ;;;; SETQ
774
775 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
776 ;;; look at the global information. If the name is for a constant,
777 ;;; then error out.
778 (def-ir1-translator setq ((&whole source &rest things) start cont)
779   (let ((len (length things)))
780     (when (oddp len)
781       (compiler-error "odd number of args to SETQ: ~S" source))
782     (if (= len 2)
783         (let* ((name (first things))
784                (leaf (or (lexenv-find name vars)
785                          (find-free-var name))))
786           (etypecase leaf
787             (leaf
788              (when (constant-p leaf)
789                (compiler-error "~S is a constant and thus can't be set." name))
790              (when (lambda-var-p leaf)
791                (let ((home-lambda (continuation-home-lambda-or-null start)))
792                  (when home-lambda
793                    (pushnew leaf (lambda-calls-or-closes home-lambda))))
794                (when (lambda-var-ignorep leaf)
795                  ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
796                  ;; requires that this be a STYLE-WARNING, not a full warning.
797                  (compiler-style-warn
798                   "~S is being set even though it was declared to be ignored."
799                   name)))
800              (setq-var start cont leaf (second things)))
801             (cons
802              (aver (eq (car leaf) 'MACRO))
803              (ir1-convert start cont `(setf ,(cdr leaf) ,(second things))))
804             (heap-alien-info
805              (ir1-convert start cont
806                           `(%set-heap-alien ',leaf ,(second things))))))
807         (collect ((sets))
808           (do ((thing things (cddr thing)))
809               ((endp thing)
810                (ir1-convert-progn-body start cont (sets)))
811             (sets `(setq ,(first thing) ,(second thing))))))))
812
813 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
814 ;;; This should only need to be called in SETQ.
815 (defun setq-var (start cont var value)
816   (declare (type continuation start cont) (type basic-var var))
817   (let ((dest (make-continuation)))
818     (setf (continuation-asserted-type dest) (leaf-type var))
819     (ir1-convert start dest value)
820     (let ((res (make-set :var var :value dest)))
821       (setf (continuation-dest dest) res)
822       (setf (leaf-ever-used var) t)
823       (push res (basic-var-sets var))
824       (link-node-to-previous-continuation res dest)
825       (use-continuation res cont))))
826 \f
827 ;;;; CATCH, THROW and UNWIND-PROTECT
828
829 ;;; We turn THROW into a multiple-value-call of a magical function,
830 ;;; since as as far as IR1 is concerned, it has no interesting
831 ;;; properties other than receiving multiple-values.
832 (def-ir1-translator throw ((tag result) start cont)
833   #!+sb-doc
834   "Throw Tag Form
835   Do a non-local exit, return the values of Form from the CATCH whose tag
836   evaluates to the same thing as Tag."
837   (ir1-convert start cont
838                `(multiple-value-call #'%throw ,tag ,result)))
839
840 ;;; This is a special special form used to instantiate a cleanup as
841 ;;; the current cleanup within the body. KIND is the kind of cleanup
842 ;;; to make, and MESS-UP is a form that does the mess-up action. We
843 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
844 ;;; and introduce the cleanup into the lexical environment. We
845 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
846 ;;; cleanup, since this inner cleanup is the interesting one.
847 (def-ir1-translator %within-cleanup ((kind mess-up &body body) start cont)
848   (let ((dummy (make-continuation))
849         (dummy2 (make-continuation)))
850     (ir1-convert start dummy mess-up)
851     (let* ((mess-node (continuation-use dummy))
852            (cleanup (make-cleanup :kind kind
853                                   :mess-up mess-node))
854            (old-cup (lexenv-cleanup *lexenv*))
855            (*lexenv* (make-lexenv :cleanup cleanup)))
856       (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
857       (ir1-convert dummy dummy2 '(%cleanup-point))
858       (ir1-convert-progn-body dummy2 cont body))))
859
860 ;;; This is a special special form that makes an "escape function"
861 ;;; which returns unknown values from named block. We convert the
862 ;;; function, set its kind to :ESCAPE, and then reference it. The
863 ;;; :ESCAPE kind indicates that this function's purpose is to
864 ;;; represent a non-local control transfer, and that it might not
865 ;;; actually have to be compiled.
866 ;;;
867 ;;; Note that environment analysis replaces references to escape
868 ;;; functions with references to the corresponding NLX-INFO structure.
869 (def-ir1-translator %escape-fun ((tag) start cont)
870   (let ((fun (ir1-convert-lambda
871               `(lambda ()
872                  (return-from ,tag (%unknown-values)))
873               :debug-name (debug-namify "escape function for ~S" tag))))
874     (setf (functional-kind fun) :escape)
875     (reference-leaf start cont fun)))
876
877 ;;; Yet another special special form. This one looks up a local
878 ;;; function and smashes it to a :CLEANUP function, as well as
879 ;;; referencing it.
880 (def-ir1-translator %cleanup-fun ((name) start cont)
881   (let ((fun (lexenv-find name funs)))
882     (aver (lambda-p fun))
883     (setf (functional-kind fun) :cleanup)
884     (reference-leaf start cont fun)))
885
886 ;;; We represent the possibility of the control transfer by making an
887 ;;; "escape function" that does a lexical exit, and instantiate the
888 ;;; cleanup using %WITHIN-CLEANUP.
889 (def-ir1-translator catch ((tag &body body) start cont)
890   #!+sb-doc
891   "Catch Tag Form*
892   Evaluates Tag and instantiates it as a catcher while the body forms are
893   evaluated in an implicit PROGN. If a THROW is done to Tag within the dynamic
894   scope of the body, then control will be transferred to the end of the body
895   and the thrown values will be returned."
896   (ir1-convert
897    start cont
898    (let ((exit-block (gensym "EXIT-BLOCK-")))
899      `(block ,exit-block
900         (%within-cleanup
901             :catch
902             (%catch (%escape-fun ,exit-block) ,tag)
903           ,@body)))))
904
905 ;;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
906 ;;; cleanup forms into a local function so that they can be referenced
907 ;;; both in the case where we are unwound and in any local exits. We
908 ;;; use %CLEANUP-FUN on this to indicate that reference by
909 ;;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
910 ;;; an XEP.
911 (def-ir1-translator unwind-protect ((protected &body cleanup) start cont)
912   #!+sb-doc
913   "Unwind-Protect Protected Cleanup*
914   Evaluate the form Protected, returning its values. The cleanup forms are
915   evaluated whenever the dynamic scope of the Protected form is exited (either
916   due to normal completion or a non-local exit such as THROW)."
917   (ir1-convert
918    start cont
919    (let ((cleanup-fun (gensym "CLEANUP-FUN-"))
920          (drop-thru-tag (gensym "DROP-THRU-TAG-"))
921          (exit-tag (gensym "EXIT-TAG-"))
922          (next (gensym "NEXT"))
923          (start (gensym "START"))
924          (count (gensym "COUNT")))
925      `(flet ((,cleanup-fun () ,@cleanup nil))
926         ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
927         ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
928         ;; and something can be done to make %ESCAPE-FUN have
929         ;; dynamic extent too.
930         (block ,drop-thru-tag
931           (multiple-value-bind (,next ,start ,count)
932               (block ,exit-tag
933                 (%within-cleanup
934                     :unwind-protect
935                     (%unwind-protect (%escape-fun ,exit-tag)
936                                      (%cleanup-fun ,cleanup-fun))
937                   (return-from ,drop-thru-tag ,protected)))
938             (,cleanup-fun)
939             (%continue-unwind ,next ,start ,count)))))))
940 \f
941 ;;;; multiple-value stuff
942
943 ;;; If there are arguments, MULTIPLE-VALUE-CALL turns into an
944 ;;; MV-COMBINATION.
945 ;;;
946 ;;; If there are no arguments, then we convert to a normal
947 ;;; combination, ensuring that a MV-COMBINATION always has at least
948 ;;; one argument. This can be regarded as an optimization, but it is
949 ;;; more important for simplifying compilation of MV-COMBINATIONS.
950 (def-ir1-translator multiple-value-call ((fun &rest args) start cont)
951   #!+sb-doc
952   "MULTIPLE-VALUE-CALL Function Values-Form*
953   Call Function, passing all the values of each Values-Form as arguments,
954   values from the first Values-Form making up the first argument, etc."
955   (let* ((fun-cont (make-continuation))
956          (node (if args
957                    (make-mv-combination fun-cont)
958                    (make-combination fun-cont))))
959     (ir1-convert start fun-cont
960                  (if (and (consp fun) (eq (car fun) 'function))
961                      fun
962                      `(%coerce-callable-to-fun ,fun)))
963     (setf (continuation-dest fun-cont) node)
964     (assert-continuation-type fun-cont
965                               (specifier-type '(or function symbol)))
966     (collect ((arg-conts))
967       (let ((this-start fun-cont))
968         (dolist (arg args)
969           (let ((this-cont (make-continuation node)))
970             (ir1-convert this-start this-cont arg)
971             (setq this-start this-cont)
972             (arg-conts this-cont)))
973         (link-node-to-previous-continuation node this-start)
974         (use-continuation node cont)
975         (setf (basic-combination-args node) (arg-conts))))))
976
977 ;;; MULTIPLE-VALUE-PROG1 is represented implicitly in IR1 by having a
978 ;;; the result code use result continuation (CONT), but transfer
979 ;;; control to the evaluation of the body. In other words, the result
980 ;;; continuation isn't IMMEDIATELY-USED-P by the nodes that compute
981 ;;; the result.
982 ;;;
983 ;;; In order to get the control flow right, we convert the result with
984 ;;; a dummy result continuation, then convert all the uses of the
985 ;;; dummy to be uses of CONT. If a use is an EXIT, then we also
986 ;;; substitute CONT for the dummy in the corresponding ENTRY node so
987 ;;; that they are consistent. Note that this doesn't amount to
988 ;;; changing the exit target, since the control destination of an exit
989 ;;; is determined by the block successor; we are just indicating the
990 ;;; continuation that the result is delivered to.
991 ;;;
992 ;;; We then convert the body, using another dummy continuation in its
993 ;;; own block as the result. After we are done converting the body, we
994 ;;; move all predecessors of the dummy end block to CONT's block.
995 ;;;
996 ;;; Note that we both exploit and maintain the invariant that the CONT
997 ;;; to an IR1 convert method either has no block or starts the block
998 ;;; that control should transfer to after completion for the form.
999 ;;; Nested MV-PROG1's work because during conversion of the result
1000 ;;; form, we use dummy continuation whose block is the true control
1001 ;;; destination.
1002 (def-ir1-translator multiple-value-prog1 ((result &rest forms) start cont)
1003   #!+sb-doc
1004   "MULTIPLE-VALUE-PROG1 Values-Form Form*
1005   Evaluate Values-Form and then the Forms, but return all the values of
1006   Values-Form."
1007   (continuation-starts-block cont)
1008   (let* ((dummy-result (make-continuation))
1009          (dummy-start (make-continuation))
1010          (cont-block (continuation-block cont)))
1011     (continuation-starts-block dummy-start)
1012     (ir1-convert start dummy-start result)
1013
1014     (substitute-continuation-uses cont dummy-start)
1015
1016     (continuation-starts-block dummy-result)
1017     (ir1-convert-progn-body dummy-start dummy-result forms)
1018     (let ((end-block (continuation-block dummy-result)))
1019       (dolist (pred (block-pred end-block))
1020         (unlink-blocks pred end-block)
1021         (link-blocks pred cont-block))
1022       (aver (not (continuation-dest dummy-result)))
1023       (delete-continuation dummy-result)
1024       (remove-from-dfo end-block))))
1025 \f
1026 ;;;; interface to defining macros
1027
1028 ;;;; FIXME:
1029 ;;;;   classic CMU CL comment:
1030 ;;;;     DEFMACRO and DEFUN expand into calls to %DEFxxx functions
1031 ;;;;     so that we get a chance to see what is going on. We define
1032 ;;;;     IR1 translators for these functions which look at the
1033 ;;;;     definition and then generate a call to the %%DEFxxx function.
1034 ;;;; Alas, this implementation doesn't do the right thing for
1035 ;;;; non-toplevel uses of these forms, so this should probably
1036 ;;;; be changed to use EVAL-WHEN instead.
1037
1038 ;;; Return a new source path with any stuff intervening between the
1039 ;;; current path and the first form beginning with NAME stripped off.
1040 ;;; This is used to hide the guts of DEFmumble macros to prevent
1041 ;;; annoying error messages.
1042 (defun revert-source-path (name)
1043   (do ((path *current-path* (cdr path)))
1044       ((null path) *current-path*)
1045     (let ((first (first path)))
1046       (when (or (eq first name)
1047                 (eq first 'original-source-start))
1048         (return path)))))
1049
1050 ;;; Warn about incompatible or illegal definitions and add the macro
1051 ;;; to the compiler environment.
1052 ;;;
1053 ;;; Someday we could check for macro arguments being incompatibly
1054 ;;; redefined. Doing this right will involve finding the old macro
1055 ;;; lambda-list and comparing it with the new one.
1056 (def-ir1-translator %defmacro ((qname qdef lambda-list doc) start cont
1057                                :kind :function)
1058   (let (;; QNAME is typically a quoted name. I think the idea is to
1059         ;; let %DEFMACRO work as an ordinary function when
1060         ;; interpreting. Whatever the reason the quote is there, we
1061         ;; don't want it any more. -- WHN 19990603
1062         (name (eval qname))
1063         ;; QDEF should be a sharp-quoted definition. We don't want to
1064         ;; make a function of it just yet, so we just drop the
1065         ;; sharp-quote.
1066         (def (progn
1067                (aver (eq 'function (first qdef)))
1068                (aver (proper-list-of-length-p qdef 2))
1069                (second qdef))))
1070
1071     (/show "doing IR1 translator for %DEFMACRO" name)
1072
1073     (unless (symbolp name)
1074       (compiler-error "The macro name ~S is not a symbol." name))
1075
1076     (ecase (info :function :kind name)
1077       ((nil))
1078       (:function
1079        (remhash name *free-funs*)
1080        (undefine-fun-name name)
1081        (compiler-warn
1082         "~S is being redefined as a macro when it was ~
1083          previously ~(~A~) to be a function."
1084         name
1085         (info :function :where-from name)))
1086       (:macro)
1087       (:special-form
1088        (compiler-error "The special form ~S can't be redefined as a macro."
1089                        name)))
1090
1091     (setf (info :function :kind name) :macro
1092           (info :function :where-from name) :defined
1093           (info :function :macro-function name) (coerce def 'function))
1094
1095     (let* ((*current-path* (revert-source-path 'defmacro))
1096            (fun (ir1-convert-lambda def 
1097                                     :debug-name (debug-namify "DEFMACRO ~S"
1098                                                               name))))
1099       (setf (functional-arg-documentation fun) (eval lambda-list))
1100
1101       (ir1-convert start cont `(%%defmacro ',name ,fun ,doc)))
1102
1103     (when sb!xc:*compile-print*
1104       ;; FIXME: It would be nice to convert this, and the other places
1105       ;; which create compiler diagnostic output prefixed by
1106       ;; semicolons, to use some common utility which automatically
1107       ;; prefixes all its output with semicolons. (The addition of
1108       ;; semicolon prefixes was introduced ca. sbcl-0.6.8.10 as the
1109       ;; "MNA compiler message patch", and implemented by modifying a
1110       ;; bunch of output statements on a case-by-case basis, which
1111       ;; seems unnecessarily error-prone and unclear, scattering
1112       ;; implicit information about output style throughout the
1113       ;; system.) Starting by rewriting COMPILER-MUMBLE to add
1114       ;; semicolon prefixes would be a good start, and perhaps also:
1115       ;;   * Add semicolon prefixes for "FOO assembled" messages emitted 
1116       ;;     when e.g. src/assembly/x86/assem-rtns.lisp is processed.
1117       ;;   * At least some debugger output messages deserve semicolon
1118       ;;     prefixes too:
1119       ;;     ** restarts table
1120       ;;     ** "Within the debugger, you can type HELP for help."
1121       (compiler-mumble "~&; converted ~S~%" name))))
1122
1123 (def-ir1-translator %define-compiler-macro ((name def lambda-list doc)
1124                                             start cont
1125                                             :kind :function)
1126   (let ((name (eval name))
1127         (def (second def))) ; We don't want to make a function just yet...
1128
1129     (when (eq (info :function :kind name) :special-form)
1130       (compiler-error "attempt to define a compiler-macro for special form ~S"
1131                       name))
1132
1133     (setf (info :function :compiler-macro-function name)
1134           (coerce def 'function))
1135
1136     (let* ((*current-path* (revert-source-path 'define-compiler-macro))
1137            (fun (ir1-convert-lambda def 
1138                                     :debug-name (debug-namify
1139                                                  "DEFINE-COMPILER-MACRO ~S"
1140                                                  name))))
1141       (setf (functional-arg-documentation fun) (eval lambda-list))
1142
1143       (ir1-convert start cont `(%%define-compiler-macro ',name ,fun ,doc)))
1144
1145     (when sb!xc:*compile-print*
1146       (compiler-mumble "~&; converted ~S~%" name))))