ArticleslgStudy

science

Intermediate representation

Intermediate representation is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Intermediate representation rather than just read about it. In short: An intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An IR is designed to be conducive to further processing, such as optimization and translation.

Key takeaways

  • Intermediate representation belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Intermediate representation to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Intermediate representation from memory before moving on to harder problems.

Reference excerpt

An intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An IR is designed to be conducive to further processing, such as optimization and translation. A "good" IR must be accurate – capable of representing the source code without loss of information – and independent of any particular source or target language. An IR may take one of several forms: an in-memory data structure, or a special tuple- or stack-based code readable by the program. In the latter case it is also called an intermediate language. A canonical example is found in most modern compilers. For example, the CPython interpreter transforms the linear human-readable text representing a program into an intermediate graph structure that allows flow analysis and re-arrangement before execution. Use of an intermediate representation such as this allows compiler systems like the GNU Compiler Collection and LLVM to be used by many different source languages to generate code for many different target architectures.

Intermediate language

An intermediate language is the language of an abstract machine designed to aid in the analysis of computer programs. The term comes from their use in compilers, where the source code of a program is translated into a form more suitable for code-improving transformations before being used to generate object or machine code for a target machine. The design of an intermediate language typically differs from that of a practical machine language in three fundamental ways:

Each instruction represents exactly one fundamental operation; e.g. "shift-add" addressing modes common in microprocessors are not present. Control flow information may not be included in the instruction set. The number of processor registers available may be large, even limitless. A popular format for intermediate languages is three-address code. The term is also used to refer to languages used as intermediates by some high-level programming languages which do not output object or machine code themselves, but output the intermediate language only. This intermediate language is submitted to a compiler for such language, which then outputs finished object or machine code. This is usually done to ease the process of optimization or to increase portability by using an intermediate language that has compilers for many processors and operating systems, such as C. Languages used for this fall in complexity between high-level languages and low-level languages, such as assembly languages.

Languages Though not explicitly designed as an intermediate language, C's nature as an abstraction of assembly and its ubiquity as the de facto system language in Unix-like and other operating systems has made it a popular intermediate language: Eiffel, Sather, Esterel, some dialects of Lisp (Lush, Gambit), Squeak's Smalltalk-subset Slang, Nim, Cython, SystemTap, Vala, V, and others make use of C as an intermediate language. Variants of C have been designed to provide C's features as a portable assembly language, including C-- and the C Intermediate Language. Any language targeting a virtual machine or p-code machine can be considered an intermediate language:

Java bytecode Microsoft's Common Intermediate Language is an intermediate language designed to be shared by all compilers for the .NET Framework, before static or dynamic compilation to machine code. While most intermediate languages are designed to support statically typed languages, the Parrot intermediate representation is designed to support dynamically typed languages—initially Perl and Python. TIMI is used by compilers on the IBM i platform. O-code for BCPL MATLAB precompiled code Microsoft P-Code Pascal p-code The GNU Compiler Collection (GCC) uses several intermediate languages internally to simplify portability and cross-compilation. Among these languages are

the historical Register Transfer Language (RTL) the tree language GENERIC the SSA-based GIMPLE. (Lower-level than GENERIC; input for most optimizers; has a compact "bytecode" notation.) GCC supports generating these IRs, as a final target:

HSA Intermediate Layer LLVM Intermediate Representation (converted from GIMPLE in the now-defunct llvm-gcc which uses LLVM optimizers and codegen) The LLVM compiler framework is based on the LLVM IR intermediate language, of which the compact, binary serialized representation is also referred to as "bitcode" and has been productized by Apple. Like GIMPLE Bytecode, LLVM Bitcode is useful in link-time optimization. Like GCC, LLVM also targets some IRs meant for direct distribution, including Google's PNaCl IR and SPIR. A further development within LLVM is the use of Multi-Level Intermediate Representation (MLIR) with the potential to generate code for different heterogeneous targets, and to combine the outputs of different compilers. The ILOC intermediate language is used in classes on compiler design as a simple target language.

Other Static analysis tools often use an intermediate representation. For instance, Radare2 is a toolbox for binary files analysis and reverse-engineering. It uses the intermediate languages ESIL and REIL to analyze binary files.

See also Abstract syntax tree – Tree representation of the abstract syntactic structure of source code BURS Bytecode – Instruction set designed to be run by a software interpreter Graph rewriting – Creating a new graph from an existing graph Interlingual machine translation – Type of machine translation Pivot language – Intermediary language between different languages Source-to-source compiler – Translator of computer source code Symbol table – Data structure used by a language translator such as a compiler or interpreter Term rewriting – Replacing subterm in a formula with another termPages displaying short descriptions of redirect targets UNCOL

References

External links The Stanford SUIF Group

Worked examples

Example 1 — a first encounter with Intermediate representation

Start with the simplest possible case. Write down what Intermediate representation claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Intermediate representation before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Intermediate representation ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Intermediate representation

In research
Intermediate representation appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Intermediate representation in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Intermediate representation is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiler construction, Programming language classification, so understanding it makes those chapters shorter.
In everyday life
Look for Intermediate representation outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Intermediate representation” →

How to study Intermediate representation in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Intermediate representation means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Intermediate representation out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Intermediate representation in simple terms?

An intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An IR is designed to be conducive to further processing, such as optimization and translation.

Why does Intermediate representation matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Intermediate representation?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Intermediate representation.

Tags

  • Compiler construction
  • Programming language classification

Keep exploring