Moving the debug trace point to be prior to advancing %rsi
[rrq/rrqforth.git] / stack.asm
1 ;;; Words for stack manipulations
2         
3         WORD p_depth,'DEPTH',fasm
4         ;; ( -- v )
5         ;; Push stack depth (before push)
6         lea rax,[DS_TOP]
7         sub rax,rsp
8         shr rax,3
9         push rax
10         next
11
12         WORD p_dup, 'DUP',fasm
13         ;; ( v -- v v )
14         ;; Duplicate top ov stack value.
15         ;; push qword [rsp] ??
16         mov rax,qword [rsp]
17         push rax
18         next
19
20         WORD p_2dup, '2DUP',fasm
21         ;; ( x1 x2 -- x1 x2 x1 x2 )
22         ;; Duplicate cell pair x1 x2.
23         push qword [rsp+8]
24         push qword [rsp+8]
25         next
26
27         WORD p_drop, 'DROP',fasm
28         ;; ( x -- )
29         ;; Remove x from the stack.
30         add rsp,8
31         next
32
33         WORD p_2drop, '2DROP',fasm
34         ;; ( x1 x2 -- )
35         ;; Drop cell pair x1 x2 from the stack.
36         add rsp,16
37         next
38
39         WORD p_over, 'OVER',fasm
40         ;; ( x1 x2 -- x1 x2 x1 )
41         ;; Place a copy of x1 on top of the stack. 
42         push qword [rsp+8]
43         next
44
45         WORD p_2over, '2OVER',fasm
46         ;; ( x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2 )
47         ;; Copy cell pair x1 x2 to the top of the stack.
48         push qword [rsp+24]
49         push qword [rsp+24]
50         next
51
52         WORD p_swap, 'SWAP',fasm
53         ;; ( x1 x2 -- x2 x1 )
54         ;; Exchange the top two stack items.
55         mov rax,qword [rsp]
56         mov rbx,qword [rsp+8]
57         mov qword [rsp+8],rax
58         mov qword [rsp],rbx
59         next
60
61         WORD p_2swap, '2SWAP',fasm
62         ;; ( x1 x2 x3 x4 -- x3 x4 x1 x2 )
63         ;; Exchange the top two cell pairs.
64         mov rax,qword [rsp]
65         mov rbx,qword [rsp+16]
66         mov qword [rsp], rbx
67         mov qword [rsp+16],rax
68         mov rax,qword [rsp+8]
69         mov rbx,qword [rsp+24]
70         mov qword [rsp+8], rbx
71         mov qword [rsp+24],rax
72         next
73
74         WORD p_rot, 'ROT',fasm
75         ;; ( x1 x2 x3 -- x2 x3 x1 )
76         ;; Rotate the top three stack entries.
77         mov rax,qword [rsp+16]
78         mov rbx,qword [rsp+8]
79         mov qword [rsp+16],rbx
80         mov rbx,qword [rsp]
81         mov qword [rsp+8],rbx
82         mov qword [rsp],rax
83         next
84
85         ;; ( xu xu-1 ... x0 u -- xu-1 ... x0 xu )
86         ;; Remove u. Rotate u+1 items on the top of the stack. An
87         ;; ambiguous condition exists if there are less than u+2 items
88         ;; on the stack before ROLL is executed.
89         WORD p_roll, 'ROLL',fasm
90         pop rcx
91         shl rcx,3
92         add rcx,rsp
93         mov rax,[rcx+8]
94 p_roll_loop:
95         cmp rcx,rsp
96         je p_roll_eq
97         mov rbx,[rcx]
98         mov [rcx+8],rbx
99         sub rcx,8
100         jmp p_roll_loop
101 p_roll_eq:
102         mov [rsp],rax
103         next
104
105         WORD p_nip, 'NIP',fasm
106         ;; ( x1 x2 -- x2 )
107         ;; Discard the second stack item. 
108         pop rax
109         mov qword [rsp],rax
110         next
111
112         WORD p_pick, 'PICK',fasm
113         ;; ( xu...x1 x0 u -- xu...x1 x0 xu )
114         ;; Remove u. Copy the xu to the top of the stack. An ambiguous
115         ;; condition exists if there are less than u+2 items on the
116         ;; stack before PICK is executed.
117         pop rax
118         shl rax,3               ; 8 bytes per index
119         push qword [rsp+rax]
120         next
121
122         WORD p_tuck, 'TUCK',fasm
123         ;; ( x1 x2 -- x2 x1 x2 )
124         ;; copy the top stack value into below second stack value.
125         pop rax
126         pop rbx
127         push rax
128         push rbx
129         push rax
130         next
131
132 ;;; ========================================
133 ;;; Return stack operations
134
135         WORD p_gtR, '>R',fasm
136         ;; ( x -- ) ( R: -- x )
137         ;; Move x to the return stack.
138         pop rax
139         pushr rax
140         next
141
142         WORD p_Rgt, 'R>',fasm
143         ;; ( -- x ) ( R: x -- )
144         ;; Move x from the return stack to the data stack.
145         popr rax
146         push rax
147         next
148
149         WORD p_Rget, 'R@',fasm
150         ;; ( -- x ) ( R: x -- x )
151         ;; Copy x from the return stack to the data stack.
152         push qword [rbp]
153         next
154
155         WORD p_rsp,'R[n]',fasm
156         ;; ( n -- a )
157         ;; push the address of the n:th cell on the return stack
158         mov rax,qword [rsp]
159         shl rax,3
160         add rax,rbp
161         mov qword [rsp],rax
162         next