[Media] A Rust program compiled to only move instructions

905 points FractalFir 26 comments
[Media] A Rust program compiled to only move instructions

This screenshot is from a Rust program compiled to only the move x86 instruction.

The bulk of the work is done by the M/o/Vfuscator2 by xoreaxeaxeax, a C compiler which only uses the mov instruction.

All I really did was use my Rust to C compiler to compile a simple iterator benchmark to C, and then passed that to movcc. So, this is almost entirely simply a showcase of what compiling Rust to C can do. Still, it is cool to see Rust code compiled to a single instruction.

 81b8342:	8b 14 85 c0 d6 37 08 	mov    0x837d6c0(,%eax,4),%edx
 81b8349:	8b 14 8a             	mov    (%edx,%ecx,4),%edx
 81b834c:	8b 14 95 c0 d6 37 08 	mov    0x837d6c0(,%edx,4),%edx
 81b8353:	8b 0d 90 27 51 08    	mov    0x8512790,%ecx
 81b8359:	8b 14 8a             	mov    (%edx,%ecx,4),%edx
 81b835c:	66 89 15 88 27 51 08 	mov    %dx,0x8512788
 81b8363:	89 15 8e 27 51 08    	mov    %edx,0x851278e
 81b8369:	66 a1 82 27 51 08    	mov    0x8512782,%ax
 81b836f:	66 8b 0d 86 27 51 08 	mov    0x8512786,%cx

Why have I done this?

movcc is based on the lcc compiler, and only supports ANSI C(with some caveats). So, supporting it(even partially) would mean that my Rust to C compiler produces valid ANSI C. That is a pretty good milestone, since it means adding support for even more obscure C compilers should be far easier. I am also a huge fan of Chris's work, so working towards my own silly goal of "compiling Rust to mov's" was a great source of motivation.

Other things I did in the past few months

I have also been making a tiny bit of progress in some other areas(refactoring the project), and I even took a stab at implementing some MIR optimizations in the upstream compiler. None of them ended up being merged(for some, better solutions got implemented), but I still learned a lot along the way.

I also merged a few PRs with tiny performance improvements to the Rust compiler.

I am also proud to announce that I'll be giving a talk at RustWeek about my work compiling Rust to C!

If you have any questions regarding this project, feel free to ask!