figForth is written in itself !!
Some parts of this page/site are currently incomplete & will be updated asap
Other parts will change continually so use “Refresh” in your browser !!
There is extensive use of “Tooltips” text to support learning which do not seem to render on a Smartphone. This site is best viewed via a computer’s HD monitor
There is already a computer science definition of “reflective” but this definition is not at all how Forth’s reflection works…
Forth is reflective in the sense that it is written in Forth itself with a small number of machine code primitives called “Code Definitions” and machine-code “run-time” words so that it can run on any real microprocessor
Forth seems to be unique among ALL computer languages in respect of this, and in order to explain it, the Forth Word QUERY will used as an example to discuss…
QUERY
Input 80 characters of text (or until a "return") from the operators terminal
Text is placed at the address contained in TIB with IN set to zero
So QUERY is Forth’s way of getting text input (normally from the keyboard) into the Forth System
NFA: 006D5E 85 DB 85H
006D5F 51554552 DB "QUER"
006D63 D9 DB 'Y'+80H
LFA: 006D64 D96C DW EXPEC-9
CFA: 006D66 3C28 QUERY: DW DOCOL ; :
PFA: 006D68 4868 DW TIB ; TIB
006D6A 8F27 DW AT ; @
006D6C 0621 5000 DW LIT,50H ; 80
006D70 E26C DW EXPEC ; EXPECT
006D72 BF67 DW ZERO ; 0
006D74 6368 DW INN ; IN
006D76 F027 DW STORE ; !
006D78 1725 DW SEMIS ; ;
Fig #1 The assembler source code of QUERY
The Meaning of the Dictionary Fields:
Definition: : TIB @ LIT 50H EXPECT 0 IN ! ;
It can be seen from the source code above that all QUERY is really doing is setting up a call to EXPECT and initialising TIB’s index afterwards
EXPECT is a much more complex word than QUERY but still written 100% in high-level Forth
So, it can be seen from the assembler source code above that QUERY’s CFA points towards the Colon Compiler’s run-time code-interpreter DOCOL 1
“In colon definitions, the code field contains the address of a run-time routine called DOCOL, meaning ‘DO the COLon routine’, which is the ‘address interpreter’ for colon definitions” Source: (Ting 2013, p. 26) (My Emphasis)
00283C DOCOL: Run-time routine for all colon definitions
00283C 2AFBFF LD HL,(RPP)
00283F 2B DEC HL Push the address of the next word
002840 70 LD (HL),B to the return stack
002841 2B DEC HL
002842 71 LD (HL),C to enter a lower nesting level...
002843 22FBFF LD (RPP),HL
002846 13 INC DE
002847 4B LD C,E Move the parameter field address into IP (Register pair BC)
002848 42 LD B,D and so pointing to the first word in this definition...
002849 C3 E2 20 JP NEXT The old IP was saved on the Return Stack and the new IP
is pointing to the word to be executed...
NEXT will bring about the proper actions
Fig #2 The annotated assembler source code of DOCOL Text comments based upon: (Ting 2013, pp. 26-7)
“Using the interpretive pointer IP alone would only allow the processing of a address list at a single level. The return stack is used as an extension of IP. When a colon definition calls other colon definitions, the contents of IP are saved on the return stack so that the IP can be used to call other definitions in the called colon definition. DOCOL thus provides the mechanism to nest indefinitely within colon definitions”
Source: (Ting 2013, p. 27) (My Emphasis)
“At the end of a colon definition, execution must be returned to the calling definition. The analogy of NEXT in colon definitions is a word named SEMIS which does the unnesting. SEMIS returns execution to the calling definition by unnesting one level” Based upon: (Ting 2013, p. 27) (My Emphasis & links)
002512 82 DB 82H
002513 3B DB ";"
002514 D3 DB 'S'+80H
002515 F924 DW RPSTO-6
002517 1925 SEMIS: DW $+2
002519 2AFBFF LD HL,(RPP)
00251C 4E LD C,(HL) Pop the return stack into IP (Register pair BC)
00251D 23 INC HL pointing now to the next word to be executed
00251E 46 LD B,(HL) in the calling definition...
00251F 23 INC HL
002520 22FBFF LD (RPP),HL Prepare the return stack for next step...
002523 C3 E2 20 JP NEXT So execute the word pointed to by IP via NEXT...
Fig #3 The annotated assembler source code of SEMIS Text comments based upon: (Ting 2013, p. 27)
“The interplay among the four virtual-machine registers, IP - W - RP - SP allows the colon definitions to nest and to unnest correctly to an indefinite depth, limited only by the size of the return stack allocated in the system. This process of nesting and unnesting is a major contributor to the compactness of the Forth language. The overhead of a subroutine call in Forth is only two bytes, identifying the address of the called subroutine”
Based upon: (Ting 2013, p. 27) (My Emphasis)
This is the essence of the figForth Virtual Machine…
… in operation. In the example discussed on this page, we have focussed upon the mechanism involving the operation of DOCOL and SEMIS upon the return stack, and NEXT
And In Particular…
… why the use of the code field address is so important:
- The Code Field Address ALWAYS points towards executable machine-code
- COLON, CONSTANT, VARIABLE, USER, VOCABULARY, etc, all compile “run-time” words
- DOCOL, DOCON, DOVAR, DOUSE, DOVOC, etc, are all “run-time” words containing machine-code
Not forgetting…
… the vital role of the Primatives (“code definitions”), which work with the “run time” machine code, without either of which a Forth System could not be run on a real microprocessor…
Finally, to make it clear…
Earlier, we quoted (Ting 2013, p. 27) saying: “The interplay among the four virtual-machine registers, IP - W - RP - SP allows the colon definitions to nest and to unnest correctly to an indefinite depth…“ by using the Return Stack as it was intended, via the “run-time” code parts of the various compilers…
But mainly and fundamentally via the Colon Compiler that uses (;CODE) to create the “run-time” machine code… Discussion
References:
Ting, C. H., 2013. Systems Guide to fig-Forth [online]. 3rd ed. San Mateo, CA 94402, USA: Offete Enterprises, Inc. Available from: http://figforth.org.uk/library/Systems.Guide.to.figForth.pdf.
So DOCOL works with the Return Stack and NEXT, the address interpreter (threader) ↩