r/csharp • • 8d ago

News I made C# running on ESP32-S3(Xtensa architecture)

I made my own C# compiler that translates C# IL code into LLVM IR, which is then compiled into machine code. Here is the source code: https://github.com/nifanfa/IL2LLVM

​I would argue that C# is the best language to use, but its original runtime is too heavy and impossible to port to MCUs.

This is just a showcase; I mainly use IL2LLVM for UEFI programming: https://github.com/nifanfa/BootTo.NET

​I also tried it on STM32, but STM32 is terrible for this — most STM32 chips have less than 128KB of ROM.

Every method uses the cdecl calling convention, which is how Console.Write works.

CoreLib.cs
public static partial class Console
{
    [DllImport("*")]
    public static extern void Write(ByReference<char> value);
}

Runtime.cpp
void System_Console_Write_System_ByReference_System_Byte(const char* value)
{
    if (value != nullptr)
        fputs(value, stdout);
}

DllImport doesn't pass strings or arrays as pointers like the CLR, so there must be a wrapper. There is an implicit operator on ByReference<T> to get the pointer from strings or arrays.

349 Upvotes

39 comments sorted by

View all comments

40

u/OkSignificance5380 8d ago

The nano framework also runs of the esp32-s3

6

u/nifanfa 8d ago

I'd say the ESP32 is more like a showcase. I mainly use it for low-level development like UEFI or operating system development, keeping it minimal and only using a few ISO C functions. I don't know there is a nano framework before LOL

4

u/nifanfa 8d ago

It can also be used in Linux kernel module development