Back to series
· 9 min read

Compilation, Interpretation, and Execution


1. Why do programs need compilation or interpretation?

Programmers use programming languages to describe computational logic, but the syntax and semantics of different programming languages are not instruction formats that a CPU can understand directly.

A CPU executes machine instructions that conform to its instruction set architecture (ISA). Therefore, a program must use some execution mechanism to translate the operations described by the source code into computation the processor can perform.

Common program execution mechanisms include:

  • Compilation: converting source code or an intermediate representation into another form of code; common results include machine code and bytecode.
  • Interpretation: an interpreter reads the program representation and performs the corresponding operations according to the language semantics.
  • Just-in-time compilation (JIT): dynamically compiling part of the code into machine code while the program is running.
  • Ahead-of-time compilation (AOT): completing compilation before the program runs, generating code for the target platform in advance.

Note that compilation and interpretation are not mutually exclusive language properties. Modern language runtimes often combine multiple techniques to execute programs.

2. Compilation

2.1 What is compilation?

Compilation is the process of converting one program representation into another.

The most common case is converting high-level language source code into machine code for the target platform.

For example, a C program:

#include <stdio.h>

int main(void) {
    int a = 10;
    int b = 20;

    printf("%d\n", a + b);

    return 0;
}

Compiled with GCC:

gcc main.c -o app

The compiler analyzes the source code and generates a program that the target platform can execute.

Logically, it looks like this:

