www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unexpected behavior of dmd -H switch

reply Sergey Gromov <snake.scaly gmail.com> writes:
I'm getting strange results when using -H switch with DMD 2.018.  
Consider an example:

---- begin test.d
uint foo(uint x) { return x & 0xE000_0000; }
---- end test.d

Nothing complex.  Now, compile it using "dmd -c -H test.d".  The 
resulting .di is:

---- begin test.di
// D import file generated from 'test.d'
uint foo(uint x)
{
return x & -536870912u;
}
---- end test.di

Things start getting funny.  A negative unsigned literal?  OK I'm going 
to test if this compiles.  The test file is:

---- begin test2.d
import test;
import std.stdio;
void main() { writeln(foo(0xb000_0000)); }
---- end test2.d

Compile it using "dmd test2.d" and get "Error 42: Symbol Undefined 
_D4test3fooFkZk".  But the .di contains the full function body.  Isn't 
it for inlining?  Well, the "dmd test2.d test.o" compiles fine and 
executable gives the correct answer.  So the compiler is OK with a 
negative unsigned literal...

I expect that:
1. the constant should be 3758096384u
2. there should be no need in test.obj, or there should be no body for 
foo()

Could anybody clarify this?

-- 
SnakE
Aug 23 2008
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Sergey Gromov:
 I expect that:
 1. the constant should be 3758096384u
 2. there should be no need in test.obj, or there should be no body for 
 foo()
I don't know regarding 1 (but I don't like it) but regarding 2 D keeps the short functions, shortening the long ones only. Bye, bearophile
Aug 23 2008
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Sergey Gromov" <snake.scaly gmail.com> wrote in message 
news:MPG.231ad584323c5e919896ac news.digitalmars.com...

 1. the constant should be 3758096384u
I can at least answer number 1. Syntactically, numeric literals do not include the sign. When you write "-5" it's actually parsed as a negation performed on a positive integer. The semantic pass performs constant folding and turns it into an actual negative integer. So yes, -5u is legal from a syntactic point of view. I wasn't entirely aware that negation could be performed on unsigned integers, though.
Aug 23 2008