4 ms·
I may be the only one but I feel like this (and the old) asm syntax is too complex and not very user friendly. It looks heavily inspired GCC. I'm sorry for the
by livre 6y ago
I may be the only one but I feel like this (and the old) asm syntax is too complex and not very user friendly. It looks heavily inspired GCC. I'm sorry for the upcoming German (you can ignore it and just read the examples), I feel like a smarter compiler could have a friendlier asm syntax, similar to the one used by FreeBASIC[1].
I hope you can see why I prefer that. Even though the FB compiler is not very smart and just pushes and pops all important regs to and from the stack its asm syntax is a joy to write. I don't have to specify clobber registers, in, out and variables and functions are in scope by default. Also the asm code is not a string and doesn't need to be quoted, it's doesn't look like something foreign at all.
I know that something like that is currently not possible in Rust but I'd love to see it some day.
[1] https://www.freebasic-portal.de/befehlsreferenz/inline-assembler-434.html https://www.freebasic-portal.de/befehlsreferenz/inline-assem...
- zozbot234 6y ago> I feel like a smarter compiler could have a friendlier asm syntax, similar to the one used by FreeBASIC[1]. Feel free to propose a better syntax, then. This feature is not yet part of Stable Rust, so syntax improvements are very much possible. The FreeBASIC syntax seems problematic though, since it would require architecture-specific smarts in the compiler and make it hard to port it to different architectures or add support for newer asm features. It might be better to have these additional features as separate tooling or crates, that might even wrap the asm! syntax itself via proc macros.
- livre 6y agoSadly I think Rust's way is the best one in the long term even if it's not very user friendly. There are a lot of problems with the FreeBASIC syntax, my comment was mostly wishful thinking. As other user has said in a reply to my comment, you can't beat the level of control Rust's syntax provides and that's what most people writing assembly would want. You are also right that a proper asm parser would be difficult to maintain due to the amount of architectures Rust has to support.
- fluffything 6y agoNotice that you can quite easily implement a Rust proc macro that takes whatever DSL you want to use for inline assembly, and generates an `asm!` expression from it. So better syntaxes are possible, as long as they can be built on top of the proposed inline-assembly feature. They can, however, just be implemented as normal Rust libraries, and do not need to be part of the compiler.
- dathinab 6y agoYes, I mean we shouldn't forget that asm today is a last resort escape hatch which should be avoided if possible. EDIT: Just to be clear it's still important. But it's mainly used for a view "very high control" use cases.
- rwmj 6y agoThe problem with baking the asm syntax into the language is that it can make it really hard to use new / obscure instructions. And yet that is often the only reason to use inline asm in the first place, because you need some specific CPU feature which the language you're using doesn't expose.
- jcranmer 6y agoYou can track some of the history of the proposals here: https://github.com/rust-lang/rust/issues/29722 https://github.com/rust-lang/rust/issues/29722. A lot of the improvements you've suggested have been suggested in the past, but there's some reasons why they aren't being proposed. In short, sufficiently smart compilers can probably guess at a lot of constraints automatically, but building that level of smarts is very daunting, and for most use cases, the user probably wants/needs the fine-grained control anyways. For example, take the call instruction. Which registers should the compiler mark that as using and/or clobbering? It depends on the ABI of the function being called--and the user might not even be calling a function at all (say, using a call as a gadget to get the current program counter). Saving a register is another common technique in inline assembly, and you don't want the compiler to infer that the register is clobbered--indeed you're saving it to explicitly not clobber it.
- lilyball 6y agoCould we build a sufficiently smart compiler as a procedural macro that wraps asm!(), so it can be developed as part of the ecosystem?
- jfkebwjsbx 6y agoThat’s a very perilous road. Not to mention expensive to maintain, slow to compile, with very little gain.
- lilyball 6y agoAll of that would be true if the compiler had it built-in too though, right?
- fluffything 6y ago> I don't have to specify clobber registers, So how does the compiler know that these are clobbered then ?
- masklinn 6y agoI expect the entire assembly is encoded into the language.
- int_19h 6y agoThat doesn't really help - the compiler doesn't know what could be clobbered by any given (sys)call.
- livre 6y agoNot really, the other reply is right. Most registers are clobbered. FreeBASIC only passes the asm code inside the asm block directly to GAS (using the .intel_syntax noprefix directive). As I said, the compiler is too dumb to figure out what registers are being used so as a safety measure it clobbers almost all of them (see Register Preservation here[1]). [1] https://documentation.help/FreeBASIC/KeyPgAsm.html https://documentation.help/FreeBASIC/KeyPgAsm.html
- cesarb 6y agoSince the parent comment said "just pushes and pops all important regs", it probably assumes that all registers are clobbered.