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