0

description of my problem is to create:

  1. C# parser
  2. compose AST tree from parsed input .cs file
  3. create CIL/bytecode representation of program
  4. create VM
  5. execute code stored as "bytecode" - make the parsed program work

So basically I need to write my own language with all the "stuff" around.

I'll write my sample program which I want to run. I know how to create C# parser to handle this program and compose AST tree.

I've also found CIL reference http://en.csharp-online.net/CIL_Instruction_Set so I can save my AST as CIL code (hopefully there will be no problem). But my question is, how can I test this CIL before I have my virtual machine? Can I use C# CLR? And how?

How can I create virtual machine executing CIL instructions in C#? Can I "force" C# program to run CIL instructions?

Is my idea, the steps, correct? Should I avoid writing AST to bytecode using CIL instructions?

My ideas were inspired by .NET http://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/CLR_diag.svg/400px-CLR_diag.svg.png

Thank you for any replies which will help me to find the correct way of solving this problem.

P.S. I need to implement everything by myself

EDIT

I've found this: http://msdn.microsoft.com/en-us/magazine/cc136756.aspx

http://www.codeproject.com/Articles/3778/Introduction-to-IL-Assembly-Language

http://www.codeguru.com/csharp/.net/net_general/il/article.php/c4635#Array1

It might solve my problem. I'll give it a try.

Seki
  • 11,135
  • 7
  • 46
  • 70
luccio
  • 485
  • 1
  • 6
  • 24
  • 2
    Once you've created CIL why do you then want to write your own VM? Isn't that just reinventing .net? If you do want to do this look at the Mono source, and this answer: http://stackoverflow.com/questions/3328901/how-to-insert-cil-code-to-c-sharp – James Gaunt Feb 17 '12 at 15:36
  • Have you looked at Expression Trees? http://msdn.microsoft.com/en-us/library/bb397951.aspx Also, why on earth do you want to write a C# parser? There are lots available. – 3Dave Feb 17 '12 at 16:32
  • Hi, it's a homework...so I need to implement everything by myself :-/ – luccio Feb 17 '12 at 17:54

2 Answers2

2

I would do the tasks from your list out of order: First I'd write the a simple VM (CIL interpreter) in C#. I would test that with little CIL sequences created 'by hand'. Once I had that working I would build a small compiler that emits a tiny subset of CIL corresponding to what the VM supports and test that. Then I would incrementally refine this until I had the support I desired.

  • Any ideas how to start with VM? – luccio Feb 17 '12 at 18:10
  • That's basically going to be a switch statement in a loop, with an instruction pointer index, iterating over an array of byte-coded instructions, each case implementing the functionality of an instruction. Simple in principle :) – 500 - Internal Server Error Feb 17 '12 at 18:54
  • thanks for showing me the way. But where can I find the instructions for native code? Sorry for dumb question...some source code of compiler to native code would be great. – luccio Feb 17 '12 at 23:44
  • @luccio You don't need to convert to "machine" (native) code. Basically your VM would be an interpreter – Vagaus Feb 22 '12 at 12:27
0

Assuming you already have written C# parser and IL code emitter (which by itself is no small task, look at how long the C# specification is), the normal way to execute the code would be to create an assembly: an .exe or .dll file that contains the IL code along with the necessary metadata. You could then use that the same way as any other .Net assembly.

To create the assembly in C#, you can use System.Reflection.Emit. Specifically, start with AppDomain.DefineDynamicAssembly().

Another option, if you don't want to create whole assembly, just a single method, is to use DynamicMethod.

svick
  • 236,525
  • 50
  • 385
  • 514