2 \ A sometimes minimal FORTH compiler and tutorial for Linux / i386 systems. -*- asm -*-
3 \ By Richard W.M. Jones <rich@annexia.org> http://annexia.org/forth
4 \ This is PUBLIC DOMAIN (see public domain release statement below).
5 \ $Id: jonesforth.f,v 1.3 2007-09-25 09:50:54 rich Exp $
7 \ The first part of this tutorial is in jonesforth.S. Get if from http://annexia.org/forth
9 \ PUBLIC DOMAIN ----------------------------------------------------------------------
11 \ I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
13 \ In case this is not legally possible, I grant any entity the right to use this work for any purpose,
14 \ without any conditions, unless such conditions are required by law.
16 \ SETTING UP ----------------------------------------------------------------------
18 \ Let's get a few housekeeping things out of the way. Firstly because I need to draw lots of
19 \ ASCII-art diagrams to explain concepts, the best way to look at this is using a window which
20 \ uses a fixed width font and is at least this wide:
22 \<------------------------------------------------------------------------------------------------------------------------>
24 \ Secondly make sure TABS are set to 8 characters. The following should be a vertical
25 \ line. If not, sort out your tabs.
31 \ Thirdly I assume that your screen is at least 50 characters high.
33 \ START OF FORTH CODE ----------------------------------------------------------------------
35 \ We've now reached the stage where the FORTH system is running and self-hosting. All further
36 \ words can be written as FORTH itself, including words like IF, THEN, .", etc which in most
37 \ languages would be considered rather fundamental.
39 \ Some notes about the code:
41 \ I use indenting to show structure. The amount of whitespace has no meaning to FORTH however
42 \ except that you must use at least one whitespace character between words, and words themselves
43 \ cannot contain whitespace.
45 \ FORTH is case-sensitive. Use capslock!
47 \ Define some character constants
51 \ CR prints a carriage return
54 \ SPACE prints a space
55 : SPACE 'SPACE' EMIT ;
57 \ DUP, DROP are defined in assembly for speed, but this is how you might define them
58 \ in FORTH. Notice use of the scratch variables _X and _Y.
59 \ : DUP _X ! _X @ _X @ ;
62 \ The built-in . (DOT) function doesn't print a space after the number (unlike the real FORTH word).
63 \ However this is very easily fixed by redefining . (DOT). Any built-in word can be redefined.
65 . \ this refers back to the previous definition (but see also RECURSE below)
69 \ The 2... versions of the standard operators work on pairs of stack entries. They're not used
70 \ very commonly so not really worth writing in assembler. Here is how they are defined in FORTH.
74 \ More standard FORTH words.
78 \ Standard words for manipulating BASE.
82 \ Standard words for booleans.
87 \ LITERAL takes whatever is on the stack and compiles LIT <foo>
90 , \ compile the literal itself (from the stack)
93 \ Now we can use [ and ] to insert literals which are calculated at compile time.
94 \ Within definitions, use [ ... ] LITERAL anywhere that '...' is a constant expression which you
95 \ would rather only compute once (at compile time, rather than calculating it each time your word runs).
97 [ \ go into immediate mode temporarily
98 CHAR : \ push the number 58 (ASCII code of colon) on the stack
99 ] \ go back to compile mode
100 LITERAL \ compile LIT 58 as the definition of ':' word
103 \ A few more character constants defined the same way as above.
104 : '(' [ CHAR ( ] LITERAL ;
105 : ')' [ CHAR ) ] LITERAL ;
106 : '"' [ CHAR " ] LITERAL ;
108 \ While compiling, '[COMPILE] word' compiles 'word' if it would otherwise be IMMEDIATE.
109 : [COMPILE] IMMEDIATE
110 WORD \ get the next word
111 FIND \ find it in the dictionary
112 >CFA \ get its codeword
116 \ So far we have defined only very simple definitions. Before we can go further, we really need to
117 \ make some control structures, like IF ... THEN and loops. Luckily we can define arbitrary control
118 \ structures directly in FORTH.
120 \ Please note that the control structures as I have defined them here will only work inside compiled
121 \ words. If you try to type in expressions using IF, etc. in immediate mode, then they won't work.
122 \ Making these work in immediate mode is left as an exercise for the reader.
124 \ condition IF true-part THEN rest
125 \ -- compiles to: --> condition 0BRANCH OFFSET true-part rest
126 \ where OFFSET is the offset of 'rest'
127 \ condition IF true-part ELSE false-part THEN
128 \ -- compiles to: --> condition 0BRANCH OFFSET true-part BRANCH OFFSET2 false-part rest
129 \ where OFFSET if the offset of false-part and OFFSET2 is the offset of rest
131 \ IF is an IMMEDIATE word which compiles 0BRANCH followed by a dummy offset, and places
132 \ the address of the 0BRANCH on the stack. Later when we see THEN, we pop that address
133 \ off the stack, calculate the offset, and back-fill the offset.
135 ' 0BRANCH , \ compile 0BRANCH
136 HERE @ \ save location of the offset on the stack
137 0 , \ compile a dummy offset
142 HERE @ SWAP - \ calculate the offset from the address saved on the stack
143 SWAP ! \ store the offset in the back-filled location
147 ' BRANCH , \ definite branch to just over the false-part
148 HERE @ \ save location of the offset on the stack
149 0 , \ compile a dummy offset
150 SWAP \ now back-fill the original (IF) offset
151 DUP \ same as for THEN word above
156 \ BEGIN loop-part condition UNTIL
157 \ -- compiles to: --> loop-part condition 0BRANCH OFFSET
158 \ where OFFSET points back to the loop-part
159 \ This is like do { loop-part } while (condition) in the C language
161 HERE @ \ save location on the stack
165 ' 0BRANCH , \ compile 0BRANCH
166 HERE @ - \ calculate the offset from the address saved on the stack
167 , \ compile the offset here
170 \ BEGIN loop-part AGAIN
171 \ -- compiles to: --> loop-part BRANCH OFFSET
172 \ where OFFSET points back to the loop-part
173 \ In other words, an infinite loop which can only be returned from with EXIT
175 ' BRANCH , \ compile BRANCH
176 HERE @ - \ calculate the offset back
177 , \ compile the offset here
180 \ BEGIN condition WHILE loop-part REPEAT
181 \ -- compiles to: --> condition 0BRANCH OFFSET2 loop-part BRANCH OFFSET
182 \ where OFFSET points back to condition (the beginning) and OFFSET2 points to after the whole piece of code
183 \ So this is like a while (condition) { loop-part } loop in the C language
185 ' 0BRANCH , \ compile 0BRANCH
186 HERE @ \ save location of the offset2 on the stack
187 0 , \ compile a dummy offset2
191 ' BRANCH , \ compile BRANCH
192 SWAP \ get the original offset (from BEGIN)
193 HERE @ - , \ and compile it after BRANCH
195 HERE @ SWAP - \ calculate the offset2
196 SWAP ! \ and back-fill it in the original location
199 \ FORTH allows ( ... ) as comments within function definitions. This works by having an IMMEDIATE
200 \ word called ( which just drops input characters until it hits the corresponding ).
202 1 \ allowed nested parens by keeping track of depth
204 KEY \ read next character
205 DUP '(' = IF \ open paren?
206 DROP \ drop the open paren
209 ')' = IF \ close paren?
213 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
214 DROP \ drop the depth counter
218 From now on we can use ( ... ) for comments.
220 In FORTH style we can also use ( ... -- ... ) to show the effects that a word has on the
221 parameter stack. For example:
223 ( n -- ) means that the word consumes an integer (n) from the parameter stack.
224 ( b a -- c ) means that the word uses two integers (a and b, where a is at the top of stack)
225 and returns a single integer (c).
226 ( -- ) means the word has no effect on the stack
229 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
232 DUP 0> ( while n > 0 )
234 SPACE ( print a space )
235 1- ( until we count down to 0 )
240 ( c a b WITHIN returns true if a <= c and c < b )
256 ( .S prints the contents of the stack. Very useful for debugging. )
258 DSP@ ( get current stack pointer )
262 DUP @ . ( print the stack element )
268 ( DEPTH returns the depth of the stack. )
271 4- ( adjust because S0 was on the stack when we pushed DSP )
275 S" string" is used in FORTH to define strings. It leaves the address of the string and
276 its length on the stack, with the address at the top. The space following S" is the normal
277 space between FORTH words and is not a part of the string.
279 This is tricky to define because it has to do different things depending on whether
280 we are compiling or in immediate mode. (Thus the word is marked IMMEDIATE so it can
281 detect this and do different things).
283 In compile mode we append
284 LITSTRING <string length> <string rounded up 4 bytes>
285 to the current word. The primitive LITSTRING does the right thing when the current
288 In immediate mode there isn't a particularly good place to put the string, but in this
289 case we put the string at HERE (but we _don't_ change HERE). This is meant as a temporary
290 location, likely to be overwritten soon after.
292 : S" IMMEDIATE ( -- len addr )
293 STATE @ IF ( compiling? )
294 ' LITSTRING , ( compile LITSTRING )
295 HERE @ ( save the address of the length word on the stack )
296 0 , ( dummy length - we don't know what it is yet )
298 KEY ( get next character of the string )
301 HERE @ !b ( store the character in the compiled image )
302 1 HERE +! ( increment HERE pointer by 1 byte )
304 DROP ( drop the double quote character at the end )
305 DUP ( get the saved address of the length word )
306 HERE @ SWAP - ( calculate the length )
307 4- ( subtract 4 (because we measured from the start of the length word) )
308 SWAP ! ( and back-fill the length location )
309 HERE @ ( round up to next multiple of 4 bytes for the remaining code )
313 ELSE ( immediate mode )
314 HERE @ ( get the start address of the temporary space )
319 OVER !b ( save next character )
320 1+ ( increment address )
322 DROP ( drop the final " character )
323 HERE @ - ( calculate the length )
324 HERE @ ( push the start address )
329 ." is the print string operator in FORTH. Example: ." Something to print"
330 The space after the operator is the ordinary space required between words and is not
331 a part of what is printed.
333 In immediate mode we just keep reading characters and printing them until we get to
334 the next double quote.
336 In compile mode we use S" to store the string, then add EMITSTRING afterwards:
337 LITSTRING <string length> <string rounded up to 4 bytes> EMITSTRING
339 It may be interesting to note the use of [COMPILE] to turn the call to the immediate
340 word S" into compilation of that word. It compiles it into the definition of .",
341 not into the definition of the word being compiled when this is running (complicated
344 : ." IMMEDIATE ( -- )
345 STATE @ IF ( compiling? )
346 [COMPILE] S" ( read the string, and compile LITSTRING, etc. )
347 ' EMITSTRING , ( compile the final EMITSTRING )
349 ( In immediate mode, just read characters and print them until we get
350 to the ending double quote. )
354 DROP ( drop the double quote character )
355 EXIT ( return from this function )
363 In FORTH, global constants and variables are defined like this:
365 10 CONSTANT TEN when TEN is executed, it leaves the integer 10 on the stack
366 VARIABLE VAR when VAR is executed, it leaves the address of VAR on the stack
368 Constants can be read by not written, eg:
372 You can read a variable (in this example called VAR) by doing:
374 VAR @ leaves the value of VAR on the stack
375 VAR @ . CR prints the value of VAR
377 and update the variable by doing:
379 20 VAR ! sets VAR to 20
381 Note that variables are uninitialised (but see VALUE later on which provides initialised
382 variables with a slightly simpler syntax).
384 How can we define the words CONSTANT and VARIABLE?
386 The trick is to define a new word for the variable itself (eg. if the variable was called
387 'VAR' then we would define a new word called VAR). This is easy to do because we exposed
388 dictionary entry creation through the CREATE word (part of the definition of : above).
389 A call to CREATE TEN leaves the dictionary entry:
394 +---------+---+---+---+---+
395 | LINK | 3 | T | E | N |
396 +---------+---+---+---+---+
399 For CONSTANT we can continue by appending DOCOL (the codeword), then LIT followed by
400 the constant itself and then EXIT, forming a little word definition that returns the
403 +---------+---+---+---+---+------------+------------+------------+------------+
404 | LINK | 3 | T | E | N | DOCOL | LIT | 10 | EXIT |
405 +---------+---+---+---+---+------------+------------+------------+------------+
408 Notice that this word definition is exactly the same as you would have got if you had
412 CREATE ( make the dictionary entry (the name follows CONSTANT) )
413 DOCOL , ( append DOCOL (the codeword field of this word) )
414 ' LIT , ( append the codeword LIT )
415 , ( append the value on the top of the stack )
416 ' EXIT , ( append the codeword EXIT )
420 VARIABLE is a little bit harder because we need somewhere to put the variable. There is
421 nothing particularly special about the 'user definitions area' (the area of memory pointed
422 to by HERE where we have previously just stored new word definitions). We can slice off
423 bits of this memory area to store anything we want, so one possible definition of
424 VARIABLE might create this:
426 +--------------------------------------------------------------+
429 +---------+---------+---+---+---+---+------------+------------+---|--------+------------+
430 | <var> | LINK | 3 | V | A | R | DOCOL | LIT | <addr var> | EXIT |
431 +---------+---------+---+---+---+---+------------+------------+------------+------------+
434 where <var> is the place to store the variable, and <addr var> points back to it.
436 To make this more general let's define a couple of words which we can use to allocate
437 arbitrary memory from the user definitions area.
439 First ALLOT, where n ALLOT allocates n bytes of memory. (Note when calling this that
440 it's a very good idea to make sure that n is a multiple of 4, or at least that next time
441 a word is compiled that HERE has been left as a multiple of 4).
443 : ALLOT ( n -- addr )
444 HERE @ SWAP ( here n )
445 HERE +! ( adds n to HERE, after this the old value of HERE is still on the stack )
449 Second, CELLS. In FORTH the phrase 'n CELLS ALLOT' means allocate n integers of whatever size
450 is the natural size for integers on this machine architecture. On this 32 bit machine therefore
451 CELLS just multiplies the top of stack by 4.
453 : CELLS ( n -- n ) 4 * ;
456 So now we can define VARIABLE easily in much the same way as CONSTANT above. Refer to the
457 diagram above to see what the word that this creates will look like.
460 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
461 CREATE ( make the dictionary entry (the name follows VARIABLE) )
462 DOCOL , ( append DOCOL (the codeword field of this word) )
463 ' LIT , ( append the codeword LIT )
464 , ( append the pointer to the new memory )
465 ' EXIT , ( append the codeword EXIT )
469 VALUEs are like VARIABLEs but with a simpler syntax. You would generally use them when you
470 want a variable which is read often, and written infrequently.
472 20 VALUE VAL creates VAL with initial value 20
473 VAL pushes the value directly on the stack
474 30 TO VAL updates VAL, setting it to 30
476 Notice that 'VAL' on its own doesn't return the address of the value, but the value itself,
477 making values simpler and more obvious to use than variables (no indirection through '@').
478 The price is a more complicated implementation, although despite the complexity there is no
479 performance penalty at runtime.
481 A naive implementation of 'TO' would be quite slow, involving a dictionary search each time.
482 But because this is FORTH we have complete control of the compiler so we can compile TO more
483 efficiently, turning:
487 and calculating <addr> (the address of the value) at compile time.
489 Now this is the clever bit. We'll compile our value like this:
491 +---------+---+---+---+---+------------+------------+------------+------------+
492 | LINK | 3 | V | A | L | DOCOL | LIT | <value> | EXIT |
493 +---------+---+---+---+---+------------+------------+------------+------------+
496 where <value> is the actual value itself. Note that when VAL executes, it will push the
497 value on the stack, which is what we want.
499 But what will TO use for the address <addr>? Why of course a pointer to that <value>:
501 code compiled - - - - --+------------+------------+------------+-- - - - -
502 by TO VAL | LIT | <addr> | ! |
503 - - - - --+------------+-----|------+------------+-- - - - -
506 +---------+---+---+---+---+------------+------------+------------+------------+
507 | LINK | 3 | V | A | L | DOCOL | LIT | <value> | EXIT |
508 +---------+---+---+---+---+------------+------------+------------+------------+
511 In other words, this is a kind of self-modifying code.
513 (Note to the people who want to modify this FORTH to add inlining: values defined this
514 way cannot be inlined).
517 CREATE ( make the dictionary entry (the name follows VALUE) )
518 DOCOL , ( append DOCOL )
519 ' LIT , ( append the codeword LIT )
520 , ( append the initial value )
521 ' EXIT , ( append the codeword EXIT )
524 : TO IMMEDIATE ( n -- )
525 WORD ( get the name of the value )
526 FIND ( look it up in the dictionary )
527 >DFA ( get a pointer to the first data field (the 'LIT') )
528 4+ ( increment to point at the value )
529 STATE @ IF ( compiling? )
530 ' LIT , ( compile LIT )
531 , ( compile the address of the value )
533 ELSE ( immediate mode )
534 ! ( update it straightaway )
538 ( x +TO VAL adds x to VAL )
540 WORD ( get the name of the value )
541 FIND ( look it up in the dictionary )
542 >DFA ( get a pointer to the first data field (the 'LIT') )
543 4+ ( increment to point at the value )
544 STATE @ IF ( compiling? )
545 ' LIT , ( compile LIT )
546 , ( compile the address of the value )
547 ' +! , ( compile +! )
548 ELSE ( immediate mode )
549 +! ( update it straightaway )
554 ID. takes an address of a dictionary entry and prints the word's name.
556 For example: LATEST @ ID. would print the name of the last word that was defined.
559 4+ ( skip over the link pointer )
560 DUP @b ( get the flags/length byte )
561 F_LENMASK AND ( mask out the flags - just want the length )
564 DUP 0> ( length > 0? )
566 SWAP 1+ ( addr len -- len addr+1 )
567 DUP @b ( len addr -- len addr char | get the next character)
568 EMIT ( len addr char -- len addr | and print it)
569 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
571 2DROP ( len addr -- )
575 'WORD word FIND ?HIDDEN' returns true if 'word' is flagged as hidden.
577 'WORD word FIND ?IMMEDIATE' returns true if 'word' is flagged as immediate.
580 4+ ( skip over the link pointer )
581 @b ( get the flags/length byte )
582 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
585 4+ ( skip over the link pointer )
586 @b ( get the flags/length byte )
587 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
591 WORDS prints all the words defined in the dictionary, starting with the word defined most recently.
592 However it doesn't print hidden words.
594 The implementation simply iterates backwards from LATEST using the link pointers.
597 LATEST @ ( start at LATEST dictionary entry )
599 DUP 0<> ( while link pointer is not null )
601 DUP ?HIDDEN NOT IF ( ignore hidden words )
602 DUP ID. ( but if not hidden, print the word )
605 @ ( dereference the link pointer - go to previous word )
612 So far we have only allocated words and memory. FORTH provides a rather primitive method
615 'FORGET word' deletes the definition of 'word' from the dictionary and everything defined
616 after it, including any variables and other memory allocated after.
618 The implementation is very simple - we look up the word (which returns the dictionary entry
619 address). Then we set HERE to point to that address, so in effect all future allocations
620 and definitions will overwrite memory starting at the word. We also need to set LATEST to
621 point to the previous word.
623 Note that you cannot FORGET built-in words (well, you can try but it will probably cause
626 XXX: Because we wrote VARIABLE to store the variable in memory allocated before the word,
627 in the current implementation VARIABLE FOO FORGET FOO will leak 1 cell of memory.
630 WORD FIND ( find the word, gets the dictionary entry address )
631 DUP @ LATEST ! ( set LATEST to point to the previous word )
632 HERE ! ( and store HERE with the dictionary address )
636 RECURSE makes a recursive call to the current word that is being compiled.
638 Normally while a word is being compiled, it is marked HIDDEN so that references to the
639 same word within are calls to the previous definition of the word. However we still have
640 access to the word which we are currently compiling through the LATEST pointer so we
641 can use that to compile a recursive call.
644 LATEST @ >CFA ( LATEST points to the word being compiled at the moment )
649 DUMP is used to dump out the contents of memory, in the 'traditional' hexdump format.
651 : DUMP ( addr len -- )
652 BASE @ ROT ( save the current BASE at the bottom of the stack )
653 HEX ( and switch the hexadecimal mode )
656 DUP 0> ( while len > 0 )
658 OVER . ( print the address )
661 ( print up to 16 words on this line )
662 2DUP ( addr len addr len )
663 1- 15 AND 1+ ( addr len addr linelen )
665 DUP 0> ( while linelen > 0 )
667 SWAP ( addr len linelen addr )
668 DUP @b ( addr len linelen addr byte )
669 . SPACE ( print the byte )
670 1+ SWAP 1- ( addr len linelen addr -- addr len addr+1 linelen-1 )
674 ( print the ASCII equivalents )
675 2DUP 1- 15 AND 1+ ( addr len addr linelen )
677 DUP 0> ( while linelen > 0)
679 SWAP ( addr len linelen addr )
680 DUP @b ( addr len linelen addr byte )
681 DUP 32 128 WITHIN IF ( 32 <= c < 128? )
684 DROP [ CHAR ? ] LITERAL EMIT
686 1+ SWAP 1- ( addr len linelen addr -- addr len addr+1 linelen-1 )
691 DUP 1- 15 AND 1+ ( addr len linelen )
692 DUP ( addr len linelen linelen )
693 ROT ( addr linelen len linelen )
694 - ( addr linelen len-linelen )
695 ROT ( len-linelen addr linelen )
696 + ( len-linelen addr+linelen )
697 SWAP ( addr-linelen len-linelen )
700 2DROP ( restore stack )
701 BASE ! ( restore saved BASE )
704 ( Finally print the welcome prompt. )
705 ." JONESFORTH VERSION " VERSION . CR