Annotation of gforth/gforth.ds, revision 1.30

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

FreeBSD-CVSweb <>