AQA 7517, section 4.6.3.1
A processor can only run machine code, so programs written in any other language must be translated first. A program translator does this.
Source code and object code
- Source code is the program as the programmer wrote it, in a high-level language or assembly language. People can read it.
- Object code (executable code) is the machine code that results from translating the source code. The processor can run it directly.
Types of translator
Assembler
An assembler translates assembly language into machine code. Since there's a one-to-one relationship between the two, this is simple: each mnemonic is replaced by its binary opcode, and each label by an address.
Compiler
A compiler translates the whole of a high-level program into machine code in one go, before the program runs. The result is a separate executable file.
If there are errors in the source code, the compiler reports them, and no executable is produced.
Interpreter
An interpreter translates and runs the program one statement at a time, as it goes. No separate executable file is produced.
If there is an error, the interpreter stops at that line, after having run everything before it.
Compilation compared with interpretation
| Compiler | Interpreter | |
|---|---|---|
| Translates | The whole program first | One statement at a time while running |
| Output | An executable file | No file: runs directly |
| Execution speed | Fast: already machine code | Slower: translation happens every time |
| Starting the program | After the (maybe long) compile | Immediately |
| Error reporting | All errors listed before running | Stops at the first error found while running |
| Source code needed to run? | No: only the executable is shared | Yes: the source (and interpreter) must be present |
| Protecting your code | Better: users get machine code that is hard to read | Poor: the source is shared |
| Portability | The executable only runs on one type of processor | The same source runs wherever an interpreter exists |
| Testing and debugging | Slower: recompile after each change | Quicker: change and re-run at once |
When is each appropriate?
Compiler is best for:
- finished software that is distributed to users, such as commercial applications and games, where speed matters and the source code should be kept private;
- programs that will be run many times, because they are translated once.
Interpreter is best for:
- learning and development, where the programmer wants to test small changes quickly and see errors straight away;
- scripts and small programs;
- running the same program on different types of computer, since only the interpreter needs to be written for each.
Bytecode
Some compilers don't produce machine code for a particular processor. Instead, they produce an intermediate language called bytecode. Java and Python (as .pyc files) work like this.
Bytecode is a simple, compact set of instructions for an imaginary machine. A program called a virtual machine (VM) (such as the Java Virtual Machine) then interprets the bytecode, or translates it to machine code as it runs (just-in-time compilation).
Why produce bytecode?
- Portability. The same bytecode runs on any computer that has a suitable virtual machine, whatever the processor. The programmer compiles once, and the program runs "anywhere". Only the VM needs to be written for each platform.
- Faster than interpreting the source code, because the bytecode is already partly translated and simple, and can be optimised.
- Smaller than the source code.
- Some protection for the source code, since the users get bytecode.
- Security. The VM can check the bytecode before it runs, and restrict what it can do.
Worked example
Task: a company has written a game and wants to sell it. Should they use a compiler or an interpreter? What about a teacher who wants students to test small programs quickly?
- The game company: a compiler. The game runs faster as machine code, and customers receive only the executable, so the source code stays secret.
- The teacher and students: an interpreter. They can change a line and run it at once, and an error is shown at the point where it happens, which is ideal for learning.
Exam tip: contrast compiler (whole program first, executable produced, faster to run) with interpreter (statement by statement, no executable, quicker to test). When asked to describe where each is appropriate, link it to a situation, and for bytecode, give portability as the main reason.
Check yourself
What is the difference between source code and object code?
Source code is the program written by the programmer in a high-level or assembly language. Object code is the machine code produced by translating the source code, which the processor can run.
Give two differences between a compiler and an interpreter
Any two of: a compiler translates the whole program first but an interpreter does it statement by statement; a compiler produces an executable file but an interpreter doesn't; compiled code runs faster; an interpreter stops at the first error as it runs; the source code is not needed to run compiled code.
Why do some compilers produce bytecode?
Bytecode is portable: the same bytecode can run on any computer that has a virtual machine, so the program doesn't have to be recompiled for each type of processor.
Which translator is used for assembly language?
An assembler.