Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Conklin E.K.Forth programmer's handbook.2000.pdf
Скачиваний:
321
Добавлен:
23.08.2013
Размер:
2.04 Mб
Скачать

1. INTRODUCTION

This Forth Programmer’s Handbook provides a reference source for the most common features of the integrated software development systems based on the Forth programming language. We assume at least an elementary knowledge of programming, including any high-level language or assembler. If you are new to Forth, we encourage you to begin by reading this chapter and the next carefully, writing simple programs using an ANS Forth system of your choice.

This book is primarily intended to describe how a programmer can use Forth to solve problems. This is a rather different goal from explaining how Forth works, but it is a practical necessity for the new user of a Forth system. This manual is also organized to serve experienced programmers who need to check some point quickly.

We highly recommend that you spend time examining the Forth source code supplied with your system, along with its documentation. Forth was designed to be highly readable, and the source code offers many examples of good usage and programming practice.

This manual does not attempt to cover all Forth commands. Indeed, no book can do that—Forth is an extensible system, and no two implementations need or use identical components. What we can do is provide a detailed exposition of the most valuable and most commonly used features and facilities of the fundamental system from which your application begins.

FORTH, Inc. provides development environments for a growing number of computer systems and embedded microprocessors. Since hardware is unique for each computer, it is not feasible for this document to cover every feature of every system supported. The Forth Programmer’s Handbook presents features common to Standard Forth and to the most common extensions found in all FORTH, Inc. systems. When discussing hardware-specific features, particu-

Introduction 1

Forth Programmer’s Handbook

larly dictionary structure, high-level object format, database management, and device drivers, an idealized model of a Forth system is used. Separate product documentation provides implementation details and descriptions of features specific to that system.

In this manual, typefaces are used as follows:

!This typeface is used for text, with italic used for symbolic notation and for the first appearance of new terms;

!Executable Forth commands and source code are shown in distinctive bold type, e.g., 60 LIST.

!Parameters that are described indirectly instead of explicitly are shown in distinctive plain type and inside brackets, e.g., <block number> LIST. When these parameters are discussed in text, they usually are shown in italic.

!Non-executable text strings such as error messages are shown in plain type without brackets, e.g., Page Fault.

1.1FORTH LANGUAGE FEATURES

This section highlights special considerations arising from the actual implementation of a system. More detailed technical discussions of subjects covered here will be found in later sections of this book, especially Section 2. Appendix B, ”Glossary & Notation” provides supplementary definitions of many of the terms used in this manual, as well as a detailed description of the notation conventions.

1.1.1 Definitions of Terms

Forth allows any kind of ASCII string (except one containing spaces) to be a valid name, and this introduces some ambiguities in references. For instance, Forth calls subroutines words, but word could also mean an addressable unit of memory. To resolve this, we use the following conventions:

!A Forth execution procedure is called a definition. A word is the name of such a definition.

2 Introduction

Forth Programmer’s Handbook

!The word length of the processor is always referred to as a cell. This is also the size of an address and the size of a single item on Forth’s stacks.

!Eight bits is called a byte. On a 32-bit or larger processor, a 16-bit item may be called a 16-bit cell or half-cell.

1.1.2 Dictionary

The dictionary contains all the executable routines (or words) that make up a Forth system. System routines are entries predefined in the dictionary that become available when the system is booted. Electives are optionally compiled after booting. User-defined words are entries the user adds. In a multi-user configuration, system and elective definitions are available to all users, whereas user-defined words are available only to the user who defines them. Otherwise, there are no differences in size, speed, or structure. You may make user words available to other users simply by loading them with the other electives.

The basic form of the most common type of word definition is:

: <name> <words to be executed> ;

where : constructs a new definition called name, which is terminated by ;. When name is referenced, the words in the body of the definition name will be executed. There are other kinds of words in Forth: words defined in assembler code, words that function as data objects, etc. All have dictionary entries with a similar structure, and are managed by the same internal rules. The various kinds of definitions are discussed in Section 4.2.

The dictionary is the fundamental mechanism by which Forth allocates memory and performs symbol table operations. Because the dictionary serves so many purposes, it’s important that you understand how to use it.

The dictionary is a linked list of variable-length entries, each of which is a Forth word and its definition. In most implementations, the dictionary grows toward high memory; the discussion in this section will assume it does. Each dictionary entry points to the entry that logically precedes it (see Figure 1). The address of the next available cell at the end of the dictionary is put on the stack by the word HERE.

Introduction 3

Forth Programmer’s Handbook

HERE

 

Less recent def. Less recent def. Most recent def.

