1 ;;;; the LOOP iteration macro
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This code was modified by William Harold Newman beginning
7 ;;;; 19981106, originally to conform to the new SBCL bootstrap package
8 ;;;; system and then subsequently to address other cross-compiling
9 ;;;; bootstrap issues, SBCLification (e.g. DECLARE used to check
10 ;;;; argument types), and other maintenance. Whether or not it then
11 ;;;; supported all the environments implied by the reader conditionals
12 ;;;; in the source code (e.g. #!+CLOE-RUNTIME) before that
13 ;;;; modification, it sure doesn't now. It might perhaps, by blind
14 ;;;; luck, be appropriate for some other CMU-CL-derived system, but
15 ;;;; really it only attempts to be appropriate for SBCL.
17 ;;;; This software is derived from software originally released by the
18 ;;;; Massachusetts Institute of Technology and Symbolics, Inc. Copyright and
19 ;;;; release statements follow. Later modifications to the software are in
20 ;;;; the public domain and are provided with absolutely no warranty. See the
21 ;;;; COPYING and CREDITS files for more information.
23 ;;;; Portions of LOOP are Copyright (c) 1986 by the Massachusetts Institute
24 ;;;; of Technology. All Rights Reserved.
26 ;;;; Permission to use, copy, modify and distribute this software and its
27 ;;;; documentation for any purpose and without fee is hereby granted,
28 ;;;; provided that the M.I.T. copyright notice appear in all copies and that
29 ;;;; both that copyright notice and this permission notice appear in
30 ;;;; supporting documentation. The names "M.I.T." and "Massachusetts
31 ;;;; Institute of Technology" may not be used in advertising or publicity
32 ;;;; pertaining to distribution of the software without specific, written
33 ;;;; prior permission. Notice must be given in supporting documentation that
34 ;;;; copying distribution is by permission of M.I.T. M.I.T. makes no
35 ;;;; representations about the suitability of this software for any purpose.
36 ;;;; It is provided "as is" without express or implied warranty.
38 ;;;; Massachusetts Institute of Technology
39 ;;;; 77 Massachusetts Avenue
40 ;;;; Cambridge, Massachusetts 02139
41 ;;;; United States of America
44 ;;;; Portions of LOOP are Copyright (c) 1989, 1990, 1991, 1992 by Symbolics,
45 ;;;; Inc. All Rights Reserved.
47 ;;;; Permission to use, copy, modify and distribute this software and its
48 ;;;; documentation for any purpose and without fee is hereby granted,
49 ;;;; provided that the Symbolics copyright notice appear in all copies and
50 ;;;; that both that copyright notice and this permission notice appear in
51 ;;;; supporting documentation. The name "Symbolics" may not be used in
52 ;;;; advertising or publicity pertaining to distribution of the software
53 ;;;; without specific, written prior permission. Notice must be given in
54 ;;;; supporting documentation that copying distribution is by permission of
55 ;;;; Symbolics. Symbolics makes no representations about the suitability of
56 ;;;; this software for any purpose. It is provided "as is" without express
57 ;;;; or implied warranty.
59 ;;;; Symbolics, CLOE Runtime, and Minima are trademarks, and CLOE, Genera,
60 ;;;; and Zetalisp are registered trademarks of Symbolics, Inc.
63 ;;;; 8 New England Executive Park, East
64 ;;;; Burlington, Massachusetts 01803
65 ;;;; United States of America
68 (in-package "SB!LOOP")
70 ;;;; The design of this LOOP is intended to permit, using mostly the same
71 ;;;; kernel of code, up to three different "loop" macros:
73 ;;;; (1) The unextended, unextensible ANSI standard LOOP;
75 ;;;; (2) A clean "superset" extension of the ANSI LOOP which provides
76 ;;;; functionality similar to that of the old LOOP, but "in the style of"
77 ;;;; the ANSI LOOP. For instance, user-definable iteration paths, with a
78 ;;;; somewhat cleaned-up interface.
80 ;;;; (3) Extensions provided in another file which can make this LOOP
81 ;;;; kernel behave largely compatibly with the Genera-vintage LOOP macro,
82 ;;;; with only a small addition of code (instead of two whole, separate,
85 ;;;; Each of the above three LOOP variations can coexist in the same LISP
88 ;;;; KLUDGE: In SBCL, we only really use variant (1), and any generality
89 ;;;; for the other variants is wasted. -- WHN 20000121
91 ;;;; list collection macrology
93 (sb!int:defmacro-mundanely with-loop-list-collection-head
94 ((head-var tail-var &optional user-head-var) &body body)
95 (let ((l (and user-head-var (list (list user-head-var nil)))))
96 `(let* ((,head-var (list nil)) (,tail-var ,head-var) ,@l)
99 (sb!int:defmacro-mundanely loop-collect-rplacd
100 (&environment env (head-var tail-var &optional user-head-var) form)
101 (setq form (sb!int:%macroexpand form env))
102 (flet ((cdr-wrap (form n)
104 (do () ((<= n 4) (setq form `(,(case n
110 (setq form `(cddddr ,form) n (- n 4)))))
111 (let ((tail-form form) (ncdrs nil))
112 ;; Determine whether the form being constructed is a list of known
115 (cond ((eq (car form) 'list)
116 (setq ncdrs (1- (length (cdr form)))))
117 ((member (car form) '(list* cons))
118 (when (and (cddr form) (member (car (last form)) '(nil 'nil)))
119 (setq ncdrs (- (length (cdr form)) 2))))))
122 `(when (setf (cdr ,tail-var) ,tail-form)
123 (setq ,tail-var (last (cdr ,tail-var)))))
124 ((< ncdrs 0) (return-from loop-collect-rplacd nil))
126 ;; @@@@ Here we have a choice of two idioms:
127 ;; (RPLACD TAIL (SETQ TAIL TAIL-FORM))
128 ;; (SETQ TAIL (SETF (CDR TAIL) TAIL-FORM)).
129 ;; Genera and most others I have seen do better with the
131 `(rplacd ,tail-var (setq ,tail-var ,tail-form)))
132 (t `(setq ,tail-var ,(cdr-wrap `(setf (cdr ,tail-var)
135 ;; If not using locatives or something similar to update the
136 ;; user's head variable, we've got to set it... It's harmless
137 ;; to repeatedly set it unconditionally, and probably faster
142 (setq ,user-head-var (cdr ,head-var)))))
145 (sb!int:defmacro-mundanely loop-collect-answer (head-var
146 &optional user-head-var)
150 ;;;; maximization technology
153 The basic idea of all this minimax randomness here is that we have to
154 have constructed all uses of maximize and minimize to a particular
155 "destination" before we can decide how to code them. The goal is to not
156 have to have any kinds of flags, by knowing both that (1) the type is
157 something which we can provide an initial minimum or maximum value for
158 and (2) know that a MAXIMIZE and MINIMIZE are not being combined.
160 SO, we have a datastructure which we annotate with all sorts of things,
161 incrementally updating it as we generate loop body code, and then use
162 a wrapper and internal macros to do the coding when the loop has been
166 (defstruct (loop-minimax
167 (:constructor make-loop-minimax-internal)
177 (defvar *loop-minimax-type-infinities-alist*
178 ;; FIXME: Now that SBCL supports floating point infinities again, we
179 ;; should have floating point infinities here, as cmucl-2.4.8 did.
180 '((fixnum most-positive-fixnum most-negative-fixnum)))
182 (defun make-loop-minimax (answer-variable type)
183 (let ((infinity-data (cdr (assoc type
184 *loop-minimax-type-infinities-alist*
185 :test #'sb!xc:subtypep))))
186 (make-loop-minimax-internal
187 :answer-variable answer-variable
189 :temp-variable (gensym "LOOP-MAXMIN-TEMP-")
190 :flag-variable (and (not infinity-data)
191 (gensym "LOOP-MAXMIN-FLAG-"))
193 :infinity-data infinity-data)))
195 (defun loop-note-minimax-operation (operation minimax)
196 (pushnew (the symbol operation) (loop-minimax-operations minimax))
197 (when (and (cdr (loop-minimax-operations minimax))
198 (not (loop-minimax-flag-variable minimax)))
199 (setf (loop-minimax-flag-variable minimax)
200 (gensym "LOOP-MAXMIN-FLAG-")))
203 (sb!int:defmacro-mundanely with-minimax-value (lm &body body)
204 (let ((init (loop-typed-init (loop-minimax-type lm)))
205 (which (car (loop-minimax-operations lm)))
206 (infinity-data (loop-minimax-infinity-data lm))
207 (answer-var (loop-minimax-answer-variable lm))
208 (temp-var (loop-minimax-temp-variable lm))
209 (flag-var (loop-minimax-flag-variable lm))
210 (type (loop-minimax-type lm)))
212 `(let ((,answer-var ,init) (,temp-var ,init) (,flag-var nil))
213 (declare (type ,type ,answer-var ,temp-var))
215 `(let ((,answer-var ,(if (eq which 'min)
216 (first infinity-data)
217 (second infinity-data)))
219 (declare (type ,type ,answer-var ,temp-var))
222 (sb!int:defmacro-mundanely loop-accumulate-minimax-value (lm operation form)
223 (let* ((answer-var (loop-minimax-answer-variable lm))
224 (temp-var (loop-minimax-temp-variable lm))
225 (flag-var (loop-minimax-flag-variable lm))
226 (test `(,(ecase operation
229 ,temp-var ,answer-var)))
231 (setq ,temp-var ,form)
232 (when ,(if flag-var `(or (not ,flag-var) ,test) test)
233 (setq ,@(and flag-var `(,flag-var t))
234 ,answer-var ,temp-var)))))
236 ;;;; LOOP keyword tables
239 LOOP keyword tables are hash tables string keys and a test of EQUAL.
241 The actual descriptive/dispatch structure used by LOOP is called a "loop
242 universe" contains a few tables and parameterizations. The basic idea is
243 that we can provide a non-extensible ANSI-compatible loop environment,
244 an extensible ANSI-superset loop environment, and (for such environments
245 as CLOE) one which is "sufficiently close" to the old Genera-vintage
246 LOOP for use by old user programs without requiring all of the old LOOP
252 ;;; Compare two "tokens". The first is the frob out of *LOOP-SOURCE-CODE*,
253 ;;; the second a symbol to check against.
254 (defun loop-tequal (x1 x2)
255 (and (symbolp x1) (string= x1 x2)))
257 (defun loop-tassoc (kwd alist)
258 (and (symbolp kwd) (assoc kwd alist :test #'string=)))
260 (defun loop-tmember (kwd list)
261 (and (symbolp kwd) (member kwd list :test #'string=)))
263 (defun loop-lookup-keyword (loop-token table)
264 (and (symbolp loop-token)
265 (values (gethash (symbol-name loop-token) table))))
267 (sb!int:defmacro-mundanely loop-store-table-data (symbol table datum)
268 `(setf (gethash (symbol-name ,symbol) ,table) ,datum))
270 (defstruct (loop-universe
273 keywords ; hash table, value = (fn-name . extra-data)
274 iteration-keywords ; hash table, value = (fn-name . extra-data)
275 for-keywords ; hash table, value = (fn-name . extra-data)
276 path-keywords ; hash table, value = (fn-name . extra-data)
277 type-symbols ; hash table of type SYMBOLS, test EQ,
278 ; value = CL type specifier
279 type-keywords) ; hash table of type STRINGS, test EQUAL,
280 ; value = CL type spec
281 (sb!int:def!method print-object ((u loop-universe) stream)
282 (print-unreadable-object (u stream :type t :identity t)))
284 ;;; This is the "current" loop context in use when we are expanding a
285 ;;; loop. It gets bound on each invocation of LOOP.
286 (defvar *loop-universe*)
288 (defun make-standard-loop-universe (&key keywords for-keywords
289 iteration-keywords path-keywords
290 type-keywords type-symbols)
291 (flet ((maketable (entries)
292 (let* ((size (length entries))
293 (ht (make-hash-table :size (if (< size 10) 10 size)
296 (setf (gethash (symbol-name (car x)) ht) (cadr x)))
299 :keywords (maketable keywords)
300 :for-keywords (maketable for-keywords)
301 :iteration-keywords (maketable iteration-keywords)
302 :path-keywords (maketable path-keywords)
303 :type-keywords (maketable type-keywords)
304 :type-symbols (let* ((size (length type-symbols))
305 (ht (make-hash-table :size (if (< size 10) 10 size)
307 (dolist (x type-symbols)
309 (setf (gethash x ht) x)
310 (setf (gethash (car x) ht) (cadr x))))
313 ;;;; SETQ hackery, including destructuring ("DESETQ")
315 (defun loop-make-psetq (frobs)
319 (if (null (cddr frobs)) (cadr frobs)
320 `(prog1 ,(cadr frobs)
321 ,(loop-make-psetq (cddr frobs))))))))
323 (defun loop-make-desetq (var-val-pairs)
324 (if (null var-val-pairs)
326 (cons 'loop-really-desetq var-val-pairs)))
328 (defvar *loop-desetq-temporary*
329 (make-symbol "LOOP-DESETQ-TEMP"))
331 (sb!int:defmacro-mundanely loop-really-desetq (&environment env
333 (labels ((find-non-null (var)
334 ;; See whether there's any non-null thing here. Recurse
335 ;; if the list element is itself a list.
336 (do ((tail var)) ((not (consp tail)) tail)
337 (when (find-non-null (pop tail)) (return t))))
338 (loop-desetq-internal (var val &optional temp)
339 ;; returns a list of actions to be performed
343 ;; Don't lose possible side effects.
344 (if (eq (car val) 'prog1)
345 ;; These can come from PSETQ or DESETQ below.
346 ;; Throw away the value, keep the side effects.
347 ;; Special case is for handling an expanded POP.
350 (or (not (eq (car x) 'car))
351 (not (symbolp (cadr x)))
352 (not (symbolp (setq x (sb!int:%macroexpand x env)))))
357 (let* ((car (car var))
359 (car-non-null (find-non-null car))
360 (cdr-non-null (find-non-null cdr)))
361 (when (or car-non-null cdr-non-null)
364 (temp (or temp *loop-desetq-temporary*))
365 (body `(,@(loop-desetq-internal car
367 (setq ,temp (cdr ,temp))
368 ,@(loop-desetq-internal cdr
372 `(,@(unless (eq temp val)
373 `((setq ,temp ,val)))
375 `((let ((,temp ,val))
378 (loop-desetq-internal car `(car ,val) temp)))))
381 `((setq ,var ,val)))))))
383 ((null var-val-pairs)
384 (if (null (cdr actions)) (car actions) `(progn ,@(nreverse actions))))
385 (setq actions (revappend
386 (loop-desetq-internal (pop var-val-pairs)
390 ;;;; LOOP-local variables
392 ;;; This is the "current" pointer into the LOOP source code.
393 (defvar *loop-source-code*)
395 ;;; This is the pointer to the original, for things like NAMED that
396 ;;; insist on being in a particular position
397 (defvar *loop-original-source-code*)
399 ;;; This is *loop-source-code* as of the "last" clause. It is used
400 ;;; primarily for generating error messages (see loop-error, loop-warn).
401 (defvar *loop-source-context*)
403 ;;; list of names for the LOOP, supplied by the NAMED clause
404 (defvar *loop-names*)
406 ;;; The macroexpansion environment given to the macro.
407 (defvar *loop-macro-environment*)
409 ;;; This holds variable names specified with the USING clause.
410 ;;; See LOOP-NAMED-VAR.
411 (defvar *loop-named-vars*)
413 ;;; LETlist-like list being accumulated for current group of bindings.
416 ;;; List of declarations being accumulated in parallel with
418 (defvar *loop-declarations*)
420 ;;; This is used by LOOP for destructuring binding, if it is doing
421 ;;; that itself. See LOOP-MAKE-VAR.
422 (defvar *loop-desetq-crocks*)
424 ;;; list of wrapping forms, innermost first, which go immediately
425 ;;; inside the current set of parallel bindings being accumulated in
426 ;;; *LOOP-VARS*. The wrappers are appended onto a body. E.g., this
427 ;;; list could conceivably have as its value
428 ;;; ((WITH-OPEN-FILE (G0001 G0002 ...))),
429 ;;; with G0002 being one of the bindings in *LOOP-VARS* (This is why
430 ;;; the wrappers go inside of the variable bindings).
431 (defvar *loop-wrappers*)
433 ;;; This accumulates lists of previous values of *LOOP-VARS* and the
434 ;;; other lists above, for each new nesting of bindings. See
436 (defvar *loop-bind-stack*)
438 ;;; list of prologue forms of the loop, accumulated in reverse order
439 (defvar *loop-prologue*)
441 (defvar *loop-before-loop*)
443 (defvar *loop-after-body*)
445 ;;; This is T if we have emitted any body code, so that iteration
446 ;;; driving clauses can be disallowed. This is not strictly the same
447 ;;; as checking *LOOP-BODY*, because we permit some clauses such as
448 ;;; RETURN to not be considered "real" body (so as to permit the user
449 ;;; to "code" an abnormal return value "in loop").
450 (defvar *loop-emitted-body*)
452 ;;; list of epilogue forms (supplied by FINALLY generally), accumulated
454 (defvar *loop-epilogue*)
456 ;;; list of epilogue forms which are supplied after the above "user"
457 ;;; epilogue. "Normal" termination return values are provide by
458 ;;; putting the return form in here. Normally this is done using
459 ;;; LOOP-EMIT-FINAL-VALUE, q.v.
460 (defvar *loop-after-epilogue*)
462 ;;; the "culprit" responsible for supplying a final value from the
463 ;;; loop. This is so LOOP-DISALLOW-AGGREGATE-BOOLEANS can moan about
464 ;;; disallowed anonymous collections.
465 (defvar *loop-final-value-culprit*)
467 ;;; If this is true, we are in some branch of a conditional. Some
468 ;;; clauses may be disallowed.
469 (defvar *loop-inside-conditional*)
471 ;;; If not NIL, this is a temporary bound around the loop for holding
472 ;;; the temporary value for "it" in things like "when (f) collect it".
473 ;;; It may be used as a supertemporary by some other things.
474 (defvar *loop-when-it-var*)
476 ;;; Sometimes we decide we need to fold together parts of the loop,
477 ;;; but some part of the generated iteration code is different for the
478 ;;; first and remaining iterations. This variable will be the
479 ;;; temporary which is the flag used in the loop to tell whether we
480 ;;; are in the first or remaining iterations.
481 (defvar *loop-never-stepped-var*)
483 ;;; list of all the value-accumulation descriptor structures in the
484 ;;; loop. See LOOP-GET-COLLECTION-INFO.
485 (defvar *loop-collection-cruft*) ; for multiple COLLECTs (etc.)
487 ;;;; code analysis stuff
489 (defun loop-constant-fold-if-possible (form &optional expected-type)
490 (let* ((constantp (sb!xc:constantp form))
491 (value (and constantp (sb!int:constant-form-value form))))
492 (when (and constantp expected-type)
493 (unless (sb!xc:typep value expected-type)
494 (loop-warn "~@<The form ~S evaluated to ~S, which was not of ~
495 the anticipated type ~S.~:@>"
496 form value expected-type)
497 (setq constantp nil value nil)))
498 (values form constantp value)))
500 ;;;; LOOP iteration optimization
502 (defvar *loop-duplicate-code* nil)
504 (defvar *loop-iteration-flag-var* (make-symbol "LOOP-NOT-FIRST-TIME"))
506 (defun loop-code-duplication-threshold (env)
507 (declare (ignore env))
508 (let (;; If we could read optimization declaration information (as
509 ;; with the DECLARATION-INFORMATION function (present in
510 ;; CLTL2, removed from ANSI standard) we could set these
511 ;; values flexibly. Without DECLARATION-INFORMATION, we have
512 ;; to set them to constants.
514 ;; except FIXME: we've lost all pretence of portability,
515 ;; considering this instead an internal implementation, so
516 ;; we're free to couple to our own representation of the
520 (+ 40 (* (- speed space) 10))))
522 (sb!int:defmacro-mundanely loop-body (&environment env
528 &aux rbefore rafter flagvar)
529 (unless (= (length before-loop) (length after-loop))
530 (error "LOOP-BODY called with non-synched before- and after-loop lists"))
531 ;;All our work is done from these copies, working backwards from the end:
532 (setq rbefore (reverse before-loop) rafter (reverse after-loop))
539 (member (car x) '(go return return-from)))
542 (pify (l) (if (null (cdr l)) (car l) `(progn ,@l)))
544 (let ((form `(tagbody
545 ,@(psimp (append prologue (nreverse rbefore)))
547 ,@(psimp (append main-body
551 ,@(psimp epilogue))))
552 (if flagvar `(let ((,flagvar nil)) ,form) form))))
553 (when (or *loop-duplicate-code* (not rbefore))
554 (return-from loop-body (makebody)))
555 ;; This outer loop iterates once for each not-first-time flag test
556 ;; generated plus once more for the forms that don't need a flag test.
557 (do ((threshold (loop-code-duplication-threshold env))) (nil)
558 (declare (fixnum threshold))
559 ;; Go backwards from the ends of before-loop and after-loop
560 ;; merging all the equivalent forms into the body.
561 (do () ((or (null rbefore) (not (equal (car rbefore) (car rafter)))))
562 (push (pop rbefore) main-body)
564 (unless rbefore (return (makebody)))
565 ;; The first forms in RBEFORE & RAFTER (which are the
566 ;; chronologically last forms in the list) differ, therefore
567 ;; they cannot be moved into the main body. If everything that
568 ;; chronologically precedes them either differs or is equal but
569 ;; is okay to duplicate, we can just put all of rbefore in the
570 ;; prologue and all of rafter after the body. Otherwise, there
571 ;; is something that is not okay to duplicate, so it and
572 ;; everything chronologically after it in rbefore and rafter
573 ;; must go into the body, with a flag test to distinguish the
574 ;; first time around the loop from later times. What
575 ;; chronologically precedes the non-duplicatable form will be
576 ;; handled the next time around the outer loop.
577 (do ((bb rbefore (cdr bb))
582 ((null bb) (return-from loop-body (makebody))) ; Did it.
583 (cond ((not (equal (car bb) (car aa))) (setq lastdiff bb count 0))
584 ((or (not (setq inc (estimate-code-size (car bb) env)))
585 (> (incf count inc) threshold))
586 ;; Ok, we have found a non-duplicatable piece of code.
587 ;; Everything chronologically after it must be in the
588 ;; central body. Everything chronologically at and
589 ;; after LASTDIFF goes into the central body under a
591 (let ((then nil) (else nil))
593 (push (pop rbefore) else)
594 (push (pop rafter) then)
595 (when (eq rbefore (cdr lastdiff)) (return)))
597 (push `(setq ,(setq flagvar *loop-iteration-flag-var*)
600 (push `(if ,flagvar ,(pify (psimp then)) ,(pify (psimp else)))
602 ;; Everything chronologically before lastdiff until the
603 ;; non-duplicatable form (CAR BB) is the same in
604 ;; RBEFORE and RAFTER, so just copy it into the body.
607 (push (pop rbefore) main-body)
608 (when (eq rbefore (cdr bb)) (return)))
611 (defun duplicatable-code-p (expr env)
613 (let ((ans (estimate-code-size expr env)))
614 (declare (fixnum ans))
615 ;; @@@@ Use (DECLARATION-INFORMATION 'OPTIMIZE ENV) here to
616 ;; get an alist of optimize quantities back to help quantify
617 ;; how much code we are willing to duplicate.
620 (defvar *special-code-sizes*
621 '((return 0) (progn 0)
622 (null 1) (not 1) (eq 1) (car 1) (cdr 1)
623 (when 1) (unless 1) (if 1)
624 (caar 2) (cadr 2) (cdar 2) (cddr 2)
625 (caaar 3) (caadr 3) (cadar 3) (caddr 3)
626 (cdaar 3) (cdadr 3) (cddar 3) (cdddr 3)
627 (caaaar 4) (caaadr 4) (caadar 4) (caaddr 4)
628 (cadaar 4) (cadadr 4) (caddar 4) (cadddr 4)
629 (cdaaar 4) (cdaadr 4) (cdadar 4) (cdaddr 4)
630 (cddaar 4) (cddadr 4) (cdddar 4) (cddddr 4)))
632 (defvar *estimate-code-size-punt*
636 labels lambda let let* locally
637 macrolet multiple-value-bind
644 (defun destructuring-size (x)
645 (do ((x x (cdr x)) (n 0 (+ (destructuring-size (car x)) n)))
646 ((atom x) (+ n (if (null x) 0 1)))))
648 (defun estimate-code-size (x env)
649 (catch 'estimate-code-size
650 (estimate-code-size-1 x env)))
652 (defun estimate-code-size-1 (x env)
653 (flet ((list-size (l)
656 (dolist (x l n) (incf n (estimate-code-size-1 x env))))))
657 ;;@@@@ ???? (declare (function list-size (list) fixnum))
658 (cond ((constantp x) 1)
659 ((symbolp x) (multiple-value-bind (new-form expanded-p)
660 (sb!int:%macroexpand-1 x env)
662 (estimate-code-size-1 new-form env)
664 ((atom x) 1) ;; ??? self-evaluating???
666 (let ((fn (car x)) (tem nil) (n 0))
667 (declare (symbol fn) (fixnum n))
668 (macrolet ((f (overhead &optional (args nil args-p))
669 `(the fixnum (+ (the fixnum ,overhead)
671 (list-size ,(if args-p
674 (cond ((setq tem (get fn 'estimate-code-size))
677 (t (funcall tem x env))))
678 ((setq tem (assoc fn *special-code-sizes*))
681 (dolist (clause (cdr x) n)
682 (incf n (list-size clause)) (incf n)))
684 (do ((l (cdr x) (cdr l))) ((null l) n)
686 (destructuring-size (car l))
687 (estimate-code-size-1 (cadr l) env)))))
688 ((member fn '(setq psetq))
689 (do ((l (cdr x) (cdr l))) ((null l) n)
690 (setq n (+ n (estimate-code-size-1 (cadr l) env) 1))))
693 (if (sb!int:legal-fun-name-p (cadr x))
695 ;; FIXME: This tag appears not to be present
697 (throw 'duplicatable-code-p nil)))
698 ((eq fn 'multiple-value-setq)
699 (f (length (second x)) (cddr x)))
700 ((eq fn 'return-from)
701 (1+ (estimate-code-size-1 (third x) env)))
702 ((or (special-operator-p fn)
703 (member fn *estimate-code-size-punt*))
704 (throw 'estimate-code-size nil))
705 (t (multiple-value-bind (new-form expanded-p)
706 (sb!int:%macroexpand-1 x env)
708 (estimate-code-size-1 new-form env)
710 (t (throw 'estimate-code-size nil)))))
714 (defun loop-context ()
715 (do ((l *loop-source-context* (cdr l)) (new nil (cons (car l) new)))
716 ((eq l (cdr *loop-source-code*)) (nreverse new))))
718 (defun loop-error (format-string &rest format-args)
719 (error 'sb!int:simple-program-error
720 :format-control "~?~%current LOOP context:~{ ~S~}."
721 :format-arguments (list format-string format-args (loop-context))))
723 (defun loop-warn (format-string &rest format-args)
724 (warn "~?~%current LOOP context:~{ ~S~}."
729 (defun loop-check-data-type (specified-type required-type
730 &optional (default-type required-type))
731 (if (null specified-type)
733 (multiple-value-bind (a b) (sb!xc:subtypep specified-type required-type)
735 (loop-warn "LOOP couldn't verify that ~S is a subtype of the required type ~S."
736 specified-type required-type))
738 (loop-error "The specified data type ~S is not a subtype of ~S."
739 specified-type required-type)))
742 (defun subst-gensyms-for-nil (tree)
743 (declare (special *ignores*))
745 ((null tree) (car (push (gensym "LOOP-IGNORED-VAR-") *ignores*)))
747 (t (cons (subst-gensyms-for-nil (car tree))
748 (subst-gensyms-for-nil (cdr tree))))))
750 (sb!int:defmacro-mundanely loop-destructuring-bind
751 (lambda-list arg-list &rest body)
752 (let ((*ignores* nil))
753 (declare (special *ignores*))
754 (let ((d-var-lambda-list (subst-gensyms-for-nil lambda-list)))
755 `(destructuring-bind ,d-var-lambda-list
757 (declare (ignore ,@*ignores*))
760 (defun loop-build-destructuring-bindings (crocks forms)
762 `((loop-destructuring-bind ,(car crocks) ,(cadr crocks)
763 ,@(loop-build-destructuring-bindings (cddr crocks) forms)))
766 (defun loop-translate (*loop-source-code*
767 *loop-macro-environment*
769 (let ((*loop-original-source-code* *loop-source-code*)
770 (*loop-source-context* nil)
772 (*loop-named-vars* nil)
773 (*loop-declarations* nil)
774 (*loop-desetq-crocks* nil)
775 (*loop-bind-stack* nil)
776 (*loop-prologue* nil)
777 (*loop-wrappers* nil)
778 (*loop-before-loop* nil)
780 (*loop-emitted-body* nil)
781 (*loop-after-body* nil)
782 (*loop-epilogue* nil)
783 (*loop-after-epilogue* nil)
784 (*loop-final-value-culprit* nil)
785 (*loop-inside-conditional* nil)
786 (*loop-when-it-var* nil)
787 (*loop-never-stepped-var* nil)
789 (*loop-collection-cruft* nil))
790 (loop-iteration-driver)
792 (let ((answer `(loop-body
793 ,(nreverse *loop-prologue*)
794 ,(nreverse *loop-before-loop*)
795 ,(nreverse *loop-body*)
796 ,(nreverse *loop-after-body*)
797 ,(nreconc *loop-epilogue*
798 (nreverse *loop-after-epilogue*)))))
799 (dolist (entry *loop-bind-stack*)
800 (let ((vars (first entry))
801 (dcls (second entry))
802 (crocks (third entry))
803 (wrappers (fourth entry)))
805 (setq answer (append w (list answer))))
806 (when (or vars dcls crocks)
807 (let ((forms (list answer)))
808 ;;(when crocks (push crocks forms))
809 (when dcls (push `(declare ,@dcls) forms))
810 (setq answer `(,(if vars 'let 'locally)
812 ,@(loop-build-destructuring-bindings crocks
815 (setq answer `(block ,(pop *loop-names*) ,answer))
816 (unless *loop-names* (return nil)))
819 (defun loop-iteration-driver ()
821 ((null *loop-source-code*))
822 (let ((keyword (car *loop-source-code*)) (tem nil))
823 (cond ((not (symbolp keyword))
824 (loop-error "~S found where LOOP keyword expected" keyword))
825 (t (setq *loop-source-context* *loop-source-code*)
828 (loop-lookup-keyword keyword
829 (loop-universe-keywords
831 ;; It's a "miscellaneous" toplevel LOOP keyword (DO,
832 ;; COLLECT, NAMED, etc.)
833 (apply (symbol-function (first tem)) (rest tem)))
835 (loop-lookup-keyword keyword
836 (loop-universe-iteration-keywords *loop-universe*)))
837 (loop-hack-iteration tem))
838 ((loop-tmember keyword '(and else))
839 ;; The alternative is to ignore it, i.e. let it go
840 ;; around to the next keyword...
841 (loop-error "secondary clause misplaced at top level in LOOP macro: ~S ~S ~S ..."
843 (car *loop-source-code*)
844 (cadr *loop-source-code*)))
845 (t (loop-error "unknown LOOP keyword: ~S" keyword))))))))
847 (defun loop-pop-source ()
848 (if *loop-source-code*
849 (pop *loop-source-code*)
850 (loop-error "LOOP source code ran out when another token was expected.")))
852 (defun loop-get-form ()
853 (if *loop-source-code*
855 (loop-error "LOOP code ran out where a form was expected.")))
857 (defun loop-get-compound-form ()
858 (let ((form (loop-get-form)))
860 (loop-error "A compound form was expected, but ~S found." form))
863 (defun loop-get-progn ()
864 (do ((forms (list (loop-get-compound-form))
865 (cons (loop-get-compound-form) forms))
866 (nextform (car *loop-source-code*)
867 (car *loop-source-code*)))
869 (if (null (cdr forms)) (car forms) (cons 'progn (nreverse forms))))))
871 (defun loop-construct-return (form)
872 `(return-from ,(car *loop-names*) ,form))
874 (defun loop-pseudo-body (form)
875 (cond ((or *loop-emitted-body* *loop-inside-conditional*)
876 (push form *loop-body*))
877 (t (push form *loop-before-loop*) (push form *loop-after-body*))))
879 (defun loop-emit-body (form)
880 (setq *loop-emitted-body* t)
881 (loop-pseudo-body form))
883 (defun loop-emit-final-value (&optional (form nil form-supplied-p))
884 (when form-supplied-p
885 (push (loop-construct-return form) *loop-after-epilogue*))
886 (setq *loop-final-value-culprit* (car *loop-source-context*)))
888 (defun loop-disallow-conditional (&optional kwd)
889 (when *loop-inside-conditional*
890 (loop-error "~:[This LOOP~;The LOOP ~:*~S~] clause is not permitted inside a conditional." kwd)))
892 (defun loop-disallow-anonymous-collectors ()
893 (when (find-if-not 'loop-collector-name *loop-collection-cruft*)
894 (loop-error "This LOOP clause is not permitted with anonymous collectors.")))
896 (defun loop-disallow-aggregate-booleans ()
897 (when (loop-tmember *loop-final-value-culprit* '(always never thereis))
898 (loop-error "This anonymous collection LOOP clause is not permitted with aggregate booleans.")))
902 (defun loop-typed-init (data-type &optional step-var-p)
903 (cond ((null data-type)
905 ((sb!xc:subtypep data-type 'number)
906 (let ((init (if step-var-p 1 0)))
907 (flet ((like (&rest types)
908 (coerce init (find-if (lambda (type)
909 (sb!xc:subtypep data-type type))
911 (cond ((sb!xc:subtypep data-type 'float)
912 (like 'single-float 'double-float
913 'short-float 'long-float 'float))
914 ((sb!xc:subtypep data-type '(complex float))
915 (like '(complex single-float)
916 '(complex double-float)
917 '(complex short-float)
918 '(complex long-float)
922 ((sb!xc:subtypep data-type 'vector)
923 (let ((ctype (sb!kernel:specifier-type data-type)))
924 (when (sb!kernel:array-type-p ctype)
925 (let ((etype (sb!kernel:type-*-to-t
926 (sb!kernel:array-type-specialized-element-type ctype))))
927 (make-array 0 :element-type (sb!kernel:type-specifier etype))))))
931 (defun loop-optional-type (&optional variable)
932 ;; No variable specified implies that no destructuring is permissible.
933 (and *loop-source-code* ; Don't get confused by NILs..
934 (let ((z (car *loop-source-code*)))
935 (cond ((loop-tequal z 'of-type)
936 ;; This is the syntactically unambigous form in that
937 ;; the form of the type specifier does not matter.
938 ;; Also, it is assumed that the type specifier is
939 ;; unambiguously, and without need of translation, a
940 ;; common lisp type specifier or pattern (matching the
941 ;; variable) thereof.
946 ;; This is the (sort of) "old" syntax, even though we
947 ;; didn't used to support all of these type symbols.
948 (let ((type-spec (or (gethash z
949 (loop-universe-type-symbols
951 (gethash (symbol-name z)
952 (loop-universe-type-keywords
958 ;; This is our sort-of old syntax. But this is only
959 ;; valid for when we are destructuring, so we will be
960 ;; compulsive (should we really be?) and require that
961 ;; we in fact be doing variable destructuring here. We
962 ;; must translate the old keyword pattern typespec
963 ;; into a fully-specified pattern of real type
968 "~S found where a LOOP keyword, LOOP type keyword, or LOOP type pattern expected"
970 (loop-error "~S found where a LOOP keyword or LOOP type keyword expected" z))
972 (labels ((translate (k v)
977 (loop-universe-type-symbols
979 (gethash (symbol-name k)
980 (loop-universe-type-keywords
983 "The destructuring type pattern ~S contains the unrecognized type keyword ~S."
988 "The destructuring type pattern ~S doesn't match the variable pattern ~S."
990 (t (cons (translate (car k) (car v))
991 (translate (cdr k) (cdr v))))))
995 (cons (replicate typ (car v))
996 (replicate typ (cdr v))))))
997 (translate z variable)))))))
1001 (defun loop-bind-block ()
1002 (when (or *loop-vars* *loop-declarations* *loop-wrappers*)
1003 (push (list (nreverse *loop-vars*)
1005 *loop-desetq-crocks*
1008 (setq *loop-vars* nil
1009 *loop-declarations* nil
1010 *loop-desetq-crocks* nil
1011 *loop-wrappers* nil)))
1013 (defun loop-var-p (name)
1014 (do ((entry *loop-bind-stack* (cdr entry)))
1017 ((null entry) (return nil))
1018 ((assoc name (caar entry) :test #'eq) (return t)))))
1020 (defun loop-make-var (name initialization dtype &optional step-var-p)
1022 (setq name (gensym "LOOP-IGNORE-"))
1023 (push (list name initialization) *loop-vars*)
1024 (push `(ignore ,name) *loop-declarations*)
1025 (loop-declare-var name dtype))
1027 (when (or (assoc name *loop-vars*)
1029 (loop-error "duplicated variable ~S in a LOOP binding" name))
1030 (unless (symbolp name)
1031 (loop-error "bad variable ~S somewhere in LOOP" name))
1032 (loop-declare-var name dtype step-var-p initialization)
1033 ;; We use ASSOC on this list to check for duplications (above),
1034 ;; so don't optimize out this list:
1035 (push (list name (or initialization (loop-typed-init dtype step-var-p)))
1038 (let ((newvar (gensym "LOOP-DESTRUCTURE-")))
1039 (loop-declare-var name dtype)
1040 (push (list newvar initialization) *loop-vars*)
1041 ;; *LOOP-DESETQ-CROCKS* gathered in reverse order.
1042 (setq *loop-desetq-crocks*
1043 (list* name newvar *loop-desetq-crocks*))))
1044 (t (let ((tcar nil) (tcdr nil))
1045 (if (atom dtype) (setq tcar (setq tcdr dtype))
1046 (setq tcar (car dtype) tcdr (cdr dtype)))
1047 (loop-make-var (car name) nil tcar)
1048 (loop-make-var (cdr name) nil tcdr))))
1051 (defun loop-declare-var (name dtype &optional step-var-p initialization)
1052 (cond ((or (null name) (null dtype) (eq dtype t)) nil)
1054 (unless (or (sb!xc:subtypep t dtype)
1055 (and (eq (find-package :cl) (symbol-package name))
1056 (eq :special (sb!int:info :variable :kind name))))
1057 (let ((dtype (if initialization
1059 (let ((init (loop-typed-init dtype step-var-p)))
1060 (if (sb!xc:typep init dtype)
1062 `(or ,(type-of init) ,dtype))))))
1063 (push `(type ,dtype ,name) *loop-declarations*))))
1065 (cond ((consp dtype)
1066 (loop-declare-var (car name) (car dtype))
1067 (loop-declare-var (cdr name) (cdr dtype)))
1068 (t (loop-declare-var (car name) dtype)
1069 (loop-declare-var (cdr name) dtype))))
1070 (t (error "invalid LOOP variable passed in: ~S" name))))
1072 (defun loop-maybe-bind-form (form data-type)
1073 (if (constantp form)
1075 (loop-make-var (gensym "LOOP-BIND-") form data-type)))
1077 (defun loop-do-if (for negatep)
1078 (let ((form (loop-get-form))
1079 (*loop-inside-conditional* t)
1082 (flet ((get-clause (for)
1083 (do ((body nil)) (nil)
1084 (let ((key (car *loop-source-code*)) (*loop-body* nil) data)
1085 (cond ((not (symbolp key))
1087 "~S found where keyword expected getting LOOP clause after ~S"
1089 (t (setq *loop-source-context* *loop-source-code*)
1091 (when (and (loop-tequal (car *loop-source-code*) 'it)
1093 (setq *loop-source-code*
1096 (loop-when-it-var)))
1097 (cdr *loop-source-code*))))
1098 (cond ((or (not (setq data (loop-lookup-keyword
1099 key (loop-universe-keywords *loop-universe*))))
1100 (progn (apply (symbol-function (car data))
1102 (null *loop-body*)))
1104 "~S does not introduce a LOOP clause that can follow ~S."
1106 (t (setq body (nreconc *loop-body* body)))))))
1107 (setq first-clause-p nil)
1108 (if (loop-tequal (car *loop-source-code*) :and)
1110 (return (if (cdr body)
1111 `(progn ,@(nreverse body))
1113 (let ((then (get-clause for))
1114 (else (when (loop-tequal (car *loop-source-code*) :else)
1116 (list (get-clause :else)))))
1117 (when (loop-tequal (car *loop-source-code*) :end)
1119 (when it-p (setq form `(setq ,it-p ,form)))
1121 `(if ,(if negatep `(not ,form) form)
1125 (defun loop-do-initially ()
1126 (loop-disallow-conditional :initially)
1127 (push (loop-get-progn) *loop-prologue*))
1129 (defun loop-do-finally ()
1130 (loop-disallow-conditional :finally)
1131 (push (loop-get-progn) *loop-epilogue*))
1133 (defun loop-do-do ()
1134 (loop-emit-body (loop-get-progn)))
1136 (defun loop-do-named ()
1137 (let ((name (loop-pop-source)))
1138 (unless (symbolp name)
1139 (loop-error "~S is an invalid name for your LOOP" name))
1140 (when (or *loop-before-loop* *loop-body* *loop-after-epilogue* *loop-inside-conditional*)
1141 (loop-error "The NAMED ~S clause occurs too late." name))
1143 (loop-error "You may only use one NAMED clause in your loop: NAMED ~S ... NAMED ~S."
1144 (car *loop-names*) name))
1145 (setq *loop-names* (list name))))
1147 (defun loop-do-return ()
1148 (loop-emit-body (loop-construct-return (loop-get-form))))
1150 ;;;; value accumulation: LIST
1152 (defstruct (loop-collector
1160 (data nil)) ;collector-specific data
1162 (defun loop-get-collection-info (collector class default-type)
1163 (let ((form (loop-get-form))
1164 (name (when (loop-tequal (car *loop-source-code*) 'into)
1166 (loop-pop-source))))
1167 (when (not (symbolp name))
1168 (loop-error "The value accumulation recipient name, ~S, is not a symbol." name))
1170 (loop-disallow-aggregate-booleans))
1171 (let ((dtype (or (loop-optional-type) default-type))
1172 (cruft (find (the symbol name) *loop-collection-cruft*
1173 :key #'loop-collector-name)))
1175 (when (and name (loop-var-p name))
1176 (loop-error "Variable ~S in INTO clause is a duplicate" name))
1177 (push (setq cruft (make-loop-collector
1178 :name name :class class
1179 :history (list collector) :dtype dtype))
1180 *loop-collection-cruft*))
1181 (t (unless (eq (loop-collector-class cruft) class)
1183 "incompatible kinds of LOOP value accumulation specified for collecting~@
1184 ~:[as the value of the LOOP~;~:*INTO ~S~]: ~S and ~S"
1185 name (car (loop-collector-history cruft)) collector))
1186 (unless (equal dtype (loop-collector-dtype cruft))
1188 "unequal datatypes specified in different LOOP value accumulations~@
1190 name dtype (loop-collector-dtype cruft))
1191 (when (eq (loop-collector-dtype cruft) t)
1192 (setf (loop-collector-dtype cruft) dtype)))
1193 (push collector (loop-collector-history cruft))))
1194 (values cruft form))))
1196 (defun loop-list-collection (specifically) ; NCONC, LIST, or APPEND
1197 (multiple-value-bind (lc form)
1198 (loop-get-collection-info specifically 'list 'list)
1199 (let ((tempvars (loop-collector-tempvars lc)))
1201 (setf (loop-collector-tempvars lc)
1202 (setq tempvars (list* (gensym "LOOP-LIST-HEAD-")
1203 (gensym "LOOP-LIST-TAIL-")
1204 (and (loop-collector-name lc)
1205 (list (loop-collector-name lc))))))
1206 (push `(with-loop-list-collection-head ,tempvars) *loop-wrappers*)
1207 (unless (loop-collector-name lc)
1208 (loop-emit-final-value `(loop-collect-answer ,(car tempvars)
1209 ,@(cddr tempvars)))))
1211 (list (setq form `(list ,form)))
1213 (append (unless (and (consp form) (eq (car form) 'list))
1214 (setq form `(copy-list ,form)))))
1215 (loop-emit-body `(loop-collect-rplacd ,tempvars ,form)))))
1217 ;;;; value accumulation: MAX, MIN, SUM, COUNT
1219 (defun loop-sum-collection (specifically required-type default-type);SUM, COUNT
1220 (multiple-value-bind (lc form)
1221 (loop-get-collection-info specifically 'sum default-type)
1222 (loop-check-data-type (loop-collector-dtype lc) required-type)
1223 (let ((tempvars (loop-collector-tempvars lc)))
1225 (setf (loop-collector-tempvars lc)
1226 (setq tempvars (list (loop-make-var
1227 (or (loop-collector-name lc)
1228 (gensym "LOOP-SUM-"))
1229 nil (loop-collector-dtype lc)))))
1230 (unless (loop-collector-name lc)
1231 (loop-emit-final-value (car (loop-collector-tempvars lc)))))
1233 (if (eq specifically 'count)
1235 (setq ,(car tempvars)
1236 (1+ ,(car tempvars))))
1237 `(setq ,(car tempvars)
1241 (defun loop-maxmin-collection (specifically)
1242 (multiple-value-bind (lc form)
1243 (loop-get-collection-info specifically 'maxmin 'real)
1244 (loop-check-data-type (loop-collector-dtype lc) 'real)
1245 (let ((data (loop-collector-data lc)))
1247 (setf (loop-collector-data lc)
1248 (setq data (make-loop-minimax
1249 (or (loop-collector-name lc)
1250 (gensym "LOOP-MAXMIN-"))
1251 (loop-collector-dtype lc))))
1252 (unless (loop-collector-name lc)
1253 (loop-emit-final-value (loop-minimax-answer-variable data))))
1254 (loop-note-minimax-operation specifically data)
1255 (push `(with-minimax-value ,data) *loop-wrappers*)
1256 (loop-emit-body `(loop-accumulate-minimax-value ,data
1260 ;;;; value accumulation: aggregate booleans
1262 ;;; handling the ALWAYS and NEVER loop keywords
1264 ;;; Under ANSI these are not permitted to appear under conditionalization.
1265 (defun loop-do-always (restrictive negate)
1266 (let ((form (loop-get-form)))
1267 (when restrictive (loop-disallow-conditional))
1268 (loop-disallow-anonymous-collectors)
1269 (loop-emit-body `(,(if negate 'when 'unless) ,form
1270 ,(loop-construct-return nil)))
1271 (loop-emit-final-value t)))
1273 ;;; handling the THEREIS loop keyword
1275 ;;; Under ANSI this is not permitted to appear under conditionalization.
1276 (defun loop-do-thereis (restrictive)
1277 (when restrictive (loop-disallow-conditional))
1278 (loop-disallow-anonymous-collectors)
1279 (loop-emit-final-value)
1280 (loop-emit-body `(when (setq ,(loop-when-it-var) ,(loop-get-form))
1281 ,(loop-construct-return *loop-when-it-var*))))
1283 (defun loop-do-while (negate kwd &aux (form (loop-get-form)))
1284 (loop-disallow-conditional kwd)
1285 (loop-pseudo-body `(,(if negate 'when 'unless) ,form (go end-loop))))
1287 (defun loop-do-repeat ()
1288 (loop-disallow-conditional :repeat)
1289 (let ((form (loop-get-form))
1291 (let ((var (loop-make-var (gensym "LOOP-REPEAT-") `(ceiling ,form) type)))
1292 (push `(if (<= ,var 0) (go end-loop) (decf ,var)) *loop-before-loop*)
1293 (push `(if (<= ,var 0) (go end-loop) (decf ,var)) *loop-after-body*)
1294 ;; FIXME: What should
1295 ;; (loop count t into a
1298 ;; finally (return (list a b)))
1299 ;; return: (3 3) or (4 3)? PUSHes above are for the former
1300 ;; variant, L-P-B below for the latter.
1301 #+nil (loop-pseudo-body `(when (minusp (decf ,var)) (go end-loop))))))
1303 (defun loop-do-with ()
1304 (loop-disallow-conditional :with)
1305 (do ((var) (val) (dtype))
1307 (setq var (loop-pop-source)
1308 dtype (loop-optional-type var)
1309 val (cond ((loop-tequal (car *loop-source-code*) :=)
1313 (when (and var (loop-var-p var))
1314 (loop-error "Variable ~S has already been used" var))
1315 (loop-make-var var val dtype)
1316 (if (loop-tequal (car *loop-source-code*) :and)
1318 (return (loop-bind-block)))))
1320 ;;;; the iteration driver
1322 (defun loop-hack-iteration (entry)
1323 (flet ((make-endtest (list-of-forms)
1324 (cond ((null list-of-forms) nil)
1325 ((member t list-of-forms) '(go end-loop))
1326 (t `(when ,(if (null (cdr (setq list-of-forms
1327 (nreverse list-of-forms))))
1329 (cons 'or list-of-forms))
1331 (do ((pre-step-tests nil)
1333 (post-step-tests nil)
1335 (pre-loop-pre-step-tests nil)
1336 (pre-loop-steps nil)
1337 (pre-loop-post-step-tests nil)
1338 (pre-loop-pseudo-steps nil)
1341 ;; Note that we collect endtests in reverse order, but steps in correct
1342 ;; order. MAKE-ENDTEST does the nreverse for us.
1343 (setq tem (setq data
1344 (apply (symbol-function (first entry)) (rest entry))))
1345 (and (car tem) (push (car tem) pre-step-tests))
1346 (setq steps (nconc steps (copy-list (car (setq tem (cdr tem))))))
1347 (and (car (setq tem (cdr tem))) (push (car tem) post-step-tests))
1349 (nconc pseudo-steps (copy-list (car (setq tem (cdr tem))))))
1350 (setq tem (cdr tem))
1351 (when *loop-emitted-body*
1352 (loop-error "iteration in LOOP follows body code"))
1353 (unless tem (setq tem data))
1354 (when (car tem) (push (car tem) pre-loop-pre-step-tests))
1355 ;; FIXME: This (SETF FOO (NCONC FOO BAR)) idiom appears often enough
1356 ;; that it might be worth making it into an NCONCF macro.
1357 (setq pre-loop-steps
1358 (nconc pre-loop-steps (copy-list (car (setq tem (cdr tem))))))
1359 (when (car (setq tem (cdr tem)))
1360 (push (car tem) pre-loop-post-step-tests))
1361 (setq pre-loop-pseudo-steps
1362 (nconc pre-loop-pseudo-steps (copy-list (cadr tem))))
1363 (unless (loop-tequal (car *loop-source-code*) :and)
1364 (setq *loop-before-loop*
1365 (list* (loop-make-desetq pre-loop-pseudo-steps)
1366 (make-endtest pre-loop-post-step-tests)
1367 (loop-make-psetq pre-loop-steps)
1368 (make-endtest pre-loop-pre-step-tests)
1369 *loop-before-loop*))
1370 (setq *loop-after-body*
1371 (list* (loop-make-desetq pseudo-steps)
1372 (make-endtest post-step-tests)
1373 (loop-make-psetq steps)
1374 (make-endtest pre-step-tests)
1378 (loop-pop-source)))) ; Flush the "AND".
1380 ;;;; main iteration drivers
1382 ;;; FOR variable keyword ..args..
1383 (defun loop-do-for ()
1384 (let* ((var (loop-pop-source))
1385 (data-type (loop-optional-type var))
1386 (keyword (loop-pop-source))
1389 (setq first-arg (loop-get-form))
1390 (unless (and (symbolp keyword)
1391 (setq tem (loop-lookup-keyword
1393 (loop-universe-for-keywords *loop-universe*))))
1394 (loop-error "~S is an unknown keyword in FOR or AS clause in LOOP."
1396 (apply (car tem) var first-arg data-type (cdr tem))))
1398 (defun loop-when-it-var ()
1399 (or *loop-when-it-var*
1400 (setq *loop-when-it-var*
1401 (loop-make-var (gensym "LOOP-IT-") nil nil))))
1403 ;;;; various FOR/AS subdispatches
1405 ;;; ANSI "FOR x = y [THEN z]" is sort of like the old Genera one when
1406 ;;; the THEN is omitted (other than being more stringent in its
1407 ;;; placement), and like the old "FOR x FIRST y THEN z" when the THEN
1408 ;;; is present. I.e., the first initialization occurs in the loop body
1409 ;;; (first-step), not in the variable binding phase.
1410 (defun loop-ansi-for-equals (var val data-type)
1411 (loop-make-var var nil data-type)
1412 (cond ((loop-tequal (car *loop-source-code*) :then)
1413 ;; Then we are the same as "FOR x FIRST y THEN z".
1415 `(() (,var ,(loop-get-form)) () ()
1416 () (,var ,val) () ()))
1417 (t ;; We are the same as "FOR x = y".
1418 `(() (,var ,val) () ()))))
1420 (defun loop-for-across (var val data-type)
1421 (loop-make-var var nil data-type)
1422 (let ((vector-var (gensym "LOOP-ACROSS-VECTOR-"))
1423 (index-var (gensym "LOOP-ACROSS-INDEX-")))
1424 (multiple-value-bind (vector-form constantp vector-value)
1425 (loop-constant-fold-if-possible val 'vector)
1427 vector-var vector-form
1428 (if (and (consp vector-form) (eq (car vector-form) 'the))
1431 (loop-make-var index-var 0 'fixnum)
1433 (length-form (cond ((not constantp)
1434 (let ((v (gensym "LOOP-ACROSS-LIMIT-")))
1435 (push `(setq ,v (length ,vector-var))
1437 (loop-make-var v 0 'fixnum)))
1438 (t (setq length (length vector-value)))))
1439 (first-test `(>= ,index-var ,length-form))
1440 (other-test first-test)
1441 (step `(,var (aref ,vector-var ,index-var)))
1442 (pstep `(,index-var (1+ ,index-var))))
1443 (declare (fixnum length))
1445 (setq first-test (= length 0))
1447 (setq other-test t)))
1448 `(,other-test ,step () ,pstep
1449 ,@(and (not (eq first-test other-test))
1450 `(,first-test ,step () ,pstep)))))))
1454 (defun loop-list-step (listvar)
1455 ;; We are not equipped to analyze whether 'FOO is the same as #'FOO
1456 ;; here in any sensible fashion, so let's give an obnoxious warning
1457 ;; whenever 'FOO is used as the stepping function.
1459 ;; While a Discerning Compiler may deal intelligently with
1460 ;; (FUNCALL 'FOO ...), not recognizing FOO may defeat some LOOP
1462 (let ((stepper (cond ((loop-tequal (car *loop-source-code*) :by)
1465 (t '(function cdr)))))
1466 (cond ((and (consp stepper) (eq (car stepper) 'quote))
1467 (loop-warn "Use of QUOTE around stepping function in LOOP will be left verbatim.")
1468 `(funcall ,stepper ,listvar))
1469 ((and (consp stepper) (eq (car stepper) 'function))
1470 (list (cadr stepper) listvar))
1472 `(funcall ,(loop-make-var (gensym "LOOP-FN-") stepper 'function)
1475 (defun loop-for-on (var val data-type)
1476 (multiple-value-bind (list constantp list-value)
1477 (loop-constant-fold-if-possible val)
1478 (let ((listvar var))
1479 (cond ((and var (symbolp var))
1480 (loop-make-var var list data-type))
1482 (loop-make-var (setq listvar (gensym)) list 't)
1483 (loop-make-var var nil data-type)))
1484 (let ((list-step (loop-list-step listvar)))
1485 (let* ((first-endtest
1486 ;; mysterious comment from original CMU CL sources:
1487 ;; the following should use `atom' instead of `endp',
1490 (other-endtest first-endtest))
1491 (when (and constantp (listp list-value))
1492 (setq first-endtest (null list-value)))
1493 (cond ((eq var listvar)
1494 ;; The contour of the loop is different because we
1495 ;; use the user's variable...
1496 `(() (,listvar ,list-step)
1497 ,other-endtest () () () ,first-endtest ()))
1498 (t (let ((step `(,var ,listvar))
1499 (pseudo `(,listvar ,list-step)))
1500 `(,other-endtest ,step () ,pseudo
1501 ,@(and (not (eq first-endtest other-endtest))
1502 `(,first-endtest ,step () ,pseudo)))))))))))
1504 (defun loop-for-in (var val data-type)
1505 (multiple-value-bind (list constantp list-value)
1506 (loop-constant-fold-if-possible val)
1507 (let ((listvar (gensym "LOOP-LIST-")))
1508 (loop-make-var var nil data-type)
1509 (loop-make-var listvar list 'list)
1510 (let ((list-step (loop-list-step listvar)))
1511 (let* ((first-endtest `(endp ,listvar))
1512 (other-endtest first-endtest)
1513 (step `(,var (car ,listvar)))
1514 (pseudo-step `(,listvar ,list-step)))
1515 (when (and constantp (listp list-value))
1516 (setq first-endtest (null list-value)))
1517 `(,other-endtest ,step () ,pseudo-step
1518 ,@(and (not (eq first-endtest other-endtest))
1519 `(,first-endtest ,step () ,pseudo-step))))))))
1521 ;;;; iteration paths
1523 (defstruct (loop-path
1532 (defun add-loop-path (names function universe
1533 &key preposition-groups inclusive-permitted user-data)
1534 (declare (type loop-universe universe))
1535 (unless (listp names)
1536 (setq names (list names)))
1537 (let ((ht (loop-universe-path-keywords universe))
1539 :names (mapcar #'symbol-name names)
1541 :user-data user-data
1542 :preposition-groups (mapcar (lambda (x)
1543 (if (listp x) x (list x)))
1545 :inclusive-permitted inclusive-permitted)))
1546 (dolist (name names)
1547 (setf (gethash (symbol-name name) ht) lp))
1550 ;;; Note: Path functions are allowed to use LOOP-MAKE-VAR, hack
1551 ;;; the prologue, etc.
1552 (defun loop-for-being (var val data-type)
1553 ;; FOR var BEING each/the pathname prep-phrases using-stuff... each/the =
1554 ;; EACH or THE. Not clear if it is optional, so I guess we'll warn.
1559 (initial-prepositions nil))
1560 (cond ((loop-tmember val '(:each :the)) (setq path (loop-pop-source)))
1561 ((loop-tequal (car *loop-source-code*) :and)
1564 (unless (loop-tmember (car *loop-source-code*)
1565 '(:its :each :his :her))
1566 (loop-error "~S was found where ITS or EACH expected in LOOP iteration path syntax."
1567 (car *loop-source-code*)))
1569 (setq path (loop-pop-source))
1570 (setq initial-prepositions `((:in ,val))))
1571 (t (loop-error "unrecognizable LOOP iteration path syntax: missing EACH or THE?")))
1572 (cond ((not (symbolp path))
1574 "~S was found where a LOOP iteration path name was expected."
1576 ((not (setq data (loop-lookup-keyword path (loop-universe-path-keywords *loop-universe*))))
1577 (loop-error "~S is not the name of a LOOP iteration path." path))
1578 ((and inclusive (not (loop-path-inclusive-permitted data)))
1579 (loop-error "\"Inclusive\" iteration is not possible with the ~S LOOP iteration path." path)))
1580 (let ((fun (loop-path-function data))
1581 (preps (nconc initial-prepositions
1582 (loop-collect-prepositional-phrases
1583 (loop-path-preposition-groups data)
1585 (user-data (loop-path-user-data data)))
1586 (when (symbolp fun) (setq fun (symbol-function fun)))
1587 (setq stuff (if inclusive
1588 (apply fun var data-type preps :inclusive t user-data)
1589 (apply fun var data-type preps user-data))))
1590 (when *loop-named-vars*
1591 (loop-error "Unused USING vars: ~S." *loop-named-vars*))
1592 ;; STUFF is now (bindings prologue-forms . stuff-to-pass-back).
1593 ;; Protect the system from the user and the user from himself.
1594 (unless (member (length stuff) '(6 10))
1595 (loop-error "Value passed back by LOOP iteration path function for path ~S has invalid length."
1597 (do ((l (car stuff) (cdr l)) (x)) ((null l))
1598 (if (atom (setq x (car l)))
1599 (loop-make-var x nil nil)
1600 (loop-make-var (car x) (cadr x) (caddr x))))
1601 (setq *loop-prologue* (nconc (reverse (cadr stuff)) *loop-prologue*))
1604 (defun loop-named-var (name)
1605 (let ((tem (loop-tassoc name *loop-named-vars*)))
1606 (declare (list tem))
1607 (cond ((null tem) (values (gensym) nil))
1608 (t (setq *loop-named-vars* (delete tem *loop-named-vars*))
1609 (values (cdr tem) t)))))
1611 (defun loop-collect-prepositional-phrases (preposition-groups
1615 (flet ((in-group-p (x group) (car (loop-tmember x group))))
1617 (prepositional-phrases initial-phrases)
1618 (this-group nil nil)
1620 (disallowed-prepositions
1623 (find (car x) preposition-groups :test #'in-group-p)))
1625 (used-prepositions (mapcar #'car initial-phrases)))
1626 ((null *loop-source-code*) (nreverse prepositional-phrases))
1627 (declare (symbol this-prep))
1628 (setq token (car *loop-source-code*))
1629 (dolist (group preposition-groups)
1630 (when (setq this-prep (in-group-p token group))
1631 (return (setq this-group group))))
1633 (when (member this-prep disallowed-prepositions)
1635 (if (member this-prep used-prepositions)
1636 "A ~S prepositional phrase occurs multiply for some LOOP clause."
1637 "Preposition ~S was used when some other preposition has subsumed it.")
1639 (setq used-prepositions (if (listp this-group)
1640 (append this-group used-prepositions)
1641 (cons this-group used-prepositions)))
1643 (push (list this-prep (loop-get-form)) prepositional-phrases))
1644 ((and using-allowed (loop-tequal token 'using))
1646 (do ((z (loop-pop-source) (loop-pop-source)) (tem)) (nil)
1648 (if (setq tem (loop-tassoc (car z) *loop-named-vars*))
1650 "The variable substitution for ~S occurs twice in a USING phrase,~@
1652 (car z) (cadr z) (cadr tem))
1653 (push (cons (car z) (cadr z)) *loop-named-vars*)))
1654 (when (or (null *loop-source-code*)
1655 (symbolp (car *loop-source-code*)))
1657 (t (return (nreverse prepositional-phrases)))))))
1659 ;;;; master sequencer function
1661 (defun loop-sequencer (indexv indexv-type
1662 variable variable-type
1663 sequence-variable sequence-type
1664 step-hack default-top
1666 (let ((endform nil) ; form (constant or variable) with limit value
1667 (sequencep nil) ; T if sequence arg has been provided
1668 (testfn nil) ; endtest function
1669 (test nil) ; endtest form
1670 (stepby (1+ (or (loop-typed-init indexv-type) 0))) ; our increment
1671 (stepby-constantp t)
1672 (step nil) ; step form
1673 (dir nil) ; direction of stepping: NIL, :UP, :DOWN
1674 (inclusive-iteration nil) ; T if include last index
1675 (start-given nil) ; T when prep phrase has specified start
1677 (start-constantp nil)
1678 (limit-given nil) ; T when prep phrase has specified end
1679 (limit-constantp nil)
1682 (flet ((assert-index-for-arithmetic (index)
1683 (unless (atom index)
1684 (loop-error "Arithmetic index must be an atom."))))
1685 (when variable (loop-make-var variable nil variable-type))
1686 (do ((l prep-phrases (cdr l)) (prep) (form) (odir)) ((null l))
1687 (setq prep (caar l) form (cadar l))
1691 (loop-make-var sequence-variable form sequence-type))
1692 ((:from :downfrom :upfrom)
1693 (setq start-given t)
1694 (cond ((eq prep :downfrom) (setq dir ':down))
1695 ((eq prep :upfrom) (setq dir ':up)))
1696 (multiple-value-setq (form start-constantp start-value)
1697 (loop-constant-fold-if-possible form indexv-type))
1698 (assert-index-for-arithmetic indexv)
1699 ;; KLUDGE: loop-make-var generates a temporary symbol for
1700 ;; indexv if it is NIL. We have to use it to have the index
1702 (setq indexv (loop-make-var indexv form indexv-type)))
1703 ((:upto :to :downto :above :below)
1704 (cond ((loop-tequal prep :upto) (setq inclusive-iteration
1706 ((loop-tequal prep :to) (setq inclusive-iteration t))
1707 ((loop-tequal prep :downto) (setq inclusive-iteration
1709 ((loop-tequal prep :above) (setq dir ':down))
1710 ((loop-tequal prep :below) (setq dir ':up)))
1711 (setq limit-given t)
1712 (multiple-value-setq (form limit-constantp limit-value)
1713 (loop-constant-fold-if-possible form `(and ,indexv-type real)))
1714 (setq endform (if limit-constantp
1717 (gensym "LOOP-LIMIT-") form
1718 `(and ,indexv-type real)))))
1720 (multiple-value-setq (form stepby-constantp stepby)
1721 (loop-constant-fold-if-possible form
1722 `(and ,indexv-type (real (0)))))
1723 (unless stepby-constantp
1724 (loop-make-var (setq stepby (gensym "LOOP-STEP-BY-"))
1726 `(and ,indexv-type (real (0)))
1729 "~S invalid preposition in sequencing or sequence path;~@
1730 maybe invalid prepositions were specified in iteration path descriptor?"
1732 (when (and odir dir (not (eq dir odir)))
1734 "conflicting stepping directions in LOOP sequencing path"))
1736 (when (and sequence-variable (not sequencep))
1737 (loop-error "missing OF or IN phrase in sequence path"))
1738 ;; Now fill in the defaults.
1741 ;; if both start and limit are given, they had better both
1742 ;; be REAL. We already enforce the REALness of LIMIT,
1743 ;; above; here's the KLUDGE to enforce the type of START.
1744 (flet ((type-declaration-of (x)
1745 (and (eq (car x) 'type) (caddr x))))
1746 (let ((decl (find indexv *loop-declarations*
1747 :key #'type-declaration-of))
1748 (%decl (find indexv *loop-declarations*
1749 :key #'type-declaration-of
1751 (sb!int:aver (eq decl %decl))
1754 `(and real ,(cadr decl)))))))
1756 ;; DUPLICATE KLUDGE: loop-make-var generates a temporary
1757 ;; symbol for indexv if it is NIL. See also the comment in
1758 ;; the (:from :downfrom :upfrom) case
1760 (assert-index-for-arithmetic indexv)
1764 (setq start-constantp t
1765 start-value (or (loop-typed-init indexv-type) 0))
1766 `(and ,indexv-type real)))))
1767 (cond ((member dir '(nil :up))
1768 (when (or limit-given default-top)
1770 (loop-make-var (setq endform (gensym "LOOP-SEQ-LIMIT-"))
1773 (push `(setq ,endform ,default-top) *loop-prologue*))
1774 (setq testfn (if inclusive-iteration '> '>=)))
1775 (setq step (if (eql stepby 1) `(1+ ,indexv) `(+ ,indexv ,stepby))))
1776 (t (unless start-given
1778 (loop-error "don't know where to start stepping"))
1779 (push `(setq ,indexv (1- ,default-top)) *loop-prologue*))
1780 (when (and default-top (not endform))
1781 (setq endform (loop-typed-init indexv-type)
1782 inclusive-iteration t))
1783 (when endform (setq testfn (if inclusive-iteration '< '<=)))
1785 (if (eql stepby 1) `(1- ,indexv) `(- ,indexv ,stepby)))))
1788 `(,testfn ,indexv ,endform)))
1791 `(,variable ,step-hack)))
1792 (let ((first-test test) (remaining-tests test))
1793 ;; As far as I can tell, the effect of the following code is
1794 ;; to detect cases where we know statically whether the first
1795 ;; iteration of the loop will be executed. Depending on the
1796 ;; situation, we can either:
1797 ;; a) save one jump and one comparison per loop (not per iteration)
1798 ;; when it will get executed
1799 ;; b) remove the loop body completely when it won't be executed
1801 ;; Noble goals. However, the code generated in case a) will
1802 ;; fool the loop induction variable detection, and cause
1803 ;; code like (LOOP FOR I TO 10 ...) to use generic addition
1806 ;; Since the gain in case a) is rather minimal and Python is
1807 ;; generally smart enough to handle b) without any extra
1808 ;; support from the loop macro, I've disabled this code for
1809 ;; now. The code and the comment left here in case somebody
1810 ;; extends the induction variable bound detection to work
1811 ;; with code where the stepping precedes the test.
1812 ;; -- JES 2005-11-30
1814 (when (and stepby-constantp start-constantp limit-constantp
1815 (realp start-value) (realp limit-value))
1816 (when (setq first-test
1817 (funcall (symbol-function testfn)
1820 (setq remaining-tests t)))
1821 `(() (,indexv ,step)
1822 ,remaining-tests ,step-hack () () ,first-test ,step-hack)))))
1824 ;;;; interfaces to the master sequencer
1826 (defun loop-for-arithmetic (var val data-type kwd)
1828 var (loop-check-data-type data-type 'number)
1829 nil nil nil nil nil nil
1830 (loop-collect-prepositional-phrases
1831 '((:from :upfrom :downfrom) (:to :upto :downto :above :below) (:by))
1832 nil (list (list kwd val)))))
1835 ;;;; builtin LOOP iteration paths
1838 (loop for v being the hash-values of ht do (print v))
1839 (loop for k being the hash-keys of ht do (print k))
1840 (loop for v being the hash-values of ht using (hash-key k) do (print (list k v)))
1841 (loop for k being the hash-keys of ht using (hash-value v) do (print (list k v)))
1844 (defun loop-hash-table-iteration-path (variable data-type prep-phrases
1845 &key (which (sb!int:missing-arg)))
1846 (declare (type (member :hash-key :hash-value) which))
1847 (cond ((or (cdr prep-phrases) (not (member (caar prep-phrases) '(:in :of))))
1848 (loop-error "too many prepositions!"))
1849 ((null prep-phrases)
1850 (loop-error "missing OF or IN in ~S iteration path")))
1851 (let ((ht-var (gensym "LOOP-HASHTAB-"))
1852 (next-fn (gensym "LOOP-HASHTAB-NEXT-"))
1853 (dummy-predicate-var nil)
1855 (multiple-value-bind (other-var other-p)
1856 (loop-named-var (ecase which
1857 (:hash-key 'hash-value)
1858 (:hash-value 'hash-key)))
1859 ;; @@@@ LOOP-NAMED-VAR returns a second value of T if the name
1860 ;; was actually specified, so clever code can throw away the
1861 ;; GENSYM'ed-up variable if it isn't really needed. The
1862 ;; following is for those implementations in which we cannot put
1863 ;; dummy NILs into MULTIPLE-VALUE-SETQ variable lists.
1865 dummy-predicate-var (loop-when-it-var))
1866 (let* ((key-var nil)
1868 (variable (or variable (gensym "LOOP-HASH-VAR-TEMP-")))
1869 (bindings `((,variable nil ,data-type)
1870 (,ht-var ,(cadar prep-phrases))
1871 ,@(and other-p other-var `((,other-var nil))))))
1873 (:hash-key (setq key-var variable
1874 val-var (and other-p other-var)))
1875 (:hash-value (setq key-var (and other-p other-var)
1877 (push `(with-hash-table-iterator (,next-fn ,ht-var)) *loop-wrappers*)
1878 (when (or (consp key-var) data-type)
1880 `(,key-var ,(setq key-var (gensym "LOOP-HASH-KEY-TEMP-"))
1882 (push `(,key-var nil) bindings))
1883 (when (or (consp val-var) data-type)
1885 `(,val-var ,(setq val-var (gensym "LOOP-HASH-VAL-TEMP-"))
1887 (push `(,val-var nil) bindings))
1888 `(,bindings ;bindings
1892 (not (multiple-value-setq (,dummy-predicate-var ,key-var ,val-var)
1893 (,next-fn))) ;post-test
1896 (defun loop-package-symbols-iteration-path (variable data-type prep-phrases
1898 (cond ((and prep-phrases (cdr prep-phrases))
1899 (loop-error "Too many prepositions!"))
1900 ((and prep-phrases (not (member (caar prep-phrases) '(:in :of))))
1901 (sb!int:bug "Unknown preposition ~S." (caar prep-phrases))))
1902 (unless (symbolp variable)
1903 (loop-error "Destructuring is not valid for package symbol iteration."))
1904 (let ((pkg-var (gensym "LOOP-PKGSYM-"))
1905 (next-fn (gensym "LOOP-PKGSYM-NEXT-"))
1906 (variable (or variable (gensym "LOOP-PKGSYM-VAR-")))
1907 (package (or (cadar prep-phrases) '*package*)))
1908 (push `(with-package-iterator (,next-fn ,pkg-var ,@symbol-types))
1910 `(((,variable nil ,data-type) (,pkg-var ,package))
1914 (not (multiple-value-setq (,(loop-when-it-var)
1921 (defun make-ansi-loop-universe ()
1922 (let ((w (make-standard-loop-universe
1923 :keywords '((named (loop-do-named))
1924 (initially (loop-do-initially))
1925 (finally (loop-do-finally))
1927 (doing (loop-do-do))
1928 (return (loop-do-return))
1929 (collect (loop-list-collection list))
1930 (collecting (loop-list-collection list))
1931 (append (loop-list-collection append))
1932 (appending (loop-list-collection append))
1933 (nconc (loop-list-collection nconc))
1934 (nconcing (loop-list-collection nconc))
1935 (count (loop-sum-collection count
1938 (counting (loop-sum-collection count
1941 (sum (loop-sum-collection sum number number))
1942 (summing (loop-sum-collection sum number number))
1943 (maximize (loop-maxmin-collection max))
1944 (minimize (loop-maxmin-collection min))
1945 (maximizing (loop-maxmin-collection max))
1946 (minimizing (loop-maxmin-collection min))
1947 (always (loop-do-always t nil)) ; Normal, do always
1948 (never (loop-do-always t t)) ; Negate test on always.
1949 (thereis (loop-do-thereis t))
1950 (while (loop-do-while nil :while)) ; Normal, do while
1951 (until (loop-do-while t :until)) ;Negate test on while
1952 (when (loop-do-if when nil)) ; Normal, do when
1953 (if (loop-do-if if nil)) ; synonymous
1954 (unless (loop-do-if unless t)) ; Negate test on when
1955 (with (loop-do-with))
1956 (repeat (loop-do-repeat)))
1957 :for-keywords '((= (loop-ansi-for-equals))
1958 (across (loop-for-across))
1961 (from (loop-for-arithmetic :from))
1962 (downfrom (loop-for-arithmetic :downfrom))
1963 (upfrom (loop-for-arithmetic :upfrom))
1964 (below (loop-for-arithmetic :below))
1965 (above (loop-for-arithmetic :above))
1966 (to (loop-for-arithmetic :to))
1967 (upto (loop-for-arithmetic :upto))
1968 (downto (loop-for-arithmetic :downto))
1969 (by (loop-for-arithmetic :by))
1970 (being (loop-for-being)))
1971 :iteration-keywords '((for (loop-do-for))
1973 :type-symbols '(array atom bignum bit bit-vector character
1974 compiled-function complex cons double-float
1975 fixnum float function hash-table integer
1976 keyword list long-float nil null number
1977 package pathname random-state ratio rational
1978 readtable sequence short-float simple-array
1979 simple-bit-vector simple-string simple-vector
1980 single-float standard-char stream string
1981 base-char symbol t vector)
1982 :type-keywords nil)))
1983 (add-loop-path '(hash-key hash-keys) 'loop-hash-table-iteration-path w
1984 :preposition-groups '((:of :in))
1985 :inclusive-permitted nil
1986 :user-data '(:which :hash-key))
1987 (add-loop-path '(hash-value hash-values) 'loop-hash-table-iteration-path w
1988 :preposition-groups '((:of :in))
1989 :inclusive-permitted nil
1990 :user-data '(:which :hash-value))
1991 (add-loop-path '(symbol symbols) 'loop-package-symbols-iteration-path w
1992 :preposition-groups '((:of :in))
1993 :inclusive-permitted nil
1994 :user-data '(:symbol-types (:internal
1997 (add-loop-path '(external-symbol external-symbols)
1998 'loop-package-symbols-iteration-path w
1999 :preposition-groups '((:of :in))
2000 :inclusive-permitted nil
2001 :user-data '(:symbol-types (:external)))
2002 (add-loop-path '(present-symbol present-symbols)
2003 'loop-package-symbols-iteration-path w
2004 :preposition-groups '((:of :in))
2005 :inclusive-permitted nil
2006 :user-data '(:symbol-types (:internal
2010 (defparameter *loop-ansi-universe*
2011 (make-ansi-loop-universe))
2013 (defun loop-standard-expansion (keywords-and-forms environment universe)
2014 (if (and keywords-and-forms (symbolp (car keywords-and-forms)))
2015 (loop-translate keywords-and-forms environment universe)
2016 (let ((tag (gensym)))
2017 `(block nil (tagbody ,tag (progn ,@keywords-and-forms) (go ,tag))))))
2019 (sb!int:defmacro-mundanely loop (&environment env &rest keywords-and-forms)
2020 (loop-standard-expansion keywords-and-forms env *loop-ansi-universe*))
2022 (sb!int:defmacro-mundanely loop-finish ()
2024 "Cause the iteration to terminate \"normally\", the same as implicit
2025 termination by an iteration driving clause, or by use of WHILE or
2026 UNTIL -- the epilogue code (if any) will be run, and any implicitly
2027 collected result will be returned as the value of the LOOP."