I’ve been spending a bunch of time recently working on the LLVM AVR backend and integrating it into the Rust programming language.

In the coming months the Rust compiler should support AVR support out-of-the-box!

LLVM

A few years back I started getting into Rust. Around the same time I started playing around with electronics and microcontrollers. After a bit I realised that the two would be a match made in heaven and so I started to investigate what it would take to get a Rust program running on the Arduino UNO.

I found a stale fork of LLVM on SourceForge and spent a few days updating it to LLVM trunk.

Two years later…..

It turned out that it was quite a cool project to work on and so here I am, still working on it 😅

The backend has since managed to

  • Go from a few dozen supported instructions to the entire instruction set (ignoring one or two instructions)
  • Support a wider range of input programs
  • Gain machine code support from scratch
  • Support parsing textual AVR assembly
  • Have dozens of bugs fixed
  • Get merged into LLVM trunk!

The backend started as a GitHub fork. It now lives inside LLVM’s svn repository.

I now act as the code owner and maintainer of the project in upstream LLVM.

Rust

After a bunch of work on the AVR-LLVM backend, I started a fork of Rust which integrated the two projects on GitHub. I managed to get a working proof of concept, and it attracted a few contributors which was pretty cool.

However, it was quite hard to maintain. Rust’s version of LLVM has a few patches on top of stock LLVM, and it also tended to have extra commits cherry-picked on top. It took a large amount of time to update every time Rust updated their LLVM fork (which was quite often!), and so I more-or-less let it go stale.

Almost a year later the AVR backend became completely merged into upstream LLVM. The effort to support Rust on AVR then ignited for the second time. The moment Rust upgrades from LLVM 3.9 to 4.0, it will be simple to enable the AVR backend and then add some support code to the compiler.

We’ve got rust-lang/rust#37609 raised to track the LLVM 4.0 upgrade effort, and work is almost complete!

Once LLVM 4.0 has had its final tag created, we can finish updating the emscripten library (which Rust depends on) and then update the submodule in the Rust compiler.

The reason this must be done is because Rust has fairly recently gained support for JavaScript as a target. In order to accomplish this, the compiler uses the emscripten-fastcomp fork of LLVM instead of using a standard copy of LLVM.

Pull requests exist for the emscripten upgrade effort

Once Hans from the LLVM team makes the final tag for the 4.0 branch, we can bring in the extra commits and then finally merge those PRs.

This will be the last step needed before 8-bit microcontroller programming in Rust becomes a reality.

Take a look at Jake Goulding’s Rust Arduino library. It’s a really cool starting point for any future AVR projects!