View Issue Details

IDProjectCategoryView StatusLast Update
0038142FPCCompilerpublic2020-11-28 08:00
ReporterChristo Crause Assigned ToFlorian  
Status closedResolutionfixed 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0038142: AVR Read-modify-write bug for avrtiny subarch
DescriptionWhen reading a variable from RAM, modifying it and writing the resulting value back to RAM, the address stored in the Z pointer could have been modified by predecrement/postincrement versions of LD. Below find an example of code that loads a value from RAM, modifies it then stores the modified value back to RAM. Note in the generated assembler that ld r21, Z+ is used to load the high byte of w, after this Z points to the next byte address. Z still points to the next byte address when the store operation is started, which then stores (for this example) the value of w with an offset of 1 relative to the linker assigned address of w.

This issue has been exposed by 0037929 because the avrtiny subarch doesn't have a load with displacement instruction. This means that loading variables larger than a byte in size from RAM via the Z-pointer will currently always modify the Z-pointer.

Looking at tcg.a_op_const_ref shows that the high level code generator only loads the address of a reference once, before the read-modify-write sequence.
Steps To Reproduceprogram test;

  w: word;

procedure dummy;
  OCR0A := w;

Additional Information# [8] inc(w);
    ldi r19,lo8(U_sPsTEST_ss_W)
    ldi r18,hi8(U_sPsTEST_ss_W)
    mov r30,r19
    mov r31,r18
    ld r21,Z+
    ld r22,Z
    ldi r20,1
    add r21,r20
    adc r22,r17
    st Z+,r21
    st Z,r22
TagsNo tags attached.
Fixed in Revision47612
Attached Files


Christo Crause

2020-11-28 08:00

reporter   ~0127224

Great, thanks!

Issue History

Date Modified Username Field Change
2020-11-27 20:46 Christo Crause New Issue
2020-11-27 22:16 Florian Assigned To => Florian
2020-11-27 22:16 Florian Status new => resolved
2020-11-27 22:16 Florian Resolution open => fixed
2020-11-27 22:16 Florian Fixed in Version => 3.3.1
2020-11-27 22:16 Florian Fixed in Revision => 47612
2020-11-27 22:16 Florian FPCTarget => -
2020-11-28 08:00 Christo Crause Status resolved => closed
2020-11-28 08:00 Christo Crause Note Added: 0127224