Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / SLOF / slof / fs / stack.fs
1 \ *****************************************************************************
2 \ * Copyright (c) 2004, 2008 IBM Corporation
3 \ * All rights reserved.
4 \ * This program and the accompanying materials
5 \ * are made available under the terms of the BSD License
6 \ * which accompanies this distribution, and is available at
7 \ * http://www.opensource.org/licenses/bsd-license.php
8 \ *
9 \ * Contributors:
10 \ *     IBM Corporation - initial implementation
11 \ ****************************************************************************/
12
13
14 \ Example:
15 \
16 \ To get a 30 element stack, go:
17 \
18 \ 0 > 30 new-stack my-stack
19 \ 0 > my-stack
20 \ 0 > 20 push 30 push
21 \ 0 > pop pop .s
22
23 0 value current-stack
24
25 : new-stack ( cells <>name -- )
26    create >r here    ( here R: cells )
27    dup r@ 2 + cells  ( here here bytes R: cells )
28    dup allot erase   ( here R: cells)
29    cell+ r>          ( here+1cell cells )
30    swap !            ( )
31    DOES> to current-stack
32 ;
33
34 : reset-stack ( -- )
35    0 current-stack !
36 ;
37
38 : stack-depth ( -- depth )
39    current-stack @
40 ;
41
42 : push ( value -- )
43    current-stack @
44    current-stack cell+ @ over <= ABORT" Stack overflow"
45    cells
46    1 current-stack +!
47    current-stack 2 cells + + !
48 ;
49
50 : pop ( -- value )
51    current-stack @ 0= ABORT" Stack underflow"
52    current-stack @ cells
53    current-stack + cell+ @
54    -1 current-stack +!
55 ;
56
57