This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

unaligned access am3703

Hello, sorry for my pure english.

I have some little problem when try write to unaligned variable.

Source code, in compiler option -gcc is enabled:

struct __attribute__ ((__packed__)) S1
{
    char     ID;
    short     X;
    long     Y;
};

S1 mystruct;

int main(void)
{
    mystruct.ID = 0x77;
    mystruct.X = 0x4477;
    mystruct.Y = 0x11223344;
    
    return 0;
}

I debug it directly in CCSv5.3, so, on "mystruct.X = 0x4477;" it fails, with data abort exception (address 0x14010, it's ROM code dead loop)

Assembler, that i see in debug:

          main:
40200c2c:   E59F0024 LDR             R0, $C$CON1
40200c30:   E3A0C077 MOV             R12, #119
40200c34:   E5C0C000 STRB            R12, [R0]
16            mystruct.X = 0x4477;
40200c38:   E59F001C LDR             R0, $C$CON2
40200c3c:   E304C477 MOVW            R12, #17527
40200c40:   E1C0C0B0 STRH            R12, [R0]   <= after this instruction - exception
17            mystruct.Y = 0x11223344;
40200c44:   E59F0014 LDR             R0, $C$CON3
40200c48:   E59FC014 LDR             R12, $C$CON4
40200c4c:   E580C000 STR             R12, [R0]
19            return 0;
40200c50:   E3A00000 MOV             R0, #0

"A" bit in cp15 is clear, and this code fail with any "A"-bit value...Whats wrong, what i need to do for unaligned correct access?

If the processor doesn't support this access, how to "talk" compiler - generate code with always unaligned access?

CCS arm compiler ignored option "--no_unaligned_access"...

  • Alexander,

    Looks like compiler has not at all generated the aligned access code. But may because the GCC attributes may not be supported by the toolchain. Can you try to refer to TMS470 compiler user guide on how to give compiler attributes to the compiler?

  • Thanks for your answer.

    Compiler generate code, understanding that proccessor can support unaligned access, isn't it? Can you explain, what mean - "not at all generated the aligned access code"? So, you think - problem with compiler, not proccessor?

    I am use only CCSv5.3. What GCC attributes may be not supported by the toolchain - "packed"? It's work, size of such struct is 7, that is true. But this attribute work, only if I set "Enable support for GCC extensions (--gcc)" in project properties in CCS, else compiler return some errors. I read about gcc extensions support at TI_wiki. I don't see some mistakes or errors in this part.

    And the last you suggestion I don't understand - I see, that version compiler TI-5.0.1 and use "ARM optimizing C\C++ Compiler v5.0 User's Guide" (spnu151h), read it week ago. There is "--unaligned-access" option for enable\disable generate different access code, but guide says, that for Cortex proccessors it is always "ON".

  • Alexander,

    ARM processor doesn't support unaligned access. The compiler still supports unaligned access by generating multiple load/store instructions instead of a single load/store instruction. 

    I have pasted the code that is generated by gcc with -O2 set. Compare it with the code that you got. 

    @ args = 0, pretend = 0, frame = 0
    @ frame_needed = 0, uses_anonymous_args = 0
    @ link register save eliminated.
    ldr r3, .L3
    mov r2, #17
    mov r0, #119
    mov ip, #68
    strb r2, [r3, #6]
    mov r1, #34
    mov r2, #51
    strb r0, [r3, #1]
    strb r0, [r3, #0]
    strb ip, [r3, #3]
    strb r2, [r3, #4]
    strb r1, [r3, #5]
    strb ip, [r3, #2]
    mov r0, #0
    bx lr

  • Hm...guides&TRM says that CortexA8 support unaligned access, and i think - this is reason why CCS compiler don't try to generate multiple load\store instructions...

    maybe i'm wrong...

    Thanks, you code look workable, many strb, it is good. But my code with -O2 same like without optimization, compiler use strb, strh..instead many strb :(

             main:
    40200c2c:   E59FC01C LDR             R12, $C$CON1
    40200c30:   E3A00077 MOV             R0, #119
    40200c34:   E5CC0000 STRB            R0, [R12]
    32            mystruct.X = 0x4477;
    40200c38:   E3040477 MOVW            R0, #17527
    40200c3c:   E1CC00B1 STRH            R0, [R12, #0x1]
    33            mystruct.Y = 0x11223344;
    40200c40:   E59F000C LDR             R0, $C$CON2
    40200c44:   E58C0003 STR             R0, [R12, #3]
    35            return 0;
    40200c48:   E3A00000 MOV             R0, #0
    40200c4c:   E12FFF1E BX              R14

    TI "ARM compiler guide" says about  "--unaligned_access" option - "This option is on by default for all Cortex devices", so it's look like no way to generate code from CCScompiler with strb instructions use only.

  • :)) i don't know what happened, but CCS compiler begin generate many "strb"-instructions for unaligned variables, and compiler don't ignored option "--unaligned_access=off" now... :)) it's work, but situation is very strange...

  • Alexandir,

    This is strange. So, does this mean that the issue is resolved? If so, mark this is post as answered and close.

  • Renjith Thomas,

    yes, issue resolved, thanks for your help.