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