Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


The Commando Forth Compiler

An Example Program

Listing One shows a simple game written using the Forth compiler. The program is essentially a binary search algorithm and guesses your number. The program itself is unremarkable but demonstrates many of the 1Forth features such as variables (both initialized and uninitialized), the monitor.4th words and basic math.

If you are familiar with Forth, Listing One should look almost normal to you. Only the strings look different. The compiled code (Listing Two) shows many of the optimizations employed.

For example, under the _J1 label is the line where the following Forth code appears:

 1  .disp

The resulting assembly language is:


Note, too, that the _L5 label (which is the "=" definition falls through to _L10, the "not" definition. That's because the equal word is:

: = - not ;

In other word, subtract the two values on the top of the stack and then do a logical not on the result. You can also find a hidden return at the end of iwin (_L12) where a jump to register 60 causes that library routine to return from iwin instead of to iwin.

Another example of programming the One-Der CPU using all of the tools available for download here is presented in this video.

Lessons Learned

While I really wanted a C compiler, the Forth compiler is very usable and was much easier to build. While awk is an unusual choice for a serious language translator, the fact that awk and Forth both share the concept of a word made it an obvious choice for this project.

The way strings are handled is, granted, non-standard. But the syntax is highly usable and it doesn't take much to get used to the new format. Sometimes a small concession can pay big dividends in the ease of implementation.

Forth isn't much in vogue these days with its lack of type safety and other modern features (for example, while there are some Forth packages to address object oriented techniques, none of them are widely accepted as a standard). However, for an embedded system Forth brings a lot to the table. Its simplicity allows highly efficient code and its tight integration to assembly (especially in this implementation) makes it a great fit for embedded systems. The fact that the implementation was very simple is an extra bonus.

The compiler itself should be reasonably straightforward to port to other CPUs. There are just a few machine-specific parts to the compiler. Most of the porting would be in the 1forth.lib file.

Although Forth is satisfying to use (and it is particularly satisfying to write programs on a CPU you designed using a compiler, assembler, monitor, and debugger that you created), it isn't the best answer for a CPU like One-Der. As I mentioned earlier, the true value to the One-Der's architecture is its bus-like structure is a natural for reconfigurable computing. Ideally, a compiler would decide the best instruction set for the program it was compiling and emit a CPU definition as well as object code. While I suppose it would be possible for the Forth compiler to note the operations used in a program and select functional units, it seems as though a higher level abstraction would have more opportunity to exploit this. Forth is fairly close to the machine architecture, so what you write is likely to map to the machine's instruction set anyway.

For an example of a C compiler that works with this type of CPU, look at TCE). This project from the Tampere University of Technology in Finland and allows you to graphically build TTA CPUs with multiple bus flows and then compile C code for the new processor. The tool outputs a VHDL CPU description (unlike One-Der, which is written in Verilog).

Even then, to get the maximum benefit out of a compiler-generated CPU, I expect the language will have to be very high level. The compiler can then exploit the description of what you want to do and use its own heuristics to determine how to do it.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.