source: trunk/j/src/org/armedbear/lisp/loop.lisp @ 11297

Last change on this file since 11297 was 11297, checked in by ehuelsmann, 13 years ago

Set Id keyword for expansion.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 76.7 KB
Line 
1;;; loop.lisp
2;;;
3;;; Copyright (C) 2004-2007 Peter Graves
4;;; $Id: loop.lisp 11297 2008-08-31 13:26:45Z ehuelsmann $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20;;; Adapted from SBCL.
21
22;;;; the LOOP iteration macro
23
24;;;; This software is part of the SBCL system. See the README file for
25;;;; more information.
26
27;;;; This code was modified by William Harold Newman beginning
28;;;; 19981106, originally to conform to the new SBCL bootstrap package
29;;;; system and then subsequently to address other cross-compiling
30;;;; bootstrap issues, SBCLification (e.g. DECLARE used to check
31;;;; argument types), and other maintenance. Whether or not it then
32;;;; supported all the environments implied by the reader conditionals
33;;;; in the source code (e.g. #!+CLOE-RUNTIME) before that
34;;;; modification, it sure doesn't now. It might perhaps, by blind
35;;;; luck, be appropriate for some other CMU-CL-derived system, but
36;;;; really it only attempts to be appropriate for SBCL.
37
38;;;; This software is derived from software originally released by the
39;;;; Massachusetts Institute of Technology and Symbolics, Inc. Copyright and
40;;;; release statements follow. Later modifications to the software are in
41;;;; the public domain and are provided with absolutely no warranty. See the
42;;;; COPYING and CREDITS files for more information.
43
44;;;; Portions of LOOP are Copyright (c) 1986 by the Massachusetts Institute
45;;;; of Technology. All Rights Reserved.
46;;;;
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 M.I.T. copyright notice appear in all copies and that
50;;;; both that copyright notice and this permission notice appear in
51;;;; supporting documentation. The names "M.I.T." and "Massachusetts
52;;;; Institute of Technology" may not be used in advertising or publicity
53;;;; pertaining to distribution of the software without specific, written
54;;;; prior permission. Notice must be given in supporting documentation that
55;;;; copying distribution is by permission of M.I.T. M.I.T. makes no
56;;;; representations about the suitability of this software for any purpose.
57;;;; It is provided "as is" without express or implied warranty.
58;;;;
59;;;;      Massachusetts Institute of Technology
60;;;;      77 Massachusetts Avenue
61;;;;      Cambridge, Massachusetts  02139
62;;;;      United States of America
63;;;;      +1-617-253-1000
64
65;;;; Portions of LOOP are Copyright (c) 1989, 1990, 1991, 1992 by Symbolics,
66;;;; Inc. All Rights Reserved.
67;;;;
68;;;; Permission to use, copy, modify and distribute this software and its
69;;;; documentation for any purpose and without fee is hereby granted,
70;;;; provided that the Symbolics copyright notice appear in all copies and
71;;;; that both that copyright notice and this permission notice appear in
72;;;; supporting documentation. The name "Symbolics" may not be used in
73;;;; advertising or publicity pertaining to distribution of the software
74;;;; without specific, written prior permission. Notice must be given in
75;;;; supporting documentation that copying distribution is by permission of
76;;;; Symbolics. Symbolics makes no representations about the suitability of
77;;;; this software for any purpose. It is provided "as is" without express
78;;;; or implied warranty.
79;;;;
80;;;; Symbolics, CLOE Runtime, and Minima are trademarks, and CLOE, Genera,
81;;;; and Zetalisp are registered trademarks of Symbolics, Inc.
82;;;;
83;;;;      Symbolics, Inc.
84;;;;      8 New England Executive Park, East
85;;;;      Burlington, Massachusetts  01803
86;;;;      United States of America
87;;;;      +1-617-221-1000
88
89(in-package #:system)
90
91(defpackage "LOOP" (:use "COMMON-LISP"))
92
93(in-package "LOOP")
94
95;;;; The design of this LOOP is intended to permit, using mostly the same
96;;;; kernel of code, up to three different "loop" macros:
97;;;;
98;;;; (1) The unextended, unextensible ANSI standard LOOP;
99;;;;
100;;;; (2) A clean "superset" extension of the ANSI LOOP which provides
101;;;; functionality similar to that of the old LOOP, but "in the style of"
102;;;; the ANSI LOOP. For instance, user-definable iteration paths, with a
103;;;; somewhat cleaned-up interface.
104;;;;
105;;;; (3) Extensions provided in another file which can make this LOOP
106;;;; kernel behave largely compatibly with the Genera-vintage LOOP macro,
107;;;; with only a small addition of code (instead of two whole, separate,
108;;;; LOOP macros).
109;;;;
110;;;; Each of the above three LOOP variations can coexist in the same LISP
111;;;; environment.
112;;;;
113;;;; KLUDGE: In SBCL, we only really use variant (1), and any generality
114;;;; for the other variants is wasted. -- WHN 20000121
115
116;;;; FIXME: the STEP-FUNCTION stuff in the code seems to've been
117;;;; intended to support code which was conditionalized with
118;;;; LOOP-PREFER-POP (not true on CMU CL) and which has since been
119;;;; removed. Thus, STEP-FUNCTION stuff could probably be removed too.
120
121;;;; list collection macrology
122
123(defmacro with-loop-list-collection-head
124    ((head-var tail-var &optional user-head-var) &body body)
125  (let ((l (and user-head-var (list (list user-head-var nil)))))
126    `(let* ((,head-var (list nil)) (,tail-var ,head-var) ,@l)
127       ,@body)))
128
129(defmacro loop-collect-rplacd
130    (&environment env (head-var tail-var &optional user-head-var) form)
131  (setq form (macroexpand form env))
132  (flet ((cdr-wrap (form n)
133     (declare (fixnum n))
134     (do () ((<= n 4) (setq form `(,(case n
135              (1 'cdr)
136              (2 'cddr)
137              (3 'cdddr)
138              (4 'cddddr))
139           ,form)))
140       (setq form `(cddddr ,form) n (- n 4)))))
141    (let ((tail-form form) (ncdrs nil))
142      ;; Determine whether the form being constructed is a list of known
143      ;; length.
144      (when (consp form)
145  (cond ((eq (car form) 'list)
146         (setq ncdrs (1- (length (cdr form)))))
147        ((member (car form) '(list* cons))
148         (when (and (cddr form) (member (car (last form)) '(nil 'nil)))
149     (setq ncdrs (- (length (cdr form)) 2))))))
150      (let ((answer
151        (cond ((null ncdrs)
152         `(when (setf (cdr ,tail-var) ,tail-form)
153      (setq ,tail-var (last (cdr ,tail-var)))))
154        ((< ncdrs 0) (return-from loop-collect-rplacd nil))
155        ((= ncdrs 0)
156         ;; @@@@ Here we have a choice of two idioms:
157         ;;   (RPLACD TAIL (SETQ TAIL TAIL-FORM))
158         ;;   (SETQ TAIL (SETF (CDR TAIL) TAIL-FORM)).
159         ;; Genera and most others I have seen do better with the
160         ;; former.
161         `(rplacd ,tail-var (setq ,tail-var ,tail-form)))
162        (t `(setq ,tail-var ,(cdr-wrap `(setf (cdr ,tail-var)
163                ,tail-form)
164               ncdrs))))))
165  ;; If not using locatives or something similar to update the
166  ;; user's head variable, we've got to set it... It's harmless
167  ;; to repeatedly set it unconditionally, and probably faster
168  ;; than checking.
169  (when user-head-var
170    (setq answer
171    `(progn ,answer
172      (setq ,user-head-var (cdr ,head-var)))))
173  answer))))
174
175(defmacro loop-collect-answer (head-var
176               &optional user-head-var)
177  (or user-head-var
178      `(cdr ,head-var)))
179
180;;;; maximization technology
181
182#|
183The basic idea of all this minimax randomness here is that we have to
184have constructed all uses of maximize and minimize to a particular
185"destination" before we can decide how to code them. The goal is to not
186have to have any kinds of flags, by knowing both that (1) the type is
187something which we can provide an initial minimum or maximum value for
188and (2) know that a MAXIMIZE and MINIMIZE are not being combined.
189
190SO, we have a datastructure which we annotate with all sorts of things,
191incrementally updating it as we generate loop body code, and then use
192a wrapper and internal macros to do the coding when the loop has been
193constructed.
194|#
195
196(defstruct (loop-minimax
197       (:constructor make-loop-minimax-internal)
198       (:copier nil)
199       (:predicate nil))
200  answer-variable
201  type
202  temp-variable
203  flag-variable
204  operations
205  infinity-data)
206
207(defvar *loop-minimax-type-infinities-alist*
208  ;; FIXME: Now that SBCL supports floating point infinities again, we
209  ;; should have floating point infinities here, as cmucl-2.4.8 did.
210  '((fixnum most-positive-fixnum most-negative-fixnum)))
211
212(defun make-loop-minimax (answer-variable type)
213  (let ((infinity-data (cdr (assoc type
214           *loop-minimax-type-infinities-alist*
215           :test #'subtypep))))
216    (make-loop-minimax-internal
217      :answer-variable answer-variable
218      :type type
219      :temp-variable (gensym "LOOP-MAXMIN-TEMP-")
220      :flag-variable (and (not infinity-data)
221        (gensym "LOOP-MAXMIN-FLAG-"))
222      :operations nil
223      :infinity-data infinity-data)))
224
225(defun loop-note-minimax-operation (operation minimax)
226  (pushnew (the symbol operation) (loop-minimax-operations minimax))
227  (when (and (cdr (loop-minimax-operations minimax))
228       (not (loop-minimax-flag-variable minimax)))
229    (setf (loop-minimax-flag-variable minimax)
230    (gensym "LOOP-MAXMIN-FLAG-")))
231  operation)
232
233(defmacro with-minimax-value (lm &body body)
234  (let ((init (loop-typed-init (loop-minimax-type lm)))
235  (which (car (loop-minimax-operations lm)))
236  (infinity-data (loop-minimax-infinity-data lm))
237  (answer-var (loop-minimax-answer-variable lm))
238  (temp-var (loop-minimax-temp-variable lm))
239  (flag-var (loop-minimax-flag-variable lm))
240  (type (loop-minimax-type lm)))
241    (if flag-var
242  `(let ((,answer-var ,init) (,temp-var ,init) (,flag-var nil))
243     (declare (type ,type ,answer-var ,temp-var))
244     ,@body)
245  `(let ((,answer-var ,(if (eq which 'min)
246         (first infinity-data)
247         (second infinity-data)))
248         (,temp-var ,init))
249     (declare (type ,type ,answer-var ,temp-var))
250     ,@body))))
251
252(defmacro loop-accumulate-minimax-value (lm operation form)
253  (let* ((answer-var (loop-minimax-answer-variable lm))
254   (temp-var (loop-minimax-temp-variable lm))
255   (flag-var (loop-minimax-flag-variable lm))
256   (test `(,(ecase operation
257        (min '<)
258        (max '>))
259     ,temp-var ,answer-var)))
260    `(progn
261       (setq ,temp-var ,form)
262       (when ,(if flag-var `(or (not ,flag-var) ,test) test)
263   (setq ,@(and flag-var `(,flag-var t))
264         ,answer-var ,temp-var)))))
265
266;;;; LOOP keyword tables
267
268#|
269LOOP keyword tables are hash tables string keys and a test of EQUAL.
270
271The actual descriptive/dispatch structure used by LOOP is called a "loop
272universe" contains a few tables and parameterizations. The basic idea is
273that we can provide a non-extensible ANSI-compatible loop environment,
274an extensible ANSI-superset loop environment, and (for such environments
275as CLOE) one which is "sufficiently close" to the old Genera-vintage
276LOOP for use by old user programs without requiring all of the old LOOP
277code to be loaded.
278|#
279
280;;;; token hackery
281
282;;; Compare two "tokens". The first is the frob out of *LOOP-SOURCE-CODE*,
283;;; the second a symbol to check against.
284(defun loop-tequal (x1 x2)
285  (and (symbolp x1) (string= x1 x2)))
286
287(defun loop-tassoc (kwd alist)
288  (and (symbolp kwd) (assoc kwd alist :test #'string=)))
289
290(defun loop-tmember (kwd list)
291  (and (symbolp kwd) (member kwd list :test #'string=)))
292
293(defun loop-lookup-keyword (loop-token table)
294  (and (symbolp loop-token)
295       (values (gethash (symbol-name (the symbol loop-token)) table))))
296
297(defmacro loop-store-table-data (symbol table datum)
298  `(setf (gethash (symbol-name ,symbol) ,table) ,datum))
299
300(defstruct (loop-universe
301       (:copier nil)
302       (:predicate nil))
303  keywords             ; hash table, value = (fn-name . extra-data)
304  iteration-keywords   ; hash table, value = (fn-name . extra-data)
305  for-keywords         ; hash table, value = (fn-name . extra-data)
306  path-keywords        ; hash table, value = (fn-name . extra-data)
307  type-symbols         ; hash table of type SYMBOLS, test EQ,
308                       ; value = CL type specifier
309  type-keywords        ; hash table of type STRINGS, test EQUAL,
310                       ; value = CL type spec
311  ansi                 ; NIL, T, or :EXTENDED
312  implicit-for-required) ; see loop-hack-iteration
313
314#+sbcl
315(sb!int:def!method print-object ((u loop-universe) stream)
316  (let ((string (case (loop-universe-ansi u)
317      ((nil) "non-ANSI")
318      ((t) "ANSI")
319      (:extended "extended-ANSI")
320      (t (loop-universe-ansi u)))))
321    (print-unreadable-object (u stream :type t)
322      (write-string string stream))))
323
324;;; This is the "current" loop context in use when we are expanding a
325;;; loop. It gets bound on each invocation of LOOP.
326(defvar *loop-universe*)
327
328(defun make-standard-loop-universe (&key keywords for-keywords
329           iteration-keywords path-keywords
330           type-keywords type-symbols ansi)
331  (declare (type (member nil t :extended) ansi))
332  (flet ((maketable (entries)
333     (let* ((size (length entries))
334      (ht (make-hash-table :size (if (< size 10) 10 size)
335               :test 'equal)))
336       (dolist (x entries)
337         (setf (gethash (symbol-name (car x)) ht) (cadr x)))
338       ht)))
339    (make-loop-universe
340      :keywords (maketable keywords)
341      :for-keywords (maketable for-keywords)
342      :iteration-keywords (maketable iteration-keywords)
343      :path-keywords (maketable path-keywords)
344      :ansi ansi
345      :implicit-for-required (not (null ansi))
346      :type-keywords (maketable type-keywords)
347      :type-symbols (let* ((size (length type-symbols))
348         (ht (make-hash-table :size (if (< size 10) 10 size)
349            :test 'eq)))
350          (dolist (x type-symbols)
351      (if (atom x)
352          (setf (gethash x ht) x)
353          (setf (gethash (car x) ht) (cadr x))))
354          ht))))
355
356;;;; SETQ hackery, including destructuring ("DESETQ")
357
358(defun loop-make-psetq (frobs)
359  (and frobs
360       (loop-make-desetq
361   (list (car frobs)
362         (if (null (cddr frobs)) (cadr frobs)
363       `(prog1 ,(cadr frobs)
364         ,(loop-make-psetq (cddr frobs))))))))
365
366(defun loop-make-desetq (var-val-pairs)
367  (if (null var-val-pairs)
368      nil
369      (cons 'loop-really-desetq var-val-pairs)))
370
371(defvar *loop-desetq-temporary*
372  (make-symbol "LOOP-DESETQ-TEMP"))
373
374(defmacro loop-really-desetq (&environment env
375                       &rest var-val-pairs)
376  (labels ((find-non-null (var)
377       ;; See whether there's any non-null thing here. Recurse
378       ;; if the list element is itself a list.
379       (do ((tail var)) ((not (consp tail)) tail)
380         (when (find-non-null (pop tail)) (return t))))
381     (loop-desetq-internal (var val &optional temp)
382       ;; returns a list of actions to be performed
383       (typecase var
384         (null
385     (when (consp val)
386       ;; Don't lose possible side effects.
387       (if (eq (car val) 'prog1)
388           ;; These can come from PSETQ or DESETQ below.
389           ;; Throw away the value, keep the side effects.
390           ;; Special case is for handling an expanded POP.
391           (mapcan (lambda (x)
392         (and (consp x)
393              (or (not (eq (car x) 'car))
394            (not (symbolp (cadr x)))
395            (not (symbolp (setq x (macroexpand x env)))))
396              (cons x nil)))
397             (cdr val))
398           `(,val))))
399         (cons
400     (let* ((car (car var))
401      (cdr (cdr var))
402      (car-non-null (find-non-null car))
403      (cdr-non-null (find-non-null cdr)))
404       (when (or car-non-null cdr-non-null)
405         (if cdr-non-null
406       (let* ((temp-p temp)
407        (temp (or temp *loop-desetq-temporary*))
408        (body `(,@(loop-desetq-internal car
409                `(car ,temp))
410            (setq ,temp (cdr ,temp))
411            ,@(loop-desetq-internal cdr
412                  temp
413                  temp))))
414         (if temp-p
415             `(,@(unless (eq temp val)
416             `((setq ,temp ,val)))
417         ,@body)
418             `((let ((,temp ,val))
419           ,@body))))
420       ;; no CDRing to do
421       (loop-desetq-internal car `(car ,val) temp)))))
422         (otherwise
423     (unless (eq var val)
424       `((setq ,var ,val)))))))
425    (do ((actions))
426  ((null var-val-pairs)
427   (if (null (cdr actions)) (car actions) `(progn ,@(nreverse actions))))
428      (setq actions (revappend
429          (loop-desetq-internal (pop var-val-pairs)
430              (pop var-val-pairs))
431          actions)))))
432
433;;;; LOOP-local variables
434
435;;; This is the "current" pointer into the LOOP source code.
436(defvar *loop-source-code*)
437
438;;; This is the pointer to the original, for things like NAMED that
439;;; insist on being in a particular position
440(defvar *loop-original-source-code*)
441
442;;; This is *loop-source-code* as of the "last" clause. It is used
443;;; primarily for generating error messages (see loop-error, loop-warn).
444(defvar *loop-source-context*)
445
446;;; list of names for the LOOP, supplied by the NAMED clause
447(defvar *loop-names*)
448
449;;; The macroexpansion environment given to the macro.
450(defvar *loop-macro-environment*)
451
452;;; This holds variable names specified with the USING clause.
453;;; See LOOP-NAMED-VAR.
454(defvar *loop-named-vars*)
455
456;;; LETlist-like list being accumulated for one group of parallel bindings.
457(defvar *loop-vars*)
458
459;;; list of declarations being accumulated in parallel with *LOOP-VARS*
460(defvar *loop-declarations*)
461
462;;; This is used by LOOP for destructuring binding, if it is doing
463;;; that itself. See LOOP-MAKE-VAR.
464(defvar *loop-desetq-crocks*)
465
466;;; list of wrapping forms, innermost first, which go immediately
467;;; inside the current set of parallel bindings being accumulated in
468;;; *LOOP-VARS*. The wrappers are appended onto a body. E.g.,
469;;; this list could conceivably have as its value
470;;;   ((WITH-OPEN-FILE (G0001 G0002 ...))),
471;;; with G0002 being one of the bindings in *LOOP-VARS* (This is
472;;; why the wrappers go inside of the variable bindings).
473(defvar *loop-wrappers*)
474
475;;; This accumulates lists of previous values of *LOOP-VARS* and
476;;; the other lists above, for each new nesting of bindings. See
477;;; LOOP-BIND-BLOCK.
478(defvar *loop-bind-stack*)
479
480;;; This is simply a list of LOOP iteration variables, used for
481;;; checking for duplications.
482(defvar *loop-iteration-vars*)
483
484;;; list of prologue forms of the loop, accumulated in reverse order
485(defvar *loop-prologue*)
486
487(defvar *loop-before-loop*)
488(defvar *loop-body*)
489(defvar *loop-after-body*)
490
491;;; This is T if we have emitted any body code, so that iteration
492;;; driving clauses can be disallowed. This is not strictly the same
493;;; as checking *LOOP-BODY*, because we permit some clauses such as
494;;; RETURN to not be considered "real" body (so as to permit the user
495;;; to "code" an abnormal return value "in loop").
496(defvar *loop-emitted-body*)
497
498;;; list of epilogue forms (supplied by FINALLY generally), accumulated
499;;; in reverse order
500(defvar *loop-epilogue*)
501
502;;; list of epilogue forms which are supplied after the above "user"
503;;; epilogue. "Normal" termination return values are provide by
504;;; putting the return form in here. Normally this is done using
505;;; LOOP-EMIT-FINAL-VALUE, q.v.
506(defvar *loop-after-epilogue*)
507
508;;; the "culprit" responsible for supplying a final value from the
509;;; loop. This is so LOOP-EMIT-FINAL-VALUE can moan about multiple
510;;; return values being supplied.
511(defvar *loop-final-value-culprit*)
512
513;;; If this is true, we are in some branch of a conditional. Some
514;;; clauses may be disallowed.
515(defvar *loop-inside-conditional*)
516
517;;; If not NIL, this is a temporary bound around the loop for holding
518;;; the temporary value for "it" in things like "when (f) collect it".
519;;; It may be used as a supertemporary by some other things.
520(defvar *loop-when-it-var*)
521
522;;; Sometimes we decide we need to fold together parts of the loop,
523;;; but some part of the generated iteration code is different for the
524;;; first and remaining iterations. This variable will be the
525;;; temporary which is the flag used in the loop to tell whether we
526;;; are in the first or remaining iterations.
527(defvar *loop-never-stepped-var*)
528
529;;; list of all the value-accumulation descriptor structures in the
530;;; loop. See LOOP-GET-COLLECTION-INFO.
531(defvar *loop-collection-cruft*) ; for multiple COLLECTs (etc.)
532
533;;;; code analysis stuff
534
535(defun loop-constant-fold-if-possible (form &optional expected-type)
536  (let ((new-form form) (constantp nil) (constant-value nil))
537    (when (setq constantp (constantp new-form))
538      (setq constant-value (eval new-form)))
539    (when (and constantp expected-type)
540      (unless (typep constant-value expected-type)
541  (loop-warn "~@<The form ~S evaluated to ~S, which was not of ~
542                    the anticipated type ~S.~:@>"
543       form constant-value expected-type)
544  (setq constantp nil constant-value nil)))
545    (values new-form constantp constant-value)))
546
547(defun loop-constantp (form)
548  (constantp form))
549
550;;;; LOOP iteration optimization
551
552(defvar *loop-duplicate-code*
553  nil)
554
555(defvar *loop-iteration-flag-var*
556  (make-symbol "LOOP-NOT-FIRST-TIME"))
557
558(defun loop-code-duplication-threshold (env)
559  (declare (ignore env))
560  (let (;; If we could read optimization declaration information (as
561  ;; with the DECLARATION-INFORMATION function (present in
562  ;; CLTL2, removed from ANSI standard) we could set these
563  ;; values flexibly. Without DECLARATION-INFORMATION, we have
564  ;; to set them to constants.
565  ;;
566  ;; except FIXME: we've lost all pretence of portability,
567  ;; considering this instead an internal implementation, so
568  ;; we're free to couple to our own representation of the
569  ;; environment.
570  (speed 1)
571  (space 1))
572    (+ 40 (* (- speed space) 10))))
573
574(defmacro loop-body (&environment env
575                                  prologue
576                                  before-loop
577                                  main-body
578                                  after-loop
579                                  epilogue
580                     &aux rbefore rafter flagvar)
581  (unless (= (length before-loop) (length after-loop))
582    (error "LOOP-BODY called with non-synched before- and after-loop lists"))
583  ;;All our work is done from these copies, working backwards from the end:
584  (setq rbefore (reverse before-loop) rafter (reverse after-loop))
585  (labels ((psimp (l)
586       (let ((ans nil))
587         (dolist (x l)
588     (when x
589       (push x ans)
590       (when (and (consp x)
591            (member (car x) '(go return return-from)))
592         (return nil))))
593         (nreverse ans)))
594     (pify (l) (if (null (cdr l)) (car l) `(progn ,@l)))
595     (makebody ()
596       (let ((form `(tagbody
597          ,@(psimp (append prologue (nreverse rbefore)))
598       next-loop
599          ,@(psimp (append main-body
600               (nreconc rafter
601                  `((go next-loop)))))
602       end-loop
603          ,@(psimp epilogue))))
604         (if flagvar `(let ((,flagvar nil)) ,form) form))))
605    (when (or *loop-duplicate-code* (not rbefore))
606      (return-from loop-body (makebody)))
607    ;; This outer loop iterates once for each not-first-time flag test
608    ;; generated plus once more for the forms that don't need a flag test.
609    (do ((threshold (loop-code-duplication-threshold env))) (nil)
610      (declare (fixnum threshold))
611      ;; Go backwards from the ends of before-loop and after-loop
612      ;; merging all the equivalent forms into the body.
613      (do () ((or (null rbefore) (not (equal (car rbefore) (car rafter)))))
614  (push (pop rbefore) main-body)
615  (pop rafter))
616      (unless rbefore (return (makebody)))
617      ;; The first forms in RBEFORE & RAFTER (which are the
618      ;; chronologically last forms in the list) differ, therefore
619      ;; they cannot be moved into the main body. If everything that
620      ;; chronologically precedes them either differs or is equal but
621      ;; is okay to duplicate, we can just put all of rbefore in the
622      ;; prologue and all of rafter after the body. Otherwise, there
623      ;; is something that is not okay to duplicate, so it and
624      ;; everything chronologically after it in rbefore and rafter
625      ;; must go into the body, with a flag test to distinguish the
626      ;; first time around the loop from later times. What
627      ;; chronologically precedes the non-duplicatable form will be
628      ;; handled the next time around the outer loop.
629      (do ((bb rbefore (cdr bb))
630     (aa rafter (cdr aa))
631     (lastdiff nil)
632     (count 0)
633     (inc nil))
634    ((null bb) (return-from loop-body (makebody)))  ; Did it.
635  (cond ((not (equal (car bb) (car aa))) (setq lastdiff bb count 0))
636        ((or (not (setq inc (estimate-code-size (car bb) env)))
637       (> (incf count inc) threshold))
638         ;; Ok, we have found a non-duplicatable piece of code.
639         ;; Everything chronologically after it must be in the
640         ;; central body. Everything chronologically at and
641         ;; after LASTDIFF goes into the central body under a
642         ;; flag test.
643         (let ((then nil) (else nil))
644     (do () (nil)
645       (push (pop rbefore) else)
646       (push (pop rafter) then)
647       (when (eq rbefore (cdr lastdiff)) (return)))
648     (unless flagvar
649       (push `(setq ,(setq flagvar *loop-iteration-flag-var*)
650        t)
651       else))
652     (push `(if ,flagvar ,(pify (psimp then)) ,(pify (psimp else)))
653           main-body))
654         ;; Everything chronologically before lastdiff until the
655         ;; non-duplicatable form (CAR BB) is the same in
656         ;; RBEFORE and RAFTER, so just copy it into the body.
657         (do () (nil)
658     (pop rafter)
659     (push (pop rbefore) main-body)
660     (when (eq rbefore (cdr bb)) (return)))
661         (return)))))))
662
663(defun duplicatable-code-p (expr env)
664  (if (null expr) 0
665      (let ((ans (estimate-code-size expr env)))
666  (declare (fixnum ans))
667  ;; @@@@ Use (DECLARATION-INFORMATION 'OPTIMIZE ENV) here to
668  ;; get an alist of optimize quantities back to help quantify
669  ;; how much code we are willing to duplicate.
670  ans)))
671
672(defvar *special-code-sizes*
673  '((return 0) (progn 0)
674    (null 1) (not 1) (eq 1) (car 1) (cdr 1)
675    (when 1) (unless 1) (if 1)
676    (caar 2) (cadr 2) (cdar 2) (cddr 2)
677    (caaar 3) (caadr 3) (cadar 3) (caddr 3)
678    (cdaar 3) (cdadr 3) (cddar 3) (cdddr 3)
679    (caaaar 4) (caaadr 4) (caadar 4) (caaddr 4)
680    (cadaar 4) (cadadr 4) (caddar 4) (cadddr 4)
681    (cdaaar 4) (cdaadr 4) (cdadar 4) (cdaddr 4)
682    (cddaar 4) (cddadr 4) (cdddar 4) (cddddr 4)))
683
684(defvar *estimate-code-size-punt*
685  '(block
686     do do* dolist
687     flet
688     labels lambda let let* locally
689     macrolet multiple-value-bind
690     prog prog*
691     symbol-macrolet
692     tagbody
693     unwind-protect
694     with-open-file))
695
696(defun destructuring-size (x)
697  (do ((x x (cdr x)) (n 0 (+ (destructuring-size (car x)) n)))
698      ((atom x) (+ n (if (null x) 0 1)))))
699
700(defun estimate-code-size (x env)
701  (catch 'estimate-code-size
702    (estimate-code-size-1 x env)))
703
704(defun estimate-code-size-1 (x env)
705  (flet ((list-size (l)
706     (let ((n 0))
707       (declare (fixnum n))
708       (dolist (x l n) (incf n (estimate-code-size-1 x env))))))
709    ;;@@@@ ???? (declare (function list-size (list) fixnum))
710    (cond ((constantp x) 1)
711    ((symbolp x) (multiple-value-bind (new-form expanded-p)
712         (macroexpand-1 x env)
713       (if expanded-p
714           (estimate-code-size-1 new-form env)
715           1)))
716    ((atom x) 1) ;; ??? self-evaluating???
717    ((symbolp (car x))
718     (let ((fn (car x)) (tem nil) (n 0))
719       (declare (symbol fn) (fixnum n))
720       (macrolet ((f (overhead &optional (args nil args-p))
721        `(the fixnum (+ (the fixnum ,overhead)
722            (the fixnum
723                 (list-size ,(if args-p
724                     args
725                   '(cdr x))))))))
726         (cond ((setq tem (get fn 'estimate-code-size))
727          (typecase tem
728      (fixnum (f tem))
729      (t (funcall tem x env))))
730         ((setq tem (assoc fn *special-code-sizes*))
731          (f (second tem)))
732         ((eq fn 'cond)
733          (dolist (clause (cdr x) n)
734      (incf n (list-size clause)) (incf n)))
735         ((eq fn 'desetq)
736          (do ((l (cdr x) (cdr l))) ((null l) n)
737      (setq n (+ n
738           (destructuring-size (car l))
739           (estimate-code-size-1 (cadr l) env)))))
740         ((member fn '(setq psetq))
741          (do ((l (cdr x) (cdr l))) ((null l) n)
742      (setq n (+ n (estimate-code-size-1 (cadr l) env) 1))))
743         ((eq fn 'go) 1)
744         ((eq fn 'function)
745          (if #+sbcl
746                          (sb!int:legal-fun-name-p (cadr x))
747                          #+armedbear
748                          (or (symbolp (cadr x))
749            (and (consp (cadr x)) (eq (caadr x) 'setf)))
750        1
751        ;; FIXME: This tag appears not to be present
752        ;; anywhere.
753        (throw 'duplicatable-code-p nil)))
754         ((eq fn 'multiple-value-setq)
755          (f (length (second x)) (cddr x)))
756         ((eq fn 'return-from)
757          (1+ (estimate-code-size-1 (third x) env)))
758         ((or (special-operator-p fn)
759        (member fn *estimate-code-size-punt*))
760          (throw 'estimate-code-size nil))
761         (t (multiple-value-bind (new-form expanded-p)
762          (macroexpand-1 x env)
763        (if expanded-p
764            (estimate-code-size-1 new-form env)
765            (f 3))))))))
766    (t (throw 'estimate-code-size nil)))))
767
768;;;; loop errors
769
770(defun loop-context ()
771  (do ((l *loop-source-context* (cdr l)) (new nil (cons (car l) new)))
772      ((eq l (cdr *loop-source-code*)) (nreverse new))))
773
774(defun loop-error (format-string &rest format-args)
775  (error 'program-error
776   :format-control "~?~%Current LOOP context:~{ ~S~}."
777   :format-arguments (list format-string format-args (loop-context))))
778
779(defun loop-warn (format-string &rest format-args)
780  (warn "~?~%Current LOOP context:~{ ~S~}."
781  format-string
782  format-args
783  (loop-context)))
784
785(defun loop-check-data-type (specified-type required-type
786           &optional (default-type required-type))
787  (if (null specified-type)
788      default-type
789      (multiple-value-bind (a b) (subtypep specified-type required-type)
790  (cond ((not b)
791         (loop-warn "LOOP couldn't verify that ~S is a subtype of the required type ~S."
792        specified-type required-type))
793        ((not a)
794         (loop-error "The specified data type ~S is not a subtype of ~S."
795         specified-type required-type)))
796  specified-type)))
797
798(defun subst-gensyms-for-nil (tree)
799  (declare (special *ignores*))
800  (cond
801    ((null tree)
802     (car (push (gensym "LOOP-IGNORED-VAR-") *ignores*)))
803    ((atom tree)
804     tree)
805    (t
806     (cons (subst-gensyms-for-nil (car tree))
807           (subst-gensyms-for-nil (cdr tree))))))
808
809(defmacro loop-destructuring-bind
810    (lambda-list arg-list &rest body)
811  (let ((*ignores* nil))
812    (declare (special *ignores*))
813    (let ((d-var-lambda-list (subst-gensyms-for-nil lambda-list)))
814      `(destructuring-bind ,d-var-lambda-list
815     ,arg-list
816   (declare (ignore ,@*ignores*))
817         ,@body))))
818
819(defun loop-build-destructuring-bindings (crocks forms)
820  (if crocks
821      `((loop-destructuring-bind ,(car crocks) ,(cadr crocks)
822        ,@(loop-build-destructuring-bindings (cddr crocks) forms)))
823      forms))
824
825(defun loop-translate (*loop-source-code*
826           *loop-macro-environment*
827           *loop-universe*)
828  (let ((*loop-original-source-code* *loop-source-code*)
829  (*loop-source-context* nil)
830  (*loop-iteration-vars* nil)
831  (*loop-vars* nil)
832  (*loop-named-vars* nil)
833  (*loop-declarations* nil)
834  (*loop-desetq-crocks* nil)
835  (*loop-bind-stack* nil)
836  (*loop-prologue* nil)
837  (*loop-wrappers* nil)
838  (*loop-before-loop* nil)
839  (*loop-body* nil)
840  (*loop-emitted-body* nil)
841  (*loop-after-body* nil)
842  (*loop-epilogue* nil)
843  (*loop-after-epilogue* nil)
844  (*loop-final-value-culprit* nil)
845  (*loop-inside-conditional* nil)
846  (*loop-when-it-var* nil)
847  (*loop-never-stepped-var* nil)
848  (*loop-names* nil)
849  (*loop-collection-cruft* nil))
850    (loop-iteration-driver)
851    (loop-bind-block)
852    (let ((answer `(loop-body
853         ,(nreverse *loop-prologue*)
854         ,(nreverse *loop-before-loop*)
855         ,(nreverse *loop-body*)
856         ,(nreverse *loop-after-body*)
857         ,(nreconc *loop-epilogue*
858             (nreverse *loop-after-epilogue*)))))
859      (dolist (entry *loop-bind-stack*)
860  (let ((vars (first entry))
861        (dcls (second entry))
862        (crocks (third entry))
863        (wrappers (fourth entry)))
864    (dolist (w wrappers)
865      (setq answer (append w (list answer))))
866    (when (or vars dcls crocks)
867      (let ((forms (list answer)))
868        ;;(when crocks (push crocks forms))
869        (when dcls (push `(declare ,@dcls) forms))
870        (setq answer `(,(if vars 'let 'locally)
871           ,vars
872           ,@(loop-build-destructuring-bindings crocks
873                  forms)))))))
874      (do () (nil)
875  (setq answer `(block ,(pop *loop-names*) ,answer))
876  (unless *loop-names* (return nil)))
877      answer)))
878
879(defun loop-iteration-driver ()
880  (do () ((null *loop-source-code*))
881    (let ((keyword (car *loop-source-code*)) (tem nil))
882      (cond ((not (symbolp keyword))
883       (loop-error "~S found where LOOP keyword expected" keyword))
884      (t (setq *loop-source-context* *loop-source-code*)
885         (loop-pop-source)
886         (cond ((setq tem
887          (loop-lookup-keyword keyword
888             (loop-universe-keywords
889              *loop-universe*)))
890          ;; It's a "miscellaneous" toplevel LOOP keyword (DO,
891          ;; COLLECT, NAMED, etc.)
892          (apply (symbol-function (first tem)) (rest tem)))
893         ((setq tem
894          (loop-lookup-keyword keyword
895             (loop-universe-iteration-keywords *loop-universe*)))
896          (loop-hack-iteration tem))
897         ((loop-tmember keyword '(and else))
898          ;; The alternative is to ignore it, i.e. let it go
899          ;; around to the next keyword...
900          (loop-error "secondary clause misplaced at top level in LOOP macro: ~S ~S ~S ..."
901          keyword
902          (car *loop-source-code*)
903          (cadr *loop-source-code*)))
904         (t (loop-error "unknown LOOP keyword: ~S" keyword))))))))
905
906(defun loop-pop-source ()
907  (if *loop-source-code*
908      (pop *loop-source-code*)
909      (loop-error "LOOP source code ran out when another token was expected.")))
910
911(defun loop-get-form ()
912  (if *loop-source-code*
913      (loop-pop-source)
914      (loop-error "LOOP code ran out where a form was expected.")))
915
916(defun loop-get-compound-form ()
917  (let ((form (loop-get-form)))
918    (unless (consp form)
919      (loop-error "A compound form was expected, but ~S found." form))
920    form))
921
922(defun loop-get-progn ()
923  (do ((forms (list (loop-get-compound-form))
924              (cons (loop-get-compound-form) forms))
925       (nextform (car *loop-source-code*)
926                 (car *loop-source-code*)))
927      ((atom nextform)
928       (if (null (cdr forms)) (car forms) (cons 'progn (nreverse forms))))))
929
930(defun loop-construct-return (form)
931  `(return-from ,(car *loop-names*) ,form))
932
933(defun loop-pseudo-body (form)
934  (cond ((or *loop-emitted-body* *loop-inside-conditional*)
935   (push form *loop-body*))
936  (t (push form *loop-before-loop*) (push form *loop-after-body*))))
937
938(defun loop-emit-body (form)
939  (setq *loop-emitted-body* t)
940  (loop-pseudo-body form))
941
942(defun loop-emit-final-value (&optional (form nil form-supplied-p))
943  (when form-supplied-p
944    (push (loop-construct-return form) *loop-after-epilogue*))
945  (when *loop-final-value-culprit*
946    (loop-warn "The LOOP clause is providing a value for the iteration;~@
947    however, one was already established by a ~S clause."
948         *loop-final-value-culprit*))
949  (setq *loop-final-value-culprit* (car *loop-source-context*)))
950
951(defun loop-disallow-conditional (&optional kwd)
952  (when *loop-inside-conditional*
953    (loop-error "~:[This LOOP~;The LOOP ~:*~S~] clause is not permitted inside a conditional." kwd)))
954
955(defun loop-disallow-anonymous-collectors ()
956  (when (find-if-not 'loop-collector-name *loop-collection-cruft*)
957    (loop-error "This LOOP clause is not permitted with anonymous collectors.")))
958
959(defun loop-disallow-aggregate-booleans ()
960  (when (loop-tmember *loop-final-value-culprit* '(always never thereis))
961    (loop-error "This anonymous collection LOOP clause is not permitted with aggregate booleans.")))
962
963;;;; loop types
964
965(defun loop-typed-init (data-type &optional step-var-p)
966  (when (and data-type (subtypep data-type 'number))
967    (if (or (subtypep data-type 'float)
968      (subtypep data-type '(complex float)))
969  (coerce (if step-var-p 1 0) data-type)
970  (if step-var-p 1 0))))
971
972(defun loop-optional-type (&optional variable)
973  ;; No variable specified implies that no destructuring is permissible.
974  (and *loop-source-code* ; Don't get confused by NILs..
975       (let ((z (car *loop-source-code*)))
976   (cond ((loop-tequal z 'of-type)
977    ;; This is the syntactically unambigous form in that
978    ;; the form of the type specifier does not matter.
979    ;; Also, it is assumed that the type specifier is
980    ;; unambiguously, and without need of translation, a
981    ;; common lisp type specifier or pattern (matching the
982    ;; variable) thereof.
983    (loop-pop-source)
984    (loop-pop-source))
985
986         ((symbolp z)
987    ;; This is the (sort of) "old" syntax, even though we
988    ;; didn't used to support all of these type symbols.
989    (let ((type-spec (or (gethash z
990                (loop-universe-type-symbols
991                 *loop-universe*))
992             (gethash (symbol-name z)
993                (loop-universe-type-keywords
994                 *loop-universe*)))))
995      (when type-spec
996        (loop-pop-source)
997        type-spec)))
998         (t
999    ;; This is our sort-of old syntax. But this is only
1000    ;; valid for when we are destructuring, so we will be
1001    ;; compulsive (should we really be?) and require that
1002    ;; we in fact be doing variable destructuring here. We
1003    ;; must translate the old keyword pattern typespec
1004    ;; into a fully-specified pattern of real type
1005    ;; specifiers here.
1006    (if (consp variable)
1007        (unless (consp z)
1008         (loop-error
1009      "~S found where a LOOP keyword, LOOP type keyword, or LOOP type pattern expected"
1010      z))
1011        (loop-error "~S found where a LOOP keyword or LOOP type keyword expected" z))
1012    (loop-pop-source)
1013    (labels ((translate (k v)
1014         (cond ((null k) nil)
1015         ((atom k)
1016          (replicate
1017            (or (gethash k
1018             (loop-universe-type-symbols
1019              *loop-universe*))
1020          (gethash (symbol-name k)
1021             (loop-universe-type-keywords
1022              *loop-universe*))
1023          (loop-error
1024            "The destructuring type pattern ~S contains the unrecognized type keyword ~S."
1025            z k))
1026            v))
1027         ((atom v)
1028          (loop-error
1029            "The destructuring type pattern ~S doesn't match the variable pattern ~S."
1030            z variable))
1031         (t (cons (translate (car k) (car v))
1032            (translate (cdr k) (cdr v))))))
1033       (replicate (typ v)
1034         (if (atom v)
1035             typ
1036             (cons (replicate typ (car v))
1037             (replicate typ (cdr v))))))
1038      (translate z variable)))))))
1039
1040;;;; loop variables
1041
1042(defun loop-bind-block ()
1043  (when (or *loop-vars* *loop-declarations* *loop-wrappers*)
1044    (push (list (nreverse *loop-vars*)
1045    *loop-declarations*
1046    *loop-desetq-crocks*
1047    *loop-wrappers*)
1048    *loop-bind-stack*)
1049    (setq *loop-vars* nil
1050    *loop-declarations* nil
1051    *loop-desetq-crocks* nil
1052    *loop-wrappers* nil)))
1053
1054(defun loop-var-p (name)
1055  (do ((entry *loop-bind-stack* (cdr entry)))
1056      (nil)
1057    (cond
1058      ((null entry) (return nil))
1059      ((assoc name (caar entry) :test #'eq) (return t)))))
1060
1061(defun loop-make-var (name initialization dtype &optional iteration-var-p step-var-p)
1062  (cond ((null name)
1063   (setq name (gensym "LOOP-IGNORE-"))
1064   (push (list name initialization) *loop-vars*)
1065   (if (null initialization)
1066       (push `(ignore ,name) *loop-declarations*)
1067       (loop-declare-var name dtype)))
1068  ((atom name)
1069   (cond (iteration-var-p
1070    (if (member name *loop-iteration-vars*)
1071        (loop-error "duplicated LOOP iteration variable ~S" name)
1072        (push name *loop-iteration-vars*)))
1073         ((assoc name *loop-vars*)
1074    (loop-error "duplicated variable ~S in LOOP parallel binding"
1075          name)))
1076   (unless (symbolp name)
1077     (loop-error "bad variable ~S somewhere in LOOP" name))
1078   (loop-declare-var name dtype step-var-p)
1079   ;; We use ASSOC on this list to check for duplications (above),
1080   ;; so don't optimize out this list:
1081   (push (list name (or initialization (loop-typed-init dtype step-var-p)))
1082         *loop-vars*))
1083  (initialization
1084   (let ((newvar (gensym "LOOP-DESTRUCTURE-")))
1085           (loop-declare-var name dtype)
1086           (push (list newvar initialization) *loop-vars*)
1087           ;; *LOOP-DESETQ-CROCKS* gathered in reverse order.
1088           (setq *loop-desetq-crocks*
1089                 (list* name newvar *loop-desetq-crocks*))))
1090  (t (let ((tcar nil) (tcdr nil))
1091       (if (atom dtype) (setq tcar (setq tcdr dtype))
1092     (setq tcar (car dtype) tcdr (cdr dtype)))
1093       (loop-make-var (car name) nil tcar iteration-var-p)
1094       (loop-make-var (cdr name) nil tcdr iteration-var-p))))
1095  name)
1096
1097(defun loop-make-iteration-var (name initialization dtype)
1098  (when (and name (loop-var-p name))
1099    (loop-error "Variable ~S has already been used." name))
1100  (loop-make-var name initialization dtype t))
1101
1102(defun loop-declare-var (name dtype &optional step-var-p)
1103  (cond ((or (null name) (null dtype) (eq dtype t)) nil)
1104  ((symbolp name)
1105   (unless (subtypep t dtype)
1106     (let ((dtype (let ((init (loop-typed-init dtype step-var-p)))
1107        (if (typep init dtype)
1108            dtype
1109            `(or (member ,init) ,dtype)))))
1110       (push `(type ,dtype ,name) *loop-declarations*))))
1111  ((consp name)
1112   (cond ((consp dtype)
1113    (loop-declare-var (car name) (car dtype))
1114    (loop-declare-var (cdr name) (cdr dtype)))
1115         (t (loop-declare-var (car name) dtype)
1116      (loop-declare-var (cdr name) dtype))))
1117  (t (error "invalid LOOP variable passed in: ~S" name))))
1118
1119(defun loop-maybe-bind-form (form data-type)
1120  (if (loop-constantp form)
1121      form
1122      (loop-make-var (gensym "LOOP-BIND-") form data-type)))
1123
1124(defun loop-do-if (for negatep)
1125  (let ((form (loop-get-form))
1126  (*loop-inside-conditional* t)
1127  (it-p nil)
1128  (first-clause-p t))
1129    (flet ((get-clause (for)
1130       (do ((body nil)) (nil)
1131         (let ((key (car *loop-source-code*)) (*loop-body* nil) data)
1132     (cond ((not (symbolp key))
1133      (loop-error
1134        "~S found where keyword expected getting LOOP clause after ~S"
1135        key for))
1136           (t (setq *loop-source-context* *loop-source-code*)
1137        (loop-pop-source)
1138        (when (and (loop-tequal (car *loop-source-code*) 'it)
1139             first-clause-p)
1140          (setq *loop-source-code*
1141          (cons (or it-p
1142              (setq it-p
1143              (loop-when-it-var)))
1144          (cdr *loop-source-code*))))
1145        (cond ((or (not (setq data (loop-lookup-keyword
1146                   key (loop-universe-keywords *loop-universe*))))
1147             (progn (apply (symbol-function (car data))
1148               (cdr data))
1149              (null *loop-body*)))
1150         (loop-error
1151           "~S does not introduce a LOOP clause that can follow ~S."
1152           key for))
1153        (t (setq body (nreconc *loop-body* body)))))))
1154         (setq first-clause-p nil)
1155         (if (loop-tequal (car *loop-source-code*) :and)
1156       (loop-pop-source)
1157       (return (if (cdr body)
1158             `(progn ,@(nreverse body))
1159             (car body)))))))
1160      (let ((then (get-clause for))
1161      (else (when (loop-tequal (car *loop-source-code*) :else)
1162        (loop-pop-source)
1163        (list (get-clause :else)))))
1164  (when (loop-tequal (car *loop-source-code*) :end)
1165    (loop-pop-source))
1166  (when it-p (setq form `(setq ,it-p ,form)))
1167  (loop-pseudo-body
1168    `(if ,(if negatep `(not ,form) form)
1169         ,then
1170         ,@else))))))
1171
1172(defun loop-do-initially ()
1173  (loop-disallow-conditional :initially)
1174  (push (loop-get-progn) *loop-prologue*))
1175
1176(defun loop-do-finally ()
1177  (loop-disallow-conditional :finally)
1178  (push (loop-get-progn) *loop-epilogue*))
1179
1180(defun loop-do-do ()
1181  (loop-emit-body (loop-get-progn)))
1182
1183(defun loop-do-named ()
1184  (let ((name (loop-pop-source)))
1185    (unless (symbolp name)
1186      (loop-error "~S is an invalid name for your LOOP" name))
1187    (when (or *loop-before-loop* *loop-body* *loop-after-epilogue* *loop-inside-conditional*)
1188      (loop-error "The NAMED ~S clause occurs too late." name))
1189    (when *loop-names*
1190      (loop-error "You may only use one NAMED clause in your loop: NAMED ~S ... NAMED ~S."
1191      (car *loop-names*) name))
1192    (setq *loop-names* (list name))))
1193
1194(defun loop-do-return ()
1195  (loop-emit-body (loop-construct-return (loop-get-form))))
1196
1197;;;; value accumulation: LIST
1198
1199(defstruct (loop-collector
1200      (:copier nil)
1201      (:predicate nil))
1202  name
1203  class
1204  (history nil)
1205  (tempvars nil)
1206  dtype
1207  (data nil)) ;collector-specific data
1208
1209(defun loop-get-collection-info (collector class default-type)
1210  (let ((form (loop-get-form))
1211  (dtype (and (not (loop-universe-ansi *loop-universe*)) (loop-optional-type)))
1212  (name (when (loop-tequal (car *loop-source-code*) 'into)
1213    (loop-pop-source)
1214    (loop-pop-source))))
1215    (when (not (symbolp name))
1216      (loop-error "The value accumulation recipient name, ~S, is not a symbol." name))
1217    (unless name
1218      (loop-disallow-aggregate-booleans))
1219    (unless dtype
1220      (setq dtype (or (loop-optional-type) default-type)))
1221    (let ((cruft (find (the symbol name) *loop-collection-cruft*
1222           :key #'loop-collector-name)))
1223      (cond ((not cruft)
1224       (when (and name (loop-var-p name))
1225         (loop-error "Variable ~S in INTO clause is a duplicate" name))
1226       (push (setq cruft (make-loop-collector
1227         :name name :class class
1228         :history (list collector) :dtype dtype))
1229       *loop-collection-cruft*))
1230      (t (unless (eq (loop-collector-class cruft) class)
1231     (loop-error
1232       "incompatible kinds of LOOP value accumulation specified for collecting~@
1233        ~:[as the value of the LOOP~;~:*INTO ~S~]: ~S and ~S"
1234       name (car (loop-collector-history cruft)) collector))
1235         (unless (equal dtype (loop-collector-dtype cruft))
1236     (loop-warn
1237       "unequal datatypes specified in different LOOP value accumulations~@
1238       into ~S: ~S and ~S"
1239       name dtype (loop-collector-dtype cruft))
1240     (when (eq (loop-collector-dtype cruft) t)
1241       (setf (loop-collector-dtype cruft) dtype)))
1242         (push collector (loop-collector-history cruft))))
1243      (values cruft form))))
1244
1245(defun loop-list-collection (specifically)  ; NCONC, LIST, or APPEND
1246  (multiple-value-bind (lc form)
1247      (loop-get-collection-info specifically 'list 'list)
1248    (let ((tempvars (loop-collector-tempvars lc)))
1249      (unless tempvars
1250  (setf (loop-collector-tempvars lc)
1251        (setq tempvars (list* (gensym "LOOP-LIST-HEAD-")
1252            (gensym "LOOP-LIST-TAIL-")
1253            (and (loop-collector-name lc)
1254           (list (loop-collector-name lc))))))
1255  (push `(with-loop-list-collection-head ,tempvars) *loop-wrappers*)
1256  (unless (loop-collector-name lc)
1257    (loop-emit-final-value `(loop-collect-answer ,(car tempvars)
1258                   ,@(cddr tempvars)))))
1259      (ecase specifically
1260  (list (setq form `(list ,form)))
1261  (nconc nil)
1262  (append (unless (and (consp form) (eq (car form) 'list))
1263      (setq form `(copy-list ,form)))))
1264      (loop-emit-body `(loop-collect-rplacd ,tempvars ,form)))))
1265
1266;;;; value accumulation: MAX, MIN, SUM, COUNT
1267
1268(defun loop-sum-collection (specifically required-type default-type);SUM, COUNT
1269  (multiple-value-bind (lc form)
1270      (loop-get-collection-info specifically 'sum default-type)
1271    (loop-check-data-type (loop-collector-dtype lc) required-type)
1272    (let ((tempvars (loop-collector-tempvars lc)))
1273      (unless tempvars
1274  (setf (loop-collector-tempvars lc)
1275        (setq tempvars (list (loop-make-var
1276             (or (loop-collector-name lc)
1277           (gensym "LOOP-SUM-"))
1278             nil (loop-collector-dtype lc)))))
1279  (unless (loop-collector-name lc)
1280    (loop-emit-final-value (car (loop-collector-tempvars lc)))))
1281      (loop-emit-body
1282  (if (eq specifically 'count)
1283      `(when ,form
1284         (setq ,(car tempvars)
1285         (1+ ,(car tempvars))))
1286      `(setq ,(car tempvars)
1287       (+ ,(car tempvars)
1288          ,form)))))))
1289
1290(defun loop-maxmin-collection (specifically)
1291  (multiple-value-bind (lc form)
1292      (loop-get-collection-info specifically 'maxmin 'real)
1293    (loop-check-data-type (loop-collector-dtype lc) 'real)
1294    (let ((data (loop-collector-data lc)))
1295      (unless data
1296  (setf (loop-collector-data lc)
1297        (setq data (make-loop-minimax
1298         (or (loop-collector-name lc)
1299             (gensym "LOOP-MAXMIN-"))
1300         (loop-collector-dtype lc))))
1301  (unless (loop-collector-name lc)
1302    (loop-emit-final-value (loop-minimax-answer-variable data))))
1303      (loop-note-minimax-operation specifically data)
1304      (push `(with-minimax-value ,data) *loop-wrappers*)
1305      (loop-emit-body `(loop-accumulate-minimax-value ,data
1306                  ,specifically
1307                  ,form)))))
1308
1309;;;; value accumulation: aggregate booleans
1310
1311;;; handling the ALWAYS and NEVER loop keywords
1312;;;
1313;;; Under ANSI these are not permitted to appear under conditionalization.
1314(defun loop-do-always (restrictive negate)
1315  (let ((form (loop-get-form)))
1316    (when restrictive (loop-disallow-conditional))
1317    (loop-disallow-anonymous-collectors)
1318    (loop-emit-body `(,(if negate 'when 'unless) ,form
1319          ,(loop-construct-return nil)))
1320    (loop-emit-final-value t)))
1321
1322;;; handling the THEREIS loop keyword
1323;;;
1324;;; Under ANSI this is not permitted to appear under conditionalization.
1325(defun loop-do-thereis (restrictive)
1326  (when restrictive (loop-disallow-conditional))
1327  (loop-disallow-anonymous-collectors)
1328  (loop-emit-final-value)
1329  (loop-emit-body `(when (setq ,(loop-when-it-var) ,(loop-get-form))
1330        ,(loop-construct-return *loop-when-it-var*))))
1331
1332(defun loop-do-while (negate kwd &aux (form (loop-get-form)))
1333  (loop-disallow-conditional kwd)
1334  (loop-pseudo-body `(,(if negate 'when 'unless) ,form (go end-loop))))
1335
1336(defun loop-do-repeat ()
1337  (loop-disallow-conditional :repeat)
1338  (let ((form (loop-get-form))
1339  (type 'integer))
1340    (let ((var (loop-make-var (gensym "LOOP-REPEAT-") `(ceiling ,form) type)))
1341      (push `(if (<= ,var 0) (go end-loop) (decf ,var)) *loop-before-loop*)
1342      (push `(if (<= ,var 0) (go end-loop) (decf ,var)) *loop-after-body*)
1343      ;; FIXME: What should
1344      ;;   (loop count t into a
1345      ;;         repeat 3
1346      ;;         count t into b
1347      ;;         finally (return (list a b)))
1348      ;; return: (3 3) or (4 3)? PUSHes above are for the former
1349      ;; variant, L-P-B below for the latter.
1350      #+nil (loop-pseudo-body `(when (minusp (decf ,var)) (go end-loop))))))
1351
1352(defun loop-do-with ()
1353  (loop-disallow-conditional :with)
1354  (do ((var) (val) (dtype)) (nil)
1355    (setq var (loop-pop-source)
1356    dtype (loop-optional-type var)
1357    val (cond ((loop-tequal (car *loop-source-code*) :=)
1358         (loop-pop-source)
1359         (loop-get-form))
1360        (t nil)))
1361    (when (and var (loop-var-p var))
1362      (loop-error "Variable ~S has already been used" var))
1363    (loop-make-var var val dtype)
1364    (if (loop-tequal (car *loop-source-code*) :and)
1365  (loop-pop-source)
1366  (return (loop-bind-block)))))
1367
1368;;;; the iteration driver
1369
1370(defun loop-hack-iteration (entry)
1371  (flet ((make-endtest (list-of-forms)
1372     (cond ((null list-of-forms) nil)
1373     ((member t list-of-forms) '(go end-loop))
1374     (t `(when ,(if (null (cdr (setq list-of-forms
1375             (nreverse list-of-forms))))
1376        (car list-of-forms)
1377        (cons 'or list-of-forms))
1378           (go end-loop))))))
1379    (do ((pre-step-tests nil)
1380   (steps nil)
1381   (post-step-tests nil)
1382   (pseudo-steps nil)
1383   (pre-loop-pre-step-tests nil)
1384   (pre-loop-steps nil)
1385   (pre-loop-post-step-tests nil)
1386   (pre-loop-pseudo-steps nil)
1387   (tem) (data))
1388  (nil)
1389      ;; Note that we collect endtests in reverse order, but steps in correct
1390      ;; order. MAKE-ENDTEST does the nreverse for us.
1391      (setq tem (setq data
1392          (apply (symbol-function (first entry)) (rest entry))))
1393      (and (car tem) (push (car tem) pre-step-tests))
1394      (setq steps (nconc steps (copy-list (car (setq tem (cdr tem))))))
1395      (and (car (setq tem (cdr tem))) (push (car tem) post-step-tests))
1396      (setq pseudo-steps
1397      (nconc pseudo-steps (copy-list (car (setq tem (cdr tem))))))
1398      (setq tem (cdr tem))
1399      (when *loop-emitted-body*
1400  (loop-error "iteration in LOOP follows body code"))
1401      (unless tem (setq tem data))
1402      (when (car tem) (push (car tem) pre-loop-pre-step-tests))
1403      ;; FIXME: This (SETF FOO (NCONC FOO BAR)) idiom appears often enough
1404      ;; that it might be worth making it into an NCONCF macro.
1405      (setq pre-loop-steps
1406      (nconc pre-loop-steps (copy-list (car (setq tem (cdr tem))))))
1407      (when (car (setq tem (cdr tem)))
1408  (push (car tem) pre-loop-post-step-tests))
1409      (setq pre-loop-pseudo-steps
1410      (nconc pre-loop-pseudo-steps (copy-list (cadr tem))))
1411      (unless (loop-tequal (car *loop-source-code*) :and)
1412  (setq *loop-before-loop*
1413        (list* (loop-make-desetq pre-loop-pseudo-steps)
1414         (make-endtest pre-loop-post-step-tests)
1415         (loop-make-psetq pre-loop-steps)
1416         (make-endtest pre-loop-pre-step-tests)
1417         *loop-before-loop*))
1418  (setq *loop-after-body*
1419        (list* (loop-make-desetq pseudo-steps)
1420         (make-endtest post-step-tests)
1421         (loop-make-psetq steps)
1422         (make-endtest pre-step-tests)
1423         *loop-after-body*))
1424  (loop-bind-block)
1425  (return nil))
1426      (loop-pop-source)       ; Flush the "AND".
1427      (when (and (not (loop-universe-implicit-for-required *loop-universe*))
1428     (setq tem
1429           (loop-lookup-keyword
1430      (car *loop-source-code*)
1431      (loop-universe-iteration-keywords *loop-universe*))))
1432  ;; The latest ANSI clarification is that the FOR/AS after the AND must
1433  ;; NOT be supplied.
1434  (loop-pop-source)
1435  (setq entry tem)))))
1436
1437;;;; main iteration drivers
1438
1439;;; FOR variable keyword ..args..
1440(defun loop-do-for ()
1441  (let* ((var (loop-pop-source))
1442   (data-type (loop-optional-type var))
1443   (keyword (loop-pop-source))
1444   (first-arg nil)
1445   (tem nil))
1446    (setq first-arg (loop-get-form))
1447    (unless (and (symbolp keyword)
1448     (setq tem (loop-lookup-keyword
1449           keyword
1450           (loop-universe-for-keywords *loop-universe*))))
1451      (loop-error "~S is an unknown keyword in FOR or AS clause in LOOP."
1452      keyword))
1453    (apply (car tem) var first-arg data-type (cdr tem))))
1454
1455(defun loop-when-it-var ()
1456  (or *loop-when-it-var*
1457      (setq *loop-when-it-var*
1458      (loop-make-var (gensym "LOOP-IT-") nil nil))))
1459
1460;;;; various FOR/AS subdispatches
1461
1462;;; ANSI "FOR x = y [THEN z]" is sort of like the old Genera one when
1463;;; the THEN is omitted (other than being more stringent in its
1464;;; placement), and like the old "FOR x FIRST y THEN z" when the THEN
1465;;; is present. I.e., the first initialization occurs in the loop body
1466;;; (first-step), not in the variable binding phase.
1467(defun loop-ansi-for-equals (var val data-type)
1468  (loop-make-iteration-var var nil data-type)
1469  (cond ((loop-tequal (car *loop-source-code*) :then)
1470   ;; Then we are the same as "FOR x FIRST y THEN z".
1471   (loop-pop-source)
1472   `(() (,var ,(loop-get-form)) () ()
1473     () (,var ,val) () ()))
1474  (t ;; We are the same as "FOR x = y".
1475   `(() (,var ,val) () ()))))
1476
1477(defun loop-for-across (var val data-type)
1478  (loop-make-iteration-var var nil data-type)
1479  (let ((vector-var (gensym "LOOP-ACROSS-VECTOR-"))
1480  (index-var (gensym "LOOP-ACROSS-INDEX-")))
1481    (multiple-value-bind (vector-form constantp vector-value)
1482  (loop-constant-fold-if-possible val 'vector)
1483      (loop-make-var
1484  vector-var vector-form
1485  (if (and (consp vector-form) (eq (car vector-form) 'the))
1486      (cadr vector-form)
1487      'vector))
1488      (loop-make-var index-var 0 'fixnum)
1489      (let* ((length 0)
1490       (length-form (cond ((not constantp)
1491         (let ((v (gensym "LOOP-ACROSS-LIMIT-")))
1492           (push `(setq ,v (length ,vector-var))
1493           *loop-prologue*)
1494           (loop-make-var v 0 'fixnum)))
1495        (t (setq length (length vector-value)))))
1496       (first-test `(>= ,index-var ,length-form))
1497       (other-test first-test)
1498       (step `(,var (aref ,vector-var ,index-var)))
1499       (pstep `(,index-var (1+ ,index-var))))
1500  (declare (fixnum length))
1501  (when constantp
1502    (setq first-test (= length 0))
1503    (when (<= length 1)
1504      (setq other-test t)))
1505  `(,other-test ,step () ,pstep
1506    ,@(and (not (eq first-test other-test))
1507     `(,first-test ,step () ,pstep)))))))
1508
1509;;;; list iteration
1510
1511(defun loop-list-step (listvar)
1512  ;; We are not equipped to analyze whether 'FOO is the same as #'FOO
1513  ;; here in any sensible fashion, so let's give an obnoxious warning
1514  ;; whenever 'FOO is used as the stepping function.
1515  ;;
1516  ;; While a Discerning Compiler may deal intelligently with
1517  ;; (FUNCALL 'FOO ...), not recognizing FOO may defeat some LOOP
1518  ;; optimizations.
1519  (let ((stepper (cond ((loop-tequal (car *loop-source-code*) :by)
1520      (loop-pop-source)
1521      (loop-get-form))
1522           (t '(function cdr)))))
1523    (cond ((and (consp stepper) (eq (car stepper) 'quote))
1524     (loop-warn "Use of QUOTE around stepping function in LOOP will be left verbatim.")
1525     `(funcall ,stepper ,listvar))
1526    ((and (consp stepper) (eq (car stepper) 'function))
1527     (list (cadr stepper) listvar))
1528    (t
1529     `(funcall ,(loop-make-var (gensym "LOOP-FN-") stepper 'function)
1530         ,listvar)))))
1531
1532(defun loop-for-on (var val data-type)
1533  (multiple-value-bind (list constantp list-value)
1534      (loop-constant-fold-if-possible val)
1535    (let ((listvar var))
1536      (cond ((and var (symbolp var))
1537       (loop-make-iteration-var var list data-type))
1538      (t (loop-make-var (setq listvar (gensym)) list 'list)
1539         (loop-make-iteration-var var nil data-type)))
1540      (let ((list-step (loop-list-step listvar)))
1541  (let* ((first-endtest
1542    ;; mysterious comment from original CMU CL sources:
1543    ;;   the following should use `atom' instead of `endp',
1544    ;;   per [bug2428]
1545    `(atom ,listvar))
1546         (other-endtest first-endtest))
1547    (when (and constantp (listp list-value))
1548      (setq first-endtest (null list-value)))
1549    (cond ((eq var listvar)
1550     ;; The contour of the loop is different because we
1551     ;; use the user's variable...
1552     `(() (,listvar ,list-step)
1553       ,other-endtest () () () ,first-endtest ()))
1554    (t (let ((step `(,var ,listvar))
1555       (pseudo `(,listvar ,list-step)))
1556         `(,other-endtest ,step () ,pseudo
1557           ,@(and (not (eq first-endtest other-endtest))
1558            `(,first-endtest ,step () ,pseudo)))))))))))
1559
1560(defun loop-for-in (var val data-type)
1561  (multiple-value-bind (list constantp list-value)
1562      (loop-constant-fold-if-possible val)
1563    (let ((listvar (gensym "LOOP-LIST-")))
1564      (loop-make-iteration-var var nil data-type)
1565      (loop-make-var listvar list 'list)
1566      (let ((list-step (loop-list-step listvar)))
1567  (let* ((first-endtest `(endp ,listvar))
1568         (other-endtest first-endtest)
1569         (step `(,var (car ,listvar)))
1570         (pseudo-step `(,listvar ,list-step)))
1571    (when (and constantp (listp list-value))
1572      (setq first-endtest (null list-value)))
1573    `(,other-endtest ,step () ,pseudo-step
1574      ,@(and (not (eq first-endtest other-endtest))
1575       `(,first-endtest ,step () ,pseudo-step))))))))
1576
1577;;;; iteration paths
1578
1579(defstruct (loop-path
1580      (:copier nil)
1581      (:predicate nil))
1582  names
1583  preposition-groups
1584  inclusive-permitted
1585  function
1586  user-data)
1587
1588(defun add-loop-path (names function universe
1589          &key preposition-groups inclusive-permitted user-data)
1590  (declare (type loop-universe universe))
1591  (unless (listp names)
1592    (setq names (list names)))
1593  (let ((ht (loop-universe-path-keywords universe))
1594  (lp (make-loop-path
1595        :names (mapcar #'symbol-name names)
1596        :function function
1597        :user-data user-data
1598        :preposition-groups (mapcar (lambda (x)
1599              (if (listp x) x (list x)))
1600            preposition-groups)
1601        :inclusive-permitted inclusive-permitted)))
1602    (dolist (name names)
1603      (setf (gethash (symbol-name name) ht) lp))
1604    lp))
1605
1606;;; Note: Path functions are allowed to use LOOP-MAKE-VAR, hack
1607;;; the prologue, etc.
1608(defun loop-for-being (var val data-type)
1609  ;; FOR var BEING each/the pathname prep-phrases using-stuff... each/the =
1610  ;; EACH or THE. Not clear if it is optional, so I guess we'll warn.
1611  (let ((path nil)
1612  (data nil)
1613  (inclusive nil)
1614  (stuff nil)
1615  (initial-prepositions nil))
1616    (cond ((loop-tmember val '(:each :the)) (setq path (loop-pop-source)))
1617    ((loop-tequal (car *loop-source-code*) :and)
1618     (loop-pop-source)
1619     (setq inclusive t)
1620     (unless (loop-tmember (car *loop-source-code*)
1621         '(:its :each :his :her))
1622       (loop-error "~S was found where ITS or EACH expected in LOOP iteration path syntax."
1623       (car *loop-source-code*)))
1624     (loop-pop-source)
1625     (setq path (loop-pop-source))
1626     (setq initial-prepositions `((:in ,val))))
1627    (t (loop-error "unrecognizable LOOP iteration path syntax: missing EACH or THE?")))
1628    (cond ((not (symbolp path))
1629     (loop-error
1630      "~S was found where a LOOP iteration path name was expected."
1631      path))
1632    ((not (setq data (loop-lookup-keyword path (loop-universe-path-keywords *loop-universe*))))
1633     (loop-error "~S is not the name of a LOOP iteration path." path))
1634    ((and inclusive (not (loop-path-inclusive-permitted data)))
1635     (loop-error "\"Inclusive\" iteration is not possible with the ~S LOOP iteration path." path)))
1636    (let ((fun (loop-path-function data))
1637    (preps (nconc initial-prepositions
1638      (loop-collect-prepositional-phrases
1639       (loop-path-preposition-groups data)
1640       t)))
1641    (user-data (loop-path-user-data data)))
1642      (when (symbolp fun) (setq fun (symbol-function fun)))
1643      (setq stuff (if inclusive
1644          (apply fun var data-type preps :inclusive t user-data)
1645          (apply fun var data-type preps user-data))))
1646    (when *loop-named-vars*
1647      (loop-error "Unused USING vars: ~S." *loop-named-vars*))
1648    ;; STUFF is now (bindings prologue-forms . stuff-to-pass-back).
1649    ;; Protect the system from the user and the user from himself.
1650    (unless (member (length stuff) '(6 10))
1651      (loop-error "Value passed back by LOOP iteration path function for path ~S has invalid length."
1652      path))
1653    (do ((l (car stuff) (cdr l)) (x)) ((null l))
1654      (if (atom (setq x (car l)))
1655    (loop-make-iteration-var x nil nil)
1656    (loop-make-iteration-var (car x) (cadr x) (caddr x))))
1657    (setq *loop-prologue* (nconc (reverse (cadr stuff)) *loop-prologue*))
1658    (cddr stuff)))
1659
1660(defun loop-named-var (name)
1661  (let ((tem (loop-tassoc name *loop-named-vars*)))
1662    (declare (list tem))
1663    (cond ((null tem) (values (gensym) nil))
1664    (t (setq *loop-named-vars* (delete tem *loop-named-vars*))
1665       (values (cdr tem) t)))))
1666
1667(defun loop-collect-prepositional-phrases (preposition-groups
1668             &optional
1669             using-allowed
1670             initial-phrases)
1671  (flet ((in-group-p (x group) (car (loop-tmember x group))))
1672    (do ((token nil)
1673   (prepositional-phrases initial-phrases)
1674   (this-group nil nil)
1675   (this-prep nil nil)
1676   (disallowed-prepositions
1677     (mapcan (lambda (x)
1678         (copy-list
1679          (find (car x) preposition-groups :test #'in-group-p)))
1680       initial-phrases))
1681   (used-prepositions (mapcar #'car initial-phrases)))
1682  ((null *loop-source-code*) (nreverse prepositional-phrases))
1683      (declare (symbol this-prep))
1684      (setq token (car *loop-source-code*))
1685      (dolist (group preposition-groups)
1686  (when (setq this-prep (in-group-p token group))
1687    (return (setq this-group group))))
1688      (cond (this-group
1689       (when (member this-prep disallowed-prepositions)
1690         (loop-error
1691     (if (member this-prep used-prepositions)
1692         "A ~S prepositional phrase occurs multiply for some LOOP clause."
1693         "Preposition ~S was used when some other preposition has subsumed it.")
1694     token))
1695       (setq used-prepositions (if (listp this-group)
1696           (append this-group used-prepositions)
1697           (cons this-group used-prepositions)))
1698       (loop-pop-source)
1699       (push (list this-prep (loop-get-form)) prepositional-phrases))
1700      ((and using-allowed (loop-tequal token 'using))
1701       (loop-pop-source)
1702       (do ((z (loop-pop-source) (loop-pop-source)) (tem)) (nil)
1703         (when (cadr z)
1704     (if (setq tem (loop-tassoc (car z) *loop-named-vars*))
1705         (loop-error
1706           "The variable substitution for ~S occurs twice in a USING phrase,~@
1707      with ~S and ~S."
1708           (car z) (cadr z) (cadr tem))
1709         (push (cons (car z) (cadr z)) *loop-named-vars*)))
1710         (when (or (null *loop-source-code*)
1711       (symbolp (car *loop-source-code*)))
1712     (return nil))))
1713      (t (return (nreverse prepositional-phrases)))))))
1714
1715;;;; master sequencer function
1716
1717(defun loop-sequencer (indexv indexv-type
1718           variable variable-type
1719           sequence-variable sequence-type
1720           step-hack default-top
1721           prep-phrases)
1722   (let ((endform nil) ; form (constant or variable) with limit value
1723   (sequencep nil) ; T if sequence arg has been provided
1724   (testfn nil) ; endtest function
1725   (test nil) ; endtest form
1726   (stepby (1+ (or (loop-typed-init indexv-type) 0))) ; our increment
1727   (stepby-constantp t)
1728   (step nil) ; step form
1729   (dir nil) ; direction of stepping: NIL, :UP, :DOWN
1730   (inclusive-iteration nil) ; T if include last index
1731   (start-given nil) ; T when prep phrase has specified start
1732   (start-value nil)
1733   (start-constantp nil)
1734   (limit-given nil) ; T when prep phrase has specified end
1735   (limit-constantp nil)
1736   (limit-value nil)
1737   )
1738     (flet ((assert-index-for-arithmetic (index)
1739        (unless (atom index)
1740    (loop-error "Arithmetic index must be an atom."))))
1741       (when variable (loop-make-iteration-var variable nil variable-type))
1742       (do ((l prep-phrases (cdr l)) (prep) (form) (odir)) ((null l))
1743   (setq prep (caar l) form (cadar l))
1744   (case prep
1745     ((:of :in)
1746      (setq sequencep t)
1747      (loop-make-var sequence-variable form sequence-type))
1748     ((:from :downfrom :upfrom)
1749      (setq start-given t)
1750      (cond ((eq prep :downfrom) (setq dir ':down))
1751      ((eq prep :upfrom) (setq dir ':up)))
1752      (multiple-value-setq (form start-constantp start-value)
1753        (loop-constant-fold-if-possible form indexv-type))
1754      (assert-index-for-arithmetic indexv)
1755      ;; KLUDGE: loop-make-var generates a temporary symbol for
1756      ;; indexv if it is NIL. We have to use it to have the index
1757      ;; actually count
1758      (setq indexv (loop-make-iteration-var indexv form indexv-type)))
1759     ((:upto :to :downto :above :below)
1760      (cond ((loop-tequal prep :upto) (setq inclusive-iteration
1761              (setq dir ':up)))
1762      ((loop-tequal prep :to) (setq inclusive-iteration t))
1763      ((loop-tequal prep :downto) (setq inclusive-iteration
1764                (setq dir ':down)))
1765      ((loop-tequal prep :above) (setq dir ':down))
1766      ((loop-tequal prep :below) (setq dir ':up)))
1767      (setq limit-given t)
1768      (multiple-value-setq (form limit-constantp limit-value)
1769        (loop-constant-fold-if-possible form `(and ,indexv-type real)))
1770      (setq endform (if limit-constantp
1771            `',limit-value
1772            (loop-make-var
1773         (gensym "LOOP-LIMIT-") form
1774         `(and ,indexv-type real)))))
1775     (:by
1776      (multiple-value-setq (form stepby-constantp stepby)
1777        (loop-constant-fold-if-possible form `(and ,indexv-type (real (0)))))
1778      (unless stepby-constantp
1779        (loop-make-var (setq stepby (gensym "LOOP-STEP-BY-"))
1780     form
1781     `(and ,indexv-type (real (0)))
1782     nil t)))
1783     (t (loop-error
1784     "~S invalid preposition in sequencing or sequence path;~@
1785        maybe invalid prepositions were specified in iteration path descriptor?"
1786     prep)))
1787   (when (and odir dir (not (eq dir odir)))
1788     (loop-error "conflicting stepping directions in LOOP sequencing path"))
1789   (setq odir dir))
1790       (when (and sequence-variable (not sequencep))
1791   (loop-error "missing OF or IN phrase in sequence path"))
1792       ;; Now fill in the defaults.
1793       (if start-given
1794     (when limit-given
1795       ;; if both start and limit are given, they had better both
1796       ;; be REAL.  We already enforce the REALness of LIMIT,
1797       ;; above; here's the KLUDGE to enforce the type of START.
1798       (flet ((type-declaration-of (x)
1799          (and (eq (car x) 'type) (caddr x))))
1800         (let ((decl (find indexv *loop-declarations*
1801         :key #'type-declaration-of))
1802         (%decl (find indexv *loop-declarations*
1803          :key #'type-declaration-of
1804          :from-end t)))
1805     #+sbcl (aver (eq decl %decl))
1806                 #-sbcl (declare (ignore %decl))
1807     (setf (cadr decl)
1808           `(and real ,(cadr decl))))))
1809     ;; default start
1810     ;; DUPLICATE KLUDGE: loop-make-var generates a temporary
1811     ;; symbol for indexv if it is NIL. See also the comment in
1812     ;; the (:from :downfrom :upfrom) case
1813     (progn
1814       (assert-index-for-arithmetic indexv)
1815       (setq indexv
1816       (loop-make-iteration-var
1817          indexv
1818          (setq start-constantp t
1819          start-value (or (loop-typed-init indexv-type) 0))
1820          `(and ,indexv-type real)))))
1821       (cond ((member dir '(nil :up))
1822        (when (or limit-given default-top)
1823    (unless limit-given
1824      (loop-make-var (setq endform (gensym "LOOP-SEQ-LIMIT-"))
1825         nil
1826         indexv-type)
1827      (push `(setq ,endform ,default-top) *loop-prologue*))
1828    (setq testfn (if inclusive-iteration '> '>=)))
1829        (setq step (if (eql stepby 1) `(1+ ,indexv) `(+ ,indexv ,stepby))))
1830       (t (unless start-given
1831      (unless default-top
1832        (loop-error "don't know where to start stepping"))
1833      (push `(setq ,indexv (1- ,default-top)) *loop-prologue*))
1834    (when (and default-top (not endform))
1835      (setq endform (loop-typed-init indexv-type)
1836      inclusive-iteration t))
1837    (when endform (setq testfn (if inclusive-iteration  '< '<=)))
1838    (setq step
1839          (if (eql stepby 1) `(1- ,indexv) `(- ,indexv ,stepby)))))
1840       (when testfn
1841   (setq test
1842         `(,testfn ,indexv ,endform)))
1843       (when step-hack
1844   (setq step-hack
1845         `(,variable ,step-hack)))
1846       (let ((first-test test) (remaining-tests test))
1847   (when (and stepby-constantp start-constantp limit-constantp
1848        (realp start-value) (realp limit-value))
1849     (when (setq first-test
1850           (funcall (symbol-function testfn)
1851        start-value
1852        limit-value))
1853       (setq remaining-tests t)))
1854   `(() (,indexv ,step)
1855     ,remaining-tests ,step-hack () () ,first-test ,step-hack)))))
1856
1857;;;; interfaces to the master sequencer
1858
1859(defun loop-for-arithmetic (var val data-type kwd)
1860  (loop-sequencer
1861   var (loop-check-data-type data-type 'number)
1862   nil nil nil nil nil nil
1863   (loop-collect-prepositional-phrases
1864    '((:from :upfrom :downfrom) (:to :upto :downto :above :below) (:by))
1865    nil (list (list kwd val)))))
1866
1867(defun loop-sequence-elements-path (variable data-type prep-phrases
1868            &key
1869            fetch-function
1870            size-function
1871            sequence-type
1872            element-type)
1873  (multiple-value-bind (indexv) (loop-named-var 'index)
1874    (let ((sequencev (loop-named-var 'sequence)))
1875      (list* nil nil        ; dummy bindings and prologue
1876       (loop-sequencer
1877        indexv 'fixnum
1878        variable (or data-type element-type)
1879        sequencev sequence-type
1880        `(,fetch-function ,sequencev ,indexv)
1881        `(,size-function ,sequencev)
1882        prep-phrases)))))
1883
1884;;;; builtin LOOP iteration paths
1885
1886#||
1887(loop for v being the hash-values of ht do (print v))
1888(loop for k being the hash-keys of ht do (print k))
1889(loop for v being the hash-values of ht using (hash-key k) do (print (list k v)))
1890(loop for k being the hash-keys of ht using (hash-value v) do (print (list k v)))
1891||#
1892
1893(defun loop-hash-table-iteration-path (variable data-type prep-phrases
1894               &key which)
1895  (declare (type (member :hash-key :hash-value) which))
1896  (cond ((or (cdr prep-phrases) (not (member (caar prep-phrases) '(:in :of))))
1897   (loop-error "too many prepositions!"))
1898  ((null prep-phrases)
1899   (loop-error "missing OF or IN in ~S iteration path")))
1900  (let ((ht-var (gensym "LOOP-HASHTAB-"))
1901  (next-fn (gensym "LOOP-HASHTAB-NEXT-"))
1902  (dummy-predicate-var nil)
1903  (post-steps nil))
1904    (multiple-value-bind (other-var other-p)
1905  (loop-named-var (ecase which
1906        (:hash-key 'hash-value)
1907        (:hash-value 'hash-key)))
1908      ;; @@@@ LOOP-NAMED-VAR returns a second value of T if the name was
1909      ;; actually specified, so clever code can throw away the GENSYM'ed-up
1910      ;; variable if it isn't really needed.
1911      (unless other-p
1912        (push `(ignorable ,other-var) *loop-declarations*))
1913      ;; The following is for those implementations in which we cannot put
1914      ;; dummy NILs into MULTIPLE-VALUE-SETQ variable lists.
1915      (setq other-p t
1916      dummy-predicate-var (loop-when-it-var))
1917      (let* ((key-var nil)
1918       (val-var nil)
1919       (variable (or variable (gensym "LOOP-HASH-VAR-TEMP-")))
1920       (bindings `((,variable nil ,data-type)
1921       (,ht-var ,(cadar prep-phrases))
1922       ,@(and other-p other-var `((,other-var nil))))))
1923  (ecase which
1924    (:hash-key (setq key-var variable
1925         val-var (and other-p other-var)))
1926    (:hash-value (setq key-var (and other-p other-var)
1927           val-var variable)))
1928  (push `(with-hash-table-iterator (,next-fn ,ht-var)) *loop-wrappers*)
1929        (when (or (consp key-var) data-type)
1930          (setq post-steps
1931                `(,key-var ,(setq key-var (gensym "LOOP-HASH-KEY-TEMP-"))
1932                           ,@post-steps))
1933          (push `(,key-var nil) bindings))
1934        (when (or (consp val-var) data-type)
1935          (setq post-steps
1936                `(,val-var ,(setq val-var (gensym "LOOP-HASH-VAL-TEMP-"))
1937                           ,@post-steps))
1938          (push `(,val-var nil) bindings))
1939        (push `(ignorable ,dummy-predicate-var) *loop-declarations*)
1940  `(,bindings                     ;bindings
1941    ()                            ;prologue
1942    ()                            ;pre-test
1943    ()                            ;parallel steps
1944    (not (multiple-value-setq (,dummy-predicate-var ,key-var ,val-var)
1945     (,next-fn)))           ;post-test
1946    ,post-steps)))))
1947
1948(defun loop-package-symbols-iteration-path (variable data-type prep-phrases
1949              &key symbol-types)
1950  (cond ((and prep-phrases (cdr prep-phrases))
1951   (loop-error "Too many prepositions!"))
1952        ((and prep-phrases (not (member (caar prep-phrases) '(:in :of))))
1953         (loop-error "Unknown preposition ~S." (caar prep-phrases))))
1954  (unless (symbolp variable)
1955    (loop-error "Destructuring is not valid for package symbol iteration."))
1956  (let ((pkg-var (gensym "LOOP-PKGSYM-"))
1957  (next-fn (gensym "LOOP-PKGSYM-NEXT-"))
1958  (variable (or variable (gensym "LOOP-PKGSYM-VAR-")))
1959        (package (or (cadar prep-phrases) '*package*)))
1960    (push `(with-package-iterator (,next-fn ,pkg-var ,@symbol-types))
1961    *loop-wrappers*)
1962    (push `(ignorable ,(loop-when-it-var)) *loop-declarations*)
1963    `(((,variable nil ,data-type) (,pkg-var ,package))
1964      ()
1965      ()
1966      ()
1967      (not (multiple-value-setq (,(loop-when-it-var)
1968         ,variable)
1969       (,next-fn)))
1970      ())))
1971
1972;;;; ANSI LOOP
1973
1974(defun make-ansi-loop-universe (extended-p)
1975  (let ((w (make-standard-loop-universe
1976       :keywords '((named (loop-do-named))
1977       (initially (loop-do-initially))
1978       (finally (loop-do-finally))
1979       (do (loop-do-do))
1980       (doing (loop-do-do))
1981       (return (loop-do-return))
1982       (collect (loop-list-collection list))
1983       (collecting (loop-list-collection list))
1984       (append (loop-list-collection append))
1985       (appending (loop-list-collection append))
1986       (nconc (loop-list-collection nconc))
1987       (nconcing (loop-list-collection nconc))
1988       (count (loop-sum-collection count
1989                 real
1990                 fixnum))
1991       (counting (loop-sum-collection count
1992              real
1993              fixnum))
1994       (sum (loop-sum-collection sum number number))
1995       (summing (loop-sum-collection sum number number))
1996       (maximize (loop-maxmin-collection max))
1997       (minimize (loop-maxmin-collection min))
1998       (maximizing (loop-maxmin-collection max))
1999       (minimizing (loop-maxmin-collection min))
2000       (always (loop-do-always t nil)) ; Normal, do always
2001       (never (loop-do-always t t)) ; Negate test on always.
2002       (thereis (loop-do-thereis t))
2003       (while (loop-do-while nil :while)) ; Normal, do while
2004       (until (loop-do-while t :until)) ;Negate test on while
2005       (when (loop-do-if when nil)) ; Normal, do when
2006       (if (loop-do-if if nil)) ; synonymous
2007       (unless (loop-do-if unless t)) ; Negate test on when
2008       (with (loop-do-with))
2009                         (repeat (loop-do-repeat)))
2010       :for-keywords '((= (loop-ansi-for-equals))
2011           (across (loop-for-across))
2012           (in (loop-for-in))
2013           (on (loop-for-on))
2014           (from (loop-for-arithmetic :from))
2015           (downfrom (loop-for-arithmetic :downfrom))
2016           (upfrom (loop-for-arithmetic :upfrom))
2017           (below (loop-for-arithmetic :below))
2018                             (above (loop-for-arithmetic :above))
2019           (to (loop-for-arithmetic :to))
2020           (upto (loop-for-arithmetic :upto))
2021           (downto (loop-for-arithmetic :downto))
2022           (by (loop-for-arithmetic :by))
2023           (being (loop-for-being)))
2024       :iteration-keywords '((for (loop-do-for))
2025           (as (loop-do-for)))
2026       :type-symbols '(array atom bignum bit bit-vector character
2027           compiled-function complex cons double-float
2028           fixnum float function hash-table integer
2029           keyword list long-float nil null number
2030           package pathname random-state ratio rational
2031           readtable sequence short-float simple-array
2032           simple-bit-vector simple-string simple-vector
2033           single-float standard-char stream string
2034           base-char symbol t vector)
2035       :type-keywords nil
2036       :ansi (if extended-p :extended t))))
2037    (add-loop-path '(hash-key hash-keys) 'loop-hash-table-iteration-path w
2038       :preposition-groups '((:of :in))
2039       :inclusive-permitted nil
2040       :user-data '(:which :hash-key))
2041    (add-loop-path '(hash-value hash-values) 'loop-hash-table-iteration-path w
2042       :preposition-groups '((:of :in))
2043       :inclusive-permitted nil
2044       :user-data '(:which :hash-value))
2045    (add-loop-path '(symbol symbols) 'loop-package-symbols-iteration-path w
2046       :preposition-groups '((:of :in))
2047       :inclusive-permitted nil
2048       :user-data '(:symbol-types (:internal
2049                 :external
2050                 :inherited)))
2051    (add-loop-path '(external-symbol external-symbols)
2052       'loop-package-symbols-iteration-path w
2053       :preposition-groups '((:of :in))
2054       :inclusive-permitted nil
2055       :user-data '(:symbol-types (:external)))
2056    (add-loop-path '(present-symbol present-symbols)
2057       'loop-package-symbols-iteration-path w
2058       :preposition-groups '((:of :in))
2059       :inclusive-permitted nil
2060       :user-data '(:symbol-types (:internal
2061                 :external)))
2062    w))
2063
2064(defparameter *loop-ansi-universe*
2065  (make-ansi-loop-universe nil))
2066
2067(defun loop-standard-expansion (keywords-and-forms environment universe)
2068  (if (and keywords-and-forms (symbolp (car keywords-and-forms)))
2069      (loop-translate keywords-and-forms environment universe)
2070      (let ((tag (gensym)))
2071  `(block nil (tagbody ,tag (progn ,@keywords-and-forms) (go ,tag))))))
2072
2073(defmacro loop (&environment env &rest keywords-and-forms)
2074  (loop-standard-expansion keywords-and-forms env *loop-ansi-universe*))
2075
2076(defmacro loop-finish ()
2077  "Cause the iteration to terminate \"normally\", the same as implicit
2078termination by an iteration driving clause, or by use of WHILE or
2079UNTIL -- the epilogue code (if any) will be run, and any implicitly
2080collected result will be returned as the value of the LOOP."
2081  '(go end-loop))
2082
2083(provide "LOOP")
Note: See TracBrowser for help on using the repository browser.