anything like C++ references?

Jeff Epler jepler at unpythonic.net
Thu Jul 17 14:03:59 EDT 2003


On Thu, Jul 17, 2003 at 01:15:03PM -0400, Terry Reedy wrote:
> 
> "Adam Ruth" <owski at hotmail.com> wrote in message
> news:f0f51c80.0307150921.7b6667e2 at posting.google.com...
> > register int x = 5;
> > int *y = &x;
> >
> > This also cannot be done.  Why?  Different semantics for register
> > variables.
> 
> Minor nit: 'register' is not a mandate, but an ignorable suggestion.
> So a compiler either could or must ignore 'register' in able to make
> '&x' possible.

I suspect that the compiler *must* issue a diagnostic for the code in
question.  That phrase, common in the C standard, doesn't mean that the
compiler can't also go on to produce an object file/executable, or that
it might not be operating in conformant mode by default (gcc does many
things a conformant C compiler could not, unless -ansi is specified)

But yes, 'register' was never a mandate.  I suspect that the reason it
was included in C originally was twofold:  First, the compiler was not
powerful enough to take two passes over a function body, one to
determine whether the addressof operator was ever used for a particular
variable, and a second to generate code.  Second, the compiler was also
not powerful enough to do the kind of analysis needed to choose
registers for variables.  With those constraints, suddenly code like
    while(*p++ = *q++) ;
improves by a factor of two or more as soon as you write
    register char *p, *q;
instead of just
    char *p, *q;
... well, that hasn't been the case for 15 years or more now.

> Of course, the idea of ignorable keyword suggestion is itself somewhat
> strange ;-)

Not at all.  Many proposals for efficient translation from Python to
native code include a notion of type hints.  Python can freely ignore
them (or translate them into runtime 'assert isinstance' or 'assert
hasattr' statements) but the new compiler would use them to generate
efficient code using native datatypes...

    def fact(n):
        if n == 0: return 1
        return fact(n-1) * n
    typeassert(RETURN_VALUE, fact, union(int,long))
    typeassert(PARAMAETERS, fact, (int,))

Jeff





More information about the Python-list mailing list