views:

104

answers:

3

I've written an interpreter for my experimental language and know I want to move on and write a small compiler for it. It will probably take the source, go through the same steps as the interpreter (tokenizer, parser) and then translate the source to assembly.

Now my questions:

  • Can I expect that every command in my language can be 1:1 translated to a bunch of assembly instructions? What I mean is if I will have to completely throw over the whole input program or if it is just translated to assembly per line.

  • Which assembler should I use as output format?

+2  A: 
  1. I interpret your question as asking whether each statement in your language will be translated into the same ASM lines in any context. The answer is: it depends on your language, but usually no. There are complicating issues such as register allocation which may cause the output not to be 1:1, depending on the context. Your statement may need some registers, which at one context may be free and at another occupied (so you'll use the stack).
  2. Depends on the platform and what your plans are. If you want to compile into native assembly for Windows, the x86 assembly language should be your goal.
Eli Bendersky
A: 

Unless you are absolutely determined to "reinvent the wheel", you might as well just emit C code and then pass that to an existing C compiler, e.g. gcc. It will be much easier than writing your own back-end and you'll get all the C compiler's optimisations etc for free.

Paul R
Why, are you scared he will provide a decent system for Windows? Advising GCC is then a good way to ruin that :-)
Marco van de Voort
A: 

No, you can't expect that. If your language contains stuff like eval() it can get funky.

As backend assembler, nasm seems to be most popular. GAS is possible, but I found it to be a bit unforgiving and incomplete.

Marco van de Voort