How Programs Run
Compilers, interpreters, bytecode VMs and JITs — the journey from source code to machine instructions, and how the OS loads and runs a process.
The CPU only understands machine code: binary instructions for its instruction set (x86-64, ARM64…). Every language needs some route from the text you write to those instructions. The route a language takes explains a lot about its speed, startup time, portability and error messages.
Three strategies
| Strategy | How | Pros | Cons | Examples |
|---|---|---|---|---|
| Ahead-of-time compiled | Translate the whole program to machine code before running | Fastest execution, no runtime needed | Compile step; a separate binary per platform | C, C++, Rust, Go, Swift |
| Bytecode + VM | Compile to portable bytecode; a virtual machine runs it and JIT-compiles hot code | "Write once, run anywhere"; fast after warm-up | VM startup and memory overhead | Java, Kotlin, C#, Scala |
| Interpreted | Read and execute the program directly (usually via internal bytecode) | Instant start, very dynamic | Slower per operation | Python (CPython), Ruby, early JS |
Modern runtimes blur the lines: V8 (JavaScript) JIT-compiles hot functions to machine code, PyPy JITs Python, and Java can be compiled ahead of time with GraalVM native-image.
Inside a compiler
- Lexing — split text into tokens:
int,x,=,42,;. - Parsing — build an abstract syntax tree (AST) following the grammar.
- Semantic analysis — resolve names, check types; most "compile errors" come from here.
- Intermediate representation (IR) — a simpler, machine-independent form.
- Optimisation — constant folding, inlining, dead-code removal, loop optimisations.
- Code generation — pick instructions, allocate registers, emit machine code.
The Compilers track goes through each stage in detail.
JIT compilation
A just-in-time compiler watches the program run, finds hot code (functions or loops executed many times), and compiles them to optimised machine code at runtime. It can use information an ahead-of-time compiler doesn't have, such as which types actually show up, and deoptimise when its assumptions break.
Linking
Large programs are compiled in pieces (object files) and then linked:
- Static linking copies library code into the executable: one self-contained file, but larger.
- Dynamic linking records a reference to a shared library (
.so,.dll,.dylib) that's loaded at runtime: smaller binaries, and libraries can be updated without rebuilding, at the risk of "DLL hell" version mismatches.
From executable to running process
When you run a program, the operating system:
- Reads the executable file (ELF on Linux, PE on Windows, Mach-O on macOS).
- Creates a process: a private virtual address space, a process ID, open-file table, and so on.
- Maps the code and data sections into memory (lazily, page by page as they're touched).
- Loads dynamic libraries and resolves symbols (the dynamic linker).
- Sets up the stack with the command-line arguments and environment variables.
- Jumps to the entry point; the runtime's startup code runs, then your
main.
That memory layout is the subject of the next lesson: Memory: stack & heap.
Compile-time vs runtime errors
- Compile-time errors (syntax errors, type mismatches) stop the program from building. Statically typed languages catch many bugs here.
- Runtime errors (null dereference, index out of range, division by zero) happen while the program runs. Dynamic languages find type errors only at runtime, which is why tests and type checkers (TypeScript, mypy) matter.
- CPUs run only machine code; languages get there by AOT compilation, bytecode VMs or interpretation.
- Compiler pipeline: lex → parse (AST) → check → IR → optimise → generate code.
- JITs compile hot code at runtime using observed behaviour.
- Linking combines object files and libraries, statically or dynamically.
- The OS loader creates a process, maps code and data, sets up the stack, and jumps to the entry point.
QIs Java compiled or interpreted?easy
Both. javac compiles source to bytecode ahead of time. The JVM interprets bytecode at first and JIT-compiles hot methods to native code at runtime.
QWhy is Python slower than C for a tight loop?medium
CPython interprets bytecode: each operation dispatches through the interpreter loop, every value is a heap-allocated object with a dynamic type check, and there is no JIT (until the experimental one in 3.13+). C compiles the loop to a few machine instructions working on registers.