figForth's Internal Architecture

Note:
There is extensive use of “Tooltips” text to support learning which do not seem to render on a Smartphone
Therefore this site is best viewed via a computer’s HD monitor


Previous Section: The figForth Paradigm

Why is figForth’s Internal Architecture & Operation of any interest?

  1. The fundamentally different nature of a Forth Application where the language itself is extended to encompass the desired solution rather than as in a C application where the desired solution is “merely” expressed in terms of the existing and un-modified C language

  2. Advanced Forth programming involving embedded hardware & software, such as I am doing @ Http://baremetal.engineer, involves & requires an expert knowledge and appreciation of the Forth internals

  3. Forth is not a mainstream language and most books on the subject are long out-of-print or very obscure, and so therefore are not easily accessible; so this is to assist the reader in gaining an understanding and appreciation of the what’s, why’s and how’s of the main ideas and methods used on this website

Forth is extremely powerful and internally very neat. I have learned from my own experience that having a deeper understanding of how and why Forth does things is essential to the understanding and effective use of the Forth language (Ting 2013, p. iii)

I have very rarely had to design anything completely from scratch, usually I can use or modify some existing Forth word; and this is what I have done many times in the past… Why re-invent the wheel?

“Generally, it is impossible to study and understand a commercial Forth system. The source code is not available in most cases. Where the source code is provided, it is too complicated to be dissected and to be put back together again. For people who are curious about how Forth really ticks, figForth is the only Forth system that an average person can understand in a couple of weeks. It is also easy to port figForth to a virgin computer un-adulterated by a prior Forth implementation. We have no valid alternative in teaching and learning Forth besides this old figForth(Ting 2013, p. vi) (My Emphasis)

1. The Different Parts of figForth

1.0 The Essense of figForth

figForth is Reflective - This means it is WRITTEN IN ITSELF !!

The whole of figForth is essentially only six parts:

  1.01 System Start-Up Code

The whole Forth system is a virtual machine which needs to be “started-up” after the host microprocessor has started-up and initialised itself

A detailed discussion of the System Start-Up Code…

  1.02 Outer Interpreter

  1.03 Inner Interpreter (Address Threader)

Apart from any code definitions, figForth’s internal form is Threaded Code 1 (in the Dictionary) comprising of a list of Code Field Addresses 2 that must always point towards executable machine code

This forms a variety of threaded code known as Indirectly Threaded Code 3

A detailed discussion of the Inner Interpreters…

  1.04 Forth Virtual Machine

figForth employs a very simple “Virtual Machine”

What is a “virtual machine” (“VM”)?

It is “an abstract computing architecture or computational engine that is independent of any particular hardware or operating system…“ (Taivalsaari 2003)

A detailed discussion of Virtual Machines (in general)…

So What is the figForth “virtual machine”?

It is a “concept of operation” that involves a number of very simple “pieces” of code; machine code, and “high-level” Forth code…

The figForth Virtual Machine is part of the Inner Interpreter (Address Threader)

  1.04 figForth’s Dictionary

In a Forth computer, the dictionary is a singly-linked-list of named entries or words which are executed when called by name. The dictionary consists of procedures defined either in assembly codes (code definitions) or in high level codes (normally colon definitions)

It also contains system information as constants and variables used by the system. Inside the computer, the dictionary is maintained as a singly-linked-list, growing from low memory towards high memory as new definitions are compiled or assembled into the dictionary

When the text interpreter parses out a text string from the input stream, if it is not a new definition, the text interpreter will try to find a word in the dictionary with a name matching the string. The word found in the dictionary will be executed or compiled depending on the state of the text interpreter. The dictionary is thus the bulk of a Forth system, containing all the information necessary to make the whole system work

Based upon (Ting 2013, p. 48)

A detailed discussion of figForth’s Dictionary…

Other Important figForth Elements & Operations

    1.1 figForth’s Compilers

As explain here elsewhere, Forth relies upon the use of its compilers to create applications to solve problems. This is fundamental to the operation of Forth. A number of compilers are supplied as a part of the default language set and there is the ability to define new compilers as needed to implement solutions to problems

A detailed discussion of figForth’s Compilers

    1.2 figForth’s Interpreters

Having compilers means you must have interpreters to execute what is compiled

A detailed discussion of figForth’s Interpreters

    1.3 figForth’s Memory Map

As part of a Forth installation on a real computing system, it is neccessary to map the Forth system correctly to suit the computing platform

A detailed discussion of figForth’s Memory Map

    1.4 figForth Multitasking

Multitasking is a time multiplexing technique that makes it appear that different processes are executing simultaneously

A detailed discussion of figForth Multitasking

    1.5 figForth and the UML

“The Unified Modeling Language (UML) is a general-purpose, developmental, modeling language in the field of software engineering that is intended to provide a standard way to visualise the design of a system. The creation of UML was originally motivated by the desire to standardise the disparate notational systems and approaches to software design”   Source: Wikipedia

