1. Gyanastra
  2. Compiler Design
  3. Intermediate Code Generation
  4. Role and Importance of Intermediate Code (Portability, Bytecode)
Free lesson, no account needed 5 min read 11 practice questions after it
Asked in past papers4 times
  • GATE CSE201420081994
  • ISRO2008

Compiler Design · Intermediate Code Generation

Role and Importance of Intermediate Code (Portability, Bytecode)

Overview

The intermediate code generator is the compiler phase that produces intermediate code, an abstract, machine-independent representation of the source program.
It matters because it splits the compiler into a machine-independent front end and a machine-dependent back end, which makes the compiler portable across machines.

Key concepts

  • Intermediate code generator: the compiler phase that produces intermediate code, an abstract, machine-independent representation of the source program.
  • Front end: lexical analysis, syntax analysis, semantic analysis, and intermediate code generation. It depends only on the source language, not on the target machine, and produces the intermediate code.
  • Intermediate code: the output of the front end. It is machine-independent and identical for every target machine.
  • Back end: target code generation and code optimization. It consumes the intermediate code and produces machine code; it is the only part that depends on the target machine.
  • Portability: because the intermediate code is the same for all machines, one front end plus one intermediate code can be reused, and only a new back end is written per machine.
  • Bytecode: in Java, bytecode is intermediate code. It can be generated on any machine and executed on any other machine because every machine has an interpreter suitable for that platform.

How it works

The compiler is split into two parts by the intermediate code:

  1. Front end — lexical analysis, syntax analysis, semantic analysis, and intermediate code generation. This part depends only on the source language, not on the target machine.
  2. Intermediate code — the output of the front end. It is machine-independent and identical for every target machine.
  3. Back end — target code generation and code optimizer. This is the only part that depends on the target machine.

Building a compiler for a new machine therefore does not require rewriting everything:

  • Suppose a C compiler has already been designed for a Windows machine, and now a C compiler is needed for a Linux machine.
  • The front end (lexical analysis, syntax analysis, semantic analysis, intermediate code generation) stays exactly the same.
  • The intermediate code coming out of the intermediate code generator is the same for all machines.
  • Only the two back-end phases — target code generation and code optimizer — have to be changed to produce a compiler for the different machine.

For Java, bytecode is generated on one machine and sent to another machine, where it executes; every machine has an interpreter suitable for that platform. In the same way, for every machine only the back end — the target code generator and the code optimizer — is changed.

Figure. Intermediate code makes the compiler portable

Worked examples

Porting a C compiler from Windows to Linux:

  • Keep the entire front end unchanged: lexical analysis, syntax analysis, semantic analysis, intermediate code generation.
  • Keep the intermediate code unchanged: it is the same for all machines.
  • Change only the two back-end phases: target code generation and code optimization.

Java bytecode:

  • Bytecode is intermediate code.
  • It is generated on one machine and sent to some other machine, where it executes.
  • It can be generated on any machine and executed on any other machine, because every machine has an interpreter suitable for that platform.

Comparison

Compiler partPhasesDepends on
Front endLexical analysis, syntax analysis, semantic analysis, intermediate code generationSource language only, not the target machine
Intermediate codeOutput of the front endNothing machine-specific; the same for all machines
Back endTarget code generation, code optimizerThe target machine

Revision summary

  • Intermediate code is machine-independent — the same for all machines.
  • The front end produces the intermediate code; the back end consumes it and produces machine code.
  • Porting to a new machine changes only target code generation and code optimization; the entire front end and the intermediate code stay unchanged.
  • Java bytecode is intermediate code: generated on one machine, executed on another, because each platform has a suitable interpreter.
  • For each new machine, only the two back-end phases need to be changed; the intermediate code phase stays the same.

Previous-year exam insights

  • A major purpose of intermediate code is to enable reuse of the machine-independent code optimizer: the same optimizer (and front end) can be shared by compilers for several target machines, since optimization and analysis are done on machine-independent intermediate code rather than on machine code.
    Asked inGATE 2014GATE 2008
  • Intermediate code based on an abstract machine model enhances the portability of the compiler (front end) — the same front end and intermediate representation can be retargeted by writing only a new back end.
    Asked inGATE 1994
  • Intermediate code is typically a machine-independent, low-level or abstract-machine representation (e.g., three-address code, quadruples, triples, indirect triples, postfix, syntax trees/DAGs); it is not tied to any real machine's instruction set or registers.
  • The front end (lexical, syntax, semantic analysis, intermediate code generation) depends only on the source language; the back end (target code generation, machine-dependent optimization) depends on the target machine.
  • Java bytecode is a portable intermediate representation executed by a platform-specific JVM interpreter (or JIT compiler), which is why the same bytecode runs on any machine that has a JVM.
  • Intermediate code also simplifies retargeting in the many-source/many-target case: with m source languages and n target machines, using a common intermediate code needs m front ends + n back ends instead of m×n compilers.
Pages 1–2 of 2