Annotation of gforth/gforth.ds, revision 1.35
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.)
1.4 anton 4: @setfilename gforth.info
1.17 anton 5: @settitle Gforth Manual
1.4 anton 6: @comment @setchapternewpage odd
1.1 anton 7: @comment %**end of header (This is for running Texinfo on a region.)
8:
9: @ifinfo
1.30 anton 10: This file documents Gforth 0.2
1.1 anton 11:
1.32 anton 12: Copyright @copyright{} 1995,1996 Free Software Foundation, Inc.
1.1 anton 13:
14: Permission is granted to make and distribute verbatim copies of
15: this manual provided the copyright notice and this permission notice
16: are preserved on all copies.
17:
1.4 anton 18: @ignore
1.1 anton 19: Permission is granted to process this file through TeX and print the
20: results, provided the printed document carries a copying permission
21: notice identical to this one except for the removal of this paragraph
22: (this paragraph not being relevant to the printed manual).
23:
1.4 anton 24: @end ignore
1.1 anton 25: Permission is granted to copy and distribute modified versions of this
26: manual under the conditions for verbatim copying, provided also that the
27: sections entitled "Distribution" and "General Public License" are
28: included exactly as in the original, and provided that the entire
29: resulting derived work is distributed under the terms of a permission
30: notice identical to this one.
31:
32: Permission is granted to copy and distribute translations of this manual
33: into another language, under the above conditions for modified versions,
34: except that the sections entitled "Distribution" and "General Public
35: License" may be included in a translation approved by the author instead
36: of in the original English.
37: @end ifinfo
38:
1.24 anton 39: @finalout
1.1 anton 40: @titlepage
41: @sp 10
1.17 anton 42: @center @titlefont{Gforth Manual}
1.1 anton 43: @sp 2
1.30 anton 44: @center for version 0.2
1.1 anton 45: @sp 2
46: @center Anton Ertl
1.25 anton 47: @center Bernd Paysan
1.17 anton 48: @sp 3
49: @center This manual is under construction
1.1 anton 50:
51: @comment The following two commands start the copyright page.
52: @page
53: @vskip 0pt plus 1filll
1.32 anton 54: Copyright @copyright{} 1995,1996 Free Software Foundation, Inc.
1.1 anton 55:
56: @comment !! Published by ... or You can get a copy of this manual ...
57:
58: Permission is granted to make and distribute verbatim copies of
59: this manual provided the copyright notice and this permission notice
60: are preserved on all copies.
61:
62: Permission is granted to copy and distribute modified versions of this
63: manual under the conditions for verbatim copying, provided also that the
64: sections entitled "Distribution" and "General Public License" are
65: included exactly as in the original, and provided that the entire
66: resulting derived work is distributed under the terms of a permission
67: notice identical to this one.
68:
69: Permission is granted to copy and distribute translations of this manual
70: into another language, under the above conditions for modified versions,
71: except that the sections entitled "Distribution" and "General Public
72: License" may be included in a translation approved by the author instead
73: of in the original English.
74: @end titlepage
75:
76:
77: @node Top, License, (dir), (dir)
78: @ifinfo
1.17 anton 79: Gforth is a free implementation of ANS Forth available on many
1.30 anton 80: personal machines. This manual corresponds to version 0.2.
1.1 anton 81: @end ifinfo
82:
83: @menu
1.4 anton 84: * License::
1.17 anton 85: * Goals:: About the Gforth Project
1.4 anton 86: * Other Books:: Things you might want to read
1.17 anton 87: * Invocation:: Starting Gforth
88: * Words:: Forth words available in Gforth
1.4 anton 89: * ANS conformance:: Implementation-defined options etc.
1.17 anton 90: * Model:: The abstract machine of Gforth
1.34 anton 91: * Integrating Gforth:: Forth as scripting language for applications.
1.17 anton 92: * Emacs and Gforth:: The Gforth Mode
1.4 anton 93: * Internals:: Implementation details
94: * Bugs:: How to report them
1.29 anton 95: * Origin:: Authors and ancestors of Gforth
1.4 anton 96: * Word Index:: An item for each Forth word
97: * Node Index:: An item for each node
1.1 anton 98: @end menu
99:
100: @node License, Goals, Top, Top
1.20 pazsan 101: @unnumbered GNU GENERAL PUBLIC LICENSE
102: @center Version 2, June 1991
103:
104: @display
105: Copyright @copyright{} 1989, 1991 Free Software Foundation, Inc.
106: 675 Mass Ave, Cambridge, MA 02139, USA
107:
108: Everyone is permitted to copy and distribute verbatim copies
109: of this license document, but changing it is not allowed.
110: @end display
111:
112: @unnumberedsec Preamble
113:
114: The licenses for most software are designed to take away your
115: freedom to share and change it. By contrast, the GNU General Public
116: License is intended to guarantee your freedom to share and change free
117: software---to make sure the software is free for all its users. This
118: General Public License applies to most of the Free Software
119: Foundation's software and to any other program whose authors commit to
120: using it. (Some other Free Software Foundation software is covered by
121: the GNU Library General Public License instead.) You can apply it to
122: your programs, too.
123:
124: When we speak of free software, we are referring to freedom, not
125: price. Our General Public Licenses are designed to make sure that you
126: have the freedom to distribute copies of free software (and charge for
127: this service if you wish), that you receive source code or can get it
128: if you want it, that you can change the software or use pieces of it
129: in new free programs; and that you know you can do these things.
130:
131: To protect your rights, we need to make restrictions that forbid
132: anyone to deny you these rights or to ask you to surrender the rights.
133: These restrictions translate to certain responsibilities for you if you
134: distribute copies of the software, or if you modify it.
135:
136: For example, if you distribute copies of such a program, whether
137: gratis or for a fee, you must give the recipients all the rights that
138: you have. You must make sure that they, too, receive or can get the
139: source code. And you must show them these terms so they know their
140: rights.
141:
142: We protect your rights with two steps: (1) copyright the software, and
143: (2) offer you this license which gives you legal permission to copy,
144: distribute and/or modify the software.
145:
146: Also, for each author's protection and ours, we want to make certain
147: that everyone understands that there is no warranty for this free
148: software. If the software is modified by someone else and passed on, we
149: want its recipients to know that what they have is not the original, so
150: that any problems introduced by others will not reflect on the original
151: authors' reputations.
152:
153: Finally, any free program is threatened constantly by software
154: patents. We wish to avoid the danger that redistributors of a free
155: program will individually obtain patent licenses, in effect making the
156: program proprietary. To prevent this, we have made it clear that any
157: patent must be licensed for everyone's free use or not licensed at all.
158:
159: The precise terms and conditions for copying, distribution and
160: modification follow.
161:
162: @iftex
163: @unnumberedsec TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
164: @end iftex
165: @ifinfo
166: @center TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
167: @end ifinfo
168:
169: @enumerate 0
170: @item
171: This License applies to any program or other work which contains
172: a notice placed by the copyright holder saying it may be distributed
173: under the terms of this General Public License. The ``Program'', below,
174: refers to any such program or work, and a ``work based on the Program''
175: means either the Program or any derivative work under copyright law:
176: that is to say, a work containing the Program or a portion of it,
177: either verbatim or with modifications and/or translated into another
178: language. (Hereinafter, translation is included without limitation in
179: the term ``modification''.) Each licensee is addressed as ``you''.
180:
181: Activities other than copying, distribution and modification are not
182: covered by this License; they are outside its scope. The act of
183: running the Program is not restricted, and the output from the Program
184: is covered only if its contents constitute a work based on the
185: Program (independent of having been made by running the Program).
186: Whether that is true depends on what the Program does.
187:
188: @item
189: You may copy and distribute verbatim copies of the Program's
190: source code as you receive it, in any medium, provided that you
191: conspicuously and appropriately publish on each copy an appropriate
192: copyright notice and disclaimer of warranty; keep intact all the
193: notices that refer to this License and to the absence of any warranty;
194: and give any other recipients of the Program a copy of this License
195: along with the Program.
196:
197: You may charge a fee for the physical act of transferring a copy, and
198: you may at your option offer warranty protection in exchange for a fee.
199:
200: @item
201: You may modify your copy or copies of the Program or any portion
202: of it, thus forming a work based on the Program, and copy and
203: distribute such modifications or work under the terms of Section 1
204: above, provided that you also meet all of these conditions:
205:
206: @enumerate a
207: @item
208: You must cause the modified files to carry prominent notices
209: stating that you changed the files and the date of any change.
210:
211: @item
212: You must cause any work that you distribute or publish, that in
213: whole or in part contains or is derived from the Program or any
214: part thereof, to be licensed as a whole at no charge to all third
215: parties under the terms of this License.
216:
217: @item
218: If the modified program normally reads commands interactively
219: when run, you must cause it, when started running for such
220: interactive use in the most ordinary way, to print or display an
221: announcement including an appropriate copyright notice and a
222: notice that there is no warranty (or else, saying that you provide
223: a warranty) and that users may redistribute the program under
224: these conditions, and telling the user how to view a copy of this
225: License. (Exception: if the Program itself is interactive but
226: does not normally print such an announcement, your work based on
227: the Program is not required to print an announcement.)
228: @end enumerate
229:
230: These requirements apply to the modified work as a whole. If
231: identifiable sections of that work are not derived from the Program,
232: and can be reasonably considered independent and separate works in
233: themselves, then this License, and its terms, do not apply to those
234: sections when you distribute them as separate works. But when you
235: distribute the same sections as part of a whole which is a work based
236: on the Program, the distribution of the whole must be on the terms of
237: this License, whose permissions for other licensees extend to the
238: entire whole, and thus to each and every part regardless of who wrote it.
239:
240: Thus, it is not the intent of this section to claim rights or contest
241: your rights to work written entirely by you; rather, the intent is to
242: exercise the right to control the distribution of derivative or
243: collective works based on the Program.
244:
245: In addition, mere aggregation of another work not based on the Program
246: with the Program (or with a work based on the Program) on a volume of
247: a storage or distribution medium does not bring the other work under
248: the scope of this License.
249:
250: @item
251: You may copy and distribute the Program (or a work based on it,
252: under Section 2) in object code or executable form under the terms of
253: Sections 1 and 2 above provided that you also do one of the following:
254:
255: @enumerate a
256: @item
257: Accompany it with the complete corresponding machine-readable
258: source code, which must be distributed under the terms of Sections
259: 1 and 2 above on a medium customarily used for software interchange; or,
260:
261: @item
262: Accompany it with a written offer, valid for at least three
263: years, to give any third party, for a charge no more than your
264: cost of physically performing source distribution, a complete
265: machine-readable copy of the corresponding source code, to be
266: distributed under the terms of Sections 1 and 2 above on a medium
267: customarily used for software interchange; or,
268:
269: @item
270: Accompany it with the information you received as to the offer
271: to distribute corresponding source code. (This alternative is
272: allowed only for noncommercial distribution and only if you
273: received the program in object code or executable form with such
274: an offer, in accord with Subsection b above.)
275: @end enumerate
276:
277: The source code for a work means the preferred form of the work for
278: making modifications to it. For an executable work, complete source
279: code means all the source code for all modules it contains, plus any
280: associated interface definition files, plus the scripts used to
281: control compilation and installation of the executable. However, as a
282: special exception, the source code distributed need not include
283: anything that is normally distributed (in either source or binary
284: form) with the major components (compiler, kernel, and so on) of the
285: operating system on which the executable runs, unless that component
286: itself accompanies the executable.
287:
288: If distribution of executable or object code is made by offering
289: access to copy from a designated place, then offering equivalent
290: access to copy the source code from the same place counts as
291: distribution of the source code, even though third parties are not
292: compelled to copy the source along with the object code.
293:
294: @item
295: You may not copy, modify, sublicense, or distribute the Program
296: except as expressly provided under this License. Any attempt
297: otherwise to copy, modify, sublicense or distribute the Program is
298: void, and will automatically terminate your rights under this License.
299: However, parties who have received copies, or rights, from you under
300: this License will not have their licenses terminated so long as such
301: parties remain in full compliance.
302:
303: @item
304: You are not required to accept this License, since you have not
305: signed it. However, nothing else grants you permission to modify or
306: distribute the Program or its derivative works. These actions are
307: prohibited by law if you do not accept this License. Therefore, by
308: modifying or distributing the Program (or any work based on the
309: Program), you indicate your acceptance of this License to do so, and
310: all its terms and conditions for copying, distributing or modifying
311: the Program or works based on it.
312:
313: @item
314: Each time you redistribute the Program (or any work based on the
315: Program), the recipient automatically receives a license from the
316: original licensor to copy, distribute or modify the Program subject to
317: these terms and conditions. You may not impose any further
318: restrictions on the recipients' exercise of the rights granted herein.
319: You are not responsible for enforcing compliance by third parties to
320: this License.
321:
322: @item
323: If, as a consequence of a court judgment or allegation of patent
324: infringement or for any other reason (not limited to patent issues),
325: conditions are imposed on you (whether by court order, agreement or
326: otherwise) that contradict the conditions of this License, they do not
327: excuse you from the conditions of this License. If you cannot
328: distribute so as to satisfy simultaneously your obligations under this
329: License and any other pertinent obligations, then as a consequence you
330: may not distribute the Program at all. For example, if a patent
331: license would not permit royalty-free redistribution of the Program by
332: all those who receive copies directly or indirectly through you, then
333: the only way you could satisfy both it and this License would be to
334: refrain entirely from distribution of the Program.
335:
336: If any portion of this section is held invalid or unenforceable under
337: any particular circumstance, the balance of the section is intended to
338: apply and the section as a whole is intended to apply in other
339: circumstances.
340:
341: It is not the purpose of this section to induce you to infringe any
342: patents or other property right claims or to contest validity of any
343: such claims; this section has the sole purpose of protecting the
344: integrity of the free software distribution system, which is
345: implemented by public license practices. Many people have made
346: generous contributions to the wide range of software distributed
347: through that system in reliance on consistent application of that
348: system; it is up to the author/donor to decide if he or she is willing
349: to distribute software through any other system and a licensee cannot
350: impose that choice.
351:
352: This section is intended to make thoroughly clear what is believed to
353: be a consequence of the rest of this License.
354:
355: @item
356: If the distribution and/or use of the Program is restricted in
357: certain countries either by patents or by copyrighted interfaces, the
358: original copyright holder who places the Program under this License
359: may add an explicit geographical distribution limitation excluding
360: those countries, so that distribution is permitted only in or among
361: countries not thus excluded. In such case, this License incorporates
362: the limitation as if written in the body of this License.
363:
364: @item
365: The Free Software Foundation may publish revised and/or new versions
366: of the General Public License from time to time. Such new versions will
367: be similar in spirit to the present version, but may differ in detail to
368: address new problems or concerns.
369:
370: Each version is given a distinguishing version number. If the Program
371: specifies a version number of this License which applies to it and ``any
372: later version'', you have the option of following the terms and conditions
373: either of that version or of any later version published by the Free
374: Software Foundation. If the Program does not specify a version number of
375: this License, you may choose any version ever published by the Free Software
376: Foundation.
377:
378: @item
379: If you wish to incorporate parts of the Program into other free
380: programs whose distribution conditions are different, write to the author
381: to ask for permission. For software which is copyrighted by the Free
382: Software Foundation, write to the Free Software Foundation; we sometimes
383: make exceptions for this. Our decision will be guided by the two goals
384: of preserving the free status of all derivatives of our free software and
385: of promoting the sharing and reuse of software generally.
386:
387: @iftex
388: @heading NO WARRANTY
389: @end iftex
390: @ifinfo
391: @center NO WARRANTY
392: @end ifinfo
393:
394: @item
395: BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
396: FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
397: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
398: PROVIDE THE PROGRAM ``AS IS'' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
399: OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
400: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
401: TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
402: PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
403: REPAIR OR CORRECTION.
404:
405: @item
406: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
407: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
408: REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
409: INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
410: OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
411: TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
412: YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
413: PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
414: POSSIBILITY OF SUCH DAMAGES.
415: @end enumerate
416:
417: @iftex
418: @heading END OF TERMS AND CONDITIONS
419: @end iftex
420: @ifinfo
421: @center END OF TERMS AND CONDITIONS
422: @end ifinfo
423:
424: @page
425: @unnumberedsec How to Apply These Terms to Your New Programs
426:
427: If you develop a new program, and you want it to be of the greatest
428: possible use to the public, the best way to achieve this is to make it
429: free software which everyone can redistribute and change under these terms.
430:
431: To do so, attach the following notices to the program. It is safest
432: to attach them to the start of each source file to most effectively
433: convey the exclusion of warranty; and each file should have at least
434: the ``copyright'' line and a pointer to where the full notice is found.
435:
436: @smallexample
437: @var{one line to give the program's name and a brief idea of what it does.}
438: Copyright (C) 19@var{yy} @var{name of author}
439:
440: This program is free software; you can redistribute it and/or modify
441: it under the terms of the GNU General Public License as published by
442: the Free Software Foundation; either version 2 of the License, or
443: (at your option) any later version.
444:
445: This program is distributed in the hope that it will be useful,
446: but WITHOUT ANY WARRANTY; without even the implied warranty of
447: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
448: GNU General Public License for more details.
449:
450: You should have received a copy of the GNU General Public License
451: along with this program; if not, write to the Free Software
452: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
453: @end smallexample
454:
455: Also add information on how to contact you by electronic and paper mail.
456:
457: If the program is interactive, make it output a short notice like this
458: when it starts in an interactive mode:
459:
460: @smallexample
461: Gnomovision version 69, Copyright (C) 19@var{yy} @var{name of author}
462: Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
463: type `show w'.
464: This is free software, and you are welcome to redistribute it
465: under certain conditions; type `show c' for details.
466: @end smallexample
467:
468: The hypothetical commands @samp{show w} and @samp{show c} should show
469: the appropriate parts of the General Public License. Of course, the
470: commands you use may be called something other than @samp{show w} and
471: @samp{show c}; they could even be mouse-clicks or menu items---whatever
472: suits your program.
473:
474: You should also get your employer (if you work as a programmer) or your
475: school, if any, to sign a ``copyright disclaimer'' for the program, if
476: necessary. Here is a sample; alter the names:
477:
478: @smallexample
479: Yoyodyne, Inc., hereby disclaims all copyright interest in the program
480: `Gnomovision' (which makes passes at compilers) written by James Hacker.
481:
482: @var{signature of Ty Coon}, 1 April 1989
483: Ty Coon, President of Vice
484: @end smallexample
485:
486: This General Public License does not permit incorporating your program into
487: proprietary programs. If your program is a subroutine library, you may
488: consider it more useful to permit linking proprietary applications with the
489: library. If this is what you want to do, use the GNU Library General
490: Public License instead of this License.
1.1 anton 491:
492: @iftex
1.23 pazsan 493: @node Preface
494: @comment node-name, next, previous, up
1.1 anton 495: @unnumbered Preface
1.23 pazsan 496: @cindex Preface
1.17 anton 497: This manual documents Gforth. The reader is expected to know
1.1 anton 498: Forth. This manual is primarily a reference manual. @xref{Other Books}
499: for introductory material.
500: @end iftex
501:
502: @node Goals, Other Books, License, Top
503: @comment node-name, next, previous, up
1.17 anton 504: @chapter Goals of Gforth
1.1 anton 505: @cindex Goals
1.17 anton 506: The goal of the Gforth Project is to develop a standard model for
1.1 anton 507: ANSI Forth. This can be split into several subgoals:
508:
509: @itemize @bullet
510: @item
1.17 anton 511: Gforth should conform to the ANSI Forth standard.
1.1 anton 512: @item
513: It should be a model, i.e. it should define all the
514: implementation-dependent things.
515: @item
516: It should become standard, i.e. widely accepted and used. This goal
517: is the most difficult one.
518: @end itemize
519:
1.17 anton 520: To achieve these goals Gforth should be
1.1 anton 521: @itemize @bullet
522: @item
523: Similar to previous models (fig-Forth, F83)
524: @item
525: Powerful. It should provide for all the things that are considered
526: necessary today and even some that are not yet considered necessary.
527: @item
528: Efficient. It should not get the reputation of being exceptionally
529: slow.
530: @item
531: Free.
532: @item
533: Available on many machines/easy to port.
534: @end itemize
535:
1.17 anton 536: Have we achieved these goals? Gforth conforms to the ANS Forth
537: standard. It may be considered a model, but we have not yet documented
1.1 anton 538: which parts of the model are stable and which parts we are likely to
1.17 anton 539: change. It certainly has not yet become a de facto standard. It has some
540: similarities and some differences to previous models. It has some
541: powerful features, but not yet everything that we envisioned. We
542: certainly have achieved our execution speed goals (@pxref{Performance}).
543: It is free and available on many machines.
1.1 anton 544:
545: @node Other Books, Invocation, Goals, Top
546: @chapter Other books on ANS Forth
547:
548: As the standard is relatively new, there are not many books out yet. It
1.17 anton 549: is not recommended to learn Forth by using Gforth and a book that is
1.1 anton 550: not written for ANS Forth, as you will not know your mistakes from the
551: deviations of the book.
552:
553: There is, of course, the standard, the definite reference if you want to
1.19 anton 554: write ANS Forth programs. It is available in printed form from the
555: National Standards Institute Sales Department (Tel.: USA (212) 642-4900;
556: Fax.: USA (212) 302-1286) as document @cite{X3.215-1994} for about $200. You
557: can also get it from Global Engineering Documents (Tel.: USA (800)
558: 854-7179; Fax.: (303) 843-9880) for about $300.
559:
560: @cite{dpANS6}, the last draft of the standard, which was then submitted to ANSI
561: for publication is available electronically and for free in some MS Word
562: format, and it has been converted to HTML. Some pointers to these
563: versions can be found through
1.24 anton 564: @*@file{http://www.complang.tuwien.ac.at/projects/forth.html}.
1.1 anton 565:
1.21 anton 566: @cite{Forth: The new model} by Jack Woehr (Prentice-Hall, 1993) is an
1.1 anton 567: introductory book based on a draft version of the standard. It does not
568: cover the whole standard. It also contains interesting background
569: information (Jack Woehr was in the ANS Forth Technical Committe). It is
570: not appropriate for complete newbies, but programmers experienced in
571: other languages should find it ok.
572:
573: @node Invocation, Words, Other Books, Top
574: @chapter Invocation
575:
576: You will usually just say @code{gforth}. In many other cases the default
1.17 anton 577: Gforth image will be invoked like this:
1.1 anton 578:
579: @example
580: gforth [files] [-e forth-code]
581: @end example
582:
583: executing the contents of the files and the Forth code in the order they
584: are given.
585:
586: In general, the command line looks like this:
587:
588: @example
589: gforth [initialization options] [image-specific options]
590: @end example
591:
592: The initialization options must come before the rest of the command
593: line. They are:
594:
595: @table @code
596: @item --image-file @var{file}
1.20 pazsan 597: @item -i @var{file}
1.1 anton 598: Loads the Forth image @var{file} instead of the default
599: @file{gforth.fi}.
600:
601: @item --path @var{path}
1.20 pazsan 602: @item -p @var{path}
1.1 anton 603: Uses @var{path} for searching the image file and Forth source code
604: files instead of the default in the environment variable
605: @code{GFORTHPATH} or the path specified at installation time (typically
606: @file{/usr/local/lib/gforth:.}). A path is given as a @code{:}-separated
607: list.
608:
609: @item --dictionary-size @var{size}
610: @item -m @var{size}
611: Allocate @var{size} space for the Forth dictionary space instead of
612: using the default specified in the image (typically 256K). The
613: @var{size} specification consists of an integer and a unit (e.g.,
614: @code{4M}). The unit can be one of @code{b} (bytes), @code{e} (element
615: size, in this case Cells), @code{k} (kilobytes), and @code{M}
616: (Megabytes). If no unit is specified, @code{e} is used.
617:
618: @item --data-stack-size @var{size}
619: @item -d @var{size}
620: Allocate @var{size} space for the data stack instead of using the
621: default specified in the image (typically 16K).
622:
623: @item --return-stack-size @var{size}
624: @item -r @var{size}
625: Allocate @var{size} space for the return stack instead of using the
626: default specified in the image (typically 16K).
627:
628: @item --fp-stack-size @var{size}
629: @item -f @var{size}
630: Allocate @var{size} space for the floating point stack instead of
631: using the default specified in the image (typically 16K). In this case
632: the unit specifier @code{e} refers to floating point numbers.
633:
634: @item --locals-stack-size @var{size}
635: @item -l @var{size}
636: Allocate @var{size} space for the locals stack instead of using the
637: default specified in the image (typically 16K).
638:
639: @end table
640:
641: As explained above, the image-specific command-line arguments for the
642: default image @file{gforth.fi} consist of a sequence of filenames and
643: @code{-e @var{forth-code}} options that are interpreted in the seqence
644: in which they are given. The @code{-e @var{forth-code}} or
645: @code{--evaluate @var{forth-code}} option evaluates the forth
646: code. This option takes only one argument; if you want to evaluate more
647: Forth words, you have to quote them or use several @code{-e}s. To exit
648: after processing the command line (instead of entering interactive mode)
649: append @code{-e bye} to the command line.
650:
1.22 anton 651: If you have several versions of Gforth installed, @code{gforth} will
652: invoke the version that was installed last. @code{gforth-@var{version}}
653: invokes a specific version. You may want to use the option
654: @code{--path}, if your environment contains the variable
655: @code{GFORTHPATH}.
656:
1.1 anton 657: Not yet implemented:
658: On startup the system first executes the system initialization file
659: (unless the option @code{--no-init-file} is given; note that the system
660: resulting from using this option may not be ANS Forth conformant). Then
661: the user initialization file @file{.gforth.fs} is executed, unless the
662: option @code{--no-rc} is given; this file is first searched in @file{.},
663: then in @file{~}, then in the normal path (see above).
664:
1.4 anton 665: @node Words, ANS conformance, Invocation, Top
1.1 anton 666: @chapter Forth Words
667:
668: @menu
1.4 anton 669: * Notation::
670: * Arithmetic::
671: * Stack Manipulation::
672: * Memory access::
673: * Control Structures::
674: * Locals::
675: * Defining Words::
676: * Wordlists::
677: * Files::
678: * Blocks::
679: * Other I/O::
680: * Programming Tools::
1.18 anton 681: * Assembler and Code words::
1.4 anton 682: * Threading Words::
1.1 anton 683: @end menu
684:
685: @node Notation, Arithmetic, Words, Words
686: @section Notation
687:
688: The Forth words are described in this section in the glossary notation
689: that has become a de-facto standard for Forth texts, i.e.
690:
1.4 anton 691: @format
1.1 anton 692: @var{word} @var{Stack effect} @var{wordset} @var{pronunciation}
1.4 anton 693: @end format
1.1 anton 694: @var{Description}
695:
696: @table @var
697: @item word
1.17 anton 698: The name of the word. BTW, Gforth is case insensitive, so you can
1.14 anton 699: type the words in in lower case (However, @pxref{core-idef}).
1.1 anton 700:
701: @item Stack effect
702: The stack effect is written in the notation @code{@var{before} --
703: @var{after}}, where @var{before} and @var{after} describe the top of
704: stack entries before and after the execution of the word. The rest of
705: the stack is not touched by the word. The top of stack is rightmost,
1.17 anton 706: i.e., a stack sequence is written as it is typed in. Note that Gforth
1.1 anton 707: uses a separate floating point stack, but a unified stack
708: notation. Also, return stack effects are not shown in @var{stack
709: effect}, but in @var{Description}. The name of a stack item describes
710: the type and/or the function of the item. See below for a discussion of
711: the types.
712:
1.19 anton 713: All words have two stack effects: A compile-time stack effect and a
714: run-time stack effect. The compile-time stack-effect of most words is
715: @var{ -- }. If the compile-time stack-effect of a word deviates from
716: this standard behaviour, or the word does other unusual things at
717: compile time, both stack effects are shown; otherwise only the run-time
718: stack effect is shown.
719:
1.1 anton 720: @item pronunciation
721: How the word is pronounced
722:
723: @item wordset
724: The ANS Forth standard is divided into several wordsets. A standard
725: system need not support all of them. So, the fewer wordsets your program
726: uses the more portable it will be in theory. However, we suspect that
727: most ANS Forth systems on personal machines will feature all
728: wordsets. Words that are not defined in the ANS standard have
1.19 anton 729: @code{gforth} or @code{gforth-internal} as wordset. @code{gforth}
730: describes words that will work in future releases of Gforth;
731: @code{gforth-internal} words are more volatile. Environmental query
732: strings are also displayed like words; you can recognize them by the
733: @code{environment} in the wordset field.
1.1 anton 734:
735: @item Description
736: A description of the behaviour of the word.
737: @end table
738:
1.4 anton 739: The type of a stack item is specified by the character(s) the name
740: starts with:
1.1 anton 741:
742: @table @code
743: @item f
744: Bool, i.e. @code{false} or @code{true}.
745: @item c
746: Char
747: @item w
748: Cell, can contain an integer or an address
749: @item n
750: signed integer
751: @item u
752: unsigned integer
753: @item d
754: double sized signed integer
755: @item ud
756: double sized unsigned integer
757: @item r
758: Float
759: @item a_
760: Cell-aligned address
761: @item c_
762: Char-aligned address (note that a Char is two bytes in Windows NT)
763: @item f_
764: Float-aligned address
765: @item df_
766: Address aligned for IEEE double precision float
767: @item sf_
768: Address aligned for IEEE single precision float
769: @item xt
770: Execution token, same size as Cell
771: @item wid
772: Wordlist ID, same size as Cell
773: @item f83name
774: Pointer to a name structure
775: @end table
776:
1.4 anton 777: @node Arithmetic, Stack Manipulation, Notation, Words
1.1 anton 778: @section Arithmetic
779: Forth arithmetic is not checked, i.e., you will not hear about integer
780: overflow on addition or multiplication, you may hear about division by
781: zero if you are lucky. The operator is written after the operands, but
782: the operands are still in the original order. I.e., the infix @code{2-1}
783: corresponds to @code{2 1 -}. Forth offers a variety of division
784: operators. If you perform division with potentially negative operands,
785: you do not want to use @code{/} or @code{/mod} with its undefined
786: behaviour, but rather @code{fm/mod} or @code{sm/mod} (probably the
1.4 anton 787: former, @pxref{Mixed precision}).
788:
789: @menu
790: * Single precision::
791: * Bitwise operations::
792: * Mixed precision:: operations with single and double-cell integers
793: * Double precision:: Double-cell integer arithmetic
794: * Floating Point::
795: @end menu
1.1 anton 796:
1.4 anton 797: @node Single precision, Bitwise operations, Arithmetic, Arithmetic
1.1 anton 798: @subsection Single precision
799: doc-+
800: doc--
801: doc-*
802: doc-/
803: doc-mod
804: doc-/mod
805: doc-negate
806: doc-abs
807: doc-min
808: doc-max
809:
1.4 anton 810: @node Bitwise operations, Mixed precision, Single precision, Arithmetic
1.1 anton 811: @subsection Bitwise operations
812: doc-and
813: doc-or
814: doc-xor
815: doc-invert
816: doc-2*
817: doc-2/
818:
1.4 anton 819: @node Mixed precision, Double precision, Bitwise operations, Arithmetic
1.1 anton 820: @subsection Mixed precision
821: doc-m+
822: doc-*/
823: doc-*/mod
824: doc-m*
825: doc-um*
826: doc-m*/
827: doc-um/mod
828: doc-fm/mod
829: doc-sm/rem
830:
1.4 anton 831: @node Double precision, Floating Point, Mixed precision, Arithmetic
1.1 anton 832: @subsection Double precision
1.16 anton 833:
834: The outer (aka text) interpreter converts numbers containing a dot into
835: a double precision number. Note that only numbers with the dot as last
836: character are standard-conforming.
837:
1.1 anton 838: doc-d+
839: doc-d-
840: doc-dnegate
841: doc-dabs
842: doc-dmin
843: doc-dmax
844:
1.4 anton 845: @node Floating Point, , Double precision, Arithmetic
846: @subsection Floating Point
1.16 anton 847:
848: The format of floating point numbers recognized by the outer (aka text)
849: interpreter is: a signed decimal number, possibly containing a decimal
850: point (@code{.}), followed by @code{E} or @code{e}, optionally followed
851: by a signed integer (the exponent). E.g., @code{1e} ist the same as
1.35 ! anton 852: @code{+1.0e+0}. Note that a number without @code{e}
1.16 anton 853: is not interpreted as floating-point number, but as double (if the
854: number contains a @code{.}) or single precision integer. Also,
855: conversions between string and floating point numbers always use base
856: 10, irrespective of the value of @code{BASE}. If @code{BASE} contains a
857: value greater then 14, the @code{E} may be interpreted as digit and the
858: number will be interpreted as integer, unless it has a signed exponent
859: (both @code{+} and @code{-} are allowed as signs).
1.4 anton 860:
861: Angles in floating point operations are given in radians (a full circle
1.17 anton 862: has 2 pi radians). Note, that Gforth has a separate floating point
1.4 anton 863: stack, but we use the unified notation.
864:
865: Floating point numbers have a number of unpleasant surprises for the
866: unwary (e.g., floating point addition is not associative) and even a few
867: for the wary. You should not use them unless you know what you are doing
868: or you don't care that the results you get are totally bogus. If you
869: want to learn about the problems of floating point numbers (and how to
1.11 anton 870: avoid them), you might start with @cite{David Goldberg, What Every
1.6 anton 871: Computer Scientist Should Know About Floating-Point Arithmetic, ACM
872: Computing Surveys 23(1):5@minus{}48, March 1991}.
1.4 anton 873:
874: doc-f+
875: doc-f-
876: doc-f*
877: doc-f/
878: doc-fnegate
879: doc-fabs
880: doc-fmax
881: doc-fmin
882: doc-floor
883: doc-fround
884: doc-f**
885: doc-fsqrt
886: doc-fexp
887: doc-fexpm1
888: doc-fln
889: doc-flnp1
890: doc-flog
1.6 anton 891: doc-falog
1.4 anton 892: doc-fsin
893: doc-fcos
894: doc-fsincos
895: doc-ftan
896: doc-fasin
897: doc-facos
898: doc-fatan
899: doc-fatan2
900: doc-fsinh
901: doc-fcosh
902: doc-ftanh
903: doc-fasinh
904: doc-facosh
905: doc-fatanh
906:
907: @node Stack Manipulation, Memory access, Arithmetic, Words
1.1 anton 908: @section Stack Manipulation
909:
1.17 anton 910: Gforth has a data stack (aka parameter stack) for characters, cells,
1.1 anton 911: addresses, and double cells, a floating point stack for floating point
912: numbers, a return stack for storing the return addresses of colon
913: definitions and other data, and a locals stack for storing local
914: variables. Note that while every sane Forth has a separate floating
915: point stack, this is not strictly required; an ANS Forth system could
916: theoretically keep floating point numbers on the data stack. As an
917: additional difficulty, you don't know how many cells a floating point
918: number takes. It is reportedly possible to write words in a way that
919: they work also for a unified stack model, but we do not recommend trying
1.4 anton 920: it. Instead, just say that your program has an environmental dependency
921: on a separate FP stack.
922:
923: Also, a Forth system is allowed to keep the local variables on the
1.1 anton 924: return stack. This is reasonable, as local variables usually eliminate
925: the need to use the return stack explicitly. So, if you want to produce
926: a standard complying program and if you are using local variables in a
927: word, forget about return stack manipulations in that word (see the
928: standard document for the exact rules).
929:
1.4 anton 930: @menu
931: * Data stack::
932: * Floating point stack::
933: * Return stack::
934: * Locals stack::
935: * Stack pointer manipulation::
936: @end menu
937:
938: @node Data stack, Floating point stack, Stack Manipulation, Stack Manipulation
1.1 anton 939: @subsection Data stack
940: doc-drop
941: doc-nip
942: doc-dup
943: doc-over
944: doc-tuck
945: doc-swap
946: doc-rot
947: doc--rot
948: doc-?dup
949: doc-pick
950: doc-roll
951: doc-2drop
952: doc-2nip
953: doc-2dup
954: doc-2over
955: doc-2tuck
956: doc-2swap
957: doc-2rot
958:
1.4 anton 959: @node Floating point stack, Return stack, Data stack, Stack Manipulation
1.1 anton 960: @subsection Floating point stack
961: doc-fdrop
962: doc-fnip
963: doc-fdup
964: doc-fover
965: doc-ftuck
966: doc-fswap
967: doc-frot
968:
1.4 anton 969: @node Return stack, Locals stack, Floating point stack, Stack Manipulation
1.1 anton 970: @subsection Return stack
971: doc->r
972: doc-r>
973: doc-r@
974: doc-rdrop
975: doc-2>r
976: doc-2r>
977: doc-2r@
978: doc-2rdrop
979:
1.4 anton 980: @node Locals stack, Stack pointer manipulation, Return stack, Stack Manipulation
1.1 anton 981: @subsection Locals stack
982:
1.4 anton 983: @node Stack pointer manipulation, , Locals stack, Stack Manipulation
1.1 anton 984: @subsection Stack pointer manipulation
985: doc-sp@
986: doc-sp!
987: doc-fp@
988: doc-fp!
989: doc-rp@
990: doc-rp!
991: doc-lp@
992: doc-lp!
993:
1.4 anton 994: @node Memory access, Control Structures, Stack Manipulation, Words
1.1 anton 995: @section Memory access
996:
1.4 anton 997: @menu
998: * Stack-Memory transfers::
999: * Address arithmetic::
1000: * Memory block access::
1001: @end menu
1002:
1003: @node Stack-Memory transfers, Address arithmetic, Memory access, Memory access
1.1 anton 1004: @subsection Stack-Memory transfers
1005:
1006: doc-@
1007: doc-!
1008: doc-+!
1009: doc-c@
1010: doc-c!
1011: doc-2@
1012: doc-2!
1013: doc-f@
1014: doc-f!
1015: doc-sf@
1016: doc-sf!
1017: doc-df@
1018: doc-df!
1019:
1.4 anton 1020: @node Address arithmetic, Memory block access, Stack-Memory transfers, Memory access
1.1 anton 1021: @subsection Address arithmetic
1022:
1023: ANS Forth does not specify the sizes of the data types. Instead, it
1024: offers a number of words for computing sizes and doing address
1025: arithmetic. Basically, address arithmetic is performed in terms of
1026: address units (aus); on most systems the address unit is one byte. Note
1027: that a character may have more than one au, so @code{chars} is no noop
1028: (on systems where it is a noop, it compiles to nothing).
1029:
1030: ANS Forth also defines words for aligning addresses for specific
1031: addresses. Many computers require that accesses to specific data types
1032: must only occur at specific addresses; e.g., that cells may only be
1033: accessed at addresses divisible by 4. Even if a machine allows unaligned
1034: accesses, it can usually perform aligned accesses faster.
1035:
1.17 anton 1036: For the performance-conscious: alignment operations are usually only
1.1 anton 1037: necessary during the definition of a data structure, not during the
1038: (more frequent) accesses to it.
1039:
1040: ANS Forth defines no words for character-aligning addresses. This is not
1041: an oversight, but reflects the fact that addresses that are not
1042: char-aligned have no use in the standard and therefore will not be
1043: created.
1044:
1045: The standard guarantees that addresses returned by @code{CREATE}d words
1.17 anton 1046: are cell-aligned; in addition, Gforth guarantees that these addresses
1.1 anton 1047: are aligned for all purposes.
1048:
1.9 anton 1049: Note that the standard defines a word @code{char}, which has nothing to
1050: do with address arithmetic.
1051:
1.1 anton 1052: doc-chars
1053: doc-char+
1054: doc-cells
1055: doc-cell+
1056: doc-align
1057: doc-aligned
1058: doc-floats
1059: doc-float+
1060: doc-falign
1061: doc-faligned
1062: doc-sfloats
1063: doc-sfloat+
1064: doc-sfalign
1065: doc-sfaligned
1066: doc-dfloats
1067: doc-dfloat+
1068: doc-dfalign
1069: doc-dfaligned
1.10 anton 1070: doc-maxalign
1071: doc-maxaligned
1072: doc-cfalign
1073: doc-cfaligned
1.1 anton 1074: doc-address-unit-bits
1075:
1.4 anton 1076: @node Memory block access, , Address arithmetic, Memory access
1.1 anton 1077: @subsection Memory block access
1078:
1079: doc-move
1080: doc-erase
1081:
1082: While the previous words work on address units, the rest works on
1083: characters.
1084:
1085: doc-cmove
1086: doc-cmove>
1087: doc-fill
1088: doc-blank
1089:
1.4 anton 1090: @node Control Structures, Locals, Memory access, Words
1.1 anton 1091: @section Control Structures
1092:
1093: Control structures in Forth cannot be used in interpret state, only in
1094: compile state, i.e., in a colon definition. We do not like this
1095: limitation, but have not seen a satisfying way around it yet, although
1096: many schemes have been proposed.
1097:
1.4 anton 1098: @menu
1099: * Selection::
1100: * Simple Loops::
1101: * Counted Loops::
1102: * Arbitrary control structures::
1103: * Calls and returns::
1104: * Exception Handling::
1105: @end menu
1106:
1107: @node Selection, Simple Loops, Control Structures, Control Structures
1.1 anton 1108: @subsection Selection
1109:
1110: @example
1111: @var{flag}
1112: IF
1113: @var{code}
1114: ENDIF
1115: @end example
1116: or
1117: @example
1118: @var{flag}
1119: IF
1120: @var{code1}
1121: ELSE
1122: @var{code2}
1123: ENDIF
1124: @end example
1125:
1.4 anton 1126: You can use @code{THEN} instead of @code{ENDIF}. Indeed, @code{THEN} is
1.1 anton 1127: standard, and @code{ENDIF} is not, although it is quite popular. We
1128: recommend using @code{ENDIF}, because it is less confusing for people
1129: who also know other languages (and is not prone to reinforcing negative
1130: prejudices against Forth in these people). Adding @code{ENDIF} to a
1131: system that only supplies @code{THEN} is simple:
1132: @example
1133: : endif POSTPONE then ; immediate
1134: @end example
1135:
1136: [According to @cite{Webster's New Encyclopedic Dictionary}, @dfn{then
1137: (adv.)} has the following meanings:
1138: @quotation
1139: ... 2b: following next after in order ... 3d: as a necessary consequence
1140: (if you were there, then you saw them).
1141: @end quotation
1142: Forth's @code{THEN} has the meaning 2b, whereas @code{THEN} in Pascal
1143: and many other programming languages has the meaning 3d.]
1144:
1.31 anton 1145: Gforth also provides the words @code{?dup-if} and @code{?dup-0=-if}, so
1146: you can avoid using @code{?dup}. Using these alternatives is also more
1147: efficient than using @code{?dup}. Definitions in plain standard Forth
1148: for @code{ENDIF}, @code{?DUP-IF} and @code{?DUP-0=-IF} are provided in
1149: @file{compat/control.fs}.
1.1 anton 1150:
1151: @example
1152: @var{n}
1153: CASE
1154: @var{n1} OF @var{code1} ENDOF
1155: @var{n2} OF @var{code2} ENDOF
1.4 anton 1156: @dots{}
1.1 anton 1157: ENDCASE
1158: @end example
1159:
1160: Executes the first @var{codei}, where the @var{ni} is equal to
1161: @var{n}. A default case can be added by simply writing the code after
1162: the last @code{ENDOF}. It may use @var{n}, which is on top of the stack,
1163: but must not consume it.
1164:
1.4 anton 1165: @node Simple Loops, Counted Loops, Selection, Control Structures
1.1 anton 1166: @subsection Simple Loops
1167:
1168: @example
1169: BEGIN
1170: @var{code1}
1171: @var{flag}
1172: WHILE
1173: @var{code2}
1174: REPEAT
1175: @end example
1176:
1177: @var{code1} is executed and @var{flag} is computed. If it is true,
1178: @var{code2} is executed and the loop is restarted; If @var{flag} is false, execution continues after the @code{REPEAT}.
1179:
1180: @example
1181: BEGIN
1182: @var{code}
1183: @var{flag}
1184: UNTIL
1185: @end example
1186:
1187: @var{code} is executed. The loop is restarted if @code{flag} is false.
1188:
1189: @example
1190: BEGIN
1191: @var{code}
1192: AGAIN
1193: @end example
1194:
1195: This is an endless loop.
1196:
1.4 anton 1197: @node Counted Loops, Arbitrary control structures, Simple Loops, Control Structures
1.1 anton 1198: @subsection Counted Loops
1199:
1200: The basic counted loop is:
1201: @example
1202: @var{limit} @var{start}
1203: ?DO
1204: @var{body}
1205: LOOP
1206: @end example
1207:
1208: This performs one iteration for every integer, starting from @var{start}
1209: and up to, but excluding @var{limit}. The counter, aka index, can be
1210: accessed with @code{i}. E.g., the loop
1211: @example
1212: 10 0 ?DO
1213: i .
1214: LOOP
1215: @end example
1216: prints
1217: @example
1218: 0 1 2 3 4 5 6 7 8 9
1219: @end example
1220: The index of the innermost loop can be accessed with @code{i}, the index
1221: of the next loop with @code{j}, and the index of the third loop with
1222: @code{k}.
1223:
1224: The loop control data are kept on the return stack, so there are some
1225: restrictions on mixing return stack accesses and counted loop
1226: words. E.g., if you put values on the return stack outside the loop, you
1227: cannot read them inside the loop. If you put values on the return stack
1228: within a loop, you have to remove them before the end of the loop and
1229: before accessing the index of the loop.
1230:
1231: There are several variations on the counted loop:
1232:
1233: @code{LEAVE} leaves the innermost counted loop immediately.
1234:
1.18 anton 1235: If @var{start} is greater than @var{limit}, a @code{?DO} loop is entered
1236: (and @code{LOOP} iterates until they become equal by wrap-around
1237: arithmetic). This behaviour is usually not what you want. Therefore,
1238: Gforth offers @code{+DO} and @code{U+DO} (as replacements for
1239: @code{?DO}), which do not enter the loop if @var{start} is greater than
1240: @var{limit}; @code{+DO} is for signed loop parameters, @code{U+DO} for
1.30 anton 1241: unsigned loop parameters.
1.18 anton 1242:
1.1 anton 1243: @code{LOOP} can be replaced with @code{@var{n} +LOOP}; this updates the
1244: index by @var{n} instead of by 1. The loop is terminated when the border
1245: between @var{limit-1} and @var{limit} is crossed. E.g.:
1246:
1.18 anton 1247: @code{4 0 +DO i . 2 +LOOP} prints @code{0 2}
1.1 anton 1248:
1.18 anton 1249: @code{4 1 +DO i . 2 +LOOP} prints @code{1 3}
1.1 anton 1250:
1251: The behaviour of @code{@var{n} +LOOP} is peculiar when @var{n} is negative:
1252:
1.2 anton 1253: @code{-1 0 ?DO i . -1 +LOOP} prints @code{0 -1}
1.1 anton 1254:
1.2 anton 1255: @code{ 0 0 ?DO i . -1 +LOOP} prints nothing
1.1 anton 1256:
1.18 anton 1257: Therefore we recommend avoiding @code{@var{n} +LOOP} with negative
1258: @var{n}. One alternative is @code{@var{u} -LOOP}, which reduces the
1259: index by @var{u} each iteration. The loop is terminated when the border
1260: between @var{limit+1} and @var{limit} is crossed. Gforth also provides
1261: @code{-DO} and @code{U-DO} for down-counting loops. E.g.:
1.1 anton 1262:
1.18 anton 1263: @code{-2 0 -DO i . 1 -LOOP} prints @code{0 -1}
1.1 anton 1264:
1.18 anton 1265: @code{-1 0 -DO i . 1 -LOOP} prints @code{0}
1.1 anton 1266:
1.18 anton 1267: @code{ 0 0 -DO i . 1 -LOOP} prints nothing
1.1 anton 1268:
1.30 anton 1269: Unfortunately, @code{+DO}, @code{U+DO}, @code{-DO}, @code{U-DO} and
1270: @code{-LOOP} are not in the ANS Forth standard. However, an
1271: implementation for these words that uses only standard words is provided
1272: in @file{compat/loops.fs}.
1.18 anton 1273:
1274: @code{?DO} can also be replaced by @code{DO}. @code{DO} always enters
1275: the loop, independent of the loop parameters. Do not use @code{DO}, even
1276: if you know that the loop is entered in any case. Such knowledge tends
1277: to become invalid during maintenance of a program, and then the
1278: @code{DO} will make trouble.
1.1 anton 1279:
1280: @code{UNLOOP} is used to prepare for an abnormal loop exit, e.g., via
1281: @code{EXIT}. @code{UNLOOP} removes the loop control parameters from the
1282: return stack so @code{EXIT} can get to its return address.
1283:
1284: Another counted loop is
1285: @example
1286: @var{n}
1287: FOR
1288: @var{body}
1289: NEXT
1290: @end example
1291: This is the preferred loop of native code compiler writers who are too
1.17 anton 1292: lazy to optimize @code{?DO} loops properly. In Gforth, this loop
1.1 anton 1293: iterates @var{n+1} times; @code{i} produces values starting with @var{n}
1294: and ending with 0. Other Forth systems may behave differently, even if
1.30 anton 1295: they support @code{FOR} loops. To avoid problems, don't use @code{FOR}
1296: loops.
1.1 anton 1297:
1.4 anton 1298: @node Arbitrary control structures, Calls and returns, Counted Loops, Control Structures
1.2 anton 1299: @subsection Arbitrary control structures
1300:
1301: ANS Forth permits and supports using control structures in a non-nested
1302: way. Information about incomplete control structures is stored on the
1303: control-flow stack. This stack may be implemented on the Forth data
1.17 anton 1304: stack, and this is what we have done in Gforth.
1.2 anton 1305:
1306: An @i{orig} entry represents an unresolved forward branch, a @i{dest}
1307: entry represents a backward branch target. A few words are the basis for
1308: building any control structure possible (except control structures that
1309: need storage, like calls, coroutines, and backtracking).
1310:
1.3 anton 1311: doc-if
1312: doc-ahead
1313: doc-then
1314: doc-begin
1315: doc-until
1316: doc-again
1317: doc-cs-pick
1318: doc-cs-roll
1.2 anton 1319:
1.17 anton 1320: On many systems control-flow stack items take one word, in Gforth they
1.2 anton 1321: currently take three (this may change in the future). Therefore it is a
1322: really good idea to manipulate the control flow stack with
1323: @code{cs-pick} and @code{cs-roll}, not with data stack manipulation
1324: words.
1325:
1326: Some standard control structure words are built from these words:
1327:
1.3 anton 1328: doc-else
1329: doc-while
1330: doc-repeat
1.2 anton 1331:
1.31 anton 1332: Gforth adds some more control-structure words:
1333:
1334: doc-endif
1335: doc-?dup-if
1336: doc-?dup-0=-if
1337:
1.2 anton 1338: Counted loop words constitute a separate group of words:
1339:
1.3 anton 1340: doc-?do
1.18 anton 1341: doc-+do
1342: doc-u+do
1343: doc--do
1344: doc-u-do
1.3 anton 1345: doc-do
1346: doc-for
1347: doc-loop
1348: doc-+loop
1.18 anton 1349: doc--loop
1.3 anton 1350: doc-next
1351: doc-leave
1352: doc-?leave
1353: doc-unloop
1.10 anton 1354: doc-done
1.2 anton 1355:
1356: The standard does not allow using @code{cs-pick} and @code{cs-roll} on
1357: @i{do-sys}. Our system allows it, but it's your job to ensure that for
1358: every @code{?DO} etc. there is exactly one @code{UNLOOP} on any path
1.3 anton 1359: through the definition (@code{LOOP} etc. compile an @code{UNLOOP} on the
1360: fall-through path). Also, you have to ensure that all @code{LEAVE}s are
1.7 pazsan 1361: resolved (by using one of the loop-ending words or @code{DONE}).
1.2 anton 1362:
1363: Another group of control structure words are
1364:
1.3 anton 1365: doc-case
1366: doc-endcase
1367: doc-of
1368: doc-endof
1.2 anton 1369:
1370: @i{case-sys} and @i{of-sys} cannot be processed using @code{cs-pick} and
1371: @code{cs-roll}.
1372:
1.3 anton 1373: @subsubsection Programming Style
1374:
1375: In order to ensure readability we recommend that you do not create
1376: arbitrary control structures directly, but define new control structure
1377: words for the control structure you want and use these words in your
1378: program.
1379:
1380: E.g., instead of writing
1381:
1382: @example
1383: begin
1384: ...
1385: if [ 1 cs-roll ]
1386: ...
1387: again then
1388: @end example
1389:
1390: we recommend defining control structure words, e.g.,
1391:
1392: @example
1393: : while ( dest -- orig dest )
1394: POSTPONE if
1395: 1 cs-roll ; immediate
1396:
1397: : repeat ( orig dest -- )
1398: POSTPONE again
1399: POSTPONE then ; immediate
1400: @end example
1401:
1402: and then using these to create the control structure:
1403:
1404: @example
1405: begin
1406: ...
1407: while
1408: ...
1409: repeat
1410: @end example
1411:
1.30 anton 1412: That's much easier to read, isn't it? Of course, @code{REPEAT} and
1.3 anton 1413: @code{WHILE} are predefined, so in this example it would not be
1414: necessary to define them.
1415:
1.4 anton 1416: @node Calls and returns, Exception Handling, Arbitrary control structures, Control Structures
1.3 anton 1417: @subsection Calls and returns
1418:
1419: A definition can be called simply be writing the name of the
1.17 anton 1420: definition. When the end of the definition is reached, it returns. An
1421: earlier return can be forced using
1.3 anton 1422:
1423: doc-exit
1424:
1425: Don't forget to clean up the return stack and @code{UNLOOP} any
1426: outstanding @code{?DO}...@code{LOOP}s before @code{EXIT}ing. The
1427: primitive compiled by @code{EXIT} is
1428:
1429: doc-;s
1430:
1.4 anton 1431: @node Exception Handling, , Calls and returns, Control Structures
1.3 anton 1432: @subsection Exception Handling
1433:
1434: doc-catch
1435: doc-throw
1436:
1.4 anton 1437: @node Locals, Defining Words, Control Structures, Words
1.1 anton 1438: @section Locals
1439:
1.2 anton 1440: Local variables can make Forth programming more enjoyable and Forth
1441: programs easier to read. Unfortunately, the locals of ANS Forth are
1442: laden with restrictions. Therefore, we provide not only the ANS Forth
1443: locals wordset, but also our own, more powerful locals wordset (we
1444: implemented the ANS Forth locals wordset through our locals wordset).
1445:
1.24 anton 1446: The ideas in this section have also been published in the paper
1447: @cite{Automatic Scoping of Local Variables} by M. Anton Ertl, presented
1448: at EuroForth '94; it is available at
1449: @*@file{http://www.complang.tuwien.ac.at/papers/ertl94l.ps.gz}.
1450:
1.2 anton 1451: @menu
1.17 anton 1452: * Gforth locals::
1.4 anton 1453: * ANS Forth locals::
1.2 anton 1454: @end menu
1455:
1.17 anton 1456: @node Gforth locals, ANS Forth locals, Locals, Locals
1457: @subsection Gforth locals
1.2 anton 1458:
1459: Locals can be defined with
1460:
1461: @example
1462: @{ local1 local2 ... -- comment @}
1463: @end example
1464: or
1465: @example
1466: @{ local1 local2 ... @}
1467: @end example
1468:
1469: E.g.,
1470: @example
1471: : max @{ n1 n2 -- n3 @}
1472: n1 n2 > if
1473: n1
1474: else
1475: n2
1476: endif ;
1477: @end example
1478:
1479: The similarity of locals definitions with stack comments is intended. A
1480: locals definition often replaces the stack comment of a word. The order
1481: of the locals corresponds to the order in a stack comment and everything
1482: after the @code{--} is really a comment.
1483:
1484: This similarity has one disadvantage: It is too easy to confuse locals
1485: declarations with stack comments, causing bugs and making them hard to
1486: find. However, this problem can be avoided by appropriate coding
1487: conventions: Do not use both notations in the same program. If you do,
1488: they should be distinguished using additional means, e.g. by position.
1489:
1490: The name of the local may be preceded by a type specifier, e.g.,
1491: @code{F:} for a floating point value:
1492:
1493: @example
1494: : CX* @{ F: Ar F: Ai F: Br F: Bi -- Cr Ci @}
1495: \ complex multiplication
1496: Ar Br f* Ai Bi f* f-
1497: Ar Bi f* Ai Br f* f+ ;
1498: @end example
1499:
1.17 anton 1500: Gforth currently supports cells (@code{W:}, @code{W^}), doubles
1.2 anton 1501: (@code{D:}, @code{D^}), floats (@code{F:}, @code{F^}) and characters
1502: (@code{C:}, @code{C^}) in two flavours: a value-flavoured local (defined
1503: with @code{W:}, @code{D:} etc.) produces its value and can be changed
1504: with @code{TO}. A variable-flavoured local (defined with @code{W^} etc.)
1505: produces its address (which becomes invalid when the variable's scope is
1506: left). E.g., the standard word @code{emit} can be defined in therms of
1507: @code{type} like this:
1508:
1509: @example
1510: : emit @{ C^ char* -- @}
1511: char* 1 type ;
1512: @end example
1513:
1514: A local without type specifier is a @code{W:} local. Both flavours of
1515: locals are initialized with values from the data or FP stack.
1516:
1517: Currently there is no way to define locals with user-defined data
1518: structures, but we are working on it.
1519:
1.17 anton 1520: Gforth allows defining locals everywhere in a colon definition. This
1.7 pazsan 1521: poses the following questions:
1.2 anton 1522:
1.4 anton 1523: @menu
1524: * Where are locals visible by name?::
1.14 anton 1525: * How long do locals live?::
1.4 anton 1526: * Programming Style::
1527: * Implementation::
1528: @end menu
1529:
1.17 anton 1530: @node Where are locals visible by name?, How long do locals live?, Gforth locals, Gforth locals
1.2 anton 1531: @subsubsection Where are locals visible by name?
1532:
1533: Basically, the answer is that locals are visible where you would expect
1534: it in block-structured languages, and sometimes a little longer. If you
1535: want to restrict the scope of a local, enclose its definition in
1536: @code{SCOPE}...@code{ENDSCOPE}.
1537:
1538: doc-scope
1539: doc-endscope
1540:
1541: These words behave like control structure words, so you can use them
1542: with @code{CS-PICK} and @code{CS-ROLL} to restrict the scope in
1543: arbitrary ways.
1544:
1545: If you want a more exact answer to the visibility question, here's the
1546: basic principle: A local is visible in all places that can only be
1547: reached through the definition of the local@footnote{In compiler
1548: construction terminology, all places dominated by the definition of the
1549: local.}. In other words, it is not visible in places that can be reached
1550: without going through the definition of the local. E.g., locals defined
1551: in @code{IF}...@code{ENDIF} are visible until the @code{ENDIF}, locals
1552: defined in @code{BEGIN}...@code{UNTIL} are visible after the
1553: @code{UNTIL} (until, e.g., a subsequent @code{ENDSCOPE}).
1554:
1555: The reasoning behind this solution is: We want to have the locals
1556: visible as long as it is meaningful. The user can always make the
1557: visibility shorter by using explicit scoping. In a place that can
1558: only be reached through the definition of a local, the meaning of a
1559: local name is clear. In other places it is not: How is the local
1560: initialized at the control flow path that does not contain the
1561: definition? Which local is meant, if the same name is defined twice in
1562: two independent control flow paths?
1563:
1564: This should be enough detail for nearly all users, so you can skip the
1565: rest of this section. If you relly must know all the gory details and
1566: options, read on.
1567:
1568: In order to implement this rule, the compiler has to know which places
1569: are unreachable. It knows this automatically after @code{AHEAD},
1570: @code{AGAIN}, @code{EXIT} and @code{LEAVE}; in other cases (e.g., after
1571: most @code{THROW}s), you can use the word @code{UNREACHABLE} to tell the
1572: compiler that the control flow never reaches that place. If
1573: @code{UNREACHABLE} is not used where it could, the only consequence is
1574: that the visibility of some locals is more limited than the rule above
1575: says. If @code{UNREACHABLE} is used where it should not (i.e., if you
1576: lie to the compiler), buggy code will be produced.
1577:
1578: Another problem with this rule is that at @code{BEGIN}, the compiler
1.3 anton 1579: does not know which locals will be visible on the incoming
1580: back-edge. All problems discussed in the following are due to this
1581: ignorance of the compiler (we discuss the problems using @code{BEGIN}
1582: loops as examples; the discussion also applies to @code{?DO} and other
1.2 anton 1583: loops). Perhaps the most insidious example is:
1584: @example
1585: AHEAD
1586: BEGIN
1587: x
1588: [ 1 CS-ROLL ] THEN
1.4 anton 1589: @{ x @}
1.2 anton 1590: ...
1591: UNTIL
1592: @end example
1593:
1594: This should be legal according to the visibility rule. The use of
1595: @code{x} can only be reached through the definition; but that appears
1596: textually below the use.
1597:
1598: From this example it is clear that the visibility rules cannot be fully
1599: implemented without major headaches. Our implementation treats common
1600: cases as advertised and the exceptions are treated in a safe way: The
1601: compiler makes a reasonable guess about the locals visible after a
1602: @code{BEGIN}; if it is too pessimistic, the
1603: user will get a spurious error about the local not being defined; if the
1604: compiler is too optimistic, it will notice this later and issue a
1605: warning. In the case above the compiler would complain about @code{x}
1606: being undefined at its use. You can see from the obscure examples in
1607: this section that it takes quite unusual control structures to get the
1608: compiler into trouble, and even then it will often do fine.
1609:
1610: If the @code{BEGIN} is reachable from above, the most optimistic guess
1611: is that all locals visible before the @code{BEGIN} will also be
1612: visible after the @code{BEGIN}. This guess is valid for all loops that
1613: are entered only through the @code{BEGIN}, in particular, for normal
1614: @code{BEGIN}...@code{WHILE}...@code{REPEAT} and
1615: @code{BEGIN}...@code{UNTIL} loops and it is implemented in our
1616: compiler. When the branch to the @code{BEGIN} is finally generated by
1617: @code{AGAIN} or @code{UNTIL}, the compiler checks the guess and
1618: warns the user if it was too optimisitic:
1619: @example
1620: IF
1.4 anton 1621: @{ x @}
1.2 anton 1622: BEGIN
1623: \ x ?
1624: [ 1 cs-roll ] THEN
1625: ...
1626: UNTIL
1627: @end example
1628:
1629: Here, @code{x} lives only until the @code{BEGIN}, but the compiler
1630: optimistically assumes that it lives until the @code{THEN}. It notices
1631: this difference when it compiles the @code{UNTIL} and issues a
1632: warning. The user can avoid the warning, and make sure that @code{x}
1633: is not used in the wrong area by using explicit scoping:
1634: @example
1635: IF
1636: SCOPE
1.4 anton 1637: @{ x @}
1.2 anton 1638: ENDSCOPE
1639: BEGIN
1640: [ 1 cs-roll ] THEN
1641: ...
1642: UNTIL
1643: @end example
1644:
1645: Since the guess is optimistic, there will be no spurious error messages
1646: about undefined locals.
1647:
1648: If the @code{BEGIN} is not reachable from above (e.g., after
1649: @code{AHEAD} or @code{EXIT}), the compiler cannot even make an
1650: optimistic guess, as the locals visible after the @code{BEGIN} may be
1651: defined later. Therefore, the compiler assumes that no locals are
1.17 anton 1652: visible after the @code{BEGIN}. However, the user can use
1.2 anton 1653: @code{ASSUME-LIVE} to make the compiler assume that the same locals are
1.17 anton 1654: visible at the BEGIN as at the point where the top control-flow stack
1655: item was created.
1.2 anton 1656:
1657: doc-assume-live
1658:
1659: E.g.,
1660: @example
1.4 anton 1661: @{ x @}
1.2 anton 1662: AHEAD
1663: ASSUME-LIVE
1664: BEGIN
1665: x
1666: [ 1 CS-ROLL ] THEN
1667: ...
1668: UNTIL
1669: @end example
1670:
1671: Other cases where the locals are defined before the @code{BEGIN} can be
1672: handled by inserting an appropriate @code{CS-ROLL} before the
1673: @code{ASSUME-LIVE} (and changing the control-flow stack manipulation
1674: behind the @code{ASSUME-LIVE}).
1675:
1676: Cases where locals are defined after the @code{BEGIN} (but should be
1677: visible immediately after the @code{BEGIN}) can only be handled by
1678: rearranging the loop. E.g., the ``most insidious'' example above can be
1679: arranged into:
1680: @example
1681: BEGIN
1.4 anton 1682: @{ x @}
1.2 anton 1683: ... 0=
1684: WHILE
1685: x
1686: REPEAT
1687: @end example
1688:
1.17 anton 1689: @node How long do locals live?, Programming Style, Where are locals visible by name?, Gforth locals
1.2 anton 1690: @subsubsection How long do locals live?
1691:
1692: The right answer for the lifetime question would be: A local lives at
1693: least as long as it can be accessed. For a value-flavoured local this
1694: means: until the end of its visibility. However, a variable-flavoured
1695: local could be accessed through its address far beyond its visibility
1696: scope. Ultimately, this would mean that such locals would have to be
1697: garbage collected. Since this entails un-Forth-like implementation
1698: complexities, I adopted the same cowardly solution as some other
1699: languages (e.g., C): The local lives only as long as it is visible;
1700: afterwards its address is invalid (and programs that access it
1701: afterwards are erroneous).
1702:
1.17 anton 1703: @node Programming Style, Implementation, How long do locals live?, Gforth locals
1.2 anton 1704: @subsubsection Programming Style
1705:
1706: The freedom to define locals anywhere has the potential to change
1707: programming styles dramatically. In particular, the need to use the
1708: return stack for intermediate storage vanishes. Moreover, all stack
1709: manipulations (except @code{PICK}s and @code{ROLL}s with run-time
1710: determined arguments) can be eliminated: If the stack items are in the
1711: wrong order, just write a locals definition for all of them; then
1712: write the items in the order you want.
1713:
1714: This seems a little far-fetched and eliminating stack manipulations is
1.4 anton 1715: unlikely to become a conscious programming objective. Still, the number
1716: of stack manipulations will be reduced dramatically if local variables
1.17 anton 1717: are used liberally (e.g., compare @code{max} in @ref{Gforth locals} with
1.4 anton 1718: a traditional implementation of @code{max}).
1.2 anton 1719:
1720: This shows one potential benefit of locals: making Forth programs more
1721: readable. Of course, this benefit will only be realized if the
1722: programmers continue to honour the principle of factoring instead of
1723: using the added latitude to make the words longer.
1724:
1725: Using @code{TO} can and should be avoided. Without @code{TO},
1726: every value-flavoured local has only a single assignment and many
1727: advantages of functional languages apply to Forth. I.e., programs are
1728: easier to analyse, to optimize and to read: It is clear from the
1729: definition what the local stands for, it does not turn into something
1730: different later.
1731:
1732: E.g., a definition using @code{TO} might look like this:
1733: @example
1734: : strcmp @{ addr1 u1 addr2 u2 -- n @}
1735: u1 u2 min 0
1736: ?do
1.31 anton 1737: addr1 c@ addr2 c@ -
1738: ?dup-if
1.2 anton 1739: unloop exit
1740: then
1741: addr1 char+ TO addr1
1742: addr2 char+ TO addr2
1743: loop
1744: u1 u2 - ;
1745: @end example
1746: Here, @code{TO} is used to update @code{addr1} and @code{addr2} at
1747: every loop iteration. @code{strcmp} is a typical example of the
1748: readability problems of using @code{TO}. When you start reading
1749: @code{strcmp}, you think that @code{addr1} refers to the start of the
1750: string. Only near the end of the loop you realize that it is something
1751: else.
1752:
1753: This can be avoided by defining two locals at the start of the loop that
1754: are initialized with the right value for the current iteration.
1755: @example
1756: : strcmp @{ addr1 u1 addr2 u2 -- n @}
1757: addr1 addr2
1758: u1 u2 min 0
1759: ?do @{ s1 s2 @}
1.31 anton 1760: s1 c@ s2 c@ -
1761: ?dup-if
1.2 anton 1762: unloop exit
1763: then
1764: s1 char+ s2 char+
1765: loop
1766: 2drop
1767: u1 u2 - ;
1768: @end example
1769: Here it is clear from the start that @code{s1} has a different value
1770: in every loop iteration.
1771:
1.17 anton 1772: @node Implementation, , Programming Style, Gforth locals
1.2 anton 1773: @subsubsection Implementation
1774:
1.17 anton 1775: Gforth uses an extra locals stack. The most compelling reason for
1.2 anton 1776: this is that the return stack is not float-aligned; using an extra stack
1777: also eliminates the problems and restrictions of using the return stack
1778: as locals stack. Like the other stacks, the locals stack grows toward
1779: lower addresses. A few primitives allow an efficient implementation:
1780:
1781: doc-@local#
1782: doc-f@local#
1783: doc-laddr#
1784: doc-lp+!#
1785: doc-lp!
1786: doc->l
1787: doc-f>l
1788:
1789: In addition to these primitives, some specializations of these
1790: primitives for commonly occurring inline arguments are provided for
1791: efficiency reasons, e.g., @code{@@local0} as specialization of
1792: @code{@@local#} for the inline argument 0. The following compiling words
1793: compile the right specialized version, or the general version, as
1794: appropriate:
1795:
1.12 anton 1796: doc-compile-@local
1797: doc-compile-f@local
1.2 anton 1798: doc-compile-lp+!
1799:
1800: Combinations of conditional branches and @code{lp+!#} like
1801: @code{?branch-lp+!#} (the locals pointer is only changed if the branch
1802: is taken) are provided for efficiency and correctness in loops.
1803:
1804: A special area in the dictionary space is reserved for keeping the
1805: local variable names. @code{@{} switches the dictionary pointer to this
1806: area and @code{@}} switches it back and generates the locals
1807: initializing code. @code{W:} etc.@ are normal defining words. This
1808: special area is cleared at the start of every colon definition.
1809:
1.17 anton 1810: A special feature of Gforth's dictionary is used to implement the
1.2 anton 1811: definition of locals without type specifiers: every wordlist (aka
1812: vocabulary) has its own methods for searching
1.4 anton 1813: etc. (@pxref{Wordlists}). For the present purpose we defined a wordlist
1.2 anton 1814: with a special search method: When it is searched for a word, it
1815: actually creates that word using @code{W:}. @code{@{} changes the search
1816: order to first search the wordlist containing @code{@}}, @code{W:} etc.,
1817: and then the wordlist for defining locals without type specifiers.
1818:
1819: The lifetime rules support a stack discipline within a colon
1820: definition: The lifetime of a local is either nested with other locals
1821: lifetimes or it does not overlap them.
1822:
1823: At @code{BEGIN}, @code{IF}, and @code{AHEAD} no code for locals stack
1824: pointer manipulation is generated. Between control structure words
1825: locals definitions can push locals onto the locals stack. @code{AGAIN}
1826: is the simplest of the other three control flow words. It has to
1827: restore the locals stack depth of the corresponding @code{BEGIN}
1828: before branching. The code looks like this:
1829: @format
1830: @code{lp+!#} current-locals-size @minus{} dest-locals-size
1831: @code{branch} <begin>
1832: @end format
1833:
1834: @code{UNTIL} is a little more complicated: If it branches back, it
1835: must adjust the stack just like @code{AGAIN}. But if it falls through,
1836: the locals stack must not be changed. The compiler generates the
1837: following code:
1838: @format
1839: @code{?branch-lp+!#} <begin> current-locals-size @minus{} dest-locals-size
1840: @end format
1841: The locals stack pointer is only adjusted if the branch is taken.
1842:
1843: @code{THEN} can produce somewhat inefficient code:
1844: @format
1845: @code{lp+!#} current-locals-size @minus{} orig-locals-size
1846: <orig target>:
1847: @code{lp+!#} orig-locals-size @minus{} new-locals-size
1848: @end format
1849: The second @code{lp+!#} adjusts the locals stack pointer from the
1.4 anton 1850: level at the @var{orig} point to the level after the @code{THEN}. The
1.2 anton 1851: first @code{lp+!#} adjusts the locals stack pointer from the current
1852: level to the level at the orig point, so the complete effect is an
1853: adjustment from the current level to the right level after the
1854: @code{THEN}.
1855:
1856: In a conventional Forth implementation a dest control-flow stack entry
1857: is just the target address and an orig entry is just the address to be
1858: patched. Our locals implementation adds a wordlist to every orig or dest
1859: item. It is the list of locals visible (or assumed visible) at the point
1860: described by the entry. Our implementation also adds a tag to identify
1861: the kind of entry, in particular to differentiate between live and dead
1862: (reachable and unreachable) orig entries.
1863:
1864: A few unusual operations have to be performed on locals wordlists:
1865:
1866: doc-common-list
1867: doc-sub-list?
1868: doc-list-size
1869:
1870: Several features of our locals wordlist implementation make these
1871: operations easy to implement: The locals wordlists are organised as
1872: linked lists; the tails of these lists are shared, if the lists
1873: contain some of the same locals; and the address of a name is greater
1874: than the address of the names behind it in the list.
1875:
1876: Another important implementation detail is the variable
1877: @code{dead-code}. It is used by @code{BEGIN} and @code{THEN} to
1878: determine if they can be reached directly or only through the branch
1879: that they resolve. @code{dead-code} is set by @code{UNREACHABLE},
1880: @code{AHEAD}, @code{EXIT} etc., and cleared at the start of a colon
1881: definition, by @code{BEGIN} and usually by @code{THEN}.
1882:
1883: Counted loops are similar to other loops in most respects, but
1884: @code{LEAVE} requires special attention: It performs basically the same
1885: service as @code{AHEAD}, but it does not create a control-flow stack
1886: entry. Therefore the information has to be stored elsewhere;
1887: traditionally, the information was stored in the target fields of the
1888: branches created by the @code{LEAVE}s, by organizing these fields into a
1889: linked list. Unfortunately, this clever trick does not provide enough
1890: space for storing our extended control flow information. Therefore, we
1891: introduce another stack, the leave stack. It contains the control-flow
1892: stack entries for all unresolved @code{LEAVE}s.
1893:
1894: Local names are kept until the end of the colon definition, even if
1895: they are no longer visible in any control-flow path. In a few cases
1896: this may lead to increased space needs for the locals name area, but
1897: usually less than reclaiming this space would cost in code size.
1898:
1899:
1.17 anton 1900: @node ANS Forth locals, , Gforth locals, Locals
1.2 anton 1901: @subsection ANS Forth locals
1902:
1903: The ANS Forth locals wordset does not define a syntax for locals, but
1904: words that make it possible to define various syntaxes. One of the
1.17 anton 1905: possible syntaxes is a subset of the syntax we used in the Gforth locals
1.2 anton 1906: wordset, i.e.:
1907:
1908: @example
1909: @{ local1 local2 ... -- comment @}
1910: @end example
1911: or
1912: @example
1913: @{ local1 local2 ... @}
1914: @end example
1915:
1916: The order of the locals corresponds to the order in a stack comment. The
1917: restrictions are:
1.1 anton 1918:
1.2 anton 1919: @itemize @bullet
1920: @item
1.17 anton 1921: Locals can only be cell-sized values (no type specifiers are allowed).
1.2 anton 1922: @item
1923: Locals can be defined only outside control structures.
1924: @item
1925: Locals can interfere with explicit usage of the return stack. For the
1926: exact (and long) rules, see the standard. If you don't use return stack
1.17 anton 1927: accessing words in a definition using locals, you will be all right. The
1.2 anton 1928: purpose of this rule is to make locals implementation on the return
1929: stack easier.
1930: @item
1931: The whole definition must be in one line.
1932: @end itemize
1933:
1.35 ! anton 1934: Locals defined in this way behave like @code{VALUE}s (@xref{Simple
! 1935: Defining Words}). I.e., they are initialized from the stack. Using their
1.2 anton 1936: name produces their value. Their value can be changed using @code{TO}.
1937:
1.17 anton 1938: Since this syntax is supported by Gforth directly, you need not do
1.2 anton 1939: anything to use it. If you want to port a program using this syntax to
1.30 anton 1940: another ANS Forth system, use @file{compat/anslocal.fs} to implement the
1941: syntax on the other system.
1.2 anton 1942:
1943: Note that a syntax shown in the standard, section A.13 looks
1944: similar, but is quite different in having the order of locals
1945: reversed. Beware!
1946:
1947: The ANS Forth locals wordset itself consists of the following word
1948:
1949: doc-(local)
1950:
1951: The ANS Forth locals extension wordset defines a syntax, but it is so
1952: awful that we strongly recommend not to use it. We have implemented this
1.17 anton 1953: syntax to make porting to Gforth easy, but do not document it here. The
1.2 anton 1954: problem with this syntax is that the locals are defined in an order
1955: reversed with respect to the standard stack comment notation, making
1956: programs harder to read, and easier to misread and miswrite. The only
1957: merit of this syntax is that it is easy to implement using the ANS Forth
1958: locals wordset.
1.3 anton 1959:
1.4 anton 1960: @node Defining Words, Wordlists, Locals, Words
1961: @section Defining Words
1962:
1.14 anton 1963: @menu
1.35 ! anton 1964: * Simple Defining Words::
! 1965: * Colon Definitions::
! 1966: * User-defined Defining Words::
! 1967: * Supplying names::
! 1968: * Interpretation and Compilation Semantics::
1.14 anton 1969: @end menu
1970:
1.35 ! anton 1971: @node Simple Defining Words, Colon Definitions, Defining Words, Defining Words
! 1972: @subsection Simple Defining Words
! 1973:
! 1974: doc-constant
! 1975: doc-2constant
! 1976: doc-fconstant
! 1977: doc-variable
! 1978: doc-2variable
! 1979: doc-fvariable
! 1980: doc-create
! 1981: doc-user
! 1982: doc-value
! 1983: doc-to
! 1984: doc-defer
! 1985: doc-is
! 1986:
! 1987: @node Colon Definitions, User-defined Defining Words, Simple Defining Words, Defining Words
! 1988: @subsection Colon Definitions
! 1989:
! 1990: @example
! 1991: : name ( ... -- ... )
! 1992: word1 word2 word3 ;
! 1993: @end example
! 1994:
! 1995: creates a word called @code{name}, that, upon execution, executes
! 1996: @code{word1 word2 word3}. @code{name} is a @dfn{(colon) definition}.
! 1997:
! 1998: The explanation above is somewhat superficial. @xref{Interpretation and
! 1999: Compilation Semantics} for an in-depth discussion of some of the issues
! 2000: involved.
! 2001:
! 2002: doc-:
! 2003: doc-;
! 2004:
! 2005: @node User-defined Defining Words, Supplying names, Colon Definitions, Defining Words
! 2006: @subsection User-defined Defining Words
! 2007:
! 2008: You can create new defining words simply by wrapping defining-time code
! 2009: around existing defining words and putting the sequence in a colon
! 2010: definition.
! 2011:
! 2012: If you want the words defined by your defining words to behave
! 2013: differently than words defined with standard defining words, you can
! 2014: write your defining word like this:
! 2015:
! 2016: @example
! 2017: : def-word ( "name" -- )
! 2018: Create @var{code1}
! 2019: DOES> ( ... -- ... )
! 2020: @var{code2} ;
! 2021:
! 2022: def-word name
! 2023: @end example
! 2024:
! 2025: Technically, this fragment defines a defining word @code{def-word}, and
! 2026: a word @code{name}; when you execute @code{name}, the address of the
! 2027: body of @code{name} is put on the data stack and @var{code2} is executed
! 2028: (the address of the body of @code{name} is the address @code{HERE}
! 2029: returns immediately after the @code{CREATE}). E.g., you can implement
! 2030: @code{Constant} in this way:
! 2031:
! 2032: @example
! 2033: : constant ( w "name" -- )
! 2034: create ,
! 2035: DOES> ( -- w )
! 2036: @ ;
! 2037: @end example
! 2038:
! 2039: When you create a constant with @code{5 constant five}, first a new word
! 2040: @code{five} is created, then the value 5 is laid down in the body of
! 2041: @code{five} with @code{,}. When @code{five} is invoked, the address of
! 2042: the body is put on the stack, and @code{@@} retrieves the value 5.
! 2043:
! 2044: In the example above the stack comment after the @code{DOES>} specifies
! 2045: the stack effect of the defined words, not the stack effect of the
! 2046: following code (the following code expects the address of the body on
! 2047: the top of stack, which is not reflected in the stack comment). This is
! 2048: the convention that I use and recommend (it clashes a bit with using
! 2049: locals declarations for stack effect specification, though).
! 2050:
! 2051: @subsubsection Applications of @code{CREATE..DOES>}
! 2052:
! 2053: You may not be sure how to use this feature. Here are some usage
! 2054: patterns:
! 2055:
! 2056: When you see a sequence of code occurring several times, and you can
! 2057: identify a meaning, you will factor it out as a colon definition. When
! 2058: you see similar colon definitions, you can factor them using
! 2059: @code{CREATE..DOES>}. E.g., an assembler usually defines several words
! 2060: that look very similar:
! 2061: @example
! 2062: : ori, ( reg-taget reg-source n -- )
! 2063: 0 asm-reg-reg-imm ;
! 2064: : andi, ( reg-taget reg-source n -- )
! 2065: 1 asm-reg-reg-imm ;
! 2066: @end example
! 2067:
! 2068: This could be factored with:
! 2069: @example
! 2070: : reg-reg-imm ( op-code -- )
! 2071: create ,
! 2072: DOES> ( reg-taget reg-source n -- )
! 2073: @ asm-reg-reg-imm ;
! 2074:
! 2075: 0 reg-reg-imm ori,
! 2076: 1 reg-reg-imm andi,
! 2077: @end example
! 2078:
! 2079: Another view of @code{CREATE..DOES>} is to consider it as a crude way to
! 2080: supply a part of the parameters for a word (known as @dfn{currying} in
! 2081: the functional language community). E.g., @code{+} needs two
! 2082: parameters. Creating versions of @code{+} with one parameter fixed can
! 2083: be done like this:
! 2084: @example
! 2085: : curry+ ( n1 -- )
! 2086: create ,
! 2087: DOES> ( n2 -- n1+n2 )
! 2088: @ + ;
! 2089:
! 2090: 3 curry+ 3+
! 2091: -2 curry+ 2-
! 2092: @end example
! 2093:
! 2094: @subsubsection The gory details of @code{CREATE..DOES>}
! 2095:
! 2096: doc-does>
! 2097:
! 2098: This means that you need not use @code{CREATE} and @code{DOES>} in the
! 2099: same definition; E.g., you can put the @code{DOES>}-part in a separate
! 2100: definition. This allows us to, e.g., select among different DOES>-parts:
! 2101: @example
! 2102: : does1
! 2103: DOES> ( ... -- ... )
! 2104: ... ;
! 2105:
! 2106: : does2
! 2107: DOES> ( ... -- ... )
! 2108: ... ;
! 2109:
! 2110: : def-word ( ... -- ... )
! 2111: create ...
! 2112: IF
! 2113: does1
! 2114: ELSE
! 2115: does2
! 2116: ENDIF ;
! 2117: @end example
! 2118:
! 2119: In a standard program you can apply a @code{DOES>}-part only if the last
! 2120: word was defined with @code{CREATE}. In Gforth, the @code{DOES>}-part
! 2121: will override the behaviour of the last word defined in any case. In a
! 2122: standard program, you can use @code{DOES>} only in a colon
! 2123: definition. In Gforth, you can also use it in interpretation state, in a
! 2124: kind of one-shot mode:
! 2125: @example
! 2126: CREATE name ( ... -- ... )
! 2127: @var{initialization}
! 2128: DOES>
! 2129: @var{code} ;
! 2130: @end example
! 2131: This is equivalwent to the standard
! 2132: @example
! 2133: :noname
! 2134: DOES>
! 2135: @var{code} ;
! 2136: CREATE name EXECUTE ( ... -- ... )
! 2137: @var{initialization}
! 2138: @end example
! 2139:
! 2140: You can get the address of the body of a word with
! 2141:
! 2142: doc->body
! 2143:
! 2144: @node Supplying names, Interpretation and Compilation Semantics, User-defined Defining Words, Defining Words
! 2145: @subsection Supplying names for the defined words
! 2146:
! 2147: By default, defining words take the names for the defined words from the
! 2148: input stream. Sometimes you want to supply the name from a string. You
! 2149: can do this with
! 2150:
! 2151: doc-nextname
! 2152:
! 2153: E.g.,
! 2154:
! 2155: @example
! 2156: s" foo" nextname create
! 2157: @end example
! 2158: is equivalent to
! 2159: @example
! 2160: create foo
! 2161: @end example
! 2162:
! 2163: Sometimes you want to define a word without a name. You can do this with
! 2164:
! 2165: doc-noname
! 2166:
! 2167: To make any use of the newly defined word, you need its execution
! 2168: token. You can get it with
! 2169:
! 2170: doc-lastxt
! 2171:
! 2172: E.g., you can initialize a deferred word with an anonymous colon
! 2173: definition:
! 2174: @example
! 2175: Defer deferred
! 2176: noname : ( ... -- ... )
! 2177: ... ;
! 2178: lastxt IS deferred
! 2179: @end example
! 2180:
! 2181: @code{lastxt} also works when the last word was not defined as
! 2182: @code{noname}.
! 2183:
! 2184: The standard has also recognized the need for anonymous words and
! 2185: provides
! 2186:
! 2187: doc-:noname
! 2188:
! 2189: This leaves the execution token for the word on the stack after the
! 2190: closing @code{;}. You can rewrite the last example with @code{:noname}:
! 2191: @example
! 2192: Defer deferred
! 2193: :noname ( ... -- ... )
! 2194: ... ;
! 2195: IS deferred
! 2196: @end example
! 2197:
! 2198: @node Interpretation and Compilation Semantics, , Supplying names, Defining Words
! 2199: @subsection Interpretation and Compilation Semantics
! 2200:
! 2201: doc-immediate
! 2202: doc-interpret/compile:
! 2203:
! 2204:
1.4 anton 2205:
2206: @node Wordlists, Files, Defining Words, Words
2207: @section Wordlists
2208:
2209: @node Files, Blocks, Wordlists, Words
2210: @section Files
2211:
2212: @node Blocks, Other I/O, Files, Words
2213: @section Blocks
2214:
2215: @node Other I/O, Programming Tools, Blocks, Words
2216: @section Other I/O
2217:
1.18 anton 2218: @node Programming Tools, Assembler and Code words, Other I/O, Words
1.4 anton 2219: @section Programming Tools
2220:
1.5 anton 2221: @menu
2222: * Debugging:: Simple and quick.
2223: * Assertions:: Making your programs self-checking.
2224: @end menu
2225:
2226: @node Debugging, Assertions, Programming Tools, Programming Tools
1.4 anton 2227: @subsection Debugging
2228:
2229: The simple debugging aids provided in @file{debugging.fs}
2230: are meant to support a different style of debugging than the
2231: tracing/stepping debuggers used in languages with long turn-around
2232: times.
2233:
2234: A much better (faster) way in fast-compilig languages is to add
2235: printing code at well-selected places, let the program run, look at
2236: the output, see where things went wrong, add more printing code, etc.,
2237: until the bug is found.
2238:
2239: The word @code{~~} is easy to insert. It just prints debugging
2240: information (by default the source location and the stack contents). It
2241: is also easy to remove (@kbd{C-x ~} in the Emacs Forth mode to
2242: query-replace them with nothing). The deferred words
2243: @code{printdebugdata} and @code{printdebugline} control the output of
2244: @code{~~}. The default source location output format works well with
2245: Emacs' compilation mode, so you can step through the program at the
1.5 anton 2246: source level using @kbd{C-x `} (the advantage over a stepping debugger
2247: is that you can step in any direction and you know where the crash has
2248: happened or where the strange data has occurred).
1.4 anton 2249:
2250: Note that the default actions clobber the contents of the pictured
2251: numeric output string, so you should not use @code{~~}, e.g., between
2252: @code{<#} and @code{#>}.
2253:
2254: doc-~~
2255: doc-printdebugdata
2256: doc-printdebugline
2257:
1.5 anton 2258: @node Assertions, , Debugging, Programming Tools
1.4 anton 2259: @subsection Assertions
2260:
1.5 anton 2261: It is a good idea to make your programs self-checking, in particular, if
2262: you use an assumption (e.g., that a certain field of a data structure is
1.17 anton 2263: never zero) that may become wrong during maintenance. Gforth supports
1.5 anton 2264: assertions for this purpose. They are used like this:
2265:
2266: @example
2267: assert( @var{flag} )
2268: @end example
2269:
2270: The code between @code{assert(} and @code{)} should compute a flag, that
2271: should be true if everything is alright and false otherwise. It should
2272: not change anything else on the stack. The overall stack effect of the
2273: assertion is @code{( -- )}. E.g.
2274:
2275: @example
2276: assert( 1 1 + 2 = ) \ what we learn in school
2277: assert( dup 0<> ) \ assert that the top of stack is not zero
2278: assert( false ) \ this code should not be reached
2279: @end example
2280:
2281: The need for assertions is different at different times. During
2282: debugging, we want more checking, in production we sometimes care more
2283: for speed. Therefore, assertions can be turned off, i.e., the assertion
2284: becomes a comment. Depending on the importance of an assertion and the
2285: time it takes to check it, you may want to turn off some assertions and
1.17 anton 2286: keep others turned on. Gforth provides several levels of assertions for
1.5 anton 2287: this purpose:
2288:
2289: doc-assert0(
2290: doc-assert1(
2291: doc-assert2(
2292: doc-assert3(
2293: doc-assert(
2294: doc-)
2295:
2296: @code{Assert(} is the same as @code{assert1(}. The variable
2297: @code{assert-level} specifies the highest assertions that are turned
2298: on. I.e., at the default @code{assert-level} of one, @code{assert0(} and
2299: @code{assert1(} assertions perform checking, while @code{assert2(} and
2300: @code{assert3(} assertions are treated as comments.
2301:
2302: Note that the @code{assert-level} is evaluated at compile-time, not at
2303: run-time. I.e., you cannot turn assertions on or off at run-time, you
2304: have to set the @code{assert-level} appropriately before compiling a
2305: piece of code. You can compile several pieces of code at several
2306: @code{assert-level}s (e.g., a trusted library at level 1 and newly
2307: written code at level 3).
2308:
2309: doc-assert-level
2310:
2311: If an assertion fails, a message compatible with Emacs' compilation mode
2312: is produced and the execution is aborted (currently with @code{ABORT"}.
2313: If there is interest, we will introduce a special throw code. But if you
2314: intend to @code{catch} a specific condition, using @code{throw} is
2315: probably more appropriate than an assertion).
2316:
1.18 anton 2317: @node Assembler and Code words, Threading Words, Programming Tools, Words
2318: @section Assembler and Code words
2319:
2320: Gforth provides some words for defining primitives (words written in
2321: machine code), and for defining the the machine-code equivalent of
2322: @code{DOES>}-based defining words. However, the machine-independent
2323: nature of Gforth poses a few problems: First of all. Gforth runs on
2324: several architectures, so it can provide no standard assembler. What's
2325: worse is that the register allocation not only depends on the processor,
1.25 anton 2326: but also on the @code{gcc} version and options used.
1.18 anton 2327:
1.25 anton 2328: The words that Gforth offers encapsulate some system dependences (e.g., the
1.18 anton 2329: header structure), so a system-independent assembler may be used in
2330: Gforth. If you do not have an assembler, you can compile machine code
2331: directly with @code{,} and @code{c,}.
2332:
2333: doc-assembler
2334: doc-code
2335: doc-end-code
2336: doc-;code
2337: doc-flush-icache
2338:
2339: If @code{flush-icache} does not work correctly, @code{code} words
2340: etc. will not work (reliably), either.
2341:
2342: These words are rarely used. Therefore they reside in @code{code.fs},
2343: which is usually not loaded (except @code{flush-icache}, which is always
1.19 anton 2344: present). You can load them with @code{require code.fs}.
1.18 anton 2345:
1.25 anton 2346: In the assembly code you will want to refer to the inner interpreter's
2347: registers (e.g., the data stack pointer) and you may want to use other
2348: registers for temporary storage. Unfortunately, the register allocation
2349: is installation-dependent.
2350:
2351: The easiest solution is to use explicit register declarations
2352: (@pxref{Explicit Reg Vars, , Variables in Specified Registers, gcc.info,
2353: GNU C Manual}) for all of the inner interpreter's registers: You have to
2354: compile Gforth with @code{-DFORCE_REG} (configure option
2355: @code{--enable-force-reg}) and the appropriate declarations must be
2356: present in the @code{machine.h} file (see @code{mips.h} for an example;
2357: you can find a full list of all declarable register symbols with
2358: @code{grep register engine.c}). If you give explicit registers to all
2359: variables that are declared at the beginning of @code{engine()}, you
2360: should be able to use the other caller-saved registers for temporary
2361: storage. Alternatively, you can use the @code{gcc} option
2362: @code{-ffixed-REG} (@pxref{Code Gen Options, , Options for Code
2363: Generation Conventions, gcc.info, GNU C Manual}) to reserve a register
2364: (however, this restriction on register allocation may slow Gforth
2365: significantly).
2366:
2367: If this solution is not viable (e.g., because @code{gcc} does not allow
2368: you to explicitly declare all the registers you need), you have to find
2369: out by looking at the code where the inner interpreter's registers
2370: reside and which registers can be used for temporary storage. You can
2371: get an assembly listing of the engine's code with @code{make engine.s}.
2372:
2373: In any case, it is good practice to abstract your assembly code from the
2374: actual register allocation. E.g., if the data stack pointer resides in
2375: register @code{$17}, create an alias for this register called @code{sp},
2376: and use that in your assembly code.
2377:
1.18 anton 2378: Another option for implementing normal and defining words efficiently
2379: is: adding the wanted functionality to the source of Gforth. For normal
1.35 ! anton 2380: words you just have to edit @file{primitives} (@pxref{Automatic
! 2381: Generation}), defining words (equivalent to @code{;CODE} words, for fast
! 2382: defined words) may require changes in @file{engine.c}, @file{kernal.fs},
! 2383: @file{prims2x.fs}, and possibly @file{cross.fs}.
1.18 anton 2384:
2385:
2386: @node Threading Words, , Assembler and Code words, Words
1.4 anton 2387: @section Threading Words
2388:
2389: These words provide access to code addresses and other threading stuff
1.17 anton 2390: in Gforth (and, possibly, other interpretive Forths). It more or less
1.4 anton 2391: abstracts away the differences between direct and indirect threading
2392: (and, for direct threading, the machine dependences). However, at
2393: present this wordset is still inclomplete. It is also pretty low-level;
2394: some day it will hopefully be made unnecessary by an internals words set
2395: that abstracts implementation details away completely.
2396:
2397: doc->code-address
2398: doc->does-code
2399: doc-code-address!
2400: doc-does-code!
2401: doc-does-handler!
2402: doc-/does-handler
2403:
1.18 anton 2404: The code addresses produced by various defining words are produced by
2405: the following words:
1.14 anton 2406:
1.18 anton 2407: doc-docol:
2408: doc-docon:
2409: doc-dovar:
2410: doc-douser:
2411: doc-dodefer:
2412: doc-dofield:
2413:
1.35 ! anton 2414: You can recognize words defined by a @code{CREATE}...@code{DOES>} word
! 2415: with @code{>DOES-CODE}. If the word was defined in that way, the value
! 2416: returned is different from 0 and identifies the @code{DOES>} used by the
! 2417: defining word.
1.14 anton 2418:
1.4 anton 2419: @node ANS conformance, Model, Words, Top
2420: @chapter ANS conformance
2421:
1.17 anton 2422: To the best of our knowledge, Gforth is an
1.14 anton 2423:
1.15 anton 2424: ANS Forth System
1.34 anton 2425: @itemize @bullet
1.15 anton 2426: @item providing the Core Extensions word set
2427: @item providing the Block word set
2428: @item providing the Block Extensions word set
2429: @item providing the Double-Number word set
2430: @item providing the Double-Number Extensions word set
2431: @item providing the Exception word set
2432: @item providing the Exception Extensions word set
2433: @item providing the Facility word set
2434: @item providing @code{MS} and @code{TIME&DATE} from the Facility Extensions word set
2435: @item providing the File Access word set
2436: @item providing the File Access Extensions word set
2437: @item providing the Floating-Point word set
2438: @item providing the Floating-Point Extensions word set
2439: @item providing the Locals word set
2440: @item providing the Locals Extensions word set
2441: @item providing the Memory-Allocation word set
2442: @item providing the Memory-Allocation Extensions word set (that one's easy)
2443: @item providing the Programming-Tools word set
1.34 anton 2444: @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
1.15 anton 2445: @item providing the Search-Order word set
2446: @item providing the Search-Order Extensions word set
2447: @item providing the String word set
2448: @item providing the String Extensions word set (another easy one)
2449: @end itemize
2450:
2451: In addition, ANS Forth systems are required to document certain
2452: implementation choices. This chapter tries to meet these
2453: requirements. In many cases it gives a way to ask the system for the
2454: information instead of providing the information directly, in
2455: particular, if the information depends on the processor, the operating
2456: system or the installation options chosen, or if they are likely to
1.17 anton 2457: change during the maintenance of Gforth.
1.15 anton 2458:
1.14 anton 2459: @comment The framework for the rest has been taken from pfe.
2460:
2461: @menu
2462: * The Core Words::
2463: * The optional Block word set::
2464: * The optional Double Number word set::
2465: * The optional Exception word set::
2466: * The optional Facility word set::
2467: * The optional File-Access word set::
2468: * The optional Floating-Point word set::
2469: * The optional Locals word set::
2470: * The optional Memory-Allocation word set::
2471: * The optional Programming-Tools word set::
2472: * The optional Search-Order word set::
2473: @end menu
2474:
2475:
2476: @c =====================================================================
2477: @node The Core Words, The optional Block word set, ANS conformance, ANS conformance
2478: @comment node-name, next, previous, up
2479: @section The Core Words
2480: @c =====================================================================
2481:
2482: @menu
1.15 anton 2483: * core-idef:: Implementation Defined Options
2484: * core-ambcond:: Ambiguous Conditions
2485: * core-other:: Other System Documentation
1.14 anton 2486: @end menu
2487:
2488: @c ---------------------------------------------------------------------
2489: @node core-idef, core-ambcond, The Core Words, The Core Words
2490: @subsection Implementation Defined Options
2491: @c ---------------------------------------------------------------------
2492:
2493: @table @i
2494:
2495: @item (Cell) aligned addresses:
1.17 anton 2496: processor-dependent. Gforth's alignment words perform natural alignment
1.14 anton 2497: (e.g., an address aligned for a datum of size 8 is divisible by
2498: 8). Unaligned accesses usually result in a @code{-23 THROW}.
2499:
2500: @item @code{EMIT} and non-graphic characters:
2501: The character is output using the C library function (actually, macro)
2502: @code{putchar}.
2503:
2504: @item character editing of @code{ACCEPT} and @code{EXPECT}:
2505: This is modeled on the GNU readline library (@pxref{Readline
2506: Interaction, , Command Line Editing, readline, The GNU Readline
2507: Library}) with Emacs-like key bindings. @kbd{Tab} deviates a little by
2508: producing a full word completion every time you type it (instead of
2509: producing the common prefix of all completions).
2510:
2511: @item character set:
2512: The character set of your computer and display device. Gforth is
2513: 8-bit-clean (but some other component in your system may make trouble).
2514:
2515: @item Character-aligned address requirements:
2516: installation-dependent. Currently a character is represented by a C
2517: @code{unsigned char}; in the future we might switch to @code{wchar_t}
2518: (Comments on that requested).
2519:
2520: @item character-set extensions and matching of names:
1.17 anton 2521: Any character except the ASCII NUL charcter can be used in a
2522: name. Matching is case-insensitive. The matching is performed using the
2523: C function @code{strncasecmp}, whose function is probably influenced by
2524: the locale. E.g., the @code{C} locale does not know about accents and
1.14 anton 2525: umlauts, so they are matched case-sensitively in that locale. For
2526: portability reasons it is best to write programs such that they work in
2527: the @code{C} locale. Then one can use libraries written by a Polish
2528: programmer (who might use words containing ISO Latin-2 encoded
2529: characters) and by a French programmer (ISO Latin-1) in the same program
2530: (of course, @code{WORDS} will produce funny results for some of the
2531: words (which ones, depends on the font you are using)). Also, the locale
2532: you prefer may not be available in other operating systems. Hopefully,
2533: Unicode will solve these problems one day.
2534:
2535: @item conditions under which control characters match a space delimiter:
2536: If @code{WORD} is called with the space character as a delimiter, all
2537: white-space characters (as identified by the C macro @code{isspace()})
2538: are delimiters. @code{PARSE}, on the other hand, treats space like other
2539: delimiters. @code{PARSE-WORD} treats space like @code{WORD}, but behaves
2540: like @code{PARSE} otherwise. @code{(NAME)}, which is used by the outer
2541: interpreter (aka text interpreter) by default, treats all white-space
2542: characters as delimiters.
2543:
2544: @item format of the control flow stack:
2545: The data stack is used as control flow stack. The size of a control flow
2546: stack item in cells is given by the constant @code{cs-item-size}. At the
2547: time of this writing, an item consists of a (pointer to a) locals list
2548: (third), an address in the code (second), and a tag for identifying the
2549: item (TOS). The following tags are used: @code{defstart},
2550: @code{live-orig}, @code{dead-orig}, @code{dest}, @code{do-dest},
2551: @code{scopestart}.
2552:
2553: @item conversion of digits > 35
2554: The characters @code{[\]^_'} are the digits with the decimal value
2555: 36@minus{}41. There is no way to input many of the larger digits.
2556:
2557: @item display after input terminates in @code{ACCEPT} and @code{EXPECT}:
2558: The cursor is moved to the end of the entered string. If the input is
2559: terminated using the @kbd{Return} key, a space is typed.
2560:
2561: @item exception abort sequence of @code{ABORT"}:
2562: The error string is stored into the variable @code{"error} and a
2563: @code{-2 throw} is performed.
2564:
2565: @item input line terminator:
2566: For interactive input, @kbd{C-m} and @kbd{C-j} terminate lines. One of
2567: these characters is typically produced when you type the @kbd{Enter} or
2568: @kbd{Return} key.
2569:
2570: @item maximum size of a counted string:
2571: @code{s" /counted-string" environment? drop .}. Currently 255 characters
2572: on all ports, but this may change.
2573:
2574: @item maximum size of a parsed string:
2575: Given by the constant @code{/line}. Currently 255 characters.
2576:
2577: @item maximum size of a definition name, in characters:
2578: 31
2579:
2580: @item maximum string length for @code{ENVIRONMENT?}, in characters:
2581: 31
2582:
2583: @item method of selecting the user input device:
1.17 anton 2584: The user input device is the standard input. There is currently no way to
2585: change it from within Gforth. However, the input can typically be
2586: redirected in the command line that starts Gforth.
1.14 anton 2587:
2588: @item method of selecting the user output device:
2589: The user output device is the standard output. It cannot be redirected
1.17 anton 2590: from within Gforth, but typically from the command line that starts
2591: Gforth. Gforth uses buffered output, so output on a terminal does not
1.14 anton 2592: become visible before the next newline or buffer overflow. Output on
2593: non-terminals is invisible until the buffer overflows.
2594:
2595: @item methods of dictionary compilation:
1.17 anton 2596: What are we expected to document here?
1.14 anton 2597:
2598: @item number of bits in one address unit:
2599: @code{s" address-units-bits" environment? drop .}. 8 in all current
2600: ports.
2601:
2602: @item number representation and arithmetic:
2603: Processor-dependent. Binary two's complement on all current ports.
2604:
2605: @item ranges for integer types:
2606: Installation-dependent. Make environmental queries for @code{MAX-N},
2607: @code{MAX-U}, @code{MAX-D} and @code{MAX-UD}. The lower bounds for
2608: unsigned (and positive) types is 0. The lower bound for signed types on
2609: two's complement and one's complement machines machines can be computed
2610: by adding 1 to the upper bound.
2611:
2612: @item read-only data space regions:
2613: The whole Forth data space is writable.
2614:
2615: @item size of buffer at @code{WORD}:
2616: @code{PAD HERE - .}. 104 characters on 32-bit machines. The buffer is
2617: shared with the pictured numeric output string. If overwriting
2618: @code{PAD} is acceptable, it is as large as the remaining dictionary
2619: space, although only as much can be sensibly used as fits in a counted
2620: string.
2621:
2622: @item size of one cell in address units:
2623: @code{1 cells .}.
2624:
2625: @item size of one character in address units:
2626: @code{1 chars .}. 1 on all current ports.
2627:
2628: @item size of the keyboard terminal buffer:
2629: Varies. You can determine the size at a specific time using @code{lp@
2630: tib - .}. It is shared with the locals stack and TIBs of files that
2631: include the current file. You can change the amount of space for TIBs
1.17 anton 2632: and locals stack at Gforth startup with the command line option
1.14 anton 2633: @code{-l}.
2634:
2635: @item size of the pictured numeric output buffer:
2636: @code{PAD HERE - .}. 104 characters on 32-bit machines. The buffer is
2637: shared with @code{WORD}.
2638:
2639: @item size of the scratch area returned by @code{PAD}:
2640: The remainder of dictionary space. You can even use the unused part of
2641: the data stack space. The current size can be computed with @code{sp@
2642: pad - .}.
2643:
2644: @item system case-sensitivity characteristics:
2645: Dictionary searches are case insensitive. However, as explained above
2646: under @i{character-set extensions}, the matching for non-ASCII
2647: characters is determined by the locale you are using. In the default
2648: @code{C} locale all non-ASCII characters are matched case-sensitively.
2649:
2650: @item system prompt:
2651: @code{ ok} in interpret state, @code{ compiled} in compile state.
2652:
2653: @item division rounding:
2654: installation dependent. @code{s" floored" environment? drop .}. We leave
1.25 anton 2655: the choice to @code{gcc} (what to use for @code{/}) and to you (whether to use
1.14 anton 2656: @code{fm/mod}, @code{sm/rem} or simply @code{/}).
2657:
2658: @item values of @code{STATE} when true:
2659: -1.
2660:
2661: @item values returned after arithmetic overflow:
2662: On two's complement machines, arithmetic is performed modulo
2663: 2**bits-per-cell for single arithmetic and 4**bits-per-cell for double
2664: arithmetic (with appropriate mapping for signed types). Division by zero
2665: typically results in a @code{-55 throw} (floatingpoint unidentified
2666: fault), although a @code{-10 throw} (divide by zero) would be more
2667: appropriate.
2668:
2669: @item whether the current definition can be found after @t{DOES>}:
2670: No.
2671:
2672: @end table
2673:
2674: @c ---------------------------------------------------------------------
2675: @node core-ambcond, core-other, core-idef, The Core Words
2676: @subsection Ambiguous conditions
2677: @c ---------------------------------------------------------------------
2678:
2679: @table @i
2680:
2681: @item a name is neither a word nor a number:
2682: @code{-13 throw} (Undefined word)
2683:
2684: @item a definition name exceeds the maximum length allowed:
2685: @code{-19 throw} (Word name too long)
2686:
2687: @item addressing a region not inside the various data spaces of the forth system:
2688: The stacks, code space and name space are accessible. Machine code space is
2689: typically readable. Accessing other addresses gives results dependent on
2690: the operating system. On decent systems: @code{-9 throw} (Invalid memory
2691: address).
2692:
2693: @item argument type incompatible with parameter:
2694: This is usually not caught. Some words perform checks, e.g., the control
2695: flow words, and issue a @code{ABORT"} or @code{-12 THROW} (Argument type
2696: mismatch).
2697:
2698: @item attempting to obtain the execution token of a word with undefined execution semantics:
2699: You get an execution token representing the compilation semantics
2700: instead.
2701:
2702: @item dividing by zero:
2703: typically results in a @code{-55 throw} (floating point unidentified
2704: fault), although a @code{-10 throw} (divide by zero) would be more
2705: appropriate.
2706:
2707: @item insufficient data stack or return stack space:
2708: Not checked. This typically results in mysterious illegal memory
2709: accesses, producing @code{-9 throw} (Invalid memory address) or
2710: @code{-23 throw} (Address alignment exception).
2711:
2712: @item insufficient space for loop control parameters:
2713: like other return stack overflows.
2714:
2715: @item insufficient space in the dictionary:
2716: Not checked. Similar results as stack overflows. However, typically the
2717: error appears at a different place when one inserts or removes code.
2718:
2719: @item interpreting a word with undefined interpretation semantics:
2720: For some words, we defined interpretation semantics. For the others:
2721: @code{-14 throw} (Interpreting a compile-only word). Note that this is
2722: checked only by the outer (aka text) interpreter; if the word is
2723: @code{execute}d in some other way, it will typically perform it's
2724: compilation semantics even in interpret state. (We could change @code{'}
2725: and relatives not to give the xt of such words, but we think that would
2726: be too restrictive).
2727:
2728: @item modifying the contents of the input buffer or a string literal:
2729: These are located in writable memory and can be modified.
2730:
2731: @item overflow of the pictured numeric output string:
2732: Not checked.
2733:
2734: @item parsed string overflow:
2735: @code{PARSE} cannot overflow. @code{WORD} does not check for overflow.
2736:
2737: @item producing a result out of range:
2738: On two's complement machines, arithmetic is performed modulo
2739: 2**bits-per-cell for single arithmetic and 4**bits-per-cell for double
2740: arithmetic (with appropriate mapping for signed types). Division by zero
2741: typically results in a @code{-55 throw} (floatingpoint unidentified
2742: fault), although a @code{-10 throw} (divide by zero) would be more
2743: appropriate. @code{convert} and @code{>number} currently overflow
2744: silently.
2745:
2746: @item reading from an empty data or return stack:
2747: The data stack is checked by the outer (aka text) interpreter after
2748: every word executed. If it has underflowed, a @code{-4 throw} (Stack
2749: underflow) is performed. Apart from that, the stacks are not checked and
2750: underflows can result in similar behaviour as overflows (of adjacent
2751: stacks).
2752:
2753: @item unexepected end of the input buffer, resulting in an attempt to use a zero-length string as a name:
2754: @code{Create} and its descendants perform a @code{-16 throw} (Attempt to
2755: use zero-length string as a name). Words like @code{'} probably will not
2756: find what they search. Note that it is possible to create zero-length
2757: names with @code{nextname} (should it not?).
2758:
2759: @item @code{>IN} greater than input buffer:
2760: The next invocation of a parsing word returns a string wih length 0.
2761:
2762: @item @code{RECURSE} appears after @code{DOES>}:
2763: Compiles a recursive call to the defining word not to the defined word.
2764:
2765: @item argument input source different than current input source for @code{RESTORE-INPUT}:
1.27 anton 2766: @code{-12 THROW}. Note that, once an input file is closed (e.g., because
2767: the end of the file was reached), its source-id may be
2768: reused. Therefore, restoring an input source specification referencing a
2769: closed file may lead to unpredictable results instead of a @code{-12
2770: THROW}.
2771:
2772: In the future, Gforth may be able to retore input source specifications
2773: from other than the current input soruce.
1.14 anton 2774:
2775: @item data space containing definitions gets de-allocated:
2776: Deallocation with @code{allot} is not checked. This typically resuls in
2777: memory access faults or execution of illegal instructions.
2778:
2779: @item data space read/write with incorrect alignment:
2780: Processor-dependent. Typically results in a @code{-23 throw} (Address
2781: alignment exception). Under Linux on a 486 or later processor with
2782: alignment turned on, incorrect alignment results in a @code{-9 throw}
2783: (Invalid memory address). There are reportedly some processors with
2784: alignment restrictions that do not report them.
2785:
2786: @item data space pointer not properly aligned, @code{,}, @code{C,}:
2787: Like other alignment errors.
2788:
2789: @item less than u+2 stack items (@code{PICK} and @code{ROLL}):
2790: Not checked. May cause an illegal memory access.
2791:
2792: @item loop control parameters not available:
2793: Not checked. The counted loop words simply assume that the top of return
2794: stack items are loop control parameters and behave accordingly.
2795:
2796: @item most recent definition does not have a name (@code{IMMEDIATE}):
2797: @code{abort" last word was headerless"}.
2798:
2799: @item name not defined by @code{VALUE} used by @code{TO}:
2800: @code{-32 throw} (Invalid name argument)
2801:
1.15 anton 2802: @item name not found (@code{'}, @code{POSTPONE}, @code{[']}, @code{[COMPILE]}):
1.14 anton 2803: @code{-13 throw} (Undefined word)
2804:
2805: @item parameters are not of the same type (@code{DO}, @code{?DO}, @code{WITHIN}):
2806: Gforth behaves as if they were of the same type. I.e., you can predict
2807: the behaviour by interpreting all parameters as, e.g., signed.
2808:
2809: @item @code{POSTPONE} or @code{[COMPILE]} applied to @code{TO}:
2810: Assume @code{: X POSTPONE TO ; IMMEDIATE}. @code{X} is equivalent to
2811: @code{TO}.
2812:
2813: @item String longer than a counted string returned by @code{WORD}:
2814: Not checked. The string will be ok, but the count will, of course,
2815: contain only the least significant bits of the length.
2816:
1.15 anton 2817: @item u greater than or equal to the number of bits in a cell (@code{LSHIFT}, @code{RSHIFT}):
1.14 anton 2818: Processor-dependent. Typical behaviours are returning 0 and using only
2819: the low bits of the shift count.
2820:
2821: @item word not defined via @code{CREATE}:
2822: @code{>BODY} produces the PFA of the word no matter how it was defined.
2823:
2824: @code{DOES>} changes the execution semantics of the last defined word no
2825: matter how it was defined. E.g., @code{CONSTANT DOES>} is equivalent to
2826: @code{CREATE , DOES>}.
2827:
2828: @item words improperly used outside @code{<#} and @code{#>}:
2829: Not checked. As usual, you can expect memory faults.
2830:
2831: @end table
2832:
2833:
2834: @c ---------------------------------------------------------------------
2835: @node core-other, , core-ambcond, The Core Words
2836: @subsection Other system documentation
2837: @c ---------------------------------------------------------------------
2838:
2839: @table @i
2840:
2841: @item nonstandard words using @code{PAD}:
2842: None.
2843:
2844: @item operator's terminal facilities available:
1.26 anton 2845: After processing the command line, Gforth goes into interactive mode,
2846: and you can give commands to Gforth interactively. The actual facilities
2847: available depend on how you invoke Gforth.
1.14 anton 2848:
2849: @item program data space available:
2850: @code{sp@ here - .} gives the space remaining for dictionary and data
2851: stack together.
2852:
2853: @item return stack space available:
1.26 anton 2854: By default 16 KBytes. The default can be overridden with the @code{-r}
2855: switch (@pxref{Invocation}) when Gforth starts up.
1.14 anton 2856:
2857: @item stack space available:
2858: @code{sp@ here - .} gives the space remaining for dictionary and data
2859: stack together.
2860:
2861: @item system dictionary space required, in address units:
2862: Type @code{here forthstart - .} after startup. At the time of this
2863: writing, this gives 70108 (bytes) on a 32-bit system.
2864: @end table
2865:
2866:
2867: @c =====================================================================
2868: @node The optional Block word set, The optional Double Number word set, The Core Words, ANS conformance
2869: @section The optional Block word set
2870: @c =====================================================================
2871:
2872: @menu
1.15 anton 2873: * block-idef:: Implementation Defined Options
2874: * block-ambcond:: Ambiguous Conditions
2875: * block-other:: Other System Documentation
1.14 anton 2876: @end menu
2877:
2878:
2879: @c ---------------------------------------------------------------------
2880: @node block-idef, block-ambcond, The optional Block word set, The optional Block word set
2881: @subsection Implementation Defined Options
2882: @c ---------------------------------------------------------------------
2883:
2884: @table @i
2885:
2886: @item the format for display by @code{LIST}:
2887: First the screen number is displayed, then 16 lines of 64 characters,
2888: each line preceded by the line number.
2889:
2890: @item the length of a line affected by @code{\}:
2891: 64 characters.
2892: @end table
2893:
2894:
2895: @c ---------------------------------------------------------------------
2896: @node block-ambcond, block-other, block-idef, The optional Block word set
2897: @subsection Ambiguous conditions
2898: @c ---------------------------------------------------------------------
2899:
2900: @table @i
2901:
2902: @item correct block read was not possible:
2903: Typically results in a @code{throw} of some OS-derived value (between
2904: -512 and -2048). If the blocks file was just not long enough, blanks are
2905: supplied for the missing portion.
2906:
2907: @item I/O exception in block transfer:
2908: Typically results in a @code{throw} of some OS-derived value (between
2909: -512 and -2048).
2910:
2911: @item invalid block number:
2912: @code{-35 throw} (Invalid block number)
2913:
2914: @item a program directly alters the contents of @code{BLK}:
2915: The input stream is switched to that other block, at the same
2916: position. If the storing to @code{BLK} happens when interpreting
2917: non-block input, the system will get quite confused when the block ends.
2918:
2919: @item no current block buffer for @code{UPDATE}:
2920: @code{UPDATE} has no effect.
2921:
2922: @end table
2923:
2924:
2925: @c ---------------------------------------------------------------------
2926: @node block-other, , block-ambcond, The optional Block word set
2927: @subsection Other system documentation
2928: @c ---------------------------------------------------------------------
2929:
2930: @table @i
2931:
2932: @item any restrictions a multiprogramming system places on the use of buffer addresses:
2933: No restrictions (yet).
2934:
2935: @item the number of blocks available for source and data:
2936: depends on your disk space.
2937:
2938: @end table
2939:
2940:
2941: @c =====================================================================
2942: @node The optional Double Number word set, The optional Exception word set, The optional Block word set, ANS conformance
2943: @section The optional Double Number word set
2944: @c =====================================================================
2945:
2946: @menu
1.15 anton 2947: * double-ambcond:: Ambiguous Conditions
1.14 anton 2948: @end menu
2949:
2950:
2951: @c ---------------------------------------------------------------------
1.15 anton 2952: @node double-ambcond, , The optional Double Number word set, The optional Double Number word set
1.14 anton 2953: @subsection Ambiguous conditions
2954: @c ---------------------------------------------------------------------
2955:
2956: @table @i
2957:
1.15 anton 2958: @item @var{d} outside of range of @var{n} in @code{D>S}:
1.14 anton 2959: The least significant cell of @var{d} is produced.
2960:
2961: @end table
2962:
2963:
2964: @c =====================================================================
2965: @node The optional Exception word set, The optional Facility word set, The optional Double Number word set, ANS conformance
2966: @section The optional Exception word set
2967: @c =====================================================================
2968:
2969: @menu
1.15 anton 2970: * exception-idef:: Implementation Defined Options
1.14 anton 2971: @end menu
2972:
2973:
2974: @c ---------------------------------------------------------------------
1.15 anton 2975: @node exception-idef, , The optional Exception word set, The optional Exception word set
1.14 anton 2976: @subsection Implementation Defined Options
2977: @c ---------------------------------------------------------------------
2978:
2979: @table @i
2980: @item @code{THROW}-codes used in the system:
2981: The codes -256@minus{}-511 are used for reporting signals (see
2982: @file{errore.fs}). The codes -512@minus{}-2047 are used for OS errors
2983: (for file and memory allocation operations). The mapping from OS error
2984: numbers to throw code is -512@minus{}@var{errno}. One side effect of
2985: this mapping is that undefined OS errors produce a message with a
2986: strange number; e.g., @code{-1000 THROW} results in @code{Unknown error
2987: 488} on my system.
2988: @end table
2989:
2990: @c =====================================================================
2991: @node The optional Facility word set, The optional File-Access word set, The optional Exception word set, ANS conformance
2992: @section The optional Facility word set
2993: @c =====================================================================
2994:
2995: @menu
1.15 anton 2996: * facility-idef:: Implementation Defined Options
2997: * facility-ambcond:: Ambiguous Conditions
1.14 anton 2998: @end menu
2999:
3000:
3001: @c ---------------------------------------------------------------------
3002: @node facility-idef, facility-ambcond, The optional Facility word set, The optional Facility word set
3003: @subsection Implementation Defined Options
3004: @c ---------------------------------------------------------------------
3005:
3006: @table @i
3007:
3008: @item encoding of keyboard events (@code{EKEY}):
3009: Not yet implemeted.
3010:
3011: @item duration of a system clock tick
3012: System dependent. With respect to @code{MS}, the time is specified in
3013: microseconds. How well the OS and the hardware implement this, is
3014: another question.
3015:
3016: @item repeatability to be expected from the execution of @code{MS}:
3017: System dependent. On Unix, a lot depends on load. If the system is
1.17 anton 3018: lightly loaded, and the delay is short enough that Gforth does not get
1.14 anton 3019: swapped out, the performance should be acceptable. Under MS-DOS and
3020: other single-tasking systems, it should be good.
3021:
3022: @end table
3023:
3024:
3025: @c ---------------------------------------------------------------------
1.15 anton 3026: @node facility-ambcond, , facility-idef, The optional Facility word set
1.14 anton 3027: @subsection Ambiguous conditions
3028: @c ---------------------------------------------------------------------
3029:
3030: @table @i
3031:
3032: @item @code{AT-XY} can't be performed on user output device:
3033: Largely terminal dependant. No range checks are done on the arguments.
3034: No errors are reported. You may see some garbage appearing, you may see
3035: simply nothing happen.
3036:
3037: @end table
3038:
3039:
3040: @c =====================================================================
3041: @node The optional File-Access word set, The optional Floating-Point word set, The optional Facility word set, ANS conformance
3042: @section The optional File-Access word set
3043: @c =====================================================================
3044:
3045: @menu
1.15 anton 3046: * file-idef:: Implementation Defined Options
3047: * file-ambcond:: Ambiguous Conditions
1.14 anton 3048: @end menu
3049:
3050:
3051: @c ---------------------------------------------------------------------
3052: @node file-idef, file-ambcond, The optional File-Access word set, The optional File-Access word set
3053: @subsection Implementation Defined Options
3054: @c ---------------------------------------------------------------------
3055:
3056: @table @i
3057:
3058: @item File access methods used:
3059: @code{R/O}, @code{R/W} and @code{BIN} work as you would
3060: expect. @code{W/O} translates into the C file opening mode @code{w} (or
3061: @code{wb}): The file is cleared, if it exists, and created, if it does
1.15 anton 3062: not (both with @code{open-file} and @code{create-file}). Under Unix
1.14 anton 3063: @code{create-file} creates a file with 666 permissions modified by your
3064: umask.
3065:
3066: @item file exceptions:
3067: The file words do not raise exceptions (except, perhaps, memory access
3068: faults when you pass illegal addresses or file-ids).
3069:
3070: @item file line terminator:
3071: System-dependent. Gforth uses C's newline character as line
3072: terminator. What the actual character code(s) of this are is
3073: system-dependent.
3074:
3075: @item file name format
3076: System dependent. Gforth just uses the file name format of your OS.
3077:
3078: @item information returned by @code{FILE-STATUS}:
3079: @code{FILE-STATUS} returns the most powerful file access mode allowed
3080: for the file: Either @code{R/O}, @code{W/O} or @code{R/W}. If the file
3081: cannot be accessed, @code{R/O BIN} is returned. @code{BIN} is applicable
3082: along with the retured mode.
3083:
3084: @item input file state after an exception when including source:
3085: All files that are left via the exception are closed.
3086:
3087: @item @var{ior} values and meaning:
1.15 anton 3088: The @var{ior}s returned by the file and memory allocation words are
3089: intended as throw codes. They typically are in the range
3090: -512@minus{}-2047 of OS errors. The mapping from OS error numbers to
3091: @var{ior}s is -512@minus{}@var{errno}.
1.14 anton 3092:
3093: @item maximum depth of file input nesting:
3094: limited by the amount of return stack, locals/TIB stack, and the number
3095: of open files available. This should not give you troubles.
3096:
3097: @item maximum size of input line:
3098: @code{/line}. Currently 255.
3099:
3100: @item methods of mapping block ranges to files:
3101: Currently, the block words automatically access the file
3102: @file{blocks.fb} in the currend working directory. More sophisticated
3103: methods could be implemented if there is demand (and a volunteer).
3104:
3105: @item number of string buffers provided by @code{S"}:
3106: 1
3107:
3108: @item size of string buffer used by @code{S"}:
3109: @code{/line}. currently 255.
3110:
3111: @end table
3112:
3113: @c ---------------------------------------------------------------------
1.15 anton 3114: @node file-ambcond, , file-idef, The optional File-Access word set
1.14 anton 3115: @subsection Ambiguous conditions
3116: @c ---------------------------------------------------------------------
3117:
3118: @table @i
3119:
3120: @item attempting to position a file outside it's boundaries:
3121: @code{REPOSITION-FILE} is performed as usual: Afterwards,
3122: @code{FILE-POSITION} returns the value given to @code{REPOSITION-FILE}.
3123:
3124: @item attempting to read from file positions not yet written:
3125: End-of-file, i.e., zero characters are read and no error is reported.
3126:
3127: @item @var{file-id} is invalid (@code{INCLUDE-FILE}):
3128: An appropriate exception may be thrown, but a memory fault or other
3129: problem is more probable.
3130:
3131: @item I/O exception reading or closing @var{file-id} (@code{include-file}, @code{included}):
3132: The @var{ior} produced by the operation, that discovered the problem, is
3133: thrown.
3134:
3135: @item named file cannot be opened (@code{included}):
3136: The @var{ior} produced by @code{open-file} is thrown.
3137:
3138: @item requesting an unmapped block number:
3139: There are no unmapped legal block numbers. On some operating systems,
3140: writing a block with a large number may overflow the file system and
3141: have an error message as consequence.
3142:
3143: @item using @code{source-id} when @code{blk} is non-zero:
3144: @code{source-id} performs its function. Typically it will give the id of
3145: the source which loaded the block. (Better ideas?)
3146:
3147: @end table
3148:
3149:
3150: @c =====================================================================
3151: @node The optional Floating-Point word set, The optional Locals word set, The optional File-Access word set, ANS conformance
1.15 anton 3152: @section The optional Floating-Point word set
1.14 anton 3153: @c =====================================================================
3154:
3155: @menu
1.15 anton 3156: * floating-idef:: Implementation Defined Options
3157: * floating-ambcond:: Ambiguous Conditions
1.14 anton 3158: @end menu
3159:
3160:
3161: @c ---------------------------------------------------------------------
3162: @node floating-idef, floating-ambcond, The optional Floating-Point word set, The optional Floating-Point word set
3163: @subsection Implementation Defined Options
3164: @c ---------------------------------------------------------------------
3165:
3166: @table @i
3167:
1.15 anton 3168: @item format and range of floating point numbers:
3169: System-dependent; the @code{double} type of C.
1.14 anton 3170:
1.15 anton 3171: @item results of @code{REPRESENT} when @var{float} is out of range:
3172: System dependent; @code{REPRESENT} is implemented using the C library
3173: function @code{ecvt()} and inherits its behaviour in this respect.
1.14 anton 3174:
1.15 anton 3175: @item rounding or truncation of floating-point numbers:
1.26 anton 3176: System dependent; the rounding behaviour is inherited from the hosting C
3177: compiler. IEEE-FP-based (i.e., most) systems by default round to
3178: nearest, and break ties by rounding to even (i.e., such that the last
3179: bit of the mantissa is 0).
1.14 anton 3180:
1.15 anton 3181: @item size of floating-point stack:
3182: @code{s" FLOATING-STACK" environment? drop .}. Can be changed at startup
3183: with the command-line option @code{-f}.
1.14 anton 3184:
1.15 anton 3185: @item width of floating-point stack:
3186: @code{1 floats}.
1.14 anton 3187:
3188: @end table
3189:
3190:
3191: @c ---------------------------------------------------------------------
1.15 anton 3192: @node floating-ambcond, , floating-idef, The optional Floating-Point word set
3193: @subsection Ambiguous conditions
1.14 anton 3194: @c ---------------------------------------------------------------------
3195:
3196: @table @i
3197:
1.15 anton 3198: @item @code{df@@} or @code{df!} used with an address that is not double-float aligned:
3199: System-dependent. Typically results in an alignment fault like other
3200: alignment violations.
1.14 anton 3201:
1.15 anton 3202: @item @code{f@@} or @code{f!} used with an address that is not float aligned:
3203: System-dependent. Typically results in an alignment fault like other
3204: alignment violations.
1.14 anton 3205:
1.15 anton 3206: @item Floating-point result out of range:
3207: System-dependent. Can result in a @code{-55 THROW} (Floating-point
3208: unidentified fault), or can produce a special value representing, e.g.,
3209: Infinity.
1.14 anton 3210:
1.15 anton 3211: @item @code{sf@@} or @code{sf!} used with an address that is not single-float aligned:
3212: System-dependent. Typically results in an alignment fault like other
3213: alignment violations.
1.14 anton 3214:
1.15 anton 3215: @item BASE is not decimal (@code{REPRESENT}, @code{F.}, @code{FE.}, @code{FS.}):
3216: The floating-point number is converted into decimal nonetheless.
1.14 anton 3217:
1.15 anton 3218: @item Both arguments are equal to zero (@code{FATAN2}):
3219: System-dependent. @code{FATAN2} is implemented using the C library
3220: function @code{atan2()}.
1.14 anton 3221:
1.15 anton 3222: @item Using ftan on an argument @var{r1} where cos(@var{r1}) is zero:
3223: System-dependent. Anyway, typically the cos of @var{r1} will not be zero
3224: because of small errors and the tan will be a very large (or very small)
3225: but finite number.
1.14 anton 3226:
1.15 anton 3227: @item @var{d} cannot be presented precisely as a float in @code{D>F}:
3228: The result is rounded to the nearest float.
1.14 anton 3229:
1.15 anton 3230: @item dividing by zero:
3231: @code{-55 throw} (Floating-point unidentified fault)
1.14 anton 3232:
1.15 anton 3233: @item exponent too big for conversion (@code{DF!}, @code{DF@@}, @code{SF!}, @code{SF@@}):
3234: System dependent. On IEEE-FP based systems the number is converted into
3235: an infinity.
1.14 anton 3236:
1.15 anton 3237: @item @var{float}<1 (@code{facosh}):
3238: @code{-55 throw} (Floating-point unidentified fault)
1.14 anton 3239:
1.15 anton 3240: @item @var{float}=<-1 (@code{flnp1}):
3241: @code{-55 throw} (Floating-point unidentified fault). On IEEE-FP systems
3242: negative infinity is typically produced for @var{float}=-1.
1.14 anton 3243:
1.15 anton 3244: @item @var{float}=<0 (@code{fln}, @code{flog}):
3245: @code{-55 throw} (Floating-point unidentified fault). On IEEE-FP systems
3246: negative infinity is typically produced for @var{float}=0.
1.14 anton 3247:
1.15 anton 3248: @item @var{float}<0 (@code{fasinh}, @code{fsqrt}):
3249: @code{-55 throw} (Floating-point unidentified fault). @code{fasinh}
3250: produces values for these inputs on my Linux box (Bug in the C library?)
1.14 anton 3251:
1.15 anton 3252: @item |@var{float}|>1 (@code{facos}, @code{fasin}, @code{fatanh}):
3253: @code{-55 throw} (Floating-point unidentified fault).
1.14 anton 3254:
1.15 anton 3255: @item integer part of float cannot be represented by @var{d} in @code{f>d}:
3256: @code{-55 throw} (Floating-point unidentified fault).
1.14 anton 3257:
1.15 anton 3258: @item string larger than pictured numeric output area (@code{f.}, @code{fe.}, @code{fs.}):
3259: This does not happen.
3260: @end table
1.14 anton 3261:
3262:
3263:
3264: @c =====================================================================
1.15 anton 3265: @node The optional Locals word set, The optional Memory-Allocation word set, The optional Floating-Point word set, ANS conformance
3266: @section The optional Locals word set
1.14 anton 3267: @c =====================================================================
3268:
3269: @menu
1.15 anton 3270: * locals-idef:: Implementation Defined Options
3271: * locals-ambcond:: Ambiguous Conditions
1.14 anton 3272: @end menu
3273:
3274:
3275: @c ---------------------------------------------------------------------
1.15 anton 3276: @node locals-idef, locals-ambcond, The optional Locals word set, The optional Locals word set
1.14 anton 3277: @subsection Implementation Defined Options
3278: @c ---------------------------------------------------------------------
3279:
3280: @table @i
3281:
1.15 anton 3282: @item maximum number of locals in a definition:
3283: @code{s" #locals" environment? drop .}. Currently 15. This is a lower
3284: bound, e.g., on a 32-bit machine there can be 41 locals of up to 8
3285: characters. The number of locals in a definition is bounded by the size
3286: of locals-buffer, which contains the names of the locals.
1.14 anton 3287:
3288: @end table
3289:
3290:
3291: @c ---------------------------------------------------------------------
1.15 anton 3292: @node locals-ambcond, , locals-idef, The optional Locals word set
1.14 anton 3293: @subsection Ambiguous conditions
3294: @c ---------------------------------------------------------------------
3295:
3296: @table @i
3297:
1.15 anton 3298: @item executing a named local in interpretation state:
3299: @code{-14 throw} (Interpreting a compile-only word).
1.14 anton 3300:
1.15 anton 3301: @item @var{name} not defined by @code{VALUE} or @code{(LOCAL)} (@code{TO}):
3302: @code{-32 throw} (Invalid name argument)
1.14 anton 3303:
3304: @end table
3305:
3306:
3307: @c =====================================================================
1.15 anton 3308: @node The optional Memory-Allocation word set, The optional Programming-Tools word set, The optional Locals word set, ANS conformance
3309: @section The optional Memory-Allocation word set
1.14 anton 3310: @c =====================================================================
3311:
3312: @menu
1.15 anton 3313: * memory-idef:: Implementation Defined Options
1.14 anton 3314: @end menu
3315:
3316:
3317: @c ---------------------------------------------------------------------
1.15 anton 3318: @node memory-idef, , The optional Memory-Allocation word set, The optional Memory-Allocation word set
1.14 anton 3319: @subsection Implementation Defined Options
3320: @c ---------------------------------------------------------------------
3321:
3322: @table @i
3323:
1.15 anton 3324: @item values and meaning of @var{ior}:
3325: The @var{ior}s returned by the file and memory allocation words are
3326: intended as throw codes. They typically are in the range
3327: -512@minus{}-2047 of OS errors. The mapping from OS error numbers to
3328: @var{ior}s is -512@minus{}@var{errno}.
1.14 anton 3329:
3330: @end table
3331:
3332: @c =====================================================================
1.15 anton 3333: @node The optional Programming-Tools word set, The optional Search-Order word set, The optional Memory-Allocation word set, ANS conformance
3334: @section The optional Programming-Tools word set
1.14 anton 3335: @c =====================================================================
3336:
3337: @menu
1.15 anton 3338: * programming-idef:: Implementation Defined Options
3339: * programming-ambcond:: Ambiguous Conditions
1.14 anton 3340: @end menu
3341:
3342:
3343: @c ---------------------------------------------------------------------
1.15 anton 3344: @node programming-idef, programming-ambcond, The optional Programming-Tools word set, The optional Programming-Tools word set
1.14 anton 3345: @subsection Implementation Defined Options
3346: @c ---------------------------------------------------------------------
3347:
3348: @table @i
3349:
1.15 anton 3350: @item ending sequence for input following @code{;code} and @code{code}:
3351: Not implemented (yet).
1.14 anton 3352:
1.15 anton 3353: @item manner of processing input following @code{;code} and @code{code}:
3354: Not implemented (yet).
3355:
3356: @item search order capability for @code{EDITOR} and @code{ASSEMBLER}:
3357: Not implemented (yet). If they were implemented, they would use the
3358: search order wordset.
3359:
3360: @item source and format of display by @code{SEE}:
3361: The source for @code{see} is the intermediate code used by the inner
3362: interpreter. The current @code{see} tries to output Forth source code
3363: as well as possible.
3364:
1.14 anton 3365: @end table
3366:
3367: @c ---------------------------------------------------------------------
1.15 anton 3368: @node programming-ambcond, , programming-idef, The optional Programming-Tools word set
1.14 anton 3369: @subsection Ambiguous conditions
3370: @c ---------------------------------------------------------------------
3371:
3372: @table @i
3373:
1.15 anton 3374: @item deleting the compilation wordlist (@code{FORGET}):
3375: Not implemented (yet).
1.14 anton 3376:
1.15 anton 3377: @item fewer than @var{u}+1 items on the control flow stack (@code{CS-PICK}, @code{CS-ROLL}):
3378: This typically results in an @code{abort"} with a descriptive error
3379: message (may change into a @code{-22 throw} (Control structure mismatch)
3380: in the future). You may also get a memory access error. If you are
3381: unlucky, this ambiguous condition is not caught.
3382:
3383: @item @var{name} can't be found (@code{forget}):
3384: Not implemented (yet).
1.14 anton 3385:
1.15 anton 3386: @item @var{name} not defined via @code{CREATE}:
3387: @code{;code} is not implemented (yet). If it were, it would behave like
3388: @code{DOES>} in this respect, i.e., change the execution semantics of
3389: the last defined word no matter how it was defined.
1.14 anton 3390:
1.15 anton 3391: @item @code{POSTPONE} applied to @code{[IF]}:
3392: After defining @code{: X POSTPONE [IF] ; IMMEDIATE}. @code{X} is
3393: equivalent to @code{[IF]}.
1.14 anton 3394:
1.15 anton 3395: @item reaching the end of the input source before matching @code{[ELSE]} or @code{[THEN]}:
3396: Continue in the same state of conditional compilation in the next outer
3397: input source. Currently there is no warning to the user about this.
1.14 anton 3398:
1.15 anton 3399: @item removing a needed definition (@code{FORGET}):
3400: Not implemented (yet).
1.14 anton 3401:
3402: @end table
3403:
3404:
3405: @c =====================================================================
1.15 anton 3406: @node The optional Search-Order word set, , The optional Programming-Tools word set, ANS conformance
3407: @section The optional Search-Order word set
1.14 anton 3408: @c =====================================================================
3409:
3410: @menu
1.15 anton 3411: * search-idef:: Implementation Defined Options
3412: * search-ambcond:: Ambiguous Conditions
1.14 anton 3413: @end menu
3414:
3415:
3416: @c ---------------------------------------------------------------------
1.15 anton 3417: @node search-idef, search-ambcond, The optional Search-Order word set, The optional Search-Order word set
1.14 anton 3418: @subsection Implementation Defined Options
3419: @c ---------------------------------------------------------------------
3420:
3421: @table @i
3422:
1.15 anton 3423: @item maximum number of word lists in search order:
3424: @code{s" wordlists" environment? drop .}. Currently 16.
3425:
3426: @item minimum search order:
3427: @code{root root}.
1.14 anton 3428:
3429: @end table
3430:
3431: @c ---------------------------------------------------------------------
1.15 anton 3432: @node search-ambcond, , search-idef, The optional Search-Order word set
1.14 anton 3433: @subsection Ambiguous conditions
3434: @c ---------------------------------------------------------------------
3435:
3436: @table @i
3437:
1.15 anton 3438: @item changing the compilation wordlist (during compilation):
1.33 anton 3439: The word is entered into the wordlist that was the compilation wordlist
3440: at the start of the definition. Any changes to the name field (e.g.,
3441: @code{immediate}) or the code field (e.g., when executing @code{DOES>})
3442: are applied to the latest defined word (as reported by @code{last} or
3443: @code{lastxt}), if possible, irrespective of the compilation wordlist.
1.14 anton 3444:
1.15 anton 3445: @item search order empty (@code{previous}):
3446: @code{abort" Vocstack empty"}.
1.14 anton 3447:
1.15 anton 3448: @item too many word lists in search order (@code{also}):
3449: @code{abort" Vocstack full"}.
1.14 anton 3450:
3451: @end table
1.13 anton 3452:
1.34 anton 3453: @node Model, Integrating Gforth, ANS conformance, Top
3454: @chapter Model
3455:
3456: This chapter has yet to be written. It will contain information, on
3457: which internal structures you can rely.
3458:
3459: @node Integrating Gforth, Emacs and Gforth, Model, Top
3460: @chapter Integrating Gforth into C programs
3461:
3462: This is not yet implemented.
3463:
3464: Several people like to use Forth as scripting language for applications
3465: that are otherwise written in C, C++, or some other language.
3466:
3467: The Forth system ATLAST provides facilities for embedding it into
3468: applications; unfortunately it has several disadvantages: most
3469: implorantly, it is not based on ANS Forth, and it is apparently dead
3470: (i.e., not developed further and not supported). The facilities
3471: provided by Gforth in this area are inspired by ATLASTs facilities, so
3472: making the switch should not be hard.
3473:
3474: We also tried to design the interface such that it can easily be
3475: implemented by other Forth systems, so that we may one day arrive at a
3476: standardized interface. Such a standard interface would allow you to
3477: replace the Forth system without having to rewrite C code.
3478:
3479: You embed the Gforth interpreter by linking with the library
3480: @code{libgforth.a} (give the compiler the option @code{-lgforth}). All
3481: global symbols in this library that belong to the interface, have the
3482: prefix @code{forth_}. (Global symbols that are used internally have the
3483: prefix @code{gforth_}).
3484:
3485: You can include the declarations of Forth types and the functions and
3486: variables of the interface with @code{include <forth.h>}.
3487:
3488: Types.
1.13 anton 3489:
1.34 anton 3490: Variables.
3491:
3492: Data and FP Stack pointer. Area sizes.
3493:
3494: functions.
3495:
3496: forth_init(imagefile)
3497: forth_evaluate(string) exceptions?
3498: forth_goto(address) (or forth_execute(xt)?)
3499: forth_continue() (a corountining mechanism)
3500:
3501: Adding primitives.
3502:
3503: No checking.
3504:
3505: Signals?
3506:
3507: Accessing the Stacks
1.4 anton 3508:
1.34 anton 3509: @node Emacs and Gforth, Internals, Integrating Gforth, Top
1.17 anton 3510: @chapter Emacs and Gforth
1.4 anton 3511:
1.17 anton 3512: Gforth comes with @file{gforth.el}, an improved version of
1.33 anton 3513: @file{forth.el} by Goran Rydqvist (included in the TILE package). The
1.4 anton 3514: improvements are a better (but still not perfect) handling of
3515: indentation. I have also added comment paragraph filling (@kbd{M-q}),
1.8 anton 3516: commenting (@kbd{C-x \}) and uncommenting (@kbd{C-u C-x \}) regions and
3517: removing debugging tracers (@kbd{C-x ~}, @pxref{Debugging}). I left the
3518: stuff I do not use alone, even though some of it only makes sense for
3519: TILE. To get a description of these features, enter Forth mode and type
3520: @kbd{C-h m}.
1.4 anton 3521:
1.17 anton 3522: In addition, Gforth supports Emacs quite well: The source code locations
1.4 anton 3523: given in error messages, debugging output (from @code{~~}) and failed
3524: assertion messages are in the right format for Emacs' compilation mode
3525: (@pxref{Compilation, , Running Compilations under Emacs, emacs, Emacs
3526: Manual}) so the source location corresponding to an error or other
3527: message is only a few keystrokes away (@kbd{C-x `} for the next error,
3528: @kbd{C-c C-c} for the error under the cursor).
3529:
3530: Also, if you @code{include} @file{etags.fs}, a new @file{TAGS} file
3531: (@pxref{Tags, , Tags Tables, emacs, Emacs Manual}) will be produced that
3532: contains the definitions of all words defined afterwards. You can then
3533: find the source for a word using @kbd{M-.}. Note that emacs can use
1.17 anton 3534: several tags files at the same time (e.g., one for the Gforth sources
1.28 anton 3535: and one for your program, @pxref{Select Tags Table,,Selecting a Tags
3536: Table,emacs, Emacs Manual}). The TAGS file for the preloaded words is
3537: @file{$(datadir)/gforth/$(VERSION)/TAGS} (e.g.,
1.33 anton 3538: @file{/usr/local/share/gforth/0.2.0/TAGS}).
1.4 anton 3539:
3540: To get all these benefits, add the following lines to your @file{.emacs}
3541: file:
3542:
3543: @example
3544: (autoload 'forth-mode "gforth.el")
3545: (setq auto-mode-alist (cons '("\\.fs\\'" . forth-mode) auto-mode-alist))
3546: @end example
3547:
1.17 anton 3548: @node Internals, Bugs, Emacs and Gforth, Top
1.3 anton 3549: @chapter Internals
3550:
1.17 anton 3551: Reading this section is not necessary for programming with Gforth. It
3552: should be helpful for finding your way in the Gforth sources.
1.3 anton 3553:
1.24 anton 3554: The ideas in this section have also been published in the papers
3555: @cite{ANS fig/GNU/??? Forth} (in German) by Bernd Paysan, presented at
3556: the Forth-Tagung '93 and @cite{A Portable Forth Engine} by M. Anton
3557: Ertl, presented at EuroForth '93; the latter is available at
3558: @*@file{http://www.complang.tuwien.ac.at/papers/ertl93.ps.Z}.
3559:
1.4 anton 3560: @menu
3561: * Portability::
3562: * Threading::
3563: * Primitives::
3564: * System Architecture::
1.17 anton 3565: * Performance::
1.4 anton 3566: @end menu
3567:
3568: @node Portability, Threading, Internals, Internals
1.3 anton 3569: @section Portability
3570:
3571: One of the main goals of the effort is availability across a wide range
3572: of personal machines. fig-Forth, and, to a lesser extent, F83, achieved
3573: this goal by manually coding the engine in assembly language for several
3574: then-popular processors. This approach is very labor-intensive and the
3575: results are short-lived due to progress in computer architecture.
3576:
3577: Others have avoided this problem by coding in C, e.g., Mitch Bradley
3578: (cforth), Mikael Patel (TILE) and Dirk Zoller (pfe). This approach is
3579: particularly popular for UNIX-based Forths due to the large variety of
3580: architectures of UNIX machines. Unfortunately an implementation in C
3581: does not mix well with the goals of efficiency and with using
3582: traditional techniques: Indirect or direct threading cannot be expressed
3583: in C, and switch threading, the fastest technique available in C, is
3584: significantly slower. Another problem with C is that it's very
3585: cumbersome to express double integer arithmetic.
3586:
3587: Fortunately, there is a portable language that does not have these
3588: limitations: GNU C, the version of C processed by the GNU C compiler
3589: (@pxref{C Extensions, , Extensions to the C Language Family, gcc.info,
3590: GNU C Manual}). Its labels as values feature (@pxref{Labels as Values, ,
3591: Labels as Values, gcc.info, GNU C Manual}) makes direct and indirect
3592: threading possible, its @code{long long} type (@pxref{Long Long, ,
1.33 anton 3593: Double-Word Integers, gcc.info, GNU C Manual}) corresponds to Forth's
1.32 anton 3594: double numbers@footnote{Unfortunately, long longs are not implemented
3595: properly on all machines (e.g., on alpha-osf1, long longs are only 64
3596: bits, the same size as longs (and pointers), but they should be twice as
3597: long according to @ref{Long Long, , Double-Word Integers, gcc.info, GNU
3598: C Manual}). So, we had to implement doubles in C after all. Still, on
3599: most machines we can use long longs and achieve better performance than
3600: with the emulation package.}. GNU C is available for free on all
3601: important (and many unimportant) UNIX machines, VMS, 80386s running
3602: MS-DOS, the Amiga, and the Atari ST, so a Forth written in GNU C can run
3603: on all these machines.
1.3 anton 3604:
3605: Writing in a portable language has the reputation of producing code that
3606: is slower than assembly. For our Forth engine we repeatedly looked at
3607: the code produced by the compiler and eliminated most compiler-induced
3608: inefficiencies by appropriate changes in the source-code.
3609:
3610: However, register allocation cannot be portably influenced by the
3611: programmer, leading to some inefficiencies on register-starved
3612: machines. We use explicit register declarations (@pxref{Explicit Reg
3613: Vars, , Variables in Specified Registers, gcc.info, GNU C Manual}) to
3614: improve the speed on some machines. They are turned on by using the
3615: @code{gcc} switch @code{-DFORCE_REG}. Unfortunately, this feature not
3616: only depends on the machine, but also on the compiler version: On some
3617: machines some compiler versions produce incorrect code when certain
3618: explicit register declarations are used. So by default
3619: @code{-DFORCE_REG} is not used.
3620:
1.4 anton 3621: @node Threading, Primitives, Portability, Internals
1.3 anton 3622: @section Threading
3623:
3624: GNU C's labels as values extension (available since @code{gcc-2.0},
3625: @pxref{Labels as Values, , Labels as Values, gcc.info, GNU C Manual})
3626: makes it possible to take the address of @var{label} by writing
3627: @code{&&@var{label}}. This address can then be used in a statement like
3628: @code{goto *@var{address}}. I.e., @code{goto *&&x} is the same as
3629: @code{goto x}.
3630:
3631: With this feature an indirect threaded NEXT looks like:
3632: @example
3633: cfa = *ip++;
3634: ca = *cfa;
3635: goto *ca;
3636: @end example
3637: For those unfamiliar with the names: @code{ip} is the Forth instruction
3638: pointer; the @code{cfa} (code-field address) corresponds to ANS Forths
3639: execution token and points to the code field of the next word to be
3640: executed; The @code{ca} (code address) fetched from there points to some
3641: executable code, e.g., a primitive or the colon definition handler
3642: @code{docol}.
3643:
3644: Direct threading is even simpler:
3645: @example
3646: ca = *ip++;
3647: goto *ca;
3648: @end example
3649:
3650: Of course we have packaged the whole thing neatly in macros called
3651: @code{NEXT} and @code{NEXT1} (the part of NEXT after fetching the cfa).
3652:
1.4 anton 3653: @menu
3654: * Scheduling::
3655: * Direct or Indirect Threaded?::
3656: * DOES>::
3657: @end menu
3658:
3659: @node Scheduling, Direct or Indirect Threaded?, Threading, Threading
1.3 anton 3660: @subsection Scheduling
3661:
3662: There is a little complication: Pipelined and superscalar processors,
3663: i.e., RISC and some modern CISC machines can process independent
3664: instructions while waiting for the results of an instruction. The
3665: compiler usually reorders (schedules) the instructions in a way that
3666: achieves good usage of these delay slots. However, on our first tries
3667: the compiler did not do well on scheduling primitives. E.g., for
3668: @code{+} implemented as
3669: @example
3670: n=sp[0]+sp[1];
3671: sp++;
3672: sp[0]=n;
3673: NEXT;
3674: @end example
3675: the NEXT comes strictly after the other code, i.e., there is nearly no
3676: scheduling. After a little thought the problem becomes clear: The
3677: compiler cannot know that sp and ip point to different addresses (and
1.4 anton 3678: the version of @code{gcc} we used would not know it even if it was
3679: possible), so it could not move the load of the cfa above the store to
3680: the TOS. Indeed the pointers could be the same, if code on or very near
3681: the top of stack were executed. In the interest of speed we chose to
3682: forbid this probably unused ``feature'' and helped the compiler in
3683: scheduling: NEXT is divided into the loading part (@code{NEXT_P1}) and
3684: the goto part (@code{NEXT_P2}). @code{+} now looks like:
1.3 anton 3685: @example
3686: n=sp[0]+sp[1];
3687: sp++;
3688: NEXT_P1;
3689: sp[0]=n;
3690: NEXT_P2;
3691: @end example
1.4 anton 3692: This can be scheduled optimally by the compiler.
1.3 anton 3693:
3694: This division can be turned off with the switch @code{-DCISC_NEXT}. This
3695: switch is on by default on machines that do not profit from scheduling
3696: (e.g., the 80386), in order to preserve registers.
3697:
1.4 anton 3698: @node Direct or Indirect Threaded?, DOES>, Scheduling, Threading
1.3 anton 3699: @subsection Direct or Indirect Threaded?
3700:
3701: Both! After packaging the nasty details in macro definitions we
3702: realized that we could switch between direct and indirect threading by
3703: simply setting a compilation flag (@code{-DDIRECT_THREADED}) and
3704: defining a few machine-specific macros for the direct-threading case.
3705: On the Forth level we also offer access words that hide the
3706: differences between the threading methods (@pxref{Threading Words}).
3707:
3708: Indirect threading is implemented completely
3709: machine-independently. Direct threading needs routines for creating
3710: jumps to the executable code (e.g. to docol or dodoes). These routines
3711: are inherently machine-dependent, but they do not amount to many source
3712: lines. I.e., even porting direct threading to a new machine is a small
3713: effort.
3714:
1.4 anton 3715: @node DOES>, , Direct or Indirect Threaded?, Threading
1.3 anton 3716: @subsection DOES>
3717: One of the most complex parts of a Forth engine is @code{dodoes}, i.e.,
3718: the chunk of code executed by every word defined by a
3719: @code{CREATE}...@code{DOES>} pair. The main problem here is: How to find
3720: the Forth code to be executed, i.e. the code after the @code{DOES>} (the
3721: DOES-code)? There are two solutions:
3722:
3723: In fig-Forth the code field points directly to the dodoes and the
3724: DOES-code address is stored in the cell after the code address
3725: (i.e. at cfa cell+). It may seem that this solution is illegal in the
3726: Forth-79 and all later standards, because in fig-Forth this address
3727: lies in the body (which is illegal in these standards). However, by
3728: making the code field larger for all words this solution becomes legal
3729: again. We use this approach for the indirect threaded version. Leaving
3730: a cell unused in most words is a bit wasteful, but on the machines we
3731: are targetting this is hardly a problem. The other reason for having a
3732: code field size of two cells is to avoid having different image files
1.4 anton 3733: for direct and indirect threaded systems (@pxref{System Architecture}).
1.3 anton 3734:
3735: The other approach is that the code field points or jumps to the cell
3736: after @code{DOES}. In this variant there is a jump to @code{dodoes} at
3737: this address. @code{dodoes} can then get the DOES-code address by
3738: computing the code address, i.e., the address of the jump to dodoes,
3739: and add the length of that jump field. A variant of this is to have a
3740: call to @code{dodoes} after the @code{DOES>}; then the return address
3741: (which can be found in the return register on RISCs) is the DOES-code
3742: address. Since the two cells available in the code field are usually
3743: used up by the jump to the code address in direct threading, we use
3744: this approach for direct threading. We did not want to add another
3745: cell to the code field.
3746:
1.4 anton 3747: @node Primitives, System Architecture, Threading, Internals
1.3 anton 3748: @section Primitives
3749:
1.4 anton 3750: @menu
3751: * Automatic Generation::
3752: * TOS Optimization::
3753: * Produced code::
3754: @end menu
3755:
3756: @node Automatic Generation, TOS Optimization, Primitives, Primitives
1.3 anton 3757: @subsection Automatic Generation
3758:
3759: Since the primitives are implemented in a portable language, there is no
3760: longer any need to minimize the number of primitives. On the contrary,
3761: having many primitives is an advantage: speed. In order to reduce the
3762: number of errors in primitives and to make programming them easier, we
3763: provide a tool, the primitive generator (@file{prims2x.fs}), that
3764: automatically generates most (and sometimes all) of the C code for a
3765: primitive from the stack effect notation. The source for a primitive
3766: has the following form:
3767:
3768: @format
3769: @var{Forth-name} @var{stack-effect} @var{category} [@var{pronounc.}]
3770: [@code{""}@var{glossary entry}@code{""}]
3771: @var{C code}
3772: [@code{:}
3773: @var{Forth code}]
3774: @end format
3775:
3776: The items in brackets are optional. The category and glossary fields
3777: are there for generating the documentation, the Forth code is there
3778: for manual implementations on machines without GNU C. E.g., the source
3779: for the primitive @code{+} is:
3780: @example
3781: + n1 n2 -- n core plus
3782: n = n1+n2;
3783: @end example
3784:
3785: This looks like a specification, but in fact @code{n = n1+n2} is C
3786: code. Our primitive generation tool extracts a lot of information from
3787: the stack effect notations@footnote{We use a one-stack notation, even
3788: though we have separate data and floating-point stacks; The separate
3789: notation can be generated easily from the unified notation.}: The number
3790: of items popped from and pushed on the stack, their type, and by what
3791: name they are referred to in the C code. It then generates a C code
3792: prelude and postlude for each primitive. The final C code for @code{+}
3793: looks like this:
3794:
3795: @example
3796: I_plus: /* + ( n1 n2 -- n ) */ /* label, stack effect */
3797: /* */ /* documentation */
1.4 anton 3798: @{
1.3 anton 3799: DEF_CA /* definition of variable ca (indirect threading) */
3800: Cell n1; /* definitions of variables */
3801: Cell n2;
3802: Cell n;
3803: n1 = (Cell) sp[1]; /* input */
3804: n2 = (Cell) TOS;
3805: sp += 1; /* stack adjustment */
3806: NAME("+") /* debugging output (with -DDEBUG) */
1.4 anton 3807: @{
1.3 anton 3808: n = n1+n2; /* C code taken from the source */
1.4 anton 3809: @}
1.3 anton 3810: NEXT_P1; /* NEXT part 1 */
3811: TOS = (Cell)n; /* output */
3812: NEXT_P2; /* NEXT part 2 */
1.4 anton 3813: @}
1.3 anton 3814: @end example
3815:
3816: This looks long and inefficient, but the GNU C compiler optimizes quite
3817: well and produces optimal code for @code{+} on, e.g., the R3000 and the
3818: HP RISC machines: Defining the @code{n}s does not produce any code, and
3819: using them as intermediate storage also adds no cost.
3820:
3821: There are also other optimizations, that are not illustrated by this
3822: example: Assignments between simple variables are usually for free (copy
3823: propagation). If one of the stack items is not used by the primitive
3824: (e.g. in @code{drop}), the compiler eliminates the load from the stack
3825: (dead code elimination). On the other hand, there are some things that
3826: the compiler does not do, therefore they are performed by
3827: @file{prims2x.fs}: The compiler does not optimize code away that stores
3828: a stack item to the place where it just came from (e.g., @code{over}).
3829:
3830: While programming a primitive is usually easy, there are a few cases
3831: where the programmer has to take the actions of the generator into
3832: account, most notably @code{?dup}, but also words that do not (always)
3833: fall through to NEXT.
3834:
1.4 anton 3835: @node TOS Optimization, Produced code, Automatic Generation, Primitives
1.3 anton 3836: @subsection TOS Optimization
3837:
3838: An important optimization for stack machine emulators, e.g., Forth
3839: engines, is keeping one or more of the top stack items in
1.4 anton 3840: registers. If a word has the stack effect @var{in1}...@var{inx} @code{--}
3841: @var{out1}...@var{outy}, keeping the top @var{n} items in registers
1.34 anton 3842: @itemize @bullet
1.3 anton 3843: @item
3844: is better than keeping @var{n-1} items, if @var{x>=n} and @var{y>=n},
3845: due to fewer loads from and stores to the stack.
3846: @item is slower than keeping @var{n-1} items, if @var{x<>y} and @var{x<n} and
3847: @var{y<n}, due to additional moves between registers.
3848: @end itemize
3849:
3850: In particular, keeping one item in a register is never a disadvantage,
3851: if there are enough registers. Keeping two items in registers is a
3852: disadvantage for frequent words like @code{?branch}, constants,
3853: variables, literals and @code{i}. Therefore our generator only produces
3854: code that keeps zero or one items in registers. The generated C code
3855: covers both cases; the selection between these alternatives is made at
3856: C-compile time using the switch @code{-DUSE_TOS}. @code{TOS} in the C
3857: code for @code{+} is just a simple variable name in the one-item case,
3858: otherwise it is a macro that expands into @code{sp[0]}. Note that the
3859: GNU C compiler tries to keep simple variables like @code{TOS} in
3860: registers, and it usually succeeds, if there are enough registers.
3861:
3862: The primitive generator performs the TOS optimization for the
3863: floating-point stack, too (@code{-DUSE_FTOS}). For floating-point
3864: operations the benefit of this optimization is even larger:
3865: floating-point operations take quite long on most processors, but can be
3866: performed in parallel with other operations as long as their results are
3867: not used. If the FP-TOS is kept in a register, this works. If
3868: it is kept on the stack, i.e., in memory, the store into memory has to
3869: wait for the result of the floating-point operation, lengthening the
3870: execution time of the primitive considerably.
3871:
3872: The TOS optimization makes the automatic generation of primitives a
3873: bit more complicated. Just replacing all occurrences of @code{sp[0]} by
3874: @code{TOS} is not sufficient. There are some special cases to
3875: consider:
1.34 anton 3876: @itemize @bullet
1.3 anton 3877: @item In the case of @code{dup ( w -- w w )} the generator must not
3878: eliminate the store to the original location of the item on the stack,
3879: if the TOS optimization is turned on.
1.4 anton 3880: @item Primitives with stack effects of the form @code{--}
3881: @var{out1}...@var{outy} must store the TOS to the stack at the start.
3882: Likewise, primitives with the stack effect @var{in1}...@var{inx} @code{--}
1.3 anton 3883: must load the TOS from the stack at the end. But for the null stack
3884: effect @code{--} no stores or loads should be generated.
3885: @end itemize
3886:
1.4 anton 3887: @node Produced code, , TOS Optimization, Primitives
1.3 anton 3888: @subsection Produced code
3889:
3890: To see what assembly code is produced for the primitives on your machine
3891: with your compiler and your flag settings, type @code{make engine.s} and
1.4 anton 3892: look at the resulting file @file{engine.s}.
1.3 anton 3893:
1.17 anton 3894: @node System Architecture, Performance, Primitives, Internals
1.3 anton 3895: @section System Architecture
3896:
3897: Our Forth system consists not only of primitives, but also of
3898: definitions written in Forth. Since the Forth compiler itself belongs
3899: to those definitions, it is not possible to start the system with the
3900: primitives and the Forth source alone. Therefore we provide the Forth
3901: code as an image file in nearly executable form. At the start of the
3902: system a C routine loads the image file into memory, sets up the
3903: memory (stacks etc.) according to information in the image file, and
3904: starts executing Forth code.
3905:
3906: The image file format is a compromise between the goals of making it
3907: easy to generate image files and making them portable. The easiest way
3908: to generate an image file is to just generate a memory dump. However,
3909: this kind of image file cannot be used on a different machine, or on
3910: the next version of the engine on the same machine, it even might not
3911: work with the same engine compiled by a different version of the C
3912: compiler. We would like to have as few versions of the image file as
3913: possible, because we do not want to distribute many versions of the
3914: same image file, and to make it easy for the users to use their image
3915: files on many machines. We currently need to create a different image
3916: file for machines with different cell sizes and different byte order
1.17 anton 3917: (little- or big-endian)@footnote{We are considering adding information to the
1.3 anton 3918: image file that enables the loader to change the byte order.}.
3919:
3920: Forth code that is going to end up in a portable image file has to
1.4 anton 3921: comply to some restrictions: addresses have to be stored in memory with
3922: special words (@code{A!}, @code{A,}, etc.) in order to make the code
3923: relocatable. Cells, floats, etc., have to be stored at the natural
3924: alignment boundaries@footnote{E.g., store floats (8 bytes) at an address
3925: dividable by~8. This happens automatically in our system when you use
3926: the ANS Forth alignment words.}, in order to avoid alignment faults on
3927: machines with stricter alignment. The image file is produced by a
3928: metacompiler (@file{cross.fs}).
1.3 anton 3929:
3930: So, unlike the image file of Mitch Bradleys @code{cforth}, our image
3931: file is not directly executable, but has to undergo some manipulations
3932: during loading. Address relocation is performed at image load-time, not
3933: at run-time. The loader also has to replace tokens standing for
3934: primitive calls with the appropriate code-field addresses (or code
3935: addresses in the case of direct threading).
1.4 anton 3936:
1.17 anton 3937: @node Performance, , System Architecture, Internals
3938: @section Performance
3939:
3940: On RISCs the Gforth engine is very close to optimal; i.e., it is usually
3941: impossible to write a significantly faster engine.
3942:
3943: On register-starved machines like the 386 architecture processors
3944: improvements are possible, because @code{gcc} does not utilize the
3945: registers as well as a human, even with explicit register declarations;
3946: e.g., Bernd Beuster wrote a Forth system fragment in assembly language
3947: and hand-tuned it for the 486; this system is 1.19 times faster on the
3948: Sieve benchmark on a 486DX2/66 than Gforth compiled with
3949: @code{gcc-2.6.3} with @code{-DFORCE_REG}.
3950:
3951: However, this potential advantage of assembly language implementations
3952: is not necessarily realized in complete Forth systems: We compared
1.26 anton 3953: Gforth (direct threaded, compiled with @code{gcc-2.6.3} and
3954: @code{-DFORCE_REG}) with Win32Forth 1.2093, LMI's NT Forth (Beta, May
3955: 1994) and Eforth (with and without peephole (aka pinhole) optimization
3956: of the threaded code); all these systems were written in assembly
1.30 anton 3957: language. We also compared Gforth with three systems written in C:
1.32 anton 3958: PFE-0.9.14 (compiled with @code{gcc-2.6.3} with the default
3959: configuration for Linux: @code{-O2 -fomit-frame-pointer -DUSE_REGS
3960: -DUNROLL_NEXT}), ThisForth Beta (compiled with gcc-2.6.3 -O3
3961: -fomit-frame-pointer; ThisForth employs peephole optimization of the
3962: threaded code) and TILE (compiled with @code{make opt}). We benchmarked
3963: Gforth, PFE, ThisForth and TILE on a 486DX2/66 under Linux. Kenneth
3964: O'Heskin kindly provided the results for Win32Forth and NT Forth on a
3965: 486DX2/66 with similar memory performance under Windows NT. Marcel
3966: Hendrix ported Eforth to Linux, then extended it to run the benchmarks,
3967: added the peephole optimizer, ran the benchmarks and reported the
3968: results.
1.17 anton 3969:
3970: We used four small benchmarks: the ubiquitous Sieve; bubble-sorting and
3971: matrix multiplication come from the Stanford integer benchmarks and have
3972: been translated into Forth by Martin Fraeman; we used the versions
1.30 anton 3973: included in the TILE Forth package, but with bigger data set sizes; and
3974: a recursive Fibonacci number computation for benchmarking calling
3975: performance. The following table shows the time taken for the benchmarks
3976: scaled by the time taken by Gforth (in other words, it shows the speedup
3977: factor that Gforth achieved over the other systems).
1.17 anton 3978:
3979: @example
1.30 anton 3980: relative Win32- NT eforth This-
3981: time Gforth Forth Forth eforth +opt PFE Forth TILE
1.32 anton 3982: sieve 1.00 1.39 1.14 1.39 0.85 1.58 3.18 8.58
3983: bubble 1.00 1.31 1.41 1.48 0.88 1.50 3.88
3984: matmul 1.00 1.47 1.35 1.46 1.16 1.58 4.09
3985: fib 1.00 1.52 1.34 1.22 1.13 1.74 2.99 4.30
1.17 anton 3986: @end example
3987:
3988: You may find the good performance of Gforth compared with the systems
3989: written in assembly language quite surprising. One important reason for
3990: the disappointing performance of these systems is probably that they are
3991: not written optimally for the 486 (e.g., they use the @code{lods}
3992: instruction). In addition, Win32Forth uses a comfortable, but costly
3993: method for relocating the Forth image: like @code{cforth}, it computes
3994: the actual addresses at run time, resulting in two address computations
3995: per NEXT (@pxref{System Architecture}).
3996:
1.26 anton 3997: Only Eforth with the peephole optimizer performs comparable to
3998: Gforth. The speedups achieved with peephole optimization of threaded
3999: code are quite remarkable. Adding a peephole optimizer to Gforth should
4000: cause similar speedups.
4001:
1.30 anton 4002: The speedup of Gforth over PFE, ThisForth and TILE can be easily
4003: explained with the self-imposed restriction to standard C, which makes
4004: efficient threading impossible (however, the measured implementation of
4005: PFE uses a GNU C extension: @ref{Global Reg Vars, , Defining Global
4006: Register Variables, gcc.info, GNU C Manual}). Moreover, current C
4007: compilers have a hard time optimizing other aspects of the ThisForth
4008: and the TILE source.
1.17 anton 4009:
4010: Note that the performance of Gforth on 386 architecture processors
4011: varies widely with the version of @code{gcc} used. E.g., @code{gcc-2.5.8}
4012: failed to allocate any of the virtual machine registers into real
4013: machine registers by itself and would not work correctly with explicit
4014: register declarations, giving a 1.3 times slower engine (on a 486DX2/66
4015: running the Sieve) than the one measured above.
4016:
1.26 anton 4017: In @cite{Translating Forth to Efficient C} by M. Anton Ertl and Martin
4018: Maierhofer (presented at EuroForth '95), an indirect threaded version of
4019: Gforth is compared with Win32Forth, NT Forth, PFE, and ThisForth; that
4020: version of Gforth is 2\%@minus{}8\% slower on a 486 than the version
4021: used here. The paper available at
1.24 anton 4022: @*@file{http://www.complang.tuwien.ac.at/papers/ertl&maierhofer95.ps.gz};
4023: it also contains numbers for some native code systems. You can find
4024: numbers for Gforth on various machines in @file{Benchres}.
4025:
1.29 anton 4026: @node Bugs, Origin, Internals, Top
1.4 anton 4027: @chapter Bugs
4028:
1.17 anton 4029: Known bugs are described in the file BUGS in the Gforth distribution.
4030:
1.24 anton 4031: If you find a bug, please send a bug report to
1.32 anton 4032: @code{bug-gforth@@gnu.ai.mit.edu}. A bug report should
1.17 anton 4033: describe the Gforth version used (it is announced at the start of an
4034: interactive Gforth session), the machine and operating system (on Unix
4035: systems you can use @code{uname -a} to produce this information), the
1.24 anton 4036: installation options (send the @code{config.status} file), and a
4037: complete list of changes you (or your installer) have made to the Gforth
4038: sources (if any); it should contain a program (or a sequence of keyboard
4039: commands) that reproduces the bug and a description of what you think
4040: constitutes the buggy behaviour.
1.17 anton 4041:
4042: For a thorough guide on reporting bugs read @ref{Bug Reporting, , How
4043: to Report Bugs, gcc.info, GNU C Manual}.
4044:
4045:
1.29 anton 4046: @node Origin, Word Index, Bugs, Top
4047: @chapter Authors and Ancestors of Gforth
4048:
4049: @section Authors and Contributors
4050:
4051: The Gforth project was started in mid-1992 by Bernd Paysan and Anton
1.30 anton 4052: Ertl. The third major author was Jens Wilke. Lennart Benschop (who was
4053: one of Gforth's first users, in mid-1993) and Stuart Ramsden inspired us
4054: with their continuous feedback. Lennart Benshop contributed
1.29 anton 4055: @file{glosgen.fs}, while Stuart Ramsden has been working on automatic
4056: support for calling C libraries. Helpful comments also came from Paul
4057: Kleinrubatscher, Christian Pirker, Dirk Zoller and Marcel Hendrix.
4058:
1.30 anton 4059: Gforth also owes a lot to the authors of the tools we used (GCC, CVS,
4060: and autoconf, among others), and to the creators of the Internet: Gforth
4061: was developed across the Internet, and its authors have not met
4062: physically yet.
4063:
1.29 anton 4064: @section Pedigree
1.4 anton 4065:
1.17 anton 4066: Gforth descends from BigForth (1993) and fig-Forth. Gforth and PFE (by
1.24 anton 4067: Dirk Zoller) will cross-fertilize each other. Of course, a significant
4068: part of the design of Gforth was prescribed by ANS Forth.
1.17 anton 4069:
1.23 pazsan 4070: Bernd Paysan wrote BigForth, a descendent from TurboForth, an unreleased
4071: 32 bit native code version of VolksForth for the Atari ST, written
4072: mostly by Dietrich Weineck.
4073:
4074: VolksForth descends from F83. It was written by Klaus Schleisiek, Bernd
4075: Pennemann, Georg Rehfeld and Dietrich Weineck for the C64 (called
1.24 anton 4076: UltraForth there) in the mid-80s and ported to the Atari ST in 1986.
1.17 anton 4077:
1.34 anton 4078: Henry Laxen and Mike Perry wrote F83 as a model implementation of the
1.17 anton 4079: Forth-83 standard. !! Pedigree? When?
4080:
4081: A team led by Bill Ragsdale implemented fig-Forth on many processors in
1.24 anton 4082: 1979. Robert Selzer and Bill Ragsdale developed the original
4083: implementation of fig-Forth for the 6502 based on microForth.
4084:
4085: The principal architect of microForth was Dean Sanderson. microForth was
4086: FORTH, Inc.'s first off-the-shelf product. It was developped in 1976 for
4087: the 1802, and subsequently implemented on the 8080, the 6800 and the
4088: Z80.
1.17 anton 4089:
1.24 anton 4090: All earlier Forth systems were custom-made, usually by Charles Moore,
1.30 anton 4091: who discovered (as he puts it) Forth during the late 60s. The first full
4092: Forth existed in 1971.
1.17 anton 4093:
4094: A part of the information in this section comes from @cite{The Evolution
4095: of Forth} by Elizabeth D. Rather, Donald R. Colburn and Charles
4096: H. Moore, presented at the HOPL-II conference and preprinted in SIGPLAN
4097: Notices 28(3), 1993. You can find more historical and genealogical
4098: information about Forth there.
4099:
1.29 anton 4100: @node Word Index, Node Index, Origin, Top
1.4 anton 4101: @chapter Word Index
4102:
1.18 anton 4103: This index is as incomplete as the manual. Each word is listed with
4104: stack effect and wordset.
1.17 anton 4105:
4106: @printindex fn
4107:
1.4 anton 4108: @node Node Index, , Word Index, Top
4109: @chapter Node Index
1.17 anton 4110:
4111: This index is even less complete than the manual.
1.1 anton 4112:
4113: @contents
4114: @bye
4115:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>