Odin's New Inline Assembly Templates
22 points by gingerBill
22 points by gingerBill
Honestly, this is great.
In C land, inline assembly is mostly: "copy/paste this snippet in your codegen" and you have to write it as string literals with embedded linebreaks.
Here, it feels like a high level assembly with a carefully designed bridge to Odin's type system, with many annotations to help both the developer and compiler understand what's going on.
While I'm not using Odin that much, it really feels like a well designed language where every feature fit together nicely.
Thank you!
I tried to design these asm templates so they'd integrate properly with the rest of Odin rather than feeling bolted on the way they do in many other languages (looking at you, GCC/Clang string-based asm nonsense). That meant taking into account Odin's type system, multiple return values (polyadic values), explicit control over pinning of parameters, tying inputs and outputs to the same register, scratch parameters, and more.
I also wanted a single syntax that stays consistent across every ISA—not the same mnemonics, obviously, but the same surrounding syntax—taking inspiration from Plan9/Go.
The big goal was an inline assembler that actually checks the code semantically. The compiler understands all the possible forms: which operands are valid, what kinds of operands are required, and all the clobbering information. Given that, why do so few inline assemblers offer good error messages and suggestions beyond simple "did you mean?" typo fixes? So this one goes further: it flags redundant uses of #align_stack, missing #volatile, asm templates marked as diverging (-> !) that never actually diverge in practice, and plenty more.
And because they're templates (i.e. hygienic macros), they remove the need for dedicated intrinsics: you can just build those directly out of the asm templates themselves.
I honestly believe this is the best inline assembly system in any language right now. And it kind of has to lean on multiple return values, since assembly is effectively a polyadic typed algebra, even if most people think of it as "untyped".
Great news ad approach! Odin is one of my favourite "new" language and really brings the joy of programming.
Thanks for putting all that hard work into it @gingerBill.
you have to write it as string literals with embedded linebreaks.
I write them in raw strings, which is a lot nicer than plain strings.