Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / emacs-lisp / byte-opt.el
index c34c88c..0f4018d 100644 (file)
@@ -1,12 +1,12 @@
 ;;; byte-opt.el --- the optimization passes of the emacs-lisp byte compiler
 
-;; Copyright (C) 1991, 1994, 2000, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1991, 1994, 2000-2011  Free Software Foundation, Inc.
 
 ;; Author: Jamie Zawinski <jwz@lucid.com>
 ;;     Hallvard Furuseth <hbf@ulrik.uio.no>
 ;; Maintainer: FSF
 ;; Keywords: internal
+;; Package: emacs
 
 ;; This file is part of GNU Emacs.
 
                form))
          ((or (byte-code-function-p fn)
               (eq 'lambda (car-safe fn)))
-           (byte-optimize-form-code-walker
-            (byte-compile-unfold-lambda form)
-            for-effect))
+          (let ((newform (byte-compile-unfold-lambda form)))
+            (if (eq newform form)
+                ;; Some error occurred, avoid infinite recursion
+                form
+              (byte-optimize-form-code-walker newform for-effect))))
          ((memq fn '(let let*))
           ;; recursively enter the optimizer for the bindings and body
           ;; of a let or let*.  This for depth-firstness: forms that
                                (list (apply fun (nreverse constants)))))))))
     form))
 
