In computer science, the term threaded code refers to a compiler implementation technique where the generated code has a form that essentially consists entirely of calls to subroutines. The code may be processed by an interpreter, or may simply be a sequence of machine code call instructions.
One of the main advantages of threaded code is that it is very compact, compared to code generated by alternative code generation techniques and alternative calling conventions. This advantage usually comes at the expense of slightly slower execution speed (usually only one machine instruction). However, sometimes there is a synergistic effect: sometimes more compact code is smaller and significantly faster than non-threaded code. A program small enough to fit fully in a computer processor's cache may run faster than a larger program that suffers many cache misses.
Threaded code is most well known as the implementation technique commonly used in some programming languages, such as Forth, many implementations of BASIC, some implementations of COBOL, early versions of B, and other languages for small minicomputers and satellites.
The common way to make computer programs is to 'translate' a computer program written in some symbolic language to machine code using a compiler. The code is typically fast but nonportable since the binary code is designed for a specific hardware platform. A different approach uses a virtual machine instruction set - that has no particular target hardware. An interpreter executes it on each new target hardware.
Full article ▸