File:  [gforth] / gforth / doc / vmgen.texi
Revision 1.16: download - view: text, annotated - select for diffs
Wed Aug 21 09:36:01 2002 UTC (17 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
documentation changes

    1: \input texinfo    @c -*-texinfo-*-
    2: @comment %**start of header
    3: @setfilename vmgen.info
    4: @include version.texi
    5: @settitle Vmgen (Gforth @value{VERSION})
    6: @c @syncodeindex pg cp
    7: @comment %**end of header
    8: @copying
    9: This manual is for Vmgen
   10: (version @value{VERSION}, @value{UPDATED}),
   11: the virtual machine interpreter generator
   12: 
   13: Copyright @copyright{} 2002 Free Software Foundation, Inc.
   14: 
   15: @quotation
   16: Permission is granted to copy, distribute and/or modify this document
   17: under the terms of the GNU Free Documentation License, Version 1.1 or
   18: any later version published by the Free Software Foundation; with no
   19: Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
   20: and with the Back-Cover Texts as in (a) below.  A copy of the
   21: license is included in the section entitled ``GNU Free Documentation
   22: License.''
   23: 
   24: (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify
   25: this GNU Manual, like GNU software.  Copies published by the Free
   26: Software Foundation raise funds for GNU development.''
   27: @end quotation
   28: @end copying
   29: 
   30: @dircategory GNU programming tools
   31: @direntry
   32: * Vmgen: (vmgen).               Interpreter generator
   33: @end direntry
   34: 
   35: @titlepage
   36: @title Vmgen
   37: @subtitle for Gforth version @value{VERSION}, @value{UPDATED}
   38: @author M. Anton Ertl (@email{anton@@mips.complang.tuwien.ac.at})
   39: @page
   40: @vskip 0pt plus 1filll
   41: @insertcopying
   42: @end titlepage
   43: 
   44: @contents
   45: 
   46: @ifnottex
   47: @node Top, Introduction, (dir), (dir)
   48: @top Vmgen
   49: 
   50: @insertcopying
   51: @end ifnottex
   52: 
   53: @menu
   54: * Introduction::                What can Vmgen do for you?
   55: * Why interpreters?::           Advantages and disadvantages
   56: * Concepts::                    VM interpreter background
   57: * Invoking Vmgen::              
   58: * Example::                     
   59: * Input File Format::           
   60: * Error messages::              reported by Vmgen
   61: * Using the generated code::    
   62: * Hints::                       VM archictecture, efficiency
   63: * The future::                  
   64: * Changes::                     from earlier versions
   65: * Contact::                     Bug reporting etc.
   66: * Copying This Manual::         Manual License
   67: * Index::                       
   68: 
   69: @detailmenu
   70:  --- The Detailed Node Listing ---
   71: 
   72: Concepts
   73: 
   74: * Front end and VM interpreter::  Modularizing an interpretive system
   75: * Data handling::               Stacks, registers, immediate arguments
   76: * Dispatch::                    From one VM instruction to the next
   77: 
   78: Example
   79: 
   80: * Example overview::            
   81: * Using profiling to create superinstructions::  
   82: 
   83: Input File Format
   84: 
   85: * Input File Grammar::          
   86: * Simple instructions::         
   87: * Superinstructions::           
   88: * Register Machines::           How to define register VM instructions
   89: 
   90: Simple instructions
   91: 
   92: * C Code Macros::               Macros recognized by Vmgen
   93: * C Code restrictions::         Vmgen makes assumptions about C code
   94: 
   95: Using the generated code
   96: 
   97: * VM engine::                   Executing VM code
   98: * VM instruction table::        
   99: * VM code generation::          Creating VM code (in the front-end)
  100: * Peephole optimization::       Creating VM superinstructions
  101: * VM disassembler::             for debugging the front end
  102: * VM profiler::                 for finding worthwhile superinstructions
  103: 
  104: Hints
  105: 
  106: * Floating point::              and stacks
  107: 
  108: Copying This Manual
  109: 
  110: * GNU Free Documentation License::  License for copying this manual.
  111: 
  112: @end detailmenu
  113: @end menu
  114: 
  115: @c @ifnottex
  116: @c This file documents Vmgen (Gforth @value{VERSION}).
  117: 
  118: @c ************************************************************
  119: @node Introduction, Why interpreters?, Top, Top
  120: @chapter Introduction
  121: 
  122: Vmgen is a tool for writing efficient interpreters.  It takes a simple
  123: virtual machine description and generates efficient C code for dealing
  124: with the virtual machine code in various ways (in particular, executing
  125: it).  The run-time efficiency of the resulting interpreters is usually
  126: within a factor of 10 of machine code produced by an optimizing
  127: compiler.
  128: 
  129: The interpreter design strategy supported by Vmgen is to divide the
  130: interpreter into two parts:
  131: 
  132: @itemize @bullet
  133: 
  134: @item The @emph{front end} takes the source code of the language to be
  135: implemented, and translates it into virtual machine code.  This is
  136: similar to an ordinary compiler front end; typically an interpreter
  137: front-end performs no optimization, so it is relatively simple to
  138: implement and runs fast.
  139: 
  140: @item The @emph{virtual machine interpreter} executes the virtual
  141: machine code.
  142: 
  143: @end itemize
  144: 
  145: Such a division is usually used in interpreters, for modularity as well
  146: as for efficiency.  The virtual machine code is typically passed between
  147: front end and virtual machine interpreter in memory, like in a
  148: load-and-go compiler; this avoids the complexity and time cost of
  149: writing the code to a file and reading it again.
  150: 
  151: A @emph{virtual machine} (VM) represents the program as a sequence of
  152: @emph{VM instructions}, following each other in memory, similar to real
  153: machine code.  Control flow occurs through VM branch instructions, like
  154: in a real machine.
  155: 
  156: @cindex functionality features overview
  157: In this setup, Vmgen can generate most of the code dealing with virtual
  158: machine instructions from a simple description of the virtual machine
  159: instructions (@pxref{Input File Format}), in particular:
  160: 
  161: @table @strong
  162: 
  163: @item VM instruction execution
  164: 
  165: @item VM code generation
  166: Useful in the front end.
  167: 
  168: @item VM code decompiler
  169: Useful for debugging the front end.
  170: 
  171: @item VM code tracing
  172: Useful for debugging the front end and the VM interpreter.  You will
  173: typically provide other means for debugging the user's programs at the
  174: source level.
  175: 
  176: @item VM code profiling
  177: Useful for optimizing the VM interpreter with superinstructions
  178: (@pxref{VM profiler}).
  179: 
  180: @end table
  181: 
  182: To create parts of the interpretive system that do not deal with VM
  183: instructions, you have to use other tools (e.g., @command{bison}) and/or
  184: hand-code them.
  185: 
  186: @cindex efficiency features overview
  187: @noindent
  188: Vmgen supports efficient interpreters though various optimizations, in
  189: particular
  190: 
  191: @itemize @bullet
  192: 
  193: @item Threaded code
  194: 
  195: @item Caching the top-of-stack in a register
  196: 
  197: @item Combining VM instructions into superinstructions
  198: 
  199: @item
  200: Replicating VM (super)instructions for better BTB prediction accuracy
  201: (not yet in vmgen-ex, but already in Gforth).
  202: 
  203: @end itemize
  204: 
  205: @cindex speed for JVM
  206: As a result, Vmgen-based interpreters are only about an order of
  207: magnitude slower than native code from an optimizing C compiler on small
  208: benchmarks; on large benchmarks, which spend more time in the run-time
  209: system, the slowdown is often less (e.g., the slowdown of a
  210: Vmgen-generated JVM interpreter over the best JVM JIT compiler we
  211: measured is only a factor of 2-3 for large benchmarks; some other JITs
  212: and all other interpreters we looked at were slower than our
  213: interpreter).
  214: 
  215: VMs are usually designed as stack machines (passing data between VM
  216: instructions on a stack), and Vmgen supports such designs especially
  217: well; however, you can also use Vmgen for implementing a register VM
  218: (@pxref{Register Machines}) and still benefit from most of the advantages
  219: offered by Vmgen.
  220: 
  221: There are many potential uses of the instruction descriptions that are
  222: not implemented at the moment, but we are open for feature requests, and
  223: we will consider new features if someone asks for them; so the feature
  224: list above is not exhaustive.
  225: 
  226: @c *********************************************************************
  227: @node Why interpreters?, Concepts, Introduction, Top
  228: @chapter Why interpreters?
  229: @cindex interpreters, advantages
  230: @cindex advantages of interpreters
  231: @cindex advantages of vmgen
  232: 
  233: Interpreters are a popular language implementation technique because
  234: they combine all three of the following advantages:
  235: 
  236: @itemize @bullet
  237: 
  238: @item Ease of implementation
  239: 
  240: @item Portability
  241: 
  242: @item Fast edit-compile-run cycle
  243: 
  244: @end itemize
  245: 
  246: Vmgen makes it even easier to implement interpreters.
  247: 
  248: @cindex speed of interpreters
  249: The main disadvantage of interpreters is their run-time speed.  However,
  250: there are huge differences between different interpreters in this area:
  251: the slowdown over optimized C code on programs consisting of simple
  252: operations is typically a factor of 10 for the more efficient
  253: interpreters, and a factor of 1000 for the less efficient ones (the
  254: slowdown for programs executing complex operations is less, because the
  255: time spent in libraries for executing complex operations is the same in
  256: all implementation strategies).
  257: 
  258: Vmgen supports techniques for building efficient interpreters.
  259: 
  260: @c ********************************************************************
  261: @node Concepts, Invoking Vmgen, Why interpreters?, Top
  262: @chapter Concepts
  263: 
  264: @menu
  265: * Front end and VM interpreter::  Modularizing an interpretive system
  266: * Data handling::               Stacks, registers, immediate arguments
  267: * Dispatch::                    From one VM instruction to the next
  268: @end menu
  269: 
  270: @c --------------------------------------------------------------------
  271: @node Front end and VM interpreter, Data handling, Concepts, Concepts
  272: @section Front end and VM interpreter
  273: @cindex modularization of interpreters
  274: 
  275: @cindex front-end
  276: Interpretive systems are typically divided into a @emph{front end} that
  277: parses the input language and produces an intermediate representation
  278: for the program, and an interpreter that executes the intermediate
  279: representation of the program.
  280: 
  281: @cindex virtual machine
  282: @cindex VM
  283: @cindex VM instruction
  284: @cindex instruction, VM
  285: @cindex VM branch instruction
  286: @cindex branch instruction, VM
  287: @cindex VM register
  288: @cindex register, VM
  289: @cindex opcode, VM instruction
  290: @cindex immediate argument, VM instruction
  291: For efficient interpreters the intermediate representation of choice is
  292: virtual machine code (rather than, e.g., an abstract syntax tree).
  293: @emph{Virtual machine} (VM) code consists of VM instructions arranged
  294: sequentially in memory; they are executed in sequence by the VM
  295: interpreter, but VM branch instructions can change the control flow and
  296: are used for implementing control structures.  The conceptual similarity
  297: to real machine code results in the name @emph{virtual machine}.
  298: Various terms similar to terms for real machines are used; e.g., there
  299: are @emph{VM registers} (like the instruction pointer and stack
  300: pointer(s)), and the VM instruction consists of an @emph{opcode} and
  301: @emph{immediate arguments}.
  302: 
  303: In this framework, Vmgen supports building the VM interpreter and any
  304: other component dealing with VM instructions.  It does not have any
  305: support for the front end, apart from VM code generation support.  The
  306: front end can be implemented with classical compiler front-end
  307: techniques, supported by tools like @command{flex} and @command{bison}.
  308: 
  309: The intermediate representation is usually just internal to the
  310: interpreter, but some systems also support saving it to a file, either
  311: as an image file, or in a full-blown linkable file format (e.g., JVM).
  312: Vmgen currently has no special support for such features, but the
  313: information in the instruction descriptions can be helpful, and we are
  314: open to feature requests and suggestions.
  315: 
  316: @c --------------------------------------------------------------------
  317: @node Data handling, Dispatch, Front end and VM interpreter, Concepts
  318: @section Data handling
  319: 
  320: @cindex stack machine
  321: @cindex register machine
  322: Most VMs use one or more stacks for passing temporary data between VM
  323: instructions.  Another option is to use a register machine architecture
  324: for the virtual machine; we believe that using a stack architecture is
  325: usually both simpler and faster.
  326: 
  327: however, this option is slower or
  328: significantly more complex to implement than a stack machine architecture.
  329: 
  330: Vmgen has special support and optimizations for stack VMs, making their
  331: implementation easy and efficient.
  332: 
  333: You can also implement a register VM with Vmgen (@pxref{Register
  334: Machines}), and you will still profit from most Vmgen features.
  335: 
  336: @cindex stack item size
  337: @cindex size, stack items
  338: Stack items all have the same size, so they typically will be as wide as
  339: an integer, pointer, or floating-point value.  Vmgen supports treating
  340: two consecutive stack items as a single value, but anything larger is
  341: best kept in some other memory area (e.g., the heap), with pointers to
  342: the data on the stack.
  343: 
  344: @cindex instruction stream
  345: @cindex immediate arguments
  346: Another source of data is immediate arguments VM instructions (in the VM
  347: instruction stream).  The VM instruction stream is handled similar to a
  348: stack in Vmgen.
  349: 
  350: @cindex garbage collection
  351: @cindex reference counting
  352: Vmgen has no built-in support for, nor restrictions against
  353: @emph{garbage collection}.  If you need garbage collection, you need to
  354: provide it in your run-time libraries.  Using @emph{reference counting}
  355: is probably harder, but might be possible (contact us if you are
  356: interested).
  357: @c reference counting might be possible by including counting code in 
  358: @c the conversion macros.
  359: 
  360: @c --------------------------------------------------------------------
  361: @node Dispatch,  , Data handling, Concepts
  362: @section Dispatch
  363: @cindex Dispatch of VM instructions
  364: @cindex main interpreter loop
  365: 
  366: Understanding this section is probably not necessary for using Vmgen,
  367: but it may help.  You may want to skip it now, and read it if you find statements about dispatch methods confusing.
  368: 
  369: After executing one VM instruction, the VM interpreter has to dispatch
  370: the next VM instruction (Vmgen calls the dispatch routine @samp{NEXT}).
  371: Vmgen supports two methods of dispatch:
  372: 
  373: @table @strong
  374: 
  375: @item switch dispatch
  376: @cindex switch dispatch
  377: In this method the VM interpreter contains a giant @code{switch}
  378: statement, with one @code{case} for each VM instruction.  The VM
  379: instruction opcodes are represented by integers (e.g., produced by an
  380: @code{enum}) in the VM code, and dispatch occurs by loading the next
  381: opcode, @code{switch}ing on it, and continuing at the appropriate
  382: @code{case}; after executing the VM instruction, the VM interpreter
  383: jumps back to the dispatch code.
  384: 
  385: @item threaded code
  386: @cindex threaded code
  387: This method represents a VM instruction opcode by the address of the
  388: start of the machine code fragment for executing the VM instruction.
  389: Dispatch consists of loading this address, jumping to it, and
  390: incrementing the VM instruction pointer.  Typically the threaded-code
  391: dispatch code is appended directly to the code for executing the VM
  392: instruction.  Threaded code cannot be implemented in ANSI C, but it can
  393: be implemented using GNU C's labels-as-values extension (@pxref{Labels
  394: as Values, , Labels as Values, gcc.info, GNU C Manual}).
  395: 
  396: @c call threading
  397: @end table
  398: 
  399: Threaded code can be twice as fast as switch dispatch, depending on the
  400: interpreter, the benchmark, and the machine.
  401: 
  402: @c *************************************************************
  403: @node Invoking Vmgen, Example, Concepts, Top
  404: @chapter Invoking Vmgen
  405: @cindex Invoking Vmgen
  406: 
  407: The usual way to invoke Vmgen is as follows:
  408: 
  409: @example
  410: vmgen @var{inputfile}
  411: @end example
  412: 
  413: Here @var{inputfile} is the VM instruction description file, which
  414: usually ends in @file{.vmg}.  The output filenames are made by taking
  415: the basename of @file{inputfile} (i.e., the output files will be created
  416: in the current working directory) and replacing @file{.vmg} with
  417: @file{-vm.i}, @file{-disasm.i}, @file{-gen.i}, @file{-labels.i},
  418: @file{-profile.i}, and @file{-peephole.i}.  E.g., @command{vmgen
  419: hack/foo.vmg} will create @file{foo-vm.i}, @file{foo-disasm.i},
  420: @file{foo-gen.i}, @file{foo-labels.i}, @file{foo-profile.i} and
  421: @file{foo-peephole.i}.
  422: 
  423: The command-line options supported by Vmgen are
  424: 
  425: @table @option
  426: 
  427: @cindex -h, command-line option
  428: @cindex --help, command-line option
  429: @item --help
  430: @itemx -h
  431: Print a message about the command-line options
  432: 
  433: @cindex -v, command-line option
  434: @cindex --version, command-line option
  435: @item --version
  436: @itemx -v
  437: Print version and exit
  438: @end table
  439: 
  440: @c env vars GFORTHDIR GFORTHDATADIR
  441: 
  442: @c ****************************************************************
  443: @node Example, Input File Format, Invoking Vmgen, Top
  444: @chapter Example
  445: @cindex example of a Vmgen-based interpreter
  446: 
  447: @menu
  448: * Example overview::            
  449: * Using profiling to create superinstructions::  
  450: @end menu
  451: 
  452: @c --------------------------------------------------------------------
  453: @node Example overview, Using profiling to create superinstructions, Example, Example
  454: @section Example overview
  455: @cindex example overview
  456: @cindex @file{vmgen-ex}
  457: @cindex @file{vmgen-ex2}
  458: 
  459: There are two versions of the same example for using Vmgen:
  460: @file{vmgen-ex} and @file{vmgen-ex2} (you can also see Gforth as
  461: example, but it uses additional (undocumented) features, and also
  462: differs in some other respects).  The example implements @emph{mini}, a
  463: tiny Modula-2-like language with a small JavaVM-like virtual machine.
  464: 
  465: The difference between the examples is that @file{vmgen-ex} uses many
  466: casts, and @file{vmgen-ex2} tries to avoids most casts and uses unions
  467: instead.  In the rest of this manual we usually mention just files in
  468: @file{vmgen-ex}; if you want to use unions, use the equivalent file in
  469: @file{vmgen-ex2}.
  470: @cindex unions example
  471: @cindex casts example
  472: 
  473: The files provided with each example are:
  474: @cindex example files
  475: 
  476: @example
  477: Makefile
  478: README
  479: disasm.c           wrapper file
  480: engine.c           wrapper file
  481: peephole.c         wrapper file
  482: profile.c          wrapper file
  483: mini-inst.vmg      simple VM instructions
  484: mini-super.vmg     superinstructions (empty at first)
  485: mini.h             common declarations
  486: mini.l             scanner
  487: mini.y             front end (parser, VM code generator)
  488: support.c          main() and other support functions
  489: fib.mini           example mini program
  490: simple.mini        example mini program
  491: test.mini          example mini program (tests everything)
  492: test.out           test.mini output
  493: stat.awk           script for aggregating profile information
  494: peephole-blacklist list of instructions not allowed in superinstructions
  495: seq2rule.awk       script for creating superinstructions
  496: @end example
  497: 
  498: For your own interpreter, you would typically copy the following files
  499: and change little, if anything:
  500: @cindex wrapper files
  501: 
  502: @example
  503: disasm.c           wrapper file
  504: engine.c           wrapper file
  505: peephole.c         wrapper file
  506: profile.c          wrapper file
  507: stat.awk           script for aggregating profile information
  508: seq2rule.awk       script for creating superinstructions
  509: @end example
  510: 
  511: @noindent
  512: You would typically change much in or replace the following files:
  513: 
  514: @example
  515: Makefile
  516: mini-inst.vmg      simple VM instructions
  517: mini.h             common declarations
  518: mini.l             scanner
  519: mini.y             front end (parser, VM code generator)
  520: support.c          main() and other support functions
  521: peephole-blacklist list of instructions not allowed in superinstructions
  522: @end example
  523: 
  524: You can build the example by @code{cd}ing into the example's directory,
  525: and then typing @code{make}; you can check that it works with @code{make
  526: check}.  You can run run mini programs like this:
  527: 
  528: @example
  529: ./mini fib.mini
  530: @end example
  531: 
  532: To learn about the options, type @code{./mini -h}.
  533: 
  534: @c --------------------------------------------------------------------
  535: @node Using profiling to create superinstructions,  , Example overview, Example
  536: @section Using profiling to create superinstructions
  537: @cindex profiling example
  538: @cindex superinstructions example
  539: 
  540: I have not added rules for this in the @file{Makefile} (there are many
  541: options for selecting superinstructions, and I did not want to hardcode
  542: one into the @file{Makefile}), but there are some supporting scripts, and
  543: here's an example:
  544: 
  545: Suppose you want to use @file{fib.mini} and @file{test.mini} as training
  546: programs, you get the profiles like this:
  547: 
  548: @example
  549: make fib.prof test.prof #takes a few seconds
  550: @end example
  551: 
  552: You can aggregate these profiles with @file{stat.awk}:
  553: 
  554: @example
  555: awk -f stat.awk fib.prof test.prof
  556: @end example
  557: 
  558: The result contains lines like:
  559: 
  560: @example
  561:       2      16        36910041 loadlocal lit
  562: @end example
  563: 
  564: This means that the sequence @code{loadlocal lit} statically occurs a
  565: total of 16 times in 2 profiles, with a dynamic execution count of
  566: 36910041.
  567: 
  568: The numbers can be used in various ways to select superinstructions.
  569: E.g., if you just want to select all sequences with a dynamic
  570: execution count exceeding 10000, you would use the following pipeline:
  571: 
  572: @example
  573: awk -f stat.awk fib.prof test.prof|
  574: awk '$3>=10000'|                #select sequences
  575: fgrep -v -f peephole-blacklist| #eliminate wrong instructions
  576: awk -f seq2rule.awk|  #transform sequences into superinstruction rules
  577: sort -k 3 >mini-super.vmg       #sort sequences
  578: @end example
  579: 
  580: The file @file{peephole-blacklist} contains all instructions that
  581: directly access a stack or stack pointer (for mini: @code{call},
  582: @code{return}); the sort step is necessary to ensure that prefixes
  583: precede larger superinstructions.
  584: 
  585: Now you can create a version of mini with superinstructions by just
  586: saying @samp{make}
  587: 
  588: 
  589: @c ***************************************************************
  590: @node Input File Format, Error messages, Example, Top
  591: @chapter Input File Format
  592: @cindex input file format
  593: @cindex format, input file
  594: 
  595: Vmgen takes as input a file containing specifications of virtual machine
  596: instructions.  This file usually has a name ending in @file{.vmg}.
  597: 
  598: Most examples are taken from the example in @file{vmgen-ex}.
  599: 
  600: @menu
  601: * Input File Grammar::          
  602: * Simple instructions::         
  603: * Superinstructions::           
  604: * Register Machines::           How to define register VM instructions
  605: @end menu
  606: 
  607: @c --------------------------------------------------------------------
  608: @node Input File Grammar, Simple instructions, Input File Format, Input File Format
  609: @section Input File Grammar
  610: @cindex grammar, input file
  611: @cindex input file grammar
  612: 
  613: The grammar is in EBNF format, with @code{@var{a}|@var{b}} meaning
  614: ``@var{a} or @var{b}'', @code{@{@var{c}@}} meaning 0 or more repetitions
  615: of @var{c} and @code{[@var{d}]} meaning 0 or 1 repetitions of @var{d}.
  616: 
  617: @cindex free-format, not
  618: @cindex newlines, significance in syntax
  619: Vmgen input is not free-format, so you have to take care where you put
  620: newlines (and, in a few cases, white space).
  621: 
  622: @example
  623: description: @{instruction|comment|eval-escape|c-escape@}
  624: 
  625: instruction: simple-inst|superinst
  626: 
  627: simple-inst: ident '(' stack-effect ')' newline c-code newline newline
  628: 
  629: stack-effect: @{ident@} '--' @{ident@}
  630: 
  631: super-inst: ident '=' ident @{ident@}  
  632: 
  633: comment:      '\ '  text newline
  634: 
  635: eval-escape:  '\E ' text newline
  636: 
  637: c-escape:     '\C ' text newline
  638: @end example
  639: @c \+ \- \g \f \c
  640: 
  641: Note that the @code{\}s in this grammar are meant literally, not as
  642: C-style encodings for non-printable characters.
  643: 
  644: There are two ways to delimit the C code in @code{simple-inst}:
  645: 
  646: @itemize @bullet
  647: 
  648: @item
  649: If you start it with a @samp{@{} at the start of a line (i.e., not even
  650: white space before it), you have to end it with a @samp{@}} at the start
  651: of a line (followed by a newline).  In this case you may have empty
  652: lines within the C code (typically used between variable definitions and
  653: statements).
  654: 
  655: @item
  656: You do not start it with @samp{@{}.  Then the C code ends at the first
  657: empty line, so you cannot have empty lines within this code.
  658: 
  659: @end itemize
  660: 
  661: The text in @code{comment}, @code{eval-escape} and @code{c-escape} must
  662: not contain a newline.  @code{Ident} must conform to the usual
  663: conventions of C identifiers (otherwise the C compiler would choke on
  664: the Vmgen output), except that idents in @code{stack-effect} may have a
  665: stack prefix (for stack prefix syntax, @pxref{Eval escapes}).
  666: 
  667: @cindex C escape
  668: @cindex @code{\C}
  669: @cindex conditional compilation of Vmgen output
  670: The @code{c-escape} passes the text through to each output file (without
  671: the @samp{\C}).  This is useful mainly for conditional compilation
  672: (i.e., you write @samp{\C #if ...} etc.).
  673: 
  674: @cindex sync lines
  675: @cindex @code{#line}
  676: In addition to the syntax given in the grammer, Vmgen also processes
  677: sync lines (lines starting with @samp{#line}), as produced by @samp{m4
  678: -s} (@pxref{Invoking m4, , Invoking m4, m4.info, GNU m4}) and similar
  679: tools.  This allows associating C compiler error messages with the
  680: original source of the C code.
  681: 
  682: Vmgen understands a few extensions beyond the grammar given here, but
  683: these extensions are only useful for building Gforth.  You can find a
  684: description of the format used for Gforth in @file{prim}.
  685: 
  686: @subsection Eval escapes
  687: @cindex escape to Forth
  688: @cindex eval escape
  689: @cindex @code{\E}
  690: 
  691: @c woanders?
  692: The text in @code{eval-escape} is Forth code that is evaluated when
  693: Vmgen reads the line.  You will normally use this feature to define
  694: stacks and types.
  695: 
  696: If you do not know (and do not want to learn) Forth, you can build the
  697: text according to the following grammar; these rules are normally all
  698: Forth you need for using Vmgen:
  699: 
  700: @example
  701: text: stack-decl|type-prefix-decl|stack-prefix-decl
  702: 
  703: stack-decl: 'stack ' ident ident ident
  704: type-prefix-decl: 
  705:     's" ' string '" ' ('single'|'double') ident 'type-prefix' ident
  706: stack-prefix-decl:  ident 'stack-prefix' string
  707: @end example
  708: 
  709: Note that the syntax of this code is not checked thoroughly (there are
  710: many other Forth program fragments that could be written in an
  711: eval-escape).
  712: 
  713: A stack prefix can contain letters, digits, or @samp{:}, and may start
  714: with an @samp{#}; e.g., in Gforth the return stack has the stack prefix
  715: @samp{R:}.  This restriction is not checked during the stack prefix
  716: definition, but it is enforced by the parsing rules for stack items
  717: later.
  718: 
  719: If you know Forth, the stack effects of the non-standard words involved
  720: are:
  721: @findex stack
  722: @findex type-prefix
  723: @findex single
  724: @findex double
  725: @findex stack-prefix
  726: @example
  727: stack        ( "name" "pointer" "type" -- )
  728:              ( name execution: -- stack )
  729: type-prefix  ( addr u item-size stack "prefix" -- )
  730: single       ( -- item-size )
  731: double       ( -- item-size )
  732: stack-prefix ( stack "prefix" -- )
  733: @end example
  734: 
  735: An @var{item-size} takes three cells on the stack.
  736: 
  737: @c --------------------------------------------------------------------
  738: @node Simple instructions, Superinstructions, Input File Grammar, Input File Format
  739: @section Simple instructions
  740: @cindex simple VM instruction
  741: @cindex instruction, simple VM
  742: 
  743: We will use the following simple VM instruction description as example:
  744: 
  745: @example
  746: sub ( i1 i2 -- i )
  747: i = i1-i2;
  748: @end example
  749: 
  750: The first line specifies the name of the VM instruction (@code{sub}) and
  751: its stack effect (@code{i1 i2 -- i}).  The rest of the description is
  752: just plain C code.
  753: 
  754: @cindex stack effect
  755: @cindex effect, stack
  756: The stack effect specifies that @code{sub} pulls two integers from the
  757: data stack and puts them in the C variables @code{i1} and @code{i2}
  758: (with the rightmost item (@code{i2}) taken from the top of stack;
  759: intuition: if you push @code{i1}, then @code{i2} on the stack, the
  760: resulting stack picture is @code{i1 i2}) and later pushes one integer
  761: (@code{i}) on the data stack (the rightmost item is on the top
  762: afterwards).
  763: 
  764: @cindex prefix, type
  765: @cindex type prefix
  766: @cindex default stack of a type prefix
  767: How do we know the type and stack of the stack items?  Vmgen uses
  768: prefixes, similar to Fortran; in contrast to Fortran, you have to
  769: define the prefix first:
  770: 
  771: @example
  772: \E s" Cell"   single data-stack type-prefix i
  773: @end example
  774: 
  775: This defines the prefix @code{i} to refer to the type @code{Cell}
  776: (defined as @code{long} in @file{mini.h}) and, by default, to the
  777: @code{data-stack}.  It also specifies that this type takes one stack
  778: item (@code{single}).  The type prefix is part of the variable name.
  779: 
  780: @cindex stack definition
  781: @cindex defining a stack
  782: Before we can use @code{data-stack} in this way, we have to define it:
  783: 
  784: @example
  785: \E stack data-stack sp Cell
  786: @end example
  787: @c !! use something other than Cell
  788: 
  789: @cindex stack basic type
  790: @cindex basic type of a stack
  791: @cindex type of a stack, basic
  792: @cindex stack growth direction
  793: This line defines the stack @code{data-stack}, which uses the stack
  794: pointer @code{sp}, and each item has the basic type @code{Cell}; other
  795: types have to fit into one or two @code{Cell}s (depending on whether the
  796: type is @code{single} or @code{double} wide), and are cast from and to
  797: Cells on accessing the @code{data-stack} with type cast macros
  798: (@pxref{VM engine}).  Stacks grow towards lower addresses in
  799: Vmgen-erated interpreters.
  800: 
  801: @cindex stack prefix
  802: @cindex prefix, stack
  803: We can override the default stack of a stack item by using a stack
  804: prefix.  E.g., consider the following instruction:
  805: 
  806: @example
  807: lit ( #i -- i )
  808: @end example
  809: 
  810: The VM instruction @code{lit} takes the item @code{i} from the
  811: instruction stream (indicated by the prefix @code{#}), and pushes it on
  812: the (default) data stack.  The stack prefix is not part of the variable
  813: name.  Stack prefixes are defined like this:
  814: 
  815: @example
  816: \E inst-stream stack-prefix #
  817: @end example
  818: 
  819: This definition defines that the stack prefix @code{#} specifies the
  820: ``stack'' @code{inst-stream}.  Since the instruction stream behaves a
  821: little differently than an ordinary stack, it is predefined, and you do
  822: not need to define it.
  823: 
  824: @cindex instruction stream
  825: The instruction stream contains instructions and their immediate
  826: arguments, so specifying that an argument comes from the instruction
  827: stream indicates an immediate argument.  Of course, instruction stream
  828: arguments can only appear to the left of @code{--} in the stack effect.
  829: If there are multiple instruction stream arguments, the leftmost is the
  830: first one (just as the intuition suggests).
  831: 
  832: @menu
  833: * C Code Macros::               Macros recognized by Vmgen
  834: * C Code restrictions::         Vmgen makes assumptions about C code
  835: @end menu
  836: 
  837: @c --------------------------------------------------------------------
  838: @node C Code Macros, C Code restrictions, Simple instructions, Simple instructions
  839: @subsection C Code Macros
  840: @cindex macros recognized by Vmgen
  841: @cindex basic block, VM level
  842: 
  843: Vmgen recognizes the following strings in the C code part of simple
  844: instructions:
  845: 
  846: @table @code
  847: 
  848: @item SET_IP
  849: @findex SET_IP
  850: As far as Vmgen is concerned, a VM instruction containing this ends a VM
  851: basic block (used in profiling to delimit profiled sequences).  On the C
  852: level, this also sets the instruction pointer.
  853: 
  854: @item SUPER_END
  855: @findex SUPER_END
  856: This ends a basic block (for profiling), even if the instruction
  857: contains no @code{SET_IP}.
  858: 
  859: @item INST_TAIL;
  860: @findex INST_TAIL;
  861: Vmgen replaces @samp{INST_TAIL;} with code for ending a VM instruction and
  862: dispatching the next VM instruction.  Even without a @samp{INST_TAIL;} this
  863: happens automatically when control reaches the end of the C code.  If
  864: you want to have this in the middle of the C code, you need to use
  865: @samp{INST_TAIL;}.  A typical example is a conditional VM branch:
  866: 
  867: @example
  868: if (branch_condition) @{
  869:   SET_IP(target); INST_TAIL;
  870: @}
  871: /* implicit tail follows here */
  872: @end example
  873: 
  874: In this example, @samp{INST_TAIL;} is not strictly necessary, because there
  875: is another one implicitly after the if-statement, but using it improves
  876: branch prediction accuracy slightly and allows other optimizations.
  877: 
  878: @item SUPER_CONTINUE
  879: @findex SUPER_CONTINUE
  880: This indicates that the implicit tail at the end of the VM instruction
  881: dispatches the sequentially next VM instruction even if there is a
  882: @code{SET_IP} in the VM instruction.  This enables an optimization that
  883: is not yet implemented in the vmgen-ex code (but in Gforth).  The
  884: typical application is in conditional VM branches:
  885: 
  886: @example
  887: if (branch_condition) @{
  888:   SET_IP(target); INST_TAIL; /* now this INST_TAIL is necessary */
  889: @}
  890: SUPER_CONTINUE;
  891: @end example
  892: 
  893: @end table
  894: 
  895: Note that Vmgen is not smart about C-level tokenization, comments,
  896: strings, or conditional compilation, so it will interpret even a
  897: commented-out SUPER_END as ending a basic block (or, e.g.,
  898: @samp{RESET_IP;} as @samp{SET_IP;}).  Conversely, Vmgen requires the literal
  899: presence of these strings; Vmgen will not see them if they are hiding in
  900: a C preprocessor macro.
  901: 
  902: 
  903: @c --------------------------------------------------------------------
  904: @node C Code restrictions,  , C Code Macros, Simple instructions
  905: @subsection C Code restrictions
  906: @cindex C code restrictions
  907: @cindex restrictions on C code
  908: @cindex assumptions about C code
  909: 
  910: @cindex accessing stack (pointer)
  911: @cindex stack pointer, access
  912: @cindex instruction pointer, access
  913: Vmgen generates code and performs some optimizations under the
  914: assumption that the user-supplied C code does not access the stack
  915: pointers or stack items, and that accesses to the instruction pointer
  916: only occur through special macros.  In general you should heed these
  917: restrictions.  However, if you need to break these restrictions, read
  918: the following.
  919: 
  920: Accessing a stack or stack pointer directly can be a problem for several
  921: reasons: 
  922: @cindex stack caching, restriction on C code
  923: @cindex superinstructions, restrictions on components
  924: 
  925: @itemize @bullet
  926: 
  927: @item
  928: Vmgen optionally supports caching the top-of-stack item in a local
  929: variable (that is allocated to a register).  This is the most frequent
  930: source of trouble.  You can deal with it either by not using
  931: top-of-stack caching (slowdown factor 1-1.4, depending on machine), or
  932: by inserting flushing code (e.g., @samp{IF_spTOS(sp[...] = spTOS);}) at
  933: the start and reloading code (e.g., @samp{IF_spTOS(spTOS = sp[0])}) at
  934: the end of problematic C code.  Vmgen inserts a stack pointer update
  935: before the start of the user-supplied C code, so the flushing code has
  936: to use an index that corrects for that.  In the future, this flushing
  937: may be done automatically by mentioning a special string in the C code.
  938: @c sometimes flushing and/or reloading unnecessary
  939: 
  940: @item
  941: The Vmgen-erated code loads the stack items from stack-pointer-indexed
  942: memory into variables before the user-supplied C code, and stores them
  943: from variables to stack-pointer-indexed memory afterwards.  If you do
  944: any writes to the stack through its stack pointer in your C code, it
  945: will not affect the variables, and your write may be overwritten by the
  946: stores after the C code.  Similarly, a read from a stack using a stack
  947: pointer will not reflect computations of stack items in the same VM
  948: instruction.
  949: 
  950: @item
  951: Superinstructions keep stack items in variables across the whole
  952: superinstruction.  So you should not include VM instructions, that
  953: access a stack or stack pointer, as components of superinstructions
  954: (@pxref{VM profiler}).
  955: 
  956: @end itemize
  957: 
  958: You should access the instruction pointer only through its special
  959: macros (@samp{IP}, @samp{SET_IP}, @samp{IPTOS}); this ensure that these
  960: macros can be implemented in several ways for best performance.
  961: @samp{IP} points to the next instruction, and @samp{IPTOS} is its
  962: contents.
  963: 
  964: 
  965: @c --------------------------------------------------------------------
  966: @node Superinstructions, Register Machines, Simple instructions, Input File Format
  967: @section Superinstructions
  968: @cindex superinstructions, defining
  969: @cindex defining superinstructions
  970: 
  971: Note: don't invest too much work in (static) superinstructions; a future
  972: version of Vmgen will support dynamic superinstructions (see Ian
  973: Piumarta and Fabio Riccardi, @cite{Optimizing Direct Threaded Code by
  974: Selective Inlining}, PLDI'98), and static superinstructions have much
  975: less benefit in that context (preliminary results indicate only a factor
  976: 1.1 speedup).
  977: 
  978: Here is an example of a superinstruction definition:
  979: 
  980: @example
  981: lit_sub = lit sub
  982: @end example
  983: 
  984: @code{lit_sub} is the name of the superinstruction, and @code{lit} and
  985: @code{sub} are its components.  This superinstruction performs the same
  986: action as the sequence @code{lit} and @code{sub}.  It is generated
  987: automatically by the VM code generation functions whenever that sequence
  988: occurs, so if you want to use this superinstruction, you just need to
  989: add this definition (and even that can be partially automatized,
  990: @pxref{VM profiler}).
  991: 
  992: @cindex prefixes of superinstructions
  993: Vmgen requires that the component instructions are simple instructions
  994: defined before superinstructions using the components.  Currently, Vmgen
  995: also requires that all the subsequences at the start of a
  996: superinstruction (prefixes) must be defined as superinstruction before
  997: the superinstruction.  I.e., if you want to define a superinstruction
  998: 
  999: @example
 1000: foo4 = load add sub mul
 1001: @end example
 1002: 
 1003: you first have to define @code{load}, @code{add}, @code{sub} and
 1004: @code{mul}, plus
 1005: 
 1006: @example
 1007: foo2 = load add
 1008: foo3 = load add sub
 1009: @end example
 1010: 
 1011: Here, @code{sumof4} is the longest prefix of @code{sumof5}, and @code{sumof3}
 1012: is the longest prefix of @code{sumof4}.
 1013: 
 1014: Note that Vmgen assumes that only the code it generates accesses stack
 1015: pointers, the instruction pointer, and various stack items, and it
 1016: performs optimizations based on this assumption.  Therefore, VM
 1017: instructions where your C code changes the instruction pointer should
 1018: only be used as last component; a VM instruction where your C code
 1019: accesses a stack pointer should not be used as component at all.  Vmgen
 1020: does not check these restrictions, they just result in bugs in your
 1021: interpreter.
 1022: 
 1023: @c -------------------------------------------------------------------
 1024: @node Register Machines,  , Superinstructions, Input File Format
 1025: @section Register Machines
 1026: @cindex Register VM
 1027: @cindex Superinstructions for register VMs
 1028: @cindex tracing of register VMs
 1029: 
 1030: If you want to implement a register VM rather than a stack VM with
 1031: Vmgen, there are two ways to do it: Directly and through
 1032: superinstructions.
 1033: 
 1034: If you use the direct way, you define instructions that take the
 1035: register numbers as immediate arguments, like this:
 1036: 
 1037: @example
 1038: add3 ( #src1 #src2 #dest -- )
 1039: reg[dest] = reg[src1]+reg[src2];
 1040: @end example
 1041: 
 1042: A disadvantage of this method is that during tracing you only see the
 1043: register numbers, but not the register contents.  Actually, with an
 1044: appropriate definition of @code{printarg_src} (@pxref{VM engine}), you
 1045: can print the values of the source registers on entry, but you cannot
 1046: print the value of the destination register on exit.
 1047: 
 1048: If you use superinstructions to define a register VM, you define simple
 1049: instructions that use a stack, and then define superinstructions that
 1050: have no overall stack effect, like this:
 1051: 
 1052: @example
 1053: loadreg ( #src -- n )
 1054: n = reg[src];
 1055: 
 1056: storereg ( n #dest -- )
 1057: reg[dest] = n;
 1058: 
 1059: adds ( n1 n2 -- n )
 1060: n = n1+n2;
 1061: 
 1062: add3 = loadreg loadreg adds storereg
 1063: @end example
 1064: 
 1065: An advantage of this method is that you see the values and not just the
 1066: register numbers in tracing.  A disadvantage of this method is that
 1067: currently you cannot generate superinstructions directly, but only
 1068: through generating a sequence of simple instructions (we might change
 1069: this in the future if there is demand).
 1070: 
 1071: Could the register VM support be improved, apart from the issues
 1072: mentioned above?  It is hard to see how to do it in a general way,
 1073: because there are a number of different designs that different people
 1074: mean when they use the term @emph{register machine} in connection with
 1075: VM interpreters.  However, if you have ideas or requests in that
 1076: direction, please let me know (@pxref{Contact}).
 1077: 
 1078: @c ********************************************************************
 1079: @node Error messages, Using the generated code, Input File Format, Top
 1080: @chapter Error messages
 1081: @cindex error messages
 1082: 
 1083: These error messages are created by Vmgen:
 1084: 
 1085: @table @code
 1086: 
 1087: @cindex @code{# can only be on the input side} error
 1088: @item # can only be on the input side
 1089: You have used an instruction-stream prefix (usually @samp{#}) after the
 1090: @samp{--} (the output side); you can only use it before (the input
 1091: side).
 1092: 
 1093: @cindex @code{prefix for this combination must be defined earlier} error
 1094: @item the prefix for this combination must be defined earlier
 1095: You have defined a superinstruction (e.g. @code{abc = a b c}) without
 1096: defining its direct prefix (e.g., @code{ab = a b}),
 1097: @xref{Superinstructions}.
 1098: 
 1099: @cindex @code{sync line syntax} error
 1100: @item sync line syntax
 1101: If you are using a preprocessor (e.g., @command{m4}) to generate Vmgen
 1102: input code, you may want to create @code{#line} directives (aka sync
 1103: lines).  This error indicates that such a line is not in th syntax
 1104: expected by Vmgen (this should not happen; please report the offending
 1105: line in a bug report).
 1106: 
 1107: @cindex @code{syntax error, wrong char} error
 1108: @cindex syntax error, wrong char
 1109: A syntax error.  If you do not see right away where the error is, it may
 1110: be helpful to check the following: Did you put an empty line in a VM
 1111: instruction where the C code is not delimited by braces (then the empty
 1112: line ends the VM instruction)?  If you used brace-delimited C code, did
 1113: you put the delimiting braces (and only those) at the start of the line,
 1114: without preceding white space?  Did you forget a delimiting brace?
 1115: 
 1116: @cindex @code{too many stacks} error
 1117: @item too many stacks
 1118: Vmgen currently supports 3 stacks (plus the instruction stream); if you
 1119: need more, let us know.
 1120: 
 1121: @cindex @code{unknown prefix} error
 1122: @item unknown prefix
 1123: The stack item does not match any defined type prefix (after stripping
 1124: away any stack prefix).  You should either declare the type prefix you
 1125: want for that stack item, or use a different type prefix
 1126: 
 1127: @item @code{unknown primitive} error
 1128: @item unknown primitive
 1129: You have used the name of a simple VM instruction in a superinstruction
 1130: definition without defining the simple VM instruction first.
 1131: 
 1132: @end table
 1133: 
 1134: In addition, the C compiler can produce errors due to code produced by
 1135: Vmgen; e.g., you need to define type cast functions.
 1136: 
 1137: @c ********************************************************************
 1138: @node Using the generated code, Hints, Error messages, Top
 1139: @chapter Using the generated code
 1140: @cindex generated code, usage
 1141: @cindex Using vmgen-erated code
 1142: 
 1143: The easiest way to create a working VM interpreter with Vmgen is
 1144: probably to start with @file{vmgen-ex}, and modify it for your purposes.
 1145: This chapter explains what the various wrapper and generated files do.
 1146: It also contains reference-manual style descriptions of the macros,
 1147: variables etc. used by the generated code, and you can skip that on
 1148: first reading.
 1149: 
 1150: @menu
 1151: * VM engine::                   Executing VM code
 1152: * VM instruction table::        
 1153: * VM code generation::          Creating VM code (in the front-end)
 1154: * Peephole optimization::       Creating VM superinstructions
 1155: * VM disassembler::             for debugging the front end
 1156: * VM profiler::                 for finding worthwhile superinstructions
 1157: @end menu
 1158: 
 1159: @c --------------------------------------------------------------------
 1160: @node VM engine, VM instruction table, Using the generated code, Using the generated code
 1161: @section VM engine
 1162: @cindex VM instruction execution
 1163: @cindex engine
 1164: @cindex executing VM code
 1165: @cindex @file{engine.c}
 1166: @cindex @file{-vm.i} output file
 1167: 
 1168: The VM engine is the VM interpreter that executes the VM code.  It is
 1169: essential for an interpretive system.
 1170: 
 1171: Vmgen supports two methods of VM instruction dispatch: @emph{threaded
 1172: code} (fast, but gcc-specific), and @emph{switch dispatch} (slow, but
 1173: portable across C compilers); you can use conditional compilation
 1174: (@samp{defined(__GNUC__)}) to choose between these methods, and our
 1175: example does so.
 1176: 
 1177: For both methods, the VM engine is contained in a C-level function.
 1178: Vmgen generates most of the contents of the function for you
 1179: (@file{@var{name}-vm.i}), but you have to define this function, and
 1180: macros and variables used in the engine, and initialize the variables.
 1181: In our example the engine function also includes
 1182: @file{@var{name}-labels.i} (@pxref{VM instruction table}).
 1183: 
 1184: @cindex tracing VM code
 1185: @cindex superinstructions and tracing
 1186: In addition to executing the code, the VM engine can optionally also
 1187: print out a trace of the executed instructions, their arguments and
 1188: results.  For superinstructions it prints the trace as if only component
 1189: instructions were executed; this allows to introduce new
 1190: superinstructions while keeping the traces comparable to old ones
 1191: (important for regression tests).
 1192: 
 1193: It costs significant performance to check in each instruction whether to
 1194: print tracing code, so we recommend producing two copies of the engine:
 1195: one for fast execution, and one for tracing.  See the rules for
 1196: @file{engine.o} and @file{engine-debug.o} in @file{vmgen-ex/Makefile}
 1197: for an example.
 1198: 
 1199: The following macros and variables are used in @file{@var{name}-vm.i}:
 1200: 
 1201: @table @code
 1202: 
 1203: @findex LABEL
 1204: @item LABEL(@var{inst_name})
 1205: This is used just before each VM instruction to provide a jump or
 1206: @code{switch} label (the @samp{:} is provided by Vmgen).  For switch
 1207: dispatch this should expand to @samp{case @var{label}:}; for
 1208: threaded-code dispatch this should just expand to @samp{@var{label}:}.
 1209: In either case @var{label} is usually the @var{inst_name} with some
 1210: prefix or suffix to avoid naming conflicts.
 1211: 
 1212: @findex LABEL2
 1213: @item LABEL2(@var{inst_name})
 1214: This will be used for dynamic superinstructions; at the moment, this
 1215: should expand to nothing.
 1216: 
 1217: @findex NAME
 1218: @item NAME(@var{inst_name_string})
 1219: Called on entering a VM instruction with a string containing the name of
 1220: the VM instruction as parameter.  In normal execution this should be
 1221: expand to nothing, but for tracing this usually prints the name, and
 1222: possibly other information (several VM registers in our example).
 1223: 
 1224: @findex DEF_CA
 1225: @item DEF_CA
 1226: Usually empty.  Called just inside a new scope at the start of a VM
 1227: instruction.  Can be used to define variables that should be visible
 1228: during every VM instruction.  If you define this macro as non-empty, you
 1229: have to provide the finishing @samp{;} in the macro.
 1230: 
 1231: @findex NEXT_P0
 1232: @findex NEXT_P1
 1233: @findex NEXT_P2
 1234: @item NEXT_P0 NEXT_P1 NEXT_P2
 1235: The three parts of instruction dispatch.  They can be defined in
 1236: different ways for best performance on various processors (see
 1237: @file{engine.c} in the example or @file{engine/threaded.h} in Gforth).
 1238: @samp{NEXT_P0} is invoked right at the start of the VM instruction (but
 1239: after @samp{DEF_CA}), @samp{NEXT_P1} right after the user-supplied C
 1240: code, and @samp{NEXT_P2} at the end.  The actual jump has to be
 1241: performed by @samp{NEXT_P2} (if you would do it earlier, important parts
 1242: of the VM instruction would not be executed).
 1243: 
 1244: The simplest variant is if @samp{NEXT_P2} does everything and the other
 1245: macros do nothing.  Then also related macros like @samp{IP},
 1246: @samp{SET_IP}, @samp{IP}, @samp{INC_IP} and @samp{IPTOS} are very
 1247: straightforward to define.  For switch dispatch this code consists just
 1248: of a jump to the dispatch code (@samp{goto next_inst;} in our example);
 1249: for direct threaded code it consists of something like
 1250: @samp{(@{cfa=*ip++; goto *cfa;@})}.
 1251: 
 1252: Pulling code (usually the @samp{cfa=*ip++;}) up into @samp{NEXT_P1}
 1253: usually does not cause problems, but pulling things up into
 1254: @samp{NEXT_P0} usually requires changing the other macros (and, at least
 1255: for Gforth on Alpha, it does not buy much, because the compiler often
 1256: manages to schedule the relevant stuff up by itself).  An even more
 1257: extreme variant is to pull code up even further, into, e.g., NEXT_P1 of
 1258: the previous VM instruction (prefetching, useful on PowerPCs).
 1259: 
 1260: @findex INC_IP
 1261: @item INC_IP(@var{n})
 1262: This increments @code{IP} by @var{n}.
 1263: 
 1264: @findex SET_IP
 1265: @item SET_IP(@var{target})
 1266: This sets @code{IP} to @var{target}.
 1267: 
 1268: @cindex type cast macro
 1269: @findex vm_@var{A}2@var{B}
 1270: @item vm_@var{A}2@var{B}(a,b)
 1271: Type casting macro that assigns @samp{a} (of type @var{A}) to @samp{b}
 1272: (of type @var{B}).  This is mainly used for getting stack items into
 1273: variables and back.  So you need to define macros for every combination
 1274: of stack basic type (@code{Cell} in our example) and type-prefix types
 1275: used with that stack (in both directions).  For the type-prefix type,
 1276: you use the type-prefix (not the C type string) as type name (e.g.,
 1277: @samp{vm_Cell2i}, not @samp{vm_Cell2Cell}).  In addition, you have to
 1278: define a vm_@var{X}2@var{X} macro for the stack's basic type @var{X}
 1279: (used in superinstructions).
 1280: 
 1281: @cindex instruction stream, basic type
 1282: The stack basic type for the predefined @samp{inst-stream} is
 1283: @samp{Cell}.  If you want a stack with the same item size, making its
 1284: basic type @samp{Cell} usually reduces the number of macros you have to
 1285: define.
 1286: 
 1287: @cindex unions in type cast macros
 1288: @cindex casts in type cast macros
 1289: @cindex type casting between floats and integers
 1290: Here our examples differ a lot: @file{vmgen-ex} uses casts in these
 1291: macros, whereas @file{vmgen-ex2} uses union-field selection (or
 1292: assignment to union fields).  Note that casting floats into integers and
 1293: vice versa changes the bit pattern (and you do not want that).  In this
 1294: case your options are to use a (temporary) union, or to take the address
 1295: of the value, cast the pointer, and dereference that (not always
 1296: possible, and sometimes expensive).
 1297: 
 1298: @findex vm_two@var{A}2@var{B}
 1299: @findex vm_@var{B}2two@var{A}
 1300: @item vm_two@var{A}2@var{B}(a1,a2,b)
 1301: @item vm_@var{B}2two@var{A}(b,a1,a2)
 1302: Type casting between two stack items (@code{a1}, @code{a2}) and a
 1303: variable @code{b} of a type that takes two stack items.  This does not
 1304: occur in our small examples, but you can look at Gforth for examples
 1305: (see @code{vm_twoCell2d} in @file{engine/forth.h}).
 1306: 
 1307: @cindex stack pointer definition
 1308: @cindex instruction pointer definition
 1309: @item @var{stackpointer}
 1310: For each stack used, the stackpointer name given in the stack
 1311: declaration is used.  For a regular stack this must be an l-expression;
 1312: typically it is a variable declared as a pointer to the stack's basic
 1313: type.  For @samp{inst-stream}, the name is @samp{IP}, and it can be a
 1314: plain r-value; typically it is a macro that abstracts away the
 1315: differences between the various implementations of @code{NEXT_P*}.
 1316: 
 1317: @cindex top of stack caching
 1318: @cindex stack caching
 1319: @cindex TOS
 1320: @findex IPTOS
 1321: @item @var{stackpointer}TOS
 1322: The top-of-stack for the stack pointed to by @var{stackpointer}.  If you
 1323: are using top-of-stack caching for that stack, this should be defined as
 1324: variable; if you are not using top-of-stack caching for that stack, this
 1325: should be a macro expanding to @samp{@var{stackpointer}[0]}.  The stack
 1326: pointer for the predefined @samp{inst-stream} is called @samp{IP}, so
 1327: the top-of-stack is called @samp{IPTOS}.
 1328: 
 1329: @findex IF_@var{stackpointer}TOS
 1330: @item IF_@var{stackpointer}TOS(@var{expr})
 1331: Macro for executing @var{expr}, if top-of-stack caching is used for the
 1332: @var{stackpointer} stack.  I.e., this should do @var{expr} if there is
 1333: top-of-stack caching for @var{stackpointer}; otherwise it should do
 1334: nothing.
 1335: 
 1336: @findex SUPER_END
 1337: @item SUPER_END
 1338: This is used by the VM profiler (@pxref{VM profiler}); it should not do
 1339: anything in normal operation, and call @code{vm_count_block(IP)} for
 1340: profiling.
 1341: 
 1342: @findex SUPER_CONTINUE
 1343: @item SUPER_CONTINUE
 1344: This is just a hint to Vmgen and does nothing at the C level.
 1345: 
 1346: @findex VM_DEBUG
 1347: @item VM_DEBUG
 1348: If this is defined, the tracing code will be compiled in (slower
 1349: interpretation, but better debugging).  Our example compiles two
 1350: versions of the engine, a fast-running one that cannot trace, and one
 1351: with potential tracing and profiling.
 1352: 
 1353: @findex vm_debug
 1354: @item vm_debug
 1355: Needed only if @samp{VM_DEBUG} is defined.  If this variable contains
 1356: true, the VM instructions produce trace output.  It can be turned on or
 1357: off at any time.
 1358: 
 1359: @findex vm_out
 1360: @item vm_out
 1361: Needed only if @samp{VM_DEBUG} is defined.  Specifies the file on which
 1362: to print the trace output (type @samp{FILE *}).
 1363: 
 1364: @findex printarg_@var{type}
 1365: @item printarg_@var{type}(@var{value})
 1366: Needed only if @samp{VM_DEBUG} is defined.  Macro or function for
 1367: printing @var{value} in a way appropriate for the @var{type}.  This is
 1368: used for printing the values of stack items during tracing.  @var{Type}
 1369: is normally the type prefix specified in a @code{type-prefix} definition
 1370: (e.g., @samp{printarg_i}); in superinstructions it is currently the
 1371: basic type of the stack.
 1372: 
 1373: @end table
 1374: 
 1375: 
 1376: @c --------------------------------------------------------------------
 1377: @node VM instruction table, VM code generation, VM engine, Using the generated code
 1378: @section VM instruction table
 1379: @cindex instruction table
 1380: @cindex opcode definition
 1381: @cindex labels for threaded code
 1382: @cindex @code{vm_prim}, definition
 1383: @cindex @file{-labels.i} output file
 1384: 
 1385: For threaded code we also need to produce a table containing the labels
 1386: of all VM instructions.  This is needed for VM code generation
 1387: (@pxref{VM code generation}), and it has to be done in the engine
 1388: function, because the labels are not visible outside.  It then has to be
 1389: passed outside the function (and assigned to @samp{vm_prim}), to be used
 1390: by the VM code generation functions.
 1391: 
 1392: This means that the engine function has to be called first to produce
 1393: the VM instruction table, and later, after generating VM code, it has to
 1394: be called again to execute the generated VM code (yes, this is ugly).
 1395: In our example program, these two modes of calling the engine function
 1396: are differentiated by the value of the parameter ip0 (if it equals 0,
 1397: then the table is passed out, otherwise the VM code is executed); in our
 1398: example, we pass the table out by assigning it to @samp{vm_prim} and
 1399: returning from @samp{engine}.
 1400: 
 1401: In our example (@file{vmgen-ex/engine.c}), we also build such a table for
 1402: switch dispatch; this is mainly done for uniformity.
 1403: 
 1404: For switch dispatch, we also need to define the VM instruction opcodes
 1405: used as case labels in an @code{enum}.
 1406: 
 1407: For both purposes (VM instruction table, and enum), the file
 1408: @file{@var{name}-labels.i} is generated by Vmgen.  You have to define
 1409: the following macro used in this file:
 1410: 
 1411: @table @code
 1412: 
 1413: @findex INST_ADDR
 1414: @item INST_ADDR(@var{inst_name})
 1415: For switch dispatch, this is just the name of the switch label (the same
 1416: name as used in @samp{LABEL(@var{inst_name})}), for both uses of
 1417: @file{@var{name}-labels.i}.  For threaded-code dispatch, this is the
 1418: address of the label defined in @samp{LABEL(@var{inst_name})}); the
 1419: address is taken with @samp{&&} (@pxref{Labels as Values, , Labels as
 1420: Values, gcc.info, GNU C Manual}).
 1421: 
 1422: @end table
 1423: 
 1424: 
 1425: @c --------------------------------------------------------------------
 1426: @node VM code generation, Peephole optimization, VM instruction table, Using the generated code
 1427: @section VM code generation
 1428: @cindex VM code generation
 1429: @cindex code generation, VM
 1430: @cindex @file{-gen.i} output file
 1431: 
 1432: Vmgen generates VM code generation functions in @file{@var{name}-gen.i}
 1433: that the front end can call to generate VM code.  This is essential for
 1434: an interpretive system.
 1435: 
 1436: @findex gen_@var{inst}
 1437: For a VM instruction @samp{x ( #a b #c -- d )}, Vmgen generates a
 1438: function with the prototype
 1439: 
 1440: @example
 1441: void gen_x(Inst **ctp, a_type a, c_type c)
 1442: @end example
 1443: 
 1444: The @code{ctp} argument points to a pointer to the next instruction.
 1445: @code{*ctp} is increased by the generation functions; i.e., you should
 1446: allocate memory for the code to be generated beforehand, and start with
 1447: *ctp set at the start of this memory area.  Before running out of
 1448: memory, allocate a new area, and generate a VM-level jump to the new
 1449: area (this overflow handling is not implemented in our examples).
 1450: 
 1451: @cindex immediate arguments, VM code generation
 1452: The other arguments correspond to the immediate arguments of the VM
 1453: instruction (with their appropriate types as defined in the
 1454: @code{type_prefix} declaration.
 1455: 
 1456: The following types, variables, and functions are used in
 1457: @file{@var{name}-gen.i}:
 1458: 
 1459: @table @code
 1460: 
 1461: @findex Inst
 1462: @item Inst
 1463: The type of the VM instruction; if you use threaded code, this is
 1464: @code{void *}; for switch dispatch this is an integer type.
 1465: 
 1466: @cindex @code{vm_prim}, use
 1467: @item vm_prim
 1468: The VM instruction table (type: @code{Inst *}, @pxref{VM instruction table}).
 1469: 
 1470: @findex gen_inst
 1471: @item gen_inst(Inst **ctp, Inst i)
 1472: This function compiles the instruction @code{i}.  Take a look at it in
 1473: @file{vmgen-ex/peephole.c}.  It is trivial when you don't want to use
 1474: superinstructions (just the last two lines of the example function), and
 1475: slightly more complicated in the example due to its ability to use
 1476: superinstructions (@pxref{Peephole optimization}).
 1477: 
 1478: @findex genarg_@var{type_prefix}
 1479: @item genarg_@var{type_prefix}(Inst **ctp, @var{type} @var{type_prefix})
 1480: This compiles an immediate argument of @var{type} (as defined in a
 1481: @code{type-prefix} definition).  These functions are trivial to define
 1482: (see @file{vmgen-ex/support.c}).  You need one of these functions for
 1483: every type that you use as immediate argument.
 1484: 
 1485: @end table
 1486: 
 1487: @findex BB_BOUNDARY
 1488: In addition to using these functions to generate code, you should call
 1489: @code{BB_BOUNDARY} at every basic block entry point if you ever want to
 1490: use superinstructions (or if you want to use the profiling supported by
 1491: Vmgen; but this support is also useful mainly for selecting
 1492: superinstructions).  If you use @code{BB_BOUNDARY}, you should also
 1493: define it (take a look at its definition in @file{vmgen-ex/mini.y}).
 1494: 
 1495: You do not need to call @code{BB_BOUNDARY} after branches, because you
 1496: will not define superinstructions that contain branches in the middle
 1497: (and if you did, and it would work, there would be no reason to end the
 1498: superinstruction at the branch), and because the branches announce
 1499: themselves to the profiler.
 1500: 
 1501: 
 1502: @c --------------------------------------------------------------------
 1503: @node Peephole optimization, VM disassembler, VM code generation, Using the generated code
 1504: @section Peephole optimization
 1505: @cindex peephole optimization
 1506: @cindex superinstructions, generating
 1507: @cindex @file{peephole.c}
 1508: @cindex @file{-peephole.i} output file
 1509: 
 1510: You need peephole optimization only if you want to use
 1511: superinstructions.  But having the code for it does not hurt much if you
 1512: do not use superinstructions.
 1513: 
 1514: A simple greedy peephole optimization algorithm is used for
 1515: superinstruction selection: every time @code{gen_inst} compiles a VM
 1516: instruction, it checks if it can combine it with the last VM instruction
 1517: (which may also be a superinstruction resulting from a previous peephole
 1518: optimization); if so, it changes the last instruction to the combined
 1519: instruction instead of laying down @code{i} at the current @samp{*ctp}.
 1520: 
 1521: The code for peephole optimization is in @file{vmgen-ex/peephole.c}.
 1522: You can use this file almost verbatim.  Vmgen generates
 1523: @file{@var{file}-peephole.i} which contains data for the peephoile
 1524: optimizer.
 1525: 
 1526: @findex init_peeptable
 1527: You have to call @samp{init_peeptable()} after initializing
 1528: @samp{vm_prim}, and before compiling any VM code to initialize data
 1529: structures for peephole optimization.  After that, compiling with the VM
 1530: code generation functions will automatically combine VM instructions
 1531: into superinstructions.  Since you do not want to combine instructions
 1532: across VM branch targets (otherwise there will not be a proper VM
 1533: instruction to branch to), you have to call @code{BB_BOUNDARY}
 1534: (@pxref{VM code generation}) at branch targets.
 1535: 
 1536: 
 1537: @c --------------------------------------------------------------------
 1538: @node VM disassembler, VM profiler, Peephole optimization, Using the generated code
 1539: @section VM disassembler
 1540: @cindex VM disassembler
 1541: @cindex disassembler, VM code
 1542: @cindex @file{disasm.c}
 1543: @cindex @file{-disasm.i} output file
 1544: 
 1545: A VM code disassembler is optional for an interpretive system, but
 1546: highly recommended during its development and maintenance, because it is
 1547: very useful for detecting bugs in the front end (and for distinguishing
 1548: them from VM interpreter bugs).
 1549: 
 1550: Vmgen supports VM code disassembling by generating
 1551: @file{@var{file}-disasm.i}.  This code has to be wrapped into a
 1552: function, as is done in @file{vmgen-ex/disasm.c}.  You can use this file
 1553: almost verbatim.  In addition to @samp{vm_@var{A}2@var{B}(a,b)},
 1554: @samp{vm_out}, @samp{printarg_@var{type}(@var{value})}, which are
 1555: explained above, the following macros and variables are used in
 1556: @file{@var{file}-disasm.i} (and you have to define them):
 1557: 
 1558: @table @code
 1559: 
 1560: @item ip
 1561: This variable points to the opcode of the current VM instruction.
 1562: 
 1563: @cindex @code{IP}, @code{IPTOS} in disassmbler
 1564: @item IP IPTOS
 1565: @samp{IPTOS} is the first argument of the current VM instruction, and
 1566: @samp{IP} points to it; this is just as in the engine, but here
 1567: @samp{ip} points to the opcode of the VM instruction (in contrast to the
 1568: engine, where @samp{ip} points to the next cell, or even one further).
 1569: 
 1570: @findex VM_IS_INST
 1571: @item VM_IS_INST(Inst i, int n)
 1572: Tests if the opcode @samp{i} is the same as the @samp{n}th entry in the
 1573: VM instruction table.
 1574: 
 1575: @end table
 1576: 
 1577: 
 1578: @c --------------------------------------------------------------------
 1579: @node VM profiler,  , VM disassembler, Using the generated code
 1580: @section VM profiler
 1581: @cindex VM profiler
 1582: @cindex profiling for selecting superinstructions
 1583: @cindex superinstructions and profiling
 1584: @cindex @file{profile.c}
 1585: @cindex @file{-profile.i} output file
 1586: 
 1587: The VM profiler is designed for getting execution and occurence counts
 1588: for VM instruction sequences, and these counts can then be used for
 1589: selecting sequences as superinstructions.  The VM profiler is probably
 1590: not useful as profiling tool for the interpretive system.  I.e., the VM
 1591: profiler is useful for the developers, but not the users of the
 1592: interpretive system.
 1593: 
 1594: The output of the profiler is: for each basic block (executed at least
 1595: once), it produces the dynamic execution count of that basic block and
 1596: all its subsequences; e.g.,
 1597: 
 1598: @example
 1599:        9227465  lit storelocal 
 1600:        9227465  storelocal branch 
 1601:        9227465  lit storelocal branch 
 1602: @end example
 1603: 
 1604: I.e., a basic block consisting of @samp{lit storelocal branch} is
 1605: executed 9227465 times.
 1606: 
 1607: @cindex @file{stat.awk}
 1608: @cindex @file{seq2rule.awk}
 1609: This output can be combined in various ways.  E.g.,
 1610: @file{vmgen-ex/stat.awk} adds up the occurences of a given sequence wrt
 1611: dynamic execution, static occurence, and per-program occurence.  E.g.,
 1612: 
 1613: @example
 1614:       2      16        36910041 loadlocal lit 
 1615: @end example
 1616: 
 1617: @noindent
 1618: indicates that the sequence @samp{loadlocal lit} occurs in 2 programs,
 1619: in 16 places, and has been executed 36910041 times.  Now you can select
 1620: superinstructions in any way you like (note that compile time and space
 1621: typically limit the number of superinstructions to 100--1000).  After
 1622: you have done that, @file{vmgen/seq2rule.awk} turns lines of the form
 1623: above into rules for inclusion in a Vmgen input file.  Note that this
 1624: script does not ensure that all prefixes are defined, so you have to do
 1625: that in other ways.  So, an overall script for turning profiles into
 1626: superinstructions can look like this:
 1627: 
 1628: @example
 1629: awk -f stat.awk fib.prof test.prof|
 1630: awk '$3>=10000'|                #select sequences
 1631: fgrep -v -f peephole-blacklist| #eliminate wrong instructions
 1632: awk -f seq2rule.awk|            #turn into superinstructions
 1633: sort -k 3 >mini-super.vmg       #sort sequences
 1634: @end example
 1635: 
 1636: Here the dynamic count is used for selecting sequences (preliminary
 1637: results indicate that the static count gives better results, though);
 1638: the third line eliminates sequences containing instructions that must not
 1639: occur in a superinstruction, because they access a stack directly.  The
 1640: dynamic count selection ensures that all subsequences (including
 1641: prefixes) of longer sequences occur (because subsequences have at least
 1642: the same count as the longer sequences); the sort in the last line
 1643: ensures that longer superinstructions occur after their prefixes.
 1644: 
 1645: But before using this, you have to have the profiler.  Vmgen supports its
 1646: creation by generating @file{@var{file}-profile.i}; you also need the
 1647: wrapper file @file{vmgen-ex/profile.c} that you can use almost verbatim.
 1648: 
 1649: @cindex @code{SUPER_END} in profiling
 1650: @cindex @code{BB_BOUNDARY} in profiling
 1651: The profiler works by recording the targets of all VM control flow
 1652: changes (through @code{SUPER_END} during execution, and through
 1653: @code{BB_BOUNDARY} in the front end), and counting (through
 1654: @code{SUPER_END}) how often they were targeted.  After the program run,
 1655: the numbers are corrected such that each VM basic block has the correct
 1656: count (entering a block without executing a branch does not increase the
 1657: count, and the correction fixes that), then the subsequences of all
 1658: basic blocks are printed.  To get all this, you just have to define
 1659: @code{SUPER_END} (and @code{BB_BOUNDARY}) appropriately, and call
 1660: @code{vm_print_profile(FILE *file)} when you want to output the profile
 1661: on @code{file}.
 1662: 
 1663: @cindex @code{VM_IS_INST} in profiling
 1664: The @file{@var{file}-profile.i} is similar to the disassembler file, and
 1665: it uses variables and functions defined in @file{vmgen-ex/profile.c},
 1666: plus @code{VM_IS_INST} already defined for the VM disassembler
 1667: (@pxref{VM disassembler}).
 1668: 
 1669: @c **********************************************************
 1670: @node Hints, The future, Using the generated code, Top
 1671: @chapter Hints
 1672: @cindex hints
 1673: 
 1674: @menu
 1675: * Floating point::              and stacks
 1676: @end menu
 1677: 
 1678: @c --------------------------------------------------------------------
 1679: @node Floating point,  , Hints, Hints
 1680: @section Floating point
 1681: 
 1682: How should you deal with floating point values?  Should you use the same
 1683: stack as for integers/pointers, or a different one?  This section
 1684: discusses this issue with a view on execution speed.
 1685: 
 1686: The simpler approach is to use a separate floating-point stack.  This
 1687: allows you to choose FP value size without considering the size of the
 1688: integers/pointers, and you avoid a number of performance problems.  The
 1689: main downside is that this needs an FP stack pointer (and that may not
 1690: fit in the register file on the 386 arhitecture, costing some
 1691: performance, but comparatively little if you take the other option into
 1692: account).  If you use a separate FP stack (with stack pointer @code{fp}),
 1693: using an fpTOS is helpful on most machines, but some spill the fpTOS
 1694: register into memory, and fpTOS should not be used there.
 1695: 
 1696: The other approach is to share one stack (pointed to by, say, @code{sp})
 1697: between integer/pointer and floating-point values.  This is ok if you do
 1698: not use @code{spTOS}.  If you do use @code{spTOS}, the compiler has to
 1699: decide whether to put that variable into an integer or a floating point
 1700: register, and the other type of operation becomes quite expensive on
 1701: most machines (because moving values between integer and FP registers is
 1702: quite expensive).  If a value of one type has to be synthesized out of
 1703: two values of the other type (@code{double} types), things are even more
 1704: interesting.
 1705: 
 1706: One way around this problem would be to not use the @code{spTOS}
 1707: supported by Vmgen, but to use explicit top-of-stack variables (one for
 1708: integers, one for FP values), and having a kind of accumulator+stack
 1709: architecture (e.g., Ocaml bytecode uses this approach); however, this is
 1710: a major change, and it's ramifications are not completely clear.
 1711: 
 1712: @c **********************************************************
 1713: @node The future, Changes, Hints, Top
 1714: @chapter The future
 1715: @cindex future ideas
 1716: 
 1717: We have a number of ideas for future versions of Gforth.  However, there
 1718: are so many possible things to do that we would like some feedback from
 1719: you.  What are you doing with Vmgen, what features are you missing, and
 1720: why?
 1721: 
 1722: One idea we are thinking about is to generate just one @file{.c} file
 1723: instead of letting you copy and adapt all the wrapper files (you would
 1724: still have to define stuff like the type-specific macros, and stack
 1725: pointers etc. somewhere).  The advantage would be that, if we change the
 1726: wrapper files between versions, you would not need to integrate your
 1727: changes and our changes to them; Vmgen would also be easier to use for
 1728: beginners.  The main disadvantage of that is that it would reduce the
 1729: flexibility of Vmgen a little (well, those who like flexibility could
 1730: still patch the resulting @file{.c} file, like they are now doing for
 1731: the wrapper files).  In any case, if you are doing things to the wrapper
 1732: files that would cause problems in a generated-@file{.c}-file approach,
 1733: please let us know.
 1734: 
 1735: @c **********************************************************
 1736: @node Changes, Contact, The future, Top
 1737: @chapter Changes
 1738: @cindex Changes from old versions
 1739: 
 1740: Users of the gforth-0.5.9-20010501 version of Vmgen need to change
 1741: several things in their source code to use the current version.  I
 1742: recommend keeping the gforth-0.5.9-20010501 version until you have
 1743: completed the change (note that you can have several versions of Gforth
 1744: installed at the same time).  I hope to avoid such incompatible changes
 1745: in the future.
 1746: 
 1747: The required changes are:
 1748: 
 1749: @table @code
 1750: 
 1751: @cindex @code{TAIL;}, changes
 1752: @item TAIL;
 1753: has been renamed into @code{INST_TAIL;} (less chance of an accidental
 1754: match).
 1755: 
 1756: @cindex @code{vm_@var{A}2@var{B}}, changes
 1757: @item vm_@var{A}2@var{B}
 1758: now takes two arguments.
 1759: 
 1760: @cindex @code{vm_two@var{A}2@var{B}}, changes
 1761: @item vm_two@var{A}2@var{B}(b,a1,a2);
 1762: changed to vm_two@var{A}2@var{B}(a1,a2,b) (note the absence of the @samp{;}).
 1763: 
 1764: @end table
 1765: 
 1766: Also some new macros have to be defined, e.g., @code{INST_ADDR}, and
 1767: @code{LABEL}; some macros have to be defined in new contexts, e.g.,
 1768: @code{VM_IS_INST} is now also needed in the disassembler.
 1769: 
 1770: @c *********************************************************
 1771: @node Contact, Copying This Manual, Changes, Top
 1772: @chapter Contact
 1773: 
 1774: @c ***********************************************************
 1775: @node Copying This Manual, Index, Contact, Top
 1776: @appendix Copying This Manual
 1777: 
 1778: @menu
 1779: * GNU Free Documentation License::  License for copying this manual.
 1780: @end menu
 1781: 
 1782: @include fdl.texi
 1783: 
 1784: 
 1785: @node Index,  , Copying This Manual, Top
 1786: @unnumbered Index
 1787: 
 1788: @printindex cp
 1789: 
 1790: @bye

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