Compilers

What is the main advantage of using an intermediate representation (IR), independent of both source language and CPU, inside a compiler?

  1. AUsing an IR lets code be compiled without reading the source
  2. BThe CPU can execute the IR directly, so machine code generation is unnecessary
  3. CLanguage-specific front ends and CPU-specific back ends can be separated, and optimizations shared
Show answer and explanation

Answer: C. Language-specific front ends and CPU-specific back ends can be separated, and optimizations shared

Supporting M languages and N CPUs with a compiler per combination needs M × N compilers, but with a common IR it takes only M front ends (language to IR) and N back ends (IR to CPU). Most optimizations, written once on the IR, then serve every language and CPU. The IR cannot be executed directly by the CPU and must finally be converted to machine code.

  • A. The source code must still be read.
  • B. The CPU cannot execute the IR directly; it must be translated to machine code.
  • C. Correct. Front and back ends are separated, and optimizations are shared.
Practice this topic Back to topic

More Compilers questions

  1. Which describes a basic block?
  2. In a control-flow graph, what do the nodes and edges represent?
  3. Which is a characteristic of an IR in SSA form?
  4. Converting the code above to SSA form inserts an expression like "x3 = φ(x1, x2)" after the if statement. What does the φ function do?
  5. Which is an example of constant folding by a compiler?