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