Volume C1 · Basic concepts · TAOCP Section 1.4.3
Interpretive routines
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Read it next to Section 1.4.3 of the book.
Contents
Interpretive routines
Overview
A program can be a list of numbers that the machine does not run itself. A second program, the interpretive routine, reads the list one item at a time, decides what each item means, and does the work. The list is a program in the language of the interpreter. Section 1.4.3 explains why this indirection pays. It lets a long program of many similar cases become a short table, it gives the passes of a compiler a compact form in which to hand work to each other, it makes a program independent of the machine that runs it, and it gives a good place to put checks that help to find errors.
The section also names the special case that the next two sections use. When the language is the machine language of a computer, the interpreter is a simulator of that computer. The section gives no program of its own. The programs of Sections 1.4.3.1 and 1.4.3.2 are its examples. This chapter writes a small interpreter in full, so that every claim about an interpreter has a program that can be run.
The ideas
An interpreter is a loop with three parts. It takes the item that the program counter names, it jumps to the code that does what the item means, and it goes back to the loop. The program counter is a cell or a register of the interpreter, and the program that is interpreted is data. The state of the interpreted program is also data: some cells for its values and its counter.
The jump through a table is the usual way to find the code. The operation code is a small number. The interpreter jumps to the cell at the start of a table plus that number, and each cell of the table jumps to the code of one operation. The cost of finding the code is the same for every operation and does not depend on the number of operations.
The cost of an item is a fixed overhead plus the cost of its operation. The overhead is the work of fetching, splitting and dispatching. A language with operations that do much work per item makes this overhead small compared with the total.
A language that fits the problem is shorter than the code that it stands for. One item of the interpreted language can stand for a long sequence of machine instructions, and a call of a subroutine for each item would be longer than the item. The saving of space is the main reason to use an interpreter inside a larger program.
A simulator is an interpreter for a machine language. Its language is the set of instructions of a real or planned computer. Its state is the state of that computer, and its aim is to give the result that the computer would give. A simulator can also report what the real machine cannot report, such as the exact count of instructions, or can run on a computer of another kind.
Definitions, results and proofs
Definition (interpretive routine, TAOCP Section 1.4.3). A language is a set of items, each with a meaning that is a change of a state. An interpretive routine for the language is a program that has a state, a program counter, and a loop. The loop takes the item at the counter, advances the counter, and applies the change of state that the item means. If the item is a jump of the language, the change includes a new value of the counter. The loop stops when the item is a stop.
Node: 1.4.3-def-interpreter · Lean: not yet stated
Explanation. The line between an interpreter and an ordinary program is not sharp, and the definition does not try to draw it. An ordinary program that reads a table of numbers and does something for each of them is an interpreter for the language of the table. The word is used when the items are an instruction list in the sense of a program.
Example. The language of this chapter has five items. Each item is one MIX word with an operation code in byte and a number in bytes and . The state is a value , a count and the counter of the interpreted program. All three are at the start.
| Code | Name | Meaning |
|---|---|---|
| 0 | HALT | stop |
| 1 | ADD | |
| 2 | MUL | |
| 3 | DECJ | ; if then |
| 4 | SETC |
The program with the items ADD 1, SETC 4, MUL 2, DECJ 2 and HALT is at the interpreted addresses to . It sets , then it doubles four times, because DECJ goes back to the item at address while is positive after the decrement. The result is and .
| Step | Item at | before | after | after | after |
|---|---|---|---|---|---|
| 1 | ADD 1 | 0 | 1 | 0 | 1 |
| 2 | SETC 4 | 1 | 1 | 4 | 2 |
| 3 | MUL 2 | 2 | 2 | 4 | 3 |
| 4 | DECJ 2 | 3 | 2 | 3 | 2 |
| 5 | MUL 2 | 2 | 4 | 3 | 3 |
| 6 | DECJ 2 | 3 | 4 | 2 | 2 |
| 7 | MUL 2 | 2 | 8 | 2 | 3 |
| 8 | DECJ 2 | 3 | 8 | 1 | 2 |
| 9 | MUL 2 | 2 | 16 | 1 | 3 |
| 10 | DECJ 2 | 3 | 16 | 0 | 4 |
| 11 | HALT | 4 | 16 | 0 | 4 |
Proof that the program gives . After step 2, and . Let be the number of times that the pair MUL and DECJ has run. The claim is that after pairs, and , and that the counter is at the address when . For this is the state after step 2. A pair doubles and decreases by . The jump back is taken when the new is positive, which is when , so the counter is again. For the counter goes to , where the item is HALT. So the program stops with and after pairs.
Definition (simulator, TAOCP Section 1.4.3). A simulator is an interpretive routine whose language is the machine language of a computer. Its state holds the registers, the memory and the counter of that computer, and one pass of its loop changes this state in the way that one instruction of the computer would.
Node: 1.4.3-def-simulator · Lean: not yet stated
Explanation. Two other uses of the word exist, and the section keeps them apart. A simulator of a discrete system models a physical or social system by a program that keeps a list of future events. Section 2.2.5 of the book treats such systems. In this chapter, simulator always means a simulator of a computer. The interpreter above is a simulator of a machine with the three registers , and and of no memory except the program.
Example. Take the state and one pass of the loop. The item at address is DECJ 2. The pass computes , which is positive, so . The state is now . This is the row of step 4 in the table above, with the first column showing the state just after the step.
Proof that the simulator is faithful. Let be the starting state and let be the function that gives the state after one instruction of the machine. The claim is that after passes of the loop, the state of the simulator is . For it is . If it holds for , the next pass takes the item at the counter of and applies its meaning, which is by the definition of the loop, so the claim holds for .
Theorem (the cost of dispatch, TAOCP Section 1.4.3). Let an interpreter have a loop that fetches an item, splits it into fields, advances the counter and jumps through a table of jumps. Then the work of the loop is the same for every item, and the time of an item is the time of this loop plus the time of its operation. Because the table has one cell for each code, the loop does not get longer when the language gets more operations.
Node: 1.4.3-thm-dispatch · Lean: not yet stated
Explanation. The theorem is the reason that a table of jumps is used. A chain of comparisons would take more time for the later operations, and the time for an operation would depend on its place in the chain.
Example. The interpreter of the next program has a loop of u: LDA, STA, LD2 and LD3 take u each, and INC6, the jump through the table and the jump in the table take u each. The operations take these times in addition: ADD u, MUL u, SETC u, DECJ u when the jump is taken and u when it is not, and HALT u. So ADD takes u, MUL takes u, SETC takes u, DECJ takes u or u, and HALT takes u.
Proof. The lines of the loop before the jump through TABLE,3 are the same for every item. In the interpreter below they are LDA, STA, LD2, LD3 and INC6, which take u. The jump through TABLE,3 takes u and reaches the cell of the table with the number that was loaded in rI3. This cell holds one jump to the routine of the operation, which takes u. So the work before the routine is u for every code, and the time of an item is u plus the body of its routine. The table has one cell for each code, and the jump through it takes one step whatever the number of codes.
Algorithms and programs
Program (an interpreter for the five items, TAOCP Section 1.4.3). The program has the interpreted program in the words to , the value in the cell and the count in the cell . The interpreted counter is in rI6. The loop is at CYCLE. It loads the item at , stores a copy in INST, loads the number in rI2 and the code in rI3, adds to rI6 and jumps through TABLE,3. The table has the five jumps to the five routines.
Node: 1.4.3-prog-tiny · Lean: not yet stated
The routines are short, and the whole text is this.
CYCLE LDA PROG,6
STA INST
LD2 INST(0:2)
LD3 INST(5:5)
INC6 1
JMP TABLE,3
TABLE JMP HALT
JMP ADD
JMP MUL
JMP DECJ
JMP SETC
ADD LDA V
ADD INST(0:2)
STA V
JMP CYCLE
MUL LDA V
MUL INST(0:2)
STX V
JMP CYCLE
DECJ LDA C
DECA 1
STA C
JANP CYCLE
ENT6 0,2
JMP CYCLE
SETC ST2 C
JMP CYCLE
HALT HLT
Explanation. The multiplication leaves the low half of the product in rX, so MUL stores rX. This is correct only while the product has at most bytes, which holds for this language when stays small. The routine DECJ stays at the next item when is not positive after the decrement, and otherwise it puts the number of rI2 in rI6.
Example. The program was run in a simulator of MIX with the five items above. It took u, and the state at each pass of the loop is the state of the table above. The times at which the loop was entered, counting from the start, were and , and HALT ran at and ended at .
Proof of correctness. The proof is by induction on the passes of the loop. The claim is that at the entrance to CYCLE, rI6 , and the cells and hold and of the interpreted machine. At the start, ENT6 gives , and the cells are zero. In a pass, the code of the item is and the number is . The jump through TABLE,3 goes to the -th cell of the table, which jumps to the routine of the item, because the table lists the routines in the order of the codes. The routine ADD adds to the cell , and MUL multiplies. SETC stores in . DECJ subtracts and either keeps rI6, which is from INC6, or sets it to , according as the new is not positive or positive. This is the meaning in the table of items, so the claim holds after the pass. HALT stops the machine.
Time. Let the program be run with items of each kind: of ADD, of MUL, of SETC, of DECJ with a taken jump, of DECJ without a jump, and one HALT. The time is For the program above , , , , , and the time is u, the value of the run.
The overhead. The same computation as plain MIX code is a loop of five instructions: LDA V, MUL =2=, STX V, DEC1 1 and J1P back. One pass of it takes u, and the whole program, with the three setup lines and HLT, takes u in the same simulator. The interpreter spends u for the same pass, which is times as much, and u for the whole run. The table of costs shows where the extra time goes: u for each item for the dispatch. This is the price of the compact form. When the operations of the language do more work, as the multiplication does, the share of the overhead is smaller.
The exercises
The section has no exercises of its own. Exercises 1.4.3.1 and 1.4.3.2 carry the practice of interpretive routines.
Lean
The Lean code of tamnd/taocp-lean at main has the model of MIX in TAOCP/MIX/ and the program logic in TAOCP/MIX/Logic/. It has no file for Section 1.4.3. A simulator in the sense of the definition is a function on the state of a machine, and the faithfulness theorem is a statement about the iterate of such a function. The Lean model has a step function for MIX itself, but no interpreter for a second language. The Node lines of this section therefore name no declaration.
Notes and history
The first interpreters were built for languages that looked like the instruction sets of simple machines, and that were easier to write than the machine languages of the time. Symbolic assembly languages and then compilers removed that use, and interpreters stayed in other roles: as the form in which a compiler hands its output to a later pass, as a compact encoding of a large number of similar cases, and as a layer that hides the differences between computers. A language designed for such a role is often called a virtual machine.
A compiler for ALGOL 60 that translates into an interpretive language, and the interpreter for that language, are described by Randell and Russell in ALGOL 60 Implementation (1964). Section 1.4.3.1 of the book gives the simulator of MIX, and Section 1.4.3.2 the trace routine.
Section 1.4.3 treats a program that does not compute a result of its own. It reads a second program, in a language made for the purpose, and does what that program says. The section explains why a machine-like language is a good way to store a long list of cases and a good way to pass work from one pass to the next, and it separates the interpreter of a language from the simulator of a machine. This chapter gives both definitions, a complete interpreter in MIX for a small language of four operations, with a trace, a proof that it is correct and an exact count of its time, and it shows what the interpretive overhead is for each interpreted instruction.
The source of this page. Something is wrong? Report it.