flowchart TB
    A["Source code"] --> B["Lexical analysis"]
    B --> C["Syntax analysis"]
    C --> D["Semantic analysis"]
    D --> E["Intermediate representation IR"]
    E --> F["Code optimization"]
    F --> G["Target code generation"]
    G --> H["Machine code or object file"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

This is the typical processing model of a compiler. A real compiler may include more stages, or may combine several stages into one.

2.2 The main working stages of a compiler

1. Lexical analysis

Converts a sequence of source code characters into tokens that carry syntactic meaning.

For example:

int result = 10 + 20;

The following can be identified:

Token Type
int Keyword
result Identifier
= Operator
10 Integer literal
+ Operator
20 Integer literal
; Separator

Lexical analysis is mainly responsible for recognizing the basic symbols in a language; it is not responsible for fully determining whether an expression is valid.

2. Syntax analysis

Organizes tokens into syntactic structures according to the grammar rules of the language.

For example:

int result = 10 + 20;

An abstract syntax tree (AST) like the following can be built:

VariableDeclaration
├── Type: int
├── Name: result
└── Initializer
    └── Addition
        ├── IntegerLiteral: 10
        └── IntegerLiteral: 20

Syntax analysis mainly determines whether the code structure conforms to the language grammar.

For example:

int = result 10;

does not conform to the grammar rules for variable declarations in C.

3. Semantic Analysis

Syntactic correctness does not mean the program is semantically valid.

Semantic analysis requires further checks:

  • Whether variables are declared.
  • Whether types match.
  • Whether function call arguments meet the requirements.
  • Whether scope rules are satisfied.
  • Whether expressions conform to the language semantics.

For example:

int number = "hello";

The syntactic structure is basically valid, but a string literal cannot be assigned directly to a variable of type int in C, so a type diagnostic will be produced.

4. Intermediate Representation (IR)

Compilers usually do not generate final machine code directly from the AST; instead, they first convert it into an intermediate representation that is convenient for analysis and optimization.

For example, LLVM IR can express a simple computation in a form like the following:

define i32 @add() {
entry:
  %result = add i32 10, 20
  ret i32 %result
}

IR can reduce the coupling between the source language and the target processor.

A compiler can implement different optimization strategies and target code generators for the same IR.

5. Code Optimization

A compiler optimizes a program under the premise of ensuring that the program semantics remain unchanged.

Common optimizations include:

Optimization Technique Effect
Constant folding Compute constant expressions at compile time
Constant propagation Propagate known constants into other expressions
Dead code elimination Remove code that does not affect program results
Common subexpression elimination Avoid recomputing the same expression
Loop optimization Reduce repeated overhead in loops
Inlining optimization Replace function calls with the function body code
Register allocation Use CPU registers to hold variables wherever possible

For example:

int result = 10 * 20 + 5;

The compiler may directly compute:

int result = 205;

This is constant folding.

6. Code Generation

Convert the optimized intermediate representation into instructions for the target platform.

The generated result may be:

  • Assembly code.
  • Machine code.
  • Object files.
  • Other platform-specific code representations.

The final result depends on the compiler and build configuration.

2.3 Characteristics of Compilation

  • The main code transformation is usually completed before the program runs.
  • Deeper static analysis and optimization can be performed.
  • Some syntax, type, and semantic errors can be discovered in advance.
  • Machine code can be generated for a specific CPU architecture.
  • The compiled result may depend on the operating system, instruction set, and runtime libraries.

Compilation does not mean the program is necessarily a standalone executable. The generated program may still depend on dynamic libraries, the runtime environment, and operating system services.

III. Interpretation

3.1 What Is Interpretation?

Interpretation is a mechanism in which an interpreter executes the operations described by a program at runtime, according to the program's syntax and semantics.

Unlike traditional native compilation, interpreted execution does not necessarily require generating a separate machine code file in advance.

For example, a Python program:

a = 10
b = 20

result = a + b

print(result)

can be run through the CPython interpreter:

python main.py

The interpreter is responsible for reading the program, performing the corresponding operations, and outputting the result.

3.2 How Does an Interpreter Work?

The specific implementation of an interpreter depends on the language and the runtime.

A typical interpreter may adopt the following execution models:

flowchart TB
    A["Source Code"] --> B["Lexical Analysis and Syntax Analysis"]
    B --> C["Construct Syntax Structure"]
    C --> D["Interpreter Reads Program Representation"]
    D --> E["Execute Corresponding Operations"]
    E --> F["Update Runtime State"]
    F --> G{"Are There Remaining Operations to Execute?"}
    G -->|"Yes"| D
    G -->|"No"| H["Program Ends"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

It should be emphasized that this is an abstract model of interpreted execution; not all interpreters directly read the source code line by line.

For example, CPython typically first compiles Python source code into bytecode, and then a virtual machine executes the bytecode.

3.3 Main Components of an Interpreter

1. Source Code Parser

Responsible for recognizing the structure of the source code and building an AST or other internal representation.

2. Execution Engine

Executes the corresponding operations according to the program representation.

For example:

result = 10 + 20

The interpreter needs to:

  • Read constants.
  • Perform the addition operation.
  • Create or update variable bindings.
  • Save the execution result.

3. Runtime Environment

Interpreted execution usually requires the runtime environment to provide basic capabilities, such as:

  • Variable and scope management.
  • Function calls and returns.
  • Object creation and access.
  • Exception handling.
  • Memory management.
  • Module loading.

4. Built-in Functions and Runtime Library

Interpreters usually also provide a standard library, built-in functions, and the ability to interact with the operating system.

3.4 Characteristics of Interpreted Execution

  • Can process program representation at runtime.
  • Usually relies on an interpreter or virtual machine.
  • Facilitates dynamic type checking and runtime behavior management.
  • Usually has good cross-platform deployment capability.
  • May involve interpreter scheduling and dynamic execution overhead.

However, interpreted execution is not necessarily slower than compiled execution. Modern interpreters can use techniques such as caching, bytecode optimization, and JIT to significantly improve performance.

IV. The Core Differences Between Compilation and Interpretation

The essential difference between compilation and interpretation is not that one generates machine code while the other does not generate machine code at all, but rather that the timing and manner in which program code conversion and execution occur are different.

4.1 Core Comparison

Comparison Dimension Compilation Interpretation
Core Mechanism Convert program representation in advance Execute program representation at runtime
Code Conversion Timing Usually before execution Usually during execution
Typical Output Machine code, object files, bytecode AST, bytecode, or other internal representation
Execution Dependency Executable code and runtime environment Interpreter or virtual machine
Static Optimization Usually relatively rich Depends on the interpreter implementation
Error Detection Some errors can be found in advance Some errors are exposed at runtime
Deployment Method May require target platform artifacts Usually requires a corresponding runtime
Typical Implementations GCC, Clang Traditional AST interpreters

4.2 Execution Performance Differences

Traditional interpreted execution usually requires the interpreter to participate in instruction scheduling and runtime operations.

For example, when executing a single bytecode instruction, the interpreter may need to:

  1. Read the current bytecode.
  2. Parse the operation type.
  3. Obtain the operands.
  4. Invoke the corresponding execution logic.
  5. Update the virtual machine state.

Native machine code, on the other hand, can be executed directly by the CPU, without every instruction needing to be scheduled by a high-level language interpreter.

However, performance cannot be simply understood as compilation always being fast and interpretation always being slow.

Modern runtimes also employ:

  • Bytecode optimization.
  • Inline caching.
  • Type feedback.
  • JIT compilation.
  • Runtime deoptimization.
  • Garbage collection optimization.

These techniques can significantly change actual performance characteristics.

4.3 Execution Models of Compilation and Interpretation

flowchart TB
    A["Source Code"] --> B{"Execution Model"}

    B -->|"Traditional Compilation"| C["Compiler"]
    C --> D["Native Machine Code"]
    D --> E["CPU Execution"]

    B -->|"Interpreted Execution"| F["Interpreter"]
    F --> G["Program Representation"]
    G --> H["Interpretation Engine"]

    B -->|"Virtual Machine Execution"| I["Compile to Bytecode"]
    I --> J["Virtual Machine"]
    J --> K["Interpretation or JIT Compilation"]
    K --> L["CPU Executes Machine Code"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style H fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style J fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style K fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style L fill:#FFF5DE,stroke:#E8D29A,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

5. JIT Just-in-Time Compilation

5.1 What Is JIT?

JIT (Just-In-Time Compilation) is a just-in-time compilation technique.

During program execution, based on the current execution conditions, it dynamically compiles parts of the program code into machine code.

JIT combines the characteristics of compilation and interpretation:

  • It reduces initial compilation cost through interpreted execution.
  • It gathers program execution information from runtime data.
  • It compiles frequently executed code into machine code.
  • It uses runtime information for targeted optimization.

JIT is an important technology in modern virtual machines and dynamic language runtimes.

5.2 Why Is JIT Needed?

An interpreter can start quickly and begin executing a program, but for frequently executed code, repeated interpreter scheduling may incur relatively high overhead.

The core idea of JIT is:

It is not necessary to compile all code into machine code from the start; instead, priority is given to optimizing the code that is actually executed frequently.

For example, consider a function called in a loop one million times:

function calculate(value) {
    return value * 2 + 1;
}

for (let i = 0; i < 1000000; i++) {
    calculate(i);
}

At runtime, the function's call frequency and parameter types can be observed.

If a piece of code continues to be a hot spot, JIT may compile it into optimized machine code.

5.3 Typical JIT Workflow

flowchart TB
    A["Program startup"] --> B["Interpreted execution or low-cost execution"]
    B --> C["Collect runtime information"]
    C --> D["Identify hot code"]
    D --> E{"Are compilation conditions met?"}
    E -->|"No"| B
    E -->|"Yes"| F["JIT compilation"]
    F --> G["Generate optimized machine code"]
    G --> H["Execute optimized code"]
    H --> I{"Do optimization assumptions still hold?"}
    I -->|"Yes"| H
    I -->|"No"| J["Deoptimization"]
    J --> B

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style I fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style J fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

5.4 Hot Code

Hot code refers to code regions that are executed frequently during runtime.

Common identification criteria include:

  • Number of function calls.
  • Number of loop back-edge executions.
  • Method invocation frequency.
  • Runtime performance statistics.

JIT does not necessarily compile only entire functions; depending on the implementation strategy, it can also compile specific code regions.

5.5 Type Feedback

In dynamic languages, the actual types of some variables are determined only at runtime.

For example:

function add(a, b) {
    return a + b;
}

This function may receive either numbers or strings.

add(10, 20);
add("hello", "world");

JIT can collect the parameter types that the function actually receives.

If the parameters are observed to be numbers for a long time, it may generate optimized code for numeric operations.

However, if string parameters appear later, the previous optimization assumptions may no longer hold.

At this point, the runtime may perform deoptimization and fall back to a more general execution path.

5.6 Benefits and Costs of JIT

The main advantages of JIT:

  • It uses runtime information to optimize code.
  • It can reduce the overhead of interpreted execution.
  • It can generate machine code for the actual runtime environment.
  • It can dynamically adjust optimization strategies based on hot spots.

The costs of JIT:

  • It adds runtime compilation overhead.
  • It requires managing the generated machine code.
  • It increases runtime implementation complexity.
  • Deoptimization and recompilation may occur.
  • Optimization benefits may not yet be available during program startup.

Therefore, JIT does not necessarily make every program faster. For short-lived programs, the compilation cost may not be offset by subsequent performance gains.

6. AOT Ahead-of-Time Compilation

6.1 What Is AOT?

AOT (Ahead-Of-Time Compilation) is an ahead-of-time compilation technique.

It completes code conversion before the program runs, usually generating machine code directly for the target platform, and it can also generate other forms of precompiled artifacts.

For example:

gcc main.c -o app

GCC completes compilation and linking before the program runs.

This is a typical AOT compilation workflow.

6.2 Differences Between AOT and JIT

Comparison dimension AOT JIT
Compilation timing Before program execution During program execution
Compilation basis Information available at compile time Compile-time and runtime information
Startup cost Usually lower May include runtime compilation cost
Runtime optimization Mainly relies on optimizations completed ahead of time Can use runtime information for optimization
Code generation Usually generated ahead of time Dynamically generated during runtime
Typical scenarios Native programs, precompiled applications JVM, JavaScript engines

AOT and JIT are not completely opposing technologies.

Some runtimes can first use AOT to generate baseline code, and then use JIT to further optimize hot code.

7. Execution Mechanisms of Different Programming Languages

Understanding compilation, interpretation, JIT, and AOT requires looking at how specific languages are implemented.

7.1 C / C++

C and C++ typically use the AOT native compilation model.

flowchart TB
    A["C / C++ source code"] --> B["Compiler"]
    B --> C["Object files"]
    C --> D["Linker"]
    D --> E["Native executable"]
    E --> F["OS loader"]
    F --> G["CPU executes machine code"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Key characteristics:

  • The compilation phase usually produces native machine code.
  • At runtime, no high-level language interpreter is needed to interpret the source code statement by statement.
  • Deeper static optimizations are possible.
  • It may still depend on the operating system, runtime libraries, and dynamic link libraries.