]> git.eshelyaron.com Git - emacs.git/commitdiff
Support interpreted functions as input for 'native-compile' (bug#71934)
authorAndrea Corallo <acorallo@gnu.org>
Tue, 9 Jul 2024 19:11:43 +0000 (21:11 +0200)
committerEshel Yaron <me@eshelyaron.com>
Wed, 10 Jul 2024 20:00:01 +0000 (22:00 +0200)
* lisp/emacs-lisp/comp.el (comp--spill-lap-single-function): New function.
(comp--spill-lap-function): Make use of and do not accept
'(closure ...' as input.
(comp--spill-lap-function): Specialize on interpreted functions as
well.
(native-compile): Update doc.
* test/src/comp-tests.el (compile-interpreted-functions): New test.

(cherry picked from commit b9b9322a8e62299a223ff6673ed33de90d513bc4)

lisp/emacs-lisp/comp.el
test/src/comp-tests.el

index 947fb06e602ff627fcfc87878738274c7da129e9..0a2c520c5d537ab64ef88ced45ddbce4ffccb7cc 100644 (file)
@@ -792,21 +792,29 @@ clashes."
                                                   :byte-func byte-code)))
       (maphash #'comp--intern-func-in-ctxt byte-to-native-lambdas-h)))
 
-(cl-defmethod comp--spill-lap-function ((form list))
-  "Byte-compile FORM, spilling data from the byte compiler."
-  (unless (memq (car-safe form) '(lambda closure))
-    (signal 'native-compiler-error
-            '("Cannot native-compile, form is not a lambda or closure")))
+(defun comp--spill-lap-single-function (function)
+  "Byte-compile FUNCTION, spilling data from the byte compiler."
   (unless (comp-ctxt-output comp-ctxt)
     (setf (comp-ctxt-output comp-ctxt)
           (make-temp-file "comp-lambda-" nil ".eln")))
-  (let* ((byte-code (byte-compile form))
+  (let* ((byte-code (byte-compile function))
          (c-name (comp-c-func-name "anonymous-lambda" "F")))
-      (setf (comp-ctxt-top-level-forms comp-ctxt)
-            (list (make-byte-to-native-func-def :name '--anonymous-lambda
-                                                :c-name c-name
-                                                :byte-func byte-code)))
-      (maphash #'comp--intern-func-in-ctxt byte-to-native-lambdas-h)))
+    (setf (comp-ctxt-top-level-forms comp-ctxt)
+          (list (make-byte-to-native-func-def :name '--anonymous-lambda
+                                              :c-name c-name
+                                              :byte-func byte-code)))
+    (maphash #'comp--intern-func-in-ctxt byte-to-native-lambdas-h)))
+
+(cl-defmethod comp--spill-lap-function ((form list))
+  "Byte-compile FORM, spilling data from the byte compiler."
+  (unless (eq (car-safe form) 'lambda)
+    (signal 'native-compiler-error
+            '("Cannot native-compile, form is not a lambda")))
+  (comp--spill-lap-single-function form))
+
+(cl-defmethod comp--spill-lap-function ((fun interpreted-function))
+  "Spill data from the byte compiler for the interpreted-function FUN."
+  (comp--spill-lap-single-function fun))
 
 (defun comp--intern-func-in-ctxt (_ obj)
   "Given OBJ of type `byte-to-native-lambda', create a function in `comp-ctxt'."
@@ -3577,14 +3585,13 @@ Search happens in `native-comp-eln-load-path'."
 ;;;###autoload
 (defun native-compile (function-or-file &optional output)
   "Compile FUNCTION-OR-FILE into native code.
-This is the synchronous entry-point for the Emacs Lisp native
-compiler.  FUNCTION-OR-FILE is a function symbol, a form, or the
-filename of an Emacs Lisp source file.  If OUTPUT is non-nil, use
-it as the filename for the compiled object.  If FUNCTION-OR-FILE
-is a filename, if the compilation was successful return the
-filename of the compiled object.  If FUNCTION-OR-FILE is a
-function symbol or a form, if the compilation was successful
-return the compiled function."
+This is the synchronous entry-point for the Emacs Lisp native compiler.
+FUNCTION-OR-FILE is a function symbol, a form, an interpreted-function,
+or the filename of an Emacs Lisp source file.  If OUTPUT is non-nil, use
+it as the filename for the compiled object.  If FUNCTION-OR-FILE is a
+filename, if the compilation was successful return the filename of the
+compiled object.  If FUNCTION-OR-FILE is a function symbol or a form, if
+the compilation was successful return the compiled function."
   (declare (ftype (function ((or string symbol) &optional string)
                             (or native-comp-function string))))
   (comp--native-compile function-or-file nil output))
index 6744f14435b040544780fcc416d6d4a7fc46bba1..60bc190380118ebbd0a1fd587a0daa7081b2586c 100644 (file)
@@ -504,6 +504,12 @@ https://lists.gnu.org/archive/html/bug-gnu-emacs/2020-03/msg00914.html."
     (should (native-comp-function-p f))
     (should (= (funcall f 2) 3))))
 
+(comp-deftest compile-interpreted-functions ()
+  "Verify native compilation of interpreted functions."
+  (let ((f (native-compile (eval '(lambda (x) (1+ x))))))
+    (should (native-comp-function-p f))
+    (should (= (funcall f 2) 3))))
+
 (comp-deftest comp-test-defsubst ()
   ;; Bug#42664, Bug#43280, Bug#44209.
   (should-not (native-comp-function-p (symbol-function 'comp-test-defsubst-f))))