The Forth Compilers

  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


Previous Page: figForth’s Virtual Machine (“VM”)

Compilation (carried out by compilers) is fundamental to the operation of Forth and of Forth programs

What is the difference between Compiling & Interpreting?

What is a Compiler?

Knuth defines a compiler as: “a program that translates computer languages…“ (Knuth 1997, p. 632)

https://en.wikipedia.org/wiki/Compiler “a compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name “compiler” is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g. assembly language, object code, or machine code) to create an executable program”
Source:   Wikipedia

Generally, compilation is a translation process based upon a set of rules. Compilation in Forth will always insert some kind of code into the dictionary and consequently increment a User Variable called the Dictionary Pointer (“DP”)

High-Level Defining Words (Compilers)

As already discussed, the code field contains the address of machine code to be executed when the word is invoked, thereby determining the behaviour of the word, and interprets the data stored in the parameter field appropriately. (Ting 2013, p. 71)

In figForth, only a small number of these defining words are supplied as standard (described in the next few sections). However, it is possible to define new defining words

Simple Compilation with , (“comma”)

The Forth word , (“comma”) is the simplest compiler which takes a value from the stack and encloses it into the dictionary

The Forth glossary definition for , (“comma”)
Image: (Husband 2011) from the figForth Glossary

High-Level Defining Words (Compilers)

“The following words are powerful FORTH words which cause new words to be compiled to the top of the figForth dictionary” 1

[     ---    End the compilation mode. The text from the input stream is subsequently executed
                        Pronounced: left-bracket

]     ---    Set the compilation mode. The text from the input stream is subsequently compiled
                        Pronounced: right-bracket

[COMPILE] ---   Used in a colon-definition to force compilation of the following word
                This allows compilation of an IMMEDIATE word when it would otherwise be executed
                        Pronounced: bracket-compile

CREATE    ---   A defining word used to create a dictionary entry for <name>, without allocating
                any parameter field memory. When <name> is subsequently executed, the address of
                the first byte of <name>'s parameter field is left on the stack

                Form: CREATE <name>

CODE      ---   CODE creates a dictionary header for a code definition. The code field contains its
                parameter field address. Assembly codes are to be compiled (assembled) into the
                parameter field

    : CODE      CREATE          Create the header, nothing more to be done on the header
                [COMPILE]
                ASSEMBLER       Select ASSEMBLER vocabulary as the CONTEXT vocabulary, which has all
                                the assembly mnemonics and words pertaining to assembly processes
                ;               Stop compiling                                Source: Ting, 2013 p29

CONSTANT

VARIABLE

USER

:         ---   A defining word which selects the CONTEXT vocabulary to be identical to CURRENT
                Create a dictionary entry for <name> in CURRENT, and set compile mode. Words thus
                defined are called 'colon-definitions'
                The compilation addresses of subsequent words from the input stream which are not
                immediate words are stored into the dictionary to be executed when <name> is later
                executed. IMMEDIATE words are executed as encountered. If a word is not found after
                a search of the CONTEXT and FORTH vocabularies, conversion compilation of a Literal
                number is attempted, with regard to the current BASE; that failing, an error condition
                exists

                Form: : <name> ... ;               Pronounced: colon

;         ---   Terminate a colon-definition and stop compilation
                                                   Pronounced: semi-colon
Table[1]: Forth defining words

While the Forth computer is running its “Outer Interpreter” word QUIT, it is doing one of two things: executing or interpreting words with the address interpreter (“Inner Interpreter”), or parsing and compiling the input texts from the terminal or storage. These are the two ‘states’ of the Forth computer when it is executing. Internally, the Forth system uses a user variable STATE to remind itself what kind of job it is supposed to be doing. If the contents of STATE is zero, the system is in the executing state, and if the contents of STATE is not zero, it is in the compiling state   Based upon (Ting 2013, p. 28)

Two words are provided for the expert user to switch explicitly between the executing state and the compiling state. They are ”[“, leftbracket, and ”]”, right-bracket

Compile-Time Behaviour

Run-Time Behaviour

Compiling Control Words

The Inner Compilers

The Colon Compiler is the “Central Dogma” of Forth

The Colon Compiler is the Central Dogma of Forth…

The Colon Compiler in more detail…

The Semi-Colon Compiler

This word compiles the Semicolon Interpreter SEMIS then terminates the compilation of a colon definition

The Constant Compiler

The Variable Compiler

The User Variable Compiler

The Text Compiler

Source & Generated Code Co-Relations

IMPORTANT: Note from the diagrams in Fig 170 below (and elsewhere) that there is a direct co-relation between Forth words and the resultant compiled code. This is a very powerful and useful property which I am going to make good use of when I extend the Forth compiler(s) to create/regenerate objects across connected Forth systems via the Object Request Broker…

ALSO NOTE: All addresses used are absolute…

Summary

We refer to the “Forth Compiler” but as we see above, there are a number of compilers to cater for different data scenarios, plus the abilty to create NEW COMPILERS as required to solve any particular problem…

Image[1]: (Brodie 1981, p. 312)

The “Roll Your Own” Compiler

<BUILDS & DOES>

CODE & ;CODE

Next Page: figForth’s Interpreters

References:

Brodie, L., 1981. Starting FORTH : an introduction to the FORTH language and operating system for beginners and professionals [online]. Englewood Cliffs, N.J: Prentice-Hall. Available from: https://www.forth.com/starting-forth/.

Husband, D., 2011. M.Sc in IT (Software Engineering). Master’s thesis. University of Liverpool.

Knuth, D. E., 1997. The Art of Computer Programming - Vol 1 - Fundamental Algorithms - 3rd Ed.

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.

  1. They are compiling words in figForth - they create dictionary words containing tokens… 


Updated: 26th March 2022 by David Husband
© 2021 David Husband, a.k.a. Baremetal Engineer Extraordinaire
All Rights Reserved – All Trademarks & Copyrights Acknowledged
All personal information is subject to the Data Protection Act 2018 & the UK GDPR
“ad auxilium aliis ad auxilium sibi”