Question

I am writing a Tiger compiler in F# and I have finally reached the point where I can no longer postpone the decision of a target architecture.

This is my first compiler, but it will definitely not be my last. So... what would be a good target architecture for a first compiler?

I have thought about targeting the CIL (.NET), but the intermediate code in the book seems more suited for a register machine.

I would also like to know where I should go when I have finished this compiler. Should I try targeting another architecture? Should I focus on another part of the compiler? Why?

Was it helpful?

Solution

For sheer personal satisfaction, there's no substitute for targeting the hardware you have and and running your compiled code on the bare metal. However, there are reasonable alternatives:

  • The MIPS is a very clean instruction set and simulators like SPIM are readily available. Your compiler will be simple and your debugging experiences relatively happy.

  • Depending on why you are writing a compiler, you may be happy targeting a low-level compiler-target language like LLVM or C--. But why should somebody else have all the fun of writing your back end?

  • If you have Intel or AMD hardware, I highly recommend using the 64-bit instruction set with SSE extensions. You will have twice as many registers to play with, and your floating-point code (if any) will be sane.

OTHER TIPS

If you're using F# to write the compiler, emitting CIL certainly sounds like a good decision, as you'll be able to use all the built-in capabilities of CodeDOM etc.

Alternatively you could design your own output format and write a VM running inside .NET, if that would make the output easier (by virtue of being a more suitable architecture). It might be easier to debug - although of course it does mean writing the VM as well :)

Have you thought about targeting x86 assembly? I did a Tiger compiler myself few years ago, and the assembly emitter, although hard to write, was one of the most rewarding things in the whole project. Writing your own small VM in C is also a good (if not better) idea.

Targeting existing VM is pragmatic, if you're creating a production language, but removes many learning possibilities from the exercise.

If I were you, I would take a closer look at different optimization techniques later in the project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top