views:

598

answers:

3

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?

+1  A: 

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 :)

Jon Skeet
+1  A: 

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.

wuub
I have indeed thought about both x86 assembly and writing a VM. I am just wondering if a RISC architecture might be better for a first try. Any thoughts?
Jørgen Fogh
My only RISC experience is with AVR Assembly, and I love it. It's fun, clean and easy to learn. I don't know a lot about ARM or PowerPC but would guess, that anything is better than x86 :)
wuub
+1 for wuub. It's really nice work with RISC as a first attempt, you could try MIPS, and use SPIM for simulations.
Eliseo Ocampos
+2  A: 

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.

Norman Ramsey