Compiler
A compiler translates the whole of a high-level program (e.g. C or C++) into machine code before it runs. The result is an executable file that runs on its own, without the compiler or the source code.
Key uses
- 3D games and other performance-critical software, where speed matters most
- Operating systems, which must run quickly and efficiently
- Apps distributed as executables, so users never need the source code
Features of Compilers
A compiler translates a program in one go, before any of it runs.
- Whole program at once: all of the source code is translated into machine code in a single process.
- Errors reported at the end: problems are listed after the whole program has been checked, and it will not run until they are fixed.
- Executable output: it produces a standalone file for one type of processor and operating system.
Compilation Process
- Source code: High-level program written by the programmer (C, C++, etc.).
-
Preprocessing: Handles directives (
#include,#define), removes comments, and expands macros. -
Compilation (Analysis & Optimisation):
- Performs lexical, syntax, and semantic analysis.
- Generates intermediate code.
- Performs code optimisation for efficiency.
- Assembly code generation: Converts the optimised intermediate code into assembly language, which is human-readable low-level instructions for the target processor.
- Assembly (Object Code generation): The assembler translates the assembly code into object code (binary machine code) that can be linked.
- Linking: Combines object files with libraries and resolves symbols to produce the final executable.
- Executable creation: The final output is a runnable program.
What is the primary purpose of the preprocessing step in compiling high-level source code?
During the compilation process, what does semantic analysis primarily check for?
What is the role of linking in the process of creating an executable?
Advantages of Compilers
Compiling takes time up front, but the finished program has clear benefits for the people who use it.
- Fast to run: the program is already machine code, and the compiler can optimise it.
- No translator needed: users run the executable without installing a compiler.
- Source code stays private: only the executable is shared, so the code is hard to copy or change.
What is a key advantage of using a compiler over an interpreter?
Why are compiled programs generally faster than interpreted ones?
Disadvantages of Compilers
The main drawbacks of compiling show up while the program is being written, tested and shared.
- Slower development: the whole program must be recompiled after every change before it can be tested.
- Harder debugging: errors are only reported after the whole program is compiled, not as each line runs.
- Platform-specific: an executable only runs on one type of processor and operating system, so it must be recompiled for others.
Just In Time Compilation
JIT compilation translates bytecode into native machine code at runtime, only for parts of the program that are frequently executed (“hot” code).
Typical pipeline
Source code > Bytecode > Interpreter runs initially > Hot code detected > JIT compiles to machine code > Native execution (much faster)
Example languages / runtimes
- Java (HotSpot JVM)
- C# (.NET CLR)
- JavaScript (V8, SpiderMonkey)
- PyPy (Python with JIT)
Advantages of JIT Compilation
JIT compilation aims to combine the portability of bytecode with the speed of compiled machine code.
- Near-native speed: frequently used (“hot”) code is compiled and then runs as machine code.
- Runtime optimisation: code is optimised using information about how the program is actually being used.
- Portable: the same bytecode can be compiled and run on any supported platform.
Disadvantages of JIT Compilation
JIT compilation adds extra work while the program is running, which brings some costs.
- Slower start-up: compiling at runtime adds a delay, especially when the program first starts.
- More memory: the runtime has to store both the bytecode and the compiled machine code.
- Complex runtime: a JIT system is much harder to build and test than a simple interpreter.
Complete! Ready to test your knowledge?
Compilers
- Compiler
- Features of Compilers
- Compilation Process
- Advantages of Compilers
- Disadvantages of Compilers
- Just In Time Compilation
- Advantages of JIT Compilation
- Disadvantages of JIT Compilation