Annotation of gforth/gforth.ds, revision 1.48

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>