Annex D: Portability guide
A primary goal of Forth 94 was to enable a programmer to write Forth
programs that work on a wide variety of machines, Forth-2012
continues this practice. This goal is accomplished by allowing some
key Forth terms to be implementation defined (e.g., cell size) and
by providing Forth operators (words) that conceal the implementation.
This allows the implementor to produce the Forth system that most
effectively uses the native hardware. The machine independent
operators, together with some programmer discipline, support program
It can be difficult for someone familiar with only one machine
architecture to imagine the problems caused by transporting programs
between dissimilar machines.
This Annex provides guidelines for writing portable Forth programs.
The first section describes ways to make a program hardware independent.
The second section describes assumptions about Forth implementations
that many programmers make, but can't be relied upon in a portable program.
D.2 Hardware peculiarities
D.2.1 Data/memory abstraction
This standard gives definitions for data and memory that
apply to a wide variety of computers. These definitions give us a way
to talk about the common elements of data and memory while ignoring
the details of specific hardware. Similarly, Forth programs that
use data and memory in ways that conform to these definitions can
also ignore hardware details. The following sections discuss the
definitions and describe how to write programs that are independent
of the data and memory peculiarities of different computers.
Three terms defined by this standard are address unit, cell, and
The address space of a Forth system is divided into an array of
address units; an address unit is the smallest collection of bits that
can be addressed. In other words, an address unit is the number of
bits spanned by the addresses addr
The most prevalent machines use 8-bit address units, but other
address unit sizes exist.
In this standard, the size of a cell is an implementation-defined
number of address units. Forth implemented on a 16-bit microprocessor
could use a 16-bit cell and an implementation on a 32-bit machine
could use a 32-bit cell. Less common cell sizes (e.g., 18-bit or
36-bit machines, etc.) could implement Forth systems with their native
cell sizes. In all of these systems, Forth words such as DUP
do the same things (duplicate the top cell on the stack
and store the second cell into the address given by the first cell,
Similarly, the definition of a character has been generalized to be
an implementation-defined number of address units (but at least eight
bits). This removes the need for a Forth implementor to provide 8-bit
characters on processors where it is inappropriate. For example, on
an 18-bit machine with a 9-bit address unit, a 9-bit character would
be most convenient. Since, by definition, you can't address anything
smaller than an address unit, a character must be at least as big as
an address unit. This will result in big characters on machines with
large address units. An example is a 16-bit cell addressed machine
where a 16-bit character makes the most sense.
D.2.3 Addressing memory
One of the most common portability problems is the addressing of
successive cells in memory. Given the memory address of a cell, how
do you find the address of the next cell?
On a byte-addressed machine
with 32-bit cells the code to find the next cell would be
The code would be 1+
on a cell-addressed processor and
on a bit-addressed processor with 16-bit cells.
This standard provides a
next-cell operator named CELL+
that can be used in all of these cases.
Given an address, CELL+
adjusts the address by the size of a cell
(measured in address units).
A related problem is that of addressing an array of cells in an
arbitrary order. This standard provides a portable scaling operator named CELLS
Given a number n
returns the number of address
units needed to hold n
cells. Using CELLS
, we can make
a portable definition of an
There are also portability problems with addressing arrays of
In a byte-addressed machine, the size of a character equals the
size of an address unit. Addresses of successive characters
in memory can be found using 1+
and scaling indices into a character
array is a no-op (i.e.,
). However, there could be
implementations where a character is larger than an address unit.
operators, analogous to
are available to allow maximum portability.
This standard generalizes the definition of some Forth words that operate
on regions of memory to use address units. One example is
. By prefixing ALLOT
with the appropriate scaling operator
, etc.), space for any desired data structure can
be allocated (see definition of array above). For example:
D.2.4 Alignment problems
Some processors have restrictions on the addresses that can be used by
memory access instructions. This standard does not require an
implementor of a Forth to make alignment transparent; on the
contrary, it requires (in Section 22.214.171.124 Address alignment
a standard Forth program assume that character and cell alignment may be
One pitfall caused by alignment restrictions
is in creating tables containing both characters and cells. When
(comma) or C,
is used to initialize a table, data
are stored at the data-space pointer. Consequently, it must be
suitably aligned. For example, a non-portable table definition
On a machine that restricts memory fetches to aligned addresses,
would leave the data space pointer at an aligned address.
However, the first C,
would leave the data space pointer at an
unaligned address, and the subsequent ,
(comma) would violate
the alignment restriction by storing
at an unaligned address.
A portable way to create the table is:
adjusts the data space pointer to the first aligned
address greater than or equal to its current address. An aligned
address is suitable for storing or fetching characters, cells, cell
pairs, or double-cell numbers.
After initializing the table, we would also like to read values from
the table. For example, assume we want to fetch the first cell,
, from the table.
address of the first thing after the character. However this may not
be the address of
since we aligned the dictionary pointer
between the C,
and the ,
. The portable way to get the
adjusts the address on top of the stack to the first
aligned address greater than or equal to its current value.
D.3 Number representation
D.3.1 Big endian vs. little endian
The constituent bits of a number in memory are kept in different
orders on different machines. Some machines place the most-significant
part of a number at an address in memory with less-significant parts
following it at higher addresses; this is known as big-endian
ording. Other machines do the opposite; the
least-significant part is stored at the lowest address (little-endian
For example, the following code for a 16-bit little endian Forth
would produce the answer 1:
The same code on a 16-bit big-endian Forth would produce the
answer 0. A portable program cannot exploit the representation
of a number in memory.
A related issue is the representation of cell pairs and double-cell
numbers in memory. When a cell pair is moved from the stack to memory
, the cell that was on top of the stack is placed at the
lower memory address. It is useful and reasonable to manipulate the
individual cells when they are in memory.
D.3.2 ALU organization
Different computers use different bit patterns to represent integers.
Possibilities include binary representations (two's complement, one's
complement, sign magnitude, etc.) and decimal representations (BCD,
etc.). Each of these formats creates advantages and disadvantages in
the design of a computer's arithmetic logic unit (ALU). The most
commonly used representation, two's complement, is popular because of
the simplicity of its addition and subtraction operations.
Programmers who have grown up on two's complement machines tend to
become intimate with their representation of numbers and take some
properties of that representation for granted. For example, a trick
to find the remainder of a number divided by a power of two is to mask
off some bits with AND
. A common application of this trick is
to test a number for oddness using 1 AND
. However, this will
not work on a one's complement machine if the number is negative (a
portable technique is 2 MOD
The remainder of this section is a (non-exhaustive) list of things to
watch for when portability between machines with binary representations
other than two's complement is desired.
To convert a single-cell number to a double-cell number, Forth 94
provided the operator S>D
. To convert a double-cell number to
single-cell, Forth programmers have traditionally used DROP
However, this trick doesn't work on sign-magnitude machines. For
portability a D>S
operator is available. Converting an
unsigned single-cell number to a double-cell number can be done portably
by pushing a zero on the stack.
D.4 Forth system implementation
During Forth's history, an amazing variety of implementation techniques
have been developed. The ANS Forth Standard encourages this diversity
and consequently restricts the assumptions a user can make about the
underlying implementation of an ANS Forth system. Users of a particular
Forth implementation frequently become accustomed to aspects of the
implementation and assume they are common to all Forths. This section
points out many of these incorrect assumptions.
Traditionally, Forth definitions have consisted of the name of the
Forth word, a dictionary search link, data describing how to execute
the definition, and parameters describing the definition itself. These
components have historically been referred to as the name, link, code,
and parameter fields.
No method for accessing these fields has been found that works
across all of the Forth implementations currently in use. Therefore,
a portable Forth program may not use the name, link, or code field
in any way. Use of the parameter field (renamed to data field for
clarity) is limited to the operations described below.
Only words defined with CREATE
or with other defining words
that call CREATE
have data fields. The other defining words
in the standard (VARIABLE
might not be implemented with CREATE
. Consequently, a Standard
Program must assume that words defined by VARIABLE
, etc., may have no data fields. There is no
portable way for a Standard Program to modify the value of a constant or to
"patch" a colon definition at run time.
part of a defining word operates on a data field,
may only be used on words ultimately defined by CREATE
In standard Forth, FIND
(tick) return an
unspecified entity called an execution token. There are only a
few things that may be done with an execution token. The token may be
passed to EXECUTE
to execute the word ticked or compiled into
the current definition with COMPILE,
. The token can also be
stored in a variable or other data structure and used later.
Finally, if the word ticked was defined via CREATE
converts the execution token into the word's data-field address.
An execution token cannot be assumed to be an address and may not
be used as one.
In some Forth implementations, it is possible to find the address of
a stack in memory and manipulate the stack as an array of cells. This
technique is not portable. On some systems, especially
Forth-in-hardware systems, the stacks might be in memory
that can't be addressed by the program or might not be in memory at
all. Forth's parameter and return stacks must be treated as stacks.
A Standard Program may use the return stack directly only for
temporarily storing values. Every value examined or removed from the
return stack using R@
, or 2R>
must have been
put on the stack explicitly using >R
. Even this
must be done carefully because the system may use the return stack to
hold return addresses and loop-control parameters. Section
126.96.36.199 Return stack
of the standard has a
list of restrictions.
The Forth Standard does not force anyone to write
a portable program. In situations where performance is paramount,
the programmer is encouraged to use every trick available. On the
other hand, if portability to a wide variety of systems is needed(or anticipated), this standard provides the tools to accomplish this. There
might be no such thing as a completely portable program. A programmer, using
this guide, should intelligently weigh the tradeoffs of providing
portability to specific machines. For example, machines that use
sign-magnitude numbers are rare and probably don't deserve much
thought. But, systems with different cell sizes will certainly be
encountered and should be provided for. In general, making a program
portable clarifies both the programmer's thinking process and the