How Code Runs

How source code becomes instructions a CPU executes: compilers, interpreters, bytecode and JIT.

What is it?

A CPU (the computer's processor) only understands machine code: long sequences of numbers, each meaning a tiny operation like "add these two values" or "jump to that instruction". Nobody wants to write that by hand.

So we write source code in a readable language and use a tool to translate it. There are a few strategies:

- Compiler: translates the whole program into machine code ahead of time, producing an executable file. C, C++, Rust and Go work this way. Running the result needs no translator. - Interpreter: reads the program and carries out its instructions directly, without producing a standalone machine-code file. - Bytecode + virtual machine (VM): the source is compiled into a compact, portable intermediate form called bytecode, which a VM then runs. Java (JVM), C# (.NET) and Python (CPython) work this way. - JIT (just-in-time) compiler: a VM watches which code runs often ("hot" code) and compiles that into machine code while the program is running. JavaScript engines like V8 and the Java HotSpot VM do this.

"Compiled language" and "interpreted language" are really descriptions of implementations, not languages. Python is usually compiled to bytecode and then interpreted; JavaScript is interpreted and JIT-compiled.

However the code is translated, it ends up as machine instructions running on the CPU, reading and writing memory (RAM).

Explain like I'm 10

A compiler is a translator who turns a whole book into French before you read it. An interpreter is a live interpreter at a meeting, translating each sentence as it's spoken. A JIT is a live interpreter who notices you keep saying the same phrases and writes those down in French so they can read them out instantly next time.

Examples

Compiled: C

// hello.c
#include <stdio.h>

int main(void) {
    printf("Hello from machine code!\n");
    return 0;
}

// In a terminal:
//   gcc hello.c -o hello   (compile once, ahead of time)
//   ./hello                (run the produced executable)

The compiler (gcc) produces a native executable. You can delete hello.c and ./hello still runs, because it's already machine code.

Bytecode: peeking inside Python

import dis

def add(a, b):
    return a + b

dis.dis(add)
# Output (exact opcodes vary by Python version), roughly:
#   LOAD_FAST    a
#   LOAD_FAST    b
#   BINARY_OP    +
#   RETURN_VALUE

CPython compiles your function to bytecode, and the dis module shows it. The Python virtual machine then executes these instructions one by one.

Interpreted + JIT: JavaScript

function square(n) {
  return n * n;
}

let total = 0;
for (let i = 0; i < 1_000_000; i++) {
  total += square(i); // "hot" code: V8 will JIT-compile square()
}
console.log(total);

V8 starts by interpreting bytecode. After square has run many times with numbers, an optimizing compiler turns it into fast machine code specialized for numbers.

How it works

The CPU repeats one cycle billions of times per second:

1. Fetch the next instruction from memory (its address is held in a register called the program counter). 2. Decode it: work out what operation it is and which data it needs. 3. Execute it: do the arithmetic, compare, read/write memory, or jump to a different instruction (that's how if and loops work underneath).

Registers are a few tiny, extremely fast storage slots inside the CPU. RAM holds the program's instructions and data; it's much larger but slower. Caches sit in between to keep recently used data close.

From source to running program, a typical toolchain does:

  • Lexing: split text into tokens (total, =, 0).
  • Parsing: arrange tokens into a tree describing the program's structure.
  • Checking: catch errors like undefined names (some languages also check types here).
  • Generating code: emit machine code or bytecode.

The operating system loads the executable into memory, gives it a process, and starts the CPU at its entry point.

┌──────────► machine code ─────────────► CPU   (C, Rust, Go)
                      │  compiler (ahead of time)
  source code ────────┤
                      │  compile          VM interprets
                      └──────────► bytecode ─────┬───────────► CPU   (Python, Java, JS)
                                                 │ JIT: hot code
                                                 └─► machine code ─► CPU

  CPU loop:  fetch ──► decode ──► execute ──► (next instruction) ──► fetch …

Why does it exist?

Humans think in names, expressions and structure; CPUs execute numbered instructions. Translation bridges the gap. The different strategies exist because of trade-offs: ahead-of-time compiling gives fast startup and fast code but slower edit-run cycles and per-platform builds; interpreters and VMs give portability and quick iteration; JITs try to get the best of both at the cost of complexity and warm-up time.

When to use it

Knowing how your code runs helps when you:

- Pick a language for a job (a tiny fast CLI vs a quick script). - Understand error types: compile-time errors are caught before running, runtime errors only when that line actually executes. - Reason about performance: why a hot loop gets faster after warm-up, or why startup time differs between languages. - Ship software: a compiled binary must be built for each OS/CPU; a script needs the right interpreter installed.

When not to use it

Don't let this drive premature decisions. For most beginner and business programs, the clarity of your code matters far more than whether the language is compiled or interpreted. Reach for low-level details only when measurements show they matter.

Common mistakes

  • Saying "Python is interpreted, so it isn't compiled". CPython compiles source to bytecode first (you can see .pyc files in __pycache__).

  • Thinking a language is inherently compiled or interpreted. Those are properties of an implementation; C interpreters and Python compilers both exist.

  • Assuming code that "compiled fine" is correct. Compiling only proves the syntax (and maybe types) are valid; logic errors still show up at runtime.

  • Expecting a compiled binary to run on any machine. It targets a specific CPU architecture and operating system.

  • Benchmarking JIT-compiled code without warming up, then concluding the language is slow.

Practice exercises

  1. Easy:

    Write a Python function, run dis.dis on it, and explain in plain words what each bytecode instruction does.

  2. Medium:

    Write a program with a deliberate error inside a function that is never called. Run it in Python and compile it in C. Explain why one reports the error and the other may not.

  3. Hard:

    Time a loop that calls a small function 10 million times in JavaScript (Node.js). Measure the first 1,000 calls separately from the rest and explain the difference in terms of the JIT.

Interview questions

What is the difference between a compiler and an interpreter?

A compiler translates the whole program into another form (usually machine code) before it runs, producing an artifact you execute later. An interpreter executes the program directly, statement by statement or bytecode by bytecode, without producing a standalone executable.

What is bytecode?

A compact, low-level instruction set designed for a virtual machine rather than a real CPU. It's portable across hardware: the same Java .class file runs on any machine with a JVM.

What is a JIT compiler and why use one?

A just-in-time compiler turns frequently executed code into machine code while the program runs. It can use runtime information (such as the actual types seen) to optimize, combining an interpreter's flexibility with near-native speed for hot code.

Is Python compiled or interpreted?

Both, in CPython. Source is compiled to bytecode, which the CPython virtual machine interprets. Other implementations differ; PyPy, for example, adds a JIT.

Describe the fetch-decode-execute cycle.

The CPU fetches the instruction at the address in the program counter, decodes it to determine the operation and operands, executes it (arithmetic, memory access or a jump), updates the program counter, and repeats.

What is the difference between a compile-time error and a runtime error?

A compile-time error is detected while translating the code, before it runs (syntax errors, type errors in statically typed languages). A runtime error happens while the program executes, such as dividing by zero or reading a missing file.

What are registers, cache and RAM?

Registers are a handful of tiny storage slots inside the CPU, the fastest memory. Caches are small, fast memory near the CPU holding recently used data. RAM is the large main memory holding the running program's code and data, much slower than cache.

Why does a compiled C program need to be rebuilt for a different platform?

Machine code is specific to a CPU architecture (x86-64, ARM64) and the executable format and system calls are specific to an operating system. A binary built for one won't run natively on another.

What does "warm-up" mean for a JIT-compiled program?

The early phase where code runs in a slower interpreter while the VM profiles it. Once code is identified as hot and compiled, it runs much faster. Benchmarks should exclude or account for warm-up.

Scenario: a teammate asks why a typo in a rarely used Python branch only crashed in production. What do you explain?

Python resolves names when each line executes, so a misspelled variable in a branch that never ran in testing isn't detected until it runs. Tests that exercise every branch, or a linter/type checker, catch these earlier.