FORTH: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
m (Undo revision 186856 by 198.83.124.250 (Talk))
 
(24 intermediate revisions by 9 users not shown)
Line 1: Line 1:
FORTH is a terse, stack-oriented, interpreted/semi-compiled language using reverse Polish notation, like Hewlett-Packard calculators. Identifiers, called words, are composed of any sequence of printing characters. Words are separated by any whitespace characters, including space, tab, and new line. The word . prints the item on top of the stack.
'''FORTH''' is a terse, stack-oriented, interpreted/semi-compiled language using reverse Polish notation, like Hewlett-Packard calculators. Forth is at the core of [[Open Firmware]], the boot-loader for the XO-1.

==Introduction==
Identifiers, called words, are composed of any sequence of printing characters. Words are separated by any whitespace characters, including space, tab, and new line. The word . prints the item on top of the stack.


For example, the line
For example, the line
Line 15: Line 18:
5 OK
5 OK


The OK signifies that the line is finished, and FORTH is ready for the next command.
The OK signifies that the line is finished, and '''FORTH''' is ready for the next command.


The design goals for FORTH included minimum program size; readability by experienced users, though not always by novices; and extremely high productivity.
The design goals for FORTH included minimum program size; readability by experienced users, though not always by novices; and extremely high productivity.
Line 21: Line 24:
We do not have room here to explain the language properly. See references below.
We do not have room here to explain the language properly. See references below.


Free Software versions of FORTH include gforth, kforth, pforth, and yforth. Source code is of course available for each of these.
Free Software versions of '''FORTH''' include gforth, kforth, pforth, and yforth. Source code is of course available for each of these.


Due to the participation of [[Mitch Bradley]], the author of [[Open Firmware]], every OLPC laptop will have Forth as the bootloader, just like Sun and Apple computers. See also the [[OFW FAQ]].
Due to the participation of [[Mitch Bradley]], the author of [[Open Firmware]], every OLPC laptop will have Forth as the bootloader, just like Sun and Apple computers.


==FORTH for education==
==FORTH for education==


FORTH is one of the best programming languages for learning how to build languages. The FORTH kernel was originally about 8K of highly modular code. Students who know an assembly language can read and understand the code for FORTH for that processor in a few weeks. The rest of the FORTH system is written in highly modular FORTH.
'''FORTH''' is one of the best programming languages for learning how to build languages. The '''FORTH''' kernel was originally about 8K of highly modular code. Students who know an assembly language can read and understand the code for FORTH for that processor in a few weeks. The rest of the FORTH system is written in highly modular FORTH.


Learning FORTH internals is almost the same as learning Virtual Machine architecture. The primary technical difference is that VMs use byte codes (token-threaded code), while compiled FORTH consists mainly of sequences of addresses of subroutines (address-threaded code). Token-threaded code is shorter, but takes longer to interpret than address-threaded code.
Learning '''FORTH''' internals is almost the same as learning Virtual Machine architecture. The primary technical difference is that VMs use byte codes (token-threaded code), while compiled FORTH consists mainly of sequences of addresses of subroutines (direct-threaded code). Token-threaded code is shorter, but takes longer to interpret than direct-threaded code.


Most modern Forth implementations now choose to use subroutine-threaded (rather than direct-threaded) code. In subroutine threaded code, the "sequence of addresses" is really a sequence of CALL, BAL, or JSR instructions (or whatever subroutine call instruction is for your platform). This eliminates the need for an "inner interpreter", which on modern microprocessors, gives a substantial performance boost.
==External links==

==History on the XO==


