EmbDev.net

Forum: ARM programming with GCC/GNU tools Lost the return value of assembly code after optimization


von Tim C. (hardwing)


Rate this post
useful
not useful
Hello,

I made a program mixed with C and asm by GCC.  It works fine if no 
optimization but run away after optimization with switch "-Os".

After traced code, I found asm code didn't return r0 value back to C 
routine....

**********My C code**********

    :
  volatile struct iphdr *ip_hdr;
    :
    :
    :
  ip_hdr->check = ip_fast_csum((unsigned char *)ip_hdr,ip_hdr->ihl);
    :
    :

**********My ASM code**********

    .global  ip_fast_csum
    .text
ip_fast_csum:
    ldr    r3, [r0], #4
    ldr    r4, [r0], #4
    sub    r1, r1, #5
    adds  r3, r3, r4
    ldr    r4, [r0], #4
    adcs  r3, r3, r4
    ldr    r4, [r0], #4
    adcs  r3, r3, r4
_l:    ldr    r4, [r0], #4
    adcs  r3, r3, r4
    tst    r1, #15
    subne  r1, r1, #1
    bne    _l
    adc    r3, r3, #0
    adds  r3, r3, r3, lsl #16
    addcs  r3, r3, #0x10000
    mvn    r3, r3
    mov    r3, r3, lsr #16
    mov    r0,  r3
    mov    pc, lr

I found the result of ip_hdr->check always zero although the content of 
r0 is correct at the end of assembly?

How can I control the optimization to keep r0 return correctly to 
ip_hdr->check?

BRs.

Tim

von (prx) A. K. (prx)


Rate this post
useful
not useful
Either
-fdo-what-i-mean-not-what-i-say
or
-favoid-all-bugs

For more, you should provide a testcase which can be used and tested by 
readers.

And btw: If speed is premium, drop the count and compare R0 to the final 
address (TEQ).

von (prx) A. K. (prx)


Rate this post
useful
not useful
You violated the procedure calling standard. R4 must be preserved across 
function calls. See 
http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042a/IHI0042A_aapcs.pdf.

von Tim C. (hardwing)


Rate this post
useful
not useful
Ah....

You are right.  It is my mistake.  It should be R2 instead of R4.  It is 
running well now.  The problem actually didn't come from 
optimization.... just because I was lucky at "-O0".

You are helpful and thanks.

BRs.

Tim.

von Clifford S. (clifford)


Rate this post
useful
not useful
Tim Choi wrote:
> How can I control the optimization to keep r0 return correctly to
> ip_hdr->check?

That was wrong-thinking there. If your code breaks when it is optimised, 
it is most likely that your code was flawed in any case, but that 
without optimisation the error was benign.

Th optimiser may reasonably expect your code to comply with the calling 
convention and will then use available resources with that assumption.

von Jim K. (ancaritha)


Rate this post
useful
not useful
A. K. wrote:
> Either
> -fdo-what-i-mean-not-what-i-say
> or
> -favoid-all-bugs
>


Best compiler flags ever.  I use them in just about every project I work 
on :P

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.