+(defsubst byte-compile-butlast (form)
+  (nreverse (cdr (reverse form))))
+
 (defun byte-optimize-plus (form)
-  (setq form (byte-optimize-delay-constants-math form 1 '+))
+  ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
+  ;;(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)))
-;;;  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))
-       ((null (cddr form))
-        (if (numberp (nth 1 form))
-            (nth 1 form)
-          form))
-       ((and (null (nthcdr 3 form))
-             (or (memq (nth 1 form) '(1 -1))
-                 (memq (nth 2 form) '(1 -1))))
-        ;; Optimize (+ x 1) into (1+ x) and (+ x -1) into (1- x).
-        (let ((integer
-               (if (memq (nth 1 form) '(1 -1))
-                   (nth 1 form)
-                 (nth 2 form)))
-              (other
-               (if (memq (nth 1 form) '(1 -1))
-                   (nth 2 form)
-                 (nth 1 form))))
-          (list (if (eq integer 1) '1+ '1-)
-                other)))
-       (t form)))
+  ;; For (+ constants...), byte-optimize-predicate does the work.
+  (when (memq nil (mapcar 'numberp (cdr form)))
+    (cond
+     ;; (+ x 1) --> (1+ x) and (+ x -1) --> (1- x).
+     ((and (= (length form) 3)
+          (or (memq (nth 1 form) '(1 -1))
+              (memq (nth 2 form) '(1 -1))))
+      (let (integer other)
+       (if (memq (nth 1 form) '(1 -1))
+           (setq integer (nth 1 form) other (nth 2 form))
+         (setq integer (nth 2 form) other (nth 1 form)))
+       (setq form
+             (list (if (eq integer 1) '1+ '1-) other))))
+     ;; Here, we could also do
+     ;;  (+ x y ... 1) --> (1+ (+ x y ...))
+     ;;  (+ x y ... -1) --> (1- (+ x y ...))
+     ;; The resulting bytecode is smaller, but is it faster? -- cyd
+     ))
+  (byte-optimize-predicate form))
 
 (defun byte-optimize-minus (form)
-  ;; Put constants at the end, except the last constant.
-  (setq form (byte-optimize-delay-constants-math form 2 '+))
-  ;; Now only first and last element can be a number.
-  (let ((last (car (reverse (nthcdr 3 form)))))
-    (cond ((eq 0 last)
-          ;; (- x y ... 0)  --> (- x y ...)
-          (setq form (copy-sequence form))
-          (setcdr (cdr (cdr form)) (delq 0 (nthcdr 3 form))))
-         ((equal (nthcdr 2 form) '(1))
-          (setq form (list '1- (nth 1 form))))
-         ((equal (nthcdr 2 form) '(-1))
-          (setq form (list '1+ (nth 1 form))))
-         ;; If form is (- CONST foo... CONST), merge first and last.
-         ((and (numberp (nth 1 form))
-               (numberp last))
-          (setq form (nconc (list '- (- (nth 1 form) last) (nth 2 form))
-                            (delq last (copy-sequence (nthcdr 3 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).
-;;;  (if (eq (nth 2 form) 0)
-;;;      (nth 1 form)                  ; (- x 0)  -->  x
-    (byte-optimize-predicate
-     (if (and (null (cdr (cdr (cdr form))))
-             (eq (nth 1 form) 0))      ; (- 0 x)  -->  (- x)
-        (cons (car form) (cdr (cdr form)))
-       form))
-;;;    )
-  )
+  ;; Don't call `byte-optimize-delay-constants-math' (bug#1334).
+  ;;(setq form (byte-optimize-delay-constants-math form 2 '+))
+  ;; Remove zeros.
+  (when (and (nthcdr 3 form)
+            (memq 0 (cddr form)))
+    (setq form (nconc (list (car form) (cadr form))
+                     (delq 0 (copy-sequence (cddr form)))))
+    ;; After the above, we must turn (- x) back into (- x 0)
+    (or (cddr form)
+       (setq form (nconc form (list 0)))))
+  ;; For (- constants..), byte-optimize-predicate does the work.
+  (when (memq nil (mapcar 'numberp (cdr form)))
+    (cond
+     ;; (- x 1) --> (1- x)
+     ((equal (nthcdr 2 form) '(1))
+      (setq form (list '1- (nth 1 form))))
+     ;; (- x -1) --> (1+ x)
+     ((equal (nthcdr 2 form) '(-1))
+      (setq form (list '1+ (nth 1 form))))
+     ;; (- 0 x) --> (- x)
+     ((and (eq (nth 1 form) 0)
+          (= (length form) 3))
+      (setq form (list '- (nth 2 form))))
+     ;; Here, we could also do
+     ;;  (- x y ... 1) --> (1- (- x y ...))
+     ;;  (- x y ... -1) --> (1+ (- x y ...))
+     ;; The resulting bytecode is smaller, but is it faster? -- cyd
+     ))
+  (byte-optimize-predicate form))
 
 (defun byte-optimize-multiply (form)
   (setq form (byte-optimize-delay-constants-math form 1 '*))
-  ;; If there is a constant in FORM, it is now the last element.
-  (cond ((null (cdr form)) 1)
-;;;  It is not safe to delete the function entirely
-;;;  (actually, it would be safe if we know the sole arg
-;;;  is not a marker or if it appears in other arithmetic).
-;;;    ((null (cdr (cdr form))) (nth 1 form))
-       ((let ((last (car (reverse form))))
-          (cond ((eq 0 last)  (cons 'progn (cdr form)))
-                ((eq 1 last)  (delq 1 (copy-sequence form)))
-                ((eq -1 last) (list '- (delq -1 (copy-sequence form))))
-                ((and (eq 2 last)
-                      (memq t (mapcar 'symbolp (cdr form))))
-                 (prog1 (setq form (delq 2 (copy-sequence form)))
-                   (while (not (symbolp (car (setq form (cdr form))))))
-                   (setcar form (list '+ (car form) (car form)))))
-                (form))))))
-
-(defsubst byte-compile-butlast (form)
-  (nreverse (cdr (reverse form))))
+  ;; For (* constants..), byte-optimize-predicate does the work.
+  (when (memq nil (mapcar 'numberp (cdr form)))
+    ;; After `byte-optimize-predicate', if there is a INTEGER constant
+    ;; in FORM, it is in the last element.
+    (let ((last (car (reverse (cdr form)))))
+      (cond
+       ;; Would handling (* ... 0) here cause floating point errors?
+       ;; See bug#1334.
+       ((eq 1 last) (setq form (byte-compile-butlast form)))
+       ((eq -1 last)
+       (setq form (list '- (if (nthcdr 3 form)
+                               (byte-compile-butlast form)
+                             (nth 1 form))))))))
+  (byte-optimize-predicate form))
 
 (defun byte-optimize-divide (form)
   (setq form (byte-optimize-delay-constants-math form 2 '*))
+  ;; After `byte-optimize-predicate', if there is a INTEGER constant
+  ;; in FORM, it is in the last element.
   (let ((last (car (reverse (cdr (cdr form))))))
-    (if (numberp last)
-       (cond ((= (length form) 3)
-              (if (and (numberp (nth 1 form))
-                       (not (zerop last))
-                       (condition-case nil
-                           (/ (nth 1 form) last)
-                         (error nil)))
-                  (setq form (list 'progn (/ (nth 1 form) last)))))
-             ((= last 1)
-              (setq form (byte-compile-butlast form)))
-             ((numberp (nth 1 form))
-              (setq form (cons (car form)
-                               (cons (/ (nth 1 form) last)
-                                     (byte-compile-butlast (cdr (cdr form)))))
-                    last nil))))
     (cond
-;;;      ((null (cdr (cdr form)))
-;;;       (nth 1 form))
-         ((eq (nth 1 form) 0)
-          (append '(progn) (cdr (cdr form)) '(0)))
-         ((eq last -1)
-          (list '- (if (nthcdr 3 form)
-                       (byte-compile-butlast form)
-                     (nth 1 form))))
-         (form))))
+     ;; Runtime error (leave it intact).
+     ((or (null last)
+         (eq last 0)
+         (memql 0.0 (cddr form))))
+     ;; No constants in expression
+     ((not (numberp last)))
+     ;; For (* constants..), byte-optimize-predicate does the work.
+     ((null (memq nil (mapcar 'numberp (cdr form)))))
+     ;; (/ x y.. 1) --> (/ x y..)
+     ((and (eq last 1) (nthcdr 3 form))
+      (setq form (byte-compile-butlast form)))
+     ;; (/ x -1), (/ x .. -1)  --> (- x), (- (/ x ..))
+     ((eq last -1)
+      (setq form (list '- (if (nthcdr 3 form)
+                             (byte-compile-butlast form)
+                           (nth 1 form)))))))
+  (byte-optimize-predicate form))
 
 (defun byte-optimize-logmumble (form)
   (setq form (byte-optimize-delay-constants-math form 1 (car form)))
         next-window nth nthcdr number-to-string
         parse-colon-path plist-get plist-member
         prefix-numeric-value previous-window prin1-to-string propertize
+        degrees-to-radians
         radians-to-degrees rassq rassoc read-from-string regexp-quote
         region-beginning region-end reverse round
         sin sqrt string string< string= string-equal string-lessp string-to-char
   "Don't call this!"
   ;; fetch and return the offset for the current opcode.
   ;; return nil if this opcode has no offset
-  ;; OP, PTR and BYTES are used and set dynamically
-  (defvar op)
-  (defvar ptr)
-  (defvar bytes)
-  (cond ((< op byte-nth)
-        (let ((tem (logand op 7)))
-          (setq op (logand op 248))
+  ;; Used and set dynamically in byte-decompile-bytecode-1.
+  (defvar bytedecomp-op)
+  (defvar bytedecomp-ptr)
+  (defvar bytedecomp-bytes)
+  (cond ((< bytedecomp-op byte-nth)
+        (let ((tem (logand bytedecomp-op 7)))
+          (setq bytedecomp-op (logand bytedecomp-op 248))
           (cond ((eq tem 6)
-                 (setq ptr (1+ ptr))   ;offset in next byte
-                 (aref bytes ptr))
+                 ;; Offset in next byte.
+                 (setq bytedecomp-ptr (1+ bytedecomp-ptr))
+                 (aref bytedecomp-bytes bytedecomp-ptr))
                 ((eq tem 7)
-                 (setq ptr (1+ ptr))   ;offset in next 2 bytes
-                 (+ (aref bytes ptr)
-                    (progn (setq ptr (1+ ptr))
-                           (lsh (aref bytes ptr) 8))))
+                 ;; Offset in next 2 bytes.
+                 (setq bytedecomp-ptr (1+ bytedecomp-ptr))
+                 (+ (aref bytedecomp-bytes bytedecomp-ptr)
+                    (progn (setq bytedecomp-ptr (1+ bytedecomp-ptr))
+                           (lsh (aref bytedecomp-bytes bytedecomp-ptr) 8))))
                 (t tem))))             ;offset was in opcode
-       ((>= op byte-constant)
-        (prog1 (- op byte-constant)    ;offset in opcode
-          (setq op byte-constant)))
-       ((and (>= op byte-constant2)
-             (<= op byte-goto-if-not-nil-else-pop))
-        (setq ptr (1+ ptr))            ;offset in next 2 bytes
-        (+ (aref bytes ptr)
-           (progn (setq ptr (1+ ptr))
-                  (lsh (aref bytes ptr) 8))))
-       ((and (>= op byte-listN)
-             (<= op byte-insertN))
-        (setq ptr (1+ ptr))            ;offset in next byte
-        (aref bytes ptr))))
+       ((>= bytedecomp-op byte-constant)
+        (prog1 (- bytedecomp-op byte-constant) ;offset in opcode
+          (setq bytedecomp-op byte-constant)))
+       ((and (>= bytedecomp-op byte-constant2)
+             (<= bytedecomp-op byte-goto-if-not-nil-else-pop))
+        ;; Offset in next 2 bytes.
+        (setq bytedecomp-ptr (1+ bytedecomp-ptr))
+        (+ (aref bytedecomp-bytes bytedecomp-ptr)
+           (progn (setq bytedecomp-ptr (1+ bytedecomp-ptr))
+                  (lsh (aref bytedecomp-bytes bytedecomp-ptr) 8))))
+       ((and (>= bytedecomp-op byte-listN)
+             (<= bytedecomp-op byte-insertN))
+        (setq bytedecomp-ptr (1+ bytedecomp-ptr)) ;offset in next byte
+        (aref bytedecomp-bytes bytedecomp-ptr))))
 
 
 ;; This de-compiler is used for inline expansion of compiled functions,
 ;; If MAKE-SPLICEABLE is nil, we are being called for the disassembler.
 ;; In that case, we put a pc value into the list
 ;; before each insn (or its label).
-(defun byte-decompile-bytecode-1 (bytes constvec &optional make-spliceable)
-  (let ((length (length bytes))
-       (ptr 0) optr tags op offset
+(defun byte-decompile-bytecode-1 (bytedecomp-bytes constvec
+                                                  &optional make-spliceable)
+  (let ((length (length bytedecomp-bytes))
+       (bytedecomp-ptr 0) optr tags bytedecomp-op offset
        lap tmp
        endtag)
-    (while (not (= ptr length))
+    (while (not (= bytedecomp-ptr length))
       (or make-spliceable
-         (setq lap (cons ptr lap)))
-      (setq op (aref bytes ptr)
-           optr ptr
+         (setq lap (cons bytedecomp-ptr lap)))
+      (setq bytedecomp-op (aref bytedecomp-bytes bytedecomp-ptr)
+           optr bytedecomp-ptr
            offset (disassemble-offset)) ; this does dynamic-scope magic
-      (setq op (aref byte-code-vector op))
-      (cond ((memq op byte-goto-ops)
+      (setq bytedecomp-op (aref byte-code-vector bytedecomp-op))
+      (cond ((memq bytedecomp-op byte-goto-ops)
             ;; it's a pc
             (setq offset
                   (cdr (or (assq offset tags)
                                       (cons (cons offset
                                                   (byte-compile-make-tag))
                                             tags)))))))
-           ((cond ((eq op 'byte-constant2) (setq op 'byte-constant) t)
-                  ((memq op byte-constref-ops)))
+           ((cond ((eq bytedecomp-op 'byte-constant2)
+                   (setq bytedecomp-op 'byte-constant) t)
+                  ((memq bytedecomp-op byte-constref-ops)))
             (setq tmp (if (>= offset (length constvec))
                           (list 'out-of-range offset)
                         (aref constvec offset))
-                  offset (if (eq op 'byte-constant)
+                  offset (if (eq bytedecomp-op 'byte-constant)
                              (byte-compile-get-constant tmp)
                            (or (assq tmp byte-compile-variables)
                                (car (setq byte-compile-variables
                                           (cons (list tmp)
                                                 byte-compile-variables)))))))
            ((and make-spliceable
-                 (eq op 'byte-return))
-            (if (= ptr (1- length))
-                (setq op nil)
+                 (eq bytedecomp-op 'byte-return))
+            (if (= bytedecomp-ptr (1- length))
+                (setq bytedecomp-op nil)
               (setq offset (or endtag (setq endtag (byte-compile-make-tag)))
-                    op 'byte-goto))))
+                    bytedecomp-op 'byte-goto))))
       ;; lap = ( [ (pc . (op . arg)) ]* )
-      (setq lap (cons (cons optr (cons op (or offset 0)))
+      (setq lap (cons (cons optr (cons bytedecomp-op (or offset 0)))
                      lap))
-      (setq ptr (1+ ptr)))
+      (setq bytedecomp-ptr (1+ bytedecomp-ptr)))
     ;; take off the dummy nil op that we replaced a trailing "return" with.
     (let ((rest lap))
       (while rest
@@ -2047,5 +2040,4 @@ If FOR-EFFECT is non-nil, the return value is assumed to be of no importance."
               byte-optimize-lapcode))))
  nil)
 
-;; arch-tag: 0f14076b-737e-4bef-aae6-908826ec1ff1
 ;;; byte-opt.el ends here