www.digitalmars.com         C & C++   DMDScript  

D.gnu - strange pointer casting (64 bit)

reply Simon Buerger <krox gmx.net> writes:
hi

 From my point of view, the following piece of code does really 
strange thing:

write("{}", cast(void*)0xFFFF_FFFF_FFFF_FFFF_UL); // prints 
ffffffffffff, OK
write(cast(ulong*)0xFFFF_FFFF_FFFF_FFFF_UL); // prints ffffffffffff, OK
write("{}", cast(ulong*)cast(void*)0xFFFF_FFFF_FFFF_FFFF_UL); // 
prints ffffffff,  not OK

Its not a question of output routine, I've checked it in different 
envirovments and with different values. It simply cuts of the higher 
32 bit part of the address. Could, please, somebody explain me that 
behavior? For me, it looks like a compiler bug, but that seems 
impossible for such an obvious thing...

my system:
AMD64, Ubuntu Linux,
gdc (GCC) 4.1.2 20070214 ( gdc 0.24, using dmd 1.024)
(according to gdc --version)
Jun 29 2008
parent reply downs <default_357-line yahoo.de> writes:
Simon Buerger wrote:
 hi
 
 From my point of view, the following piece of code does really strange
 thing:
 
 write("{}", cast(void*)0xFFFF_FFFF_FFFF_FFFF_UL); // prints
 ffffffffffff, OK
 write(cast(ulong*)0xFFFF_FFFF_FFFF_FFFF_UL); // prints ffffffffffff, OK
 write("{}", cast(ulong*)cast(void*)0xFFFF_FFFF_FFFF_FFFF_UL); // prints
 ffffffff,  not OK
 
 Its not a question of output routine, I've checked it in different
 envirovments and with different values. It simply cuts of the higher 32
 bit part of the address. Could, please, somebody explain me that
 behavior? For me, it looks like a compiler bug, but that seems
 impossible for such an obvious thing...
 
 my system:
 AMD64, Ubuntu Linux,
 gdc (GCC) 4.1.2 20070214 ( gdc 0.24, using dmd 1.024)
 (according to gdc --version)

It's a bug with the integer conversion routines in DMD's frontend. I quote expression.c: IntegerExp::toInteger case Tpointer: case Tdchar: case Tuns32: value = (d_uns32) value; break; The following (SVN) patch fixes it: Index: expression.c =================================================================== --- expression.c (Revision 231) +++ expression.c (Arbeitskopie) -1128,10 +1128,15 case Twchar: case Tuns16: value = (d_uns16) value; break; case Tint32: value = (d_int32) value; break; +#ifndef __x86_64__ case Tpointer: +#endif case Tdchar: case Tuns32: value = (d_uns32) value; break; case Tint64: value = (d_int64) value; break; +#ifdef __x86_64__ + case Tpointer: +#endif case Tuns64: value = (d_uns64) value; break; case Tenum:
Jun 29 2008
parent Simon Buerger <krox gmx.net> writes:
downs wrote:
 It's a bug with the integer conversion routines in DMD's frontend.

Yeah, that will fix it, thanks a lot. I hope this will be in svn soon, cuz I think it could lead to really hard-to-find errors.
Jun 29 2008