Compilers
What is the main advantage of using an intermediate representation (IR), independent of both source language and CPU, inside a compiler?
- AUsing an IR lets code be compiled without reading the source
- BThe CPU can execute the IR directly, so machine code generation is unnecessary
- 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.