chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] More on aliasing.


From: Peter Keller
Subject: Re: [Chicken-users] More on aliasing.
Date: Fri, 5 Jul 2002 10:28:37 -0500
User-agent: Mutt/1.2i

On Fri, Jul 05, 2002 at 03:34:51PM +0000, address@hidden wrote:
> Given the information at
> 
>       http://www.nullstone.com/htmls/category/aliastyp.htm
> 
> ...I think chicken might be in a bit of trouble wrt aliasing, given
> the prevalence of casts back and forth between C_word and C_word* etc
> etc. I can't be certain, but given what I remember of chicken.h and
> generated code, there could well be problems there.
> 
> I've been googling around for keywords like "fstrict-aliasing",
> "dangerous", "safe", "alias optimization" etc... perhaps chicken needs
> to be made alias-safe before strict-aliasing is enabled by default?
> 
> I know next to nothing about this. It might be worthwhile finding a
> real expert :-(
> 
> In the meantime, I think saying "-fno-strict-aliasing" for Sparc
> platforms is probably a good idea. Sven (and any other Sparc users) -
> have you been having any problems with -fstrict-aliasing enabled? Or
> am I just cursed?

I happen to know a little about strict aliasing and aliasing in general in C.

The short story is that you are generally safe in doing "bad things" to
pieces of memory if the memory is _defined_ as being contiguous in memory like
a union or a struct. Off the top of my head:

Doing something as simple as this:

void foo(unsigned long long bar)
{
        char *qux = (char*)&bar;
        unsigned int upper, lower;

        /* suppose 8 byte long */

        upper = *(unsigned int*)&char[0];
        lower = *(unsigned int*)&char[4];
}

is doomed to fail since you don't know if long long is a contiguous piece
of memory(it is a language extension implemented however the compiler sees
fit!). One half of the long long can be in registers, and the other half on
the stack!

The real way to do the above would be to pass it in a union:

union qux { long long feh; };
void foo(union qux bar)
{
        char *qux = &bar.feh;
        unsigned int upper, lower;

        /* suppose 8 byte long */

        upper = *(unsigned int*)&char[0];
        lower = *(unsigned int*)&char[4];
}

Now it will work, because a union is defined to be the shortest contiguous
piece of memory that contains the type.

Basically, if a C_word is an int, things are probably ok, but if a C_word is a
long long, you're in trouble.

I'm sure if someone cared I could dig up the reference in the ansi C book by
K&R that talks about it.

Thanks.

-pete






reply via email to

[Prev in Thread] Current Thread [Next in Thread]