Skip to content

clang -target avr : miscompiling in asm() statement using sts/lds opcode #167244

@wke67

Description

@wke67

// clang -target avr : miscompiling in asm() statement using sts/lds opcode
// sts/lds is a 32 bit opcode using direct addressing sts mem,rx
// in an asm() statement it is interpreted like ld/st Z, rx
// running the linker, this results in error "undefined reference to `Z'"
// this behavior is independent of mcu, e.g. same behavior for atmega328p

// Example
char NVM;

void func(void)
{
char data;
NVM = 0;
asm(
"sts %1,%2 \n"
"lds %0, %1 \n"
: "=r" (data) : "m" (NVM), "r" (45) );
}

/* Compiler call:
clang --target=avr--none-elf -mmcu=avr64dd28 -S -Os func.c
clang version 21.1.3 (https://github.com/llvm/llvm-project 450f52e)
Target: x86_64-unknown-linux-gnu
Thread model: posix

linker used avr-ld
GNU ld (AVR_8_bit_GNU_Toolchain_4.0.0_52) 2.44

// compilation result: func.s

tmp_reg = 0
zero_reg = 1
SREG = 63
SP_H = 62
SP_L = 61
.file "func.c"
.text
.globl func ; -- Begin function func
.p2align 1
.type func,@function
func: ; @func
; %bb.0:
sts NVM, r1 <------ this is correct, the compiler uses direct adressing
ldi r30, lo8(NVM)
ldi r31, hi8(NVM)
ldi r24, 45
ldi r25, 0
;APP
sts Z, r24 <------- this should be "sts NVM,r24"
lds r24, Z <--------this should be "lds r24,NVM"

;NO_APP
ret

.Lfunc_end0:
.size func, .Lfunc_end0-func
; -- End function
; Declaring this symbol tells the CRT that it should
*/

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions