Threaded Code - Development

Development

To save space, programmers squeezed the lists of subroutine calls into simple lists of subroutine addresses, and used a small loop to call each subroutine in turn. For example:

start: ip = &thread top: jump *ip++ thread: &pushA &pushB &add ... pushA: *sp++ = A jump top pushB: *sp++ = B jump top add: *sp++ = *--sp + *--sp jump top

In this case, decoding the bytecodes is performed once, during program compilation or program load, so it is not repeated each time an instruction is executed. This can save much time and space when decode and dispatch overhead is large compared to the execution cost.

Note, however, addresses in thread for &pushA, &pushB, etc., are two or more bytes, compared to one byte, typically, for the decode and dispatch interpreter described above. In general, instructions for a decode and dispatch interpreter may be any size. For example, a decode and dispatch interpreter to simulate an Intel Pentium decodes instructions that range from 1 to 16 bytes. However, bytecoded systems typically choose 1-byte codes for the most-common operations. Thus, the thread often has a higher space cost than bytecodes. In most uses, the reduction in decode cost outweighs the increase in space cost.

Note also that while bytecodes are nominally machine-independent, the format and value of the pointers in threads generally depend on the target machine which is executing the interpreter. Thus, an interpreter might load a portable bytecode program, decode the bytecodes to generate platform-dependent threaded code, then execute threaded code without further reference to the bytecodes.

The loop is simple, so is duplicated in each handler, removing jump top from the list of machine instructions needed to execute each interpreter instruction. For example:

start: ip = &thread jump *ip++ thread: &pushA &pushB &add ... pushA: *sp++ = A jump *ip++ pushB: *sp++ = B jump *ip++ add: *sp++ = *--sp + *--sp jump *ip++

This is called direct threaded code (DTC). Although the technique is older, the first widely circulated use of the term "threaded code" is probably Bell's article "Threaded Code" from 1973.

Charles H. Moore invented a more compact notation in 1970 for his Forth virtual machine: indirect threaded code (ITC). Originally, Moore invented this because it was easy and fast on Nova minicomputers, which have an indirection bit in every address. He said (in published remarks, Byte Magazine's Forth Issue) that he found it so convenient that he propagated it into all later Forth designs.

Some Forth compilers compile Forth programs into direct-threaded code, while others make indirect-threaded code. The programs act the same either way.

Read more about this topic:  Threaded Code

Famous quotes containing the word development:

    John B. Watson, the most influential child-rearing expert [of the 1920s], warned that doting mothers could retard the development of children,... Demonstrations of affection were therefore limited. “If you must, kiss them once on the forehead when they say goodnight. Shake hands with them in the morning.”
    Sylvia Ann Hewitt (20th century)

    I have an intense personal interest in making the use of American capital in the development of China an instrument for the promotion of the welfare of China, and an increase in her material prosperity without entanglements or creating embarrassment affecting the growth of her independent political power, and the preservation of her territorial integrity.
    William Howard Taft (1857–1930)

    Somehow we have been taught to believe that the experiences of girls and women are not important in the study and understanding of human behavior. If we know men, then we know all of humankind. These prevalent cultural attitudes totally deny the uniqueness of the female experience, limiting the development of girls and women and depriving a needy world of the gifts, talents, and resources our daughters have to offer.
    Jeanne Elium (20th century)