Intermediate Topic 26 of 50 Languages & Compilers
Intermediate Representations
Language-neutral forms used between compiler stages.
How it works
An intermediate representation is the internal form a compiler uses between the source language and machine code. Examples include abstract syntax trees, three-address code, control flow graphs, static single assignment form, bytecode and LLVM IR. A good IR is simple, explicit about control and data flow, independent of both source language and target machine, and easy to analyze and transform. Compilers often use several IRs at different levels of abstraction.
Step-by-step walkthrough
- Different languages are translated into a common IR.
- Another language joins them.
- Optimizations are written once on the IR.
- The IR is then lowered to x86, ARM or others.
- This gives M plus N work instead of M times N.
Where it is used
Optimizations, analysis, cross-language compilation and support for many targets rely on IRs. LLVM IR, GCC GIMPLE, Java bytecode and MLIR are widely used examples.
Common pitfall
Overly low-level IRs lose high-level information needed for optimizations, while high-level ones make machine details hard. Maintaining many IRs increases complexity. Different phases must preserve semantics when lowering. Choose IR levels deliberately, dump and inspect IR when debugging optimizations, and verify invariants after each pass.
Where it fits in the course
Intermediate Representations is topic 26 of 50 in Languages & Compilers, rated Intermediate. It follows Parser Generators. The next topic is Three-Address Code.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Languages & Compilers quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Languages & Compilers quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Intermediate Representations?
Where do people use Intermediate Representations?
What is the most common mistake with Intermediate Representations?
What should I learn before this topic?
How can I practice it?
Is Intermediate Representations hard to learn?
How long does it take to learn Intermediate Representations?
Which topics are related to Intermediate Representations?
What should I study after Intermediate Representations?
Related topics
- Parser Generators (Languages & Compilers)
- Three-Address Code (Languages & Compilers)
- LL and LR Parsing (Languages & Compilers)
- Static Single Assignment (Languages & Compilers)