Annotation of gforth/doc/gforth.ds, revision 1.5
1.1 anton 1: \input texinfo @c -*-texinfo-*-
2: @comment The source is gforth.ds, from which gforth.texi is generated
3: @comment %**start of header (This is for running Texinfo on a region.)
4: @setfilename gforth.info
5: @settitle Gforth Manual
6: @dircategory GNU programming tools
7: @direntry
8: * Gforth: (gforth). A fast interpreter for the Forth language.
9: @end direntry
10: @comment @setchapternewpage odd
1.3 anton 11: @macro progstyle {TEXT}
12: @quotation
13: {Programming style note:} \TEXT\
14: @end quotation
15: @end macro
1.1 anton 16: @comment %**end of header (This is for running Texinfo on a region.)
17:
18: @ifinfo
1.5 ! anton 19: This file documents Gforth 0.4
1.1 anton 20:
21: Copyright @copyright{} 1995-1997 Free Software Foundation, Inc.
22:
23: Permission is granted to make and distribute verbatim copies of
24: this manual provided the copyright notice and this permission notice
25: are preserved on all copies.
26:
27: @ignore
28: Permission is granted to process this file through TeX and print the
29: results, provided the printed document carries a copying permission
30: notice identical to this one except for the removal of this paragraph
31: (this paragraph not being relevant to the printed manual).
32:
33: @end ignore
34: Permission is granted to copy and distribute modified versions of this
35: manual under the conditions for verbatim copying, provided also that the
36: sections entitled "Distribution" and "General Public License" are
37: included exactly as in the original, and provided that the entire
38: resulting derived work is distributed under the terms of a permission
39: notice identical to this one.
40:
41: Permission is granted to copy and distribute translations of this manual
42: into another language, under the above conditions for modified versions,
43: except that the sections entitled "Distribution" and "General Public
44: License" may be included in a translation approved by the author instead
45: of in the original English.
46: @end ifinfo
47:
48: @finalout
49: @titlepage
50: @sp 10
51: @center @titlefont{Gforth Manual}
52: @sp 2
1.5 ! anton 53: @center for version 0.4
1.1 anton 54: @sp 2
55: @center Anton Ertl
1.5 ! anton 56: @center Jens Wilke
1.1 anton 57: @center Bernd Paysan
58: @sp 3
59: @center This manual is under construction
60:
61: @comment The following two commands start the copyright page.
62: @page
63: @vskip 0pt plus 1filll
64: Copyright @copyright{} 1995--1997 Free Software Foundation, Inc.
65:
66: @comment !! Published by ... or You can get a copy of this manual ...
67:
68: Permission is granted to make and distribute verbatim copies of
69: this manual provided the copyright notice and this permission notice
70: are preserved on all copies.
71:
72: Permission is granted to copy and distribute modified versions of this
73: manual under the conditions for verbatim copying, provided also that the
74: sections entitled "Distribution" and "General Public License" are
75: included exactly as in the original, and provided that the entire
76: resulting derived work is distributed under the terms of a permission
77: notice identical to this one.
78:
79: Permission is granted to copy and distribute translations of this manual
80: into another language, under the above conditions for modified versions,
81: except that the sections entitled "Distribution" and "General Public
82: License" may be included in a translation approved by the author instead
83: of in the original English.
84: @end titlepage
85:
86:
87: @node Top, License, (dir), (dir)
88: @ifinfo
89: Gforth is a free implementation of ANS Forth available on many
1.5 ! anton 90: personal machines. This manual corresponds to version 0.4.
1.1 anton 91: @end ifinfo
92:
93: @menu
94: * License::
95: * Goals:: About the Gforth Project
96: * Other Books:: Things you might want to read
97: * Invoking Gforth:: Starting Gforth
98: * Words:: Forth words available in Gforth
99: * Tools:: Programming tools
100: * ANS conformance:: Implementation-defined options etc.
101: * Model:: The abstract machine of Gforth
102: * Integrating Gforth:: Forth as scripting language for applications
103: * Emacs and Gforth:: The Gforth Mode
104: * Image Files:: @code{.fi} files contain compiled code
105: * Engine:: The inner interpreter and the primitives
106: * Bugs:: How to report them
107: * Origin:: Authors and ancestors of Gforth
108: * Word Index:: An item for each Forth word
109: * Concept Index:: A menu covering many topics
110: @end menu
111:
112: @node License, Goals, Top, Top
113: @unnumbered GNU GENERAL PUBLIC LICENSE
114: @center Version 2, June 1991
115:
116: @display
117: Copyright @copyright{} 1989, 1991 Free Software Foundation, Inc.
118: 675 Mass Ave, Cambridge, MA 02139, USA
119:
120: Everyone is permitted to copy and distribute verbatim copies
121: of this license document, but changing it is not allowed.
122: @end display
123:
124: @unnumberedsec Preamble
125:
126: The licenses for most software are designed to take away your
127: freedom to share and change it. By contrast, the GNU General Public
128: License is intended to guarantee your freedom to share and change free
129: software---to make sure the software is free for all its users. This
130: General Public License applies to most of the Free Software
131: Foundation's software and to any other program whose authors commit to
132: using it. (Some other Free Software Foundation software is covered by
133: the GNU Library General Public License instead.) You can apply it to
134: your programs, too.
135:
136: When we speak of free software, we are referring to freedom, not
137: price. Our General Public Licenses are designed to make sure that you
138: have the freedom to distribute copies of free software (and charge for
139: this service if you wish), that you receive source code or can get it
140: if you want it, that you can change the software or use pieces of it
141: in new free programs; and that you know you can do these things.
142:
143: To protect your rights, we need to make restrictions that forbid
144: anyone to deny you these rights or to ask you to surrender the rights.
145: These restrictions translate to certain responsibilities for you if you
146: distribute copies of the software, or if you modify it.
147:
148: For example, if you distribute copies of such a program, whether
149: gratis or for a fee, you must give the recipients all the rights that
150: you have. You must make sure that they, too, receive or can get the
151: source code. And you must show them these terms so they know their
152: rights.
153:
154: We protect your rights with two steps: (1) copyright the software, and
155: (2) offer you this license which gives you legal permission to copy,
156: distribute and/or modify the software.
157:
158: Also, for each author's protection and ours, we want to make certain
159: that everyone understands that there is no warranty for this free
160: software. If the software is modified by someone else and passed on, we
161: want its recipients to know that what they have is not the original, so
162: that any problems introduced by others will not reflect on the original
163: authors' reputations.
164:
165: Finally, any free program is threatened constantly by software
166: patents. We wish to avoid the danger that redistributors of a free
167: program will individually obtain patent licenses, in effect making the
168: program proprietary. To prevent this, we have made it clear that any
169: patent must be licensed for everyone's free use or not licensed at all.
170:
171: The precise terms and conditions for copying, distribution and
172: modification follow.
173:
174: @iftex
175: @unnumberedsec TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
176: @end iftex
177: @ifinfo
178: @center TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
179: @end ifinfo
180:
181: @enumerate 0
182: @item
183: This License applies to any program or other work which contains
184: a notice placed by the copyright holder saying it may be distributed
185: under the terms of this General Public License. The ``Program'', below,
186: refers to any such program or work, and a ``work based on the Program''
187: means either the Program or any derivative work under copyright law:
188: that is to say, a work containing the Program or a portion of it,
189: either verbatim or with modifications and/or translated into another
190: language. (Hereinafter, translation is included without limitation in
191: the term ``modification''.) Each licensee is addressed as ``you''.
192:
193: Activities other than copying, distribution and modification are not
194: covered by this License; they are outside its scope. The act of
195: running the Program is not restricted, and the output from the Program
196: is covered only if its contents constitute a work based on the
197: Program (independent of having been made by running the Program).
198: Whether that is true depends on what the Program does.
199:
200: @item
201: You may copy and distribute verbatim copies of the Program's
202: source code as you receive it, in any medium, provided that you
203: conspicuously and appropriately publish on each copy an appropriate
204: copyright notice and disclaimer of warranty; keep intact all the
205: notices that refer to this License and to the absence of any warranty;
206: and give any other recipients of the Program a copy of this License
207: along with the Program.
208:
209: You may charge a fee for the physical act of transferring a copy, and
210: you may at your option offer warranty protection in exchange for a fee.
211:
212: @item
213: You may modify your copy or copies of the Program or any portion
214: of it, thus forming a work based on the Program, and copy and
215: distribute such modifications or work under the terms of Section 1
216: above, provided that you also meet all of these conditions:
217:
218: @enumerate a
219: @item
220: You must cause the modified files to carry prominent notices
221: stating that you changed the files and the date of any change.
222:
223: @item
224: You must cause any work that you distribute or publish, that in
225: whole or in part contains or is derived from the Program or any
226: part thereof, to be licensed as a whole at no charge to all third
227: parties under the terms of this License.
228:
229: @item
230: If the modified program normally reads commands interactively
231: when run, you must cause it, when started running for such
232: interactive use in the most ordinary way, to print or display an
233: announcement including an appropriate copyright notice and a
234: notice that there is no warranty (or else, saying that you provide
235: a warranty) and that users may redistribute the program under
236: these conditions, and telling the user how to view a copy of this
237: License. (Exception: if the Program itself is interactive but
238: does not normally print such an announcement, your work based on
239: the Program is not required to print an announcement.)
240: @end enumerate
241:
242: These requirements apply to the modified work as a whole. If
243: identifiable sections of that work are not derived from the Program,
244: and can be reasonably considered independent and separate works in
245: themselves, then this License, and its terms, do not apply to those
246: sections when you distribute them as separate works. But when you
247: distribute the same sections as part of a whole which is a work based
248: on the Program, the distribution of the whole must be on the terms of
249: this License, whose permissions for other licensees extend to the
250: entire whole, and thus to each and every part regardless of who wrote it.
251:
252: Thus, it is not the intent of this section to claim rights or contest
253: your rights to work written entirely by you; rather, the intent is to
254: exercise the right to control the distribution of derivative or
255: collective works based on the Program.
256:
257: In addition, mere aggregation of another work not based on the Program
258: with the Program (or with a work based on the Program) on a volume of
259: a storage or distribution medium does not bring the other work under
260: the scope of this License.
261:
262: @item
263: You may copy and distribute the Program (or a work based on it,
264: under Section 2) in object code or executable form under the terms of
265: Sections 1 and 2 above provided that you also do one of the following:
266:
267: @enumerate a
268: @item
269: Accompany it with the complete corresponding machine-readable
270: source code, which must be distributed under the terms of Sections
271: 1 and 2 above on a medium customarily used for software interchange; or,
272:
273: @item
274: Accompany it with a written offer, valid for at least three
275: years, to give any third party, for a charge no more than your
276: cost of physically performing source distribution, a complete
277: machine-readable copy of the corresponding source code, to be
278: distributed under the terms of Sections 1 and 2 above on a medium
279: customarily used for software interchange; or,
280:
281: @item
282: Accompany it with the information you received as to the offer
283: to distribute corresponding source code. (This alternative is
284: allowed only for noncommercial distribution and only if you
285: received the program in object code or executable form with such
286: an offer, in accord with Subsection b above.)
287: @end enumerate
288:
289: The source code for a work means the preferred form of the work for
290: making modifications to it. For an executable work, complete source
291: code means all the source code for all modules it contains, plus any
292: associated interface definition files, plus the scripts used to
293: control compilation and installation of the executable. However, as a
294: special exception, the source code distributed need not include
295: anything that is normally distributed (in either source or binary
296: form) with the major components (compiler, kernel, and so on) of the
297: operating system on which the executable runs, unless that component
298: itself accompanies the executable.
299:
300: If distribution of executable or object code is made by offering
301: access to copy from a designated place, then offering equivalent
302: access to copy the source code from the same place counts as
303: distribution of the source code, even though third parties are not
304: compelled to copy the source along with the object code.
305:
306: @item
307: You may not copy, modify, sublicense, or distribute the Program
308: except as expressly provided under this License. Any attempt
309: otherwise to copy, modify, sublicense or distribute the Program is
310: void, and will automatically terminate your rights under this License.
311: However, parties who have received copies, or rights, from you under
312: this License will not have their licenses terminated so long as such
313: parties remain in full compliance.
314:
315: @item
316: You are not required to accept this License, since you have not
317: signed it. However, nothing else grants you permission to modify or
318: distribute the Program or its derivative works. These actions are
319: prohibited by law if you do not accept this License. Therefore, by
320: modifying or distributing the Program (or any work based on the
321: Program), you indicate your acceptance of this License to do so, and
322: all its terms and conditions for copying, distributing or modifying
323: the Program or works based on it.
324:
325: @item
326: Each time you redistribute the Program (or any work based on the
327: Program), the recipient automatically receives a license from the
328: original licensor to copy, distribute or modify the Program subject to
329: these terms and conditions. You may not impose any further
330: restrictions on the recipients' exercise of the rights granted herein.
331: You are not responsible for enforcing compliance by third parties to
332: this License.
333:
334: @item
335: If, as a consequence of a court judgment or allegation of patent
336: infringement or for any other reason (not limited to patent issues),
337: conditions are imposed on you (whether by court order, agreement or
338: otherwise) that contradict the conditions of this License, they do not
339: excuse you from the conditions of this License. If you cannot
340: distribute so as to satisfy simultaneously your obligations under this
341: License and any other pertinent obligations, then as a consequence you
342: may not distribute the Program at all. For example, if a patent
343: license would not permit royalty-free redistribution of the Program by
344: all those who receive copies directly or indirectly through you, then
345: the only way you could satisfy both it and this License would be to
346: refrain entirely from distribution of the Program.
347:
348: If any portion of this section is held invalid or unenforceable under
349: any particular circumstance, the balance of the section is intended to
350: apply and the section as a whole is intended to apply in other
351: circumstances.
352:
353: It is not the purpose of this section to induce you to infringe any
354: patents or other property right claims or to contest validity of any
355: such claims; this section has the sole purpose of protecting the
356: integrity of the free software distribution system, which is
357: implemented by public license practices. Many people have made
358: generous contributions to the wide range of software distributed
359: through that system in reliance on consistent application of that
360: system; it is up to the author/donor to decide if he or she is willing
361: to distribute software through any other system and a licensee cannot
362: impose that choice.
363:
364: This section is intended to make thoroughly clear what is believed to
365: be a consequence of the rest of this License.
366:
367: @item
368: If the distribution and/or use of the Program is restricted in
369: certain countries either by patents or by copyrighted interfaces, the
370: original copyright holder who places the Program under this License
371: may add an explicit geographical distribution limitation excluding
372: those countries, so that distribution is permitted only in or among
373: countries not thus excluded. In such case, this License incorporates
374: the limitation as if written in the body of this License.
375:
376: @item
377: The Free Software Foundation may publish revised and/or new versions
378: of the General Public License from time to time. Such new versions will
379: be similar in spirit to the present version, but may differ in detail to
380: address new problems or concerns.
381:
382: Each version is given a distinguishing version number. If the Program
383: specifies a version number of this License which applies to it and ``any
384: later version'', you have the option of following the terms and conditions
385: either of that version or of any later version published by the Free
386: Software Foundation. If the Program does not specify a version number of
387: this License, you may choose any version ever published by the Free Software
388: Foundation.
389:
390: @item
391: If you wish to incorporate parts of the Program into other free
392: programs whose distribution conditions are different, write to the author
393: to ask for permission. For software which is copyrighted by the Free
394: Software Foundation, write to the Free Software Foundation; we sometimes
395: make exceptions for this. Our decision will be guided by the two goals
396: of preserving the free status of all derivatives of our free software and
397: of promoting the sharing and reuse of software generally.
398:
399: @iftex
400: @heading NO WARRANTY
401: @end iftex
402: @ifinfo
403: @center NO WARRANTY
404: @end ifinfo
405:
406: @item
407: BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
408: FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
409: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
410: PROVIDE THE PROGRAM ``AS IS'' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
411: OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
412: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
413: TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
414: PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
415: REPAIR OR CORRECTION.
416:
417: @item
418: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
419: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
420: REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
421: INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
422: OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
423: TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
424: YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
425: PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
426: POSSIBILITY OF SUCH DAMAGES.
427: @end enumerate
428:
429: @iftex
430: @heading END OF TERMS AND CONDITIONS
431: @end iftex
432: @ifinfo
433: @center END OF TERMS AND CONDITIONS
434: @end ifinfo
435:
436: @page
437: @unnumberedsec How to Apply These Terms to Your New Programs
438:
439: If you develop a new program, and you want it to be of the greatest
440: possible use to the public, the best way to achieve this is to make it
441: free software which everyone can redistribute and change under these terms.
442:
443: To do so, attach the following notices to the program. It is safest
444: to attach them to the start of each source file to most effectively
445: convey the exclusion of warranty; and each file should have at least
446: the ``copyright'' line and a pointer to where the full notice is found.
447:
448: @smallexample
449: @var{one line to give the program's name and a brief idea of what it does.}
450: Copyright (C) 19@var{yy} @var{name of author}
451:
452: This program is free software; you can redistribute it and/or modify
453: it under the terms of the GNU General Public License as published by
454: the Free Software Foundation; either version 2 of the License, or
455: (at your option) any later version.
456:
457: This program is distributed in the hope that it will be useful,
458: but WITHOUT ANY WARRANTY; without even the implied warranty of
459: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
460: GNU General Public License for more details.
461:
462: You should have received a copy of the GNU General Public License
463: along with this program; if not, write to the Free Software
464: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
465: @end smallexample
466:
467: Also add information on how to contact you by electronic and paper mail.
468:
469: If the program is interactive, make it output a short notice like this
470: when it starts in an interactive mode:
471:
472: @smallexample
473: Gnomovision version 69, Copyright (C) 19@var{yy} @var{name of author}
474: Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
475: type `show w'.
476: This is free software, and you are welcome to redistribute it
477: under certain conditions; type `show c' for details.
478: @end smallexample
479:
480: The hypothetical commands @samp{show w} and @samp{show c} should show
481: the appropriate parts of the General Public License. Of course, the
482: commands you use may be called something other than @samp{show w} and
483: @samp{show c}; they could even be mouse-clicks or menu items---whatever
484: suits your program.
485:
486: You should also get your employer (if you work as a programmer) or your
487: school, if any, to sign a ``copyright disclaimer'' for the program, if
488: necessary. Here is a sample; alter the names:
489:
490: @smallexample
491: Yoyodyne, Inc., hereby disclaims all copyright interest in the program
492: `Gnomovision' (which makes passes at compilers) written by James Hacker.
493:
494: @var{signature of Ty Coon}, 1 April 1989
495: Ty Coon, President of Vice
496: @end smallexample
497:
498: This General Public License does not permit incorporating your program into
499: proprietary programs. If your program is a subroutine library, you may
500: consider it more useful to permit linking proprietary applications with the
501: library. If this is what you want to do, use the GNU Library General
502: Public License instead of this License.
503:
504: @iftex
505: @unnumbered Preface
506: @cindex Preface
507: This manual documents Gforth. The reader is expected to know
508: Forth. This manual is primarily a reference manual. @xref{Other Books}
509: for introductory material.
510: @end iftex
511:
512: @node Goals, Other Books, License, Top
513: @comment node-name, next, previous, up
514: @chapter Goals of Gforth
515: @cindex Goals
516: The goal of the Gforth Project is to develop a standard model for
517: ANS Forth. This can be split into several subgoals:
518:
519: @itemize @bullet
520: @item
521: Gforth should conform to the Forth standard (ANS Forth).
522: @item
523: It should be a model, i.e. it should define all the
524: implementation-dependent things.
525: @item
526: It should become standard, i.e. widely accepted and used. This goal
527: is the most difficult one.
528: @end itemize
529:
530: To achieve these goals Gforth should be
531: @itemize @bullet
532: @item
533: Similar to previous models (fig-Forth, F83)
534: @item
535: Powerful. It should provide for all the things that are considered
536: necessary today and even some that are not yet considered necessary.
537: @item
538: Efficient. It should not get the reputation of being exceptionally
539: slow.
540: @item
541: Free.
542: @item
543: Available on many machines/easy to port.
544: @end itemize
545:
546: Have we achieved these goals? Gforth conforms to the ANS Forth
547: standard. It may be considered a model, but we have not yet documented
548: which parts of the model are stable and which parts we are likely to
549: change. It certainly has not yet become a de facto standard. It has some
550: similarities and some differences to previous models. It has some
551: powerful features, but not yet everything that we envisioned. We
552: certainly have achieved our execution speed goals (@pxref{Performance}).
553: It is free and available on many machines.
554:
555: @node Other Books, Invoking Gforth, Goals, Top
556: @chapter Other books on ANS Forth
557: @cindex books on Forth
558:
559: As the standard is relatively new, there are not many books out yet. It
560: is not recommended to learn Forth by using Gforth and a book that is
561: not written for ANS Forth, as you will not know your mistakes from the
562: deviations of the book.
563:
564: @cindex standard document for ANS Forth
565: @cindex ANS Forth document
566: There is, of course, the standard, the definite reference if you want to
567: write ANS Forth programs. It is available in printed form from the
568: National Standards Institute Sales Department (Tel.: USA (212) 642-4900;
569: Fax.: USA (212) 302-1286) as document @cite{X3.215-1994} for about $200. You
570: can also get it from Global Engineering Documents (Tel.: USA (800)
571: 854-7179; Fax.: (303) 843-9880) for about $300.
572:
573: @cite{dpANS6}, the last draft of the standard, which was then submitted to ANSI
574: for publication is available electronically and for free in some MS Word
575: format, and it has been converted to HTML. Some pointers to these
576: versions can be found through
577: @*@url{http://www.complang.tuwien.ac.at/projects/forth.html}.
578:
579: @cindex introductory book
580: @cindex book, introductory
581: @cindex Woehr, Jack: @cite{Forth: The New Model}
582: @cindex @cite{Forth: The new model} (book)
583: @cite{Forth: The New Model} by Jack Woehr (Prentice-Hall, 1993) is an
584: introductory book based on a draft version of the standard. It does not
585: cover the whole standard. It also contains interesting background
586: information (Jack Woehr was in the ANS Forth Technical Committee). It is
587: not appropriate for complete newbies, but programmers experienced in
588: other languages should find it ok.
589:
590: @node Invoking Gforth, Words, Other Books, Top
591: @chapter Invoking Gforth
592: @cindex invoking Gforth
593: @cindex running Gforth
594: @cindex command-line options
595: @cindex options on the command line
596: @cindex flags on the command line
597:
598: You will usually just say @code{gforth}. In many other cases the default
599: Gforth image will be invoked like this:
600:
601: @example
602: gforth [files] [-e forth-code]
603: @end example
604:
605: executing the contents of the files and the Forth code in the order they
606: are given.
607:
608: In general, the command line looks like this:
609:
610: @example
611: gforth [initialization options] [image-specific options]
612: @end example
613:
614: The initialization options must come before the rest of the command
615: line. They are:
616:
617: @table @code
618: @cindex -i, command-line option
619: @cindex --image-file, command-line option
620: @item --image-file @var{file}
621: @itemx -i @var{file}
622: Loads the Forth image @var{file} instead of the default
623: @file{gforth.fi} (@pxref{Image Files}).
624:
625: @cindex --path, command-line option
626: @cindex -p, command-line option
627: @item --path @var{path}
628: @itemx -p @var{path}
629: Uses @var{path} for searching the image file and Forth source code files
630: instead of the default in the environment variable @code{GFORTHPATH} or
631: the path specified at installation time (e.g.,
632: @file{/usr/local/share/gforth/0.2.0:.}). A path is given as a list of
633: directories, separated by @samp{:} (on Unix) or @samp{;} (on other OSs).
634:
635: @cindex --dictionary-size, command-line option
636: @cindex -m, command-line option
637: @cindex @var{size} parameters for command-line options
638: @cindex size of the dictionary and the stacks
639: @item --dictionary-size @var{size}
640: @itemx -m @var{size}
641: Allocate @var{size} space for the Forth dictionary space instead of
642: using the default specified in the image (typically 256K). The
643: @var{size} specification consists of an integer and a unit (e.g.,
644: @code{4M}). The unit can be one of @code{b} (bytes), @code{e} (element
645: size, in this case Cells), @code{k} (kilobytes), and @code{M}
646: (Megabytes). If no unit is specified, @code{e} is used.
647:
648: @cindex --data-stack-size, command-line option
649: @cindex -d, command-line option
650: @item --data-stack-size @var{size}
651: @itemx -d @var{size}
652: Allocate @var{size} space for the data stack instead of using the
653: default specified in the image (typically 16K).
654:
655: @cindex --return-stack-size, command-line option
656: @cindex -r, command-line option
657: @item --return-stack-size @var{size}
658: @itemx -r @var{size}
659: Allocate @var{size} space for the return stack instead of using the
660: default specified in the image (typically 15K).
661:
662: @cindex --fp-stack-size, command-line option
663: @cindex -f, command-line option
664: @item --fp-stack-size @var{size}
665: @itemx -f @var{size}
666: Allocate @var{size} space for the floating point stack instead of
667: using the default specified in the image (typically 15.5K). In this case
668: the unit specifier @code{e} refers to floating point numbers.
669:
670: @cindex --locals-stack-size, command-line option
671: @cindex -l, command-line option
672: @item --locals-stack-size @var{size}
673: @itemx -l @var{size}
674: Allocate @var{size} space for the locals stack instead of using the
675: default specified in the image (typically 14.5K).
676:
677: @cindex -h, command-line option
678: @cindex --help, command-line option
679: @item --help
680: @itemx -h
681: Print a message about the command-line options
682:
683: @cindex -v, command-line option
684: @cindex --version, command-line option
685: @item --version
686: @itemx -v
687: Print version and exit
688:
689: @cindex --debug, command-line option
690: @item --debug
691: Print some information useful for debugging on startup.
692:
693: @cindex --offset-image, command-line option
694: @item --offset-image
695: Start the dictionary at a slightly different position than would be used
696: otherwise (useful for creating data-relocatable images,
697: @pxref{Data-Relocatable Image Files}).
698:
1.5 ! anton 699: @cindex --no-offset-im, command-line option
! 700: @item --no-offset-im
! 701: Start the dictionary at the normal position.
! 702:
1.1 anton 703: @cindex --clear-dictionary, command-line option
704: @item --clear-dictionary
705: Initialize all bytes in the dictionary to 0 before loading the image
706: (@pxref{Data-Relocatable Image Files}).
1.5 ! anton 707:
! 708: @cindex --die-on-signal, command-line-option
! 709: @item --die-on-signal
! 710: Normally Gforth handles most signals (e.g., the user interrupt SIGINT,
! 711: or the segmentation violation SIGSEGV) by translating it into a Forth
! 712: @code{THROW}. With this option, Gforth exits if it receives such a
! 713: signal. This option is useful when the engine and/or the image might be
! 714: severely broken (such that it causes another signal before recovering
! 715: from the first); this option avoids endless loops in such cases.
1.1 anton 716: @end table
717:
718: @cindex loading files at startup
719: @cindex executing code on startup
720: @cindex batch processing with Gforth
721: As explained above, the image-specific command-line arguments for the
722: default image @file{gforth.fi} consist of a sequence of filenames and
723: @code{-e @var{forth-code}} options that are interpreted in the sequence
724: in which they are given. The @code{-e @var{forth-code}} or
725: @code{--evaluate @var{forth-code}} option evaluates the forth
726: code. This option takes only one argument; if you want to evaluate more
727: Forth words, you have to quote them or use several @code{-e}s. To exit
728: after processing the command line (instead of entering interactive mode)
729: append @code{-e bye} to the command line.
730:
731: @cindex versions, invoking other versions of Gforth
732: If you have several versions of Gforth installed, @code{gforth} will
733: invoke the version that was installed last. @code{gforth-@var{version}}
734: invokes a specific version. You may want to use the option
735: @code{--path}, if your environment contains the variable
736: @code{GFORTHPATH}.
737:
738: Not yet implemented:
739: On startup the system first executes the system initialization file
740: (unless the option @code{--no-init-file} is given; note that the system
741: resulting from using this option may not be ANS Forth conformant). Then
742: the user initialization file @file{.gforth.fs} is executed, unless the
743: option @code{--no-rc} is given; this file is first searched in @file{.},
744: then in @file{~}, then in the normal path (see above).
745:
746: @node Words, Tools, Invoking Gforth, Top
747: @chapter Forth Words
748: @cindex Words
749:
750: @menu
751: * Notation::
752: * Arithmetic::
753: * Stack Manipulation::
1.5 ! anton 754: * Memory::
1.1 anton 755: * Control Structures::
756: * Locals::
757: * Defining Words::
1.5 ! anton 758: * Structures::
! 759: * Objects::
1.1 anton 760: * Tokens for Words::
761: * Wordlists::
762: * Files::
763: * Blocks::
764: * Other I/O::
765: * Programming Tools::
766: * Assembler and Code words::
767: * Threading Words::
1.5 ! anton 768: * Including Files::
1.1 anton 769: @end menu
770:
771: @node Notation, Arithmetic, Words, Words
772: @section Notation
773: @cindex notation of glossary entries
774: @cindex format of glossary entries
775: @cindex glossary notation format
776: @cindex word glossary entry format
777:
778: The Forth words are described in this section in the glossary notation
779: that has become a de-facto standard for Forth texts, i.e.,
780:
781: @format
782: @var{word} @var{Stack effect} @var{wordset} @var{pronunciation}
783: @end format
784: @var{Description}
785:
786: @table @var
787: @item word
788: @cindex case insensitivity
789: The name of the word. BTW, Gforth is case insensitive, so you can
790: type the words in in lower case (However, @pxref{core-idef}).
791:
792: @item Stack effect
793: @cindex stack effect
794: The stack effect is written in the notation @code{@var{before} --
795: @var{after}}, where @var{before} and @var{after} describe the top of
796: stack entries before and after the execution of the word. The rest of
797: the stack is not touched by the word. The top of stack is rightmost,
798: i.e., a stack sequence is written as it is typed in. Note that Gforth
799: uses a separate floating point stack, but a unified stack
800: notation. Also, return stack effects are not shown in @var{stack
801: effect}, but in @var{Description}. The name of a stack item describes
802: the type and/or the function of the item. See below for a discussion of
803: the types.
804:
805: All words have two stack effects: A compile-time stack effect and a
806: run-time stack effect. The compile-time stack-effect of most words is
807: @var{ -- }. If the compile-time stack-effect of a word deviates from
808: this standard behaviour, or the word does other unusual things at
809: compile time, both stack effects are shown; otherwise only the run-time
810: stack effect is shown.
811:
812: @cindex pronounciation of words
813: @item pronunciation
814: How the word is pronounced.
815:
816: @cindex wordset
817: @item wordset
818: The ANS Forth standard is divided into several wordsets. A standard
819: system need not support all of them. So, the fewer wordsets your program
820: uses the more portable it will be in theory. However, we suspect that
821: most ANS Forth systems on personal machines will feature all
822: wordsets. Words that are not defined in the ANS standard have
823: @code{gforth} or @code{gforth-internal} as wordset. @code{gforth}
824: describes words that will work in future releases of Gforth;
825: @code{gforth-internal} words are more volatile. Environmental query
826: strings are also displayed like words; you can recognize them by the
827: @code{environment} in the wordset field.
828:
829: @item Description
830: A description of the behaviour of the word.
831: @end table
832:
833: @cindex types of stack items
834: @cindex stack item types
835: The type of a stack item is specified by the character(s) the name
836: starts with:
837:
838: @table @code
839: @item f
840: @cindex @code{f}, stack item type
841: Boolean flags, i.e. @code{false} or @code{true}.
842: @item c
843: @cindex @code{c}, stack item type
844: Char
845: @item w
846: @cindex @code{w}, stack item type
847: Cell, can contain an integer or an address
848: @item n
849: @cindex @code{n}, stack item type
850: signed integer
851: @item u
852: @cindex @code{u}, stack item type
853: unsigned integer
854: @item d
855: @cindex @code{d}, stack item type
856: double sized signed integer
857: @item ud
858: @cindex @code{ud}, stack item type
859: double sized unsigned integer
860: @item r
861: @cindex @code{r}, stack item type
862: Float (on the FP stack)
863: @item a_
864: @cindex @code{a_}, stack item type
865: Cell-aligned address
866: @item c_
867: @cindex @code{c_}, stack item type
868: Char-aligned address (note that a Char may have two bytes in Windows NT)
869: @item f_
870: @cindex @code{f_}, stack item type
871: Float-aligned address
872: @item df_
873: @cindex @code{df_}, stack item type
874: Address aligned for IEEE double precision float
875: @item sf_
876: @cindex @code{sf_}, stack item type
877: Address aligned for IEEE single precision float
878: @item xt
879: @cindex @code{xt}, stack item type
880: Execution token, same size as Cell
881: @item wid
882: @cindex @code{wid}, stack item type
883: Wordlist ID, same size as Cell
884: @item f83name
885: @cindex @code{f83name}, stack item type
886: Pointer to a name structure
887: @item "
888: @cindex @code{"}, stack item type
889: string in the input stream (not the stack). The terminating character is
890: a blank by default. If it is not a blank, it is shown in @code{<>}
891: quotes.
892: @end table
893:
894: @node Arithmetic, Stack Manipulation, Notation, Words
895: @section Arithmetic
896: @cindex arithmetic words
897:
898: @cindex division with potentially negative operands
899: Forth arithmetic is not checked, i.e., you will not hear about integer
900: overflow on addition or multiplication, you may hear about division by
901: zero if you are lucky. The operator is written after the operands, but
902: the operands are still in the original order. I.e., the infix @code{2-1}
903: corresponds to @code{2 1 -}. Forth offers a variety of division
904: operators. If you perform division with potentially negative operands,
905: you do not want to use @code{/} or @code{/mod} with its undefined
906: behaviour, but rather @code{fm/mod} or @code{sm/mod} (probably the
907: former, @pxref{Mixed precision}).
908:
909: @menu
910: * Single precision::
911: * Bitwise operations::
912: * Mixed precision:: operations with single and double-cell integers
913: * Double precision:: Double-cell integer arithmetic
914: * Floating Point::
915: @end menu
916:
917: @node Single precision, Bitwise operations, Arithmetic, Arithmetic
918: @subsection Single precision
919: @cindex single precision arithmetic words
920:
921: doc-+
922: doc--
923: doc-*
924: doc-/
925: doc-mod
926: doc-/mod
927: doc-negate
928: doc-abs
929: doc-min
930: doc-max
931:
932: @node Bitwise operations, Mixed precision, Single precision, Arithmetic
933: @subsection Bitwise operations
934: @cindex bitwise operation words
935:
936: doc-and
937: doc-or
938: doc-xor
939: doc-invert
940: doc-2*
941: doc-2/
942:
943: @node Mixed precision, Double precision, Bitwise operations, Arithmetic
944: @subsection Mixed precision
945: @cindex mixed precision arithmetic words
946:
947: doc-m+
948: doc-*/
949: doc-*/mod
950: doc-m*
951: doc-um*
952: doc-m*/
953: doc-um/mod
954: doc-fm/mod
955: doc-sm/rem
956:
957: @node Double precision, Floating Point, Mixed precision, Arithmetic
958: @subsection Double precision
959: @cindex double precision arithmetic words
960:
961: @cindex double-cell numbers, input format
962: @cindex input format for double-cell numbers
963: The outer (aka text) interpreter converts numbers containing a dot into
964: a double precision number. Note that only numbers with the dot as last
965: character are standard-conforming.
966:
967: doc-d+
968: doc-d-
969: doc-dnegate
970: doc-dabs
971: doc-dmin
972: doc-dmax
973:
974: @node Floating Point, , Double precision, Arithmetic
975: @subsection Floating Point
976: @cindex floating point arithmetic words
977:
978: @cindex floating-point numbers, input format
979: @cindex input format for floating-point numbers
980: The format of floating point numbers recognized by the outer (aka text)
981: interpreter is: a signed decimal number, possibly containing a decimal
982: point (@code{.}), followed by @code{E} or @code{e}, optionally followed
983: by a signed integer (the exponent). E.g., @code{1e} is the same as
984: @code{+1.0e+0}. Note that a number without @code{e}
985: is not interpreted as floating-point number, but as double (if the
986: number contains a @code{.}) or single precision integer. Also,
987: conversions between string and floating point numbers always use base
988: 10, irrespective of the value of @code{BASE}. If @code{BASE} contains a
989: value greater then 14, the @code{E} may be interpreted as digit and the
990: number will be interpreted as integer, unless it has a signed exponent
991: (both @code{+} and @code{-} are allowed as signs).
992:
993: @cindex angles in trigonometric operations
994: @cindex trigonometric operations
995: Angles in floating point operations are given in radians (a full circle
996: has 2 pi radians). Note, that Gforth has a separate floating point
997: stack, but we use the unified notation.
998:
999: @cindex floating-point arithmetic, pitfalls
1000: Floating point numbers have a number of unpleasant surprises for the
1001: unwary (e.g., floating point addition is not associative) and even a few
1002: for the wary. You should not use them unless you know what you are doing
1003: or you don't care that the results you get are totally bogus. If you
1004: want to learn about the problems of floating point numbers (and how to
1005: avoid them), you might start with @cite{David Goldberg, What Every
1006: Computer Scientist Should Know About Floating-Point Arithmetic, ACM
1007: Computing Surveys 23(1):5@minus{}48, March 1991}.
1008:
1009: doc-f+
1010: doc-f-
1011: doc-f*
1012: doc-f/
1013: doc-fnegate
1014: doc-fabs
1015: doc-fmax
1016: doc-fmin
1017: doc-floor
1018: doc-fround
1019: doc-f**
1020: doc-fsqrt
1021: doc-fexp
1022: doc-fexpm1
1023: doc-fln
1024: doc-flnp1
1025: doc-flog
1026: doc-falog
1027: doc-fsin
1028: doc-fcos
1029: doc-fsincos
1030: doc-ftan
1031: doc-fasin
1032: doc-facos
1033: doc-fatan
1034: doc-fatan2
1035: doc-fsinh
1036: doc-fcosh
1037: doc-ftanh
1038: doc-fasinh
1039: doc-facosh
1040: doc-fatanh
1041:
1042: @node Stack Manipulation, Memory, Arithmetic, Words
1043: @section Stack Manipulation
1044: @cindex stack manipulation words
1045:
1046: @cindex floating-point stack in the standard
1047: Gforth has a data stack (aka parameter stack) for characters, cells,
1048: addresses, and double cells, a floating point stack for floating point
1049: numbers, a return stack for storing the return addresses of colon
1050: definitions and other data, and a locals stack for storing local
1051: variables. Note that while every sane Forth has a separate floating
1052: point stack, this is not strictly required; an ANS Forth system could
1053: theoretically keep floating point numbers on the data stack. As an
1054: additional difficulty, you don't know how many cells a floating point
1055: number takes. It is reportedly possible to write words in a way that
1056: they work also for a unified stack model, but we do not recommend trying
1057: it. Instead, just say that your program has an environmental dependency
1058: on a separate FP stack.
1059:
1060: @cindex return stack and locals
1061: @cindex locals and return stack
1062: Also, a Forth system is allowed to keep the local variables on the
1063: return stack. This is reasonable, as local variables usually eliminate
1064: the need to use the return stack explicitly. So, if you want to produce
1065: a standard complying program and if you are using local variables in a
1066: word, forget about return stack manipulations in that word (see the
1067: standard document for the exact rules).
1068:
1069: @menu
1070: * Data stack::
1071: * Floating point stack::
1072: * Return stack::
1073: * Locals stack::
1074: * Stack pointer manipulation::
1075: @end menu
1076:
1077: @node Data stack, Floating point stack, Stack Manipulation, Stack Manipulation
1078: @subsection Data stack
1079: @cindex data stack manipulation words
1080: @cindex stack manipulations words, data stack
1081:
1082: doc-drop
1083: doc-nip
1084: doc-dup
1085: doc-over
1086: doc-tuck
1087: doc-swap
1088: doc-rot
1089: doc--rot
1090: doc-?dup
1091: doc-pick
1092: doc-roll
1093: doc-2drop
1094: doc-2nip
1095: doc-2dup
1096: doc-2over
1097: doc-2tuck
1098: doc-2swap
1099: doc-2rot
1100:
1101: @node Floating point stack, Return stack, Data stack, Stack Manipulation
1102: @subsection Floating point stack
1103: @cindex floating-point stack manipulation words
1104: @cindex stack manipulation words, floating-point stack
1105:
1106: doc-fdrop
1107: doc-fnip
1108: doc-fdup
1109: doc-fover
1110: doc-ftuck
1111: doc-fswap
1112: doc-frot
1113:
1114: @node Return stack, Locals stack, Floating point stack, Stack Manipulation
1115: @subsection Return stack
1116: @cindex return stack manipulation words
1117: @cindex stack manipulation words, return stack
1118:
1119: doc->r
1120: doc-r>
1121: doc-r@
1122: doc-rdrop
1123: doc-2>r
1124: doc-2r>
1125: doc-2r@
1126: doc-2rdrop
1127:
1128: @node Locals stack, Stack pointer manipulation, Return stack, Stack Manipulation
1129: @subsection Locals stack
1130:
1131: @node Stack pointer manipulation, , Locals stack, Stack Manipulation
1132: @subsection Stack pointer manipulation
1133: @cindex stack pointer manipulation words
1134:
1135: doc-sp@
1136: doc-sp!
1137: doc-fp@
1138: doc-fp!
1139: doc-rp@
1140: doc-rp!
1141: doc-lp@
1142: doc-lp!
1143:
1144: @node Memory, Control Structures, Stack Manipulation, Words
1145: @section Memory
1146: @cindex Memory words
1147:
1148: @menu
1149: * Memory Access::
1150: * Address arithmetic::
1151: * Memory Blocks::
1152: @end menu
1153:
1154: @node Memory Access, Address arithmetic, Memory, Memory
1155: @subsection Memory Access
1156: @cindex memory access words
1157:
1158: doc-@
1159: doc-!
1160: doc-+!
1161: doc-c@
1162: doc-c!
1163: doc-2@
1164: doc-2!
1165: doc-f@
1166: doc-f!
1167: doc-sf@
1168: doc-sf!
1169: doc-df@
1170: doc-df!
1171:
1172: @node Address arithmetic, Memory Blocks, Memory Access, Memory
1173: @subsection Address arithmetic
1174: @cindex address arithmetic words
1175:
1176: ANS Forth does not specify the sizes of the data types. Instead, it
1177: offers a number of words for computing sizes and doing address
1178: arithmetic. Basically, address arithmetic is performed in terms of
1179: address units (aus); on most systems the address unit is one byte. Note
1180: that a character may have more than one au, so @code{chars} is no noop
1181: (on systems where it is a noop, it compiles to nothing).
1182:
1183: @cindex alignment of addresses for types
1184: ANS Forth also defines words for aligning addresses for specific
1185: types. Many computers require that accesses to specific data types
1186: must only occur at specific addresses; e.g., that cells may only be
1187: accessed at addresses divisible by 4. Even if a machine allows unaligned
1188: accesses, it can usually perform aligned accesses faster.
1189:
1190: For the performance-conscious: alignment operations are usually only
1191: necessary during the definition of a data structure, not during the
1192: (more frequent) accesses to it.
1193:
1194: ANS Forth defines no words for character-aligning addresses. This is not
1195: an oversight, but reflects the fact that addresses that are not
1196: char-aligned have no use in the standard and therefore will not be
1197: created.
1198:
1199: @cindex @code{CREATE} and alignment
1200: The standard guarantees that addresses returned by @code{CREATE}d words
1201: are cell-aligned; in addition, Gforth guarantees that these addresses
1202: are aligned for all purposes.
1203:
1204: Note that the standard defines a word @code{char}, which has nothing to
1205: do with address arithmetic.
1206:
1207: doc-chars
1208: doc-char+
1209: doc-cells
1210: doc-cell+
1211: doc-cell
1212: doc-align
1213: doc-aligned
1214: doc-floats
1215: doc-float+
1216: doc-float
1217: doc-falign
1218: doc-faligned
1219: doc-sfloats
1220: doc-sfloat+
1221: doc-sfalign
1222: doc-sfaligned
1223: doc-dfloats
1224: doc-dfloat+
1225: doc-dfalign
1226: doc-dfaligned
1227: doc-maxalign
1228: doc-maxaligned
1229: doc-cfalign
1230: doc-cfaligned
1231: doc-address-unit-bits
1232:
1233: @node Memory Blocks, , Address arithmetic, Memory
1234: @subsection Memory Blocks
1235: @cindex memory block words
1236:
1237: doc-move
1238: doc-erase
1239:
1240: While the previous words work on address units, the rest works on
1241: characters.
1242:
1243: doc-cmove
1244: doc-cmove>
1245: doc-fill
1246: doc-blank
1247:
1248: @node Control Structures, Locals, Memory, Words
1249: @section Control Structures
1250: @cindex control structures
1251:
1252: Control structures in Forth cannot be used in interpret state, only in
1253: compile state@footnote{More precisely, they have no interpretation
1254: semantics (@pxref{Interpretation and Compilation Semantics})}, i.e., in
1255: a colon definition. We do not like this limitation, but have not seen a
1256: satisfying way around it yet, although many schemes have been proposed.
1257:
1258: @menu
1259: * Selection::
1260: * Simple Loops::
1261: * Counted Loops::
1262: * Arbitrary control structures::
1263: * Calls and returns::
1264: * Exception Handling::
1265: @end menu
1266:
1267: @node Selection, Simple Loops, Control Structures, Control Structures
1268: @subsection Selection
1269: @cindex selection control structures
1270: @cindex control structures for selection
1271:
1272: @cindex @code{IF} control structure
1273: @example
1274: @var{flag}
1275: IF
1276: @var{code}
1277: ENDIF
1278: @end example
1279: or
1280: @example
1281: @var{flag}
1282: IF
1283: @var{code1}
1284: ELSE
1285: @var{code2}
1286: ENDIF
1287: @end example
1288:
1289: You can use @code{THEN} instead of @code{ENDIF}. Indeed, @code{THEN} is
1290: standard, and @code{ENDIF} is not, although it is quite popular. We
1291: recommend using @code{ENDIF}, because it is less confusing for people
1292: who also know other languages (and is not prone to reinforcing negative
1293: prejudices against Forth in these people). Adding @code{ENDIF} to a
1294: system that only supplies @code{THEN} is simple:
1295: @example
1296: : endif POSTPONE then ; immediate
1297: @end example
1298:
1299: [According to @cite{Webster's New Encyclopedic Dictionary}, @dfn{then
1300: (adv.)} has the following meanings:
1301: @quotation
1302: ... 2b: following next after in order ... 3d: as a necessary consequence
1303: (if you were there, then you saw them).
1304: @end quotation
1305: Forth's @code{THEN} has the meaning 2b, whereas @code{THEN} in Pascal
1306: and many other programming languages has the meaning 3d.]
1307:
1308: Gforth also provides the words @code{?dup-if} and @code{?dup-0=-if}, so
1309: you can avoid using @code{?dup}. Using these alternatives is also more
1310: efficient than using @code{?dup}. Definitions in plain standard Forth
1311: for @code{ENDIF}, @code{?DUP-IF} and @code{?DUP-0=-IF} are provided in
1312: @file{compat/control.fs}.
1313:
1314: @cindex @code{CASE} control structure
1315: @example
1316: @var{n}
1317: CASE
1318: @var{n1} OF @var{code1} ENDOF
1319: @var{n2} OF @var{code2} ENDOF
1320: @dots{}
1321: ENDCASE
1322: @end example
1323:
1324: Executes the first @var{codei}, where the @var{ni} is equal to
1325: @var{n}. A default case can be added by simply writing the code after
1326: the last @code{ENDOF}. It may use @var{n}, which is on top of the stack,
1327: but must not consume it.
1328:
1329: @node Simple Loops, Counted Loops, Selection, Control Structures
1330: @subsection Simple Loops
1331: @cindex simple loops
1332: @cindex loops without count
1333:
1334: @cindex @code{WHILE} loop
1335: @example
1336: BEGIN
1337: @var{code1}
1338: @var{flag}
1339: WHILE
1340: @var{code2}
1341: REPEAT
1342: @end example
1343:
1344: @var{code1} is executed and @var{flag} is computed. If it is true,
1345: @var{code2} is executed and the loop is restarted; If @var{flag} is
1346: false, execution continues after the @code{REPEAT}.
1347:
1348: @cindex @code{UNTIL} loop
1349: @example
1350: BEGIN
1351: @var{code}
1352: @var{flag}
1353: UNTIL
1354: @end example
1355:
1356: @var{code} is executed. The loop is restarted if @code{flag} is false.
1357:
1358: @cindex endless loop
1359: @cindex loops, endless
1360: @example
1361: BEGIN
1362: @var{code}
1363: AGAIN
1364: @end example
1365:
1366: This is an endless loop.
1367:
1368: @node Counted Loops, Arbitrary control structures, Simple Loops, Control Structures
1369: @subsection Counted Loops
1370: @cindex counted loops
1371: @cindex loops, counted
1372: @cindex @code{DO} loops
1373:
1374: The basic counted loop is:
1375: @example
1376: @var{limit} @var{start}
1377: ?DO
1378: @var{body}
1379: LOOP
1380: @end example
1381:
1382: This performs one iteration for every integer, starting from @var{start}
1383: and up to, but excluding @var{limit}. The counter, aka index, can be
1384: accessed with @code{i}. E.g., the loop
1385: @example
1386: 10 0 ?DO
1387: i .
1388: LOOP
1389: @end example
1390: prints
1391: @example
1392: 0 1 2 3 4 5 6 7 8 9
1393: @end example
1394: The index of the innermost loop can be accessed with @code{i}, the index
1395: of the next loop with @code{j}, and the index of the third loop with
1396: @code{k}.
1397:
1398: doc-i
1399: doc-j
1400: doc-k
1401:
1402: The loop control data are kept on the return stack, so there are some
1403: restrictions on mixing return stack accesses and counted loop
1404: words. E.g., if you put values on the return stack outside the loop, you
1405: cannot read them inside the loop. If you put values on the return stack
1406: within a loop, you have to remove them before the end of the loop and
1407: before accessing the index of the loop.
1408:
1409: There are several variations on the counted loop:
1410:
1411: @code{LEAVE} leaves the innermost counted loop immediately.
1412:
1413: If @var{start} is greater than @var{limit}, a @code{?DO} loop is entered
1414: (and @code{LOOP} iterates until they become equal by wrap-around
1415: arithmetic). This behaviour is usually not what you want. Therefore,
1416: Gforth offers @code{+DO} and @code{U+DO} (as replacements for
1417: @code{?DO}), which do not enter the loop if @var{start} is greater than
1418: @var{limit}; @code{+DO} is for signed loop parameters, @code{U+DO} for
1419: unsigned loop parameters.
1420:
1421: @code{LOOP} can be replaced with @code{@var{n} +LOOP}; this updates the
1422: index by @var{n} instead of by 1. The loop is terminated when the border
1423: between @var{limit-1} and @var{limit} is crossed. E.g.:
1424:
1425: @code{4 0 +DO i . 2 +LOOP} prints @code{0 2}
1426:
1427: @code{4 1 +DO i . 2 +LOOP} prints @code{1 3}
1428:
1429: @cindex negative increment for counted loops
1430: @cindex counted loops with negative increment
1431: The behaviour of @code{@var{n} +LOOP} is peculiar when @var{n} is negative:
1432:
1433: @code{-1 0 ?DO i . -1 +LOOP} prints @code{0 -1}
1434:
1435: @code{ 0 0 ?DO i . -1 +LOOP} prints nothing
1436:
1437: Therefore we recommend avoiding @code{@var{n} +LOOP} with negative
1438: @var{n}. One alternative is @code{@var{u} -LOOP}, which reduces the
1439: index by @var{u} each iteration. The loop is terminated when the border
1440: between @var{limit+1} and @var{limit} is crossed. Gforth also provides
1441: @code{-DO} and @code{U-DO} for down-counting loops. E.g.:
1442:
1443: @code{-2 0 -DO i . 1 -LOOP} prints @code{0 -1}
1444:
1445: @code{-1 0 -DO i . 1 -LOOP} prints @code{0}
1446:
1447: @code{ 0 0 -DO i . 1 -LOOP} prints nothing
1448:
1449: Unfortunately, @code{+DO}, @code{U+DO}, @code{-DO}, @code{U-DO} and
1450: @code{-LOOP} are not in the ANS Forth standard. However, an
1451: implementation for these words that uses only standard words is provided
1452: in @file{compat/loops.fs}.
1453:
1454: @code{?DO} can also be replaced by @code{DO}. @code{DO} always enters
1455: the loop, independent of the loop parameters. Do not use @code{DO}, even
1456: if you know that the loop is entered in any case. Such knowledge tends
1457: to become invalid during maintenance of a program, and then the
1458: @code{DO} will make trouble.
1459:
1460: @code{UNLOOP} is used to prepare for an abnormal loop exit, e.g., via
1461: @code{EXIT}. @code{UNLOOP} removes the loop control parameters from the
1462: return stack so @code{EXIT} can get to its return address.
1463:
1464: @cindex @code{FOR} loops
1465: Another counted loop is
1466: @example
1467: @var{n}
1468: FOR
1469: @var{body}
1470: NEXT
1471: @end example
1472: This is the preferred loop of native code compiler writers who are too
1473: lazy to optimize @code{?DO} loops properly. In Gforth, this loop
1474: iterates @var{n+1} times; @code{i} produces values starting with @var{n}
1475: and ending with 0. Other Forth systems may behave differently, even if
1476: they support @code{FOR} loops. To avoid problems, don't use @code{FOR}
1477: loops.
1478:
1479: @node Arbitrary control structures, Calls and returns, Counted Loops, Control Structures
1480: @subsection Arbitrary control structures
1481: @cindex control structures, user-defined
1482:
1483: @cindex control-flow stack
1484: ANS Forth permits and supports using control structures in a non-nested
1485: way. Information about incomplete control structures is stored on the
1486: control-flow stack. This stack may be implemented on the Forth data
1487: stack, and this is what we have done in Gforth.
1488:
1489: @cindex @code{orig}, control-flow stack item
1490: @cindex @code{dest}, control-flow stack item
1491: An @i{orig} entry represents an unresolved forward branch, a @i{dest}
1492: entry represents a backward branch target. A few words are the basis for
1493: building any control structure possible (except control structures that
1494: need storage, like calls, coroutines, and backtracking).
1495:
1496: doc-if
1497: doc-ahead
1498: doc-then
1499: doc-begin
1500: doc-until
1501: doc-again
1502: doc-cs-pick
1503: doc-cs-roll
1504:
1505: On many systems control-flow stack items take one word, in Gforth they
1506: currently take three (this may change in the future). Therefore it is a
1507: really good idea to manipulate the control flow stack with
1508: @code{cs-pick} and @code{cs-roll}, not with data stack manipulation
1509: words.
1510:
1511: Some standard control structure words are built from these words:
1512:
1513: doc-else
1514: doc-while
1515: doc-repeat
1516:
1517: Gforth adds some more control-structure words:
1518:
1519: doc-endif
1520: doc-?dup-if
1521: doc-?dup-0=-if
1522:
1523: Counted loop words constitute a separate group of words:
1524:
1525: doc-?do
1526: doc-+do
1527: doc-u+do
1528: doc--do
1529: doc-u-do
1530: doc-do
1531: doc-for
1532: doc-loop
1533: doc-+loop
1534: doc--loop
1535: doc-next
1536: doc-leave
1537: doc-?leave
1538: doc-unloop
1539: doc-done
1540:
1541: The standard does not allow using @code{cs-pick} and @code{cs-roll} on
1542: @i{do-sys}. Our system allows it, but it's your job to ensure that for
1543: every @code{?DO} etc. there is exactly one @code{UNLOOP} on any path
1544: through the definition (@code{LOOP} etc. compile an @code{UNLOOP} on the
1545: fall-through path). Also, you have to ensure that all @code{LEAVE}s are
1546: resolved (by using one of the loop-ending words or @code{DONE}).
1547:
1548: Another group of control structure words are
1549:
1550: doc-case
1551: doc-endcase
1552: doc-of
1553: doc-endof
1554:
1555: @i{case-sys} and @i{of-sys} cannot be processed using @code{cs-pick} and
1556: @code{cs-roll}.
1557:
1558: @subsubsection Programming Style
1559:
1560: In order to ensure readability we recommend that you do not create
1561: arbitrary control structures directly, but define new control structure
1562: words for the control structure you want and use these words in your
1563: program.
1564:
1565: E.g., instead of writing
1566:
1567: @example
1568: begin
1569: ...
1570: if [ 1 cs-roll ]
1571: ...
1572: again then
1573: @end example
1574:
1575: we recommend defining control structure words, e.g.,
1576:
1577: @example
1578: : while ( dest -- orig dest )
1579: POSTPONE if
1580: 1 cs-roll ; immediate
1581:
1582: : repeat ( orig dest -- )
1583: POSTPONE again
1584: POSTPONE then ; immediate
1585: @end example
1586:
1587: and then using these to create the control structure:
1588:
1589: @example
1590: begin
1591: ...
1592: while
1593: ...
1594: repeat
1595: @end example
1596:
1597: That's much easier to read, isn't it? Of course, @code{REPEAT} and
1598: @code{WHILE} are predefined, so in this example it would not be
1599: necessary to define them.
1600:
1601: @node Calls and returns, Exception Handling, Arbitrary control structures, Control Structures
1602: @subsection Calls and returns
1603: @cindex calling a definition
1604: @cindex returning from a definition
1605:
1.3 anton 1606: @cindex recursive definitions
1607: A definition can be called simply be writing the name of the definition
1608: to be called. Note that normally a definition is invisible during its
1609: definition. If you want to write a directly recursive definition, you
1610: can use @code{recursive} to make the current definition visible.
1611:
1612: doc-recursive
1613:
1614: Another way to perform a recursive call is
1615:
1616: doc-recurse
1617:
1618: @c @progstyle{
1619: I prefer using @code{recursive} to @code{recurse}, because
1620: calling the definition by name is more descriptive (if the name is
1621: well-chosen) than the somewhat cryptic @code{recurse}. E.g., in a
1622: quicksort implementation, it is much better to read (and think) ``now
1623: sort the partitions'' than to read ``now do a recursive call''.
1624:
1625: For mutual recursion, use @code{defer}red words, like this:
1626:
1627: @example
1628: defer foo
1629:
1630: : bar ( ... -- ... )
1631: ... foo ... ;
1632:
1633: :noname ( ... -- ... )
1634: ... bar ... ;
1635: IS foo
1636: @end example
1637:
1638: When the end of the definition is reached, it returns. An earlier return
1639: can be forced using
1.1 anton 1640:
1641: doc-exit
1642:
1643: Don't forget to clean up the return stack and @code{UNLOOP} any
1644: outstanding @code{?DO}...@code{LOOP}s before @code{EXIT}ing. The
1645: primitive compiled by @code{EXIT} is
1646:
1647: doc-;s
1648:
1649: @node Exception Handling, , Calls and returns, Control Structures
1650: @subsection Exception Handling
1651: @cindex Exceptions
1652:
1653: doc-catch
1654: doc-throw
1655:
1656: @node Locals, Defining Words, Control Structures, Words
1657: @section Locals
1658: @cindex locals
1659:
1660: Local variables can make Forth programming more enjoyable and Forth
1661: programs easier to read. Unfortunately, the locals of ANS Forth are
1662: laden with restrictions. Therefore, we provide not only the ANS Forth
1663: locals wordset, but also our own, more powerful locals wordset (we
1664: implemented the ANS Forth locals wordset through our locals wordset).
1665:
1666: The ideas in this section have also been published in the paper
1667: @cite{Automatic Scoping of Local Variables} by M. Anton Ertl, presented
1668: at EuroForth '94; it is available at
1669: @*@url{http://www.complang.tuwien.ac.at/papers/ertl94l.ps.gz}.
1670:
1671: @menu
1672: * Gforth locals::
1673: * ANS Forth locals::
1674: @end menu
1675:
1676: @node Gforth locals, ANS Forth locals, Locals, Locals
1677: @subsection Gforth locals
1678: @cindex Gforth locals
1679: @cindex locals, Gforth style
1680:
1681: Locals can be defined with
1682:
1683: @example
1684: @{ local1 local2 ... -- comment @}
1685: @end example
1686: or
1687: @example
1688: @{ local1 local2 ... @}
1689: @end example
1690:
1691: E.g.,
1692: @example
1693: : max @{ n1 n2 -- n3 @}
1694: n1 n2 > if
1695: n1
1696: else
1697: n2
1698: endif ;
1699: @end example
1700:
1701: The similarity of locals definitions with stack comments is intended. A
1702: locals definition often replaces the stack comment of a word. The order
1703: of the locals corresponds to the order in a stack comment and everything
1704: after the @code{--} is really a comment.
1705:
1706: This similarity has one disadvantage: It is too easy to confuse locals
1707: declarations with stack comments, causing bugs and making them hard to
1708: find. However, this problem can be avoided by appropriate coding
1709: conventions: Do not use both notations in the same program. If you do,
1710: they should be distinguished using additional means, e.g. by position.
1711:
1712: @cindex types of locals
1713: @cindex locals types
1714: The name of the local may be preceded by a type specifier, e.g.,
1715: @code{F:} for a floating point value:
1716:
1717: @example
1718: : CX* @{ F: Ar F: Ai F: Br F: Bi -- Cr Ci @}
1719: \ complex multiplication
1720: Ar Br f* Ai Bi f* f-
1721: Ar Bi f* Ai Br f* f+ ;
1722: @end example
1723:
1724: @cindex flavours of locals
1725: @cindex locals flavours
1726: @cindex value-flavoured locals
1727: @cindex variable-flavoured locals
1728: Gforth currently supports cells (@code{W:}, @code{W^}), doubles
1729: (@code{D:}, @code{D^}), floats (@code{F:}, @code{F^}) and characters
1730: (@code{C:}, @code{C^}) in two flavours: a value-flavoured local (defined
1731: with @code{W:}, @code{D:} etc.) produces its value and can be changed
1732: with @code{TO}. A variable-flavoured local (defined with @code{W^} etc.)
1733: produces its address (which becomes invalid when the variable's scope is
1734: left). E.g., the standard word @code{emit} can be defined in terms of
1735: @code{type} like this:
1736:
1737: @example
1738: : emit @{ C^ char* -- @}
1739: char* 1 type ;
1740: @end example
1741:
1742: @cindex default type of locals
1743: @cindex locals, default type
1744: A local without type specifier is a @code{W:} local. Both flavours of
1745: locals are initialized with values from the data or FP stack.
1746:
1747: Currently there is no way to define locals with user-defined data
1748: structures, but we are working on it.
1749:
1750: Gforth allows defining locals everywhere in a colon definition. This
1751: poses the following questions:
1752:
1753: @menu
1754: * Where are locals visible by name?::
1755: * How long do locals live?::
1756: * Programming Style::
1757: * Implementation::
1758: @end menu
1759:
1760: @node Where are locals visible by name?, How long do locals live?, Gforth locals, Gforth locals
1761: @subsubsection Where are locals visible by name?
1762: @cindex locals visibility
1763: @cindex visibility of locals
1764: @cindex scope of locals
1765:
1766: Basically, the answer is that locals are visible where you would expect
1767: it in block-structured languages, and sometimes a little longer. If you
1768: want to restrict the scope of a local, enclose its definition in
1769: @code{SCOPE}...@code{ENDSCOPE}.
1770:
1771: doc-scope
1772: doc-endscope
1773:
1774: These words behave like control structure words, so you can use them
1775: with @code{CS-PICK} and @code{CS-ROLL} to restrict the scope in
1776: arbitrary ways.
1777:
1778: If you want a more exact answer to the visibility question, here's the
1779: basic principle: A local is visible in all places that can only be
1780: reached through the definition of the local@footnote{In compiler
1781: construction terminology, all places dominated by the definition of the
1782: local.}. In other words, it is not visible in places that can be reached
1783: without going through the definition of the local. E.g., locals defined
1784: in @code{IF}...@code{ENDIF} are visible until the @code{ENDIF}, locals
1785: defined in @code{BEGIN}...@code{UNTIL} are visible after the
1786: @code{UNTIL} (until, e.g., a subsequent @code{ENDSCOPE}).
1787:
1788: The reasoning behind this solution is: We want to have the locals
1789: visible as long as it is meaningful. The user can always make the
1790: visibility shorter by using explicit scoping. In a place that can
1791: only be reached through the definition of a local, the meaning of a
1792: local name is clear. In other places it is not: How is the local
1793: initialized at the control flow path that does not contain the
1794: definition? Which local is meant, if the same name is defined twice in
1795: two independent control flow paths?
1796:
1797: This should be enough detail for nearly all users, so you can skip the
1798: rest of this section. If you really must know all the gory details and
1799: options, read on.
1800:
1801: In order to implement this rule, the compiler has to know which places
1802: are unreachable. It knows this automatically after @code{AHEAD},
1803: @code{AGAIN}, @code{EXIT} and @code{LEAVE}; in other cases (e.g., after
1804: most @code{THROW}s), you can use the word @code{UNREACHABLE} to tell the
1805: compiler that the control flow never reaches that place. If
1806: @code{UNREACHABLE} is not used where it could, the only consequence is
1807: that the visibility of some locals is more limited than the rule above
1808: says. If @code{UNREACHABLE} is used where it should not (i.e., if you
1809: lie to the compiler), buggy code will be produced.
1810:
1811: doc-unreachable
1812:
1813: Another problem with this rule is that at @code{BEGIN}, the compiler
1814: does not know which locals will be visible on the incoming
1815: back-edge. All problems discussed in the following are due to this
1816: ignorance of the compiler (we discuss the problems using @code{BEGIN}
1817: loops as examples; the discussion also applies to @code{?DO} and other
1818: loops). Perhaps the most insidious example is:
1819: @example
1820: AHEAD
1821: BEGIN
1822: x
1823: [ 1 CS-ROLL ] THEN
1824: @{ x @}
1825: ...
1826: UNTIL
1827: @end example
1828:
1829: This should be legal according to the visibility rule. The use of
1830: @code{x} can only be reached through the definition; but that appears
1831: textually below the use.
1832:
1833: From this example it is clear that the visibility rules cannot be fully
1834: implemented without major headaches. Our implementation treats common
1835: cases as advertised and the exceptions are treated in a safe way: The
1836: compiler makes a reasonable guess about the locals visible after a
1837: @code{BEGIN}; if it is too pessimistic, the
1838: user will get a spurious error about the local not being defined; if the
1839: compiler is too optimistic, it will notice this later and issue a
1840: warning. In the case above the compiler would complain about @code{x}
1841: being undefined at its use. You can see from the obscure examples in
1842: this section that it takes quite unusual control structures to get the
1843: compiler into trouble, and even then it will often do fine.
1844:
1845: If the @code{BEGIN} is reachable from above, the most optimistic guess
1846: is that all locals visible before the @code{BEGIN} will also be
1847: visible after the @code{BEGIN}. This guess is valid for all loops that
1848: are entered only through the @code{BEGIN}, in particular, for normal
1849: @code{BEGIN}...@code{WHILE}...@code{REPEAT} and
1850: @code{BEGIN}...@code{UNTIL} loops and it is implemented in our
1851: compiler. When the branch to the @code{BEGIN} is finally generated by
1852: @code{AGAIN} or @code{UNTIL}, the compiler checks the guess and
1853: warns the user if it was too optimistic:
1854: @example
1855: IF
1856: @{ x @}
1857: BEGIN
1858: \ x ?
1859: [ 1 cs-roll ] THEN
1860: ...
1861: UNTIL
1862: @end example
1863:
1864: Here, @code{x} lives only until the @code{BEGIN}, but the compiler
1865: optimistically assumes that it lives until the @code{THEN}. It notices
1866: this difference when it compiles the @code{UNTIL} and issues a
1867: warning. The user can avoid the warning, and make sure that @code{x}
1868: is not used in the wrong area by using explicit scoping:
1869: @example
1870: IF
1871: SCOPE
1872: @{ x @}
1873: ENDSCOPE
1874: BEGIN
1875: [ 1 cs-roll ] THEN
1876: ...
1877: UNTIL
1878: @end example
1879:
1880: Since the guess is optimistic, there will be no spurious error messages
1881: about undefined locals.
1882:
1883: If the @code{BEGIN} is not reachable from above (e.g., after
1884: @code{AHEAD} or @code{EXIT}), the compiler cannot even make an
1885: optimistic guess, as the locals visible after the @code{BEGIN} may be
1886: defined later. Therefore, the compiler assumes that no locals are
1887: visible after the @code{BEGIN}. However, the user can use
1888: @code{ASSUME-LIVE} to make the compiler assume that the same locals are
1889: visible at the BEGIN as at the point where the top control-flow stack
1890: item was created.
1891:
1892: doc-assume-live
1893:
1894: E.g.,
1895: @example
1896: @{ x @}
1897: AHEAD
1898: ASSUME-LIVE
1899: BEGIN
1900: x
1901: [ 1 CS-ROLL ] THEN
1902: ...
1903: UNTIL
1904: @end example
1905:
1906: Other cases where the locals are defined before the @code{BEGIN} can be
1907: handled by inserting an appropriate @code{CS-ROLL} before the
1908: @code{ASSUME-LIVE} (and changing the control-flow stack manipulation
1909: behind the @code{ASSUME-LIVE}).
1910:
1911: Cases where locals are defined after the @code{BEGIN} (but should be
1912: visible immediately after the @code{BEGIN}) can only be handled by
1913: rearranging the loop. E.g., the ``most insidious'' example above can be
1914: arranged into:
1915: @example
1916: BEGIN
1917: @{ x @}
1918: ... 0=
1919: WHILE
1920: x
1921: REPEAT
1922: @end example
1923:
1924: @node How long do locals live?, Programming Style, Where are locals visible by name?, Gforth locals
1925: @subsubsection How long do locals live?
1926: @cindex locals lifetime
1927: @cindex lifetime of locals
1928:
1929: The right answer for the lifetime question would be: A local lives at
1930: least as long as it can be accessed. For a value-flavoured local this
1931: means: until the end of its visibility. However, a variable-flavoured
1932: local could be accessed through its address far beyond its visibility
1933: scope. Ultimately, this would mean that such locals would have to be
1934: garbage collected. Since this entails un-Forth-like implementation
1935: complexities, I adopted the same cowardly solution as some other
1936: languages (e.g., C): The local lives only as long as it is visible;
1937: afterwards its address is invalid (and programs that access it
1938: afterwards are erroneous).
1939:
1940: @node Programming Style, Implementation, How long do locals live?, Gforth locals
1941: @subsubsection Programming Style
1942: @cindex locals programming style
1943: @cindex programming style, locals
1944:
1945: The freedom to define locals anywhere has the potential to change
1946: programming styles dramatically. In particular, the need to use the
1947: return stack for intermediate storage vanishes. Moreover, all stack
1948: manipulations (except @code{PICK}s and @code{ROLL}s with run-time
1949: determined arguments) can be eliminated: If the stack items are in the
1950: wrong order, just write a locals definition for all of them; then
1951: write the items in the order you want.
1952:
1953: This seems a little far-fetched and eliminating stack manipulations is
1954: unlikely to become a conscious programming objective. Still, the number
1955: of stack manipulations will be reduced dramatically if local variables
1956: are used liberally (e.g., compare @code{max} in @ref{Gforth locals} with
1957: a traditional implementation of @code{max}).
1958:
1959: This shows one potential benefit of locals: making Forth programs more
1960: readable. Of course, this benefit will only be realized if the
1961: programmers continue to honour the principle of factoring instead of
1962: using the added latitude to make the words longer.
1963:
1964: @cindex single-assignment style for locals
1965: Using @code{TO} can and should be avoided. Without @code{TO},
1966: every value-flavoured local has only a single assignment and many
1967: advantages of functional languages apply to Forth. I.e., programs are
1968: easier to analyse, to optimize and to read: It is clear from the
1969: definition what the local stands for, it does not turn into something
1970: different later.
1971:
1972: E.g., a definition using @code{TO} might look like this:
1973: @example
1974: : strcmp @{ addr1 u1 addr2 u2 -- n @}
1975: u1 u2 min 0
1976: ?do
1977: addr1 c@@ addr2 c@@ -
1978: ?dup-if
1979: unloop exit
1980: then
1981: addr1 char+ TO addr1
1982: addr2 char+ TO addr2
1983: loop
1984: u1 u2 - ;
1985: @end example
1986: Here, @code{TO} is used to update @code{addr1} and @code{addr2} at
1987: every loop iteration. @code{strcmp} is a typical example of the
1988: readability problems of using @code{TO}. When you start reading
1989: @code{strcmp}, you think that @code{addr1} refers to the start of the
1990: string. Only near the end of the loop you realize that it is something
1991: else.
1992:
1993: This can be avoided by defining two locals at the start of the loop that
1994: are initialized with the right value for the current iteration.
1995: @example
1996: : strcmp @{ addr1 u1 addr2 u2 -- n @}
1997: addr1 addr2
1998: u1 u2 min 0
1999: ?do @{ s1 s2 @}
2000: s1 c@@ s2 c@@ -
2001: ?dup-if
2002: unloop exit
2003: then
2004: s1 char+ s2 char+
2005: loop
2006: 2drop
2007: u1 u2 - ;
2008: @end example
2009: Here it is clear from the start that @code{s1} has a different value
2010: in every loop iteration.
2011:
2012: @node Implementation, , Programming Style, Gforth locals
2013: @subsubsection Implementation
2014: @cindex locals implementation
2015: @cindex implementation of locals
2016:
2017: @cindex locals stack
2018: Gforth uses an extra locals stack. The most compelling reason for
2019: this is that the return stack is not float-aligned; using an extra stack
2020: also eliminates the problems and restrictions of using the return stack
2021: as locals stack. Like the other stacks, the locals stack grows toward
2022: lower addresses. A few primitives allow an efficient implementation:
2023:
2024: doc-@local#
2025: doc-f@local#
2026: doc-laddr#
2027: doc-lp+!#
2028: doc-lp!
2029: doc->l
2030: doc-f>l
2031:
2032: In addition to these primitives, some specializations of these
2033: primitives for commonly occurring inline arguments are provided for
2034: efficiency reasons, e.g., @code{@@local0} as specialization of
2035: @code{@@local#} for the inline argument 0. The following compiling words
2036: compile the right specialized version, or the general version, as
2037: appropriate:
2038:
2039: doc-compile-@local
2040: doc-compile-f@local
2041: doc-compile-lp+!
2042:
2043: Combinations of conditional branches and @code{lp+!#} like
2044: @code{?branch-lp+!#} (the locals pointer is only changed if the branch
2045: is taken) are provided for efficiency and correctness in loops.
2046:
2047: A special area in the dictionary space is reserved for keeping the
2048: local variable names. @code{@{} switches the dictionary pointer to this
2049: area and @code{@}} switches it back and generates the locals
2050: initializing code. @code{W:} etc.@ are normal defining words. This
2051: special area is cleared at the start of every colon definition.
2052:
2053: @cindex wordlist for defining locals
2054: A special feature of Gforth's dictionary is used to implement the
2055: definition of locals without type specifiers: every wordlist (aka
2056: vocabulary) has its own methods for searching
2057: etc. (@pxref{Wordlists}). For the present purpose we defined a wordlist
2058: with a special search method: When it is searched for a word, it
2059: actually creates that word using @code{W:}. @code{@{} changes the search
2060: order to first search the wordlist containing @code{@}}, @code{W:} etc.,
2061: and then the wordlist for defining locals without type specifiers.
2062:
2063: The lifetime rules support a stack discipline within a colon
2064: definition: The lifetime of a local is either nested with other locals
2065: lifetimes or it does not overlap them.
2066:
2067: At @code{BEGIN}, @code{IF}, and @code{AHEAD} no code for locals stack
2068: pointer manipulation is generated. Between control structure words
2069: locals definitions can push locals onto the locals stack. @code{AGAIN}
2070: is the simplest of the other three control flow words. It has to
2071: restore the locals stack depth of the corresponding @code{BEGIN}
2072: before branching. The code looks like this:
2073: @format
2074: @code{lp+!#} current-locals-size @minus{} dest-locals-size
2075: @code{branch} <begin>
2076: @end format
2077:
2078: @code{UNTIL} is a little more complicated: If it branches back, it
2079: must adjust the stack just like @code{AGAIN}. But if it falls through,
2080: the locals stack must not be changed. The compiler generates the
2081: following code:
2082: @format
2083: @code{?branch-lp+!#} <begin> current-locals-size @minus{} dest-locals-size
2084: @end format
2085: The locals stack pointer is only adjusted if the branch is taken.
2086:
2087: @code{THEN} can produce somewhat inefficient code:
2088: @format
2089: @code{lp+!#} current-locals-size @minus{} orig-locals-size
2090: <orig target>:
2091: @code{lp+!#} orig-locals-size @minus{} new-locals-size
2092: @end format
2093: The second @code{lp+!#} adjusts the locals stack pointer from the
2094: level at the @var{orig} point to the level after the @code{THEN}. The
2095: first @code{lp+!#} adjusts the locals stack pointer from the current
2096: level to the level at the orig point, so the complete effect is an
2097: adjustment from the current level to the right level after the
2098: @code{THEN}.
2099:
2100: @cindex locals information on the control-flow stack
2101: @cindex control-flow stack items, locals information
2102: In a conventional Forth implementation a dest control-flow stack entry
2103: is just the target address and an orig entry is just the address to be
2104: patched. Our locals implementation adds a wordlist to every orig or dest
2105: item. It is the list of locals visible (or assumed visible) at the point
2106: described by the entry. Our implementation also adds a tag to identify
2107: the kind of entry, in particular to differentiate between live and dead
2108: (reachable and unreachable) orig entries.
2109:
2110: A few unusual operations have to be performed on locals wordlists:
2111:
2112: doc-common-list
2113: doc-sub-list?
2114: doc-list-size
2115:
2116: Several features of our locals wordlist implementation make these
2117: operations easy to implement: The locals wordlists are organised as
2118: linked lists; the tails of these lists are shared, if the lists
2119: contain some of the same locals; and the address of a name is greater
2120: than the address of the names behind it in the list.
2121:
2122: Another important implementation detail is the variable
2123: @code{dead-code}. It is used by @code{BEGIN} and @code{THEN} to
2124: determine if they can be reached directly or only through the branch
2125: that they resolve. @code{dead-code} is set by @code{UNREACHABLE},
2126: @code{AHEAD}, @code{EXIT} etc., and cleared at the start of a colon
2127: definition, by @code{BEGIN} and usually by @code{THEN}.
2128:
2129: Counted loops are similar to other loops in most respects, but
2130: @code{LEAVE} requires special attention: It performs basically the same
2131: service as @code{AHEAD}, but it does not create a control-flow stack
2132: entry. Therefore the information has to be stored elsewhere;
2133: traditionally, the information was stored in the target fields of the
2134: branches created by the @code{LEAVE}s, by organizing these fields into a
2135: linked list. Unfortunately, this clever trick does not provide enough
2136: space for storing our extended control flow information. Therefore, we
2137: introduce another stack, the leave stack. It contains the control-flow
2138: stack entries for all unresolved @code{LEAVE}s.
2139:
2140: Local names are kept until the end of the colon definition, even if
2141: they are no longer visible in any control-flow path. In a few cases
2142: this may lead to increased space needs for the locals name area, but
2143: usually less than reclaiming this space would cost in code size.
2144:
2145:
2146: @node ANS Forth locals, , Gforth locals, Locals
2147: @subsection ANS Forth locals
2148: @cindex locals, ANS Forth style
2149:
2150: The ANS Forth locals wordset does not define a syntax for locals, but
2151: words that make it possible to define various syntaxes. One of the
2152: possible syntaxes is a subset of the syntax we used in the Gforth locals
2153: wordset, i.e.:
2154:
2155: @example
2156: @{ local1 local2 ... -- comment @}
2157: @end example
2158: or
2159: @example
2160: @{ local1 local2 ... @}
2161: @end example
2162:
2163: The order of the locals corresponds to the order in a stack comment. The
2164: restrictions are:
2165:
2166: @itemize @bullet
2167: @item
2168: Locals can only be cell-sized values (no type specifiers are allowed).
2169: @item
2170: Locals can be defined only outside control structures.
2171: @item
2172: Locals can interfere with explicit usage of the return stack. For the
2173: exact (and long) rules, see the standard. If you don't use return stack
2174: accessing words in a definition using locals, you will be all right. The
2175: purpose of this rule is to make locals implementation on the return
2176: stack easier.
2177: @item
2178: The whole definition must be in one line.
2179: @end itemize
2180:
2181: Locals defined in this way behave like @code{VALUE}s (@xref{Simple
2182: Defining Words}). I.e., they are initialized from the stack. Using their
2183: name produces their value. Their value can be changed using @code{TO}.
2184:
2185: Since this syntax is supported by Gforth directly, you need not do
2186: anything to use it. If you want to port a program using this syntax to
2187: another ANS Forth system, use @file{compat/anslocal.fs} to implement the
2188: syntax on the other system.
2189:
2190: Note that a syntax shown in the standard, section A.13 looks
2191: similar, but is quite different in having the order of locals
2192: reversed. Beware!
2193:
2194: The ANS Forth locals wordset itself consists of the following word
2195:
2196: doc-(local)
2197:
2198: The ANS Forth locals extension wordset defines a syntax, but it is so
2199: awful that we strongly recommend not to use it. We have implemented this
2200: syntax to make porting to Gforth easy, but do not document it here. The
2201: problem with this syntax is that the locals are defined in an order
2202: reversed with respect to the standard stack comment notation, making
2203: programs harder to read, and easier to misread and miswrite. The only
2204: merit of this syntax is that it is easy to implement using the ANS Forth
2205: locals wordset.
2206:
1.5 ! anton 2207: @node Defining Words, Structures, Locals, Words
1.1 anton 2208: @section Defining Words
2209: @cindex defining words
2210:
2211: @menu
2212: * Simple Defining Words::
2213: * Colon Definitions::
2214: * User-defined Defining Words::
2215: * Supplying names::
2216: * Interpretation and Compilation Semantics::
2217: @end menu
2218:
2219: @node Simple Defining Words, Colon Definitions, Defining Words, Defining Words
2220: @subsection Simple Defining Words
2221: @cindex simple defining words
2222: @cindex defining words, simple
2223:
2224: doc-constant
2225: doc-2constant
2226: doc-fconstant
2227: doc-variable
2228: doc-2variable
2229: doc-fvariable
2230: doc-create
2231: doc-user
2232: doc-value
2233: doc-to
2234: doc-defer
2235: doc-is
2236:
2237: @node Colon Definitions, User-defined Defining Words, Simple Defining Words, Defining Words
2238: @subsection Colon Definitions
2239: @cindex colon definitions
2240:
2241: @example
2242: : name ( ... -- ... )
2243: word1 word2 word3 ;
2244: @end example
2245:
2246: creates a word called @code{name}, that, upon execution, executes
2247: @code{word1 word2 word3}. @code{name} is a @dfn{(colon) definition}.
2248:
2249: The explanation above is somewhat superficial. @xref{Interpretation and
2250: Compilation Semantics} for an in-depth discussion of some of the issues
2251: involved.
2252:
2253: doc-:
2254: doc-;
2255:
2256: @node User-defined Defining Words, Supplying names, Colon Definitions, Defining Words
2257: @subsection User-defined Defining Words
2258: @cindex user-defined defining words
2259: @cindex defining words, user-defined
2260:
2261: You can create new defining words simply by wrapping defining-time code
2262: around existing defining words and putting the sequence in a colon
2263: definition.
2264:
2265: @cindex @code{CREATE} ... @code{DOES>}
2266: If you want the words defined with your defining words to behave
2267: differently from words defined with standard defining words, you can
2268: write your defining word like this:
2269:
2270: @example
2271: : def-word ( "name" -- )
2272: Create @var{code1}
2273: DOES> ( ... -- ... )
2274: @var{code2} ;
2275:
2276: def-word name
2277: @end example
2278:
2279: Technically, this fragment defines a defining word @code{def-word}, and
2280: a word @code{name}; when you execute @code{name}, the address of the
2281: body of @code{name} is put on the data stack and @var{code2} is executed
2282: (the address of the body of @code{name} is the address @code{HERE}
2283: returns immediately after the @code{CREATE}).
2284:
2285: In other words, if you make the following definitions:
2286:
2287: @example
2288: : def-word1 ( "name" -- )
2289: Create @var{code1} ;
2290:
2291: : action1 ( ... -- ... )
2292: @var{code2} ;
2293:
2294: def-word name1
2295: @end example
2296:
2297: Using @code{name1 action1} is equivalent to using @code{name}.
2298:
2299: E.g., you can implement @code{Constant} in this way:
2300:
2301: @example
2302: : constant ( w "name" -- )
2303: create ,
2304: DOES> ( -- w )
2305: @@ ;
2306: @end example
2307:
2308: When you create a constant with @code{5 constant five}, first a new word
2309: @code{five} is created, then the value 5 is laid down in the body of
2310: @code{five} with @code{,}. When @code{five} is invoked, the address of
2311: the body is put on the stack, and @code{@@} retrieves the value 5.
2312:
2313: @cindex stack effect of @code{DOES>}-parts
2314: @cindex @code{DOES>}-parts, stack effect
2315: In the example above the stack comment after the @code{DOES>} specifies
2316: the stack effect of the defined words, not the stack effect of the
2317: following code (the following code expects the address of the body on
2318: the top of stack, which is not reflected in the stack comment). This is
2319: the convention that I use and recommend (it clashes a bit with using
2320: locals declarations for stack effect specification, though).
2321:
2322: @subsubsection Applications of @code{CREATE..DOES>}
2323: @cindex @code{CREATE} ... @code{DOES>}, applications
2324:
2325: You may wonder how to use this feature. Here are some usage patterns:
2326:
2327: @cindex factoring similar colon definitions
2328: When you see a sequence of code occurring several times, and you can
2329: identify a meaning, you will factor it out as a colon definition. When
2330: you see similar colon definitions, you can factor them using
2331: @code{CREATE..DOES>}. E.g., an assembler usually defines several words
2332: that look very similar:
2333: @example
2334: : ori, ( reg-target reg-source n -- )
2335: 0 asm-reg-reg-imm ;
2336: : andi, ( reg-target reg-source n -- )
2337: 1 asm-reg-reg-imm ;
2338: @end example
2339:
2340: This could be factored with:
2341: @example
2342: : reg-reg-imm ( op-code -- )
2343: create ,
2344: DOES> ( reg-target reg-source n -- )
2345: @@ asm-reg-reg-imm ;
2346:
2347: 0 reg-reg-imm ori,
2348: 1 reg-reg-imm andi,
2349: @end example
2350:
2351: @cindex currying
2352: Another view of @code{CREATE..DOES>} is to consider it as a crude way to
2353: supply a part of the parameters for a word (known as @dfn{currying} in
2354: the functional language community). E.g., @code{+} needs two
2355: parameters. Creating versions of @code{+} with one parameter fixed can
2356: be done like this:
2357: @example
2358: : curry+ ( n1 -- )
2359: create ,
2360: DOES> ( n2 -- n1+n2 )
2361: @@ + ;
2362:
2363: 3 curry+ 3+
2364: -2 curry+ 2-
2365: @end example
2366:
2367: @subsubsection The gory details of @code{CREATE..DOES>}
2368: @cindex @code{CREATE} ... @code{DOES>}, details
2369:
2370: doc-does>
2371:
2372: @cindex @code{DOES>} in a separate definition
2373: This means that you need not use @code{CREATE} and @code{DOES>} in the
2374: same definition; E.g., you can put the @code{DOES>}-part in a separate
2375: definition. This allows us to, e.g., select among different DOES>-parts:
2376: @example
2377: : does1
2378: DOES> ( ... -- ... )
2379: ... ;
2380:
2381: : does2
2382: DOES> ( ... -- ... )
2383: ... ;
2384:
2385: : def-word ( ... -- ... )
2386: create ...
2387: IF
2388: does1
2389: ELSE
2390: does2
2391: ENDIF ;
2392: @end example
2393:
2394: @cindex @code{DOES>} in interpretation state
2395: In a standard program you can apply a @code{DOES>}-part only if the last
2396: word was defined with @code{CREATE}. In Gforth, the @code{DOES>}-part
2397: will override the behaviour of the last word defined in any case. In a
2398: standard program, you can use @code{DOES>} only in a colon
2399: definition. In Gforth, you can also use it in interpretation state, in a
2400: kind of one-shot mode:
2401: @example
2402: CREATE name ( ... -- ... )
2403: @var{initialization}
2404: DOES>
2405: @var{code} ;
2406: @end example
2407: This is equivalent to the standard
2408: @example
2409: :noname
2410: DOES>
2411: @var{code} ;
2412: CREATE name EXECUTE ( ... -- ... )
2413: @var{initialization}
2414: @end example
2415:
2416: You can get the address of the body of a word with
2417:
2418: doc->body
2419:
2420: @node Supplying names, Interpretation and Compilation Semantics, User-defined Defining Words, Defining Words
2421: @subsection Supplying names for the defined words
2422: @cindex names for defined words
2423: @cindex defining words, name parameter
2424:
2425: @cindex defining words, name given in a string
2426: By default, defining words take the names for the defined words from the
2427: input stream. Sometimes you want to supply the name from a string. You
2428: can do this with
2429:
2430: doc-nextname
2431:
2432: E.g.,
2433:
2434: @example
2435: s" foo" nextname create
2436: @end example
2437: is equivalent to
2438: @example
2439: create foo
2440: @end example
2441:
2442: @cindex defining words without name
2443: Sometimes you want to define a word without a name. You can do this with
2444:
2445: doc-noname
2446:
2447: @cindex execution token of last defined word
2448: To make any use of the newly defined word, you need its execution
2449: token. You can get it with
2450:
2451: doc-lastxt
2452:
2453: E.g., you can initialize a deferred word with an anonymous colon
2454: definition:
2455: @example
2456: Defer deferred
2457: noname : ( ... -- ... )
2458: ... ;
2459: lastxt IS deferred
2460: @end example
2461:
2462: @code{lastxt} also works when the last word was not defined as
2463: @code{noname}.
2464:
2465: The standard has also recognized the need for anonymous words and
2466: provides
2467:
2468: doc-:noname
2469:
2470: This leaves the execution token for the word on the stack after the
2471: closing @code{;}. You can rewrite the last example with @code{:noname}:
2472: @example
2473: Defer deferred
2474: :noname ( ... -- ... )
2475: ... ;
2476: IS deferred
2477: @end example
2478:
2479: @node Interpretation and Compilation Semantics, , Supplying names, Defining Words
2480: @subsection Interpretation and Compilation Semantics
2481: @cindex semantics, interpretation and compilation
2482:
2483: @cindex interpretation semantics
2484: The @dfn{interpretation semantics} of a word are what the text
2485: interpreter does when it encounters the word in interpret state. It also
2486: appears in some other contexts, e.g., the execution token returned by
2487: @code{' @var{word}} identifies the interpretation semantics of
2488: @var{word} (in other words, @code{' @var{word} execute} is equivalent to
2489: interpret-state text interpretation of @code{@var{word}}).
2490:
2491: @cindex compilation semantics
2492: The @dfn{compilation semantics} of a word are what the text interpreter
2493: does when it encounters the word in compile state. It also appears in
2494: other contexts, e.g, @code{POSTPONE @var{word}} compiles@footnote{In
2495: standard terminology, ``appends to the current definition''.} the
2496: compilation semantics of @var{word}.
2497:
2498: @cindex execution semantics
2499: The standard also talks about @dfn{execution semantics}. They are used
2500: only for defining the interpretation and compilation semantics of many
2501: words. By default, the interpretation semantics of a word are to
2502: @code{execute} its execution semantics, and the compilation semantics of
2503: a word are to @code{compile,} its execution semantics.@footnote{In
2504: standard terminology: The default interpretation semantics are its
2505: execution semantics; the default compilation semantics are to append its
2506: execution semantics to the execution semantics of the current
2507: definition.}
2508:
2509: @cindex immediate words
2510: You can change the compilation semantics into @code{execute}ing the
2511: execution semantics with
2512:
2513: doc-immediate
2514:
2515: @cindex compile-only words
2516: You can remove the interpretation semantics of a word with
2517:
2518: doc-compile-only
2519: doc-restrict
2520:
2521: Note that ticking (@code{'}) compile-only words gives an error
2522: (``Interpreting a compile-only word'').
2523:
2524: Gforth also allows you to define words with arbitrary combinations of
2525: interpretation and compilation semantics.
2526:
2527: doc-interpret/compile:
2528:
2529: This feature was introduced for implementing @code{TO} and @code{S"}. I
2530: recommend that you do not define such words, as cute as they may be:
2531: they make it hard to get at both parts of the word in some contexts.
2532: E.g., assume you want to get an execution token for the compilation
2533: part. Instead, define two words, one that embodies the interpretation
2534: part, and one that embodies the compilation part.
2535:
2536: There is, however, a potentially useful application of this feature:
2537: Providing differing implementations for the default semantics. While
2538: this introduces redundancy and is therefore usually a bad idea, a
2539: performance improvement may be worth the trouble. E.g., consider the
2540: word @code{foobar}:
2541:
2542: @example
2543: : foobar
2544: foo bar ;
2545: @end example
2546:
2547: Let us assume that @code{foobar} is called so frequently that the
2548: calling overhead would take a significant amount of the run-time. We can
2549: optimize it with @code{interpret/compile:}:
2550:
2551: @example
2552: :noname
2553: foo bar ;
2554: :noname
2555: POSTPONE foo POSTPONE bar ;
2556: interpret/compile: foobar
2557: @end example
2558:
2559: This definition has the same interpretation semantics and essentially
2560: the same compilation semantics as the simple definition of
2561: @code{foobar}, but the implementation of the compilation semantics is
2562: more efficient with respect to run-time.
2563:
2564: @cindex state-smart words are a bad idea
2565: Some people try to use state-smart words to emulate the feature provided
2566: by @code{interpret/compile:} (words are state-smart if they check
2567: @code{STATE} during execution). E.g., they would try to code
2568: @code{foobar} like this:
2569:
2570: @example
2571: : foobar
2572: STATE @@
2573: IF ( compilation state )
2574: POSTPONE foo POSTPONE bar
2575: ELSE
2576: foo bar
2577: ENDIF ; immediate
2578: @end example
2579:
2580: While this works if @code{foobar} is processed only by the text
2581: interpreter, it does not work in other contexts (like @code{'} or
2582: @code{POSTPONE}). E.g., @code{' foobar} will produce an execution token
2583: for a state-smart word, not for the interpretation semantics of the
2584: original @code{foobar}; when you execute this execution token (directly
2585: with @code{EXECUTE} or indirectly through @code{COMPILE,}) in compile
2586: state, the result will not be what you expected (i.e., it will not
2587: perform @code{foo bar}). State-smart words are a bad idea. Simply don't
2588: write them!
2589:
2590: @cindex defining words with arbitrary semantics combinations
2591: It is also possible to write defining words that define words with
2592: arbitrary combinations of interpretation and compilation semantics (or,
2593: preferably, arbitrary combinations of implementations of the default
2594: semantics). In general, this looks like:
2595:
2596: @example
2597: : def-word
2598: create-interpret/compile
2599: @var{code1}
2600: interpretation>
2601: @var{code2}
2602: <interpretation
2603: compilation>
2604: @var{code3}
2605: <compilation ;
2606: @end example
2607:
2608: For a @var{word} defined with @code{def-word}, the interpretation
2609: semantics are to push the address of the body of @var{word} and perform
2610: @var{code2}, and the compilation semantics are to push the address of
2611: the body of @var{word} and perform @var{code3}. E.g., @code{constant}
2612: can also be defined like this:
2613:
2614: @example
2615: : constant ( n "name" -- )
2616: create-interpret/compile
2617: ,
2618: interpretation> ( -- n )
2619: @@
2620: <interpretation
2621: compilation> ( compilation. -- ; run-time. -- n )
2622: @@ postpone literal
2623: <compilation ;
2624: @end example
2625:
2626: doc-create-interpret/compile
2627: doc-interpretation>
2628: doc-<interpretation
2629: doc-compilation>
2630: doc-<compilation
2631:
2632: Note that words defined with @code{interpret/compile:} and
2633: @code{create-interpret/compile} have an extended header structure that
2634: differs from other words; however, unless you try to access them with
2635: plain address arithmetic, you should not notice this. Words for
2636: accessing the header structure usually know how to deal with this; e.g.,
2637: @code{' word >body} also gives you the body of a word created with
2638: @code{create-interpret/compile}.
2639:
1.5 ! anton 2640: @c ----------------------------------------------------------
! 2641: @node Structures, Objects, Defining Words, Words
! 2642: @section Structures
! 2643: @cindex structures
! 2644: @cindex records
! 2645:
! 2646: This section presents the structure package that comes with Gforth. A
! 2647: version of the package implemented in plain ANS Forth is available in
! 2648: @file{compat/struct.fs}. This package was inspired by a posting on
! 2649: comp.lang.forth in 1989 (unfortunately I don't remember, by whom;
! 2650: possibly John Hayes). A version of this section has been published in
! 2651: ???. Marcel Hendrix provided helpful comments.
! 2652:
! 2653: @menu
! 2654: * Why explicit structure support?::
! 2655: * Structure Usage::
! 2656: * Structure Naming Convention::
! 2657: * Structure Implementation::
! 2658: * Structure Glossary::
! 2659: @end menu
! 2660:
! 2661: @node Why explicit structure support?, Structure Usage, Structures, Structures
! 2662: @subsection Why explicit structure support?
! 2663:
! 2664: @cindex address arithmetic for structures
! 2665: @cindex structures using address arithmetic
! 2666: If we want to use a structure containing several fields, we could simply
! 2667: reserve memory for it, and access the fields using address arithmetic
! 2668: (@pxref{Address arithmetic}). As an example, consider a structure with
! 2669: the following fields
! 2670:
! 2671: @table @code
! 2672: @item a
! 2673: is a float
! 2674: @item b
! 2675: is a cell
! 2676: @item c
! 2677: is a float
! 2678: @end table
! 2679:
! 2680: Given the (float-aligned) base address of the structure we get the
! 2681: address of the field
! 2682:
! 2683: @table @code
! 2684: @item a
! 2685: without doing anything further.
! 2686: @item b
! 2687: with @code{float+}
! 2688: @item c
! 2689: with @code{float+ cell+ faligned}
! 2690: @end table
! 2691:
! 2692: It is easy to see that this can become quite tiring.
! 2693:
! 2694: Moreover, it is not very readable, because seeing a
! 2695: @code{cell+} tells us neither which kind of structure is
! 2696: accessed nor what field is accessed; we have to somehow infer the kind
! 2697: of structure, and then look up in the documentation, which field of
! 2698: that structure corresponds to that offset.
! 2699:
! 2700: Finally, this kind of address arithmetic also causes maintenance
! 2701: troubles: If you add or delete a field somewhere in the middle of the
! 2702: structure, you have to find and change all computations for the fields
! 2703: afterwards.
! 2704:
! 2705: So, instead of using @code{cell+} and friends directly, how
! 2706: about storing the offsets in constants:
! 2707:
! 2708: @example
! 2709: 0 constant a-offset
! 2710: 0 float+ constant b-offset
! 2711: 0 float+ cell+ faligned c-offset
! 2712: @end example
! 2713:
! 2714: Now we can get the address of field @code{x} with @code{x-offset
! 2715: +}. This is much better in all respects. Of course, you still
! 2716: have to change all later offset definitions if you add a field. You can
! 2717: fix this by declaring the offsets in the following way:
! 2718:
! 2719: @example
! 2720: 0 constant a-offset
! 2721: a-offset float+ constant b-offset
! 2722: b-offset cell+ faligned constant c-offset
! 2723: @end example
! 2724:
! 2725: Since we always use the offsets with @code{+}, using a defining
! 2726: word @code{cfield} that includes the @code{+} in the
! 2727: action of the defined word offers itself:
! 2728:
! 2729: @example
! 2730: : cfield ( n "name" -- )
! 2731: create ,
! 2732: does> ( name execution: addr1 -- addr2 )
! 2733: @@ + ;
! 2734:
! 2735: 0 cfield a
! 2736: 0 a float+ cfield b
! 2737: 0 b cell+ faligned cfield c
! 2738: @end example
! 2739:
! 2740: Instead of @code{x-offset +}, we now simply write @code{x}.
! 2741:
! 2742: The structure field words now can be used quite nicely. However,
! 2743: their definition is still a bit cumbersome: We have to repeat the
! 2744: name, the information about size and alignment is distributed before
! 2745: and after the field definitions etc. The structure package presented
! 2746: here addresses these problems.
! 2747:
! 2748: @node Structure Usage, Structure Naming Convention, Why explicit structure support?, Structures
! 2749: @subsection Structure Usage
! 2750: @cindex structure usage
! 2751:
! 2752: @cindex @code{field} usage
! 2753: @cindex @code{struct} usage
! 2754: @cindex @code{end-struct} usage
! 2755: You can define a structure for a (data-less) linked list with
! 2756: @example
! 2757: struct
! 2758: cell% field list-next
! 2759: end-struct list%
! 2760: @end example
! 2761:
! 2762: With the address of the list node on the stack, you can compute the
! 2763: address of the field that contains the address of the next node with
! 2764: @code{list-next}. E.g., you can determine the length of a list
! 2765: with:
! 2766:
! 2767: @example
! 2768: : list-length ( list -- n )
! 2769: \ "list" is a pointer to the first element of a linked list
! 2770: \ "n" is the length of the list
! 2771: 0 begin ( list1 n1 )
! 2772: over
! 2773: while ( list1 n1 )
! 2774: 1+ swap list-next @@ swap
! 2775: repeat
! 2776: nip ;
! 2777: @end example
! 2778:
! 2779: You can reserve memory for a list node in the dictionary with
! 2780: @code{list% %allot}, which leaves the address of the list node on the
! 2781: stack. For the equivalent allocation on the heap you can use @code{list%
! 2782: %alloc} (or, for an @code{allocate}-like stack effect (i.e., with ior),
! 2783: use @code{list% %allocate}). You can also get the the size of a list
! 2784: node with @code{list% %size} and it's alignment with @code{list%
! 2785: %alignment}.
! 2786:
! 2787: Note that in ANS Forth the body of a @code{create}d word is
! 2788: @code{aligned} but not necessarily @code{faligned};
! 2789: therefore, if you do a
! 2790: @example
! 2791: create @emph{name} foo% %allot
! 2792: @end example
! 2793:
! 2794: then the memory alloted for @code{foo%} is
! 2795: guaranteed to start at the body of @code{@emph{name}} only if
! 2796: @code{foo%} contains only character, cell and double fields.
! 2797:
! 2798: @cindex strcutures containing structures
! 2799: You can also include a structure @code{foo%} as field of
! 2800: another structure, with:
! 2801: @example
! 2802: struct
! 2803: ...
! 2804: foo% field ...
! 2805: ...
! 2806: end-struct ...
! 2807: @end example
! 2808:
! 2809: @cindex structure extension
! 2810: @cindex extended records
! 2811: Instead of starting with an empty structure, you can also extend an
! 2812: existing structure. E.g., a plain linked list without data, as defined
! 2813: above, is hardly useful; You can extend it to a linked list of integers,
! 2814: like this:@footnote{This feature is also known as @emph{extended
! 2815: records}. It is the main innovation in the Oberon language; in other
! 2816: words, adding this feature to Modula-2 led Wirth to create a new
! 2817: language, write a new compiler etc. Adding this feature to Forth just
! 2818: requires a few lines of code.}
! 2819:
! 2820: @example
! 2821: list%
! 2822: cell% field intlist-int
! 2823: end-struct intlist%
! 2824: @end example
! 2825:
! 2826: @code{intlist%} is a structure with two fields:
! 2827: @code{list-next} and @code{intlist-int}.
! 2828:
! 2829: @cindex structures containing arrays
! 2830: You can specify an array type containing @emph{n} elements of
! 2831: type @code{foo%} like this:
! 2832:
! 2833: @example
! 2834: foo% @emph{n} *
! 2835: @end example
! 2836:
! 2837: You can use this array type in any place where you can use a normal
! 2838: type, e.g., when defining a @code{field}, or with
! 2839: @code{%allot}.
! 2840:
! 2841: @cindex first field optimization
! 2842: The first field is at the base address of a structure and the word
! 2843: for this field (e.g., @code{list-next}) actually does not change
! 2844: the address on the stack. You may be tempted to leave it away in the
! 2845: interest of run-time and space efficiency. This is not necessary,
! 2846: because the structure package optimizes this case and compiling such
! 2847: words does not generate any code. So, in the interest of readability
! 2848: and maintainability you should include the word for the field when
! 2849: accessing the field.
! 2850:
! 2851: @node Structure Naming Convention, Structure Implementation, Structure Usage, Structures
! 2852: @subsection Structure Naming Convention
! 2853: @cindex structure naming conventions
! 2854:
! 2855: The field names that come to (my) mind are often quite generic, and,
! 2856: if used, would cause frequent name clashes. E.g., many structures
! 2857: probably contain a @code{counter} field. The structure names
! 2858: that come to (my) mind are often also the logical choice for the names
! 2859: of words that create such a structure.
! 2860:
! 2861: Therefore, I have adopted the following naming conventions:
! 2862:
! 2863: @itemize @bullet
! 2864: @cindex field naming convention
! 2865: @item
! 2866: The names of fields are of the form
! 2867: @code{@emph{struct}-@emph{field}}, where
! 2868: @code{@emph{struct}} is the basic name of the structure, and
! 2869: @code{@emph{field}} is the basic name of the field. You can
! 2870: think about field words as converting converts the (address of the)
! 2871: structure into the (address of the) field.
! 2872:
! 2873: @cindex structure naming convention
! 2874: @item
! 2875: The names of structures are of the form
! 2876: @code{@emph{struct}%}, where
! 2877: @code{@emph{struct}} is the basic name of the structure.
! 2878: @end itemize
! 2879:
! 2880: This naming convention does not work that well for fields of extended
! 2881: structures; e.g., the integer list structure has a field
! 2882: @code{intlist-int}, but has @code{list-next}, not
! 2883: @code{intlist-next}.
! 2884:
! 2885: @node Structure Implementation, Structure Glossary, Structure Naming Convention, Structures
! 2886: @subsection Structure Implementation
! 2887: @cindex structure implementation
! 2888: @cindex implementation of structures
! 2889:
! 2890: The central idea in the implementation is to pass the data about the
! 2891: structure being built on the stack, not in some global
! 2892: variable. Everything else falls into place naturally once this design
! 2893: decision is made.
! 2894:
! 2895: The type description on the stack is of the form @emph{align
! 2896: size}. Keeping the size on the top-of-stack makes dealing with arrays
! 2897: very simple.
! 2898:
! 2899: @code{field} is a defining word that uses @code{create}
! 2900: and @code{does>}. The body of the field contains the offset
! 2901: of the field, and the normal @code{does>} action is
! 2902:
! 2903: @example
! 2904: @ +
! 2905: @end example
! 2906:
! 2907: i.e., add the offset to the address, giving the stack effect
! 2908: @code{addr1 -- addr2} for a field.
! 2909:
! 2910: @cindex first field optimization, implementation
! 2911: This simple structure is slightly complicated by the optimization
! 2912: for fields with offset 0, which requires a different
! 2913: @code{does>}-part (because we cannot rely on there being
! 2914: something on the stack if such a field is invoked during
! 2915: compilation). Therefore, we put the different @code{does>}-parts
! 2916: in separate words, and decide which one to invoke based on the
! 2917: offset. For a zero offset, the field is basically a noop; it is
! 2918: immediate, and therefore no code is generated when it is compiled.
! 2919:
! 2920: @node Structure Glossary, , Structure Implementation, Structures
! 2921: @subsection Structure Glossary
! 2922: @cindex structure glossary
! 2923:
! 2924: doc-%align
! 2925: doc-%alignment
! 2926: doc-%alloc
! 2927: doc-%allocate
! 2928: doc-%allot
! 2929: doc-cell%
! 2930: doc-char%
! 2931: doc-dfloat%
! 2932: doc-double%
! 2933: doc-end-struct
! 2934: doc-field
! 2935: doc-float%
! 2936: doc-nalign
! 2937: doc-sfloat%
! 2938: doc-%size
! 2939: doc-struct
! 2940:
! 2941: @c -------------------------------------------------------------
! 2942: @node Objects, Tokens for Words, Structures, Words
! 2943: @section Objects
! 2944: @cindex objects
! 2945: @cindex object-oriented programming
! 2946:
! 2947: @cindex @file{objects.fs}
! 2948: @cindex @file{oof.fs}
! 2949: Gforth comes with two packets for object-oriented programming,
! 2950: @file{objects.fs} and @file{oof.fs}; none of them is preloaded, so you
! 2951: have to @code{include} them before use. This section describes the
! 2952: @file{objects.fs} packet. You can find a description (in German) of
! 2953: @file{oof.fs} in @cite{Object oriented bigFORTH} by Bernd Paysan,
! 2954: published in @cite{Vierte Dimension} 10(2), 1994. Both packets are
! 2955: written in ANS Forth and can be used with any other standard Forth.
! 2956: @c McKewan's and Zsoter's packages
! 2957: @c this section is a variant of ...
! 2958:
! 2959: This section assumes (in some places) that you have read @ref{Structures}.
! 2960:
! 2961: @menu
! 2962: * Properties of the Objects model::
! 2963: * Why object-oriented programming?::
! 2964: * Object-Oriented Terminology::
! 2965: * Basic Objects Usage::
! 2966: * The class Object::
! 2967: * Creating objects::
! 2968: * Object-Oriented Programming Style::
! 2969: * Class Binding::
! 2970: * Method conveniences::
! 2971: * Classes and Scoping::
! 2972: * Object Interfaces::
! 2973: * Objects Implementation::
! 2974: * Comparison with other object models::
! 2975: * Objects Glossary::
! 2976: @end menu
! 2977:
! 2978: Marcel Hendrix provided helpful comments on this section. Andras Zsoter
! 2979: and Bernd Paysan helped me with the related works section.
! 2980:
! 2981: @node Properties of the Objects model, Why object-oriented programming?, Objects, Objects
! 2982: @subsection Properties of the @file{objects.fs} model
! 2983: @cindex @file{objects.fs} properties
! 2984:
! 2985: @itemize @bullet
! 2986: @item
! 2987: It is straightforward to pass objects on the stack. Passing
! 2988: selectors on the stack is a little less convenient, but possible.
! 2989:
! 2990: @item
! 2991: Objects are just data structures in memory, and are referenced by
! 2992: their address. You can create words for objects with normal defining
! 2993: words like @code{constant}. Likewise, there is no difference
! 2994: between instance variables that contain objects and those
! 2995: that contain other data.
! 2996:
! 2997: @item
! 2998: Late binding is efficient and easy to use.
! 2999:
! 3000: @item
! 3001: It avoids parsing, and thus avoids problems with state-smartness
! 3002: and reduced extensibility; for convenience there are a few parsing
! 3003: words, but they have non-parsing counterparts. There are also a few
! 3004: defining words that parse. This is hard to avoid, because all standard
! 3005: defining words parse (except @code{:noname}); however, such
! 3006: words are not as bad as many other parsing words, because they are not
! 3007: state-smart.
! 3008:
! 3009: @item
! 3010: It does not try to incorporate everything. It does a few things
! 3011: and does them well (IMO). In particular, I did not intend to support
! 3012: information hiding with this model (although it has features that may
! 3013: help); you can use a separate package for achieving this.
! 3014:
! 3015: @item
! 3016: It is layered; you don't have to learn and use all features to use this
! 3017: model. Only a few features are necessary (@xref{Basic Objects Usage},
! 3018: @xref{The class Object}, @xref{Creating objects}.), the others
! 3019: are optional and independent of each other.
! 3020:
! 3021: @item
! 3022: An implementation in ANS Forth is available.
! 3023:
! 3024: @end itemize
! 3025:
! 3026: I have used the technique, on which this model is based, for
! 3027: implementing the parser generator Gray; we have also used this technique
! 3028: in Gforth for implementing the various flavours of wordlists (hashed or
! 3029: not, case-sensitive or not, special-purpose wordlists for locals etc.).
! 3030:
! 3031: @node Why object-oriented programming?, Object-Oriented Terminology, Properties of the Objects model, Objects
! 3032: @subsection Why object-oriented programming?
! 3033: @cindex object-oriented programming motivation
! 3034: @cindex motivation for object-oriented programming
! 3035:
! 3036: Often we have to deal with several data structures (@emph{objects}),
! 3037: that have to be treated similarly in some respects, but differ in
! 3038: others. Graphical objects are the textbook example: circles,
! 3039: triangles, dinosaurs, icons, and others, and we may want to add more
! 3040: during program development. We want to apply some operations to any
! 3041: graphical object, e.g., @code{draw} for displaying it on the
! 3042: screen. However, @code{draw} has to do something different for
! 3043: every kind of object.
! 3044:
! 3045: We could implement @code{draw} as a big @code{CASE}
! 3046: control structure that executes the appropriate code depending on the
! 3047: kind of object to be drawn. This would be not be very elegant, and,
! 3048: moreover, we would have to change @code{draw} every time we add
! 3049: a new kind of graphical object (say, a spaceship).
! 3050:
! 3051: What we would rather do is: When defining spaceships, we would tell
! 3052: the system: "Here's how you @code{draw} a spaceship; you figure
! 3053: out the rest."
! 3054:
! 3055: This is the problem that all systems solve that (rightfully) call
! 3056: themselves object-oriented, and the object-oriented package I present
! 3057: here also solves this problem (and not much else).
! 3058:
! 3059: @node Object-Oriented Terminology, Basic Objects Usage, Why object-oriented programming?, Objects
! 3060: @subsection Object-Oriented Terminology
! 3061: @cindex object-oriented terminology
! 3062: @cindex terminology for object-oriented programming
! 3063:
! 3064: This section is mainly for reference, so you don't have to understand
! 3065: all of it right away. The terminology is mainly Smalltalk-inspired. In
! 3066: short:
! 3067:
! 3068: @table @emph
! 3069: @cindex class
! 3070: @item class
! 3071: a data structure definition with some extras.
! 3072:
! 3073: @cindex object
! 3074: @item object
! 3075: an instance of the data structure described by the class definition.
! 3076:
! 3077: @cindex instance variables
! 3078: @item instance variables
! 3079: fields of the data structure.
! 3080:
! 3081: @cindex selector
! 3082: @cindex method selector
! 3083: @cindex virtual function
! 3084: @item selector
! 3085: (or @emph{method selector}) a word (e.g.,
! 3086: @code{draw}) for performing an operation on a variety of data
! 3087: structures (classes). A selector describes @emph{what} operation to
! 3088: perform. In C++ terminology: a (pure) virtual function.
! 3089:
! 3090: @cindex method
! 3091: @item method
! 3092: the concrete definition that performs the operation
! 3093: described by the selector for a specific class. A method specifies
! 3094: @emph{how} the operation is performed for a specific class.
! 3095:
! 3096: @cindex selector invocation
! 3097: @cindex message send
! 3098: @cindex invoking a selector
! 3099: @item selector invocation
! 3100: a call of a selector. One argument of the call (the TOS (top-of-stack))
! 3101: is used for determining which method is used. In Smalltalk terminology:
! 3102: a message (consisting of the selector and the other arguments) is sent
! 3103: to the object.
! 3104:
! 3105: @cindex receiving object
! 3106: @item receiving object
! 3107: the object used for determining the method executed by a selector
! 3108: invocation. In our model it is the object that is on the TOS when the
! 3109: selector is invoked. (@emph{Receiving} comes from Smalltalks
! 3110: @emph{message} terminology.)
! 3111:
! 3112: @cindex child class
! 3113: @cindex parent class
! 3114: @cindex inheritance
! 3115: @item child class
! 3116: a class that has (@emph{inherits}) all properties (instance variables,
! 3117: selectors, methods) from a @emph{parent class}. In Smalltalk
! 3118: terminology: The subclass inherits from the superclass. In C++
! 3119: terminology: The derived class inherits from the base class.
! 3120:
! 3121: @end table
! 3122:
! 3123: @c If you wonder about the message sending terminology, it comes from
! 3124: @c a time when each object had it's own task and objects communicated via
! 3125: @c message passing; eventually the Smalltalk developers realized that
! 3126: @c they can do most things through simple (indirect) calls. They kept the
! 3127: @c terminology.
! 3128:
! 3129: @node Basic Objects Usage, The class Object, Object-Oriented Terminology, Objects
! 3130: @subsection Basic Objects Usage
! 3131: @cindex basic objects usage
! 3132: @cindex objects, basic usage
! 3133:
! 3134: You can define a class for graphical objects like this:
! 3135:
! 3136: @cindex @code{class} usage
! 3137: @cindex @code{end-class} usage
! 3138: @cindex @code{selector} usage
! 3139: @example
! 3140: object class \ "object" is the parent class
! 3141: selector draw ( x y graphical -- )
! 3142: end-class graphical
! 3143: @end example
! 3144:
! 3145: This code defines a class @code{graphical} with an
! 3146: operation @code{draw}. We can perform the operation
! 3147: @code{draw} on any @code{graphical} object, e.g.:
! 3148:
! 3149: @example
! 3150: 100 100 t-rex draw
! 3151: @end example
! 3152:
! 3153: where @code{t-rex} is a word (say, a constant) that produces a
! 3154: graphical object.
! 3155:
! 3156: @cindex abstract class
! 3157: How do we create a graphical object? With the present definitions,
! 3158: we cannot create a useful graphical object. The class
! 3159: @code{graphical} describes graphical objects in general, but not
! 3160: any concrete graphical object type (C++ users would call it an
! 3161: @emph{abstract class}); e.g., there is no method for the selector
! 3162: @code{draw} in the class @code{graphical}.
! 3163:
! 3164: For concrete graphical objects, we define child classes of the
! 3165: class @code{graphical}, e.g.:
! 3166:
! 3167: @cindex @code{overrides} usage
! 3168: @cindex @code{field} usage in class definition
! 3169: @example
! 3170: graphical class \ "graphical" is the parent class
! 3171: cell% field circle-radius
! 3172:
! 3173: :noname ( x y circle -- )
! 3174: circle-radius @@ draw-circle ;
! 3175: overrides draw
! 3176:
! 3177: :noname ( n-radius circle -- )
! 3178: circle-radius ! ;
! 3179: overrides construct
! 3180:
! 3181: end-class circle
! 3182: @end example
! 3183:
! 3184: Here we define a class @code{circle} as a child of @code{graphical},
! 3185: with a field @code{circle-radius} (which behaves just like a field in
! 3186: @pxref{Structures}); it defines new methods for the selectors
! 3187: @code{draw} and @code{construct} (@code{construct} is defined in
! 3188: @code{object}, the parent class of @code{graphical}).
! 3189:
! 3190: Now we can create a circle on the heap (i.e.,
! 3191: @code{allocate}d memory) with
! 3192:
! 3193: @cindex @code{heap-new} usage
! 3194: @example
! 3195: 50 circle heap-new constant my-circle
! 3196: @end example
! 3197:
! 3198: @code{heap-new} invokes @code{construct}, thus
! 3199: initializing the field @code{circle-radius} with 50. We can draw
! 3200: this new circle at (100,100) with
! 3201:
! 3202: @example
! 3203: 100 100 my-circle draw
! 3204: @end example
! 3205:
! 3206: @cindex selector invocation, restrictions
! 3207: @cindex class definition, restrictions
! 3208: Note: You can invoke a selector only if the object on the TOS
! 3209: (the receiving object) belongs to the class where the selector was
! 3210: defined or one of its descendents; e.g., you can invoke
! 3211: @code{draw} only for objects belonging to @code{graphical}
! 3212: or its descendents (e.g., @code{circle}). Immediately before
! 3213: @code{end-class}, the search order has to be the same as
! 3214: immediately after @code{class}.
! 3215:
! 3216: @node The class Object, Creating objects, Basic Objects Usage, Objects
! 3217: @subsection The class @code{object}
! 3218: @cindex @code{object} class
! 3219:
! 3220: When you define a class, you have to specify a parent class. So how do
! 3221: you start defining classes? There is one class available from the start:
! 3222: @code{object}. You can use it as ancestor for all classes. It is the
! 3223: only class that has no parent. It has two selectors: @code{construct}
! 3224: and @code{print}.
! 3225:
! 3226: @node Creating objects, Object-Oriented Programming Style, The class Object, Objects
! 3227: @subsection Creating objects
! 3228: @cindex creating objects
! 3229: @cindex object creation
! 3230: @cindex object allocation options
! 3231:
! 3232: @cindex @code{heap-new} discussion
! 3233: @cindex @code{dict-new} discussion
! 3234: @cindex @code{construct} discussion
! 3235: You can create and initialize an object of a class on the heap with
! 3236: @code{heap-new} ( ... class -- object ) and in the dictionary
! 3237: (allocation with @code{allot}) with @code{dict-new} (
! 3238: ... class -- object ). Both words invoke @code{construct}, which
! 3239: consumes the stack items indicated by "..." above.
! 3240:
! 3241: @cindex @code{init-object} discussion
! 3242: @cindex @code{class-inst-size} discussion
! 3243: If you want to allocate memory for an object yourself, you can get its
! 3244: alignment and size with @code{class-inst-size 2@@} ( class --
! 3245: align size ). Once you have memory for an object, you can initialize
! 3246: it with @code{init-object} ( ... class object -- );
! 3247: @code{construct} does only a part of the necessary work.
! 3248:
! 3249: @node Object-Oriented Programming Style, Class Binding, Creating objects, Objects
! 3250: @subsection Object-Oriented Programming Style
! 3251: @cindex object-oriented programming style
! 3252:
! 3253: This section is not exhaustive.
! 3254:
! 3255: @cindex stack effects of selectors
! 3256: @cindex selectors and stack effects
! 3257: In general, it is a good idea to ensure that all methods for the
! 3258: same selector have the same stack effect: when you invoke a selector,
! 3259: you often have no idea which method will be invoked, so, unless all
! 3260: methods have the same stack effect, you will not know the stack effect
! 3261: of the selector invocation.
! 3262:
! 3263: One exception to this rule is methods for the selector
! 3264: @code{construct}. We know which method is invoked, because we
! 3265: specify the class to be constructed at the same place. Actually, I
! 3266: defined @code{construct} as a selector only to give the users a
! 3267: convenient way to specify initialization. The way it is used, a
! 3268: mechanism different from selector invocation would be more natural
! 3269: (but probably would take more code and more space to explain).
! 3270:
! 3271: @node Class Binding, Method conveniences, Object-Oriented Programming Style, Objects
! 3272: @subsection Class Binding
! 3273: @cindex class binding
! 3274: @cindex early binding
! 3275:
! 3276: @cindex late binding
! 3277: Normal selector invocations determine the method at run-time depending
! 3278: on the class of the receiving object (late binding).
! 3279:
! 3280: Sometimes we want to invoke a different method. E.g., assume that
! 3281: you want to use the simple method for @code{print}ing
! 3282: @code{object}s instead of the possibly long-winded
! 3283: @code{print} method of the receiver class. You can achieve this
! 3284: by replacing the invocation of @code{print} with
! 3285:
! 3286: @cindex @code{[bind]} usage
! 3287: @example
! 3288: [bind] object print
! 3289: @end example
! 3290:
! 3291: in compiled code or
! 3292:
! 3293: @cindex @code{bind} usage
! 3294: @example
! 3295: bind object print
! 3296: @end example
! 3297:
! 3298: @cindex class binding, alternative to
! 3299: in interpreted code. Alternatively, you can define the method with a
! 3300: name (e.g., @code{print-object}), and then invoke it through the
! 3301: name. Class binding is just a (often more convenient) way to achieve
! 3302: the same effect; it avoids name clutter and allows you to invoke
! 3303: methods directly without naming them first.
! 3304:
! 3305: @cindex superclass binding
! 3306: @cindex parent class binding
! 3307: A frequent use of class binding is this: When we define a method
! 3308: for a selector, we often want the method to do what the selector does
! 3309: in the parent class, and a little more. There is a special word for
! 3310: this purpose: @code{[parent]}; @code{[parent]
! 3311: @emph{selector}} is equivalent to @code{[bind] @emph{parent
! 3312: selector}}, where @code{@emph{parent}} is the parent
! 3313: class of the current class. E.g., a method definition might look like:
! 3314:
! 3315: @cindex @code{[parent]} usage
! 3316: @example
! 3317: :noname
! 3318: dup [parent] foo \ do parent's foo on the receiving object
! 3319: ... \ do some more
! 3320: ; overrides foo
! 3321: @end example
! 3322:
! 3323: @cindex class binding as optimization
! 3324: In @cite{Object-oriented programming in ANS Forth} (Forth Dimensions,
! 3325: March 1997), Andrew McKewan presents class binding as an optimization
! 3326: technique. I recommend not using it for this purpose unless you are in
! 3327: an emergency. Late binding is pretty fast with this model anyway, so the
! 3328: benefit of using class binding is small; the cost of using class binding
! 3329: where it is not appropriate is reduced maintainability.
! 3330:
! 3331: While we are at programming style questions: You should bind
! 3332: selectors only to ancestor classes of the receiving object. E.g., say,
! 3333: you know that the receiving object is of class @code{foo} or its
! 3334: descendents; then you should bind only to @code{foo} and its
! 3335: ancestors.
! 3336:
! 3337: @node Method conveniences, Classes and Scoping, Class Binding, Objects
! 3338: @subsection Method conveniences
! 3339: @cindex method conveniences
! 3340:
! 3341: In a method you usually access the receiving object pretty often. If
! 3342: you define the method as a plain colon definition (e.g., with
! 3343: @code{:noname}), you may have to do a lot of stack
! 3344: gymnastics. To avoid this, you can define the method with @code{m:
! 3345: ... ;m}. E.g., you could define the method for
! 3346: @code{draw}ing a @code{circle} with
! 3347:
! 3348: @cindex @code{this} usage
! 3349: @cindex @code{m:} usage
! 3350: @cindex @code{;m} usage
! 3351: @example
! 3352: m: ( x y circle -- )
! 3353: ( x y ) this circle-radius @@ draw-circle ;m
! 3354: @end example
! 3355:
! 3356: @cindex @code{exit} in @code{m: ... ;m}
! 3357: @cindex @code{exitm} discussion
! 3358: @cindex @code{catch} in @code{m: ... ;m}
! 3359: When this method is executed, the receiver object is removed from the
! 3360: stack; you can access it with @code{this} (admittedly, in this
! 3361: example the use of @code{m: ... ;m} offers no advantage). Note
! 3362: that I specify the stack effect for the whole method (i.e. including
! 3363: the receiver object), not just for the code between @code{m:}
! 3364: and @code{;m}. You cannot use @code{exit} in
! 3365: @code{m:...;m}; instead, use
! 3366: @code{exitm}.@footnote{Moreover, for any word that calls
! 3367: @code{catch} and was defined before loading
! 3368: @code{objects.fs}, you have to redefine it like I redefined
! 3369: @code{catch}: @code{: catch this >r catch r> to-this ;}}
! 3370:
! 3371: @cindex @code{inst-var} usage
! 3372: You will frequently use sequences of the form @code{this
! 3373: @emph{field}} (in the example above: @code{this
! 3374: circle-radius}). If you use the field only in this way, you can
! 3375: define it with @code{inst-var} and eliminate the
! 3376: @code{this} before the field name. E.g., the @code{circle}
! 3377: class above could also be defined with:
! 3378:
! 3379: @example
! 3380: graphical class
! 3381: cell% inst-var radius
! 3382:
! 3383: m: ( x y circle -- )
! 3384: radius @@ draw-circle ;m
! 3385: overrides draw
! 3386:
! 3387: m: ( n-radius circle -- )
! 3388: radius ! ;m
! 3389: overrides construct
! 3390:
! 3391: end-class circle
! 3392: @end example
! 3393:
! 3394: @code{radius} can only be used in @code{circle} and its
! 3395: descendent classes and inside @code{m:...;m}.
! 3396:
! 3397: @cindex @code{inst-value} usage
! 3398: You can also define fields with @code{inst-value}, which is
! 3399: to @code{inst-var} what @code{value} is to
! 3400: @code{variable}. You can change the value of such a field with
! 3401: @code{[to-inst]}. E.g., we could also define the class
! 3402: @code{circle} like this:
! 3403:
! 3404: @example
! 3405: graphical class
! 3406: inst-value radius
! 3407:
! 3408: m: ( x y circle -- )
! 3409: radius draw-circle ;m
! 3410: overrides draw
! 3411:
! 3412: m: ( n-radius circle -- )
! 3413: [to-inst] radius ;m
! 3414: overrides construct
! 3415:
! 3416: end-class circle
! 3417: @end example
! 3418:
! 3419:
! 3420: @node Classes and Scoping, Object Interfaces, Method conveniences, Objects
! 3421: @subsection Classes and Scoping
! 3422: @cindex classes and scoping
! 3423: @cindex scoping and classes
! 3424:
! 3425: Inheritance is frequent, unlike structure extension. This exacerbates
! 3426: the problem with the field name convention (@pxref{Structure Naming
! 3427: Convention}): One always has to remember in which class the field was
! 3428: originally defined; changing a part of the class structure would require
! 3429: changes for renaming in otherwise unaffected code.
! 3430:
! 3431: @cindex @code{inst-var} visibility
! 3432: @cindex @code{inst-value} visibility
! 3433: To solve this problem, I added a scoping mechanism (which was not in my
! 3434: original charter): A field defined with @code{inst-var} (or
! 3435: @code{inst-value}) is visible only in the class where it is defined and in
! 3436: the descendent classes of this class. Using such fields only makes
! 3437: sense in @code{m:}-defined methods in these classes anyway.
! 3438:
! 3439: This scoping mechanism allows us to use the unadorned field name,
! 3440: because name clashes with unrelated words become much less likely.
! 3441:
! 3442: @cindex @code{protected} discussion
! 3443: @cindex @code{private} discussion
! 3444: Once we have this mechanism, we can also use it for controlling the
! 3445: visibility of other words: All words defined after
! 3446: @code{protected} are visible only in the current class and its
! 3447: descendents. @code{public} restores the compilation
! 3448: (i.e. @code{current}) wordlist that was in effect before. If you
! 3449: have several @code{protected}s without an intervening
! 3450: @code{public} or @code{set-current}, @code{public}
! 3451: will restore the compilation wordlist in effect before the first of
! 3452: these @code{protected}s.
! 3453:
! 3454: @node Object Interfaces, Objects Implementation, Classes and Scoping, Objects
! 3455: @subsection Object Interfaces
! 3456: @cindex object interfaces
! 3457: @cindex interfaces for objects
! 3458:
! 3459: In this model you can only call selectors defined in the class of the
! 3460: receiving objects or in one of its ancestors. If you call a selector
! 3461: with a receiving object that is not in one of these classes, the
! 3462: result is undefined; if you are lucky, the program crashes
! 3463: immediately.
! 3464:
! 3465: @cindex selectors common to hardly-related classes
! 3466: Now consider the case when you want to have a selector (or several)
! 3467: available in two classes: You would have to add the selector to a
! 3468: common ancestor class, in the worst case to @code{object}. You
! 3469: may not want to do this, e.g., because someone else is responsible for
! 3470: this ancestor class.
! 3471:
! 3472: The solution for this problem is interfaces. An interface is a
! 3473: collection of selectors. If a class implements an interface, the
! 3474: selectors become available to the class and its descendents. A class
! 3475: can implement an unlimited number of interfaces. For the problem
! 3476: discussed above, we would define an interface for the selector(s), and
! 3477: both classes would implement the interface.
! 3478:
! 3479: As an example, consider an interface @code{storage} for
! 3480: writing objects to disk and getting them back, and a class
! 3481: @code{foo} foo that implements it. The code for this would look
! 3482: like this:
! 3483:
! 3484: @cindex @code{interface} usage
! 3485: @cindex @code{end-interface} usage
! 3486: @cindex @code{implementation} usage
! 3487: @example
! 3488: interface
! 3489: selector write ( file object -- )
! 3490: selector read1 ( file object -- )
! 3491: end-interface storage
! 3492:
! 3493: bar class
! 3494: storage implementation
! 3495:
! 3496: ... overrides write
! 3497: ... overrides read
! 3498: ...
! 3499: end-class foo
! 3500: @end example
! 3501:
! 3502: (I would add a word @code{read} ( file -- object ) that uses
! 3503: @code{read1} internally, but that's beyond the point illustrated
! 3504: here.)
! 3505:
! 3506: Note that you cannot use @code{protected} in an interface; and
! 3507: of course you cannot define fields.
! 3508:
! 3509: In the Neon model, all selectors are available for all classes;
! 3510: therefore it does not need interfaces. The price you pay in this model
! 3511: is slower late binding, and therefore, added complexity to avoid late
! 3512: binding.
! 3513:
! 3514: @node Objects Implementation, Comparison with other object models, Object Interfaces, Objects
! 3515: @subsection @file{objects.fs} Implementation
! 3516: @cindex @file{objects.fs} implementation
! 3517:
! 3518: @cindex @code{object-map} discussion
! 3519: An object is a piece of memory, like one of the data structures
! 3520: described with @code{struct...end-struct}. It has a field
! 3521: @code{object-map} that points to the method map for the object's
! 3522: class.
! 3523:
! 3524: @cindex method map
! 3525: @cindex virtual function table
! 3526: The @emph{method map}@footnote{This is Self terminology; in C++
! 3527: terminology: virtual function table.} is an array that contains the
! 3528: execution tokens (XTs) of the methods for the object's class. Each
! 3529: selector contains an offset into the method maps.
! 3530:
! 3531: @cindex @code{selector} implementation, class
! 3532: @code{selector} is a defining word that uses
! 3533: @code{create} and @code{does>}. The body of the
! 3534: selector contains the offset; the @code{does>} action for a
! 3535: class selector is, basically:
! 3536:
! 3537: @example
! 3538: ( object addr ) @@ over object-map @@ + @@ execute
! 3539: @end example
! 3540:
! 3541: Since @code{object-map} is the first field of the object, it
! 3542: does not generate any code. As you can see, calling a selector has a
! 3543: small, constant cost.
! 3544:
! 3545: @cindex @code{current-interface} discussion
! 3546: @cindex class implementation and representation
! 3547: A class is basically a @code{struct} combined with a method
! 3548: map. During the class definition the alignment and size of the class
! 3549: are passed on the stack, just as with @code{struct}s, so
! 3550: @code{field} can also be used for defining class
! 3551: fields. However, passing more items on the stack would be
! 3552: inconvenient, so @code{class} builds a data structure in memory,
! 3553: which is accessed through the variable
! 3554: @code{current-interface}. After its definition is complete, the
! 3555: class is represented on the stack by a pointer (e.g., as parameter for
! 3556: a child class definition).
! 3557:
! 3558: At the start, a new class has the alignment and size of its parent,
! 3559: and a copy of the parent's method map. Defining new fields extends the
! 3560: size and alignment; likewise, defining new selectors extends the
! 3561: method map. @code{overrides} just stores a new XT in the method
! 3562: map at the offset given by the selector.
! 3563:
! 3564: @cindex class binding, implementation
! 3565: Class binding just gets the XT at the offset given by the selector
! 3566: from the class's method map and @code{compile,}s (in the case of
! 3567: @code{[bind]}) it.
! 3568:
! 3569: @cindex @code{this} implementation
! 3570: @cindex @code{catch} and @code{this}
! 3571: @cindex @code{this} and @code{catch}
! 3572: I implemented @code{this} as a @code{value}. At the
! 3573: start of an @code{m:...;m} method the old @code{this} is
! 3574: stored to the return stack and restored at the end; and the object on
! 3575: the TOS is stored @code{TO this}. This technique has one
! 3576: disadvantage: If the user does not leave the method via
! 3577: @code{;m}, but via @code{throw} or @code{exit},
! 3578: @code{this} is not restored (and @code{exit} may
! 3579: crash). To deal with the @code{throw} problem, I have redefined
! 3580: @code{catch} to save and restore @code{this}; the same
! 3581: should be done with any word that can catch an exception. As for
! 3582: @code{exit}, I simply forbid it (as a replacement, there is
! 3583: @code{exitm}).
! 3584:
! 3585: @cindex @code{inst-var} implementation
! 3586: @code{inst-var} is just the same as @code{field}, with
! 3587: a different @code{does>} action:
! 3588: @example
! 3589: @@ this +
! 3590: @end example
! 3591: Similar for @code{inst-value}.
! 3592:
! 3593: @cindex class scoping implementation
! 3594: Each class also has a wordlist that contains the words defined with
! 3595: @code{inst-var} and @code{inst-value}, and its protected
! 3596: words. It also has a pointer to its parent. @code{class} pushes
! 3597: the wordlists of the class an all its ancestors on the search order,
! 3598: and @code{end-class} drops them.
! 3599:
! 3600: @cindex interface implementation
! 3601: An interface is like a class without fields, parent and protected
! 3602: words; i.e., it just has a method map. If a class implements an
! 3603: interface, its method map contains a pointer to the method map of the
! 3604: interface. The positive offsets in the map are reserved for class
! 3605: methods, therefore interface map pointers have negative
! 3606: offsets. Interfaces have offsets that are unique throughout the
! 3607: system, unlike class selectors, whose offsets are only unique for the
! 3608: classes where the selector is available (invokable).
! 3609:
! 3610: This structure means that interface selectors have to perform one
! 3611: indirection more than class selectors to find their method. Their body
! 3612: contains the interface map pointer offset in the class method map, and
! 3613: the method offset in the interface method map. The
! 3614: @code{does>} action for an interface selector is, basically:
! 3615:
! 3616: @example
! 3617: ( object selector-body )
! 3618: 2dup selector-interface @@ ( object selector-body object interface-offset )
! 3619: swap object-map @@ + @@ ( object selector-body map )
! 3620: swap selector-offset @@ + @@ execute
! 3621: @end example
! 3622:
! 3623: where @code{object-map} and @code{selector-offset} are
! 3624: first fields and generate no code.
! 3625:
! 3626: As a concrete example, consider the following code:
! 3627:
! 3628: @example
! 3629: interface
! 3630: selector if1sel1
! 3631: selector if1sel2
! 3632: end-interface if1
! 3633:
! 3634: object class
! 3635: if1 implementation
! 3636: selector cl1sel1
! 3637: cell% inst-var cl1iv1
! 3638:
! 3639: ' m1 overrides construct
! 3640: ' m2 overrides if1sel1
! 3641: ' m3 overrides if1sel2
! 3642: ' m4 overrides cl1sel2
! 3643: end-class cl1
! 3644:
! 3645: create obj1 object dict-new drop
! 3646: create obj2 cl1 dict-new drop
! 3647: @end example
! 3648:
! 3649: The data structure created by this code (including the data structure
! 3650: for @code{object}) is shown in the <a
! 3651: href="objects-implementation.eps">figure</a>, assuming a cell size of 4.
! 3652:
! 3653: @node Comparison with other object models, Objects Glossary, Objects Implementation, Objects
! 3654: @subsection Comparison with other object models
! 3655: @cindex comparison of object models
! 3656: @cindex object models, comparison
! 3657:
! 3658: Many object-oriented Forth extensions have been proposed (@cite{A survey
! 3659: of object-oriented Forths} (SIGPLAN Notices, April 1996) by Bradford
! 3660: J. Rodriguez and W. F. S. Poehlman lists 17). Here I'll discuss the
! 3661: relation of @file{objects.fs} to two well-known and two closely-related
! 3662: (by the use of method maps) models.
! 3663:
! 3664: @cindex Neon model
! 3665: The most popular model currently seems to be the Neon model (see
! 3666: @cite{Object-oriented programming in ANS Forth} (Forth Dimensions, March
! 3667: 1997) by Andrew McKewan). The Neon model uses a @code{@emph{selector
! 3668: object}} syntax, which makes it unnatural to pass objects on the
! 3669: stack. It also requires that the selector parses the input stream (at
! 3670: compile time); this leads to reduced extensibility and to bugs that are
! 3671: hard to find. Finally, it allows using every selector to every object;
! 3672: this eliminates the need for classes, but makes it harder to create
! 3673: efficient implementations. A longer version of this critique can be
! 3674: found in @cite{On Standardizing Object-Oriented Forth Extensions} (Forth
! 3675: Dimensions, May 1997) by Anton Ertl.
! 3676:
! 3677: @cindex Pountain's object-oriented model
! 3678: Another well-known publication is @cite{Object-Oriented Forth} (Academic
! 3679: Press, London, 1987) by Dick Pountain. However, it is not really about
! 3680: object-oriented programming, because it hardly deals with late
! 3681: binding. Instead, it focuses on features like information hiding and
! 3682: overloading that are characteristic of modular languages like Ada (83).
! 3683:
! 3684: @cindex Zsoter's object-oriented model
! 3685: In @cite{Does late binding have to be slow?} (Forth Dimensions ??? 1996)
! 3686: Andras Zsoter describes a model that makes heavy use of an active object
! 3687: (like @code{this} in @file{objects.fs}): The active object is not only
! 3688: used for accessing all fields, but also specifies the receiving object
! 3689: of every selector invocation; you have to change the active object
! 3690: explicitly with @code{@{ ... @}}, whereas in @file{objects.fs} it
! 3691: changes more or less implicitly at @code{m: ... ;m}. Such a change at
! 3692: the method entry point is unnecessary with the Zsoter's model, because
! 3693: the receiving object is the active object already; OTOH, the explicit
! 3694: change is absolutely necessary in that model, because otherwise no one
! 3695: could ever change the active object. An ANS Forth implementation of this
! 3696: model is available at @url{http://www.forth.org/fig/oopf.html}.
! 3697:
! 3698: @cindex @file{oof.fs} object model
! 3699: The @file{oof.fs} model combines information hiding and overloading
! 3700: resolution (by keeping names in various wordlists) with object-oriented
! 3701: programming. It sets the active object implicitly on method entry, but
! 3702: also allows explicit changing (with @code{>o...o>} or with
! 3703: @code{with...endwith}). It uses parsing and state-smart objects and
! 3704: classes for resolving overloading and for early binding: the object or
! 3705: class parses the selector and determines the method from this. If the
! 3706: selector is not parsed by an object or class, it performs a call to the
! 3707: selector for the active object (late binding), like Zsoter's model.
! 3708: Fields are always accessed through the active object. The big
! 3709: disadvantage of this model is the parsing and the state-smartness, which
! 3710: reduces extensibility and increases the opportunities for subtle bugs;
! 3711: essentially, you are only safe if you never tick or @code{postpone} an
! 3712: object or class.
! 3713:
! 3714: @node Objects Glossary, , Comparison with other object models, Objects
! 3715: @subsection @file{objects.fs} Glossary
! 3716: @cindex @file{objects.fs} Glossary
! 3717:
! 3718: doc-bind
! 3719: doc-<bind>
! 3720: doc-bind'
! 3721: doc-[bind]
! 3722: doc-class
! 3723: doc-class->map
! 3724: doc-class-inst-size
! 3725: doc-class-override!
! 3726: doc-construct
! 3727: doc-current'
! 3728: doc-[current]
! 3729: doc-current-interface
! 3730: doc-dict-new
! 3731: doc-drop-order
! 3732: doc-end-class
! 3733: doc-end-class-noname
! 3734: doc-end-interface
! 3735: doc-end-interface-noname
! 3736: doc-exitm
! 3737: doc-heap-new
! 3738: doc-implementation
! 3739: doc-init-object
! 3740: doc-inst-value
! 3741: doc-inst-var
! 3742: doc-interface
! 3743: doc-;m
! 3744: doc-m:
! 3745: doc-method
! 3746: doc-object
! 3747: doc-overrides
! 3748: doc-[parent]
! 3749: doc-print
! 3750: doc-protected
! 3751: doc-public
! 3752: doc-push-order
! 3753: doc-selector
! 3754: doc-this
! 3755: doc-<to-inst>
! 3756: doc-[to-inst]
! 3757: doc-to-this
! 3758: doc-xt-new
! 3759:
! 3760: @c -------------------------------------------------------------
! 3761: @node Tokens for Words, Wordlists, Objects, Words
1.1 anton 3762: @section Tokens for Words
3763: @cindex tokens for words
3764:
3765: This chapter describes the creation and use of tokens that represent
3766: words on the stack (and in data space).
3767:
3768: Named words have interpretation and compilation semantics. Unnamed words
3769: just have execution semantics.
3770:
3771: @cindex execution token
3772: An @dfn{execution token} represents the execution semantics of an
3773: unnamed word. An execution token occupies one cell. As explained in
3774: section @ref{Supplying names}, the execution token of the last words
3775: defined can be produced with
3776:
3777: short-lastxt
3778:
3779: You can perform the semantics represented by an execution token with
3780: doc-execute
3781: You can compile the word with
3782: doc-compile,
3783:
3784: @cindex code field address
3785: @cindex CFA
3786: In Gforth, the abstract data type @emph{execution token} is implemented
3787: as CFA (code field address).
3788:
3789: The interpretation semantics of a named word are also represented by an
3790: execution token. You can get it with
3791:
3792: doc-[']
3793: doc-'
3794:
3795: For literals, you use @code{'} in interpreted code and @code{[']} in
3796: compiled code. Gforth's @code{'} and @code{[']} behave somewhat unusual
3797: by complaining about compile-only words. To get an execution token for a
3798: compiling word @var{X}, use @code{COMP' @var{X} drop} or @code{[COMP']
3799: @var{X} drop}.
3800:
3801: @cindex compilation token
3802: The compilation semantics are represented by a @dfn{compilation token}
3803: consisting of two cells: @var{w xt}. The top cell @var{xt} is an
3804: execution token. The compilation semantics represented by the
3805: compilation token can be performed with @code{execute}, which consumes
3806: the whole compilation token, with an additional stack effect determined
3807: by the represented compilation semantics.
3808:
3809: doc-[comp']
3810: doc-comp'
3811:
3812: You can compile the compilation semantics with @code{postpone,}. I.e.,
3813: @code{COMP' @var{word} POSTPONE,} is equivalent to @code{POSTPONE
3814: @var{word}}.
3815:
3816: doc-postpone,
3817:
3818: At present, the @var{w} part of a compilation token is an execution
3819: token, and the @var{xt} part represents either @code{execute} or
3820: @code{compile,}. However, don't rely on that knowledge, unless necessary;
3821: we may introduce unusual compilation tokens in the future (e.g.,
3822: compilation tokens representing the compilation semantics of literals).
3823:
3824: @cindex name token
3825: @cindex name field address
3826: @cindex NFA
3827: Named words are also represented by the @dfn{name token}. The abstract
3828: data type @emph{name token} is implemented as NFA (name field address).
3829:
3830: doc-find-name
3831: doc-name>int
3832: doc-name?int
3833: doc-name>comp
3834: doc-name>string
3835:
3836: @node Wordlists, Files, Tokens for Words, Words
3837: @section Wordlists
3838:
3839: @node Files, Blocks, Wordlists, Words
3840: @section Files
3841:
3842: @node Blocks, Other I/O, Files, Words
3843: @section Blocks
3844:
3845: @node Other I/O, Programming Tools, Blocks, Words
3846: @section Other I/O
3847:
3848: @node Programming Tools, Assembler and Code words, Other I/O, Words
3849: @section Programming Tools
3850: @cindex programming tools
3851:
3852: @menu
3853: * Debugging:: Simple and quick.
3854: * Assertions:: Making your programs self-checking.
1.2 jwilke 3855: * Singlestep debugger:: Executing your program word by word.
1.1 anton 3856: @end menu
3857:
3858: @node Debugging, Assertions, Programming Tools, Programming Tools
3859: @subsection Debugging
3860: @cindex debugging
3861:
1.2 jwilke 3862: The simple debugging aids provided in @file{debugs.fs}
1.1 anton 3863: are meant to support a different style of debugging than the
3864: tracing/stepping debuggers used in languages with long turn-around
3865: times.
3866:
3867: A much better (faster) way in fast-compiling languages is to add
3868: printing code at well-selected places, let the program run, look at
3869: the output, see where things went wrong, add more printing code, etc.,
3870: until the bug is found.
3871:
3872: The word @code{~~} is easy to insert. It just prints debugging
3873: information (by default the source location and the stack contents). It
3874: is also easy to remove (@kbd{C-x ~} in the Emacs Forth mode to
3875: query-replace them with nothing). The deferred words
3876: @code{printdebugdata} and @code{printdebugline} control the output of
3877: @code{~~}. The default source location output format works well with
3878: Emacs' compilation mode, so you can step through the program at the
3879: source level using @kbd{C-x `} (the advantage over a stepping debugger
3880: is that you can step in any direction and you know where the crash has
3881: happened or where the strange data has occurred).
3882:
3883: Note that the default actions clobber the contents of the pictured
3884: numeric output string, so you should not use @code{~~}, e.g., between
3885: @code{<#} and @code{#>}.
3886:
3887: doc-~~
3888: doc-printdebugdata
3889: doc-printdebugline
3890:
1.2 jwilke 3891: @node Assertions, Singlestep Debugger, Debugging, Programming Tools
1.1 anton 3892: @subsection Assertions
3893: @cindex assertions
3894:
3895: It is a good idea to make your programs self-checking, in particular, if
3896: you use an assumption (e.g., that a certain field of a data structure is
3897: never zero) that may become wrong during maintenance. Gforth supports
3898: assertions for this purpose. They are used like this:
3899:
3900: @example
3901: assert( @var{flag} )
3902: @end example
3903:
3904: The code between @code{assert(} and @code{)} should compute a flag, that
3905: should be true if everything is alright and false otherwise. It should
3906: not change anything else on the stack. The overall stack effect of the
3907: assertion is @code{( -- )}. E.g.
3908:
3909: @example
3910: assert( 1 1 + 2 = ) \ what we learn in school
3911: assert( dup 0<> ) \ assert that the top of stack is not zero
3912: assert( false ) \ this code should not be reached
3913: @end example
3914:
3915: The need for assertions is different at different times. During
3916: debugging, we want more checking, in production we sometimes care more
3917: for speed. Therefore, assertions can be turned off, i.e., the assertion
3918: becomes a comment. Depending on the importance of an assertion and the
3919: time it takes to check it, you may want to turn off some assertions and
3920: keep others turned on. Gforth provides several levels of assertions for
3921: this purpose:
3922:
3923: doc-assert0(
3924: doc-assert1(
3925: doc-assert2(
3926: doc-assert3(
3927: doc-assert(
3928: doc-)
3929:
3930: @code{Assert(} is the same as @code{assert1(}. The variable
3931: @code{assert-level} specifies the highest assertions that are turned
3932: on. I.e., at the default @code{assert-level} of one, @code{assert0(} and
3933: @code{assert1(} assertions perform checking, while @code{assert2(} and
3934: @code{assert3(} assertions are treated as comments.
3935:
3936: Note that the @code{assert-level} is evaluated at compile-time, not at
3937: run-time. I.e., you cannot turn assertions on or off at run-time, you
3938: have to set the @code{assert-level} appropriately before compiling a
3939: piece of code. You can compile several pieces of code at several
3940: @code{assert-level}s (e.g., a trusted library at level 1 and newly
3941: written code at level 3).
3942:
3943: doc-assert-level
3944:
3945: If an assertion fails, a message compatible with Emacs' compilation mode
3946: is produced and the execution is aborted (currently with @code{ABORT"}.
3947: If there is interest, we will introduce a special throw code. But if you
3948: intend to @code{catch} a specific condition, using @code{throw} is
3949: probably more appropriate than an assertion).
3950:
1.2 jwilke 3951: @node Singlestep Debugger, , Assertions, Programming Tools
3952: @subsection Singlestep Debugger
3953: @cindex singlestep Debugger
3954: @cindex debugging Singlestep
3955: @cindex @code{dbg}
3956: @cindex @code{BREAK:}
3957: @cindex @code{BREAK"}
3958:
3959: When a new word is created there's often the need to check whether it behaves
1.5 ! anton 3960: correctly or not. You can do this by typing @code{dbg badword}. This might
1.2 jwilke 3961: look like:
3962: @example
3963: : badword 0 DO i . LOOP ; ok
3964: 2 dbg badword
3965: : badword
3966: Scanning code...
3967:
3968: Nesting debugger ready!
3969:
3970: 400D4738 8049BC4 0 -> [ 2 ] 00002 00000
3971: 400D4740 8049F68 DO -> [ 0 ]
3972: 400D4744 804A0C8 i -> [ 1 ] 00000
3973: 400D4748 400C5E60 . -> 0 [ 0 ]
3974: 400D474C 8049D0C LOOP -> [ 0 ]
3975: 400D4744 804A0C8 i -> [ 1 ] 00001
3976: 400D4748 400C5E60 . -> 1 [ 0 ]
3977: 400D474C 8049D0C LOOP -> [ 0 ]
3978: 400D4758 804B384 ; -> ok
3979: @end example
3980:
1.5 ! anton 3981: Each line displayed is one step. You always have to hit return to
! 3982: execute the next word that is displayed. If you don't want to execute
! 3983: the next word in a whole, you have to type @kbd{n} for @code{nest}. Here is
! 3984: an overview what keys are available:
1.2 jwilke 3985:
3986: @table @i
3987:
1.4 anton 3988: @item <return>
1.5 ! anton 3989: Next; Execute the next word.
1.2 jwilke 3990:
3991: @item n
1.5 ! anton 3992: Nest; Single step through next word.
1.2 jwilke 3993:
3994: @item u
1.5 ! anton 3995: Unnest; Stop debugging and execute rest of word. If we got to this word
! 3996: with nest, continue debugging with the calling word.
1.2 jwilke 3997:
3998: @item d
1.5 ! anton 3999: Done; Stop debugging and execute rest.
1.2 jwilke 4000:
4001: @item s
1.5 ! anton 4002: Stopp; Abort immediately.
1.2 jwilke 4003:
4004: @end table
4005:
4006: Debugging large application with this mechanism is very difficult, because
4007: you have to nest very deep into the program before the interesting part
4008: begins. This takes a lot of time.
4009:
4010: To do it more directly put a @code{BREAK:} command into your source code.
4011: When program execution reaches @code{BREAK:} the single step debugger is
4012: invoked and you have all the features described above.
4013:
4014: If you have more than one part to debug it is useful to know where the
4015: program has stopped at the moment. You can do this by the
4016: @code{BREAK" string"} command. This behaves like @code{BREAK:} except that
4017: string is typed out when the ``breakpoint'' is reached.
4018:
1.1 anton 4019: @node Assembler and Code words, Threading Words, Programming Tools, Words
4020: @section Assembler and Code words
4021: @cindex assembler
4022: @cindex code words
4023:
4024: Gforth provides some words for defining primitives (words written in
4025: machine code), and for defining the the machine-code equivalent of
4026: @code{DOES>}-based defining words. However, the machine-independent
4027: nature of Gforth poses a few problems: First of all, Gforth runs on
4028: several architectures, so it can provide no standard assembler. What's
4029: worse is that the register allocation not only depends on the processor,
4030: but also on the @code{gcc} version and options used.
4031:
4032: The words that Gforth offers encapsulate some system dependences (e.g., the
4033: header structure), so a system-independent assembler may be used in
4034: Gforth. If you do not have an assembler, you can compile machine code
4035: directly with @code{,} and @code{c,}.
4036:
4037: doc-assembler
4038: doc-code
4039: doc-end-code
4040: doc-;code
4041: doc-flush-icache
4042:
4043: If @code{flush-icache} does not work correctly, @code{code} words
4044: etc. will not work (reliably), either.
4045:
4046: These words are rarely used. Therefore they reside in @code{code.fs},
4047: which is usually not loaded (except @code{flush-icache}, which is always
4048: present). You can load them with @code{require code.fs}.
4049:
4050: @cindex registers of the inner interpreter
4051: In the assembly code you will want to refer to the inner interpreter's
4052: registers (e.g., the data stack pointer) and you may want to use other
4053: registers for temporary storage. Unfortunately, the register allocation
4054: is installation-dependent.
4055:
4056: The easiest solution is to use explicit register declarations
4057: (@pxref{Explicit Reg Vars, , Variables in Specified Registers, gcc.info,
4058: GNU C Manual}) for all of the inner interpreter's registers: You have to
4059: compile Gforth with @code{-DFORCE_REG} (configure option
4060: @code{--enable-force-reg}) and the appropriate declarations must be
4061: present in the @code{machine.h} file (see @code{mips.h} for an example;
4062: you can find a full list of all declarable register symbols with
4063: @code{grep register engine.c}). If you give explicit registers to all
4064: variables that are declared at the beginning of @code{engine()}, you
4065: should be able to use the other caller-saved registers for temporary
4066: storage. Alternatively, you can use the @code{gcc} option
4067: @code{-ffixed-REG} (@pxref{Code Gen Options, , Options for Code
4068: Generation Conventions, gcc.info, GNU C Manual}) to reserve a register
4069: (however, this restriction on register allocation may slow Gforth
4070: significantly).
4071:
4072: If this solution is not viable (e.g., because @code{gcc} does not allow
4073: you to explicitly declare all the registers you need), you have to find
4074: out by looking at the code where the inner interpreter's registers
4075: reside and which registers can be used for temporary storage. You can
4076: get an assembly listing of the engine's code with @code{make engine.s}.
4077:
4078: In any case, it is good practice to abstract your assembly code from the
4079: actual register allocation. E.g., if the data stack pointer resides in
4080: register @code{$17}, create an alias for this register called @code{sp},
4081: and use that in your assembly code.
4082:
4083: @cindex code words, portable
4084: Another option for implementing normal and defining words efficiently
4085: is: adding the wanted functionality to the source of Gforth. For normal
4086: words you just have to edit @file{primitives} (@pxref{Automatic
4087: Generation}), defining words (equivalent to @code{;CODE} words, for fast
4088: defined words) may require changes in @file{engine.c}, @file{kernal.fs},
4089: @file{prims2x.fs}, and possibly @file{cross.fs}.
4090:
4091:
1.2 jwilke 4092: @node Threading Words, Including Files, Assembler and Code Words, Words
1.1 anton 4093: @section Threading Words
4094: @cindex threading words
4095:
4096: @cindex code address
4097: These words provide access to code addresses and other threading stuff
4098: in Gforth (and, possibly, other interpretive Forths). It more or less
4099: abstracts away the differences between direct and indirect threading
4100: (and, for direct threading, the machine dependences). However, at
4101: present this wordset is still incomplete. It is also pretty low-level;
4102: some day it will hopefully be made unnecessary by an internals wordset
4103: that abstracts implementation details away completely.
4104:
4105: doc->code-address
4106: doc->does-code
4107: doc-code-address!
4108: doc-does-code!
4109: doc-does-handler!
4110: doc-/does-handler
4111:
4112: The code addresses produced by various defining words are produced by
4113: the following words:
4114:
4115: doc-docol:
4116: doc-docon:
4117: doc-dovar:
4118: doc-douser:
4119: doc-dodefer:
4120: doc-dofield:
4121:
4122: You can recognize words defined by a @code{CREATE}...@code{DOES>} word
4123: with @code{>DOES-CODE}. If the word was defined in that way, the value
4124: returned is different from 0 and identifies the @code{DOES>} used by the
4125: defining word.
1.2 jwilke 4126:
4127: @node Including Files, , Threading Words, Words
1.5 ! anton 4128: @section Including Files
1.2 jwilke 4129: @cindex including files
4130:
4131: @node Include and Require, Path handling, Including Files, Words
4132: @subsection Include and Requrie
4133:
4134: There a two words to include the source files more intelligently.
4135:
4136: doc-include
4137: doc-require
4138:
4139: @node Path handling, ,Require, Words
4140: @subsection Path handling
4141: @cindex path handling
4142:
4143: In larger program projects it is often neccassary to build up a structured
1.5 ! anton 4144: directory tree. Standard Forth programs are somewhere more central because
1.2 jwilke 4145: they must be accessed from some more other programs. To achieve this it is
1.5 ! anton 4146: possible to manipulate the search path in which Gforth tries to find the
1.2 jwilke 4147: source file.
4148:
4149: doc-fpath+
4150: doc-fpath=
4151: doc-.fpath
4152:
4153: Using fpath and require would look like:
4154:
4155: @example
4156:
4157: fpath= /usr/lib/forth/|./
4158:
4159: require timer.fs
4160:
4161: ...
4162:
4163: @end example
4164:
4165: @cindex ~+
4166: There is another nice feature which is similar to C's @code{include <...>}
4167: and @code{include "..."}. For example: You have a program seperated into
1.5 ! anton 4168: several files in a subdirectory and you want to include some other files
! 4169: in this subdirectory from within the program. You have to tell Gforth that
! 4170: you are now looking relative to the directory the current file comes from.
! 4171: You can tell this Gforth by using the prefix @code{~+/} in front of the
1.2 jwilke 4172: filename. It is also possible to add it to the search path.
4173:
1.5 ! anton 4174: If you have the need to look for a file in the Forth search path, you could
! 4175: use this Gforth feature in your application.
1.2 jwilke 4176:
4177: doc-open-fpath-file
4178:
4179: It is even possible to use your own search paths. Create a search path like
4180: this:
4181:
4182: @example
4183:
4184: Make a buffer for the path:
4185: create mypath 100 chars , \ maximum length (is checked)
4186: 0 , \ real len
4187: 100 chars allot \ space for path
4188:
4189: @end example
4190:
4191: You have the same functions for the forth search path in an generic version
4192: for different pathes.
4193:
4194: doc-path+
4195: doc-path=
4196: doc-.path
4197: doc-open-path-file
1.1 anton 4198:
1.5 ! anton 4199: @c ******************************************************************
1.1 anton 4200: @node Tools, ANS conformance, Words, Top
4201: @chapter Tools
4202:
4203: @menu
4204: * ANS Report:: Report the words used, sorted by wordset.
4205: @end menu
4206:
4207: See also @ref{Emacs and Gforth}.
4208:
4209: @node ANS Report, , Tools, Tools
4210: @section @file{ans-report.fs}: Report the words used, sorted by wordset
4211: @cindex @file{ans-report.fs}
4212: @cindex report the words used in your program
4213: @cindex words used in your program
4214:
4215: If you want to label a Forth program as ANS Forth Program, you must
4216: document which wordsets the program uses; for extension wordsets, it is
4217: helpful to list the words the program requires from these wordsets
4218: (because Forth systems are allowed to provide only some words of them).
4219:
4220: The @file{ans-report.fs} tool makes it easy for you to determine which
4221: words from which wordset and which non-ANS words your application
4222: uses. You simply have to include @file{ans-report.fs} before loading the
4223: program you want to check. After loading your program, you can get the
4224: report with @code{print-ans-report}. A typical use is to run this as
4225: batch job like this:
4226: @example
4227: gforth ans-report.fs myprog.fs -e "print-ans-report bye"
4228: @end example
4229:
4230: The output looks like this (for @file{compat/control.fs}):
4231: @example
4232: The program uses the following words
4233: from CORE :
4234: : POSTPONE THEN ; immediate ?dup IF 0=
4235: from BLOCK-EXT :
4236: \
4237: from FILE :
4238: (
4239: @end example
4240:
4241: @subsection Caveats
4242:
4243: Note that @file{ans-report.fs} just checks which words are used, not whether
4244: they are used in an ANS Forth conforming way!
4245:
4246: Some words are defined in several wordsets in the
4247: standard. @file{ans-report.fs} reports them for only one of the
4248: wordsets, and not necessarily the one you expect. It depends on usage
4249: which wordset is the right one to specify. E.g., if you only use the
4250: compilation semantics of @code{S"}, it is a Core word; if you also use
4251: its interpretation semantics, it is a File word.
4252:
4253: @c ******************************************************************
4254: @node ANS conformance, Model, Tools, Top
4255: @chapter ANS conformance
4256: @cindex ANS conformance of Gforth
4257:
4258: To the best of our knowledge, Gforth is an
4259:
4260: ANS Forth System
4261: @itemize @bullet
4262: @item providing the Core Extensions word set
4263: @item providing the Block word set
4264: @item providing the Block Extensions word set
4265: @item providing the Double-Number word set
4266: @item providing the Double-Number Extensions word set
4267: @item providing the Exception word set
4268: @item providing the Exception Extensions word set
4269: @item providing the Facility word set
4270: @item providing @code{MS} and @code{TIME&DATE} from the Facility Extensions word set
4271: @item providing the File Access word set
4272: @item providing the File Access Extensions word set
4273: @item providing the Floating-Point word set
4274: @item providing the Floating-Point Extensions word set
4275: @item providing the Locals word set
4276: @item providing the Locals Extensions word set
4277: @item providing the Memory-Allocation word set
4278: @item providing the Memory-Allocation Extensions word set (that one's easy)
4279: @item providing the Programming-Tools word set
4280: @item providing @code{;CODE}, @code{AHEAD}, @code{ASSEMBLER}, @code{BYE}, @code{CODE}, @code{CS-PICK}, @code{CS-ROLL}, @code{STATE}, @code{[ELSE]}, @code{[IF]}, @code{[THEN]} from the Programming-Tools Extensions word set
4281: @item providing the Search-Order word set
4282: @item providing the Search-Order Extensions word set
4283: @item providing the String word set
4284: @item providing the String Extensions word set (another easy one)
4285: @end itemize
4286:
4287: @cindex system documentation
4288: In addition, ANS Forth systems are required to document certain
4289: implementation choices. This chapter tries to meet these
4290: requirements. In many cases it gives a way to ask the system for the
4291: information instead of providing the information directly, in
4292: particular, if the information depends on the processor, the operating
4293: system or the installation options chosen, or if they are likely to
4294: change during the maintenance of Gforth.
4295:
4296: @comment The framework for the rest has been taken from pfe.
4297:
4298: @menu
4299: * The Core Words::
4300: * The optional Block word set::
4301: * The optional Double Number word set::
4302: * The optional Exception word set::
4303: * The optional Facility word set::
4304: * The optional File-Access word set::
4305: * The optional Floating-Point word set::
4306: * The optional Locals word set::
4307: * The optional Memory-Allocation word set::
4308: * The optional Programming-Tools word set::
4309: * The optional Search-Order word set::
4310: @end menu
4311:
4312:
4313: @c =====================================================================
4314: @node The Core Words, The optional Block word set, ANS conformance, ANS conformance
4315: @comment node-name, next, previous, up
4316: @section The Core Words
4317: @c =====================================================================
4318: @cindex core words, system documentation
4319: @cindex system documentation, core words
4320:
4321: @menu
4322: * core-idef:: Implementation Defined Options
4323: * core-ambcond:: Ambiguous Conditions
4324: * core-other:: Other System Documentation
4325: @end menu
4326:
4327: @c ---------------------------------------------------------------------
4328: @node core-idef, core-ambcond, The Core Words, The Core Words
4329: @subsection Implementation Defined Options
4330: @c ---------------------------------------------------------------------
4331: @cindex core words, implementation-defined options
4332: @cindex implementation-defined options, core words
4333:
4334:
4335: @table @i
4336: @item (Cell) aligned addresses:
4337: @cindex cell-aligned addresses
4338: @cindex aligned addresses
4339: processor-dependent. Gforth's alignment words perform natural alignment
4340: (e.g., an address aligned for a datum of size 8 is divisible by
4341: 8). Unaligned accesses usually result in a @code{-23 THROW}.
4342:
4343: @item @code{EMIT} and non-graphic characters:
4344: @cindex @code{EMIT} and non-graphic characters
4345: @cindex non-graphic characters and @code{EMIT}
4346: The character is output using the C library function (actually, macro)
4347: @code{putc}.
4348:
4349: @item character editing of @code{ACCEPT} and @code{EXPECT}:
4350: @cindex character editing of @code{ACCEPT} and @code{EXPECT}
4351: @cindex editing in @code{ACCEPT} and @code{EXPECT}
4352: @cindex @code{ACCEPT}, editing
4353: @cindex @code{EXPECT}, editing
4354: This is modeled on the GNU readline library (@pxref{Readline
4355: Interaction, , Command Line Editing, readline, The GNU Readline
4356: Library}) with Emacs-like key bindings. @kbd{Tab} deviates a little by
4357: producing a full word completion every time you type it (instead of
4358: producing the common prefix of all completions).
4359:
4360: @item character set:
4361: @cindex character set
4362: The character set of your computer and display device. Gforth is
4363: 8-bit-clean (but some other component in your system may make trouble).
4364:
4365: @item Character-aligned address requirements:
4366: @cindex character-aligned address requirements
4367: installation-dependent. Currently a character is represented by a C
4368: @code{unsigned char}; in the future we might switch to @code{wchar_t}
4369: (Comments on that requested).
4370:
4371: @item character-set extensions and matching of names:
4372: @cindex character-set extensions and matching of names
4373: @cindex case sensitivity for name lookup
4374: @cindex name lookup, case sensitivity
4375: @cindex locale and case sensitivity
4376: Any character except the ASCII NUL charcter can be used in a
4377: name. Matching is case-insensitive (except in @code{TABLE}s). The
4378: matching is performed using the C function @code{strncasecmp}, whose
4379: function is probably influenced by the locale. E.g., the @code{C} locale
4380: does not know about accents and umlauts, so they are matched
4381: case-sensitively in that locale. For portability reasons it is best to
4382: write programs such that they work in the @code{C} locale. Then one can
4383: use libraries written by a Polish programmer (who might use words
4384: containing ISO Latin-2 encoded characters) and by a French programmer
4385: (ISO Latin-1) in the same program (of course, @code{WORDS} will produce
4386: funny results for some of the words (which ones, depends on the font you
4387: are using)). Also, the locale you prefer may not be available in other
4388: operating systems. Hopefully, Unicode will solve these problems one day.
4389:
4390: @item conditions under which control characters match a space delimiter:
4391: @cindex space delimiters
4392: @cindex control characters as delimiters
4393: If @code{WORD} is called with the space character as a delimiter, all
4394: white-space characters (as identified by the C macro @code{isspace()})
4395: are delimiters. @code{PARSE}, on the other hand, treats space like other
4396: delimiters. @code{PARSE-WORD} treats space like @code{WORD}, but behaves
4397: like @code{PARSE} otherwise. @code{(NAME)}, which is used by the outer
4398: interpreter (aka text interpreter) by default, treats all white-space
4399: characters as delimiters.
4400:
4401: @item format of the control flow stack:
4402: @cindex control flow stack, format
4403: The data stack is used as control flow stack. The size of a control flow
4404: stack item in cells is given by the constant @code{cs-item-size}. At the
4405: time of this writing, an item consists of a (pointer to a) locals list
4406: (third), an address in the code (second), and a tag for identifying the
4407: item (TOS). The following tags are used: @code{defstart},
4408: @code{live-orig}, @code{dead-orig}, @code{dest}, @code{do-dest},
4409: @code{scopestart}.
4410:
4411: @item conversion of digits > 35
4412: @cindex digits > 35
4413: The characters @code{[\]^_'} are the digits with the decimal value
4414: 36@minus{}41. There is no way to input many of the larger digits.
4415:
4416: @item display after input terminates in @code{ACCEPT} and @code{EXPECT}:
4417: @cindex @code{EXPECT}, display after end of input
4418: @cindex @code{ACCEPT}, display after end of input
4419: The cursor is moved to the end of the entered string. If the input is
4420: terminated using the @kbd{Return} key, a space is typed.
4421:
4422: @item exception abort sequence of @code{ABORT"}:
4423: @cindex exception abort sequence of @code{ABORT"}
4424: @cindex @code{ABORT"}, exception abort sequence
4425: The error string is stored into the variable @code{"error} and a
4426: @code{-2 throw} is performed.
4427:
4428: @item input line terminator:
4429: @cindex input line terminator
4430: @cindex line terminator on input
4431: @cindex newline charcter on input
4432: For interactive input, @kbd{C-m} (CR) and @kbd{C-j} (LF) terminate
4433: lines. One of these characters is typically produced when you type the
4434: @kbd{Enter} or @kbd{Return} key.
4435:
4436: @item maximum size of a counted string:
4437: @cindex maximum size of a counted string
4438: @cindex counted string, maximum size
4439: @code{s" /counted-string" environment? drop .}. Currently 255 characters
4440: on all ports, but this may change.
4441:
4442: @item maximum size of a parsed string:
4443: @cindex maximum size of a parsed string
4444: @cindex parsed string, maximum size
4445: Given by the constant @code{/line}. Currently 255 characters.
4446:
4447: @item maximum size of a definition name, in characters:
4448: @cindex maximum size of a definition name, in characters
4449: @cindex name, maximum length
4450: 31
4451:
4452: @item maximum string length for @code{ENVIRONMENT?}, in characters:
4453: @cindex maximum string length for @code{ENVIRONMENT?}, in characters
4454: @cindex @code{ENVIRONMENT?} string length, maximum
4455: 31
4456:
4457: @item method of selecting the user input device:
4458: @cindex user input device, method of selecting
4459: The user input device is the standard input. There is currently no way to
4460: change it from within Gforth. However, the input can typically be
4461: redirected in the command line that starts Gforth.
4462:
4463: @item method of selecting the user output device:
4464: @cindex user output device, method of selecting
4465: @code{EMIT} and @code{TYPE} output to the file-id stored in the value
4466: @code{outfile-id} (@code{stdout} by default). Gforth uses buffered
4467: output, so output on a terminal does not become visible before the next
4468: newline or buffer overflow. Output on non-terminals is invisible until
4469: the buffer overflows.
4470:
4471: @item methods of dictionary compilation:
4472: What are we expected to document here?
4473:
4474: @item number of bits in one address unit:
4475: @cindex number of bits in one address unit
4476: @cindex address unit, size in bits
4477: @code{s" address-units-bits" environment? drop .}. 8 in all current
4478: ports.
4479:
4480: @item number representation and arithmetic:
4481: @cindex number representation and arithmetic
4482: Processor-dependent. Binary two's complement on all current ports.
4483:
4484: @item ranges for integer types:
4485: @cindex ranges for integer types
4486: @cindex integer types, ranges
4487: Installation-dependent. Make environmental queries for @code{MAX-N},
4488: @code{MAX-U}, @code{MAX-D} and @code{MAX-UD}. The lower bounds for
4489: unsigned (and positive) types is 0. The lower bound for signed types on
4490: two's complement and one's complement machines machines can be computed
4491: by adding 1 to the upper bound.
4492:
4493: @item read-only data space regions:
4494: @cindex read-only data space regions
4495: @cindex data-space, read-only regions
4496: The whole Forth data space is writable.
4497:
4498: @item size of buffer at @code{WORD}:
4499: @cindex size of buffer at @code{WORD}
4500: @cindex @code{WORD} buffer size
4501: @code{PAD HERE - .}. 104 characters on 32-bit machines. The buffer is
4502: shared with the pictured numeric output string. If overwriting
4503: @code{PAD} is acceptable, it is as large as the remaining dictionary
4504: space, although only as much can be sensibly used as fits in a counted
4505: string.
4506:
4507: @item size of one cell in address units:
4508: @cindex cell size
4509: @code{1 cells .}.
4510:
4511: @item size of one character in address units:
4512: @cindex char size
4513: @code{1 chars .}. 1 on all current ports.
4514:
4515: @item size of the keyboard terminal buffer:
4516: @cindex size of the keyboard terminal buffer
4517: @cindex terminal buffer, size
4518: Varies. You can determine the size at a specific time using @code{lp@@
4519: tib - .}. It is shared with the locals stack and TIBs of files that
4520: include the current file. You can change the amount of space for TIBs
4521: and locals stack at Gforth startup with the command line option
4522: @code{-l}.
4523:
4524: @item size of the pictured numeric output buffer:
4525: @cindex size of the pictured numeric output buffer
4526: @cindex pictured numeric output buffer, size
4527: @code{PAD HERE - .}. 104 characters on 32-bit machines. The buffer is
4528: shared with @code{WORD}.
4529:
4530: @item size of the scratch area returned by @code{PAD}:
4531: @cindex size of the scratch area returned by @code{PAD}
4532: @cindex @code{PAD} size
4533: The remainder of dictionary space. @code{unused pad here - - .}.
4534:
4535: @item system case-sensitivity characteristics:
4536: @cindex case-sensitivity characteristics
4537: Dictionary searches are case insensitive (except in
4538: @code{TABLE}s). However, as explained above under @i{character-set
4539: extensions}, the matching for non-ASCII characters is determined by the
4540: locale you are using. In the default @code{C} locale all non-ASCII
4541: characters are matched case-sensitively.
4542:
4543: @item system prompt:
4544: @cindex system prompt
4545: @cindex prompt
4546: @code{ ok} in interpret state, @code{ compiled} in compile state.
4547:
4548: @item division rounding:
4549: @cindex division rounding
4550: installation dependent. @code{s" floored" environment? drop .}. We leave
4551: the choice to @code{gcc} (what to use for @code{/}) and to you (whether
4552: to use @code{fm/mod}, @code{sm/rem} or simply @code{/}).
4553:
4554: @item values of @code{STATE} when true:
4555: @cindex @code{STATE} values
4556: -1.
4557:
4558: @item values returned after arithmetic overflow:
4559: On two's complement machines, arithmetic is performed modulo
4560: 2**bits-per-cell for single arithmetic and 4**bits-per-cell for double
4561: arithmetic (with appropriate mapping for signed types). Division by zero
4562: typically results in a @code{-55 throw} (Floating-point unidentified
4563: fault), although a @code{-10 throw} (divide by zero) would be more
4564: appropriate.
4565:
4566: @item whether the current definition can be found after @t{DOES>}:
4567: @cindex @t{DOES>}, visibility of current definition
4568: No.
4569:
4570: @end table
4571:
4572: @c ---------------------------------------------------------------------
4573: @node core-ambcond, core-other, core-idef, The Core Words
4574: @subsection Ambiguous conditions
4575: @c ---------------------------------------------------------------------
4576: @cindex core words, ambiguous conditions
4577: @cindex ambiguous conditions, core words
4578:
4579: @table @i
4580:
4581: @item a name is neither a word nor a number:
4582: @cindex name not found
4583: @cindex Undefined word
4584: @code{-13 throw} (Undefined word). Actually, @code{-13 bounce}, which
4585: preserves the data and FP stack, so you don't lose more work than
4586: necessary.
4587:
4588: @item a definition name exceeds the maximum length allowed:
4589: @cindex Word name too long
4590: @code{-19 throw} (Word name too long)
4591:
4592: @item addressing a region not inside the various data spaces of the forth system:
4593: @cindex Invalid memory address
4594: The stacks, code space and name space are accessible. Machine code space is
4595: typically readable. Accessing other addresses gives results dependent on
4596: the operating system. On decent systems: @code{-9 throw} (Invalid memory
4597: address).
4598:
4599: @item argument type incompatible with parameter:
4600: @cindex Argument type mismatch
4601: This is usually not caught. Some words perform checks, e.g., the control
4602: flow words, and issue a @code{ABORT"} or @code{-12 THROW} (Argument type
4603: mismatch).
4604:
4605: @item attempting to obtain the execution token of a word with undefined execution semantics:
4606: @cindex Interpreting a compile-only word, for @code{'} etc.
4607: @cindex execution token of words with undefined execution semantics
4608: @code{-14 throw} (Interpreting a compile-only word). In some cases, you
4609: get an execution token for @code{compile-only-error} (which performs a
4610: @code{-14 throw} when executed).
4611:
4612: @item dividing by zero:
4613: @cindex dividing by zero
4614: @cindex floating point unidentified fault, integer division
4615: @cindex divide by zero
4616: typically results in a @code{-55 throw} (floating point unidentified
4617: fault), although a @code{-10 throw} (divide by zero) would be more
4618: appropriate.
4619:
4620: @item insufficient data stack or return stack space:
4621: @cindex insufficient data stack or return stack space
4622: @cindex stack overflow
4623: @cindex Address alignment exception, stack overflow
4624: @cindex Invalid memory address, stack overflow
4625: Depending on the operating system, the installation, and the invocation
4626: of Gforth, this is either checked by the memory management hardware, or
4627: it is not checked. If it is checked, you typically get a @code{-9 throw}
4628: (Invalid memory address) as soon as the overflow happens. If it is not
4629: check, overflows typically result in mysterious illegal memory accesses,
4630: producing @code{-9 throw} (Invalid memory address) or @code{-23 throw}
4631: (Address alignment exception); they might also destroy the internal data
4632: structure of @code{ALLOCATE} and friends, resulting in various errors in
4633: these words.
4634:
4635: @item insufficient space for loop control parameters:
4636: @cindex insufficient space for loop control parameters
4637: like other return stack overflows.
4638:
4639: @item insufficient space in the dictionary:
4640: @cindex insufficient space in the dictionary
4641: @cindex dictionary overflow
4642: Depending on the operating system, the installation, and the invocation
4643: of Gforth, this is either checked by the memory management hardware, or
4644: it is not checked. Similar results as stack overflows. However,
4645: typically the error appears at a different place when one inserts or
4646: removes code. Also, the @code{THROW} does not relieve the situation (it
4647: does for stack overflows).
4648:
4649: @item interpreting a word with undefined interpretation semantics:
4650: @cindex interpreting a word with undefined interpretation semantics
4651: @cindex Interpreting a compile-only word
4652: For some words, we have defined interpretation semantics. For the
4653: others: @code{-14 throw} (Interpreting a compile-only word).
4654:
4655: @item modifying the contents of the input buffer or a string literal:
4656: @cindex modifying the contents of the input buffer or a string literal
4657: These are located in writable memory and can be modified.
4658:
4659: @item overflow of the pictured numeric output string:
4660: @cindex overflow of the pictured numeric output string
4661: @cindex pictured numeric output string, overflow
4662: Not checked. Runs into the dictionary and destroys it (at least,
4663: partially).
4664:
4665: @item parsed string overflow:
4666: @cindex parsed string overflow
4667: @code{PARSE} cannot overflow. @code{WORD} does not check for overflow.
4668:
4669: @item producing a result out of range:
4670: @cindex result out of range
4671: On two's complement machines, arithmetic is performed modulo
4672: 2**bits-per-cell for single arithmetic and 4**bits-per-cell for double
4673: arithmetic (with appropriate mapping for signed types). Division by zero
4674: typically results in a @code{-55 throw} (floatingpoint unidentified
4675: fault), although a @code{-10 throw} (divide by zero) would be more
4676: appropriate. @code{convert} and @code{>number} currently overflow
4677: silently.
4678:
4679: @item reading from an empty data or return stack:
4680: @cindex stack empty
4681: @cindex stack underflow
4682: The data stack is checked by the outer (aka text) interpreter after
4683: every word executed. If it has underflowed, a @code{-4 throw} (Stack
4684: underflow) is performed. Apart from that, stacks may be checked or not,
4685: depending on operating system, installation, and invocation. The
4686: consequences of stack underflows are similar to the consequences of
4687: stack overflows. Note that even if the system uses checking (through the
4688: MMU), your program may have to underflow by a significant number of
4689: stack items to trigger the reaction (the reason for this is that the
4690: MMU, and therefore the checking, works with a page-size granularity).
4691:
4692: @item unexpected end of the input buffer, resulting in an attempt to use a zero-length string as a name:
4693: @cindex unexpected end of the input buffer
4694: @cindex zero-length string as a name
4695: @cindex Attempt to use zero-length string as a name
4696: @code{Create} and its descendants perform a @code{-16 throw} (Attempt to
4697: use zero-length string as a name). Words like @code{'} probably will not
4698: find what they search. Note that it is possible to create zero-length
4699: names with @code{nextname} (should it not?).
4700:
4701: @item @code{>IN} greater than input buffer:
4702: @cindex @code{>IN} greater than input buffer
4703: The next invocation of a parsing word returns a string with length 0.
4704:
4705: @item @code{RECURSE} appears after @code{DOES>}:
4706: @cindex @code{RECURSE} appears after @code{DOES>}
4707: Compiles a recursive call to the defining word, not to the defined word.
4708:
4709: @item argument input source different than current input source for @code{RESTORE-INPUT}:
4710: @cindex argument input source different than current input source for @code{RESTORE-INPUT}
4711: @cindex Argument type mismatch, @code{RESTORE-INPUT}
4712: @cindex @code{RESTORE-INPUT}, Argument type mismatch
4713: @code{-12 THROW}. Note that, once an input file is closed (e.g., because
4714: the end of the file was reached), its source-id may be
4715: reused. Therefore, restoring an input source specification referencing a
4716: closed file may lead to unpredictable results instead of a @code{-12
4717: THROW}.
4718:
4719: In the future, Gforth may be able to restore input source specifications
4720: from other than the current input source.
4721:
4722: @item data space containing definitions gets de-allocated:
4723: @cindex data space containing definitions gets de-allocated
4724: Deallocation with @code{allot} is not checked. This typically results in
4725: memory access faults or execution of illegal instructions.
4726:
4727: @item data space read/write with incorrect alignment:
4728: @cindex data space read/write with incorrect alignment
4729: @cindex alignment faults
4730: @cindex Address alignment exception
4731: Processor-dependent. Typically results in a @code{-23 throw} (Address
4732: alignment exception). Under Linux on a 486 or later processor with
4733: alignment turned on, incorrect alignment results in a @code{-9 throw}
4734: (Invalid memory address). There are reportedly some processors with
4735: alignment restrictions that do not report them.
4736:
4737: @item data space pointer not properly aligned, @code{,}, @code{C,}:
4738: @cindex data space pointer not properly aligned, @code{,}, @code{C,}
4739: Like other alignment errors.
4740:
4741: @item less than u+2 stack items (@code{PICK} and @code{ROLL}):
4742: Like other stack underflows.
4743:
4744: @item loop control parameters not available:
4745: @cindex loop control parameters not available
4746: Not checked. The counted loop words simply assume that the top of return
4747: stack items are loop control parameters and behave accordingly.
4748:
4749: @item most recent definition does not have a name (@code{IMMEDIATE}):
4750: @cindex most recent definition does not have a name (@code{IMMEDIATE})
4751: @cindex last word was headerless
4752: @code{abort" last word was headerless"}.
4753:
4754: @item name not defined by @code{VALUE} used by @code{TO}:
4755: @cindex name not defined by @code{VALUE} used by @code{TO}
4756: @cindex @code{TO} on non-@code{VALUE}s
4757: @cindex Invalid name argument, @code{TO}
4758: @code{-32 throw} (Invalid name argument) (unless name is a local or was
4759: defined by @code{CONSTANT}; in the latter case it just changes the constant).
4760:
4761: @item name not found (@code{'}, @code{POSTPONE}, @code{[']}, @code{[COMPILE]}):
4762: @cindex name not found (@code{'}, @code{POSTPONE}, @code{[']}, @code{[COMPILE]})
4763: @cindex Undefined word, @code{'}, @code{POSTPONE}, @code{[']}, @code{[COMPILE]}
4764: @code{-13 throw} (Undefined word)
4765:
4766: @item parameters are not of the same type (@code{DO}, @code{?DO}, @code{WITHIN}):
4767: @cindex parameters are not of the same type (@code{DO}, @code{?DO}, @code{WITHIN})
4768: Gforth behaves as if they were of the same type. I.e., you can predict
4769: the behaviour by interpreting all parameters as, e.g., signed.
4770:
4771: @item @code{POSTPONE} or @code{[COMPILE]} applied to @code{TO}:
4772: @cindex @code{POSTPONE} or @code{[COMPILE]} applied to @code{TO}
4773: Assume @code{: X POSTPONE TO ; IMMEDIATE}. @code{X} performs the
4774: compilation semantics of @code{TO}.
4775:
4776: @item String longer than a counted string returned by @code{WORD}:
4777: @cindex String longer than a counted string returned by @code{WORD}
4778: @cindex @code{WORD}, string overflow
4779: Not checked. The string will be ok, but the count will, of course,
4780: contain only the least significant bits of the length.
4781:
4782: @item u greater than or equal to the number of bits in a cell (@code{LSHIFT}, @code{RSHIFT}):
4783: @cindex @code{LSHIFT}, large shift counts
4784: @cindex @code{RSHIFT}, large shift counts
4785: Processor-dependent. Typical behaviours are returning 0 and using only
4786: the low bits of the shift count.
4787:
4788: @item word not defined via @code{CREATE}:
4789: @cindex @code{>BODY} of non-@code{CREATE}d words
4790: @code{>BODY} produces the PFA of the word no matter how it was defined.
4791:
4792: @cindex @code{DOES>} of non-@code{CREATE}d words
4793: @code{DOES>} changes the execution semantics of the last defined word no
4794: matter how it was defined. E.g., @code{CONSTANT DOES>} is equivalent to
4795: @code{CREATE , DOES>}.
4796:
4797: @item words improperly used outside @code{<#} and @code{#>}:
4798: Not checked. As usual, you can expect memory faults.
4799:
4800: @end table
4801:
4802:
4803: @c ---------------------------------------------------------------------
4804: @node core-other, , core-ambcond, The Core Words
4805: @subsection Other system documentation
4806: @c ---------------------------------------------------------------------
4807: @cindex other system documentation, core words
4808: @cindex core words, other system documentation
4809:
4810: @table @i
4811: @item nonstandard words using @code{PAD}:
4812: @cindex @code{PAD} use by nonstandard words
4813: None.
4814:
4815: @item operator's terminal facilities available:
4816: @cindex operator's terminal facilities available
4817: After processing the command line, Gforth goes into interactive mode,
4818: and you can give commands to Gforth interactively. The actual facilities
4819: available depend on how you invoke Gforth.
4820:
4821: @item program data space available:
4822: @cindex program data space available
4823: @cindex data space available
4824: @code{UNUSED .} gives the remaining dictionary space. The total
4825: dictionary space can be specified with the @code{-m} switch
4826: (@pxref{Invoking Gforth}) when Gforth starts up.
4827:
4828: @item return stack space available:
4829: @cindex return stack space available
4830: You can compute the total return stack space in cells with
4831: @code{s" RETURN-STACK-CELLS" environment? drop .}. You can specify it at
4832: startup time with the @code{-r} switch (@pxref{Invoking Gforth}).
4833:
4834: @item stack space available:
4835: @cindex stack space available
4836: You can compute the total data stack space in cells with
4837: @code{s" STACK-CELLS" environment? drop .}. You can specify it at
4838: startup time with the @code{-d} switch (@pxref{Invoking Gforth}).
4839:
4840: @item system dictionary space required, in address units:
4841: @cindex system dictionary space required, in address units
4842: Type @code{here forthstart - .} after startup. At the time of this
4843: writing, this gives 80080 (bytes) on a 32-bit system.
4844: @end table
4845:
4846:
4847: @c =====================================================================
4848: @node The optional Block word set, The optional Double Number word set, The Core Words, ANS conformance
4849: @section The optional Block word set
4850: @c =====================================================================
4851: @cindex system documentation, block words
4852: @cindex block words, system documentation
4853:
4854: @menu
4855: * block-idef:: Implementation Defined Options
4856: * block-ambcond:: Ambiguous Conditions
4857: * block-other:: Other System Documentation
4858: @end menu
4859:
4860:
4861: @c ---------------------------------------------------------------------
4862: @node block-idef, block-ambcond, The optional Block word set, The optional Block word set
4863: @subsection Implementation Defined Options
4864: @c ---------------------------------------------------------------------
4865: @cindex implementation-defined options, block words
4866: @cindex block words, implementation-defined options
4867:
4868: @table @i
4869: @item the format for display by @code{LIST}:
4870: @cindex @code{LIST} display format
4871: First the screen number is displayed, then 16 lines of 64 characters,
4872: each line preceded by the line number.
4873:
4874: @item the length of a line affected by @code{\}:
4875: @cindex length of a line affected by @code{\}
4876: @cindex @code{\}, line length in blocks
4877: 64 characters.
4878: @end table
4879:
4880:
4881: @c ---------------------------------------------------------------------
4882: @node block-ambcond, block-other, block-idef, The optional Block word set
4883: @subsection Ambiguous conditions
4884: @c ---------------------------------------------------------------------
4885: @cindex block words, ambiguous conditions
4886: @cindex ambiguous conditions, block words
4887:
4888: @table @i
4889: @item correct block read was not possible:
4890: @cindex block read not possible
4891: Typically results in a @code{throw} of some OS-derived value (between
4892: -512 and -2048). If the blocks file was just not long enough, blanks are
4893: supplied for the missing portion.
4894:
4895: @item I/O exception in block transfer:
4896: @cindex I/O exception in block transfer
4897: @cindex block transfer, I/O exception
4898: Typically results in a @code{throw} of some OS-derived value (between
4899: -512 and -2048).
4900:
4901: @item invalid block number:
4902: @cindex invalid block number
4903: @cindex block number invalid
4904: @code{-35 throw} (Invalid block number)
4905:
4906: @item a program directly alters the contents of @code{BLK}:
4907: @cindex @code{BLK}, altering @code{BLK}
4908: The input stream is switched to that other block, at the same
4909: position. If the storing to @code{BLK} happens when interpreting
4910: non-block input, the system will get quite confused when the block ends.
4911:
4912: @item no current block buffer for @code{UPDATE}:
4913: @cindex @code{UPDATE}, no current block buffer
4914: @code{UPDATE} has no effect.
4915:
4916: @end table
4917:
4918: @c ---------------------------------------------------------------------
4919: @node block-other, , block-ambcond, The optional Block word set
4920: @subsection Other system documentation
4921: @c ---------------------------------------------------------------------
4922: @cindex other system documentation, block words
4923: @cindex block words, other system documentation
4924:
4925: @table @i
4926: @item any restrictions a multiprogramming system places on the use of buffer addresses:
4927: No restrictions (yet).
4928:
4929: @item the number of blocks available for source and data:
4930: depends on your disk space.
4931:
4932: @end table
4933:
4934:
4935: @c =====================================================================
4936: @node The optional Double Number word set, The optional Exception word set, The optional Block word set, ANS conformance
4937: @section The optional Double Number word set
4938: @c =====================================================================
4939: @cindex system documentation, double words
4940: @cindex double words, system documentation
4941:
4942: @menu
4943: * double-ambcond:: Ambiguous Conditions
4944: @end menu
4945:
4946:
4947: @c ---------------------------------------------------------------------
4948: @node double-ambcond, , The optional Double Number word set, The optional Double Number word set
4949: @subsection Ambiguous conditions
4950: @c ---------------------------------------------------------------------
4951: @cindex double words, ambiguous conditions
4952: @cindex ambiguous conditions, double words
4953:
4954: @table @i
4955: @item @var{d} outside of range of @var{n} in @code{D>S}:
4956: @cindex @code{D>S}, @var{d} out of range of @var{n}
4957: The least significant cell of @var{d} is produced.
4958:
4959: @end table
4960:
4961:
4962: @c =====================================================================
4963: @node The optional Exception word set, The optional Facility word set, The optional Double Number word set, ANS conformance
4964: @section The optional Exception word set
4965: @c =====================================================================
4966: @cindex system documentation, exception words
4967: @cindex exception words, system documentation
4968:
4969: @menu
4970: * exception-idef:: Implementation Defined Options
4971: @end menu
4972:
4973:
4974: @c ---------------------------------------------------------------------
4975: @node exception-idef, , The optional Exception word set, The optional Exception word set
4976: @subsection Implementation Defined Options
4977: @c ---------------------------------------------------------------------
4978: @cindex implementation-defined options, exception words
4979: @cindex exception words, implementation-defined options
4980:
4981: @table @i
4982: @item @code{THROW}-codes used in the system:
4983: @cindex @code{THROW}-codes used in the system
4984: The codes -256@minus{}-511 are used for reporting signals. The mapping
4985: from OS signal numbers to throw codes is -256@minus{}@var{signal}. The
4986: codes -512@minus{}-2047 are used for OS errors (for file and memory
4987: allocation operations). The mapping from OS error numbers to throw codes
4988: is -512@minus{}@code{errno}. One side effect of this mapping is that
4989: undefined OS errors produce a message with a strange number; e.g.,
4990: @code{-1000 THROW} results in @code{Unknown error 488} on my system.
4991: @end table
4992:
4993: @c =====================================================================
4994: @node The optional Facility word set, The optional File-Access word set, The optional Exception word set, ANS conformance
4995: @section The optional Facility word set
4996: @c =====================================================================
4997: @cindex system documentation, facility words
4998: @cindex facility words, system documentation
4999:
5000: @menu
5001: * facility-idef:: Implementation Defined Options
5002: * facility-ambcond:: Ambiguous Conditions
5003: @end menu
5004:
5005:
5006: @c ---------------------------------------------------------------------
5007: @node facility-idef, facility-ambcond, The optional Facility word set, The optional Facility word set
5008: @subsection Implementation Defined Options
5009: @c ---------------------------------------------------------------------
5010: @cindex implementation-defined options, facility words
5011: @cindex facility words, implementation-defined options
5012:
5013: @table @i
5014: @item encoding of keyboard events (@code{EKEY}):
5015: @cindex keyboard events, encoding in @code{EKEY}
5016: @cindex @code{EKEY}, encoding of keyboard events
5017: Not yet implemented.
5018:
5019: @item duration of a system clock tick:
5020: @cindex duration of a system clock tick
5021: @cindex clock tick duration
5022: System dependent. With respect to @code{MS}, the time is specified in
5023: microseconds. How well the OS and the hardware implement this, is
5024: another question.
5025:
5026: @item repeatability to be expected from the execution of @code{MS}:
5027: @cindex repeatability to be expected from the execution of @code{MS}
5028: @cindex @code{MS}, repeatability to be expected
5029: System dependent. On Unix, a lot depends on load. If the system is
5030: lightly loaded, and the delay is short enough that Gforth does not get
5031: swapped out, the performance should be acceptable. Under MS-DOS and
5032: other single-tasking systems, it should be good.
5033:
5034: @end table
5035:
5036:
5037: @c ---------------------------------------------------------------------
5038: @node facility-ambcond, , facility-idef, The optional Facility word set
5039: @subsection Ambiguous conditions
5040: @c ---------------------------------------------------------------------
5041: @cindex facility words, ambiguous conditions
5042: @cindex ambiguous conditions, facility words
5043:
5044: @table @i
5045: @item @code{AT-XY} can't be performed on user output device:
5046: @cindex @code{AT-XY} can't be performed on user output device
5047: Largely terminal dependent. No range checks are done on the arguments.
5048: No errors are reported. You may see some garbage appearing, you may see
5049: simply nothing happen.
5050:
5051: @end table
5052:
5053:
5054: @c =====================================================================
5055: @node The optional File-Access word set, The optional Floating-Point word set, The optional Facility word set, ANS conformance
5056: @section The optional File-Access word set
5057: @c =====================================================================
5058: @cindex system documentation, file words
5059: @cindex file words, system documentation
5060:
5061: @menu
5062: * file-idef:: Implementation Defined Options
5063: * file-ambcond:: Ambiguous Conditions
5064: @end menu
5065:
5066: @c ---------------------------------------------------------------------
5067: @node file-idef, file-ambcond, The optional File-Access word set, The optional File-Access word set
5068: @subsection Implementation Defined Options
5069: @c ---------------------------------------------------------------------
5070: @cindex implementation-defined options, file words
5071: @cindex file words, implementation-defined options
5072:
5073: @table @i
5074: @item file access methods used:
5075: @cindex file access methods used
5076: @code{R/O}, @code{R/W} and @code{BIN} work as you would
5077: expect. @code{W/O} translates into the C file opening mode @code{w} (or
5078: @code{wb}): The file is cleared, if it exists, and created, if it does
5079: not (with both @code{open-file} and @code{create-file}). Under Unix
5080: @code{create-file} creates a file with 666 permissions modified by your
5081: umask.
5082:
5083: @item file exceptions:
5084: @cindex file exceptions
5085: The file words do not raise exceptions (except, perhaps, memory access
5086: faults when you pass illegal addresses or file-ids).
5087:
5088: @item file line terminator:
5089: @cindex file line terminator
5090: System-dependent. Gforth uses C's newline character as line
5091: terminator. What the actual character code(s) of this are is
5092: system-dependent.
5093:
5094: @item file name format:
5095: @cindex file name format
5096: System dependent. Gforth just uses the file name format of your OS.
5097:
5098: @item information returned by @code{FILE-STATUS}:
5099: @cindex @code{FILE-STATUS}, returned information
5100: @code{FILE-STATUS} returns the most powerful file access mode allowed
5101: for the file: Either @code{R/O}, @code{W/O} or @code{R/W}. If the file
5102: cannot be accessed, @code{R/O BIN} is returned. @code{BIN} is applicable
5103: along with the returned mode.
5104:
5105: @item input file state after an exception when including source:
5106: @cindex exception when including source
5107: All files that are left via the exception are closed.
5108:
5109: @item @var{ior} values and meaning:
5110: @cindex @var{ior} values and meaning
5111: The @var{ior}s returned by the file and memory allocation words are
5112: intended as throw codes. They typically are in the range
5113: -512@minus{}-2047 of OS errors. The mapping from OS error numbers to
5114: @var{ior}s is -512@minus{}@var{errno}.
5115:
5116: @item maximum depth of file input nesting:
5117: @cindex maximum depth of file input nesting
5118: @cindex file input nesting, maximum depth
5119: limited by the amount of return stack, locals/TIB stack, and the number
5120: of open files available. This should not give you troubles.
5121:
5122: @item maximum size of input line:
5123: @cindex maximum size of input line
5124: @cindex input line size, maximum
5125: @code{/line}. Currently 255.
5126:
5127: @item methods of mapping block ranges to files:
5128: @cindex mapping block ranges to files
5129: @cindex files containing blocks
5130: @cindex blocks in files
5131: By default, blocks are accessed in the file @file{blocks.fb} in the
5132: current working directory. The file can be switched with @code{USE}.
5133:
5134: @item number of string buffers provided by @code{S"}:
5135: @cindex @code{S"}, number of string buffers
5136: 1
5137:
5138: @item size of string buffer used by @code{S"}:
5139: @cindex @code{S"}, size of string buffer
5140: @code{/line}. currently 255.
5141:
5142: @end table
5143:
5144: @c ---------------------------------------------------------------------
5145: @node file-ambcond, , file-idef, The optional File-Access word set
5146: @subsection Ambiguous conditions
5147: @c ---------------------------------------------------------------------
5148: @cindex file words, ambiguous conditions
5149: @cindex ambiguous conditions, file words
5150:
5151: @table @i
5152: @item attempting to position a file outside its boundaries:
5153: @cindex @code{REPOSITION-FILE}, outside the file's boundaries
5154: @code{REPOSITION-FILE} is performed as usual: Afterwards,
5155: @code{FILE-POSITION} returns the value given to @code{REPOSITION-FILE}.
5156:
5157: @item attempting to read from file positions not yet written:
5158: @cindex reading from file positions not yet written
5159: End-of-file, i.e., zero characters are read and no error is reported.
5160:
5161: @item @var{file-id} is invalid (@code{INCLUDE-FILE}):
5162: @cindex @code{INCLUDE-FILE}, @var{file-id} is invalid
5163: An appropriate exception may be thrown, but a memory fault or other
5164: problem is more probable.
5165:
5166: @item I/O exception reading or closing @var{file-id} (@code{INCLUDE-FILE}, @code{INCLUDED}):
5167: @cindex @code{INCLUDE-FILE}, I/O exception reading or closing @var{file-id}
5168: @cindex @code{INCLUDED}, I/O exception reading or closing @var{file-id}
5169: The @var{ior} produced by the operation, that discovered the problem, is
5170: thrown.
5171:
5172: @item named file cannot be opened (@code{INCLUDED}):
5173: @cindex @code{INCLUDED}, named file cannot be opened
5174: The @var{ior} produced by @code{open-file} is thrown.
5175:
5176: @item requesting an unmapped block number:
5177: @cindex unmapped block numbers
5178: There are no unmapped legal block numbers. On some operating systems,
5179: writing a block with a large number may overflow the file system and
5180: have an error message as consequence.
5181:
5182: @item using @code{source-id} when @code{blk} is non-zero:
5183: @cindex @code{SOURCE-ID}, behaviour when @code{BLK} is non-zero
5184: @code{source-id} performs its function. Typically it will give the id of
5185: the source which loaded the block. (Better ideas?)
5186:
5187: @end table
5188:
5189:
5190: @c =====================================================================
5191: @node The optional Floating-Point word set, The optional Locals word set, The optional File-Access word set, ANS conformance
5192: @section The optional Floating-Point word set
5193: @c =====================================================================
5194: @cindex system documentation, floating-point words
5195: @cindex floating-point words, system documentation
5196:
5197: @menu
5198: * floating-idef:: Implementation Defined Options
5199: * floating-ambcond:: Ambiguous Conditions
5200: @end menu
5201:
5202:
5203: @c ---------------------------------------------------------------------
5204: @node floating-idef, floating-ambcond, The optional Floating-Point word set, The optional Floating-Point word set
5205: @subsection Implementation Defined Options
5206: @c ---------------------------------------------------------------------
5207: @cindex implementation-defined options, floating-point words
5208: @cindex floating-point words, implementation-defined options
5209:
5210: @table @i
5211: @item format and range of floating point numbers:
5212: @cindex format and range of floating point numbers
5213: @cindex floating point numbers, format and range
5214: System-dependent; the @code{double} type of C.
5215:
5216: @item results of @code{REPRESENT} when @var{float} is out of range:
5217: @cindex @code{REPRESENT}, results when @var{float} is out of range
5218: System dependent; @code{REPRESENT} is implemented using the C library
5219: function @code{ecvt()} and inherits its behaviour in this respect.
5220:
5221: @item rounding or truncation of floating-point numbers:
5222: @cindex rounding of floating-point numbers
5223: @cindex truncation of floating-point numbers
5224: @cindex floating-point numbers, rounding or truncation
5225: System dependent; the rounding behaviour is inherited from the hosting C
5226: compiler. IEEE-FP-based (i.e., most) systems by default round to
5227: nearest, and break ties by rounding to even (i.e., such that the last
5228: bit of the mantissa is 0).
5229:
5230: @item size of floating-point stack:
5231: @cindex floating-point stack size
5232: @code{s" FLOATING-STACK" environment? drop .} gives the total size of
5233: the floating-point stack (in floats). You can specify this on startup
5234: with the command-line option @code{-f} (@pxref{Invoking Gforth}).
5235:
5236: @item width of floating-point stack:
5237: @cindex floating-point stack width
5238: @code{1 floats}.
5239:
5240: @end table
5241:
5242:
5243: @c ---------------------------------------------------------------------
5244: @node floating-ambcond, , floating-idef, The optional Floating-Point word set
5245: @subsection Ambiguous conditions
5246: @c ---------------------------------------------------------------------
5247: @cindex floating-point words, ambiguous conditions
5248: @cindex ambiguous conditions, floating-point words
5249:
5250: @table @i
5251: @item @code{df@@} or @code{df!} used with an address that is not double-float aligned:
5252: @cindex @code{df@@} or @code{df!} used with an address that is not double-float aligned
5253: System-dependent. Typically results in a @code{-23 THROW} like other
5254: alignment violations.
5255:
5256: @item @code{f@@} or @code{f!} used with an address that is not float aligned:
5257: @cindex @code{f@@} used with an address that is not float aligned
5258: @cindex @code{f!} used with an address that is not float aligned
5259: System-dependent. Typically results in a @code{-23 THROW} like other
5260: alignment violations.
5261:
5262: @item floating-point result out of range:
5263: @cindex floating-point result out of range
5264: System-dependent. Can result in a @code{-55 THROW} (Floating-point
5265: unidentified fault), or can produce a special value representing, e.g.,
5266: Infinity.
5267:
5268: @item @code{sf@@} or @code{sf!} used with an address that is not single-float aligned:
5269: @cindex @code{sf@@} or @code{sf!} used with an address that is not single-float aligned
5270: System-dependent. Typically results in an alignment fault like other
5271: alignment violations.
5272:
5273: @item @code{BASE} is not decimal (@code{REPRESENT}, @code{F.}, @code{FE.}, @code{FS.}):
5274: @cindex @code{BASE} is not decimal (@code{REPRESENT}, @code{F.}, @code{FE.}, @code{FS.})
5275: The floating-point number is converted into decimal nonetheless.
5276:
5277: @item Both arguments are equal to zero (@code{FATAN2}):
5278: @cindex @code{FATAN2}, both arguments are equal to zero
5279: System-dependent. @code{FATAN2} is implemented using the C library
5280: function @code{atan2()}.
5281:
5282: @item Using @code{FTAN} on an argument @var{r1} where cos(@var{r1}) is zero:
5283: @cindex @code{FTAN} on an argument @var{r1} where cos(@var{r1}) is zero
5284: System-dependent. Anyway, typically the cos of @var{r1} will not be zero
5285: because of small errors and the tan will be a very large (or very small)
5286: but finite number.
5287:
5288: @item @var{d} cannot be presented precisely as a float in @code{D>F}:
5289: @cindex @code{D>F}, @var{d} cannot be presented precisely as a float
5290: The result is rounded to the nearest float.
5291:
5292: @item dividing by zero:
5293: @cindex dividing by zero, floating-point
5294: @cindex floating-point dividing by zero
5295: @cindex floating-point unidentified fault, FP divide-by-zero
5296: @code{-55 throw} (Floating-point unidentified fault)
5297:
5298: @item exponent too big for conversion (@code{DF!}, @code{DF@@}, @code{SF!}, @code{SF@@}):
5299: @cindex exponent too big for conversion (@code{DF!}, @code{DF@@}, @code{SF!}, @code{SF@@})
5300: System dependent. On IEEE-FP based systems the number is converted into
5301: an infinity.
5302:
5303: @item @var{float}<1 (@code{FACOSH}):
5304: @cindex @code{FACOSH}, @var{float}<1
5305: @cindex floating-point unidentified fault, @code{FACOSH}
5306: @code{-55 throw} (Floating-point unidentified fault)
5307:
5308: @item @var{float}=<-1 (@code{FLNP1}):
5309: @cindex @code{FLNP1}, @var{float}=<-1
5310: @cindex floating-point unidentified fault, @code{FLNP1}
5311: @code{-55 throw} (Floating-point unidentified fault). On IEEE-FP systems
5312: negative infinity is typically produced for @var{float}=-1.
5313:
5314: @item @var{float}=<0 (@code{FLN}, @code{FLOG}):
5315: @cindex @code{FLN}, @var{float}=<0
5316: @cindex @code{FLOG}, @var{float}=<0
5317: @cindex floating-point unidentified fault, @code{FLN} or @code{FLOG}
5318: @code{-55 throw} (Floating-point unidentified fault). On IEEE-FP systems
5319: negative infinity is typically produced for @var{float}=0.
5320:
5321: @item @var{float}<0 (@code{FASINH}, @code{FSQRT}):
5322: @cindex @code{FASINH}, @var{float}<0
5323: @cindex @code{FSQRT}, @var{float}<0
5324: @cindex floating-point unidentified fault, @code{FASINH} or @code{FSQRT}
5325: @code{-55 throw} (Floating-point unidentified fault). @code{fasinh}
5326: produces values for these inputs on my Linux box (Bug in the C library?)
5327:
5328: @item |@var{float}|>1 (@code{FACOS}, @code{FASIN}, @code{FATANH}):
5329: @cindex @code{FACOS}, |@var{float}|>1
5330: @cindex @code{FASIN}, |@var{float}|>1
5331: @cindex @code{FATANH}, |@var{float}|>1
5332: @cindex floating-point unidentified fault, @code{FACOS}, @code{FASIN} or @code{FATANH}
5333: @code{-55 throw} (Floating-point unidentified fault).
5334:
5335: @item integer part of float cannot be represented by @var{d} in @code{F>D}:
5336: @cindex @code{F>D}, integer part of float cannot be represented by @var{d}
5337: @cindex floating-point unidentified fault, @code{F>D}
5338: @code{-55 throw} (Floating-point unidentified fault).
5339:
5340: @item string larger than pictured numeric output area (@code{f.}, @code{fe.}, @code{fs.}):
5341: @cindex string larger than pictured numeric output area (@code{f.}, @code{fe.}, @code{fs.})
5342: This does not happen.
5343: @end table
5344:
5345: @c =====================================================================
5346: @node The optional Locals word set, The optional Memory-Allocation word set, The optional Floating-Point word set, ANS conformance
5347: @section The optional Locals word set
5348: @c =====================================================================
5349: @cindex system documentation, locals words
5350: @cindex locals words, system documentation
5351:
5352: @menu
5353: * locals-idef:: Implementation Defined Options
5354: * locals-ambcond:: Ambiguous Conditions
5355: @end menu
5356:
5357:
5358: @c ---------------------------------------------------------------------
5359: @node locals-idef, locals-ambcond, The optional Locals word set, The optional Locals word set
5360: @subsection Implementation Defined Options
5361: @c ---------------------------------------------------------------------
5362: @cindex implementation-defined options, locals words
5363: @cindex locals words, implementation-defined options
5364:
5365: @table @i
5366: @item maximum number of locals in a definition:
5367: @cindex maximum number of locals in a definition
5368: @cindex locals, maximum number in a definition
5369: @code{s" #locals" environment? drop .}. Currently 15. This is a lower
5370: bound, e.g., on a 32-bit machine there can be 41 locals of up to 8
5371: characters. The number of locals in a definition is bounded by the size
5372: of locals-buffer, which contains the names of the locals.
5373:
5374: @end table
5375:
5376:
5377: @c ---------------------------------------------------------------------
5378: @node locals-ambcond, , locals-idef, The optional Locals word set
5379: @subsection Ambiguous conditions
5380: @c ---------------------------------------------------------------------
5381: @cindex locals words, ambiguous conditions
5382: @cindex ambiguous conditions, locals words
5383:
5384: @table @i
5385: @item executing a named local in interpretation state:
5386: @cindex local in interpretation state
5387: @cindex Interpreting a compile-only word, for a local
5388: Locals have no interpretation semantics. If you try to perform the
5389: interpretation semantics, you will get a @code{-14 throw} somewhere
5390: (Interpreting a compile-only word). If you perform the compilation
5391: semantics, the locals access will be compiled (irrespective of state).
5392:
5393: @item @var{name} not defined by @code{VALUE} or @code{(LOCAL)} (@code{TO}):
5394: @cindex name not defined by @code{VALUE} or @code{(LOCAL)} used by @code{TO}
5395: @cindex @code{TO} on non-@code{VALUE}s and non-locals
5396: @cindex Invalid name argument, @code{TO}
5397: @code{-32 throw} (Invalid name argument)
5398:
5399: @end table
5400:
5401:
5402: @c =====================================================================
5403: @node The optional Memory-Allocation word set, The optional Programming-Tools word set, The optional Locals word set, ANS conformance
5404: @section The optional Memory-Allocation word set
5405: @c =====================================================================
5406: @cindex system documentation, memory-allocation words
5407: @cindex memory-allocation words, system documentation
5408:
5409: @menu
5410: * memory-idef:: Implementation Defined Options
5411: @end menu
5412:
5413:
5414: @c ---------------------------------------------------------------------
5415: @node memory-idef, , The optional Memory-Allocation word set, The optional Memory-Allocation word set
5416: @subsection Implementation Defined Options
5417: @c ---------------------------------------------------------------------
5418: @cindex implementation-defined options, memory-allocation words
5419: @cindex memory-allocation words, implementation-defined options
5420:
5421: @table @i
5422: @item values and meaning of @var{ior}:
5423: @cindex @var{ior} values and meaning
5424: The @var{ior}s returned by the file and memory allocation words are
5425: intended as throw codes. They typically are in the range
5426: -512@minus{}-2047 of OS errors. The mapping from OS error numbers to
5427: @var{ior}s is -512@minus{}@var{errno}.
5428:
5429: @end table
5430:
5431: @c =====================================================================
5432: @node The optional Programming-Tools word set, The optional Search-Order word set, The optional Memory-Allocation word set, ANS conformance
5433: @section The optional Programming-Tools word set
5434: @c =====================================================================
5435: @cindex system documentation, programming-tools words
5436: @cindex programming-tools words, system documentation
5437:
5438: @menu
5439: * programming-idef:: Implementation Defined Options
5440: * programming-ambcond:: Ambiguous Conditions
5441: @end menu
5442:
5443:
5444: @c ---------------------------------------------------------------------
5445: @node programming-idef, programming-ambcond, The optional Programming-Tools word set, The optional Programming-Tools word set
5446: @subsection Implementation Defined Options
5447: @c ---------------------------------------------------------------------
5448: @cindex implementation-defined options, programming-tools words
5449: @cindex programming-tools words, implementation-defined options
5450:
5451: @table @i
5452: @item ending sequence for input following @code{;CODE} and @code{CODE}:
5453: @cindex @code{;CODE} ending sequence
5454: @cindex @code{CODE} ending sequence
5455: @code{END-CODE}
5456:
5457: @item manner of processing input following @code{;CODE} and @code{CODE}:
5458: @cindex @code{;CODE}, processing input
5459: @cindex @code{CODE}, processing input
5460: The @code{ASSEMBLER} vocabulary is pushed on the search order stack, and
5461: the input is processed by the text interpreter, (starting) in interpret
5462: state.
5463:
5464: @item search order capability for @code{EDITOR} and @code{ASSEMBLER}:
5465: @cindex @code{ASSEMBLER}, search order capability
5466: The ANS Forth search order word set.
5467:
5468: @item source and format of display by @code{SEE}:
5469: @cindex @code{SEE}, source and format of output
5470: The source for @code{see} is the intermediate code used by the inner
5471: interpreter. The current @code{see} tries to output Forth source code
5472: as well as possible.
5473:
5474: @end table
5475:
5476: @c ---------------------------------------------------------------------
5477: @node programming-ambcond, , programming-idef, The optional Programming-Tools word set
5478: @subsection Ambiguous conditions
5479: @c ---------------------------------------------------------------------
5480: @cindex programming-tools words, ambiguous conditions
5481: @cindex ambiguous conditions, programming-tools words
5482:
5483: @table @i
5484:
5485: @item deleting the compilation wordlist (@code{FORGET}):
5486: @cindex @code{FORGET}, deleting the compilation wordlist
5487: Not implemented (yet).
5488:
5489: @item fewer than @var{u}+1 items on the control flow stack (@code{CS-PICK}, @code{CS-ROLL}):
5490: @cindex @code{CS-PICK}, fewer than @var{u}+1 items on the control flow stack
5491: @cindex @code{CS-ROLL}, fewer than @var{u}+1 items on the control flow stack
5492: @cindex control-flow stack underflow
5493: This typically results in an @code{abort"} with a descriptive error
5494: message (may change into a @code{-22 throw} (Control structure mismatch)
5495: in the future). You may also get a memory access error. If you are
5496: unlucky, this ambiguous condition is not caught.
5497:
5498: @item @var{name} can't be found (@code{FORGET}):
5499: @cindex @code{FORGET}, @var{name} can't be found
5500: Not implemented (yet).
5501:
5502: @item @var{name} not defined via @code{CREATE}:
5503: @cindex @code{;CODE}, @var{name} not defined via @code{CREATE}
5504: @code{;CODE} behaves like @code{DOES>} in this respect, i.e., it changes
5505: the execution semantics of the last defined word no matter how it was
5506: defined.
5507:
5508: @item @code{POSTPONE} applied to @code{[IF]}:
5509: @cindex @code{POSTPONE} applied to @code{[IF]}
5510: @cindex @code{[IF]} and @code{POSTPONE}
5511: After defining @code{: X POSTPONE [IF] ; IMMEDIATE}. @code{X} is
5512: equivalent to @code{[IF]}.
5513:
5514: @item reaching the end of the input source before matching @code{[ELSE]} or @code{[THEN]}:
5515: @cindex @code{[IF]}, end of the input source before matching @code{[ELSE]} or @code{[THEN]}
5516: Continue in the same state of conditional compilation in the next outer
5517: input source. Currently there is no warning to the user about this.
5518:
5519: @item removing a needed definition (@code{FORGET}):
5520: @cindex @code{FORGET}, removing a needed definition
5521: Not implemented (yet).
5522:
5523: @end table
5524:
5525:
5526: @c =====================================================================
5527: @node The optional Search-Order word set, , The optional Programming-Tools word set, ANS conformance
5528: @section The optional Search-Order word set
5529: @c =====================================================================
5530: @cindex system documentation, search-order words
5531: @cindex search-order words, system documentation
5532:
5533: @menu
5534: * search-idef:: Implementation Defined Options
5535: * search-ambcond:: Ambiguous Conditions
5536: @end menu
5537:
5538:
5539: @c ---------------------------------------------------------------------
5540: @node search-idef, search-ambcond, The optional Search-Order word set, The optional Search-Order word set
5541: @subsection Implementation Defined Options
5542: @c ---------------------------------------------------------------------
5543: @cindex implementation-defined options, search-order words
5544: @cindex search-order words, implementation-defined options
5545:
5546: @table @i
5547: @item maximum number of word lists in search order:
5548: @cindex maximum number of word lists in search order
5549: @cindex search order, maximum depth
5550: @code{s" wordlists" environment? drop .}. Currently 16.
5551:
5552: @item minimum search order:
5553: @cindex minimum search order
5554: @cindex search order, minimum
5555: @code{root root}.
5556:
5557: @end table
5558:
5559: @c ---------------------------------------------------------------------
5560: @node search-ambcond, , search-idef, The optional Search-Order word set
5561: @subsection Ambiguous conditions
5562: @c ---------------------------------------------------------------------
5563: @cindex search-order words, ambiguous conditions
5564: @cindex ambiguous conditions, search-order words
5565:
5566: @table @i
5567: @item changing the compilation wordlist (during compilation):
5568: @cindex changing the compilation wordlist (during compilation)
5569: @cindex compilation wordlist, change before definition ends
5570: The word is entered into the wordlist that was the compilation wordlist
5571: at the start of the definition. Any changes to the name field (e.g.,
5572: @code{immediate}) or the code field (e.g., when executing @code{DOES>})
5573: are applied to the latest defined word (as reported by @code{last} or
5574: @code{lastxt}), if possible, irrespective of the compilation wordlist.
5575:
5576: @item search order empty (@code{previous}):
5577: @cindex @code{previous}, search order empty
5578: @cindex Vocstack empty, @code{previous}
5579: @code{abort" Vocstack empty"}.
5580:
5581: @item too many word lists in search order (@code{also}):
5582: @cindex @code{also}, too many word lists in search order
5583: @cindex Vocstack full, @code{also}
5584: @code{abort" Vocstack full"}.
5585:
5586: @end table
5587:
5588: @c ***************************************************************
5589: @node Model, Integrating Gforth, ANS conformance, Top
5590: @chapter Model
5591:
5592: This chapter has yet to be written. It will contain information, on
5593: which internal structures you can rely.
5594:
5595: @c ***************************************************************
5596: @node Integrating Gforth, Emacs and Gforth, Model, Top
5597: @chapter Integrating Gforth into C programs
5598:
5599: This is not yet implemented.
5600:
5601: Several people like to use Forth as scripting language for applications
5602: that are otherwise written in C, C++, or some other language.
5603:
5604: The Forth system ATLAST provides facilities for embedding it into
5605: applications; unfortunately it has several disadvantages: most
5606: importantly, it is not based on ANS Forth, and it is apparently dead
5607: (i.e., not developed further and not supported). The facilities
5608: provided by Gforth in this area are inspired by ATLASTs facilities, so
5609: making the switch should not be hard.
5610:
5611: We also tried to design the interface such that it can easily be
5612: implemented by other Forth systems, so that we may one day arrive at a
5613: standardized interface. Such a standard interface would allow you to
5614: replace the Forth system without having to rewrite C code.
5615:
5616: You embed the Gforth interpreter by linking with the library
5617: @code{libgforth.a} (give the compiler the option @code{-lgforth}). All
5618: global symbols in this library that belong to the interface, have the
5619: prefix @code{forth_}. (Global symbols that are used internally have the
5620: prefix @code{gforth_}).
5621:
5622: You can include the declarations of Forth types and the functions and
5623: variables of the interface with @code{#include <forth.h>}.
5624:
5625: Types.
5626:
5627: Variables.
5628:
5629: Data and FP Stack pointer. Area sizes.
5630:
5631: functions.
5632:
5633: forth_init(imagefile)
5634: forth_evaluate(string) exceptions?
5635: forth_goto(address) (or forth_execute(xt)?)
5636: forth_continue() (a corountining mechanism)
5637:
5638: Adding primitives.
5639:
5640: No checking.
5641:
5642: Signals?
5643:
5644: Accessing the Stacks
5645:
5646: @node Emacs and Gforth, Image Files, Integrating Gforth, Top
5647: @chapter Emacs and Gforth
5648: @cindex Emacs and Gforth
5649:
5650: @cindex @file{gforth.el}
5651: @cindex @file{forth.el}
5652: @cindex Rydqvist, Goran
5653: @cindex comment editing commands
5654: @cindex @code{\}, editing with Emacs
5655: @cindex debug tracer editing commands
5656: @cindex @code{~~}, removal with Emacs
5657: @cindex Forth mode in Emacs
5658: Gforth comes with @file{gforth.el}, an improved version of
5659: @file{forth.el} by Goran Rydqvist (included in the TILE package). The
5660: improvements are a better (but still not perfect) handling of
5661: indentation. I have also added comment paragraph filling (@kbd{M-q}),
5662: commenting (@kbd{C-x \}) and uncommenting (@kbd{C-u C-x \}) regions and
5663: removing debugging tracers (@kbd{C-x ~}, @pxref{Debugging}). I left the
5664: stuff I do not use alone, even though some of it only makes sense for
5665: TILE. To get a description of these features, enter Forth mode and type
5666: @kbd{C-h m}.
5667:
5668: @cindex source location of error or debugging output in Emacs
5669: @cindex error output, finding the source location in Emacs
5670: @cindex debugging output, finding the source location in Emacs
5671: In addition, Gforth supports Emacs quite well: The source code locations
5672: given in error messages, debugging output (from @code{~~}) and failed
5673: assertion messages are in the right format for Emacs' compilation mode
5674: (@pxref{Compilation, , Running Compilations under Emacs, emacs, Emacs
5675: Manual}) so the source location corresponding to an error or other
5676: message is only a few keystrokes away (@kbd{C-x `} for the next error,
5677: @kbd{C-c C-c} for the error under the cursor).
5678:
5679: @cindex @file{TAGS} file
5680: @cindex @file{etags.fs}
5681: @cindex viewing the source of a word in Emacs
5682: Also, if you @code{include} @file{etags.fs}, a new @file{TAGS} file
5683: (@pxref{Tags, , Tags Tables, emacs, Emacs Manual}) will be produced that
5684: contains the definitions of all words defined afterwards. You can then
5685: find the source for a word using @kbd{M-.}. Note that emacs can use
5686: several tags files at the same time (e.g., one for the Gforth sources
5687: and one for your program, @pxref{Select Tags Table,,Selecting a Tags
5688: Table,emacs, Emacs Manual}). The TAGS file for the preloaded words is
5689: @file{$(datadir)/gforth/$(VERSION)/TAGS} (e.g.,
5690: @file{/usr/local/share/gforth/0.2.0/TAGS}).
5691:
5692: @cindex @file{.emacs}
5693: To get all these benefits, add the following lines to your @file{.emacs}
5694: file:
5695:
5696: @example
5697: (autoload 'forth-mode "gforth.el")
5698: (setq auto-mode-alist (cons '("\\.fs\\'" . forth-mode) auto-mode-alist))
5699: @end example
5700:
5701: @node Image Files, Engine, Emacs and Gforth, Top
5702: @chapter Image Files
5703: @cindex image files
5704: @cindex @code{.fi} files
5705: @cindex precompiled Forth code
5706: @cindex dictionary in persistent form
5707: @cindex persistent form of dictionary
5708:
5709: An image file is a file containing an image of the Forth dictionary,
5710: i.e., compiled Forth code and data residing in the dictionary. By
5711: convention, we use the extension @code{.fi} for image files.
5712:
5713: @menu
5714: * Image File Background:: Why have image files?
5715: * Non-Relocatable Image Files:: don't always work.
5716: * Data-Relocatable Image Files:: are better.
5717: * Fully Relocatable Image Files:: better yet.
5718: * Stack and Dictionary Sizes:: Setting the default sizes for an image.
5719: * Running Image Files:: @code{gforth -i @var{file}} or @var{file}.
5720: * Modifying the Startup Sequence:: and turnkey applications.
5721: @end menu
5722:
5723: @node Image File Background, Non-Relocatable Image Files, Image Files, Image Files
5724: @section Image File Background
5725: @cindex image file background
5726:
5727: Our Forth system consists not only of primitives, but also of
5728: definitions written in Forth. Since the Forth compiler itself belongs to
5729: those definitions, it is not possible to start the system with the
5730: primitives and the Forth source alone. Therefore we provide the Forth
5731: code as an image file in nearly executable form. At the start of the
5732: system a C routine loads the image file into memory, optionally
5733: relocates the addresses, then sets up the memory (stacks etc.) according
5734: to information in the image file, and starts executing Forth code.
5735:
5736: The image file variants represent different compromises between the
5737: goals of making it easy to generate image files and making them
5738: portable.
5739:
5740: @cindex relocation at run-time
5741: Win32Forth 3.4 and Mitch Bradleys @code{cforth} use relocation at
5742: run-time. This avoids many of the complications discussed below (image
5743: files are data relocatable without further ado), but costs performance
5744: (one addition per memory access).
5745:
5746: @cindex relocation at load-time
5747: By contrast, our loader performs relocation at image load time. The
5748: loader also has to replace tokens standing for primitive calls with the
5749: appropriate code-field addresses (or code addresses in the case of
5750: direct threading).
5751:
5752: There are three kinds of image files, with different degrees of
5753: relocatability: non-relocatable, data-relocatable, and fully relocatable
5754: image files.
5755:
5756: @cindex image file loader
5757: @cindex relocating loader
5758: @cindex loader for image files
5759: These image file variants have several restrictions in common; they are
5760: caused by the design of the image file loader:
5761:
5762: @itemize @bullet
5763: @item
5764: There is only one segment; in particular, this means, that an image file
5765: cannot represent @code{ALLOCATE}d memory chunks (and pointers to
5766: them). And the contents of the stacks are not represented, either.
5767:
5768: @item
5769: The only kinds of relocation supported are: adding the same offset to
5770: all cells that represent data addresses; and replacing special tokens
5771: with code addresses or with pieces of machine code.
5772:
5773: If any complex computations involving addresses are performed, the
5774: results cannot be represented in the image file. Several applications that
5775: use such computations come to mind:
5776: @itemize @minus
5777: @item
5778: Hashing addresses (or data structures which contain addresses) for table
5779: lookup. If you use Gforth's @code{table}s or @code{wordlist}s for this
5780: purpose, you will have no problem, because the hash tables are
5781: recomputed automatically when the system is started. If you use your own
5782: hash tables, you will have to do something similar.
5783:
5784: @item
5785: There's a cute implementation of doubly-linked lists that uses
5786: @code{XOR}ed addresses. You could represent such lists as singly-linked
5787: in the image file, and restore the doubly-linked representation on
5788: startup.@footnote{In my opinion, though, you should think thrice before
5789: using a doubly-linked list (whatever implementation).}
5790:
5791: @item
5792: The code addresses of run-time routines like @code{docol:} cannot be
5793: represented in the image file (because their tokens would be replaced by
5794: machine code in direct threaded implementations). As a workaround,
5795: compute these addresses at run-time with @code{>code-address} from the
5796: executions tokens of appropriate words (see the definitions of
5797: @code{docol:} and friends in @file{kernel.fs}).
5798:
5799: @item
5800: On many architectures addresses are represented in machine code in some
5801: shifted or mangled form. You cannot put @code{CODE} words that contain
5802: absolute addresses in this form in a relocatable image file. Workarounds
5803: are representing the address in some relative form (e.g., relative to
5804: the CFA, which is present in some register), or loading the address from
5805: a place where it is stored in a non-mangled form.
5806: @end itemize
5807: @end itemize
5808:
5809: @node Non-Relocatable Image Files, Data-Relocatable Image Files, Image File Background, Image Files
5810: @section Non-Relocatable Image Files
5811: @cindex non-relocatable image files
5812: @cindex image files, non-relocatable
5813:
5814: These files are simple memory dumps of the dictionary. They are specific
5815: to the executable (i.e., @file{gforth} file) they were created
5816: with. What's worse, they are specific to the place on which the
5817: dictionary resided when the image was created. Now, there is no
5818: guarantee that the dictionary will reside at the same place the next
5819: time you start Gforth, so there's no guarantee that a non-relocatable
5820: image will work the next time (Gforth will complain instead of crashing,
5821: though).
5822:
5823: You can create a non-relocatable image file with
5824:
5825: doc-savesystem
5826:
5827: @node Data-Relocatable Image Files, Fully Relocatable Image Files, Non-Relocatable Image Files, Image Files
5828: @section Data-Relocatable Image Files
5829: @cindex data-relocatable image files
5830: @cindex image files, data-relocatable
5831:
5832: These files contain relocatable data addresses, but fixed code addresses
5833: (instead of tokens). They are specific to the executable (i.e.,
5834: @file{gforth} file) they were created with. For direct threading on some
5835: architectures (e.g., the i386), data-relocatable images do not work. You
5836: get a data-relocatable image, if you use @file{gforthmi} with a
5837: Gforth binary that is not doubly indirect threaded (@pxref{Fully
5838: Relocatable Image Files}).
5839:
5840: @node Fully Relocatable Image Files, Stack and Dictionary Sizes, Data-Relocatable Image Files, Image Files
5841: @section Fully Relocatable Image Files
5842: @cindex fully relocatable image files
5843: @cindex image files, fully relocatable
5844:
5845: @cindex @file{kern*.fi}, relocatability
5846: @cindex @file{gforth.fi}, relocatability
5847: These image files have relocatable data addresses, and tokens for code
5848: addresses. They can be used with different binaries (e.g., with and
5849: without debugging) on the same machine, and even across machines with
5850: the same data formats (byte order, cell size, floating point
5851: format). However, they are usually specific to the version of Gforth
5852: they were created with. The files @file{gforth.fi} and @file{kernl*.fi}
5853: are fully relocatable.
5854:
5855: There are two ways to create a fully relocatable image file:
5856:
5857: @menu
5858: * gforthmi:: The normal way
5859: * cross.fs:: The hard way
5860: @end menu
5861:
5862: @node gforthmi, cross.fs, Fully Relocatable Image Files, Fully Relocatable Image Files
5863: @subsection @file{gforthmi}
5864: @cindex @file{comp-i.fs}
5865: @cindex @file{gforthmi}
5866:
5867: You will usually use @file{gforthmi}. If you want to create an
5868: image @var{file} that contains everything you would load by invoking
5869: Gforth with @code{gforth @var{options}}, you simply say
5870: @example
5871: gforthmi @var{file} @var{options}
5872: @end example
5873:
5874: E.g., if you want to create an image @file{asm.fi} that has the file
5875: @file{asm.fs} loaded in addition to the usual stuff, you could do it
5876: like this:
5877:
5878: @example
5879: gforthmi asm.fi asm.fs
5880: @end example
5881:
5882: @file{gforthmi} works like this: It produces two non-relocatable
5883: images for different addresses and then compares them. Its output
5884: reflects this: first you see the output (if any) of the two Gforth
5885: invocations that produce the nonrelocatable image files, then you see
5886: the output of the comparing program: It displays the offset used for
5887: data addresses and the offset used for code addresses;
5888: moreover, for each cell that cannot be represented correctly in the
5889: image files, it displays a line like the following one:
5890:
5891: @example
5892: 78DC BFFFFA50 BFFFFA40
5893: @end example
5894:
5895: This means that at offset $78dc from @code{forthstart}, one input image
5896: contains $bffffa50, and the other contains $bffffa40. Since these cells
5897: cannot be represented correctly in the output image, you should examine
5898: these places in the dictionary and verify that these cells are dead
5899: (i.e., not read before they are written).
5900:
5901: @cindex @code{savesystem} during @file{gforthmi}
5902: @cindex @code{bye} during @file{gforthmi}
5903: @cindex doubly indirect threaded code
5904: @cindex environment variable @code{GFORTHD}
5905: @cindex @code{GFORTHD} environment variable
5906: @cindex @code{gforth-ditc}
5907: There are a few wrinkles: After processing the passed @var{options}, the
5908: words @code{savesystem} and @code{bye} must be visible. A special doubly
5909: indirect threaded version of the @file{gforth} executable is used for
5910: creating the nonrelocatable images; you can pass the exact filename of
5911: this executable through the environment variable @code{GFORTHD}
5912: (default: @file{gforth-ditc}); if you pass a version that is not doubly
5913: indirect threaded, you will not get a fully relocatable image, but a
5914: data-relocatable image (because there is no code address offset).
5915:
5916: @node cross.fs, , gforthmi, Fully Relocatable Image Files
5917: @subsection @file{cross.fs}
5918: @cindex @file{cross.fs}
5919: @cindex cross-compiler
5920: @cindex metacompiler
5921:
5922: You can also use @code{cross}, a batch compiler that accepts a Forth-like
5923: programming language. This @code{cross} language has to be documented
5924: yet.
5925:
5926: @cindex target compiler
5927: @code{cross} also allows you to create image files for machines with
5928: different data sizes and data formats than the one used for generating
5929: the image file. You can also use it to create an application image that
5930: does not contain a Forth compiler. These features are bought with
5931: restrictions and inconveniences in programming. E.g., addresses have to
5932: be stored in memory with special words (@code{A!}, @code{A,}, etc.) in
5933: order to make the code relocatable.
5934:
5935:
5936: @node Stack and Dictionary Sizes, Running Image Files, Fully Relocatable Image Files, Image Files
5937: @section Stack and Dictionary Sizes
5938: @cindex image file, stack and dictionary sizes
5939: @cindex dictionary size default
5940: @cindex stack size default
5941:
5942: If you invoke Gforth with a command line flag for the size
5943: (@pxref{Invoking Gforth}), the size you specify is stored in the
5944: dictionary. If you save the dictionary with @code{savesystem} or create
5945: an image with @file{gforthmi}, this size will become the default
5946: for the resulting image file. E.g., the following will create a
5947: fully relocatable version of gforth.fi with a 1MB dictionary:
5948:
5949: @example
5950: gforthmi gforth.fi -m 1M
5951: @end example
5952:
5953: In other words, if you want to set the default size for the dictionary
5954: and the stacks of an image, just invoke @file{gforthmi} with the
5955: appropriate options when creating the image.
5956:
5957: @cindex stack size, cache-friendly
5958: Note: For cache-friendly behaviour (i.e., good performance), you should
5959: make the sizes of the stacks modulo, say, 2K, somewhat different. E.g.,
5960: the default stack sizes are: data: 16k (mod 2k=0); fp: 15.5k (mod
5961: 2k=1.5k); return: 15k(mod 2k=1k); locals: 14.5k (mod 2k=0.5k).
5962:
5963: @node Running Image Files, Modifying the Startup Sequence, Stack and Dictionary Sizes, Image Files
5964: @section Running Image Files
5965: @cindex running image files
5966: @cindex invoking image files
5967: @cindex image file invocation
5968:
5969: @cindex -i, invoke image file
5970: @cindex --image file, invoke image file
5971: You can invoke Gforth with an image file @var{image} instead of the
5972: default @file{gforth.fi} with the @code{-i} flag (@pxref{Invoking Gforth}):
5973: @example
5974: gforth -i @var{image}
5975: @end example
5976:
5977: @cindex executable image file
5978: @cindex image files, executable
5979: If your operating system supports starting scripts with a line of the
5980: form @code{#! ...}, you just have to type the image file name to start
5981: Gforth with this image file (note that the file extension @code{.fi} is
5982: just a convention). I.e., to run Gforth with the image file @var{image},
5983: you can just type @var{image} instead of @code{gforth -i @var{image}}.
5984:
5985: doc-#!
5986:
5987: @node Modifying the Startup Sequence, , Running Image Files, Image Files
5988: @section Modifying the Startup Sequence
5989: @cindex startup sequence for image file
5990: @cindex image file initialization sequence
5991: @cindex initialization sequence of image file
5992:
5993: You can add your own initialization to the startup sequence through the
5994: deferred word
5995:
5996: doc-'cold
5997:
5998: @code{'cold} is invoked just before the image-specific command line
5999: processing (by default, loading files and evaluating (@code{-e}) strings)
6000: starts.
6001:
6002: A sequence for adding your initialization usually looks like this:
6003:
6004: @example
6005: :noname
6006: Defers 'cold \ do other initialization stuff (e.g., rehashing wordlists)
6007: ... \ your stuff
6008: ; IS 'cold
6009: @end example
6010:
6011: @cindex turnkey image files
6012: @cindex image files, turnkey applications
6013: You can make a turnkey image by letting @code{'cold} execute a word
6014: (your turnkey application) that never returns; instead, it exits Gforth
6015: via @code{bye} or @code{throw}.
6016:
6017: @cindex command-line arguments, access
6018: @cindex arguments on the command line, access
6019: You can access the (image-specific) command-line arguments through the
6020: variables @code{argc} and @code{argv}. @code{arg} provides conventient
6021: access to @code{argv}.
6022:
6023: doc-argc
6024: doc-argv
6025: doc-arg
6026:
6027: If @code{'cold} exits normally, Gforth processes the command-line
6028: arguments as files to be loaded and strings to be evaluated. Therefore,
6029: @code{'cold} should remove the arguments it has used in this case.
6030:
6031: @c ******************************************************************
6032: @node Engine, Bugs, Image Files, Top
6033: @chapter Engine
6034: @cindex engine
6035: @cindex virtual machine
6036:
6037: Reading this section is not necessary for programming with Gforth. It
6038: may be helpful for finding your way in the Gforth sources.
6039:
6040: The ideas in this section have also been published in the papers
6041: @cite{ANS fig/GNU/??? Forth} (in German) by Bernd Paysan, presented at
6042: the Forth-Tagung '93 and @cite{A Portable Forth Engine} by M. Anton
6043: Ertl, presented at EuroForth '93; the latter is available at
6044: @*@url{http://www.complang.tuwien.ac.at/papers/ertl93.ps.Z}.
6045:
6046: @menu
6047: * Portability::
6048: * Threading::
6049: * Primitives::
6050: * Performance::
6051: @end menu
6052:
6053: @node Portability, Threading, Engine, Engine
6054: @section Portability
6055: @cindex engine portability
6056:
6057: One of the main goals of the effort is availability across a wide range
6058: of personal machines. fig-Forth, and, to a lesser extent, F83, achieved
6059: this goal by manually coding the engine in assembly language for several
6060: then-popular processors. This approach is very labor-intensive and the
6061: results are short-lived due to progress in computer architecture.
6062:
6063: @cindex C, using C for the engine
6064: Others have avoided this problem by coding in C, e.g., Mitch Bradley
6065: (cforth), Mikael Patel (TILE) and Dirk Zoller (pfe). This approach is
6066: particularly popular for UNIX-based Forths due to the large variety of
6067: architectures of UNIX machines. Unfortunately an implementation in C
6068: does not mix well with the goals of efficiency and with using
6069: traditional techniques: Indirect or direct threading cannot be expressed
6070: in C, and switch threading, the fastest technique available in C, is
6071: significantly slower. Another problem with C is that it is very
6072: cumbersome to express double integer arithmetic.
6073:
6074: @cindex GNU C for the engine
6075: @cindex long long
6076: Fortunately, there is a portable language that does not have these
6077: limitations: GNU C, the version of C processed by the GNU C compiler
6078: (@pxref{C Extensions, , Extensions to the C Language Family, gcc.info,
6079: GNU C Manual}). Its labels as values feature (@pxref{Labels as Values, ,
6080: Labels as Values, gcc.info, GNU C Manual}) makes direct and indirect
6081: threading possible, its @code{long long} type (@pxref{Long Long, ,
6082: Double-Word Integers, gcc.info, GNU C Manual}) corresponds to Forth's
6083: double numbers@footnote{Unfortunately, long longs are not implemented
6084: properly on all machines (e.g., on alpha-osf1, long longs are only 64
6085: bits, the same size as longs (and pointers), but they should be twice as
1.4 anton 6086: long according to @pxref{Long Long, , Double-Word Integers, gcc.info, GNU
1.1 anton 6087: C Manual}). So, we had to implement doubles in C after all. Still, on
6088: most machines we can use long longs and achieve better performance than
6089: with the emulation package.}. GNU C is available for free on all
6090: important (and many unimportant) UNIX machines, VMS, 80386s running
6091: MS-DOS, the Amiga, and the Atari ST, so a Forth written in GNU C can run
6092: on all these machines.
6093:
6094: Writing in a portable language has the reputation of producing code that
6095: is slower than assembly. For our Forth engine we repeatedly looked at
6096: the code produced by the compiler and eliminated most compiler-induced
6097: inefficiencies by appropriate changes in the source code.
6098:
6099: @cindex explicit register declarations
6100: @cindex --enable-force-reg, configuration flag
6101: @cindex -DFORCE_REG
6102: However, register allocation cannot be portably influenced by the
6103: programmer, leading to some inefficiencies on register-starved
6104: machines. We use explicit register declarations (@pxref{Explicit Reg
6105: Vars, , Variables in Specified Registers, gcc.info, GNU C Manual}) to
6106: improve the speed on some machines. They are turned on by using the
6107: configuration flag @code{--enable-force-reg} (@code{gcc} switch
6108: @code{-DFORCE_REG}). Unfortunately, this feature not only depends on the
6109: machine, but also on the compiler version: On some machines some
6110: compiler versions produce incorrect code when certain explicit register
6111: declarations are used. So by default @code{-DFORCE_REG} is not used.
6112:
6113: @node Threading, Primitives, Portability, Engine
6114: @section Threading
6115: @cindex inner interpreter implementation
6116: @cindex threaded code implementation
6117:
6118: @cindex labels as values
6119: GNU C's labels as values extension (available since @code{gcc-2.0},
6120: @pxref{Labels as Values, , Labels as Values, gcc.info, GNU C Manual})
6121: makes it possible to take the address of @var{label} by writing
6122: @code{&&@var{label}}. This address can then be used in a statement like
6123: @code{goto *@var{address}}. I.e., @code{goto *&&x} is the same as
6124: @code{goto x}.
6125:
6126: @cindex NEXT, indirect threaded
6127: @cindex indirect threaded inner interpreter
6128: @cindex inner interpreter, indirect threaded
6129: With this feature an indirect threaded NEXT looks like:
6130: @example
6131: cfa = *ip++;
6132: ca = *cfa;
6133: goto *ca;
6134: @end example
6135: @cindex instruction pointer
6136: For those unfamiliar with the names: @code{ip} is the Forth instruction
6137: pointer; the @code{cfa} (code-field address) corresponds to ANS Forths
6138: execution token and points to the code field of the next word to be
6139: executed; The @code{ca} (code address) fetched from there points to some
6140: executable code, e.g., a primitive or the colon definition handler
6141: @code{docol}.
6142:
6143: @cindex NEXT, direct threaded
6144: @cindex direct threaded inner interpreter
6145: @cindex inner interpreter, direct threaded
6146: Direct threading is even simpler:
6147: @example
6148: ca = *ip++;
6149: goto *ca;
6150: @end example
6151:
6152: Of course we have packaged the whole thing neatly in macros called
6153: @code{NEXT} and @code{NEXT1} (the part of NEXT after fetching the cfa).
6154:
6155: @menu
6156: * Scheduling::
6157: * Direct or Indirect Threaded?::
6158: * DOES>::
6159: @end menu
6160:
6161: @node Scheduling, Direct or Indirect Threaded?, Threading, Threading
6162: @subsection Scheduling
6163: @cindex inner interpreter optimization
6164:
6165: There is a little complication: Pipelined and superscalar processors,
6166: i.e., RISC and some modern CISC machines can process independent
6167: instructions while waiting for the results of an instruction. The
6168: compiler usually reorders (schedules) the instructions in a way that
6169: achieves good usage of these delay slots. However, on our first tries
6170: the compiler did not do well on scheduling primitives. E.g., for
6171: @code{+} implemented as
6172: @example
6173: n=sp[0]+sp[1];
6174: sp++;
6175: sp[0]=n;
6176: NEXT;
6177: @end example
6178: the NEXT comes strictly after the other code, i.e., there is nearly no
6179: scheduling. After a little thought the problem becomes clear: The
6180: compiler cannot know that sp and ip point to different addresses (and
6181: the version of @code{gcc} we used would not know it even if it was
6182: possible), so it could not move the load of the cfa above the store to
6183: the TOS. Indeed the pointers could be the same, if code on or very near
6184: the top of stack were executed. In the interest of speed we chose to
6185: forbid this probably unused ``feature'' and helped the compiler in
6186: scheduling: NEXT is divided into the loading part (@code{NEXT_P1}) and
6187: the goto part (@code{NEXT_P2}). @code{+} now looks like:
6188: @example
6189: n=sp[0]+sp[1];
6190: sp++;
6191: NEXT_P1;
6192: sp[0]=n;
6193: NEXT_P2;
6194: @end example
6195: This can be scheduled optimally by the compiler.
6196:
6197: This division can be turned off with the switch @code{-DCISC_NEXT}. This
6198: switch is on by default on machines that do not profit from scheduling
6199: (e.g., the 80386), in order to preserve registers.
6200:
6201: @node Direct or Indirect Threaded?, DOES>, Scheduling, Threading
6202: @subsection Direct or Indirect Threaded?
6203: @cindex threading, direct or indirect?
6204:
6205: @cindex -DDIRECT_THREADED
6206: Both! After packaging the nasty details in macro definitions we
6207: realized that we could switch between direct and indirect threading by
6208: simply setting a compilation flag (@code{-DDIRECT_THREADED}) and
6209: defining a few machine-specific macros for the direct-threading case.
6210: On the Forth level we also offer access words that hide the
6211: differences between the threading methods (@pxref{Threading Words}).
6212:
6213: Indirect threading is implemented completely machine-independently.
6214: Direct threading needs routines for creating jumps to the executable
6215: code (e.g. to docol or dodoes). These routines are inherently
6216: machine-dependent, but they do not amount to many source lines. I.e.,
6217: even porting direct threading to a new machine is a small effort.
6218:
6219: @cindex --enable-indirect-threaded, configuration flag
6220: @cindex --enable-direct-threaded, configuration flag
6221: The default threading method is machine-dependent. You can enforce a
6222: specific threading method when building Gforth with the configuration
6223: flag @code{--enable-direct-threaded} or
6224: @code{--enable-indirect-threaded}. Note that direct threading is not
6225: supported on all machines.
6226:
6227: @node DOES>, , Direct or Indirect Threaded?, Threading
6228: @subsection DOES>
6229: @cindex @code{DOES>} implementation
6230:
6231: @cindex dodoes routine
6232: @cindex DOES-code
6233: One of the most complex parts of a Forth engine is @code{dodoes}, i.e.,
6234: the chunk of code executed by every word defined by a
6235: @code{CREATE}...@code{DOES>} pair. The main problem here is: How to find
6236: the Forth code to be executed, i.e. the code after the
6237: @code{DOES>} (the DOES-code)? There are two solutions:
6238:
6239: In fig-Forth the code field points directly to the dodoes and the
6240: DOES-code address is stored in the cell after the code address (i.e. at
6241: @code{@var{cfa} cell+}). It may seem that this solution is illegal in
6242: the Forth-79 and all later standards, because in fig-Forth this address
6243: lies in the body (which is illegal in these standards). However, by
6244: making the code field larger for all words this solution becomes legal
6245: again. We use this approach for the indirect threaded version and for
6246: direct threading on some machines. Leaving a cell unused in most words
6247: is a bit wasteful, but on the machines we are targeting this is hardly a
6248: problem. The other reason for having a code field size of two cells is
6249: to avoid having different image files for direct and indirect threaded
6250: systems (direct threaded systems require two-cell code fields on many
6251: machines).
6252:
6253: @cindex DOES-handler
6254: The other approach is that the code field points or jumps to the cell
6255: after @code{DOES}. In this variant there is a jump to @code{dodoes} at
6256: this address (the DOES-handler). @code{dodoes} can then get the
6257: DOES-code address by computing the code address, i.e., the address of
6258: the jump to dodoes, and add the length of that jump field. A variant of
6259: this is to have a call to @code{dodoes} after the @code{DOES>}; then the
6260: return address (which can be found in the return register on RISCs) is
6261: the DOES-code address. Since the two cells available in the code field
6262: are used up by the jump to the code address in direct threading on many
6263: architectures, we use this approach for direct threading on these
6264: architectures. We did not want to add another cell to the code field.
6265:
6266: @node Primitives, Performance, Threading, Engine
6267: @section Primitives
6268: @cindex primitives, implementation
6269: @cindex virtual machine instructions, implementation
6270:
6271: @menu
6272: * Automatic Generation::
6273: * TOS Optimization::
6274: * Produced code::
6275: @end menu
6276:
6277: @node Automatic Generation, TOS Optimization, Primitives, Primitives
6278: @subsection Automatic Generation
6279: @cindex primitives, automatic generation
6280:
6281: @cindex @file{prims2x.fs}
6282: Since the primitives are implemented in a portable language, there is no
6283: longer any need to minimize the number of primitives. On the contrary,
6284: having many primitives has an advantage: speed. In order to reduce the
6285: number of errors in primitives and to make programming them easier, we
6286: provide a tool, the primitive generator (@file{prims2x.fs}), that
6287: automatically generates most (and sometimes all) of the C code for a
6288: primitive from the stack effect notation. The source for a primitive
6289: has the following form:
6290:
6291: @cindex primitive source format
6292: @format
6293: @var{Forth-name} @var{stack-effect} @var{category} [@var{pronounc.}]
6294: [@code{""}@var{glossary entry}@code{""}]
6295: @var{C code}
6296: [@code{:}
6297: @var{Forth code}]
6298: @end format
6299:
6300: The items in brackets are optional. The category and glossary fields
6301: are there for generating the documentation, the Forth code is there
6302: for manual implementations on machines without GNU C. E.g., the source
6303: for the primitive @code{+} is:
6304: @example
6305: + n1 n2 -- n core plus
6306: n = n1+n2;
6307: @end example
6308:
6309: This looks like a specification, but in fact @code{n = n1+n2} is C
6310: code. Our primitive generation tool extracts a lot of information from
6311: the stack effect notations@footnote{We use a one-stack notation, even
6312: though we have separate data and floating-point stacks; The separate
6313: notation can be generated easily from the unified notation.}: The number
6314: of items popped from and pushed on the stack, their type, and by what
6315: name they are referred to in the C code. It then generates a C code
6316: prelude and postlude for each primitive. The final C code for @code{+}
6317: looks like this:
6318:
6319: @example
6320: I_plus: /* + ( n1 n2 -- n ) */ /* label, stack effect */
6321: /* */ /* documentation */
6322: @{
6323: DEF_CA /* definition of variable ca (indirect threading) */
6324: Cell n1; /* definitions of variables */
6325: Cell n2;
6326: Cell n;
6327: n1 = (Cell) sp[1]; /* input */
6328: n2 = (Cell) TOS;
6329: sp += 1; /* stack adjustment */
6330: NAME("+") /* debugging output (with -DDEBUG) */
6331: @{
6332: n = n1+n2; /* C code taken from the source */
6333: @}
6334: NEXT_P1; /* NEXT part 1 */
6335: TOS = (Cell)n; /* output */
6336: NEXT_P2; /* NEXT part 2 */
6337: @}
6338: @end example
6339:
6340: This looks long and inefficient, but the GNU C compiler optimizes quite
6341: well and produces optimal code for @code{+} on, e.g., the R3000 and the
6342: HP RISC machines: Defining the @code{n}s does not produce any code, and
6343: using them as intermediate storage also adds no cost.
6344:
6345: There are also other optimizations, that are not illustrated by this
6346: example: Assignments between simple variables are usually for free (copy
6347: propagation). If one of the stack items is not used by the primitive
6348: (e.g. in @code{drop}), the compiler eliminates the load from the stack
6349: (dead code elimination). On the other hand, there are some things that
6350: the compiler does not do, therefore they are performed by
6351: @file{prims2x.fs}: The compiler does not optimize code away that stores
6352: a stack item to the place where it just came from (e.g., @code{over}).
6353:
6354: While programming a primitive is usually easy, there are a few cases
6355: where the programmer has to take the actions of the generator into
6356: account, most notably @code{?dup}, but also words that do not (always)
6357: fall through to NEXT.
6358:
6359: @node TOS Optimization, Produced code, Automatic Generation, Primitives
6360: @subsection TOS Optimization
6361: @cindex TOS optimization for primitives
6362: @cindex primitives, keeping the TOS in a register
6363:
6364: An important optimization for stack machine emulators, e.g., Forth
6365: engines, is keeping one or more of the top stack items in
6366: registers. If a word has the stack effect @var{in1}...@var{inx} @code{--}
6367: @var{out1}...@var{outy}, keeping the top @var{n} items in registers
6368: @itemize @bullet
6369: @item
6370: is better than keeping @var{n-1} items, if @var{x>=n} and @var{y>=n},
6371: due to fewer loads from and stores to the stack.
6372: @item is slower than keeping @var{n-1} items, if @var{x<>y} and @var{x<n} and
6373: @var{y<n}, due to additional moves between registers.
6374: @end itemize
6375:
6376: @cindex -DUSE_TOS
6377: @cindex -DUSE_NO_TOS
6378: In particular, keeping one item in a register is never a disadvantage,
6379: if there are enough registers. Keeping two items in registers is a
6380: disadvantage for frequent words like @code{?branch}, constants,
6381: variables, literals and @code{i}. Therefore our generator only produces
6382: code that keeps zero or one items in registers. The generated C code
6383: covers both cases; the selection between these alternatives is made at
6384: C-compile time using the switch @code{-DUSE_TOS}. @code{TOS} in the C
6385: code for @code{+} is just a simple variable name in the one-item case,
6386: otherwise it is a macro that expands into @code{sp[0]}. Note that the
6387: GNU C compiler tries to keep simple variables like @code{TOS} in
6388: registers, and it usually succeeds, if there are enough registers.
6389:
6390: @cindex -DUSE_FTOS
6391: @cindex -DUSE_NO_FTOS
6392: The primitive generator performs the TOS optimization for the
6393: floating-point stack, too (@code{-DUSE_FTOS}). For floating-point
6394: operations the benefit of this optimization is even larger:
6395: floating-point operations take quite long on most processors, but can be
6396: performed in parallel with other operations as long as their results are
6397: not used. If the FP-TOS is kept in a register, this works. If
6398: it is kept on the stack, i.e., in memory, the store into memory has to
6399: wait for the result of the floating-point operation, lengthening the
6400: execution time of the primitive considerably.
6401:
6402: The TOS optimization makes the automatic generation of primitives a
6403: bit more complicated. Just replacing all occurrences of @code{sp[0]} by
6404: @code{TOS} is not sufficient. There are some special cases to
6405: consider:
6406: @itemize @bullet
6407: @item In the case of @code{dup ( w -- w w )} the generator must not
6408: eliminate the store to the original location of the item on the stack,
6409: if the TOS optimization is turned on.
6410: @item Primitives with stack effects of the form @code{--}
6411: @var{out1}...@var{outy} must store the TOS to the stack at the start.
6412: Likewise, primitives with the stack effect @var{in1}...@var{inx} @code{--}
6413: must load the TOS from the stack at the end. But for the null stack
6414: effect @code{--} no stores or loads should be generated.
6415: @end itemize
6416:
6417: @node Produced code, , TOS Optimization, Primitives
6418: @subsection Produced code
6419: @cindex primitives, assembly code listing
6420:
6421: @cindex @file{engine.s}
6422: To see what assembly code is produced for the primitives on your machine
6423: with your compiler and your flag settings, type @code{make engine.s} and
6424: look at the resulting file @file{engine.s}.
6425:
6426: @node Performance, , Primitives, Engine
6427: @section Performance
6428: @cindex performance of some Forth interpreters
6429: @cindex engine performance
6430: @cindex benchmarking Forth systems
6431: @cindex Gforth performance
6432:
6433: On RISCs the Gforth engine is very close to optimal; i.e., it is usually
6434: impossible to write a significantly faster engine.
6435:
6436: On register-starved machines like the 386 architecture processors
6437: improvements are possible, because @code{gcc} does not utilize the
6438: registers as well as a human, even with explicit register declarations;
6439: e.g., Bernd Beuster wrote a Forth system fragment in assembly language
6440: and hand-tuned it for the 486; this system is 1.19 times faster on the
6441: Sieve benchmark on a 486DX2/66 than Gforth compiled with
6442: @code{gcc-2.6.3} with @code{-DFORCE_REG}.
6443:
6444: @cindex Win32Forth performance
6445: @cindex NT Forth performance
6446: @cindex eforth performance
6447: @cindex ThisForth performance
6448: @cindex PFE performance
6449: @cindex TILE performance
6450: However, this potential advantage of assembly language implementations
6451: is not necessarily realized in complete Forth systems: We compared
6452: Gforth (direct threaded, compiled with @code{gcc-2.6.3} and
6453: @code{-DFORCE_REG}) with Win32Forth 1.2093, LMI's NT Forth (Beta, May
6454: 1994) and Eforth (with and without peephole (aka pinhole) optimization
6455: of the threaded code); all these systems were written in assembly
6456: language. We also compared Gforth with three systems written in C:
6457: PFE-0.9.14 (compiled with @code{gcc-2.6.3} with the default
6458: configuration for Linux: @code{-O2 -fomit-frame-pointer -DUSE_REGS
6459: -DUNROLL_NEXT}), ThisForth Beta (compiled with gcc-2.6.3 -O3
6460: -fomit-frame-pointer; ThisForth employs peephole optimization of the
6461: threaded code) and TILE (compiled with @code{make opt}). We benchmarked
6462: Gforth, PFE, ThisForth and TILE on a 486DX2/66 under Linux. Kenneth
6463: O'Heskin kindly provided the results for Win32Forth and NT Forth on a
6464: 486DX2/66 with similar memory performance under Windows NT. Marcel
6465: Hendrix ported Eforth to Linux, then extended it to run the benchmarks,
6466: added the peephole optimizer, ran the benchmarks and reported the
6467: results.
6468:
6469: We used four small benchmarks: the ubiquitous Sieve; bubble-sorting and
6470: matrix multiplication come from the Stanford integer benchmarks and have
6471: been translated into Forth by Martin Fraeman; we used the versions
6472: included in the TILE Forth package, but with bigger data set sizes; and
6473: a recursive Fibonacci number computation for benchmarking calling
6474: performance. The following table shows the time taken for the benchmarks
6475: scaled by the time taken by Gforth (in other words, it shows the speedup
6476: factor that Gforth achieved over the other systems).
6477:
6478: @example
6479: relative Win32- NT eforth This-
6480: time Gforth Forth Forth eforth +opt PFE Forth TILE
6481: sieve 1.00 1.39 1.14 1.39 0.85 1.58 3.18 8.58
6482: bubble 1.00 1.31 1.41 1.48 0.88 1.50 3.88
6483: matmul 1.00 1.47 1.35 1.46 0.74 1.58 4.09
6484: fib 1.00 1.52 1.34 1.22 0.86 1.74 2.99 4.30
6485: @end example
6486:
6487: You may find the good performance of Gforth compared with the systems
6488: written in assembly language quite surprising. One important reason for
6489: the disappointing performance of these systems is probably that they are
6490: not written optimally for the 486 (e.g., they use the @code{lods}
6491: instruction). In addition, Win32Forth uses a comfortable, but costly
6492: method for relocating the Forth image: like @code{cforth}, it computes
6493: the actual addresses at run time, resulting in two address computations
6494: per NEXT (@pxref{Image File Background}).
6495:
6496: Only Eforth with the peephole optimizer performs comparable to
6497: Gforth. The speedups achieved with peephole optimization of threaded
6498: code are quite remarkable. Adding a peephole optimizer to Gforth should
6499: cause similar speedups.
6500:
6501: The speedup of Gforth over PFE, ThisForth and TILE can be easily
6502: explained with the self-imposed restriction of the latter systems to
6503: standard C, which makes efficient threading impossible (however, the
1.4 anton 6504: measured implementation of PFE uses a GNU C extension: @pxref{Global Reg
1.1 anton 6505: Vars, , Defining Global Register Variables, gcc.info, GNU C Manual}).
6506: Moreover, current C compilers have a hard time optimizing other aspects
6507: of the ThisForth and the TILE source.
6508:
6509: Note that the performance of Gforth on 386 architecture processors
6510: varies widely with the version of @code{gcc} used. E.g., @code{gcc-2.5.8}
6511: failed to allocate any of the virtual machine registers into real
6512: machine registers by itself and would not work correctly with explicit
6513: register declarations, giving a 1.3 times slower engine (on a 486DX2/66
6514: running the Sieve) than the one measured above.
6515:
6516: Note also that there have been several releases of Win32Forth since the
6517: release presented here, so the results presented here may have little
6518: predictive value for the performance of Win32Forth today.
6519:
6520: @cindex @file{Benchres}
6521: In @cite{Translating Forth to Efficient C} by M. Anton Ertl and Martin
6522: Maierhofer (presented at EuroForth '95), an indirect threaded version of
6523: Gforth is compared with Win32Forth, NT Forth, PFE, and ThisForth; that
6524: version of Gforth is 2%@minus{}8% slower on a 486 than the direct
6525: threaded version used here. The paper available at
6526: @*@url{http://www.complang.tuwien.ac.at/papers/ertl&maierhofer95.ps.gz};
6527: it also contains numbers for some native code systems. You can find a
6528: newer version of these measurements at
6529: @url{http://www.complang.tuwien.ac.at/forth/performance.html}. You can
6530: find numbers for Gforth on various machines in @file{Benchres}.
6531:
6532: @node Bugs, Origin, Engine, Top
6533: @chapter Bugs
6534: @cindex bug reporting
6535:
6536: Known bugs are described in the file BUGS in the Gforth distribution.
6537:
6538: If you find a bug, please send a bug report to
6539: @email{bug-gforth@@gnu.ai.mit.edu}. A bug report should
6540: describe the Gforth version used (it is announced at the start of an
6541: interactive Gforth session), the machine and operating system (on Unix
6542: systems you can use @code{uname -a} to produce this information), the
6543: installation options (send the @file{config.status} file), and a
6544: complete list of changes you (or your installer) have made to the Gforth
6545: sources (if any); it should contain a program (or a sequence of keyboard
6546: commands) that reproduces the bug and a description of what you think
6547: constitutes the buggy behaviour.
6548:
6549: For a thorough guide on reporting bugs read @ref{Bug Reporting, , How
6550: to Report Bugs, gcc.info, GNU C Manual}.
6551:
6552:
6553: @node Origin, Word Index, Bugs, Top
6554: @chapter Authors and Ancestors of Gforth
6555:
6556: @section Authors and Contributors
6557: @cindex authors of Gforth
6558: @cindex contributors to Gforth
6559:
6560: The Gforth project was started in mid-1992 by Bernd Paysan and Anton
6561: Ertl. The third major author was Jens Wilke. Lennart Benschop (who was
6562: one of Gforth's first users, in mid-1993) and Stuart Ramsden inspired us
6563: with their continuous feedback. Lennart Benshop contributed
6564: @file{glosgen.fs}, while Stuart Ramsden has been working on automatic
6565: support for calling C libraries. Helpful comments also came from Paul
6566: Kleinrubatscher, Christian Pirker, Dirk Zoller, Marcel Hendrix, John
6567: Wavrik, Barrie Stott and Marc de Groot.
6568:
6569: Gforth also owes a lot to the authors of the tools we used (GCC, CVS,
6570: and autoconf, among others), and to the creators of the Internet: Gforth
6571: was developed across the Internet, and its authors have not met
6572: physically yet.
6573:
6574: @section Pedigree
6575: @cindex Pedigree of Gforth
6576:
6577: Gforth descends from BigForth (1993) and fig-Forth. Gforth and PFE (by
6578: Dirk Zoller) will cross-fertilize each other. Of course, a significant
6579: part of the design of Gforth was prescribed by ANS Forth.
6580:
6581: Bernd Paysan wrote BigForth, a descendent from TurboForth, an unreleased
6582: 32 bit native code version of VolksForth for the Atari ST, written
6583: mostly by Dietrich Weineck.
6584:
6585: VolksForth descends from F83. It was written by Klaus Schleisiek, Bernd
6586: Pennemann, Georg Rehfeld and Dietrich Weineck for the C64 (called
6587: UltraForth there) in the mid-80s and ported to the Atari ST in 1986.
6588:
6589: Henry Laxen and Mike Perry wrote F83 as a model implementation of the
6590: Forth-83 standard. !! Pedigree? When?
6591:
6592: A team led by Bill Ragsdale implemented fig-Forth on many processors in
6593: 1979. Robert Selzer and Bill Ragsdale developed the original
6594: implementation of fig-Forth for the 6502 based on microForth.
6595:
6596: The principal architect of microForth was Dean Sanderson. microForth was
6597: FORTH, Inc.'s first off-the-shelf product. It was developed in 1976 for
6598: the 1802, and subsequently implemented on the 8080, the 6800 and the
6599: Z80.
6600:
6601: All earlier Forth systems were custom-made, usually by Charles Moore,
6602: who discovered (as he puts it) Forth during the late 60s. The first full
6603: Forth existed in 1971.
6604:
6605: A part of the information in this section comes from @cite{The Evolution
6606: of Forth} by Elizabeth D. Rather, Donald R. Colburn and Charles
6607: H. Moore, presented at the HOPL-II conference and preprinted in SIGPLAN
6608: Notices 28(3), 1993. You can find more historical and genealogical
6609: information about Forth there.
6610:
6611: @node Word Index, Concept Index, Origin, Top
6612: @unnumbered Word Index
6613:
6614: This index is as incomplete as the manual. Each word is listed with
6615: stack effect and wordset.
6616:
6617: @printindex fn
6618:
6619: @node Concept Index, , Word Index, Top
6620: @unnumbered Concept and Word Index
6621:
6622: This index is as incomplete as the manual. Not all entries listed are
6623: present verbatim in the text. Only the names are listed for the words
6624: here.
6625:
6626: @printindex cp
6627:
6628: @contents
6629: @bye
6630:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>