[Mono-dev] Inline assembly in C#

James Bellinger james at illusorystudios.com
Tue May 30 17:29:13 UTC 2017


This would actually be a *very* useful tool, even on Windows.

If you made it so whatever pseudo-library got linked returned false for 
all of these (and NotSupportedException if assembly was attempted to be 
used), if the preprocessor was never run, everything would use the 
fallback. This would also make the fallback get testing.

Being able to test for architecture *in a normal, code-based way* is 
really important. It isn't just processors. What about SSE, AVX, etc.? 
Weird blocks x86 { } ppc { } etc. look pretty but are a million times 
worse. Don't choose a platform for me. That's brittle.

For my part, I'd love to be able to embed vector instructions in C# for 
various code hotspots, but I'd like to keep my assembly entirely 
platform independent otherwise. I would do this by if () around various 
platform tests before getting into the serious part of the loop. The 
problem with the Mono vector types, when they existed, was typing. 
Assembly nicely avoids that.

James


On 5/30/2017 12:16 PM, Jonathan Pryor wrote:
> The original class-based design supported this:
>
> 	if (asm.x64supported) { asm.x64 (“mov $1, rax”);
> 	else if (asm.x86supported) {asm.x86 (“mov $1, eax”);
> 	else if (asm.ppcsupported) {asm.ppc (“li r3, 1”);
> 	else {
> 		// new/unknown architecture; write the code in IL
> 	}
>


On 5/30/2017 12:16 PM, Jonathan Pryor wrote:
> Reply inline:
>
> On May 28, 2017, at 10:42 AM, Miguel de Icaza via Mono-devel-list <mono-devel-list at lists.dot.net> wrote:
> ...
>> This would require some changes to the language, but it would look something like this:
>>
>> public int ReturnOne ()  {
>>     x86 {
>>        mov $1, rax
>>     }
>>     ppc {
>>        li r3, 1
>>     }
>> }
> Much as I’d hate to start syntax bikeshedding, I don’t like this syntax:
>
> 1. It makes it hard to easily grep for uses of inline assembly
> 2. I think it would require too much “architecture hardcoding” into the compiler
> 3. As-is, I don’t see how I could provide an “IL fallback” path
>
> (1) and (2) could be solved by using `asm “architecture”` instead:
>
> 	asm “x86” {mov $1, rax}
>
> but that leaves (3): how do you support conditional code flow between inline-assembly and “normal” code? How could you have a “fallback” codepath?
>
> The original class-based design supported this:
>
> 	if (asm.x64supported) { asm.x64 (“mov $1, rax”);
> 	else if (asm.x86supported) {asm.x86 (“mov $1, eax”);
> 	else if (asm.ppcsupported) {asm.ppc (“li r3, 1”);
> 	else {
> 		// new/unknown architecture; write the code in IL
> 	}
>
> I can think of two ways to support IL fallbacks:
>
> 1. Treat “asm architecture” as an `if`, allowing `else asm:
>
> 	asm “x86” { mov $1, rax }
> 	else asm “ppc” {li r3, 1}
> 	else { /* IL */ }
>
> 2. Instead of `asm architecture`, rely on conditional compilation:
>
> 	#if x86
> 		asm { mov $1, rax }
> 	#elif ppc
> 		asm { li r3, 1 }
> 	#else
> 		/* new/unknown arch; default IL fallback */
> 	#endif
>
> This could get ugly quick, but perhaps such ugliness is a *good* thing?
>
> Additionally, “IL fallback” isn’t *just* for IL fallbacks; it’s for *intermixing* inline assembly with normal code within the same body.
>
> Finally, methods should be marked as `unsafe` in order to use inline assembly.
>
>   - Jon
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.dot.net
> http://lists.dot.net/mailman/listinfo/mono-devel-list



More information about the Mono-devel-list mailing list