Annotation of gforth/vmgen-ex/mini.vmg, revision 1.2

1.1       anton       1: \ example .vmg file
                      2: 
                      3: \ Copyright (C) 2001 Free Software Foundation, Inc.
                      4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
                     20: 
                     21: 
                     22: \ WARNING: This file is processed by m4. Make sure your identifiers
                     23: \ don't collide with m4's (e.g. by undefining them).
                     24: 
                     25: 
                     26: \ comments start with "\ "
                     27: 
                     28: \ stack definitions:
                     29: \E stack data-stack sp Cell
                     30: 
                     31: \ stack prefix definitions
                     32: \E inst-stream stack-prefix #
                     33: 
                     34: \ type prefix definitions:
                     35: \E s" Cell"   single data-stack type-prefix i
                     36: \E s" char *" single data-stack type-prefix a
                     37: \E s" Inst *" single data-stack type-prefix target
                     38: 
                     39: \ simple VM instructions:
                     40: add  ( i1 i2 -- i )
                     41: i = i1+i2;
                     42: 
                     43: sub ( i1 i2 -- i )
                     44: i = i1-i2;
                     45: 
                     46: mul ( i1 i2 -- i )
                     47: i = i1*i2;
                     48: 
                     49: and ( i1 i2 -- i )
                     50: i = i1 & i2;
                     51: 
                     52: or ( i1 i2 -- i )
                     53: i = i1 | i2;
                     54: 
                     55: lessthan ( i1 i2 -- i )
                     56: i = i1<i2;
                     57: 
                     58: equals ( i1 i2 -- i )
                     59: i = i1==i2;
                     60: 
                     61: not ( i1 -- i2 )
                     62: i2 = !i1;
                     63: 
                     64: negate ( i1 -- i2 )
                     65: i2 = -i1;
                     66: 
                     67: lit ( #i -- i )
                     68: 
                     69: drop ( i -- )
                     70: 
                     71: print ( i -- )
                     72: printf("%ld\n", i);
                     73: 
                     74: branch ( #target -- )
                     75: SET_IP(target);
                     76: 
                     77: zbranch ( #target i -- )
                     78: if (i==0) {
                     79:   SET_IP(target);
                     80:   TAIL;
                     81: }
                     82: 
                     83: \  The stack is organized as follows:
                     84: \  The stack grows downwards; a stack usually looks like this:
                     85: 
                     86: \  higher addresses
                     87: \  --------------------- bottom of stack
                     88: \     locals of main
                     89: \     return address (points to VM code after call)
                     90: \  +->oldfp (NULL)
                     91: \  |  intermediate results (e.g., 1 for a call like 1+foo(...))
                     92: \  |  arguments passed to the called function
                     93: \  |  locals of the called function
                     94: \  |  return address (points to VM code after call)
                     95: \  +--oldfp                <-- fp
                     96: \     intermediate results <-- sp
                     97: \  ---------------------- top of stack
                     98: \  lower addresses
                     99: 
1.2     ! anton     100: \ The following VM instructions also explicitly reference sp and
        !           101: \ therefore may have to do something about spTOS caching.
        !           102: 
1.1       anton     103: call ( #target #iadjust -- targetret aoldfp )
1.2     ! anton     104: /* IF_spTOS(sp[2] = spTOS);*/ /* unnecessary; vmgen inserts a flush anyway */
1.1       anton     105: targetret = IP;
                    106: SET_IP(target);
                    107: aoldfp = fp;
                    108: sp = (Cell *)(((char *)sp)+iadjust);
                    109: fp = (char *)sp;
1.2     ! anton     110: /* IF_spTOS(spTOS = sp[0]); */ /* dead, thus unnecessary; vmgen copies aoldfp there */
1.1       anton     111: 
                    112: return ( #iadjust target afp i1 -- i2 )
1.2     ! anton     113: /* IF_spTOS(sp[-2] = spTOS); */ /* unnecessary; that stack item is dead */
1.1       anton     114: SET_IP(target);
                    115: sp = (Cell *)(((char *)sp)+iadjust);
                    116: fp = afp;
                    117: i2=i1;
1.2     ! anton     118: /* IF_spTOS(spTOS = sp[0]); */ /* dead, thus unnecessary; vmgen copies i2 there */
        !           119: 
        !           120: \ loadlocal and storelocal access stack items below spTOS, so we can
        !           121: \ ignore spTOS caching.
1.1       anton     122: 
                    123: loadlocal ( #ioffset -- i )
                    124: i = *(Cell *)(fp+ioffset);
                    125: 
                    126: storelocal ( #ioffset i -- )
                    127: *(Cell *)(fp+ioffset) = i;
                    128: 
                    129: end ( i -- )
                    130: /* SUPER_END would increment the next BB count (because IP points there);
                    131:    this would be a problem if there is no following BB.
                    132:    Instead, we do the following to add an end point for the current BB: */
                    133: #ifdef VM_PROFILING
1.2     ! anton     134: block_insert(IP); /* we also do this at compile time, so this is unnecessary */
1.1       anton     135: #endif
                    136: return i;

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