XEmacs 21.2.25 "Hephaestus".
[chise/xemacs-chise.git.1] / lisp / byte-optimize.el
index 9d34537..4c57efb 100644 (file)
   (setq form (byte-optimize-delay-constants-math form 1 '+))
   (if (memq 0 form) (setq form (delq 0 (copy-sequence form))))
   ;;(setq form (byte-optimize-associative-two-args-math form))
-  (cond ((null (cdr form))
-        (condition-case ()
-            (eval form)
-          (error form)))
-
-       ;; `add1' and `sub1' are a marginally fewer instructions
-       ;; than `plus' and `minus', so use them when possible.
-       ((and (null (nthcdr 3 form))
-             (eq (nth 2 form) 1))
-        (list '1+ (nth 1 form)))       ; (+ x 1)  -->  (1+ x)
-       ((and (null (nthcdr 3 form))
-             (eq (nth 1 form) 1))
-        (list '1+ (nth 2 form)))       ; (+ 1 x)  -->  (1+ x)
-       ((and (null (nthcdr 3 form))
-             (eq (nth 2 form) -1))
-        (list '1- (nth 1 form)))       ; (+ x -1)  -->  (1- x)
-       ((and (null (nthcdr 3 form))
-             (eq (nth 1 form) -1))
-        (list '1- (nth 2 form)))       ; (+ -1 x)  -->  (1- x)
-
-;;; It is not safe to delete the function entirely
-;;; (actually, it would be safe if we know the sole arg
-;;; is not a marker).
-;;     ((null (cdr (cdr form))) (nth 1 form))
-       (t form)))
+  (case (length (cdr form))
+    ((0)
+     (condition-case ()
+        (eval form)
+       (error form)))
+
+    ;; `add1' and `sub1' are a marginally fewer instructions
+    ;; than `plus' and `minus', so use them when possible.
+    ((2)
+     (cond
+      ((eq (nth 1 form)  1) `(1+ ,(nth 2 form))) ; (+ 1 x)   -->  (1+ x)
+      ((eq (nth 2 form)  1) `(1+ ,(nth 1 form))) ; (+ x 1)   -->  (1+ x)
+      ((eq (nth 1 form) -1) `(1- ,(nth 2 form))) ; (+ -1 x)  -->  (1- x)
+      ((eq (nth 2 form) -1) `(1- ,(nth 1 form))) ; (+ x -1)  -->  (1- x)
+      (t form)))
+
+    ;; It is not safe to delete the function entirely
+    ;; (actually, it would be safe if we know the sole arg
+    ;; is not a marker).
+    ;; ((null (cdr (cdr form))) (nth 1 form))
+    (t form)))
 
 (defun byte-optimize-minus (form)
   ;; Put constants at the end, except the last constant.
                    (setcar form (list '+ (car form) (car form)))))
                 (form))))))
 
-(defsubst byte-compile-butlast (form)
-  (nreverse (cdr (reverse form))))
-
 (defun byte-optimize-divide (form)
   (setq form (byte-optimize-delay-constants-math form 2 '*))
   (let ((last (car (reverse (cdr (cdr form))))))
                          (error nil)))
                   (setq form (list 'progn (/ (nth 1 form) last)))))
              ((= last 1)
-              (setq form (byte-compile-butlast form)))
+              (setq form (butlast form)))
              ((numberp (nth 1 form))
               (setq form (cons (car form)
                                (cons (/ (nth 1 form) last)
-                                     (byte-compile-butlast (cdr (cdr form)))))
+                                     (butlast (cdr (cdr form)))))
                     last nil))))
     (cond 
 ;;;      ((null (cdr (cdr form)))
           (append '(progn) (cdr (cdr form)) '(0)))
          ((eq last -1)
           (list '- (if (nthcdr 3 form)
-                       (byte-compile-butlast form)
+                       (butlast form)
                      (nth 1 form))))
          (form))))
 
 (put 'if    'byte-optimizer 'byte-optimize-if)
 (put 'while 'byte-optimizer 'byte-optimize-while)
 
+;; Remove any reason for avoiding `char-before'.
+(defun byte-optimize-char-before (form)
+  `(char-after (1- ,(or (nth 1 form) '(point))) ,@(cdr (cdr form))))
+     
+(put 'char-before 'byte-optimizer 'byte-optimize-char-before)
+
 ;; byte-compile-negation-optimizer lives in bytecomp.el
 ;(put '/= 'byte-optimizer 'byte-compile-negation-optimizer)
 (put 'atom 'byte-optimizer 'byte-compile-negation-optimizer)