Unused

dictionary

 

 

space

Figure 1. The “top” of a dictionary. HERE returns the address of the next available location.

Dictionary entries are not necessarily contiguous. For example, in cross-com- pilers used to construct programs for embedded systems, the searchable portion of the dictionary (name, link, and a pointer to the content—see Figure 2) may reside in a host computer, and the actual content may reside in a target image being constructed in the host computer’s memory for later downloading or for burning into PROM.

 

 

 

 

Most recent

 

 

 

 

definition

Pointer to top entry

Link

Name

Content

 

 

 

 

 

Next most

 

Link

Name

Content

recent

 

definition

 

 

More definitions...

Last

 

 

 

 

 

 

 

 

definition

 

Link

Name

Content

 

Figure 2. Logical structure of the Forth dictionary

The dictionary is searched by sequentially matching names in source text against names compiled in the dictionary. On some systems, the search is speeded by providing more than one chain of definitions, with the entries linked in logical sequences that do not necessarily reflect their physical location. The Forth text interpreter selects one of these chains to search; the selection mechanism is implementation dependent, and may include two or more chains in a programmer-controlled order (see Section 4.6). The search follows

4 Introduction

Forth Programmer’s Handbook

the selected chain until a match is found or the end of the chain is reached. Because the latest definition will be found first, this organization permits words to be redefined, a technique that is frequently useful.

The Standard Forth term for one of these chains is word list. A word list is a subset of the dictionary containing words for some special purpose. There usually are several word lists present in a system and these are normally available to all users on a re-entrant basis.

The essential structure of dictionary entries is the same for all words, and is diagrammed in Figure 2. The link cell contains the location of the preceding entry. This speeds up searches, which start at the recent end of the dictionary and work backwards to the older end. By this process, the most recent definition of a word is always found. In a developed application, where the user is dealing with the highest level of the program, this process optimizes search time.

The name field in a dictionary entry contains the count of characters in the full name, followed by some number of characters in the name. The count (and, thus, the longest allowable name length) usually is limited to 31 characters. On most systems, any characters other than space, backspace, and carriage return can be used as part of a name field. However, Standard Forth advises that you can only depend on being able to use graphic characters.

Some systems are case sensitive and others are not; see your product documentation for details. To avoid problems and to maximize the transportability of code, the names of the words provided in a standard system are defined in all upper-case letters and should always be referred to in all upper-case letters when using them in subsequent definitions. When defining and using new names, it is important to be consistent; always refer to a name using exactly the same case(s) in which it was defined. Also, in systems that are case sensitive, avoid creating names that differ only in their use of case; such code will not be transportable to a case-insensitive system.

Although the order of the fields in a dictionary entry is arranged in each implementation to optimize each machine’s dictionary search, Figure 3 shows a general model. There will always be a link field and a name field, and usually a code field. The code field directs the system to the run-time code to be executed when this definition is invoked. There is often a parameter field of variable length, containing references to data needed when this definition executes. There may also be a locate field, containing information about where

Introduction 5

Forth Programmer’s Handbook

this word is defined in source code. When developing programs for embedded systems, this structure may exist only on the host, with a parameter field containing a pointer to the actual executable portion being constructed in the target image.

Link to next definition

Control bits

LOCATE

Link

 

Count

Name

Code Field

Parameter Field

 

 

 

 

 

 

 

Figure 3. Structural details of a typical dictionary entry

In addition, usually there are several control bits to control the type and use of the definition. Since the longest name field in most systems has 31 characters, requiring only five bits to express a count, the control bits are often found in the byte containing the count. The most important control bit is called the precedence bit. A word whose precedence bit is set executes at compile time. The precedence bit is set by the word IMMEDIATE. The precedence bit is used for a few special words, such as compiler directives, but it is zero for most words.

Another common control bit is the smudge bit. A word whose smudge bit is set is invisible to a dictionary search. This bit is set by the compiler when starting to compile a high-level : (colon) definition, to prevent unintentional recursive references. It is reset by the word ; (semicolon) that ends the definition.

The code field, pointing to the run-time code for a definition, causes different behaviors depending on the type of word being defined. In some implementation strategies, the code field is not required, or contains the code itself.

The cells (if any) after the code field address are called the parameter field, which is of variable length. CONSTANTs and VARIABLEs keep their data in the first cell of the parameter field. Other definitions may keep several values.

References CODE definitions, Section 5.1

Code field addresses, Section 4.2.4

Creating dictionary entries, Section 4.2.1

Word lists, Section 4.6

6 Introduction

Соседние файлы в предмете Электротехника