Issue 167244
Summary clang -target avr : miscompiling in asm() statement using sts/lds opcode
Labels clang
Assignees
Reporter 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 450f52eec88f728c89a9efd667dbeaf2dad93826)
 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
*/
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to