= *--sp addend2 = *--sp *sp++ = addend1 + addend2 ret Token threading Token-threaded code implements the thread as a list of indices into a table of operations; the index width is naturally chosen to be as small as possible for density and efficiency. 1 byte / 8-bits is the natural choice for ease of programming, but smaller sizes like 4-bits, or larger like 12 or 16 bits, can be used depending on the number of operations supported. As long as the index width is chosen to be narrower than a machine pointer, it will naturally be more compact than the other threading types without much special effort by the programmer. It is usually half to three-fourths the size of other threadings, which are themselves a quarter to an eighth the size of non-threaded code. The table's pointers can either be indirect or direct. Some Forth compilers produce token-threaded code. Some programmers consider the "p-code" generated by some Pascal compilers, as well as the bytecodes used by .NET, Java, BASIC and some C compilers, to be token-threading. A common approach, historically, is bytecode, which typically uses 8-bit opcodes with a stack-based virtual machine. The archetypal bytecode interpreter is known as a "decode and dispatch interpreter" and follows the form: start: vpc = &thread dispatch: addr = decode(&vpc) // Convert the next bytecode operation to a pointer to machine code that implements it // Any inter-instruction operations are performed here (e.g. updating global state, event processing, etc) jump addr CODE_PTR decode(BYTE_CODE **p) { // In a more complex encoding, there may be multiple tables to choose between or control/mode flags return table[*(*p)++]; } thread: /* Contains bytecode, not machine addresses. Hence it is more compact. */ 1 /*pushA*/ 2 /*pushB*/ 0 /*add*/ table: &add /* table[0] = address of machine code that implements bytecode 0 */ &pushA /* table[1] ... */ &pushB /* table[2] ... */ pushA: *sp++ = A jump dispatch pushB: *sp++ = B jump dispatch add: addend1 = *--sp addend2 = *--sp *sp++ = addend1 + addend2 jump dispatch If the virtual machine uses only byte-size instructions, decode() is simply a fetch from thread, but often there are commonly used 1-byte instructions plus some less-common multibyte instructions (see complex instruction set computer), in which case decode() is more complex. The decoding of single byte opcodes can be very simply and efficiently handled by a branch table using the