Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing an intermediate representation for a compiler

I've been looking at compiler design. I've done a one semester course on it at University and have been reading Modern Compiler Design by Grune et al, the book seems to advocate an annotated Abstract Syntax Tree as the intermediate code, and this is what we used in the course.

My question is what are the benefits of this approach versus producing some kind of stack-machine language or low level pseudo code , particularly with regard to having a compiler which can target many machines.

Is it a good idea to simply target an already existing low level representation such as LLVM and use that as the intermediate representation?

like image 930
Magnus Avatar asked Jan 06 '11 01:01

Magnus


People also ask

What is intermediate representation in compiler design?

An intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An IR is designed to be conducive to further processing, such as optimization and translation.

What are the three kinds of intermediate representation in compiler design?

Types of Intermediate RepresentationsFlat, tuple-based, generally three-address code (quadruples) Flat, stack-based.

What is intermediate code generation in compiler design?

Intermediate code can translate the source program into the machine program. Intermediate code is generated because the compiler can't generate machine code directly in one pass. Therefore, first, it converts the source program into intermediate code, which performs efficient generation of machine code further.


1 Answers

If your language is complicated enough, you'd end up having a sequence of slightly different intermediate representations any way. And it does not really matter, which representation will be your final target - llvm, C, native code, CLR, JVM, whatever. It should not affect the design and architecture of your compiler.

And, from my personal experience, the more intermediate steps you have, with transforms in between as trivial as possible, the better your compiler's architecture is.

like image 87
SK-logic Avatar answered Sep 24 '22 00:09

SK-logic