Rahix / avr-device

Register access crate for AVR microcontrollers
Apache License 2.0
176 stars 67 forks source link

Add support for `asm!()` #97

Closed Patryk27 closed 2 years ago

Patryk27 commented 2 years ago

Closes https://github.com/Rahix/avr-device/issues/91.

Abstract

Starting from 1.59.0, rustc has been removed support for the llvm_asm!() macro, making it impossible to use avr-device with newer compilers.

This merge requests adjusts the code, so that it continues to use llvm_asm!() on older compilers (such as the infamous nightly-2021-01-07), but switches to asm!() on the newer ones.

Due Diligence

I've checked the code on nightly-2021-01-07 and a custom-built rustc from around yesterday (b759b2218649016cc40e82bdd6d958e2277ff6d7), and everything seems to be working correctly.

I've compared the generated LLVM IRs for an application I'm developing, and they are virtually identical:

enable():

older rustc:
  tail call addrspace(0) void asm sideeffect "sei", ""() #10, !srcloc !5

newer rustc:
  tail call addrspace(0) void asm sideeffect alignstack "sei", "~{sreg},~{memory}"() #14, !srcloc !6

disable():

older rustc:
  %0 = tail call addrspace(0) i8 asm sideeffect "in $0,0x3F", "=r"() #10, !srcloc !2
  tail call addrspace(0) void asm sideeffect "cli", ""() #10, !srcloc !3

newer rustc:
  %1 = tail call addrspace(0) i8 asm sideeffect alignstack "in ${0}, 0x3F", "=&r,~{sreg},~{memory}"() #14, !srcloc !2
  tail call addrspace(0) void asm sideeffect alignstack "cli", "~{sreg},~{memory}"() #14, !srcloc !3

The compiled binary seems to be working correctly on an Atmega328p, too.

Follow-ups

I've also got a similar set of changes to avr-hal, I'll try pushing the merge request in a few minutes :-)