9 ms·
Rust folks, thank you so much for making this far, far better than GCC’s asm syntax for C. Also, thank you for using Intel x86 syntax instead of AT&T. It woul
by amluto 6y ago
Rust folks, thank you so much for making this far, far better than GCC’s asm syntax for C. Also, thank you for using Intel x86 syntax instead of AT&T.
It would be delightful if GCC were to adopt something similar after this stabilizes in Rust.
- pjmlp 6y agoStill fails short of asm code blocks of PC based languages.
- systems 6y agowhat are PC based languages ? what is PC short for in this context
- pjmlp 6y agoMS-DOS/Windows heritage. Modula-2, Pascal, C, C++, Basic based compilers with inline Assembly parsers or intrisics that interact with host language type system instead of manually dealing with strings UNIX style.
- dathinab 6y agoBut in rust asm! does interact with the type system. The asm macro does know about what group of registers (or what specific register) a input/output is stored/loaded from and compilation will fail if the input isn't compatible with the register. Then it's delegating to a assembler for the rest. Sure it could also have a list of all asm commands the given target does support and parameters/registers it can be used with but this means keeping track of all supported targets and all commands for all potential featuresets of all targets and all way they can be called. This is a lot of work. Even more such support can be added later one, too. For now it's more important to support asm on stable. Also with proc-macros you can implement this as a library on top of `asm`! Also with a more "knowledgeable" asm any support for a new platform would need to manually add support for all asm of that language so having a standard stringly interface is a good think anyway.
- jfkebwjsbx 6y agoWhat you describe is how it works in GCC/LLVM and is required in any inline asm system. Otherwise it is not really inline. Saying that fits with the type system is a stretch... it is like any other FFI.
- Rusky 6y agoThe use of strings in Rust's implementation is limited to the assembly template and the operand constraints. Functionally speaking, moving those out of strings gains you absolutely nothing and can make parsing more difficult. The important part, type system integration, is already there. Sizes must match, move and borrow checking still happens, etc. This avoids the typical failure modes of unix-like stringly-typed systems. In fact if you read the announcement and RFC, you'll see that this was a primary goal of the new design!
- simias 6y agoI've followed the discussion regarding how ASM should be integrated in Rust over the past few years, the idea of integrating the ASM syntax more tightly with Rust has been brought up. Basically one could treat assembly as an ad-hoc domain specific language with its own parsing rules that would make it more like a first class citizen instead of a magical character string. I've actually implemented a very simplistic MIPS assembler in a similar way for emulation purposes: https://github.com/simias/rustation/blob/master/src/parallel_io/exe_loader.rs#L138-L152 https://github.com/simias/rustation/blob/master/src/parallel... I think they were absolutely right not to go down that route, because then you have to basically specify this syntax for every possible architecture targeted by Rust. Integrating ASM in the language syntax is fine if you only care about x86, but it's going to be a mess if you want to support MIPS, ARM32, AARCH64, Sparc and whatnot because the ASM dialects for each of these architectures have special bespoke syntax to deal with various quirks of the underlying ISA. MIPS has special syntax to load the top and bottom half of an address as well as dealing with delay slots (.noreorder), ARM has special sugar for PC-relative addressing etc... Importing that baggage into Rust would be a fool's errand IMO. It would also make it harder both to port Rust to new architectures and to transfer existing assembly code into Rust since it would require adjusting the syntax. I'm very happy with Rust's current approach, it's a good middle ground IMO.
- FpUser 6y agoSecond that. Very simple example: function MultiplyBy2Reg(aInput: int64): int64 assembler register; asm SHL RAX, 1; end; function MultiplyBy2(aInput: int64): int64 assembler; asm mov RAX, aInput; SHL RAX, 1; end;
- simias 6y agoNow I need to port that code to MIPS, what do? This is a very nice feature to have if you know your environment is overwhelmingly tied to a certain architecture and you want to make it easy to target this architecture in particular. This might actually be a somewhat reasonable, if short-sighted approach if we were in 2005 and you could make the point that anything that's not x86 is effectively legacy or niche but nowadays you need to at the very least support x86-64, ARM32 and AARCH64. Baking all these flavors of assembly into a language would be very heavy handed and a maintenance nightmare. Beyond that we're not in the 90's anymore, ASM is not routinely used outside of very low level code these days. Even for performance people often opt for intrinsics instead. Compilers got massively better at optimizing code while humans got worse due to ever more complex architectures. Being able to seamlessly blend ASM with normal code isn't that much of a killer feature anymore IMO. It would be a costly gimmick to implement.
- pcwalton 6y agoIs the MSVC dialect of C/C++ on x86-64 not a "PC based language"? Because it doesn't support inline asm at all.
- pjmlp 6y agoBetter not supporting it and just use external Assemblers or intrisics, like it does by shipping MASM in the box, a very good Macro Assembler, at least none of them are glueing strings together. Also that same compiler still does it for x86.
- josephcsible 6y agoIs AT&T syntax really that bad? In particular, it's way nicer for specifying operand sizes. I'd prefer "andl $3, (%eax)" any day over "and DWORD PTR [eax], 3".
- fluffything 6y agoThe problem is not whether its bad or not, but rather, having to learn a completely-different second syntax for one architecture. Most Rust code is quite portable, targeting ARM, x86, MIPS, PPC, WASM, Sparc, s390x, riscv, ... That means, that for many snippets of inline assembly, you might encounter ~8 of them, one for each architecture, all using different syntaxes. Intel syntax is quite similar to that of other popular architectures `op dst, args...`. Adding another second syntax for x86 just doesn't add that much value IMO, and adds quite a bit of cost: now everybody dealing with x86 assembly needs to learn 2 syntaxes... and everybody dealing with portable code now needs to be at least able to read 2 syntaxes for x86... Without talking about the cost of implementing a second syntax in the compiler, etc. If you prefer AT&T, you can always write a proc macro that translates it to Intel, and use that in your projects. If I ever need to deal with such code, I'd just expand the macro to read the actual Intel syntax, modify that, and either fork the project, or submit a patch with a fix using Intel syntax.
- JoshTriplett 6y ago> If you prefer AT&T, you can always write a proc macro that translates it to Intel, and use that in your projects. If you prefer AT&T (or you have a large body of existing AT&T code you don't want to have to translate all at once), use asm!("...", options(att_syntax)) and it'll Just Work.
- jfkebwjsbx 6y agoAT&T syntax is actually the common one in most low-level programming if you count by architectures and most likely also by code size produced, if only because GCC has/had been the de facto compiler for new chip uarchs for more than a decade (helped by the fact that everyone wants their chips to run Linux). Nowadays GCC and LLVM support both styles and archs pick whatever they prefer and nobody cares, really. > either fork the project, or submit a patch with a fix using Intel syntax. That sounds a bit extreme? Reading/writing in both styles is not an issue for anyone that has dealt with x86 professionally.
- JoshTriplett 6y ago> Also, thank you for using Intel x86 syntax instead of AT&T. Thank you for helping to validate that decision; this is the kind of feedback we needed. (And note that you can still choose to use AT&T syntax, it just isn't the default.)
- binarycrusader 6y agoYes, this is great, but I have to disagree on the Intel x86 syntax assertion by the original poster. Perhaps it's because of the era I grew up in and how I first learned machine language and then assembly language, but I've always preferred AT&T syntax. Regardless thanks for supporting the saner syntax even if it's not by default ;)
- exmadscientist 6y agoIf you're ever in doubt on which syntax to implement first or make default, just look at the processor manual (TRM). Match it, and you'll have an ironclad defense :)
- JoshTriplett 6y agoThat was one of our primary justifications. The tradeoff was between that and what's currently widely used with GCC and clang; the latter (along with people porting from what's now called llvm_asm!) motivated us adding the att_syntax option.
- jdright 6y agoYes, thank you for the Intel syntax. It is by far the most frequent syntax you'll find anywhere somewhat recent (1990+) and it is by far the most straightforward to learn by being clean. I did learn both in the 90s, but I've always struggled with AT&T symbols and different offerings which to me was hard to switch to when learning from different material sources.
- monadic2 6y agoFWIW I mostly see AT&T when compiling against C codebases to a) avoid requiring a secondary assembler & wanting to support legacy binutils and b) because inline C is AT&T in practice, at least outside of windows (no clue what the c ecosystem is like there). However, most resources exploring “assembly” do so in a context where it makes sense to use intel syntax & work with a “dedicated” assembler. That said, this is a good decision because C compilers seem to be the major holdouts at this point—binutils has had .intel_syntax for a long time now, it’s just not supported inline.
- simias 6y agoI agree. They took their time but was worth it. I really missed not having had ASM is stable Rust the past few years but since it means not having to deal with something as horrendous as GCC's __asm__ syntax I'm glad they took their time.
- bobly_today 6y agoAs someone who only knows at&t syntax I guess I should view this is an opportunity to learn intel syntax
- koverstreet 6y agoYou can also use intel syntax with inline assembly in gcc, e.g.: asm(".intel_syntax noprefix;" "xor eax, eax;" "xor edx, edx;" "1:;" "mov r8, [rdi];" "mov r9, [rsi];" "sub ecx, 64;" "jl 2f;" "cmp r8, r9;" "jnz 3f;" "lea rdi, [rdi - 8];" "lea rsi, [rsi - 8];" "jmp 1b;" "2:;" "not ecx;" "shr r8, 1;" "shr r9, 1;" "shr r8, cl;" "shr r9, cl;" "cmp r8, r9;" "3:\n" "seta al;" "setb dl;" "sub eax, edx;" ".att_syntax prefix;" : "=&D" (d0), "=&S" (d1), "=&d" (d2), "=&c" (d3), "=&a" (cmp) : "0" (l), "1" (r), "3" (nr_key_bits) : "r8", "r9", "cc", "memory");
- de_watcher 6y agoGCC supports Intel syntax. Why there is always a guy who doesn't like the default and unaware that it's just one of the options?
- colejohnson66 6y agoThey’re upset that it’s the default, not that the option doesn’t exist.