3

I am doing a compilers discipline at college and we must generate code for our invented language to any platform we want to. I think the simplest case is generating code for the Java JVM or .NET CLR. Any suggestion which one to choose, and which APIs out there can help me on this task? I already have all the semantic analysis done, just need to generate code for a given program.

Thank you

4
  • 1
    I believe that the fun part of the project is generating yourself the code. I had a similar project, but I had to build also an intermediate language and a virtual machine for that language. It was very nice to compile a "high-language", run the "binary" and see that your program really works Commented Dec 14, 2009 at 18:55
  • Yeah that was my first thought, since we already have a 'common' IL at our class... But I thougth generating code to run on a real environment was more challenging. Commented Dec 15, 2009 at 10:15
  • There's nothing wrong in doing it step-by-step. After writing a compiler to JVM bytecode, for example, one can write a compiler from JVM bytecode to native code :) Commented Dec 15, 2009 at 17:02
  • By the way, Pedro, if you aren't specifically looking for a VM (i.e. you do not need GC and other services), but just for portable and slightly more high-level intermediate language to compile to, you should also consider LLVM; see (en.wikipedia.org/wiki/Low_Level_Virtual_Machine and llvm.org/docs/LangRef.html) - the advantage is that you can compile from LLVM code directly to native code that runs without any runtime VM. Commented Dec 15, 2009 at 17:04

3 Answers 3

6

From what I know, on higher level, two VMs are actually quite similar: both are classic stack-based machines, with largely high-level operations (e.g. virtual method dispatch is an opcode). That said, CLR lets you get down to the metal if you want, as it has raw data pointers with arithmetic, raw function pointers, unions etc. It also has proper tailcalls. So, if the implementation of language needs any of the above (e.g. Scheme spec mandates tailcalls), or if it is significantly advantaged by having those features, then you would probably want to go the CLR way.

The other advantage there is that you get a stock API to emit bytecode there - System.Reflection.Emit - even though it is somewhat limited for full-fledged compiler scenarios, it is still generally enough for a simple compiler.

With JVM, two main advantages you get are better portability, and the fact that bytecode itself is arguably simpler (because of less features).

Sign up to request clarification or add additional context in comments.

4 Comments

Nice, maybe I'll stay with Java since the language is very simple, it only has a few basic operations. Any sugestions on an API on Java that can help me or I'll have to learn the inners of bytecode to generate it 'manually'?
Apache BCEL (jakarta.apache.org/bcel) seems to be a popular bytecode manipulation library. ASM (asm.ow2.org) also looks interesting.
On the other hand, note that you do not need to use the full set of CLR operations, either, and the basic ones are mostly equivalent to those in JVM. Also, CLR has a standardized textual syntax to represent its MSIL bytecode (en.wikipedia.org/wiki/MSIL), so one other option is to output MSIL, and then have ilasm compile that; this may be somewhat easier to debug compared to outputting binary bytecode directly.
Thanks, Pavel, I'll try Apache BCEL, seems to fit my needs. I always see your constructive comments at Eric Lippert's blog. Keep it up the good work :)
1

Another option that i came across what a library called run sharp that can generate the MSIL code in runtime using emit. But in a nicer more user friendly way that is more like c#. The latest version of the library can be found here. http://code.google.com/p/runsharp/

Comments

0

In .NET you can use the Reflection.Emit Namespace to generate MSIL code.

See the msdn link: http://msdn.microsoft.com/en-us/library/3y322t50.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.