A detailed discussion of figForth and the UML

    1.6 New figForth Builds

The figForth models generated a new figForth system by using an assember to process an assembler source listing. There is another way of regenerating a new Forth system

A detailed discussion of New figForth Builds

    1.7 figForth’s Glossary

figForth’s expandable word set is listed and documented in the glossary

A detailed discussion of figForth’s Glossary

1.8 Parsing the Input Stream

“Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term parsing comes from Latin pars (orationis), meaning part (of speech). The term is used in the analysis of computer languages, referring to the syntactic analysis of the input code into its component parts in order to facilitate the writing of compilers and interpreters”   Source: Wikipedia

A detailed discussion of the default figForth Parser

1.9 How Forth Treats Numbers

“A number is a string which causes the Forth computer to push a piece of data onto the data stack. Characters used in a number must belong to a subset of ASCII characters - the numerals. The total number of numerals in this subset is equal to a ‘base’ value specified by the programmer. A number may have a leading ‘-‘ sign to designate data of negative value”   Source: (Ting 2013, p. 3)

A detailed discussion of how Forth treats numbers

    1.91 About Fixed Point Numbers

Fixed-Point refers to a method of representing fractional (non-integer) numbers by storing a fixed number of digits of their fractional part. Fixed-point number representation is often contrasted to the more complicated and computationally demanding floating-point representation”

“A fixed-point representation of a fractional number is essentially an integer that is to be implicitly multiplied by a fixed scaling factor. For example, the value 1.23 can be stored in a variable as the integer value 1230 with implicit scaling factor of 1/1000 (meaning that the last 3 decimal digits are implicitly assumed to be a decimal fraction), and the value 1 230 000 can be represented as 1230 with an implicit scaling factor of 1000 (with “minus 3” implied decimal fraction digits, that is, with 3 implicit zero digits at right). This representation allows standard integer arithmetic units to perform rational number calculations”

“Negative values are usually represented in binary fixed-point format as a signed integer in two’s complement representation with an implicit scaling factor as above. The sign of the value will always be indicated by the first stored bit (1 = negative, 0 = non-negative), even if the number of fraction bits is greater than or equal to the total number of bits”   Source: Wikipedia

Forth deals with all of these issues

A detailed discussion about Fixed Point Numbers

    1.92 About Reverse Polish

“Reverse Polish notation (RPN), also known as postfix notation, is a mathematical notation in which operators follow their operands”   Source: Wikipedia

A detailed discussion about Reverse Polish Notation

1.10 How figForth deals with Errors

The figForth word ERROR is invoked upon an error (“exception”) being detected. This is a complex subject !!

A detailed discussion of how figForth deals with Errors

1.11 Forth State Machines

“A finite-state machine (FSM) or finite-state automaton (FSA, plural: automata), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some inputs; the change from one state to another is called a transition. An FSM is defined by a list of its states, its initial state, and the inputs that trigger each transition”   Source: Wikipedia

“The behavior of state machines can be observed in many devices in modern society that perform a predetermined sequence of actions depending on a sequence of events with which they are presented. Simple examples are vending machines, which dispense products when the proper combination of coins is deposited; elevators, whose sequence of stops is determined by the floors requested by riders; traffic lights, which change sequence when cars are waiting; and combination locks, which require the input of a sequence of numbers in the proper order”
Source: Wikipedia

I am not sure it is possible to program without employing some kind of state machine, whether one realises it or not !! So some figForth words are state machines

A detailed discussion of Forth State Machines

1.12 Existing Object-based Forth Words

A number of figForth words are “object-based” and have been long before the “object-oriented” paradigm became “fashionable”

A detailed discussion of Forth Objects

References:

Bell, J. R., 1973. Threaded Code. Available from: Http://figforth.org.uk/library/Threaded.Code.p370-bell.pdf.

Rather, E. D., Colburn, D. R. and Moore, C. H., 1996. The Evolution of Forth. ACM [online]. Available from: Http://figforth.org.uk/library/p625-rather_The.Evolution.of.Forth.pdf.

Dewar, R. B. K., 1975. Indirect Threaded Code. Available from: Http://figforth.org.uk/library/Indirect.Threaded.Code.p330-dewar.pdf.

Ertl, A., n.d. Threaded Code. Available from: http://www.complang.tuwien.ac.at/forth/threaded-code.html.

Rodriguez, B., 1993. Moving Forth. Available from: http://www.bradrodriguez.com/papers/moving1.htm.

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.

Next Section: The figForth Glossary

  1. A classic paper on “threaded code” was (Bell 1973) followed by Dewar’s paper on “indirect threading” (Dewar 1975) 

  2. Moore’s genius lay in his invention of the indirect “code field address” (“CFA”) and the way it is used in Forth 

  3. Charles Moore’s practical work implementing Forth as indirectly threaded code in 1970 predated both Bell’s and Dewar’s publications (Rather et al. 1996) 


Updated: 29th July 2023 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”