Can compiler macros make use of type declarations?

Steve Haflich shaflich at gmail.com
Tue Nov 7 05:00:27 UTC 2017


A compiler-macro, just like a regular macro, receives an &environment
argument which is supposed to contain (amongst other things) the
lexical and global properties of bindings in the environment in which
the macro is expanded.  In the original language definition proposed
by CLtL  there was  an environment access interface with functions
like variablle-information which could return (for instance) what is
known about the type of A in the lexical environment in which FOO is
expanded.

Unfortunately, the environment access system in CLtL was
underspecified, there were no existing implementations, and no one was
sure an implementation was even possible without imposing large
efficiency hits at compile time.  So we of X3J13 removed it from the
standard.

Duane Rettig of Franz later implemented a portable open-source sane
environment module inspired by the original CLtL specification.  This
can be used to write a portable CL code walker of any kind, but it
won't do you any good unless the code walker that is your favorite
implementation's compiler happens to use it.  But fortunately, several
implementations provide documented environment access interfaces built
into their compilers.  Unfortunately, these interfaces are not defined
by the standard, so their use generally makes code nonportable.

BTW, a plain INTEGER type declaration is often not as effective as
you'd expect because INTEGER type is the union of FIXNUM and BIGNUM.
Arithmetic on the  former can generally be inlined in a few machine
instructions (particularly if the integer range can be narrowed
further) but if  any arguments or the result could be bignums, further
runtime type tests and/or callouts will be generated.


On Mon, Nov 6, 2017 at 12:43 PM, Chaitanya Gupta
<mail at chaitanyagupta.com> wrote:
> Let's say I have created a function called `FOO`:
>
> (defun foo (x)
>   ...)
>
> To optimize this function, I write a compiler macro. Can I make use of
> type declarations that users of `FOO` might have made for the argument
> `X` that is passed in to it?
>
> That is, let's say `FOO` is used like this:
>
> (let ((a (something-that-returns-an-integer)))
>   (declare (integer a))
>   (foo a))
>
> The compiler macro for `FOO` cannot make any optimizations on the
> value of `A`, but can it take advantage of the fact that `A` is
> declared as in `INTEGER` here?
>
> Chaitanya
>



More information about the pro mailing list