Overview
I use an indirect-threaded Forth for my lowest level projects, when I need precise control over each byte emitted.
I have two variants in use. One runs on my Linux desktop. The other is runs baremetal off a hard disk on an old desktop I have. I'll be talking about the latter since its the simpler of the two. I am not happy with how the desktop version has developed, as the latest version was an experiment combining a lot of ideas I had, including Tail Call Optimization by default.
Cryospark
This is a 16 bit Forth, with store/fetch words for either 16 or 8 bit values.
There is only 1 Code Field Address, begin. I discovered variables and constants were trivially implementable as normal words.
Numbers are always parsed and printed in hexadecimal form. I wrote a lot of machine code instructions, so not having to translate them was a bonus I wanted by default. I never needed the decimal words enough to justify including them in the interpreter.
Core Vocabulary
These are usually implemented as machine-coded primitives.
begin
This is the only Code Field Address in the system. It begins all high-level words by saving the return stack before executing the next token.
end
This is used to end high-level words. It restores the caller and resumes executing it.
jump
This restores the caller before executing the next token. The instruction sequence [ [ jump a ] b c ]
behaves as though 'a' were called by the calling word like in: [ a b c ].
This is useful for early returns using 'end'. For example: [ [ jump end ] b c ]
will not execute 'b' or 'c'.
Many of the conditional words are built on this.
do
( address - )
Calls the address on the data stack.
drop dup swp
These have not changed from other Forths. Drop removes a number from the data stack, dup duplicates a number, and swp swaps the top two numbers.
r> >r
These move addresses to/from the return stack and the data stack.
If the > points to the r, an address moves to the return stack.
If the > points away from the r, an address moves from the return stack.
branch
( number - )
Adds the number on the data stack to the program counter. Can move both forwards and backwards.
!+
( number address - address+size )
! (pronounced "store") stores a 2 byte number to the address specified and adds 2 to the address.
@+
( address - address+size number )
@ (pronounced "fetch") fetches a 2 byte number from the address specified and adds 2 to the address.
.!+ .@+
Like the non-dotted versions, except these store/fetch a single byte and add 1 to the address.
! .!
( number address - )
Versions of store that do not keep the address around.
@ .@
( address - number )
Versions of fetch that do not keep the address around.
~~
( a - b )
b is equal to bitwise not a.
+ - * / % << >> && || ^^ = > <
( a b - c )
c is equal to "a op b", where op is either
- addition (+)
- subtraction (-)
- multiplication (*)
- division (/)
- modulo (%)
- bitshift left (<<)
- bitshift right (>>)
- bitwise and (&&)
- bitwise or (||)
- bitwise xor (^^)
- equality comparison (=)
- less than comparison (<)
- greater than comparison (>)
For bitshifts a is bitshifted by b bits.
For comparisons, c is either 0 for false or 1 for true.
= > <
( a b - c )
Compiler Vocabulary
def
Adds the next word to the dictionary.
:
calls def to add a definition and then the compiler.
; (immediate)
It compiles an end before "early ending" the compiler.
c
c is a variable containing the next unused address in memory.
There are a number of helper words for this:
- >c compiles a 2-byte number.
- .>c compiles a 1-byte number.
- c> returns the last compiled 2-byte number and decrements c.
- c< when executed, compiles the next token and points the program counter past it.
- c+ and c- adds/subtracts a number to/from c, respectively.
literals
: literal r> @+ >r ;
During execution, this pushes the next token to the data stack and points the program counter past it. Numbers are always compiled with this preceding it.
Extended Vocabulary
1+ 1- 2* 4* 16* 2/
: 1+ 1 + ;
: 1- 1 - ;
: 2* 1 << ;
: 4* 2 << ;
: 16* 4 << ;
: 2/ 1 >> ;
Common arithmetic words.
0=
: 0= 0 = ; This could be machine coded and probably will be in the future.
cells blocks
: cells 2* ;
: blocks A << ;
This is useful for some machine independence.
0branch
: 0branch 0= r> @+ >r * jump branch ;
: 0branch> dup jump 0branch ;
This word has the most inconsistent definition across each implementation. I can't decide whether I prefer it to keep its condition or not using a dup at the beginning. So I'm adding the second definition 0branch> for consistency with the following conditionals naming scheme.
unless when and or then
: unless 0= cells branch jump end ;
: unless> dup jump unless ;
: when 0= jump unless ;
: when> dup jump when ;
I like Scheme's conditionals unless and when.
unless the condition is true, continue this word.
when the condition is true, continue this word.
The > at the end of the name indicates whether the condition is kept on the stack or not.
and or then
: and unless 0 jump end ;
: or 0branch 4 jump end jump drop ;
: then unless> drop jump end ;
Same as Scheme's conditionals and and or.
If condition is 0:
- and ends the current word and keeps the condition on the stack.
- or drops it and continues the current word.
- then drops it and ends the current word.
If condition is not 0:
- and drops it and continues the current word.
- or ends the current word and keeps the condition on the stack.
- then continues the current word and keeps the condition on the stack.
then is very useful in loops: : times then 1- something jump times ; (of course making sure
something leaves the condition at the top of the stack).
cell :cell :array
: cell r> ;
: :cell def c< cell 0 >c ;
: loop then 0 .>c 1- jump loop ;
: :array def c< cell loop ;
Both are zero-initialized.
Combined with words like cells and blocks, this can lead to very readable source:
2 blocks :array buffer
:cell active
: switch active @ 1+ 1 && active ! ;
const :const
: const r> @ ;
: :const def c< const >c ;
Used for naming numbers: 41 :const letter_A
^
: ^ r> r> swp >r >r ;
^ is pronounced "defer", and is often used with variables that have a word applied to their contents, saving the result.
Here is an example implementation of the compiler variable using it:
: ^c c @ ^ c ! ;
: >c ^c !+ ;
: .>c ^c .!+ ;
How it works is by freezing the caller and swapping it with its caller before returning.
Given the instruction sequence [ [ ^ c ! ] !+ ], after ^ is executed
it looks like [ [ !+ ] c ! ]. !+ is executed before the deferred c !+.