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.

Beginner 4 min readLesson 5 of 8 Interview Q&A

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

C / C++ / Rust / Gocompilerahead of timenative machine codeJava / C# / Kotlinjavacto bytecodeVM: interpret + JITJVM / CLRPython / JavaScriptruntimeparse → bytecodeinterpreter (+ JIT in V8)
StrategyHowProsConsExamples
Ahead-of-time compiledTranslate the whole program to machine code before runningFastest execution, no runtime neededCompile step; a separate binary per platformC, C++, Rust, Go, Swift
Bytecode + VMCompile to portable bytecode; a virtual machine runs it and JIT-compiles hot code"Write once, run anywhere"; fast after warm-upVM startup and memory overheadJava, Kotlin, C#, Scala
InterpretedRead and execute the program directly (usually via internal bytecode)Instant start, very dynamicSlower per operationPython (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

  1. Lexing — split text into tokens: int, x, =, 42, ;.
  2. Parsing — build an abstract syntax tree (AST) following the grammar.
  3. Semantic analysis — resolve names, check types; most "compile errors" come from here.
  4. Intermediate representation (IR) — a simpler, machine-independent form.
  5. Optimisation — constant folding, inlining, dead-code removal, loop optimisations.
  6. 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:

  1. Reads the executable file (ELF on Linux, PE on Windows, Mach-O on macOS).
  2. Creates a process: a private virtual address space, a process ID, open-file table, and so on.
  3. Maps the code and data sections into memory (lazily, page by page as they're touched).
  4. Loads dynamic libraries and resolves symbols (the dynamic linker).
  5. Sets up the stack with the command-line arguments and environment variables.
  6. Jumps to the entry point; the runtime's startup code runs, then your main.
high addresseslow addressesStackfunction calls, local variables — grows down↓ free ↑Heapdynamic allocations — grows upData / BSSglobals and staticsTextmachine code (read-only)

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.
Key takeaways
  • 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.
Interview questions
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.