C#C
C#3y ago
🔹

❔ IL CodeGen for toy compiler [advice]

I am trying to learn more about compilers. I'd like to target some type of IR language for a toy compiler, because I don't really want to target a high-level language and I don't want to mess with LLVM stuff yet. WASM seems like a good target and I'll probably go with it if this IL stuff does not work out, but being able to leverage a bunch of BCL code from the start even for a toy language sounds really cool, so I'd like to start out targeting a small, small subset of IL.

Problem is, I'm having trouble producing an assembly, like, at all:
  1. I keep reading System.Reflection.Emit can write a dynamic assembly to a PE, but all this seems like it's .NET Framework stuff. I've also heard emit can be slow but this is a toy project so whatever
  2. When I try to override Compilation from Microsoft.CodeAnalysis, there are internal abstract members which tells me I'm not supposed to touch it (I'm sure implementing MyLangCompilation is wayyy more work than I realize, but this seems like the nicest option)
  3. (https://github.com/kkokosa/Mobius.ILasm)[Mobius.ILasm] is apparently what https://sharplab.io uses to compile IL, and while this is a toy project and I'm not stuck up, I was hoping to plug into something from
    dotnet
    organization. Seems like this is the best bet so far
  4. I know the F# compiler predates Roslyn and can produce IL on it own, so I tried peeking into it to see if there are any interfaces (nuget packages, dependencies, idk, etc.,) that it plugs into when emitting IL.. you know, for things like release signing, writing to streams, byte alignment,, just "staying in spec" type stuff, and I came away so confused seeing why the Rosyln team didn't bother trying to fit in F#.
  5. Maybe Mono.Cecil ????
C#/VB/F# compiler playground.
GitHub
Cecil is a library to inspect, modify and create .NET programs and libraries. - GitHub - jbevain/cecil: Cecil is a library to inspect, modify and create .NET programs and libraries.
Was this page helpful?