On Sat, 4 Nov 2006 00:57:07 +0000 (UTC), Rainer Buchty
Post by Rainer BuchtyAgreed, trying to grok compiler-generated code can be a really hard task.
Trying to work out why the compiler did it (without access to the
compiler source code) is fun as well. For some value of 'fun'.
Post by Rainer BuchtyI remember a discussion on the avr-gcc mailing list where the compiler
spit out something which at first glance was suspected to be buggy but then
turned out to be a really clever optimization -- which no human would have
ever used, because it was just to far off thinking.
Optimisers frequently do things like eliminating whole chunks of code
which they 'know' can't be executed. They don't always get it right, it
can be most dinconcerting to look at the assembler (or core dump) and
see half the function missing!
Post by Rainer BuchtyConverting human-generated code, instead, is comparably easy, because
whatever optimization was done was most likely still is comprehensible,
whereas modern optimized compiler-generated code is sometimes beyond
recognition and takes rather hard work to decipher.
Hand-optimised assembler is often worse. At least compilers have to
obey some common contraints (consistent order of parameters on stack or
in registers, often consistent stack frames, etc.), whereas assembler
programmers are free to do anything they want, including using
instructions as data or vice versa. Or running code on the stack.
Changing processors, there was a fairly common piece of PDP-11 code
which went:
ENTRY0: MOV *(PC++), *(PC++)
ENTRY1: CLR *(PC++)
FLAG: DW 0
...
(syntax modified). If called as ENTRY0, the flas was non-zero, if
called as ENTRY1 it was zero. Easy for a human to recognise, impossible
to translate back into a high-level language automatically at all
sensibly (few HLLs have multiple entry points). And it didn't actually
modify the code (code space, yes, but there was no distinction between
code and data spaces).
Chris C