
Writing a JIT Compiler in C# - lewurm
https://www.mono-project.com/news/2018/09/11/csharp-jit/
======
dagoat
For those interested in writing their own compiler in C#. There was a decent
article a while back written by Microsoft employees with example code
provided. Looks like MS have archived the post, but you can still browse the
code (almost unadulterated) here:

[https://github.com/johandanforth/good-for-nothing-
compiler](https://github.com/johandanforth/good-for-nothing-compiler) (not my
github)

And read an expanded/in depth version of the original article (with a link to
download the original source) here:

[https://www.cs.helsinki.fi/u/vihavain/k14/code_generation/pr...](https://www.cs.helsinki.fi/u/vihavain/k14/code_generation/project/msdn.microsoft.com%20-%20cc136756.pdf)

------
chrisaycock
The appendix ( _Converting Stack-Based OpCodes into Register Operations_ ) is
the most interesting part to me. Targeting a stack machine is much easier when
writing a compiler for the first time, but real-world machines are generally
register based.

I couldn't find the authors' logic in a cursory look at the source code. Does
anyone know where it actually is?

~~~
lewurm
_Targeting a stack machine is much easier when writing a compiler for the
first time, but real-world machines are generally register based._

In general this is true, here however, we are targeting LLMV IR which is
register based with _infinite_ registers; so it's kinda easy again :-) The
hard part, register allocation, is actually done by LLVM.

This class implements the conversion:
[https://github.com/lambdageek/mono/blob/mjit/mcs/class/Mono....](https://github.com/lambdageek/mono/blob/mjit/mcs/class/Mono.Compiler/Mono.Compiler.BigStep/CILSymbolicExecutor.cs)

