Forth programming language - CCC Event Weblog

7 downloads 138 Views 26KB Size Report
Dec 10, 2004 ... Forth programming language. From Wikipedia, the free encyclopedia with additions from the OpenBIOS Project and Carsten Strotmann.
Forth programming language From Wikipedia, the free encyclopedia with additions from the OpenBIOS Project and Carsten Strotmann compiled for 21C3. 10th December 2004

Contents 1 FORTH 1.1 Overview . . . . . . . . . . . . . . . . 1.2 Forth from a programmer’s perspective . 1.3 Facilities of a FORTH system . . . . . . 1.4 Structure of the language . . . . . . . . 1.5 Computer programs in FORTH . . . . . 1.6 Implementation of a FORTH System . . 2 Open Firmware ? 2.1 A Brief History of Open Firmware 2.2 Hardware Independent Boot Code? 2.3 The Tasks of Boot Code . . . . . . 2.4 Why FORTH for all This? . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

1 2 2 4 4 4 5

. . . . . . Get Real! . . . . . . . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

5 5 6 6 7

. . . .

7 7 7 8 8

. . . . . .

. . . . . .

3 OpenBIOS, free OpenSource OpenFirmware not only for PC 3.1 What is OpenBIOS . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Why and where is Forth used? . . . . . . . . . . . . . . . . . . 3.3 FCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.4 Why bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 References

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

8

1 FORTH Forth is a computer programming environment. It was initially developed by Chuck Moore at the US National Radio Astronomy Observatory (NRAO) during the 1960s, formalized as a pro-

1

gramming language in 1977, and standardized by ANSI in 1994. It features both interactive execution of commands (making it suitable as a shell for systems that lack a more formal operating system), as well as the ability to compile sequences of commands into threaded code for later execution. Its name is derived from Mr. Moore’s belief that it was a "fourth-generation computer language" but it was developed on a computer whose file system allowed only five-letter identifiers.

1.1 Overview Forth offers a standalone programming environment consisting of a stack oriented interactive incremental interpreter/compiler. Programming is done by extending the language with ’words’ (the term used for Forth subroutines), which become part of the language once defined. Forth is usually implemented with an inner interpreter tracing indirectly threaded machine code, which yields extremely compact and fast high-level code that can be compiled rapidly. A character-oriented screen/block mechanism and standard editor written in Forth, provide a file mechanism for creating and storing Forth source code. A typical Forth package will consist of a pre-compiled kernel of the core words, which the programmer uses to define new words for the application. The application, once complete, can be saved as an image, with all new words already compiled. Generally, programmers will extend the initial core with words that are useful to the sorts of applications that they do, and save this as their working foundation. Due to the ease of adding use small machine code definitions to the language and using those in an interactive high-level programming environment, the Forth language has been popular as a development language for embedded systems and as a vehicle for instrument control. The structure of the inner interpreter is similar to modern RISC processors and processors that use Forth as machine language have been produced (free examples are the b16 processor and µCore). Because of the modular extensible nature of Forth, which allows, e.g., for objectoriented programming, many high-level applications, such as CAD systems were written in Forth. Forth is used in the OpenFirmware boot ROMs used by Apple and Sun. It is also used by FreeBSD as the first stage boot controller.

1.2 Forth from a programmer’s perspective Forth relies heavily on explicit use of the stack data structure and Reverse Polish Notation (or RPN, also used on advanced calculators from Hewlett-Packard). This notation is also called postfix notation because the operator comes after its operands, as opposed to the more common infix notation where the operator comes between its operands. The rationale for postfix notation is that it is closer to the machine language the computer will eventually use, and should therefore be faster to execute. For example, one could get the result of a mathematical expression this way:

2

25 10 * 50 + . 300 This command line first puts the numbers 25 and 10 on the implied stack; the "*" command multiplies the two numbers on the top of the stack and replaces them with their product; then the number 50 is placed on the stack, and the "+" command adds it to the previous product; finally, the "." command prints the result to the user’s terminal. Even the language’s structural features are stack-based. For example: : FLOOR5 DUP 5 < IF DROP 5 ELSE 1 - THEN ; This code defines a new word (again ’word’ is the term used for a subroutine) called "FLOOR5" using the following commands: "DUP" simply duplicates the number on the stack; "