Debian Codegraph @

src/ikarus-enter.S

raw
1 # Ikarus Scheme -- A compiler for R6RS Scheme.
2 # Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17
18 .text
19 .globl ik_asm_enter
20 .globl _ik_asm_enter
21 .globl ik_foreign_call
22 .globl _ik_foreign_call
23 .globl ik_asm_reenter
24 .globl _ik_asm_reenter
25
26
27 #if __x86_64__
28
29 ####################################################################
30 # 64-bit
31
32 .align 8
33 ik_asm_enter:
34 _ik_asm_enter:
35 # c parameters come in registers:
36 # %rdi, %rsi, %rdx, %rcx, %r8 and %r9
37 # return value registers are %rax and %rdi
38 # callee-save registers:
39 # %rbp, %rbx, %r12, r13, r14, %r15 are callee-save
40
41
42 # First, save all callee-save registers
43 mov %rbp, -8(%rsp) # preserve
44 mov %rbx, -16(%rsp) # preserve
45 mov %r12, -24(%rsp) # preserve
46 mov %r13, -32(%rsp) # preserve
47 mov %r14, -40(%rsp) # preserve
48 mov %r15, -48(%rsp) # preserve
49
50 # closure pointer is the 4th arg, or %rcx
51 # argcount is the third arg, or %rdx
52 # code is the second arg, or %rsi
53 # pcb is the first arg, or %rdi
54 # return point is at 0(%rsp)
55
56 mov %rdx, %rax # set up arg count
57 mov %rsi, %rdx # move code pointer to %rdx
58 mov %rdi, %rsi # move pcb into pcb-register (%rsi)
59 mov %rcx, %rdi # move closure pointer into cpr
60 mov 0(%rsi), %rbp # allocation pointer is at 0(pcb)
61 sub $64, %rsp # 64 for alignment
62 mov %rsp, 48(%rsi) # save esp in pcb->system_stack
63 mov 16(%rsi), %rsp # load scheme stack from pcb->frame_pinter
64 jmp L_call
65 .quad 8
66 .quad 0
67 L_multivalue_label: # FIXME
68 .quad L_multivalue_underflow
69 .quad 0
70 L_call:
71 call *%rdx # goooooooo
72 # now we're back
73 ik_underflow_handler:
74 _ik_underflow_handler:
75 mov %rax, -16(%rsp) # store the return value
76 mov $-8, %rax # set rvcount = 1
77 L_do_underflow:
78 mov %rsp, 16(%rsi) # store scheme stack in pcb->frame_pointer
79 mov %rbp, 0(%rsi) # store allocation pointer
80 mov 48(%rsi), %rsp # restore system stack
81 add $64, %rsp # 64 for alignment
82
83 # restore callee-save registers
84 mov -8(%rsp) , %rbp # restore
85 mov -16(%rsp), %rbx # restore
86 mov -24(%rsp), %r12 # restore
87 mov -32(%rsp), %r13 # restore
88 mov -40(%rsp), %r14 # restore
89 mov -48(%rsp), %r15 # restore
90 ret # back to C, which handled the underflow
91 multivalue_underflow:
92 L_multivalue_underflow:
93 add $8, %rsp
94 jmp L_do_underflow
95
96
97 .align 8
98 ik_asm_reenter:
99 _ik_asm_reenter:
100 # c parameters come in registers:
101 # %rdi, %rsi, %rdx, %rcx, %r8 and %r9
102 # return value registers are %rax and %rdi
103 # callee-save registers:
104 # %rbp, %rbx, %r12, r13, r14, %r15 are callee-save
105
106 # argc is the third arg 12(%esp) %rdx
107 # scheme stack is second arg 8(%esp) %rsi
108 # pcb is the first arg 4(%esp) %rdi
109 # return point is at 0(%esp)
110
111 mov %rbp, -8(%rsp) # preserve
112 mov %rbx, -16(%rsp) # preserve
113 mov %r12, -24(%rsp) # preserve
114 mov %r13, -32(%rsp) # preserve
115 mov %r14, -40(%rsp) # preserve
116 mov %r15, -48(%rsp) # preserve
117
118 movq %rdx, %rax # third arg -> argc
119 movq %rsi, %rbx # second arg -> rbx (scheme stack)
120 movq %rdi, %rsi # first arg -> pcb
121 movq 0(%rsi), %rbp # allocation pointer is at 0(pcb)
122
123
124 subq $64, %rsp # for alignment
125 movq %rsp, 48(%rsi) # save esp in pcb->system_stack
126 movq %rbx, %rsp # load scheme stack from rbx
127 cmpq $-8, %rax
128 jne L_multi_reentry
129 movq -8(%rsp), %rax
130 ret
131 L_multi_reentry:
132 movq 0(%rsp), %rbx
133 jmp *-18(%rbx)
134
135
136 .align 8
137 ik_foreign_call:
138 _ik_foreign_call:
139 movq %rsp, 16(%rsi) # (movl fpr (pcb-ref 'frame-pointer))
140 movq %rbp, 0(%rsi) # (movl apr (pcb-ref 'allocation-pointer))
141 movq %rsp, %rbx # (movl fpr ebx)
142 movq 48(%rsi), %rsp # (movl (pcb-ref 'system-stack) esp)
143 # %esp is the system stack, %eax is the index to the last arg,
144 # %esi is the pcb.
145
146 # align the system stack by subtracting 8 if it's not 16-byte aligned
147 movq %rsp, %r12
148 andq $15, %r12
149 subq %r12, %rsp
150
151 # AMD64 says: %rbp, %rbx, %r12 .. %r15 are preserved by the callee
152 # %rdi, %rsi, %rdx, %rcx, %r8, %r9 are parameter regs
153
154 movq %rdi, %r12 # put target of call in %r12
155 movq %rsi, %r13 # put pcb in %r13
156
157 movq %rsi, %rdi # put pcb in first arg reg
158 cmpq $0, %rax
159 je L_zero_args
160 cmpq $-8, %rax
161 je L_one_arg
162 cmpq $-16, %rax
163 je L_two_args
164 cmpq $-24, %rax
165 je L_three_args
166 cmpq $-32, %rax
167 je L_four_args
168
169 movq $0, %rbx
170 movq %rbx,0(%rbx)
171 L_four_args:
172 movq %rdi, %r8 # pcb
173 movq -8(%rbx), %rdi
174 movq -16(%rbx), %rsi
175 movq -24(%rbx), %rdx
176 movq -32(%rbx), %rcx
177 jmp L_set
178 L_three_args:
179 movq %rdi, %rcx # pcb
180 movq -8(%rbx), %rdi
181 movq -16(%rbx), %rsi
182 movq -24(%rbx), %rdx
183 jmp L_set
184 L_two_args:
185 movq %rdi, %rdx # pcb
186 movq -8(%rbx), %rdi
187 movq -16(%rbx), %rsi
188 jmp L_set
189 L_one_arg:
190 movq %rdi, %rsi
191 movq -8(%rbx), %rdi
192 jmp L_set
193
194 L_zero_args:
195 L_set: # (label Lset)
196 call *%r12 # (call cpr)
197 L_back:
198 movq %r13, %rsi # restore pcb from r13
199 movq 16(%rsi), %rsp # get frame pointer from 16(pcb)
200 movq 0(%rsi), %rbp # get allocation pointer from 0(pcb)
201 ret # return to scheme
202
203 #else
204
205 ####################################################################
206 # 32-bit
207
208 .align 8
209 ik_asm_enter:
210 _ik_asm_enter:
211 # closure pointer is the 4th arg, 16(%esp)
212 # argcount is the third arg, or 12(%esp)
213 # code is the second arg 8(%esp)
214 # pcb is the first arg 4(%esp)
215 # return point is at 0(%esp)
216 movl 12(%esp), %eax # arg count
217 movl 16(%esp), %edi # closure pointer
218 movl %esi, -4(%esp) # preserve
219 movl %ebp, -8(%esp) # preserve
220 movl %edi, -12(%esp) # preserve
221 movl 4(%esp), %esi
222 movl 0(%esi), %ebp # allocation pointer is at 0(pcb)
223 movl %esp, %ecx
224 subl $16, %esp # 16 for alignment
225 movl %esp, 24(%esi) # save esp in pcb->system_stack
226 movl 8(%esi), %esp # load scheme stack from pcb->frame_pinter
227 jmp L_call
228 .long 4
229 .long 0
230 .long L_multivalue_underflow
231 .byte 0
232 .byte 0
233 L_call:
234 call *8(%ecx) # goooooooo
235 # now we're back
236 ik_underflow_handler:
237 movl %eax, -8(%esp) # store the return value
238 movl $-4, %eax # set rvcount = 1
239 L_do_underflow:
240 movl %esp, 8(%esi) # store scheme stack in pcb->frame_pointer
241 movl %ebp, 0(%esi) # store allocation pointer
242 movl 24(%esi), %esp # restore system stack
243 addl $16, %esp # 24 for alignment (>= 16)
244 movl -4(%esp), %esi # restore callee-save registers
245 movl -8(%esp), %ebp #
246 movl -12(%esp), %edi #
247 ret # back to C, which handled the underflow
248 L_multivalue_underflow:
249 addl $4, %esp
250 jmp L_do_underflow
251
252
253 .align 8
254 ik_asm_reenter:
255 _ik_asm_reenter:
256 # argc is at 12(%esp)
257 # scheme stack is third arg 8(%esp)
258 # pcb is the first arg 4(%esp)
259 # return point is at 0(%esp)
260 movl 12(%esp), %eax
261 movl 8(%esp), %ebx
262 movl %esi, -4(%esp) # preserve
263 movl %ebp, -8(%esp) # preserve
264 movl %edi, -12(%esp) # preserve
265 movl 4(%esp), %esi
266 movl 0(%esi), %ebp # allocation pointer is at 0(pcb)
267 subl $16, %esp # 24 for alignment
268 movl %esp, 24(%esi) # save esp in pcb->system_stack
269 movl %ebx, %esp # load scheme stack from second arg
270 cmpl $-4, %eax
271 jne L_multi_reentry
272 movl -4(%esp), %eax
273 ret
274 L_multi_reentry:
275 movl 0(%esp), %ebx
276 jmp *-9(%ebx)
277
278
279 .align 8
280 ik_foreign_call:
281 _ik_foreign_call:
282 movl %esp, 8(%esi) # (movl fpr (pcb-ref 'frame-pointer))
283 movl %ebp, 0(%esi) # (movl apr (pcb-ref 'allocation-pointer))
284 movl %esp, %ebx # (movl fpr ebx)
285 movl 24(%esi), %esp # (movl (pcb-ref 'system-stack) esp)
286 # %esp is the system stack, %eax is the index to the last arg,
287 # %esi is the pcb.
288 # 0 args require 6 (2) pushes => argc= 0 (0000): %esp += -8
289 # 1 args require 5 (1) pushes => argc= -4 (1100): %esp += -4
290 # 2 args require 4 (0) pushes => argc= -8 (1000): %esp += 0
291 # 3 args require 3 (3) pushes => argc= -12 (0100): %esp += -12
292 movl %eax, %ecx
293 andl $15, %ecx
294 check_ecx:
295 cmpl $8, %ecx
296 je L_zero
297 cmpl $12, %ecx
298 je L_one
299 cmpl $0, %ecx
300 je L_two
301 push $0
302 L_two:
303 push $0
304 L_one:
305 push $0
306 L_zero:
307
308 # Now, the value of %esp is 16-byte aligned
309 # we always push %esi (4 bytes) and do a call (4 bytes),
310 push %esi # (pushl pcr)
311
312 cmpl $0, %eax # (cmpl (int 0) eax)
313 je L_set # (je (label Lset))
314 L_loop: # (label Lloop)
315 movl (%ebx,%eax), %ecx # (movl (mem ebx eax) ecx)
316 push %ecx # (pushl ecx)
317 addl $4, %eax # (addl (int 4) eax)
318 cmpl $0, %eax # (cmpl (int 0) eax)
319 jne L_loop # (jne (label Lloop))
320 L_set: # (label Lset)
321 call *%edi # (call cpr)
322 movl 8(%esi), %esp # (movl (pcb-ref 'frame-pointer) fpr)
323 movl 0(%esi), %ebp # (movl (pcb-ref 'allocation-pointer) apr)
324 ret # (ret)))
325
326 #endif
327