taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

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
  1. Interpretive routines

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 55 and a number aa in bytes 11 and 22. The state is a value VV, a count CC and the counter PP of the interpreted program. All three are 00 at the start.

Code Name Meaning
0 HALT stop
1 ADD aa V←V+aV \leftarrow V + a
2 MUL aa V←V⋅aV \leftarrow V \cdot a
3 DECJ aa C←C−1C \leftarrow C - 1; if C>0C > 0 then P←aP \leftarrow a
4 SETC aa C←aC \leftarrow a

The program with the items ADD 1, SETC 4, MUL 2, DECJ 2 and HALT is at the interpreted addresses 00 to 44. It sets V=1V = 1, then it doubles VV four times, because DECJ goes back to the item at address 22 while CC is positive after the decrement. The result is V=16V = 16 and C=0C = 0.

Step Item at PP PP before VV after CC after PP 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 V=16V = 16. After step 2, V=1V = 1 and C=4C = 4. Let jj be the number of times that the pair MUL and DECJ has run. The claim is that after jj pairs, V=2jV = 2^j and C=4−jC = 4 - j, and that the counter is at the address 22 when j<4j < 4. For j=0j = 0 this is the state after step 2. A pair doubles VV and decreases CC by 11. The jump back is taken when the new CC is positive, which is when j+1<4j + 1 < 4, so the counter is 22 again. For j+1=4j + 1 = 4 the counter goes to 44, where the item is HALT. So the program stops with V=24=16V = 2^4 = 16 and C=0C = 0 after 44 pairs. □\square

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 VV, CC and PP and of no memory except the program.

Example. Take the state (P,V,C)=(3,2,4)(P, V, C) = (3, 2, 4) and one pass of the loop. The item at address 33 is DECJ 2. The pass computes C=3C = 3, which is positive, so P←2P \leftarrow 2. The state is now (2,2,3)(2, 2, 3). 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 s0s_0 be the starting state and let ff be the function that gives the state after one instruction of the machine. The claim is that after kk passes of the loop, the state of the simulator is fk(s0)f^k(s_0). For k=0k = 0 it is s0s_0. If it holds for kk, the next pass takes the item at the counter of fk(s0)f^k(s_0) and applies its meaning, which is ff by the definition of the loop, so the claim holds for k+1k + 1. □\square

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 1111 u: LDA, STA, LD2 and LD3 take 22 u each, and INC6, the jump through the table and the jump in the table take 11 u each. The operations take these times in addition: ADD 77 u, MUL 1515 u, SETC 33 u, DECJ 88 u when the jump is taken and 66 u when it is not, and HALT 11 u. So ADD takes 1818 u, MUL takes 2626 u, SETC takes 1414 u, DECJ takes 1919 u or 1717 u, and HALT takes 1212 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 2+2+2+2+1=92+2+2+2+1 = 9 u. The jump through TABLE,3 takes 11 u and reaches the cell of the table with the number cc that was loaded in rI3. This cell holds one jump to the routine of the operation, which takes 11 u. So the work before the routine is 1111 u for every code, and the time of an item is 1111 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. □\square

Algorithms and programs

Program (an interpreter for the five items, TAOCP Section 1.4.3). The program has the interpreted program in the words 100100 to 104104, the value VV in the cell 9090 and the count CC in the cell 9191. The interpreted counter PP is in rI6. The loop is at CYCLE. It loads the item at PP, stores a copy in INST, loads the number aa in rI2 and the code in rI3, adds 11 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 55 bytes, which holds for this language when VV stays small. The routine DECJ stays at the next item when CC is not positive after the decrement, and otherwise it puts the number aa of rI2 in rI6.

Example. The program was run in a simulator of MIX with the five items above. It took 223223 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 1,19,33,59,78,104,123,149,168,1941, 19, 33, 59, 78, 104, 123, 149, 168, 194 and 211211, and HALT ran at 211211 and ended at 223223.

Proof of correctness. The proof is by induction on the passes of the loop. The claim is that at the entrance to CYCLE, rI6 =P= P, and the cells 9090 and 9191 hold VV and CC of the interpreted machine. At the start, ENT6 00 gives P=0P = 0, and the cells are zero. In a pass, the code of the item is cc and the number is aa. The jump through TABLE,3 goes to the cc-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 aa to the cell VV, and MUL multiplies. SETC stores aa in CC. DECJ subtracts 11 and either keeps rI6, which is P+1P + 1 from INC6, or sets it to aa, according as the new CC 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. □\square

Time. Let the program be run with nn items of each kind: nAn_A of ADD, nMn_M of MUL, nSn_S of SETC, nDn_D of DECJ with a taken jump, nEn_E of DECJ without a jump, and one HALT. The time is 1+18nA+26nM+14nS+19nD+17nE+12.1 + 18 n_A + 26 n_M + 14 n_S + 19 n_D + 17 n_E + 12. For the program above nA=1n_A = 1, nM=4n_M = 4, nS=1n_S = 1, nD=3n_D = 3, nE=1n_E = 1, and the time is 1+18+104+14+57+17+12=2231 + 18 + 104 + 14 + 57 + 17 + 12 = 223 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 2+10+2+1+1=162 + 10 + 2 + 1 + 1 = 16 u, and the whole program, with the three setup lines and HLT, takes 6969 u in the same simulator. The interpreter spends 26+19=4526 + 19 = 45 u for the same pass, which is 2.82.8 times as much, and 223223 u for the whole run. The table of costs shows where the extra time goes: 1111 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.