27.01.2015 Views

FAST Forth Native-Language Embedded Computers

FAST Forth Native-Language Embedded Computers

FAST Forth Native-Language Embedded Computers

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure Five. Examples of rectifying the stack state and Voffs value for various control-structure jumps. I<br />

{ssX, VoffsX) = stack state #X;<br />

Voffs value vX<br />

I -LBL-nnn:<br />

To implement the combining of machine instructions,<br />

the compiler looks for sequences of simple <strong>Forth</strong> words<br />

that could potentially generate combinable instructions. In<br />

practice, an array is used to retain up to some maximum<br />

number of entries that contain tokens for words that have<br />

been recently parsed off during compilation, but for which<br />

no machine code has yet been generated. When the array<br />

is full, a pattern-matching scan is started on the array,<br />

beginning with the first entry. A set of code-generating<br />

patterns is matched, one pattern at a time, against the<br />

array's contents. Longer patterns are scanned before<br />

shorter ones, but the matched pattern must always begin<br />

with the first entry in the array. Since patterns are defined<br />

for all possible single tokens, at least one token in the array<br />

is guaranteed to be matched.<br />

When a matching pattern is found in the array, the<br />

-~- -<br />

In order to avoid the overhead of actually moving array entries down after<br />

earlier entrtes have been removed, thearray should, in practice, beimplemented<br />

as acircular queue. The length of the array should be at least equal to the length<br />

of the longest code-generating pattern. However, the use of "tokens" permits<br />

the individual entries in both the array and the patterns to be reduced down to<br />

one or two bytes apiece: it also speeds up the process of matching patterns to<br />

the array's contents.<br />

In the case of literal or constant values (including the starting addresses of<br />

arrays), the associated values may be kept in a separate, parallel array, where<br />

each entry is 32 bits (four bytes). For certain commonly used constants, such<br />

as 0, 1,2, and -1, special "tokens" may be used that permit generation of code<br />

that can be optimized for the presence of such constants.<br />

I matched entries are removed from the array, and the<br />

remaining entries are moved down to fill in the vacated<br />

entries.' (If computations involving literals or constants<br />

are involved, the removed entries may instead be replaced<br />

by a smaller number of entries.) The matched pattern is<br />

associated with the code to be generated for that pattern.<br />

Since at least one entry will now be vacant at the end of<br />

the array, the compiler can then continue to parse off at<br />

least one additional word. Note that pattern-matching on<br />

the array is performed only when it is full, or when the<br />

compiler must force the entire array to be compiled into<br />

code and then emptied. The latter situation may occur<br />

when a non-simple word (such as another compiled<br />

word) is called from the currently compiled word, when<br />

an exit is encountered, or when a branch target is<br />

reached. Figure Six illustrates an example of the patternmatching<br />

process for a simple (if contrived) setup.8<br />

8 There are two additional areas where optimizations may have a significant<br />

impact. The first area is in replacing multiplications by "small" constants with<br />

sequences of shifts, additions, and/or subtractions. The second area involves<br />

the multiplication of a loop index by a constant value; the multiplication of the<br />

index can be replaced by maintenance of a "shadow" index in parallel with the<br />

"regular" loop index. (Multiple "shadow" indexes may be associated with a<br />

single "regular" index.)<br />

Both of these areas of optimization are beyond the scope of this article.<br />

Interested readers are referred to the sections on code optimization in the book<br />

Principles of Compiler Design by Aho and Ullman, published by Addison-<br />

Wesley. (Computer science students may recognize this book as the infamous<br />

"dragon book" because of the cover design.)<br />

<strong>Forth</strong> Dimensions 39 March 1994 April

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!