[imp-hackers] [pro] Declarations in compilers (feedback welcome)
Martin Simmons
martin at lispworks.com
Fri Dec 30 16:29:05 UTC 2011
>>>>> On Thu, 29 Dec 2011 11:24:37 +0100, Juan Jose Garcia-Ripoll said:
>
> After struggling mentally with this for a few weeks, I would like to have
> some consultation before I introduce some changes in ECL -- not that I
> expect many users here, but at least some implementor-fellows and power
> users of other implementations.
>
> My concerns right now relate to how declarations should be used by a
> compiler, and in particular how declarations interact with SAFETY levels.
> Please correct me if I am wrong, but I have seen more or less the following
> approaches
>
> [a]- Most implementations blindly believe declarations below a certain
> safety level. Above it, they seem more or less useless.
>
> [b]- SBCL takes declarations (and THE) as type assertions. For instance, in
> (LET ((Y (FOO X))) (DECLARE (FIXNUM Y))) ...) the assignment to Y would be
> checked to be a FIXNUM. This means the type declaration is actually
> enforced and believed and only at SAFETY 0 the checks are dropped (*)
>
> In both cases one ends up with a model in which in order to truly believe a
> declaration and have no extra burden (assertions), one has to drop to
> SAFETY 0 in all code that is involved with it, which is a mess, because it
> might inadvertently affect other parts of the code. It is for this reason
> that I am considering an alternative model for ECL which would grade safety
> as follows
>
> - Type declarations are always believed
> - SAFETY >= 1 adds type checks to enforce them.
> - SAFETY = 0, no checks.
> - SAFETY = 1, the special form THE or additional proclamations on the
> functions can be used to deactivate the check. As in (LET ((Y (THE FIXNUM
> (FOO X))) ...)
>
> This would allow one to keep most code safe, while deactivating some checks
> when they are really known to be true (**). Do you think this is
> useful/useless? The problem I see with this approach is that all code
> around is written for model [a] or [b], but I could not come up with
> something more sensible so far.
I don't like this because it contradicts the CL spec:
"The meaning of a type declaration is equivalent to changing each reference to
a variable (var) within the scope of the declaration to (the typespec var),
changing each expression assigned to the variable (new-value) within the scope
of the declaration to (the typespec new-value), and executing (the typespec
var) at the moment the scope of the declaration is entered."
(from http://www.lispworks.com/documentation/HyperSpec/Body/d_type.htm).
In LispWorks, type declarations and THE forms have the same semantics and they
are checked when safety = 3 and debug = 3. The reason for involving debug is
that the checking code can be large and relatively slow.
__Martin
More information about the implementation-hackers
mailing list