Annotation of gforth/gforth.ds, revision 1.21

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

FreeBSD-CVSweb <>