Live data from Hacker News

C as an intermediate language (2012)

yosefk.com

1–10 of 32 posts

Re: C as an intermediate language (2012)

#2
For anyone wanting to read about C as intermediate language,

Compiler Design in C (1990)

http://www.amazon.com/Compiler-Design-C-Prentice-Hall-softwa...

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.

Re: C as an intermediate language (2012)

#3
There's a lot of extra stuff you get with targeting C: being able to write the run time in C very easily. For example its a lot easier to write your entire OO system in C in a few hundred lines, and keeping that easily debuggable is a massive time saver.

Re: C as an intermediate language (2012)

#4
post #2

For anyone wanting to read about C as intermediate language, Compiler Design in C (1990) http://www.amazon.com/Compiler-Design-C-Prentice-Hall-softwa... 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 m…

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.

Re: C as an intermediate language (2012)

#5
post #2

For anyone wanting to read about C as intermediate language, Compiler Design in C (1990) http://www.amazon.com/Compiler-Design-C-Prentice-Hall-softwa... 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 m…

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.

[deleted]

Re: C as an intermediate language (2012)

#6
post #2

For anyone wanting to read about C as intermediate language, Compiler Design in C (1990) http://www.amazon.com/Compiler-Design-C-Prentice-Hall-softwa... 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 m…

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?

Re: C as an intermediate language (2012)

#7
post #2

For anyone wanting to read about C as intermediate language, Compiler Design in C (1990) http://www.amazon.com/Compiler-Design-C-Prentice-Hall-softwa... 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 m…

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?

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

  touch *.c *.h

Re: C as an intermediate language (2012)

#8
post #2

For anyone wanting to read about C as intermediate language, Compiler Design in C (1990) http://www.amazon.com/Compiler-Design-C-Prentice-Hall-softwa... 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 m…

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

Re: C as an intermediate language (2012)

#9
post #2

For anyone wanting to read about C as intermediate language, Compiler Design in C (1990) http://www.amazon.com/Compiler-Design-C-Prentice-Hall-softwa... 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 m…

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.

Agree.

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.

Re: C as an intermediate language (2012)

#10
post #3

There's a lot of extra stuff you get with targeting C: being able to write the run time in C very easily. For example its a lot easier to write your entire OO system in C in a few hundred lines, and keeping that easily debuggable is a massive time saver.

Wouldn't that be even easier if you used LLVM IR, as you'd then be able to write your runtime in any language that LLVM supports?
Post reply on HN