Roslyn: New Backend that Emit C Code

Created on 25 Aug 2018  路  17Comments  路  Source: dotnet/roslyn

It is good to have a backend that emit C code. C is portable to all machine.

Area-Compilers

Most helpful comment

@zero0000zero you have posted several issues all up and down https://github.com/dotnet suggesting you want to port .NET to run on some new platform but have been very coy about which platform that is.

  • this issue suggests you want to run code on the PS4. Both monogame and unity support running C# code on that platform and @benaadams already pointed you at the documentation for mono so you should have everything you need if that is your goal.

  • in this comment you suggest that your actual aim is to design a proprietary CPU + instruction set and port .net core to it. If this is the case, having roslyn emit C code is the least of your worries. I would estimate this would take a team of 20 engineers with the requisite skills 5 years to accomplish.

Whatever your goal I suggest you describe, in detail, what you are trying to do so someone can offer an informed suggestion of how to accomplish it. Regarding this actual issue, the closest technology that exists today is IL2CPP which can take the output of the roslyn compiler and convert it to C++.

All 17 comments

You should look at https://github.com/dotnet/corert . The platform could be portable, not the language.

Also, of note, there is nothing about a decent high-level language that isn't portable. In fact, that's the entire point of the language, as pioneered by Infocom's Z-machine way back in '79. You don't change the output of the compiler and then recompile every app in existence just to run on a new platform, but you port the runtime to that platform and every app that targets that runtime will work straight out of the box.

Porting DotNet runtime to new platform sucks. It is complex, require too much manual work to port.

@zero0000zero you have posted several issues all up and down https://github.com/dotnet suggesting you want to port .NET to run on some new platform but have been very coy about which platform that is.

  • this issue suggests you want to run code on the PS4. Both monogame and unity support running C# code on that platform and @benaadams already pointed you at the documentation for mono so you should have everything you need if that is your goal.

  • in this comment you suggest that your actual aim is to design a proprietary CPU + instruction set and port .net core to it. If this is the case, having roslyn emit C code is the least of your worries. I would estimate this would take a team of 20 engineers with the requisite skills 5 years to accomplish.

Whatever your goal I suggest you describe, in detail, what you are trying to do so someone can offer an informed suggestion of how to accomplish it. Regarding this actual issue, the closest technology that exists today is IL2CPP which can take the output of the roslyn compiler and convert it to C++.

I want to run .NET stack on new platform, but .NET stack uses IL code in PE format.
To run .NET stack on the platform, .NET have to be in a portable minimal assembly format. It have to do file operation through the assembly.

@zero0000zero

I want to run .NET stack on new platform

please describe what this platform is and what format applications need to be in to run on it

It have to do file operation through the assembly.

I do not know that this means

The platform has compute, has memory, has IO. It can run a kind of new assembly code. This kind of assembly code has a format that is minimal and portable.

Assembly Format
multiple instruction

Instruction Format
[label:] operation source1 source2 destination

label is location of the instruction.
operation is the do of the instruction.
source1, source2, destination are variable.

variable
is one of the type in this list:
Bool
Int
String
Array

operation is one of the do in this list:
new
and
or
not
count
add
subtract
multiply
divide
equal
bigger
smaller
append
subscript
callinstructions
return
goto
in
out

In this assembly format, .NET do file operation with the in and out operations.

To run .NET stack on the platform, .NET have to be in a portable minimal assembly format

.NET already uses a "portable minimal assembly format". C#, today, compiles down to CIL (the Common Intermediate Language). There then exists a backend compiler (the JIT) which will process this intermediate assembly language and convert it to the target machine language (x86, x64, Arm, Arm64, etc).

C/C++ (for Clang) achieves the same thing with their own "portable minimal assembly format" called LLVM.

These intermediate formats mean that you don't have to port the higher level compiler (Clang or Roslyn) and instead only have to port the backend to a new architecture (this would be CoreCLR or LLVM).

If you want to C#/.NET support some new architecture/platform in any capacity, you will:

  1. Need to add a backend to LLVM: https://llvm.org/docs/WritingAnLLVMBackend.html. This will allow you to convert C/C++ code to your target platform,
  2. Need to add a backend to CoreCLR: https://github.com/dotnet/coreclr/blob/master/Documentation/botr/porting-ryujit.md. This will allow you to convert CIL code (which all .NET languages use) to your target platform

To be blunt, if you want to support a new target platform (for either .NET or C/C++) you will need to either:

  • Do it yourself, because it is a very niche architecture that only you are interested in
  • Find a group of people that are also interested in the architecture, and convince them to help you
  • Gather enough data which tells the .NET Foundation or the LLVM Working Group that devoting people to add a new backend targeting this platform is worthwhile

C# compiles to CIL, CIL is not minimal. It is too much work to run CIL directly or convert it to machine language. And .NET stack is in Portable Executable format. It is too much work to support the PE format.

The platform that I wrote is NET. The assembly format that I wrote is NET format.
NET is the best platform to build on. NET is minimal and complete for any computing work.

CIL is not minimal

In what ways is it not minimal. It is, in many ways (some good, some bad), less complex than LLVM (which is what the majority of cross-platform languages target today).

And .NET stack is in Portable Executable format. It is too much work to support the PE format.

Many things use the PE format, not just .NET and Windows. For example, it is also the format used by UEFI (which is the code that allows you to load the Operating System on most modern computers -- it replaced the BIOS a few years ago).

If the firmware that boots your computer can support it, I'm pretty sure other software can do so fairly easily as well 馃槃

  • You also don't need to directly support it yourself, you just need to write the code that converts the CIL code to \

It is too much work

Targeting a new platform/architecture is a lot of work, and there is no way around that.

Making Roslyn have a new backend (that generates code for your new platform/architecture) will also be significantly more work than adding a new backend for LLVM and CoreCLR.

@zero0000zero, it might help if you could indicate what platform/architecture you are trying to target.

Also, it might help if you could indicate what existing C/C++ compiler exists that will compile to that platform/architecture.

How does garbage collection work in C?

Closing as this is not a realistic request for the C# compiler. The C# language is tied pretty heavily to the CLR and the associated runtime. Unless the runtime had a plan for a C emit strategy there really not much for the compiler to do here.

Was this page helpful?
0 / 5 - 0 ratings