jsnyder / avr32-toolchain

Makefile & supporting patches/scripts to build an AVR32 toolchain.
29 stars 30 forks source link

Incorrect strcmp generated for UC3C #23

Closed denravonska closed 6 years ago

denravonska commented 11 years ago

I have been debugging an issue where I keep getting unaligned memory access exceptions when attempting do do strcmp on a buffer and a constant. After checking the generated assembler I saw this:

8002d3cc : 8002d3cc: 19 0a ld.w r10,r12++ 8002d3ce: 17 09 ld.w r9,r11++ ...

This assumes that the variables are always word aligned. The Atmel assembler file seems to provide two different versions depending on if the target supports unaligned words or not. Which one it picks depends on what the __AVR32_HAS_UNALIGNED_WORD__ is set to, and it's only enabled on AP* MCUs.

This is how the strcmp version from Atmel Studio looks like:

800079f8 : 800079f8: f9 eb 10 0a or r10,r12,r11 800079fc: e2 1a 00 03 andl r10,0x3,COH 80007a00: c3 41 brne 80007a68 <strcmp+0x70> ...

This one correctly checks if the two variables are word aligned. If they are the fast comparison is made, otherwise it falls back to a byte-by-byte comparison.

For some reason __AVR32_HAS_UNALIGNED_WORD__ is enabled when strcmp.S is built using this toolchain. Explicitly compiling newlib using the -march=ucr3fp fixes the issue for me but I don't think that's how Atmel compiles their version.

Any ideas?

Edit: strcmp.S can be found in newlib-1.16.0/newlib/libc/machine/avr32/strcmp.S