[http://laptop.media.mit.edu/laptopnews.nsf/2e76a5a80bc36cbf85256cd700545fa5/e0fd646743b04525852571d00002e297?OpenDocument OLPC News (2006-08-19)]
[http://laptop.media.mit.edu/laptopnews.nsf/2e76a5a80bc36cbf85256cd700545fa5/e0fd646743b04525852571d00002e297?OpenDocument OLPC News (2006-08-19)]
:Mitch Bradley has learned his way around LinuxBIOS, and has brought up a '''Forth''' implementation on it. Forth is a very productive environment for test and debugging of hardware, and will come in very handy for easier debug, testing, and exercise of the CAFE chip. A Forth environment allows for immediate turn around of tests on hardware without a compile, link, reboot step that a conventional device driver requires.


[http://laptop.media.mit.edu/laptopnews.nsf/2e76a5a80bc36cbf85256cd700545fa5/2cc25a58f0d1a6e8852572070033befc?OpenDocument OLPC News 2006-10-14]
Mitch Bradley has learned his way around LinuxBIOS, and has brought up a Forth implementation on it. Forth is a very productive environment for test and debugging of hardware, and will come in very handy for easier debug, testing, and exercise of the CAFE chip. A Forth environment allows for immediate turn around of tests on hardware without a compile, link, reboot step that a conventional device driver requires.
:One month ago, without warning, Sun released Open Firmware under a BSD license. After much deliberation, we reached the decision that rather than continuing to use Linux itself as boot loader, we will transition to Open Firmware (OFW), while retaining LinuxBIOS. The date for a transition has not yet been set. Open Firmware has been in use on both Sun and PPC hardware (e.g. Apple) for approaching a decade, and has shipped in quantity millions. Mitch Bradley, currently working for OLPC is its original author.


[http://laptop.media.mit.edu/laptopnews.nsf/2e76a5a80bc36cbf85256cd700545fa5/3d6d608f93214eef85257215006c9105?OpenDocument OLPC News 2006-10-28]
[http://www.forth.org/ Forth Interest Group]
:Wednesday, we had no fully functioning BIOS for use with CAFE. We had intended to use LinuxBIOS with Linux as bootloader for B1 with a transition to Open Firmware (OFW) as bootloader before B2. We continued with both possibilities in parallel, such that by Friday we had both working. Testing of OFW's new USB stack has succeeded, so we have decided to use OFW immediately. Our efforts will now focus on the LinuxBIOS/OFW combination.


[[OLPC Firmware q2c01]], released on April 6, 2007 introduces the "c" series of firmware, which removes LinuxBIOS and uses only Open Firmware, in order to speed up booting and resumption from sleep.
[http://en.wikipedia.org/wiki/Forth_programming_language Wikipedia: FORTH]


==See also==
[http://www.phact.org/e/forth.htm Forth, a programming language for real programmers]
*[[Forth Lessons]]
*[[Forth stack operators]]
*[[OFW FAQ|Open Firmware FAQ]]

==External links==
* [http://www.forth.org/ Forth Interest Group]
* [http://en.wikipedia.org/wiki/Forth_programming_language Wikipedia: FORTH]
* [http://www.phact.org/e/forth.htm Forth, a programming language for real programmers]
* [http://www.forth.org/tutorials.html FORTH tutorials]
* [http://sunsolve.sun.com/data/816/816-1177/pdf/816-1177-10.pdf OpenBoot 4.x Command Reference Manual]
* [http://firmworks.com/QuickRef.html Forth Reference for the XO]
===Forth in the XO===
* [http://dev.laptop.org/pub/firmware/scripts/batman.fth Batman Script]
* [http://dev.laptop.org/pub/firmware/scripts/olpc.fth OLPC Boot Script]
* [http://dev.laptop.org/~joel/ec-dump.fth ec-dump.fth]
* http://dev.laptop.org/~wmb/sdkit.tgz - the Forth core of OFW as a Linux executable, used for hardware debugging from within Linux


[[Category:Programming language]]
[http://www.forth.org/tutorials.html FORTH tutorials]
[[Category:Forth]]

Latest revision as of 19:30, 16 December 2008

FORTH is a terse, stack-oriented, interpreted/semi-compiled language using reverse Polish notation, like Hewlett-Packard calculators. Forth is at the core of Open Firmware, the boot-loader for the XO-1.

Introduction

Identifiers, called words, are composed of any sequence of printing characters. Words are separated by any whitespace characters, including space, tab, and new line. The word . prints the item on top of the stack.

For example, the line

2 3 + .

starts by putting 2 on the stack, leaving 3 + . to interpret.

The next step is to put 3 on the stack, giving 3 2, with + . left to interpret.

Then + adds the two top elements of the stack and puts the result back, so that the stack contains just 5, and only . is left.

Finally, . removes the 5 from the stack and prints it

5 OK

The OK signifies that the line is finished, and FORTH is ready for the next command.

The design goals for FORTH included minimum program size; readability by experienced users, though not always by novices; and extremely high productivity.

We do not have room here to explain the language properly. See references below.

Free Software versions of FORTH include gforth, kforth, pforth, and yforth. Source code is of course available for each of these.

Due to the participation of Mitch Bradley, the author of Open Firmware, every OLPC laptop will have Forth as the bootloader, just like Sun and Apple computers.

FORTH for education

FORTH is one of the best programming languages for learning how to build languages. The FORTH kernel was originally about 8K of highly modular code. Students who know an assembly language can read and understand the code for FORTH for that processor in a few weeks. The rest of the FORTH system is written in highly modular FORTH.

Learning FORTH internals is almost the same as learning Virtual Machine architecture. The primary technical difference is that VMs use byte codes (token-threaded code), while compiled FORTH consists mainly of sequences of addresses of subroutines (direct-threaded code). Token-threaded code is shorter, but takes longer to interpret than direct-threaded code.

Most modern Forth implementations now choose to use subroutine-threaded (rather than direct-threaded) code. In subroutine threaded code, the "sequence of addresses" is really a sequence of CALL, BAL, or JSR instructions (or whatever subroutine call instruction is for your platform). This eliminates the need for an "inner interpreter", which on modern microprocessors, gives a substantial performance boost.

History on the XO

OLPC News (2006-08-19)

Mitch Bradley has learned his way around LinuxBIOS, and has brought up a Forth implementation on it. Forth is a very productive environment for test and debugging of hardware, and will come in very handy for easier debug, testing, and exercise of the CAFE chip. A Forth environment allows for immediate turn around of tests on hardware without a compile, link, reboot step that a conventional device driver requires.

OLPC News 2006-10-14

One month ago, without warning, Sun released Open Firmware under a BSD license. After much deliberation, we reached the decision that rather than continuing to use Linux itself as boot loader, we will transition to Open Firmware (OFW), while retaining LinuxBIOS. The date for a transition has not yet been set. Open Firmware has been in use on both Sun and PPC hardware (e.g. Apple) for approaching a decade, and has shipped in quantity millions. Mitch Bradley, currently working for OLPC is its original author.

OLPC News 2006-10-28

Wednesday, we had no fully functioning BIOS for use with CAFE. We had intended to use LinuxBIOS with Linux as bootloader for B1 with a transition to Open Firmware (OFW) as bootloader before B2. We continued with both possibilities in parallel, such that by Friday we had both working. Testing of OFW's new USB stack has succeeded, so we have decided to use OFW immediately. Our efforts will now focus on the LinuxBIOS/OFW combination.

OLPC Firmware q2c01, released on April 6, 2007 introduces the "c" series of firmware, which removes LinuxBIOS and uses only Open Firmware, in order to speed up booting and resumption from sleep.

See also

External links

Forth in the XO