Can compiler macros make use of type declarations?

Chaitanya Gupta mail at chaitanyagupta.com
Thu Nov 9 09:17:53 UTC 2017


I tried this on SBCL:

(defun bar (x)
  x)

(define-compiler-macro bar (&whole form x &environment env)
  (when (symbolp x)
    (print (multiple-value-list (clast:variable-information x env))))
  form)

The form below, with the declaration, prints type information correctly:

(compile nil
         (lambda ()
           (let ((y 10)) (declare (fixnum y)) (bar y))))
>> (:LEXICAL T ((TYPE . FIXNUM)))

However, the one below with no declarations, doesn't give any info:

(compile nil
         (lambda ()
           (let ((y 10)) (bar y))
           (let ((y '(1 2 3))) (bar y))
           (let ((y 'foo)) (bar y))))
>> (:LEXICAL T NIL)
>> (:LEXICAL T NIL)
>> (:LEXICAL T NIL)

So I guess that, at least on SBCL, this is not possible.

Chaitanya


On 9 November 2017 at 14:09, Antoniotti Marco
<antoniotti.marco at disco.unimib.it> wrote:
> CMUCL and SBCL have the Environment API somewhat available (*)
>
> You can see whether they store the inferred values in there.
>
> Marco
>
> (*) Again, you can have a look at CLAST to see how to access it; I know: it is a shameless plug.
>
>
>
>> On Nov 9, 2017, at 09:18 , Chaitanya Gupta <mail at chaitanyagupta.com> wrote:
>>
>> So thanks to the replies on this I now know that most of the popular
>> Lisps do support inspecting the environment to figure out declared
>> types.
>>
>> But what about inferred types (e.g. in CMUCL, SBCL)? Do these Lisps
>> provide a way to know the inferred type of a variable if no
>> declaration was made explicitly?
>>
>> Chaitanya
>>
>> On 7 November 2017 at 02:13, 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
>>
>
> --
> Marco Antoniotti, Associate Professor           tel.    +39 - 02 64 48 79 01
> DISCo, Università Milano Bicocca U14 2043               http://bimib.disco.unimib.it
> Viale Sarca 336
> I-20126 Milan (MI) ITALY
>
> Please check: http://troncopackage.org
>
> Please note that I am not checking my Spam-box anymore.
> Please do not forward this email without asking me first (cum grano salis).
>
>
>
>
>



More information about the pro mailing list