f00d8c32e0dddb5dfe358040abd66254c63e5e65
[sbcl.git] / src / compiler / seqtran.lisp
1 ;;;; optimizers for list and sequence functions
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!C")
13
14 (file-comment
15   "$Header$")
16 \f
17 ;;;; mapping onto lists: the MAPFOO functions
18
19 (defun mapfoo-transform (fn arglists accumulate take-car)
20   (collect ((do-clauses)
21             (args-to-fn)
22             (tests))
23     (let ((n-first (gensym)))
24       (dolist (a (if accumulate
25                      arglists
26                      `(,n-first ,@(rest arglists))))
27         (let ((v (gensym)))
28           (do-clauses `(,v ,a (cdr ,v)))
29           (tests `(endp ,v))
30           (args-to-fn (if take-car `(car ,v) v))))
31
32       (let ((call `(funcall ,fn . ,(args-to-fn)))
33             (endtest `(or ,@(tests))))
34         (ecase accumulate
35           (:nconc
36            (let ((temp (gensym))
37                  (map-result (gensym)))
38              `(let ((,map-result (list nil)))
39                 (do-anonymous ((,temp ,map-result) . ,(do-clauses))
40                               (,endtest (cdr ,map-result))
41                   (setq ,temp (last (nconc ,temp ,call)))))))
42           (:list
43            (let ((temp (gensym))
44                  (map-result (gensym)))
45              `(let ((,map-result (list nil)))
46                 (do-anonymous ((,temp ,map-result) . ,(do-clauses))
47                               (,endtest (cdr ,map-result))
48                   (rplacd ,temp (setq ,temp (list ,call)))))))
49           ((nil)
50            `(let ((,n-first ,(first arglists)))
51               (do-anonymous ,(do-clauses)
52                             (,endtest ,n-first) ,call))))))))
53
54 (def-source-transform mapc (function list &rest more-lists)
55   (mapfoo-transform function (cons list more-lists) nil t))
56
57 (def-source-transform mapcar (function list &rest more-lists)
58   (mapfoo-transform function (cons list more-lists) :list t))
59
60 (def-source-transform mapcan (function list &rest more-lists)
61   (mapfoo-transform function (cons list more-lists) :nconc t))
62
63 (def-source-transform mapl (function list &rest more-lists)
64   (mapfoo-transform function (cons list more-lists) nil nil))
65
66 (def-source-transform maplist (function list &rest more-lists)
67   (mapfoo-transform function (cons list more-lists) :list nil))
68
69 (def-source-transform mapcon (function list &rest more-lists)
70   (mapfoo-transform function (cons list more-lists) :nconc nil))
71 \f
72 ;;;; mapping onto sequences: the MAP function
73
74 ;;; MAP is %MAP plus a check to make sure that any length specified in
75 ;;; the result type matches the actual result. We also wrap it in a
76 ;;; TRULY-THE for the most specific type we can determine.
77 (deftransform map ((result-type-arg fun &rest seqs) * * :node node)
78   (let* ((seq-names (make-gensym-list (length seqs)))
79          (bare `(%map result-type-arg fun ,@seq-names))
80          (constant-result-type-arg-p (constant-continuation-p result-type-arg))
81          ;; what we know about the type of the result. (Note that the
82          ;; "result type" argument is not necessarily the type of the
83          ;; result, since NIL means the result has NULL type.)
84          (result-type (if (not constant-result-type-arg-p)
85                           'consed-sequence
86                           (let ((result-type-arg-value
87                                  (continuation-value result-type-arg)))
88                             (if (null result-type-arg-value)
89                                 'null
90                                 result-type-arg-value)))))
91     `(lambda (result-type-arg fun ,@seq-names)
92        (truly-the ,result-type
93          ,(cond ((policy node (> speed safety))
94                  bare)
95                 ((not constant-result-type-arg-p)
96                  `(sequence-of-checked-length-given-type ,bare
97                                                          result-type-arg))
98                 (t
99                  (let ((result-ctype (specifier-type result-type)))
100                    (if (array-type-p result-ctype)
101                        (let* ((dims (array-type-dimensions result-ctype))
102                               (dim (first dims)))
103                          (if (eq dim '*)
104                              bare
105                              `(vector-of-checked-length-given-length ,bare
106                                                                      ,dim)))
107                        bare))))))))
108
109 ;;; Try to compile %MAP efficiently when we can determine sequence
110 ;;; argument types at compile time.
111 ;;;
112 ;;; Note: This transform was written to allow open coding of
113 ;;; quantifiers by expressing them in terms of (MAP NIL ..). For
114 ;;; non-NIL values of RESULT-TYPE, it's still useful, but not
115 ;;; necessarily as efficient as possible. In particular, it will be
116 ;;; inefficient when RESULT-TYPE is a SIMPLE-ARRAY with specialized
117 ;;; numeric element types. It should be straightforward to make it
118 ;;; handle that case more efficiently, but it's left as an exercise to
119 ;;; the reader, because the code is complicated enough already and I
120 ;;; don't happen to need that functionality right now. -- WHN 20000410
121 (deftransform %map ((result-type fun &rest seqs) * * :policy (>= speed space))
122   "open code"
123   (unless seqs (abort-ir1-transform "no sequence args"))
124   (unless (constant-continuation-p result-type)
125     (give-up-ir1-transform "RESULT-TYPE argument not constant"))
126   (labels (;; 1-valued SUBTYPEP, fails unless second value of SUBTYPEP is true
127            (fn-1subtypep (fn x y)
128              (multiple-value-bind (subtype-p valid-p) (funcall fn x y)
129                (if valid-p
130                    subtype-p
131                    (give-up-ir1-transform
132                     "can't analyze sequence type relationship"))))
133            (1subtypep (x y) (fn-1subtypep #'sb!xc:subtypep x y))
134            (1csubtypep (x y) (fn-1subtypep #'csubtypep x y))
135            (seq-supertype (seq)
136              (let ((ctype (continuation-type seq)))
137                (cond ((1csubtypep ctype (specifier-type 'vector)) 'vector)
138                      ((1csubtypep ctype (specifier-type 'list)) 'list)
139                      (t
140                       (give-up-ir1-transform
141                        "can't determine sequence argument type"))))))
142     (let* ((result-type-value (continuation-value result-type))
143            (result-supertype (cond ((null result-type-value) 'null)
144                                    ((1subtypep result-type-value 'vector)
145                                     'vector)
146                                    ((1subtypep result-type-value 'list)
147                                     'list)
148                                    (t
149                                     (give-up-ir1-transform
150                                      "can't determine result type"))))
151            (seq-supertypes (mapcar #'seq-supertype seqs)))
152       (cond ((and result-type-value (= 1 (length seqs)))
153              ;; The consing arity-1 cases can be implemented
154              ;; reasonably efficiently as function calls, and the cost
155              ;; of consing should be significantly larger than
156              ;; function call overhead, so we always compile these
157              ;; cases as full calls regardless of speed-versus-space
158              ;; optimization policy.
159              (cond ((subtypep 'list result-type-value)
160                     '(apply #'%map-to-list-arity-1 fun seqs))
161                    (;; (This one can be inefficient due to COERCE, but
162                     ;; the current open-coded implementation has the
163                     ;; same problem.)
164                     (subtypep result-type-value 'vector)
165                     `(coerce (apply #'%map-to-simple-vector-arity-1 fun seqs)
166                              ',result-type-value))
167                    (t (give-up-ir1-transform
168                        "internal error: unexpected sequence type"))))
169             (t
170              (let* ((seq-args (make-gensym-list (length seqs)))
171                     (index-bindingoids
172                      (mapcar (lambda (seq-arg seq-supertype)
173                                (let ((i (gensym "I"))) 
174                                  (ecase seq-supertype
175                                    (vector `(,i 0 (1+ ,i)))
176                                    (list `(,i ,seq-arg (rest ,i))))))
177                              seq-args seq-supertypes))
178                     (indices (mapcar #'first index-bindingoids))
179                     (index-decls (mapcar (lambda (index seq-supertype)
180                                            `(type ,(ecase seq-supertype
181                                                      (vector 'index)
182                                                      (list 'list))
183                                                   ,index))
184                                          indices seq-supertypes))
185                     (tests (mapcar (lambda (seq-arg seq-supertype index)
186                                      (ecase seq-supertype
187                                        (vector `(>= ,index (length ,seq-arg)))
188                                        (list `(endp ,index))))
189                                    seq-args seq-supertypes indices))
190                     (values (mapcar (lambda (seq-arg seq-supertype index)
191                                       (ecase seq-supertype
192                                         (vector `(aref ,seq-arg ,index))
193                                         (list `(first ,index))))
194                                     seq-args seq-supertypes indices)))
195                (multiple-value-bind (push-dacc final-result)
196                    (ecase result-supertype
197                      (null (values nil nil))
198                      (list (values `(push dacc acc) `(nreverse acc)))
199                      (vector (values `(push dacc acc)
200                                      `(coerce (nreverse acc)
201                                               ',result-type-value))))
202                  ;; (We use the same idiom, of returning a LAMBDA from
203                  ;; DEFTRANSFORM, as is used in the DEFTRANSFORMs for
204                  ;; FUNCALL and ALIEN-FUNCALL, and for the same
205                  ;; reason: we need to get the runtime values of each
206                  ;; of the &REST vars.)
207                  `(lambda (result-type fun ,@seq-args)
208                     (declare (ignore result-type))
209                     (do ((really-fun (%coerce-callable-to-function fun))
210                          ,@index-bindingoids
211                          (acc nil))
212                     ((or ,@tests)
213                      ,final-result)
214                     (declare ,@index-decls)
215                     (declare (type list acc))
216                     (declare (ignorable acc))
217                     (let ((dacc (funcall really-fun ,@values)))
218                       (declare (ignorable dacc))
219                       ,push-dacc))))))))))
220 \f
221 (deftransform elt ((s i) ((simple-array * (*)) *) * :when :both)
222   '(aref s i))
223
224 (deftransform elt ((s i) (list *) * :when :both)
225   '(nth i s))
226
227 (deftransform %setelt ((s i v) ((simple-array * (*)) * *) * :when :both)
228   '(%aset s i v))
229
230 (deftransform %setelt ((s i v) (list * *))
231   '(setf (car (nthcdr i s)) v))
232
233 ;;; FIXME: I still think (DOLIST (..) (DEFTRANSFORM ..)) is weird.
234 ;;; For that matter, it would be nice to use DEF-FROB for these
235 ;;; sorts of things, so folks looking for the definitions of
236 ;;; FOO can search for '\(def.*\<foo\>' and have a chance in hell..
237 (dolist (name '(member memq))
238   (deftransform name ((e l &key (test #'eql)) '* '* :node node :when :both
239                       :eval-name t)
240     (unless (constant-continuation-p l)
241       (give-up-ir1-transform))
242
243     (let ((val (continuation-value l)))
244       (unless (policy node
245                       (or (= speed 3)
246                           (and (>= speed space)
247                                (<= (length val) 5))))
248         (give-up-ir1-transform))
249
250       (labels ((frob (els)
251                  (if els
252                      `(if (funcall test e ',(car els))
253                           ',els
254                           ,(frob (cdr els)))
255                      'nil)))
256         (frob val)))))
257
258 ;;; FIXME: Rewrite this so that these definitions of DELETE, ASSOC, and MEMBER
259 ;;; are lexically findable:
260 ;;; (MACROLET ((DEF-FROB (X Y) ..))
261 ;;;   (DEF-FROB DELETE DELQ)
262 ;;;   (DEF-FROB ASSOC ASSQ)
263 ;;;   (DEF-FROB MEMBER MEMQ))
264 ;;; And while I'm at it, I could save a few byte by implementing the
265 ;;; transform body as call to a shared function instead of duplicated
266 ;;; macroexpanded code.
267 (dolist (x '((delete delq)
268              (assoc assq)
269              (member memq)))
270   (destructuring-bind (fun eq-fun) x
271     (deftransform fun ((item list &key test) '(t list &rest t) '*
272                         :eval-name t)
273       "convert to EQ test"
274       ;; FIXME: The scope of this transformation could be widened somewhat,
275       ;; letting it work whenever the test is 'EQL and we know from the
276       ;; type of ITEM that it #'EQ works like #'EQL on it. (E.g. types
277       ;; FIXNUM, CHARACTER, and SYMBOL.)
278       ;;   If TEST is EQ, apply transform, else
279       ;;   if test is not EQL, then give up on transform, else
280       ;;   if ITEM is not a NUMBER or is a FIXNUM, apply transform, else
281       ;;   give up on transform.
282       (cond (test
283              (unless (continuation-function-is test '(eq))
284                (give-up-ir1-transform)))
285             ((types-intersect (continuation-type item)
286                               (specifier-type 'number))
287              (give-up-ir1-transform "Item might be a number.")))
288       `(,eq-fun item list))))
289
290 (deftransform delete-if ((pred list) (t list))
291   "open code"
292   '(do ((x list (cdr x))
293         (splice '()))
294        ((endp x) list)
295      (cond ((funcall pred (car x))
296             (if (null splice)
297                 (setq list (cdr x))
298                 (rplacd splice (cdr x))))
299            (T (setq splice x)))))
300
301 (deftransform fill ((seq item &key (start 0) (end (length seq)))
302                     (simple-array t &key (:start t) (:end index)))
303   "open code"
304   '(do ((i start (1+ i)))
305        ((= i end) seq)
306      (declare (type index i))
307      (setf (aref seq i) item)))
308
309 (deftransform position ((item list &key (test #'eql)) (t list))
310   "open code"
311   '(do ((i 0 (1+ i))
312         (l list (cdr l)))
313        ((endp l) nil)
314      (declare (type index i))
315      (when (funcall test item (car l)) (return i))))
316
317 (deftransform position ((item vec &key (test #'eql) (start 0)
318                               (end (length vec)))
319                         (t simple-array &key (:start t) (:end index)))
320   "open code"
321   '(do ((i start (1+ i)))
322        ((= i end) nil)
323      (declare (type index i))
324      (when (funcall test item (aref vec i)) (return i))))
325
326 ;;; names of predicates that compute the same value as CHAR= when
327 ;;; applied to characters
328 (defconstant char=-functions '(eql equal char=))
329
330 (deftransform search ((string1 string2 &key (start1 0) end1 (start2 0) end2
331                                test)
332                       (simple-string simple-string &rest t))
333   (unless (or (not test)
334               (continuation-function-is test char=-functions))
335     (give-up-ir1-transform))
336   '(sb!impl::%sp-string-search string1 start1 (or end1 (length string1))
337                                string2 start2 (or end2 (length string2))))
338
339 (deftransform position ((item sequence &key from-end test (start 0) end)
340                         (t simple-string &rest t))
341   (unless (or (not test)
342               (continuation-function-is test char=-functions))
343     (give-up-ir1-transform))
344   `(and (typep item 'character)
345         (,(if (constant-value-or-lose from-end)
346               'sb!impl::%sp-reverse-find-character
347               'sb!impl::%sp-find-character)
348          sequence start (or end (length sequence))
349          item)))
350
351 (deftransform find ((item sequence &key from-end (test #'eql) (start 0) end)
352                     (t simple-string &rest t))
353   `(if (position item sequence
354                  ,@(when from-end `(:from-end from-end))
355                  :test test :start start :end end)
356        item
357        nil))
358 \f
359 ;;;; utilities
360
361 ;;; Return true if Cont's only use is a non-notinline reference to a global
362 ;;; function with one of the specified Names.
363 (defun continuation-function-is (cont names)
364   (declare (type continuation cont) (list names))
365   (let ((use (continuation-use cont)))
366     (and (ref-p use)
367          (let ((leaf (ref-leaf use)))
368            (and (global-var-p leaf)
369                 (eq (global-var-kind leaf) :global-function)
370                 (not (null (member (leaf-name leaf) names :test #'equal))))))))
371
372 ;;; If Cont is a constant continuation, the return the constant value. If
373 ;;; it is null, then return default, otherwise quietly GIVE-UP.
374 ;;; ### Probably should take an ARG and flame using the NAME.
375 (defun constant-value-or-lose (cont &optional default)
376   (declare (type (or continuation null) cont))
377   (cond ((not cont) default)
378         ((constant-continuation-p cont)
379          (continuation-value cont))
380         (t
381          (give-up-ir1-transform))))
382
383 #|
384 ;;; This is a frob whose job it is to make it easier to pass around the
385 ;;; arguments to IR1 transforms. It bundles together the name of the argument
386 ;;; (which should be referenced in any expansion), and the continuation for
387 ;;; that argument (or NIL if unsupplied.)
388 (defstruct (arg (:constructor %make-arg (name cont)))
389   (name nil :type symbol)
390   (cont nil :type (or continuation null)))
391 (defmacro make-arg (name)
392   `(%make-arg ',name ,name))
393
394 ;;; If Arg is null or its CONT is null, then return Default, otherwise
395 ;;; return Arg's NAME.
396 (defun default-arg (arg default)
397   (declare (type (or arg null) arg))
398   (if (and arg (arg-cont arg))
399       (arg-name arg)
400       default))
401
402 ;;; If Arg is null or has no CONT, return the default. Otherwise, Arg's
403 ;;; CONT must be a constant continuation whose value we return. If not, we
404 ;;; give up.
405 (defun arg-constant-value (arg default)
406   (declare (type (or arg null) arg))
407   (if (and arg (arg-cont arg))
408       (let ((cont (arg-cont arg)))
409         (unless (constant-continuation-p cont)
410           (give-up-ir1-transform "Argument is not constant: ~S."
411                                  (arg-name arg)))
412         (continuation-value from-end))
413       default))
414
415 ;;; If Arg is a constant and is EQL to X, then return T, otherwise NIL. If
416 ;;; Arg is NIL or its CONT is NIL, then compare to the default.
417 (defun arg-eql (arg default x)
418   (declare (type (or arg null) x))
419   (if (and arg (arg-cont arg))
420       (let ((cont (arg-cont arg)))
421         (and (constant-continuation-p cont)
422              (eql (continuation-value cont) x)))
423       (eql default x)))
424
425 (defstruct iterator
426   ;; The kind of iterator.
427   (kind nil (member :normal :result))
428   ;; A list of LET* bindings to create the initial state.
429   (binds nil :type list)
430   ;; A list of declarations for Binds.
431   (decls nil :type list)
432   ;; A form that returns the current value. This may be set with SETF to set
433   ;; the current value.
434   (current (error "Must specify CURRENT."))
435   ;; In a :Normal iterator, a form that tests whether there is a current value.
436   (done nil)
437   ;; In a :Result iterator, a form that truncates the result at the current
438   ;; position and returns it.
439   (result nil)
440   ;; A form that returns the initial total number of values. The result is
441   ;; undefined after NEXT has been evaluated.
442   (length (error "Must specify LENGTH."))
443   ;; A form that advances the state to the next value. It is an error to call
444   ;; this when the iterator is Done.
445   (next (error "Must specify NEXT.")))
446
447 ;;; Type of an index var that can go negative (in the from-end case.)
448 (deftype neg-index ()
449   `(integer -1 ,most-positive-fixnum))
450
451 ;;; Return an ITERATOR structure describing how to iterate over an arbitrary
452 ;;; sequence. Sequence is a variable bound to the sequence, and Type is the
453 ;;; type of the sequence. If true, INDEX is a variable that should be bound to
454 ;;; the index of the current element in the sequence.
455 ;;;
456 ;;; If we can't tell whether the sequence is a list or a vector, or whether
457 ;;; the iteration is forward or backward, then GIVE-UP.
458 (defun make-sequence-iterator (sequence type &key start end from-end index)
459   (declare (symbol sequence) (type ctype type)
460            (type (or arg null) start end from-end)
461            (type (or symbol null) index))
462   (let ((from-end (arg-constant-value from-end nil)))
463     (cond ((csubtypep type (specifier-type 'vector))
464            (let* ((n-stop (gensym))
465                   (n-idx (or index (gensym)))
466                   (start (default-arg 0 start))
467                   (end (default-arg `(length ,sequence) end)))
468              (make-iterator
469               :kind :normal
470               :binds `((,n-idx ,(if from-end `(1- ,end) ,start))
471                        (,n-stop ,(if from-end `(1- ,start) ,end)))
472               :decls `((type neg-index ,n-idx ,n-stop))
473               :current `(aref ,sequence ,n-idx)
474               :done `(,(if from-end '<= '>=) ,n-idx ,n-stop)
475               :next `(setq ,n-idx
476                            ,(if from-end `(1- ,n-idx) `(1+ ,n-idx)))
477               :length (if from-end
478                           `(- ,n-idx ,n-stop)
479                           `(- ,n-stop ,n-idx)))))
480           ((csubtypep type (specifier-type 'list))
481            (let* ((n-stop (if (and end (not from-end)) (gensym) nil))
482                   (n-current (gensym))
483                   (start-p (not (arg-eql start 0 0)))
484                   (end-p (not (arg-eql end nil nil)))
485                   (start (default-arg start 0))
486                   (end (default-arg end nil)))
487              (make-iterator
488               :binds `((,n-current
489                         ,(if from-end
490                              (if (or start-p end-p)
491                                  `(nreverse (subseq ,sequence ,start
492                                                     ,@(when end `(,end))))
493                                  `(reverse ,sequence))
494                              (if start-p
495                                  `(nthcdr ,start ,sequence)
496                                  sequence)))
497                        ,@(when n-stop
498                            `((,n-stop (nthcdr (the index
499                                                    (- ,end ,start))
500                                               ,n-current))))
501                        ,@(when index
502                            `((,index ,(if from-end `(1- ,end) start)))))
503               :kind :normal
504               :decls `((list ,n-current ,n-end)
505                        ,@(when index `((type neg-index ,index))))
506               :current `(car ,n-current)
507               :done `(eq ,n-current ,n-stop)
508               :length `(- ,(or end `(length ,sequence)) ,start)
509               :next `(progn
510                        (setq ,n-current (cdr ,n-current))
511                        ,@(when index
512                            `((setq ,n-idx
513                                    ,(if from-end
514                                         `(1- ,index)
515                                         `(1+ ,index)))))))))
516           (t
517            (give-up-ir1-transform
518             "can't tell whether sequence is a list or a vector")))))
519
520 ;;; Make an iterator used for constructing result sequences. Name is a
521 ;;; variable to be bound to the result sequence. Type is the type of result
522 ;;; sequence to make. Length is an expression to be evaluated to get the
523 ;;; maximum length of the result (not evaluated in list case.)
524 (defun make-result-sequence-iterator (name type length)
525   (declare (symbol name) (type ctype type))
526
527 ;;; Defines each Name as a local macro that will call the value of the
528 ;;; Fun-Arg with the given arguments. If the argument isn't known to be a
529 ;;; function, give them an efficiency note and reference a coerced version.
530 (defmacro coerce-functions (specs &body body)
531   #!+sb-doc
532   "COERCE-FUNCTIONS ({(Name Fun-Arg Default)}*) Form*"
533   (collect ((binds)
534             (defs))
535     (dolist (spec specs)
536       `(let ((body (progn ,@body))
537              (n-fun (arg-name ,(second spec)))
538              (fun-cont (arg-cont ,(second spec))))
539          (cond ((not fun-cont)
540                 `(macrolet ((,',(first spec) (&rest args)
541                              `(,',',(third spec) ,@args)))
542                    ,body))
543                ((not (csubtypep (continuation-type fun-cont)
544                                 (specifier-type 'function)))
545                 (when (policy *compiler-error-context* (> speed brevity))
546                   (compiler-note
547                    "~S may not be a function, so must coerce at run-time."
548                    n-fun))
549                 (once-only ((n-fun `(if (functionp ,n-fun)
550                                         ,n-fun
551                                         (symbol-function ,n-fun))))
552                   `(macrolet ((,',(first spec) (&rest args)
553                                `(funcall ,',n-fun ,@args)))
554                      ,body)))
555                (t
556                 `(macrolet ((,',(first spec) (&rest args)
557                               `(funcall ,',n-fun ,@args)))
558                    ,body)))))))
559
560 ;;; Wrap code around the result of the body to define Name as a local macro
561 ;;; that returns true when its arguments satisfy the test according to the Args
562 ;;; Test and Test-Not. If both Test and Test-Not are supplied, abort the
563 ;;; transform.
564 (defmacro with-sequence-test ((name test test-not) &body body)
565   `(let ((not-p (arg-cont ,test-not)))
566      (when (and (arg-cont ,test) not-p)
567        (abort-ir1-transform "Both ~S and ~S were supplied."
568                             (arg-name ,test)
569                             (arg-name ,test-not)))
570      (coerce-functions ((,name (if not-p ,test-not ,test) eql))
571        ,@body)))
572 |#
573 \f
574 ;;;; hairy sequence transforms
575
576 ;;; FIXME: no hairy sequence transforms in SBCL?
577 \f
578 ;;;; string operations
579
580 ;;; We transform the case-sensitive string predicates into a non-keyword
581 ;;; version. This is an IR1 transform so that we don't have to worry about
582 ;;; changing the order of evaluation.
583 (dolist (stuff '((string< string<*)
584                  (string> string>*)
585                  (string<= string<=*)
586                  (string>= string>=*)
587                  (string= string=*)
588                  (string/= string/=*)))
589   (destructuring-bind (fun pred*) stuff
590     (deftransform fun ((string1 string2 &key (start1 0) end1
591                                 (start2 0) end2)
592                        '* '* :eval-name t)
593       `(,pred* string1 string2 start1 end1 start2 end2))))
594
595 ;;; Return a form that tests the free variables STRING1 and STRING2 for the
596 ;;; ordering relationship specified by Lessp and Equalp. The start and end are
597 ;;; also gotten from the environment. Both strings must be simple strings.
598 (dolist (stuff '((string<* t nil)
599                  (string<=* t t)
600                  (string>* nil nil)
601                  (string>=* nil t)))
602   (destructuring-bind (name lessp equalp) stuff
603     (deftransform name ((string1 string2 start1 end1 start2 end2)
604                         '(simple-string simple-string t t t t) '*
605                         :eval-name t)
606       `(let* ((end1 (if (not end1) (length string1) end1))
607               (end2 (if (not end2) (length string2) end2))
608               (index (sb!impl::%sp-string-compare
609                       string1 start1 end1 string2 start2 end2)))
610          (if index
611              (cond ((= index ,(if lessp 'end1 'end2)) index)
612                    ((= index ,(if lessp 'end2 'end1)) nil)
613                    ((,(if lessp 'char< 'char>)
614                      (schar string1 index)
615                      (schar string2
616                             (truly-the index
617                                        (+ index
618                                           (truly-the fixnum
619                                                      (- start2 start1))))))
620                     index)
621                    (t nil))
622              ,(if equalp 'end1 'nil))))))
623
624 (dolist (stuff '((string=* not)
625                  (string/=* identity)))
626   (destructuring-bind (name result-fun) stuff
627     (deftransform name ((string1 string2 start1 end1 start2 end2)
628                         '(simple-string simple-string t t t t) '*
629                         :eval-name t)
630       `(,result-fun
631         (sb!impl::%sp-string-compare
632          string1 start1 (or end1 (length string1))
633          string2 start2 (or end2 (length string2)))))))