Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Threaded code
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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, the following pseudocode uses this technique to add two numbers A and B. In the example, the list is labeled '''thread''' and a variable '''ip''' (Instruction Pointer) tracks our place within the list. Another variable '''sp''' (Stack Pointer) contains an address elsewhere in memory that is available to hold a value temporarily. <syntaxhighlight lang="c"> start: ip = &thread // points to the address '&pushA', not the textual label 'thread' top: jump *ip++ // follow ip to address in thread, follow that address to subroutine, advance ip thread: &pushA &pushB &add ... pushA: *sp++ = A // follow sp to available memory, store A there, advance sp to next jump top pushB: *sp++ = B jump top add: addend1 = *--sp // Pop the top value off the stack addend2 = *--sp // Pop second value off the stack *sp++ = addend1 + addend2 // Add the two values together and store the result on the top of the stack jump top </syntaxhighlight> <!--In this case, decoding the [[bytecode]]s 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 <code>thread</code> for <code>&pushA</code>, <code>&pushB</code>, etc., are two or more bytes, compared to (typically) one byte, 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 calling loop at <code>top</code> is so simple that it can be repeated inline at the end of each subroutine. Control now jumps once, from the end of a subroutine to the start of another, instead of jumping twice via <code>top</code>. For example: <syntaxhighlight lang="c"> start: ip = &thread // ip points to &pushA (which points to the first instruction of pushA) jump *ip++ // send control to first instruction of pushA and advance ip to &pushB thread: &pushA &pushB &add ... pushA: *sp++ = A // follow sp to available memory, store A there, advance sp to next jump *ip++ // send control where ip says to (i.e. to pushB) and advance ip pushB: *sp++ = B jump *ip++ add: addend1 = *--sp // Pop the top value off the stack addend2 = *--sp // Pop second value off the stack *sp++ = addend1 + addend2 // Add the two values together and store the result on top of the stack jump *ip++ </syntaxhighlight> This is called '''direct threaded code''' (DTC). Although the technique is older, the first widely circulated use of the term "threaded code" is probably James R. Bell's 1973 article "Threaded Code".<ref>{{cite journal|last=Bell|first=James R.|title=Threaded code|journal=Communications of the ACM|year=1973|volume=16|issue=6|pages=370β372|doi=10.1145/362248.362270|s2cid=19042952 |doi-access=free}}</ref> In 1970, [[Charles H. Moore]] invented a more compact arrangement, '''indirect threaded code''' (ITC), for his Forth virtual machine. Moore arrived at this arrangement because [[Data General Nova|Nova]] minicomputers had an [[indirection bit]] in every address, which made ITC easy and fast. Later, he said that he found it so convenient that he propagated it into all later Forth designs.<ref>Moore, Charles H., published remarks in Byte Magazine's Forth Issue</ref> Today, some Forth compilers generate direct-threaded code while others generate indirect-threaded code. The executables act the same either way.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)