Hacker News new | past | comments | ask | show | jobs | submit login

For anyone wanting to read about C as intermediate language,

Compiler Design in C (1990)


EDIT: Adding some extra remarks I think might also be interesting to share.

Another approach, that I really like, is to output bytecodes that are mapped directly to macros in typical macro assemblers like NASM/MASM/TASM. Those macro assemblers provide very powerful macro systems.

Then map those macros to the corresponding assembly code.

Sure it gives a bit more work, but I find it more fun.

From a look over the Amazon page, the book seems to be about writing a compiler in C, not about writing a compiler targeting C. Does it actually describe potential issues with compiling to C?

It is an old book about implementing an C compiler in C.

The intermediate code is similar to the article, a mix of macros and basic C expressions as high level assembler.

I cannot remember all the details, the last time I used the book was around 1996.

You can still get the source code, http://www.holub.com/software/compiler.design.in.c.html

here's my one line C compiler (compiles to C):

  touch *.c *.h

One advantage of going through C (which admittedly might not pay off) is that you get the advantage of compiler optimisations.

When compiling dynamic languages of course, that often doesn't work as the optimiser doesn't have anything to go on if you do everything through void* pointers, or a similar dynamic construct.


You might find some anti-C bias from my post history, but I am also very pragmatic and would certainly use C if required to do so.

I used the compilation to bytecode disguised as Assembler macros as part of a few toy compilers back in late 90's.

If it was today I would probably use LLVM or C(++) as being discussed in the article.

C: AKA "portable assembler" :-)

TI has a DSP processor where the assembly looks quite much like the early days of C:

    mov reg1, reg2    ==>  reg1 = reg2
    mov reg1, [reg2]  ==>  reg1 = *reg2
    add reg1, reg2    ==>  reg1 += reg2
And so on.

looks sort of like PDP 11 assembler, particularly the [] brackets for indirect access through a register.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact