IB COMPUTER SCIENCE · A1.4.1

Interpreters and compilers

Lesson objective

Evaluate how different translation processes execute programs and justify a choice for rapid testing, performance-critical work and cross-platform deployment.

LEARN

From source code to execution

Why do programs need translation?

A processor can directly execute only instructions from its own instruction set. Each instruction is encoded as a pattern of bits and can perform a basic operation such as loading a value, adding values or jumping to another instruction.

  • High-level languages such as Python let programmers express ideas using variables, functions and data structures. For example, print(total) expresses a task in one line that takes many processor instructions to carry out.
  • Low-level languages describe operations closer to the hardware. Assembly uses symbols such as ADD to represent processor instructions. An assembler translates those symbols into the corresponding machine code.

Source files, bytecode and machine code are all stored as bits. Their meaning differs. The bits in a Python source file encode text characters. Bytecode bits encode instructions for a virtual machine. Machine-code bits encode instructions for a physical processor.

Compiler and interpreter

A compiler translates a program into another form, such as machine code or bytecode. An interpreter reads a program representation and carries out its operations as it runs. A compiler and an interpreter can work together, as they do in the standard CPython workflow below.

Explain the difference between a compiler and an interpreter

Ahead-of-time native compiler

  1. Build time. The compiler translates the source into machine code.
  2. Build output. A linker can produce an executable for the target platform.
  3. Run time. The CPU executes the program’s machine code.

Interpreter

  1. Input. The interpreter receives source code or bytecode.
  2. Run time. It reads the instructions and carries out their operations.
  3. CPU. The processor runs the interpreter’s native instructions.

Model answer. A compiler translates a program into another representation, such as machine code or bytecode. An interpreter carries out the program’s operations as it reads the source or bytecode at runtime. With ahead-of-time native compilation, the resulting machine code can be run repeatedly without recompiling the source each time.

Some systems use both. CPython compiles Python source to bytecode and then interprets it.

What form is the code in at each stage?

Follow the rows for each route. A native build may use assembly and object code. A virtual-machine workflow uses bytecode and may use a JIT compiler to generate machine code at runtime.

Code form or file What it contains Where it fits
Source code Program text written by the programmer, such as Python or C. A compiler or interpreter takes this as input.
Assembly language Readable mnemonics and operands for a processor’s instruction set, such as ADD. A native compiler can emit assembly, and an assembler translates it into machine code. Programmers can also write assembly directly.
Object code An object file in a typical native build contains machine code, data and information the linker uses. A linker combines object files and resolves references between them and libraries.
Bytecode Instructions for a virtual machine. A compiler produces it. A bytecode interpreter executes it, or a JIT compiler can translate parts of it to machine code.
Machine code Bit patterns encoding instructions for a specific physical processor. An assembler, native compiler or JIT can produce it. The CPU executes it.
Executable file A file the operating system can load to start a program. It can contain code, data and metadata. A linker can produce a native executable. A packaged Python .exe can include a native launcher, interpreter and app bytecode.

From Python source to processor instructions

What normally happens when CPython runs a script
  1. Python source · written by the programmer
    price = 7
    quantity = 3
    total = price * quantity
    print(total)
  2. CPython compiler · produces bytecode

    CPython parses the source and compiles it to bytecode. Here are some of the instructions for the multiplication, shown as readable names from a modern CPython version.

    LOAD_NAME    price
    LOAD_NAME    quantity
    BINARY_OP    *
    STORE_NAME   total
  3. CPython interpreter · executes bytecode

    The interpreter reads the multiplication instruction and calls the runtime code that handles multiplication for the two integer values.

  4. CPU · executes native machine code

    The CPU runs the already-compiled machine instructions of the interpreter and its runtime. Those instructions carry out the multiplication and the rest of the program, which prints 21.

    For example, 10010000 encodes one x86 instruction, NOP (no operation). This illustrates what a native instruction looks like in binary. The actual instructions used for the calculation depend on the processor and the CPython build.

For comparison, an ahead-of-time native compiler can produce machine code for the program itself before it runs. In the normal CPython workflow, the CPU executes the interpreter’s machine code while the interpreter executes the Python bytecode.

Python documentation on bytecode

What changes when you distribute a Python program as an .exe?

If you share app.py, the recipient needs a compatible Python runtime and any dependencies. A Windows bundle made with a tool such as PyInstaller can include the Python runtime, the app’s bytecode and its dependencies. The recipient launches app.exe without having to install Python separately.

Windows first runs the bundle’s native launcher. The launcher starts the bundled interpreter, which executes the app’s bytecode. Packaging changes how users install and launch the program. It can increase the file size and add startup time, particularly when a single-file bundle extracts files at launch. A Windows bundle targets Windows and a compatible processor architecture.

An .exe tells you what Windows can launch. To tell whether the program’s logic was compiled to native instructions ahead of time or bundled with an interpreter, check how the file was built.

PyInstaller documentation on bundling

How each approach works

The processor executes native machine instructions. A compiler translates code into another form, such as native machine code or bytecode. An interpreter executes the instructions described by a program, using its source or an intermediate representation as input. One runtime can combine both approaches.

Ahead-of-time (AOT) native compilation

Before the program runs, a compiler analyses the source, may optimise it and generates code for a target processor. A linker can combine compiled code and libraries into an executable. For example, a C++ team can build a native executable for a simulation and run it many times from one build.

Source interpretation

A source interpreter reads the program, parses it and carries out its operations during execution. It may parse an entire script before executing any part of it. An interactive interpreter can also accept a command at a time, which helps when trying out short expressions or testing a small change.

Bytecode interpretation

A compiler converts the source into bytecode, a set of instructions for a virtual machine. The virtual machine reads those instructions and performs their operations. For example, CPython typically compiles Python source to bytecode internally and executes it with its interpreter. The developer can run a script without a separate manual build step.

Just-in-time (JIT) compilation

A JIT compiler generates native machine code while a program is running. For example, a Java runtime can interpret bytecode initially, measure which methods run frequently and compile those methods to native code. Later calls can reuse the compiled code. Compilation adds work during warm-up, while frequently executed code can benefit over a long run.

Compare the trade-offs

Trade-offs by translation approach
Approach Error detection Translation and execution time Portability Applicability
AOT native compilation Reports syntax errors and any static checks the language requires during the build. Runtime errors can still occur. Pays the build cost before execution. One build can serve many runs. The executable targets a particular OS and processor architecture. Source may be rebuilt for others. Repeated workloads and latency-sensitive applications that benefit from native code being ready at launch.
Source interpretation May detect syntax errors during parsing. Errors that depend on values or paths can appear during execution. Usually gives a quick edit–run cycle. Parsing and interpretation contribute to execution cost. Source can run on systems with a compatible interpreter and dependencies. Interactive experiments, scripting and rapid testing.
Bytecode interpretation The source compiler can catch syntax errors and, where supported, static type errors. The VM can check bytecode validity. Runtime faults still depend on execution. Pays for source-to-bytecode translation before execution or at load time. Decoding and executing bytecode costs time during each run. The same bytecode can run on compatible VMs, provided required libraries and features are available. Portable distribution and environments where a native build per platform is inconvenient.
JIT compilation Earlier compilation or parsing can detect errors before execution. Compiling hot code during execution still leaves input-dependent faults for runtime. Adds compilation time during execution. Repeated execution can repay that cost and may benefit from runtime profiling. A compatible runtime can generate native code for its own platform from the same portable input. Long-running services and workloads where warm-up costs can be spread across many operations.

A missing bracket may prevent a program from starting. Division by a value entered by the user may fail only when the input is zero and the division runs. The language, its checks and the path through the program all affect when an error appears.

Rapid development and testing

A student is changing a CSV-cleaning script every few minutes and running it on small test files.

Judgement. Use an interpreted workflow such as Python. A short edit–run cycle helps the student see results quickly, and runtime overhead is likely to matter little for small files. Test branches with invalid input, since faults on those paths may surface only when exercised.

Performance-critical application

A tool must process a large image batch quickly from a cold start, and it runs on a fixed platform.

Judgement. An optimised AOT native build is a sensible starting point. The build cost is paid before deployment and native instructions are ready at launch. A JIT could improve throughput on repeated work, so compare cold-start and total batch times before deciding.

Cross-platform development

A school wants to distribute one application to Windows, macOS and Linux, and can provide compatible Java runtimes.

Judgement. Distribute common bytecode and use the VM for each platform. This reduces the need to ship separate native builds, and the VM may use JIT compilation on each machine. Check runtime versions and dependencies on all three platforms, especially any native libraries.

Drag-and-drop practice

Drag each answer onto its description. You can also select an answer, then select a description using touch or the keyboard.

Match the code form

Match the tool to its job

Choose an approach for each scenario