5

I would have expected the 8-bit AVR platform to not require any alignment. However, I've found the following comment in an LLVM commit:

The previous data layout caused issues when dealing with atomics.

For example, it is illegal to load a 16-bit value with less than 16-bits of alignment.

This changes the data layout so that all types are aligned by at least their own width.

Unfortunately, the original author of this commit isn't sure if that is right either:

Most of the alignment stuff has been untouched since I originally imported the old SVN repository from SourceForge. I haven't dealt with it much and so my knowledge is pretty poor.

Safest to assume that if something looks intentional, it probably isn't ;P

What exactly is the alignment story on (8-bit) AVR?

Cactus
  • 27,075
  • 9
  • 69
  • 149
  • I don't know LLVM's AVR ABI, but gcc does some padding of function arguments both in registers and on the stack, see https://gcc.gnu.org/wiki/avr-gcc#line-124 – JimmyB May 29 '17 at 09:16
  • @JimmyB: but doesn't that link talk only about *register number* alignment, the same way tofro explained in his answer? – Cactus May 29 '17 at 12:46
  • 1
    @JimmyB: specifically, it says "If the current argument is passed in memory, stop the procedure: All subsequent arguments will also be passed in memory." but then does it ever say if arguments passed in memory are aligned or not? – Cactus May 29 '17 at 12:46
  • Note the AVR registers are also memory-mapped into the register file. So *register number* alignment can also show up as *memory alignment* in case you implement register access through memory in the lower 32bytes of RAM. For a compiler, this might be a limitation. – tofro Jun 01 '17 at 10:34

1 Answers1

6

This comment doesn't make any sense to me. AVR does not natively know 16-bit-types, and specifically not atomic access to 16-bit-types, regardless of any alignment.

On a classic AVR CPU, alignment of 16 bit data is not required as 16-bit memory access always evaluates to two 8-bit fetches to two registers.

There is a kind of "alignment restriction", though, when using the movw instruction available on some AVRs that moves data from one register pair to another - Here the register number of the lower registers must be even. This has nothing to do with memory alignment, though, but may have implications when building a compiler and maybe also when trying to access register content through the register file as memory (lowest 32 RAM addresses), depending on how exactly the compiler is implemented. The compiler might limit itself by keepint the option open to be able to access 16-bit values in registers through a memory access into the register file which would then indeed need word-alignment.

A second kind of "alignment restriction" applies when trying to write to program memory (Flash) - The manual explicitly states that on "SPM instructions, the least significant bit of the address should be cleared". This is understandable, as the AVR's flash is word-addressed according to the minimal instruction size. You can, however, read program memory byte-addressed with the LPM instruction. As direct-access SPM is not supported on all AVRs anyhow, I don't know if this is relevant at all, though. How SPM and "atomic types" would relate to each other escapes meas well - When write accessing program memory, the whole access must be made atomic anyhow, by disabling interrupts. And gcc handles program memory access in libraries anyhow.

Outside this specific cases (register file, program memory store) the AVR has absolutely no problems whatsoever to access word values on odd addresses.

tofro
  • 5,640
  • 14
  • 31
  • 1
    OK, so this looks like what I expected; and I managed to compile a non-trivial codebase after turning off all alignment. However, I'm reluctant to accept this answer until there's some kind of authoritative reference. I am starting to think that the reason I can't find any black&white reference to alignment is simply because there is NO alignment; but it'd be nice to see an explicit statement of this. – Cactus Jun 01 '17 at 03:44
  • 1
    You'll probably find as many references to AVR and memory alignment requirements as you might find for a Z80 or 6502.... Note, however, my extension to the answer after I have thought a bit more about this issue. – tofro Jun 01 '17 at 09:56