❔ 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 ????SharpLab
C#/VB/F# compiler playground.
GitHub
GitHub - jbevain/cecil: Cecil is a library to inspect, modify and c...
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.
21 Replies
yeah, something like Cecil is your best bet. the other options are bad ideas/do not work
personally i think Cecil is kind of a crummy library and i would suggest something like https://github.com/Washi1337/AsmResolver instead
GitHub
GitHub - Washi1337/AsmResolver: A library for creating, reading and...
A library for creating, reading and editing PE files and .NET modules. - GitHub - Washi1337/AsmResolver: A library for creating, reading and editing PE files and .NET modules.
there is also the
System.Reflection.Metadata
API if you would prefer to work directly with the lower-level constructs described in the ECMA-335 standard, AsmResolver is somewhat higher levelinteresting
Right now I'm just looking for a "fastest up and running" that I can then tweak with to try to understand why it works
i'll take a look at it and don't expect any more of your time but if you've got any micro examples for outputting something I can execute with
dotnet run
i'll gladly take themhere is something simple using AsmResolver
Works first try! Thank you!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.do you mind if I ask another question on this? I actually got some code from my language to compile to valid IL, but im trying to change the TFM to 5+ and I keep getting
and
if you are just running the .exe it will not work
when you run an .exe that is a .NET assembly, it always runs in .NET Framework
I've tried these methods and tried screwing around in the source code to see what it actually does, but im working in a notebook rn so i can't switch to VS and turn off Just My Code yet
https://docs.washi.dev/asmresolver/guides/dotnet/basics.html#creating-a-new-net-module
the IL is almost certainly fine, you just need to run it in .NET Core instead of .NET Framework
the easiest way is probably to just do
dotnet MyOutput.dll
it will nag you for a runtimeconfig.json
, so add MyOutput.runtimeconfig.json that looks like this you are coming in as the GOAT
unless you actually have .NET 5 you will need to configure roll-forward. so, add
"rollForward": "LatestMajor",
after "tfm": "net5.0",
I've published and run self-contained apps but I don't think i've actually run a framework dependent app
have you encountered this before?
oh damn Console has its own assembly now
or I would have assumed this would be part of corlib because it's obv not nuget? this is kinda just stream of thoughts for me so sorry not expecting u to hand-hold, i just have a lot to learn lol 😅
Oh it's Internal.Console in CoreLib? hmm
there are dozens of assemblies that come with the Microsoft.NETCore.App framework
there is an Internal.Console but it's used for print debugging during development (since Console doesn't exist in CoreLib), that's not the implementation of Console
I see comparing on sharplib that the assembly referenced is
call void [System.Console]System.Console::Write(int32)
I also see on the code you provided for netfx that the type is imported with the default importer
i'm guessing i have to qualify it? this is probably where I should read the docs morethere was an effort to make mscorlib.dll smaller by splitting unrelated things into different assemblies, which is why there is System.Console.dll, System.Collections.dll, etc.
yes, you would have to import System.Console
I fumbled my way through 🥳
thank you!!
I do kind of have a curiosity question if ur still there tho maybe it doesn't belong in this channel?
is the distinction between module and assembly an artifact of net framework days, or are there assemblies out there that have more than one module?
because i think from just the average C# developer perspective, you're expected to know about assemblies and namespaces but not screw with modules much
interesting https://stackoverflow.com/questions/9271805/net-module-vs-assembly
i think this is the answer right here
yeah, that answer is alright
in .NET Core there is no support for multi-module assemblies
so, i would not try to figure out how to make them work, because they won't
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.