Previous    Next

Translation to Intermediate Code

trans-late: to turn into one's own or another language

Webster's Dictionary


The semantic analysis phase of a compiler must translate abstract syntax into abstract machine code. It can do this after type-checking, or at the same time. Though it is possible to translate directly to real machine code, this hinders portability and modularity. Suppose we want compilers for N different source languages, targeted to M different machines. In principle this is N ยท M compilers (Image 7.1a), a large implementation task.

Java Click To expand
Image 7.1: Compilers for five languages and four target machines: (a) without an IR, (b) with an IR.

An intermediate representation (IR) is a kind of abstract machine language that can express the target-machine operations without committing to too much machine-specific detail. But it is also independent of the details of the source language. The front end of the compiler does lexical analysis, parsing, semantic analysis, and translation to intermediate representation. The back end does optimization of the intermediate representation and translation to machine language. A portable compiler translates the source language into IR and then translates the IR into machine language, as illustrated in Image 7.1b. Now only N front ends and M back ends are required. Such an implementation task is more reasonable.

Even when only one front end and one back end are being built, a good IR can modularize the task, so that the front end is not complicated with machine-specific details, and the back end is not bothered with information specific to one source language. Many different kinds of IR are used in compilers; for this compiler we have chosen simple expression trees.

JaVaScreenshot Previous    Next