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.1 2007-09-24 00:18:19 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 \ So far we have defined only very simple definitions. Before we can go further, we really need to
109 \ make some control structures, like IF ... THEN and loops. Luckily we can define arbitrary control
110 \ structures directly in FORTH.
112 \ Please note that the control structures as I have defined them here will only work inside compiled
113 \ words. If you try to type in expressions using IF, etc. in immediate mode, then they won't work.
114 \ Making these work in immediate mode is left as an exercise for the reader.
116 \ condition IF true-part THEN rest
117 \ -- compiles to: --> condition 0BRANCH OFFSET true-part rest
118 \ where OFFSET is the offset of 'rest'
119 \ condition IF true-part ELSE false-part THEN
120 \ -- compiles to: --> condition 0BRANCH OFFSET true-part BRANCH OFFSET2 false-part rest
121 \ where OFFSET if the offset of false-part and OFFSET2 is the offset of rest
123 \ IF is an IMMEDIATE word which compiles 0BRANCH followed by a dummy offset, and places
124 \ the address of the 0BRANCH on the stack. Later when we see THEN, we pop that address
125 \ off the stack, calculate the offset, and back-fill the offset.
127 ' 0BRANCH , \ compile 0BRANCH
128 HERE @ \ save location of the offset on the stack
129 0 , \ compile a dummy offset
134 HERE @ SWAP - \ calculate the offset from the address saved on the stack
135 SWAP ! \ store the offset in the back-filled location
139 ' BRANCH , \ definite branch to just over the false-part
140 HERE @ \ save location of the offset on the stack
141 0 , \ compile a dummy offset
142 SWAP \ now back-fill the original (IF) offset
143 DUP \ same as for THEN word above
148 \ BEGIN loop-part condition UNTIL
149 \ -- compiles to: --> loop-part condition 0BRANCH OFFSET
150 \ where OFFSET points back to the loop-part
151 \ This is like do { loop-part } while (condition) in the C language
153 HERE @ \ save location on the stack
157 ' 0BRANCH , \ compile 0BRANCH
158 HERE @ - \ calculate the offset from the address saved on the stack
159 , \ compile the offset here
162 \ BEGIN loop-part AGAIN
163 \ -- compiles to: --> loop-part BRANCH OFFSET
164 \ where OFFSET points back to the loop-part
165 \ In other words, an infinite loop which can only be returned from with EXIT
167 ' BRANCH , \ compile BRANCH
168 HERE @ - \ calculate the offset back
169 , \ compile the offset here
172 \ BEGIN condition WHILE loop-part REPEAT
173 \ -- compiles to: --> condition 0BRANCH OFFSET2 loop-part BRANCH OFFSET
174 \ where OFFSET points back to condition (the beginning) and OFFSET2 points to after the whole piece of code
175 \ So this is like a while (condition) { loop-part } loop in the C language
177 ' 0BRANCH , \ compile 0BRANCH
178 HERE @ \ save location of the offset2 on the stack
179 0 , \ compile a dummy offset2
183 ' BRANCH , \ compile BRANCH
184 SWAP \ get the original offset (from BEGIN)
185 HERE @ - , \ and compile it after BRANCH
187 HERE @ SWAP - \ calculate the offset2
188 SWAP ! \ and back-fill it in the original location
191 \ FORTH allows ( ... ) as comments within function definitions. This works by having an IMMEDIATE
192 \ word called ( which just drops input characters until it hits the corresponding ).
194 1 \ allowed nested parens by keeping track of depth
196 KEY \ read next character
197 DUP '(' = IF \ open paren?
198 DROP \ drop the open paren
201 ')' = IF \ close paren?
205 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
206 DROP \ drop the depth counter
210 From now on we can use ( ... ) for comments.
212 In FORTH style we can also use ( ... -- ... ) to show the effects that a word has on the
213 parameter stack. For example:
215 ( n -- ) means that the word consumes an integer (n) from the parameter stack.
216 ( b a -- c ) means that the word uses two integers (a and b, where a is at the top of stack)
217 and returns a single integer (c).
218 ( -- ) means the word has no effect on the stack
221 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
224 DUP 0> ( while n > 0 )
226 SPACE ( print a space )
227 1- ( until we count down to 0 )
232 ( c a b WITHIN returns true if a <= c and c < b )
248 ( .S prints the contents of the stack. Very useful for debugging. )
250 DSP@ ( get current stack pointer )
254 DUP @ . ( print the stack element )
260 ( DEPTH returns the depth of the stack. )
263 4- ( adjust because S0 was on the stack when we pushed DSP )
267 [NB. The following may be a bit confusing because of the need to use backslash before
268 each double quote character. The backslashes are there to keep the assembler happy.
269 They are NOT part of the final output. So here we are defining a function called
270 'S double-quote' (not 'S backslash double-quote').]
272 S" string" is used in FORTH to define strings. It leaves the address of the string and
273 its length on the stac,k with the address at the top. The space following S" is the normal
274 space between FORTH words and is not a part of the string.
276 In compile mode we append
277 LITSTRING <string length> <string rounded up 4 bytes>
278 to the current word. The primitive LITSTRING does the right thing when the current
281 In immediate mode there isn't a particularly good place to put the string, but in this
282 case we put the string at HERE (but we _don't_ change HERE). This is meant as a temporary
283 location, likely to be overwritten soon after.
285 : S" IMMEDIATE ( -- len addr )
286 STATE @ IF ( compiling? )
287 ' LITSTRING , ( compile LITSTRING )
288 HERE @ ( save the address of the length word on the stack )
289 0 , ( dummy length - we don't know what it is yet )
291 KEY ( get next character of the string )
294 HERE @ !b ( store the character in the compiled image )
295 1 HERE +! ( increment HERE pointer by 1 byte )
297 DROP ( drop the double quote character at the end )
298 DUP ( get the saved address of the length word )
299 HERE @ SWAP - ( calculate the length )
300 4- ( subtract 4 (because we measured from the start of the length word) )
301 SWAP ! ( and back-fill the length location )
302 HERE @ ( round up to next multiple of 4 bytes for the remaining code )
306 ELSE ( immediate mode )
307 HERE @ ( get the start address of the temporary space )
312 OVER !b ( save next character )
313 1+ ( increment address )
315 HERE @ - ( calculate the length )
316 HERE @ ( push the start address )
321 ." is the print string operator in FORTH. Example: ." Something to print"
322 The space after the operator is the ordinary space required between words.
324 This is tricky to define because it has to do different things depending on whether
325 we are compiling or in immediate mode. (Thus the word is marked IMMEDIATE so it can
326 detect this and do different things).
328 In immediate mode we just keep reading characters and printing them until we get to
329 the next double quote.
331 In compile mode we have the problem of where we're going to store the string (remember
332 that the input buffer where the string comes from may be overwritten by the time we
333 come round to running the function). We store the string in the compiled function
335 ..., LITSTRING, string length, string rounded up to 4 bytes, EMITSTRING, ...
337 : ." IMMEDIATE ( -- )
338 STATE @ IF ( compiling? )
339 ' LITSTRING , ( compile LITSTRING )
340 HERE @ ( save the address of the length word on the stack )
341 0 , ( dummy length - we don't know what it is yet )
343 KEY ( get next character of the string )
346 HERE @ !b ( store the character in the compiled image )
347 1 HERE +! ( increment HERE pointer by 1 byte )
349 DROP ( drop the double quote character at the end )
350 DUP ( get the saved address of the length word )
351 HERE @ SWAP - ( calculate the length )
352 4- ( subtract 4 (because we measured from the start of the length word) )
353 SWAP ! ( and back-fill the length location )
354 HERE @ ( round up to next multiple of 4 bytes for the remaining code )
358 ' EMITSTRING , ( compile the final EMITSTRING )
360 ( In immediate mode, just read characters and print them until we get
361 to the ending double quote. Much simpler than the above code! )
365 DROP ( drop the double quote character )
366 EXIT ( return from this function )
374 In FORTH, global constants and variables are defined like this:
376 10 CONSTANT TEN when TEN is executed, it leaves the integer 10 on the stack
377 VARIABLE VAR when VAR is executed, it leaves the address of VAR on the stack
379 Constants can be read by not written, eg:
383 You can read a variable (in this example called VAR) by doing:
385 VAR @ leaves the value of VAR on the stack
386 VAR @ . CR prints the value of VAR
388 and update the variable by doing:
390 20 VAR ! sets VAR to 20
392 Note that variables are uninitialised (but see VALUE later on which provides initialised
393 variables with a slightly simpler syntax).
395 How can we define the words CONSTANT and VARIABLE?
397 The trick is to define a new word for the variable itself (eg. if the variable was called
398 'VAR' then we would define a new word called VAR). This is easy to do because we exposed
399 dictionary entry creation through the CREATE word (part of the definition of : above).
400 A call to CREATE TEN leaves the dictionary entry:
405 +---------+---+---+---+---+
406 | LINK | 3 | T | E | N |
407 +---------+---+---+---+---+
410 For CONSTANT we can continue by appending DOCOL (the codeword), then LIT followed by
411 the constant itself and then EXIT, forming a little word definition that returns the
414 +---------+---+---+---+---+------------+------------+------------+------------+
415 | LINK | 3 | T | E | N | DOCOL | LIT | 10 | EXIT |
416 +---------+---+---+---+---+------------+------------+------------+------------+
419 Notice that this word definition is exactly the same as you would have got if you had
423 CREATE ( make the dictionary entry (the name follows CONSTANT) )
424 DOCOL , ( append DOCOL (the codeword field of this word) )
425 ' LIT , ( append the codeword LIT )
426 , ( append the value on the top of the stack )
427 ' EXIT , ( append the codeword EXIT )
431 VARIABLE is a little bit harder because we need somewhere to put the variable. There is
432 nothing particularly special about the 'user definitions area' (the area of memory pointed
433 to by HERE where we have previously just stored new word definitions). We can slice off
434 bits of this memory area to store anything we want, so one possible definition of
435 VARIABLE might create this:
437 +--------------------------------------------------------------+
440 +---------+---------+---+---+---+---+------------+------------+---|--------+------------+
441 | <var> | LINK | 3 | V | A | R | DOCOL | LIT | <addr var> | EXIT |
442 +---------+---------+---+---+---+---+------------+------------+------------+------------+
445 where <var> is the place to store the variable, and <addr var> points back to it.
447 To make this more general let's define a couple of words which we can use to allocate
448 arbitrary memory from the user definitions area.
450 First ALLOT, where n ALLOT allocates n bytes of memory. (Note when calling this that
451 it's a very good idea to make sure that n is a multiple of 4, or at least that next time
452 a word is compiled that n has been left as a multiple of 4).
454 : ALLOT ( n -- addr )
455 HERE @ SWAP ( here n -- )
456 HERE +! ( adds n to HERE, after this the old value of HERE is still on the stack )
460 Second, CELLS. In FORTH the phrase 'n CELLS ALLOT' means allocate n integers of whatever size
461 is the natural size for integers on this machine architecture. On this 32 bit machine therefore
462 CELLS just multiplies the top of stack by 4.
464 : CELLS ( n -- n ) 4 * ;
467 So now we can define VARIABLE easily in much the same way as CONSTANT above. Refer to the
468 diagram above to see what the word that this creates will look like.
471 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
472 CREATE ( make the dictionary entry (the name follows VARIABLE) )
473 DOCOL , ( append DOCOL (the codeword field of this word) )
474 ' LIT , ( append the codeword LIT )
475 , ( append the pointer to the new memory )
476 ' EXIT , ( append the codeword EXIT )
480 VALUEs are like VARIABLEs but with a simpler syntax. You would generally use them when you
481 want a variable which is read often, and written infrequently.
483 20 VALUE VAL creates VAL with initial value 20
484 VAL pushes the value directly on the stack
485 30 TO VAL updates VAL, setting it to 30
487 Notice that 'VAL' on its own doesn't return the address of the value, but the value itself,
488 making values simpler and more obvious to use than variables (no indirection through '@').
489 The price is a more complicated implementation, although despite the complexity there is no
490 particular performance penalty at runtime.
492 A naive implementation of 'TO' would be quite slow, involving a dictionary search each time.
493 But because this is FORTH we have complete control of the compiler so we can compile TO more
494 efficiently, turning:
498 and calculating <addr> (the address of the value) at compile time.
500 Now this is the clever bit. We'll compile our value like this:
502 +---------+---+---+---+---+------------+------------+------------+------------+
503 | LINK | 3 | V | A | L | DOCOL | LIT | <value> | EXIT |
504 +---------+---+---+---+---+------------+------------+------------+------------+
507 where <value> is the actual value itself. Note that when VAL executes, it will push the
508 value on the stack, which is what we want.
510 But what will TO use for the address <addr>? Why of course a pointer to that <value>:
512 code compiled - - - - --+------------+------------+------------+-- - - - -
513 by TO VAL | LIT | <addr> | ! |
514 - - - - --+------------+-----|------+------------+-- - - - -
517 +---------+---+---+---+---+------------+------------+------------+------------+
518 | LINK | 3 | V | A | L | DOCOL | LIT | <value> | EXIT |
519 +---------+---+---+---+---+------------+------------+------------+------------+
522 In other words, this is a kind of self-modifying code.
524 (Note to the people who want to modify this FORTH to add inlining: values defined this
525 way cannot be inlined).
528 CREATE ( make the dictionary entry (the name follows VALUE) )
529 DOCOL , ( append DOCOL )
530 ' LIT , ( append the codeword LIT )
531 , ( append the initial value )
532 ' EXIT , ( append the codeword EXIT )
535 : TO IMMEDIATE ( n -- )
536 WORD ( get the name of the value )
537 FIND ( look it up in the dictionary )
538 >DFA ( get a pointer to the first data field (the 'LIT') )
539 4+ ( increment to point at the value )
540 STATE @ IF ( compiling? )
541 ' LIT , ( compile LIT )
542 , ( compile the address of the value )
544 ELSE ( immediate mode )
545 ! ( update it straightaway )
549 ( x +TO VAL adds x to VAL )
551 WORD ( get the name of the value )
552 FIND ( look it up in the dictionary )
553 >DFA ( get a pointer to the first data field (the 'LIT') )
554 4+ ( increment to point at the value )
555 STATE @ IF ( compiling? )
556 ' LIT , ( compile LIT )
557 , ( compile the address of the value )
558 ' +! , ( compile +! )
559 ELSE ( immediate mode )
560 +! ( update it straightaway )
565 ID. takes an address of a dictionary entry and prints the word's name.
567 For example: LATEST @ ID. would print the name of the last word that was defined.
570 4+ ( skip over the link pointer )
571 DUP @b ( get the flags/length byte )
572 F_LENMASK AND ( mask out the flags - just want the length )
575 DUP 0> ( length > 0? )
577 SWAP 1+ ( addr len -- len addr+1 )
578 DUP @b ( len addr -- len addr char | get the next character)
579 EMIT ( len addr char -- len addr | and print it)
580 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
582 2DROP ( len addr -- )
586 'WORD word FIND ?HIDDEN' returns true if 'word' is flagged as hidden.
588 'WORD word FIND ?IMMEDIATE' returns true if 'word' is flagged as immediate.
591 4+ ( skip over the link pointer )
592 @b ( get the flags/length byte )
593 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
596 4+ ( skip over the link pointer )
597 @b ( get the flags/length byte )
598 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
602 WORDS prints all the words defined in the dictionary, starting with the word defined most recently.
603 However it doesn't print hidden words.
605 The implementation simply iterates backwards from LATEST using the link pointers.
608 LATEST @ ( start at LATEST dictionary entry )
610 DUP 0<> ( while link pointer is not null )
613 DUP ID. ( print the word )
616 @ ( dereference the link pointer - go to previous word )
623 So far we have only allocated words and memory. FORTH provides a rather primitive method
626 'FORGET word' deletes the definition of 'word' from the dictionary and everything defined
627 after it, including any variables and other memory allocated after.
629 The implementation is very simple - we look up the word (which returns the dictionary entry
630 address). Then we set HERE to point to that address, so in effect all future allocations
631 and definitions will overwrite memory starting at the word. We also need to set LATEST to
632 point to the previous word.
634 Note that you cannot FORGET built-in words (well, you can try but it will probably cause
637 XXX: Because we wrote VARIABLE to store the variable in memory allocated before the word,
638 in the current implementation VARIABLE FOO FORGET FOO will leak 1 cell of memory.
641 WORD FIND ( find the word, gets the dictionary entry address )
642 DUP @ LATEST ! ( set LATEST to point to the previous word )
643 HERE ! ( and store HERE with the dictionary address )
647 While compiling, '[COMPILE] word' compiles 'word' if it would otherwise be IMMEDIATE.
649 : [COMPILE] IMMEDIATE
650 WORD ( get the next word )
651 FIND ( find it in the dictionary )
652 >CFA ( get its codeword )
653 , ( and compile that )
657 RECURSE makes a recursive call to the current word that is being compiled.
659 Normally while a word is being compiled, it is marked HIDDEN so that references to the
660 same word within are calls to the previous definition of the word. However we still have
661 access to the word which we are currently compiling through the LATEST pointer so we
662 can use that to compile a recursive call.
665 LATEST @ >CFA ( LATEST points to the word being compiled at the moment )
670 DUMP is used to dump out the contents of memory, in the 'traditional' hexdump format.
672 : DUMP ( addr len -- )
673 BASE @ ROT ( save the current BASE at the bottom of the stack )
674 HEX ( and switch the hexadecimal mode )
677 DUP 0> ( while len > 0 )
679 OVER . ( print the address )
682 ( print up to 16 words on this line )
683 2DUP ( addr len addr len )
684 1- 15 AND 1+ ( addr len addr linelen )
686 DUP 0> ( while linelen > 0 )
688 SWAP ( addr len linelen addr )
689 DUP @b ( addr len linelen addr byte )
690 . SPACE ( print the byte )
691 1+ SWAP 1- ( addr len linelen addr -- addr len addr+1 linelen-1 )
695 ( print the ASCII equivalents )
696 2DUP 1- 15 AND 1+ ( addr len addr linelen )
698 DUP 0> ( while linelen > 0)
700 SWAP ( addr len linelen addr )
701 DUP @b ( addr len linelen addr byte )
702 DUP 32 128 WITHIN IF ( 32 <= c < 128? )
705 DROP [ CHAR ? ] LITERAL EMIT
707 1+ SWAP 1- ( addr len linelen addr -- addr len addr+1 linelen-1 )
712 DUP 1- 15 AND 1+ ( addr len linelen )
713 DUP ( addr len linelen linelen )
714 ROT ( addr linelen len linelen )
715 - ( addr linelen len-linelen )
716 ROT ( len-linelen addr linelen )
717 + ( len-linelen addr+linelen )
718 SWAP ( addr-linelen len-linelen )
721 2DROP ( restore stack )
722 BASE ! ( restore saved BASE )
725 ( Finally print the welcome prompt. )
726 ." JONESFORTH VERSION " VERSION . CR