all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* macroexpand and bytecomp.el
       [not found] <f6486604-8a50-4e56-bd88-78e23b50477a@Spark>
@ 2024-02-09 18:32 ` Troy Hinckley
  2024-02-12 21:24   ` Stefan Monnier via Emacs development discussions.
  0 siblings, 1 reply; 2+ messages in thread
From: Troy Hinckley @ 2024-02-09 18:32 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1886 bytes --]

I have a question about how macro-expansion is handled in bytecomp.el. There are two variables byte-code-vector and byte-stack+-info in bytecomp.el. They are populated by calls to byte-defop which will store the values in the tmp-compile-time-value property. After this, the variables are populated by a call to the byte-extrude-byte-code-vectors macro:

(defmacro byte-extrude-byte-code-vectors ()
 (prog1 (list 'setq 'byte-code-vector
 (get 'byte-code-vector 'tmp-compile-time-value)
 'byte-stack+-info
 (get 'byte-stack+-info 'tmp-compile-time-value))
 (put 'byte-code-vector 'tmp-compile-time-value nil)
 (put 'byte-stack+-info 'tmp-compile-time-value nil)))

However I don’t see how this would work with macro expansion. Eager macro expansion is enabled once internal-macroexpand-for-load is defined in macroexp.el (which should be before bytecomp.el is loaded). In lread.c the function load will call readevalloop which calls readevalloop_eager_expand_eval. That function will call internal-macroexpand-for-load at least twice. First time it will only expand the top level form, and then if that is not a progn form it will call it again to expand all forms. However I don’t understand how this would work with byte-extrude-byte-code-vectors. When that macro is called it will set the tmp-compile-time-value properties to nil, meaning that when the macro is called again byte-code-vector and byte-stack+-info will be set to nil as well.

I can see that happen if I load this dummy file

(defvar tmp-var [1 2 3])
(defvar my-var nil)

(defmacro my-extrude ()
 (prog1 (list 'setq 'my-var 'tmp-var)
 (setq tmp-var nil)))

(my-extrude)


Here my-var is set to nil. I understand why that is happening. What I don’t understand is why does this work in bytecomp.el? Why doesn't it have the issue with double expanded macros?

-Troy

[-- Attachment #2: Type: text/html, Size: 3174 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: macroexpand and bytecomp.el
  2024-02-09 18:32 ` macroexpand and bytecomp.el Troy Hinckley
@ 2024-02-12 21:24   ` Stefan Monnier via Emacs development discussions.
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier via Emacs development discussions. @ 2024-02-12 21:24 UTC (permalink / raw)
  To: emacs-devel

> (defmacro byte-extrude-byte-code-vectors ()
>   (prog1 (list 'setq 'byte-code-vector
>                (get 'byte-code-vector 'tmp-compile-time-value)
>                'byte-stack+-info
>                (get 'byte-stack+-info 'tmp-compile-time-value))
>     (put 'byte-code-vector 'tmp-compile-time-value nil)
>     (put 'byte-stack+-info 'tmp-compile-time-value nil)))
[...]
> (defvar tmp-var [1 2 3])
> (defvar my-var nil)
> (defmacro my-extrude ()
>  (prog1 (list 'setq 'my-var 'tmp-var)
>  (setq tmp-var nil)))
> (my-extrude)

I don't fully understand your question, but notice that in your macro
you use 'tmp-var in the `setq`, i.e. your macro returns code that uses
the name of the variable and not its content (so the variable will be
read *after* when the code is run rather than when it's macro-expanded),
whereas in `byte-extrude-byte-code-vectors` the macro executes
(get 'byte-code-vector 'tmp-compile-time-value) during the macro
expansion and thus returns code which contains the value therein.

IOW, `byte-extrude-byte-code-vectors` is more akin to:

    (defmacro my-extrude ()
      (prog1 (list 'setq 'my-var tmp-var)
        (setq tmp-var nil)))


- Stefan




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-12 21:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <f6486604-8a50-4e56-bd88-78e23b50477a@Spark>
2024-02-09 18:32 ` macroexpand and bytecomp.el Troy Hinckley
2024-02-12 21:24   ` Stefan Monnier via Emacs development discussions.

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.