aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias EngdegÄrd2023-07-27 11:51:26 +0200
committerMattias EngdegÄrd2023-07-27 17:09:30 +0200
commit93eccb5e040c8fff4c4527819888e01683df5aaa (patch)
tree10585a8bb9296f805f450a67fc42e45f08b690ab
parente055c635b0d73efe3826e418690a3d91eee69647 (diff)
downloademacs-93eccb5e040c8fff4c4527819888e01683df5aaa.tar.gz
emacs-93eccb5e040c8fff4c4527819888e01683df5aaa.zip
Better compilation of char-before, backward-char and backward-word
Implement char-before, backward-char and backward-word as compiler macros instead of byte-compile handlers so that the source-level optimiser gets to simplify the result. In particular, this removes some branches. * lisp/emacs-lisp/bytecomp.el (byte-compile-char-before) (byte-compile-backward-char, byte-compile-backward-word): Remove. (bytecomp--char-before, bytecomp--backward-char) (bytecomp--backward-word): New.
-rw-r--r--lisp/emacs-lisp/bytecomp.el59
1 files changed, 22 insertions, 37 deletions
diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index 489a9724fc4..5b1d958e6c2 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -4306,9 +4306,6 @@ This function is never called when `lexical-binding' is nil."
4306 4306
4307;; more complicated compiler macros 4307;; more complicated compiler macros
4308 4308
4309(byte-defop-compiler char-before)
4310(byte-defop-compiler backward-char)
4311(byte-defop-compiler backward-word)
4312(byte-defop-compiler list) 4309(byte-defop-compiler list)
4313(byte-defop-compiler concat) 4310(byte-defop-compiler concat)
4314(byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to) 4311(byte-defop-compiler (indent-to-column byte-indent-to) byte-compile-indent-to)
@@ -4319,40 +4316,6 @@ This function is never called when `lexical-binding' is nil."
4319(byte-defop-compiler (/ byte-quo) byte-compile-quo) 4316(byte-defop-compiler (/ byte-quo) byte-compile-quo)
4320(byte-defop-compiler nconc) 4317(byte-defop-compiler nconc)
4321 4318
4322;; Is this worth it? Both -before and -after are written in C.
4323(defun byte-compile-char-before (form)
4324 (cond ((or (= 1 (length form))
4325 (and (= 2 (length form)) (not (nth 1 form))))
4326 (byte-compile-form '(char-after (1- (point)))))
4327 ((= 2 (length form))
4328 (byte-compile-form (list 'char-after (if (numberp (nth 1 form))
4329 (1- (nth 1 form))
4330 `(1- (or ,(nth 1 form)
4331 (point)))))))
4332 (t (byte-compile-subr-wrong-args form "0-1"))))
4333
4334;; backward-... ==> forward-... with negated argument.
4335;; Is this worth it? Both -backward and -forward are written in C.
4336(defun byte-compile-backward-char (form)
4337 (cond ((or (= 1 (length form))
4338 (and (= 2 (length form)) (not (nth 1 form))))
4339 (byte-compile-form '(forward-char -1)))
4340 ((= 2 (length form))
4341 (byte-compile-form (list 'forward-char (if (numberp (nth 1 form))
4342 (- (nth 1 form))
4343 `(- (or ,(nth 1 form) 1))))))
4344 (t (byte-compile-subr-wrong-args form "0-1"))))
4345
4346(defun byte-compile-backward-word (form)
4347 (cond ((or (= 1 (length form))
4348 (and (= 2 (length form)) (not (nth 1 form))))
4349 (byte-compile-form '(forward-word -1)))
4350 ((= 2 (length form))
4351 (byte-compile-form (list 'forward-word (if (numberp (nth 1 form))
4352 (- (nth 1 form))
4353 `(- (or ,(nth 1 form) 1))))))
4354 (t (byte-compile-subr-wrong-args form "0-1"))))
4355
4356(defun byte-compile-list (form) 4319(defun byte-compile-list (form)
4357 (let ((count (length (cdr form)))) 4320 (let ((count (length (cdr form))))
4358 (cond ((= count 0) 4321 (cond ((= count 0)
@@ -5797,6 +5760,28 @@ and corresponding effects."
5797(put 'remq 'compiler-macro #'bytecomp--check-memq-args) 5760(put 'remq 'compiler-macro #'bytecomp--check-memq-args)
5798(put 'delq 'compiler-macro #'bytecomp--check-memq-args) 5761(put 'delq 'compiler-macro #'bytecomp--check-memq-args)
5799 5762
5763;; Implement `char-before', `backward-char' and `backward-word' in
5764;; terms of `char-after', `forward-char' and `forward-word' which have
5765;; their own byte-ops.
5766
5767(put 'char-before 'compiler-macro #'bytecomp--char-before)
5768(defun bytecomp--char-before (form &optional arg &rest junk-args)
5769 (if junk-args
5770 form ; arity error
5771 `(char-after (1- (or ,arg (point))))))
5772
5773(put 'backward-char 'compiler-macro #'bytecomp--backward-char)
5774(defun bytecomp--backward-char (form &optional arg &rest junk-args)
5775 (if junk-args
5776 form ; arity error
5777 `(forward-char (- (or ,arg 1)))))
5778
5779(put 'backward-word 'compiler-macro #'bytecomp--backward-word)
5780(defun bytecomp--backward-word (form &optional arg &rest junk-args)
5781 (if junk-args
5782 form ; arity error
5783 `(forward-word (- (or ,arg 1)))))
5784
5800(provide 'byte-compile) 5785(provide 'byte-compile)
5801(provide 'bytecomp) 5786(provide 'bytecomp)
5802 5787