On Sat, Feb 22, 2003 at 08:44:12PM -0000, Rafael Garcia-Suarez wrote:
> Nicholas Clark wrote in perl.perl6.internals :
> > 
> >> >   r->score = r->use_count + (r->lhs_use_count << 2);
> >> > 
> >> >    r->score += 1 << (loop_depth * 3);
> [...]
> > I wonder how hard it would be to make a --fsummon-nasal-demons flag for gcc
> > that added trap code for all classes of undefined behaviour, and caused
> > code to abort (or something more colourfully "undefined") if anything
> > undefined gets executed. I realise that code would run very slowly, but it
> > would be a very very useful debugging tool.
> 
> What undefined behaviour are you referring to exactly ? the shift
> overrun ? AFAIK it's very predictable (given one int size). Cases of

Will you accept a shortcut written in perl? The shift op uses C signed
integers:

$ perl -MConfig -le 'print foreach ($^O, $Config{byteorder},  1 << 32)'
linux
1234
0

vs

$ perl -MConfig -le 'print foreach ($^O, $Config{byteorder},  1 << 32)'
linux
1234
1

$ perl -MConfig -le 'print foreach ($^O, $Config{byteorder},  1 << 32)'
linux
4321
1

vs

$ perl -MConfig -le 'print foreach ($^O, $Config{byteorder},  1 << 32)'
linux
4321
0

(all 4 are Debian GNU/Linux
 And both architectures that give 0 for a shift of 32, happen to give 1 for
 a shift of 256.
 But I wouldn't count on it for all architectures)

> potential undefined behavior can usually be detected at compile-time. I

In this specific case, maybe. In the general case no.
signed integer arithmetic overflowing is undefined behavior

> imagine that shift overrun detection can be enabled via an ugly macro
> and a cpp symbol.
> 
> (what's a nasal demon ? can't find the nasald(8) manpage)

Demons flying out of your nose. One alleged consequence of undefined
behaviour. Another is your computer turning into a butterfly. I guess a
third is "Microsoft releasing a bug free program"

Nicholas Clark

Reply via email to