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