1.0.4.25: 16 byte align asm functions
[sbcl.git] / src / runtime / x86-assem.S
1 /*
2  * very-low-level utilities for runtime support
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15 \f
16 #define LANGUAGE_ASSEMBLY
17 #include "sbcl.h"
18 #include "validate.h"
19 #include "genesis/closure.h"
20 #include "genesis/funcallable-instance.h"
21 #include "genesis/fdefn.h"
22 #include "genesis/static-symbols.h"
23 #include "genesis/symbol.h"
24 #include "genesis/thread.h"
25         
26 /* Minimize conditionalization for different OS naming schemes. 
27  *
28  * (As of sbcl-0.8.10, this seems no longer to be much of an issue, 
29  * since everyone has converged on ELF. If this generality really 
30  * turns out not to matter, perhaps it's just clutter we could get
31  * rid of? -- WHN 2004-04-18)
32  *
33  * (Except Win32, which is unlikely ever to be ELF, sorry. -- AB 2005-12-08)
34  */
35 #if defined __linux__  || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __sun
36 #define GNAME(var) var
37 #else
38 #define GNAME(var) _##var
39 #endif
40
41 /* Get the right type of alignment. Linux, FreeBSD and NetBSD (but not OpenBSD)
42  * want alignment in bytes. 
43  *
44  * (As in the GNAME() definitions above, as of sbcl-0.8.10, this seems 
45  * no longer to be much of an issue, since everyone has converged on
46  * the same value. If this generality really turns out not to 
47  * matter any more, perhaps it's just clutter we could get
48  * rid of? -- WHN 2004-04-18)
49  */
50 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__sun) || defined(LISP_FEATURE_WIN32)
51 #define align_4byte     4
52 #define align_8byte     8
53 #define align_16byte    16
54 #else
55 #define align_4byte     2
56 #define align_8byte     3
57 #define align_16byte    4       
58 #endif                  
59
60 /*
61  * The assembler used for win32 doesn't like .type or .size directives,
62  * so we want to conditionally kill them out. So let's wrap them in macros
63  * that are defined to be no-ops on win32. Hopefully this still works on
64  * other platforms.
65  */
66 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_DARWIN)
67 #define TYPE(name) .type name,@function
68 #define SIZE(name) .size name,.-name
69 #else
70 #define TYPE(name)
71 #define SIZE(name)
72 #endif
73
74 /*
75  * x86/darwin (as of MacOS X 10.4.5) doesn't reliably file signal
76  * handlers (SIGTRAP or Mach exception handlers) for 0xCC, wo we have
77  * to use ud2 instead. ud2 is an undefined opcode, #x0b0f, or
78  * 0F 0B in low-endian notation, that causes SIGILL to fire. We check
79  * for this instruction in the SIGILL handler and if we see it, we
80  * advance the EIP by two bytes to skip over ud2 instruction and
81  * call sigtrap_handler. */
82 #if defined(LISP_FEATURE_DARWIN)
83 #define END()
84 #define TRAP ud2
85 #else
86 #define END() .end
87 #define TRAP int3
88 #endif
89
90         .text
91         .globl  GNAME(all_threads)
92 \f
93 /*
94  * A call to call_into_c preserves esi, edi, and ebp.   
95  * (The C function will preserve ebx, esi, edi, and ebp across its
96  * function call, but we trash ebx ourselves by using it to save the
97  * return Lisp address.)
98  *
99  * Return values are in eax and maybe edx for quads, or st(0) for
100  * floats.
101  *
102  * This should work for Lisp calls C calls Lisp calls C..
103  */
104         .text
105         .align  align_16byte,0x90
106         .globl GNAME(call_into_c)
107         TYPE(GNAME(call_into_c))
108 GNAME(call_into_c):
109 /* Save the return Lisp address in ebx. */
110         popl    %ebx
111
112 /* Setup the NPX for C */
113         fstp    %st(0)
114         fstp    %st(0)
115         fstp    %st(0)
116         fstp    %st(0)
117         fstp    %st(0)
118         fstp    %st(0)
119         fstp    %st(0)
120         fstp    %st(0)
121
122 #ifdef LISP_FEATURE_WIN32
123         cld
124 #endif
125
126 #ifdef LISP_FEATURE_DARWIN
127         andl    $0xfffffff0,%esp        # align stack to 16-byte boundary before calling C
128 #endif
129         call    *%eax           # normal callout using Lisp stack
130
131         movl    %eax,%ecx       # remember integer return value
132
133 /* Check for a return FP value. */
134         fxam
135         fnstsw  %eax
136         andl    $0x4500,%eax
137         cmpl    $0x4100,%eax
138         jne     Lfp_rtn_value
139
140 /* The return value is in eax, or eax,edx? */
141 /* Set up the NPX stack for Lisp. */
142         fldz                    # Ensure no regs are empty.
143         fldz
144         fldz
145         fldz
146         fldz
147         fldz
148         fldz
149         fldz
150
151 /* Restore the return value. */
152         movl    %ecx,%eax       # maybe return value
153
154 /* Return. */
155         jmp     *%ebx
156
157 Lfp_rtn_value:
158 /* The return result is in st(0). */
159 /* Set up the NPX stack for Lisp, placing the result in st(0). */
160         fldz                    # Ensure no regs are empty.
161         fldz
162         fldz
163         fldz
164         fldz
165         fldz
166         fldz
167         fxch    %st(7)          # Move the result back to st(0).
168
169 /* We don't need to restore eax, because the result is in st(0). */
170
171 /* Return. */   
172         jmp     *%ebx
173
174         SIZE(GNAME(call_into_c))
175
176 \f
177         .text   
178         .globl GNAME(call_into_lisp_first_time)
179         TYPE(GNAME(call_into_lisp_first_time))
180                 
181 /* The *ALIEN-STACK* pointer is set up on the first call_into_lisp when
182  * the stack changes.  We don't worry too much about saving registers 
183  * here, because we never expect to return from the initial call to lisp 
184  * anyway */
185         
186         .align  align_16byte,0x90
187 GNAME(call_into_lisp_first_time):
188         pushl   %ebp            # Save old frame pointer.
189         movl    %esp,%ebp       # Establish new frame.
190 #ifndef LISP_FEATURE_WIN32
191         movl    %esp,ALIEN_STACK + SYMBOL_VALUE_OFFSET
192         movl    GNAME(all_threads),%eax
193         movl    THREAD_CONTROL_STACK_START_OFFSET(%eax) ,%esp
194         /* don't think too hard about what happens if we get interrupted
195         * here */
196         addl    $(THREAD_CONTROL_STACK_SIZE),%esp
197 #else
198 /* Win32 -really- doesn't like you switching stacks out from under it. */
199         movl    GNAME(all_threads),%eax
200 #endif
201         jmp     Lstack
202 \f
203         .text   
204         .globl GNAME(call_into_lisp)
205         TYPE(GNAME(call_into_lisp))
206                 
207 /* The C conventions require that ebx, esi, edi, and ebp be preserved
208  * across function calls. */
209         
210         .align  align_16byte,0x90
211 GNAME(call_into_lisp):
212         pushl   %ebp            # Save old frame pointer.
213         movl    %esp,%ebp       # Establish new frame.
214 Lstack:
215 /* Save the NPX state */
216         fwait                   # Catch any pending NPX exceptions.
217         subl    $108,%esp       # Make room for the NPX state.
218         fnsave  (%esp)          # save and reset NPX
219
220         movl    (%esp),%eax     # Load NPX control word.
221         andl    $0xfffff2ff,%eax        # Set rounding mode to nearest.
222         orl     $0x00000200,%eax        # Set precision to 64 bits.  (53-bit mantissa)
223         pushl   %eax
224         fldcw   (%esp)          # Recover modes.
225         popl    %eax
226
227         fldz                    # Ensure no FP regs are empty.
228         fldz
229         fldz
230         fldz
231         fldz
232         fldz
233         fldz
234         fldz
235         
236 /* Save C regs: ebx esi edi. */
237         pushl   %ebx
238         pushl   %esi
239         pushl   %edi
240         
241 /* Clear descriptor regs. */
242         xorl    %eax,%eax       # lexenv
243         xorl    %ebx,%ebx       # available
244         xorl    %ecx,%ecx       # arg count
245         xorl    %edx,%edx       # first arg
246         xorl    %edi,%edi       # second arg
247         xorl    %esi,%esi       # third arg
248
249 /* no longer in function call */
250         movl    %esp,%ebx       # remember current stack
251         pushl   %ebx            # Save entry stack on (maybe) new stack.
252
253         /* Establish Lisp args. */
254         movl     8(%ebp),%eax   # lexenv?
255         movl    12(%ebp),%ebx   # address of arg vec
256         movl    16(%ebp),%ecx   # num args
257         shll    $2,%ecx         # Make num args into fixnum.
258         cmpl    $0,%ecx
259         je      Ldone
260         movl    (%ebx),%edx     # arg0
261         cmpl    $4,%ecx
262         je      Ldone
263         movl    4(%ebx),%edi    # arg1
264         cmpl    $8,%ecx
265         je      Ldone
266         movl    8(%ebx),%esi    # arg2
267 Ldone:  
268         /* Registers eax, ecx, edx, edi, and esi are now live. */
269
270 #ifdef LISP_FEATURE_WIN32
271         /* Establish an SEH frame. */
272 #ifdef LISP_FEATURE_SB_THREAD
273         /* FIXME: need to save BSP here. */
274 #error "need to save BSP here, but don't know how yet."
275 #else
276         pushl   BINDING_STACK_POINTER + SYMBOL_VALUE_OFFSET
277 #endif
278         pushl   $GNAME(exception_handler_wrapper)
279         pushl   %fs:0
280         movl    %esp, %fs:0
281 #endif
282
283         /* Alloc new frame. */
284         mov     %esp,%ebx       # The current sp marks start of new frame.
285         push    %ebp            # fp in save location S0
286         sub     $8,%esp         # Ensure 3 slots are allocated, one above.
287         mov     %ebx,%ebp       # Switch to new frame.
288
289         call    *CLOSURE_FUN_OFFSET(%eax)
290         
291         /* If the function returned multiple values, it will return to
292            this point.  Lose them */
293         jnc     LsingleValue
294         mov     %ebx, %esp
295 LsingleValue:
296         /* A singled value function returns here */
297
298 #ifdef LISP_FEATURE_WIN32
299         /* Remove our SEH frame. */
300         popl    %fs:0
301         add     $8, %esp
302 #endif
303
304 /* Restore the stack, in case there was a stack change. */
305         popl    %esp            # c-sp
306
307 /* Restore C regs: ebx esi edi. */
308         popl    %edi
309         popl    %esi
310         popl    %ebx
311
312 /* Restore the NPX state. */
313         frstor  (%esp)
314         addl    $108, %esp
315         
316         popl    %ebp            # c-sp
317         movl    %edx,%eax       # c-val
318         ret
319         SIZE(GNAME(call_into_lisp))
320 \f
321 /* support for saving and restoring the NPX state from C */
322         .text
323         .globl  GNAME(fpu_save)
324         TYPE(GNAME(fpu_save))
325         .align  2,0x90
326 GNAME(fpu_save):
327         movl    4(%esp),%eax
328         fnsave  (%eax)          # Save the NPX state. (resets NPX)
329         ret
330         SIZE(GNAME(fpu_save))
331
332         .globl  GNAME(fpu_restore)
333         TYPE(GNAME(fpu_restore))
334         .align  2,0x90
335 GNAME(fpu_restore):
336         movl    4(%esp),%eax
337         frstor  (%eax)          # Restore the NPX state.
338         ret
339         SIZE(GNAME(fpu_restore))
340 \f
341 /*
342  * the undefined-function trampoline
343  */
344         .text
345         .align  align_4byte,0x90
346         .globl GNAME(undefined_tramp)
347         TYPE(GNAME(undefined_tramp))
348         .byte   0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
349 GNAME(undefined_tramp):
350         TRAP
351         .byte   trap_Error
352         .byte   2
353         .byte   UNDEFINED_FUN_ERROR
354         .byte   sc_DescriptorReg # eax in the Descriptor-reg SC
355         ret
356         SIZE(GNAME(undefined_tramp))
357
358 /*
359  * the closure trampoline
360  */
361         .text
362         .align  align_4byte,0x90
363         .globl GNAME(closure_tramp)
364         TYPE(GNAME(closure_tramp))
365         .byte   0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
366 GNAME(closure_tramp):
367         movl    FDEFN_FUN_OFFSET(%eax),%eax
368         /* FIXME: The '*' after "jmp" in the next line is from PVE's
369          * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
370          * reasonable, and it certainly seems as though if CMU CL needs it,
371          * SBCL needs it too, but I haven't actually verified that it's
372          * right. It would be good to find a way to force the flow of
373          * control through here to test it. */
374         jmp     *CLOSURE_FUN_OFFSET(%eax)
375         SIZE(GNAME(closure_tramp))
376
377         .text
378         .align  align_4byte,0x90
379         .globl GNAME(funcallable_instance_tramp)
380         TYPE(GNAME(funcallable_instance_tramp))
381 GNAME(funcallable_instance_tramp):
382         movl    FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%eax),%eax 
383         /* KLUDGE: on this platform, whatever kind of function is in %rax
384          * now, the first word of it contains the address to jump to. */
385         jmp     *CLOSURE_FUN_OFFSET(%eax)
386         SIZE(GNAME(funcallable_instance_tramp))
387         
388 /*
389  * fun-end breakpoint magic
390  */
391         .text
392         .globl  GNAME(fun_end_breakpoint_guts)
393         .align  align_4byte
394 GNAME(fun_end_breakpoint_guts):
395         /* Multiple Value return */
396         jc      multiple_value_return
397         /* Single value return: The eventual return will now use the
398            multiple values return convention but with a return values
399            count of one. */
400         movl    %esp,%ebx       # Setup ebx - the ofp.
401         subl    $4,%esp         # Allocate one stack slot for the return value
402         movl    $4,%ecx         # Setup ecx for one return value.
403         movl    $(NIL),%edi     # default second value
404         movl    $(NIL),%esi     # default third value
405                 
406 multiple_value_return:
407         
408         .globl GNAME(fun_end_breakpoint_trap)
409 GNAME(fun_end_breakpoint_trap):
410         TRAP
411         .byte   trap_FunEndBreakpoint
412         hlt                     # We should never return here.
413
414         .globl GNAME(fun_end_breakpoint_end)
415 GNAME(fun_end_breakpoint_end):
416
417 \f
418         .globl  GNAME(do_pending_interrupt)
419         TYPE(GNAME(do_pending_interrupt))
420         .align  align_4byte,0x90
421 GNAME(do_pending_interrupt):
422         TRAP
423         .byte   trap_PendingInterrupt
424         ret
425         SIZE(GNAME(do_pending_interrupt))
426 \f
427
428 /*
429  * Allocate bytes and return the start of the allocated space
430  * in the specified destination register.
431  *
432  * In the general case the size will be in the destination register.
433  *
434  * All registers must be preserved except the destination.
435  * The C conventions will preserve ebx, esi, edi, and ebp.
436  * So only eax, ecx, and edx need special care here.
437  */
438         
439         .globl  GNAME(alloc_to_eax)
440         TYPE(GNAME(alloc_to_eax))
441         .align  align_4byte,0x90
442 GNAME(alloc_to_eax):
443         pushl   %ecx    # Save ecx and edx as C could destroy them.
444         pushl   %edx
445         pushl   %eax    # Push the size.
446         call    GNAME(alloc)
447         addl    $4,%esp # Pop the size arg.
448         popl    %edx    # Restore ecx and edx.
449         popl    %ecx
450         ret
451         SIZE(GNAME(alloc_to_eax))
452
453         .globl  GNAME(alloc_8_to_eax)
454         TYPE(GNAME(alloc_8_to_eax))
455         .align  align_4byte,0x90
456 GNAME(alloc_8_to_eax):
457         pushl   %ecx    # Save ecx and edx as C could destroy them.
458         pushl   %edx
459         pushl   $8      # Push the size.
460         call    GNAME(alloc)
461         addl    $4,%esp # Pop the size arg.
462         popl    %edx    # Restore ecx and edx.
463         popl    %ecx
464         ret
465         SIZE(GNAME(alloc_8_to_eax))
466
467         .globl  GNAME(alloc_8_to_eax)
468         TYPE(GNAME(alloc_8_to_eax))
469         .align  align_4byte,0x90
470
471         .globl  GNAME(alloc_16_to_eax)
472         TYPE(GNAME(alloc_16_to_eax))
473         .align  align_4byte,0x90
474 GNAME(alloc_16_to_eax):
475         pushl   %ecx    # Save ecx and edx as C could destroy them.
476         pushl   %edx
477         pushl   $16     # Push the size.
478         call    GNAME(alloc)
479         addl    $4,%esp # Pop the size arg.
480         popl    %edx    # Restore ecx and edx.
481         popl    %ecx
482         ret
483         SIZE(GNAME(alloc_16_to_eax))
484
485         .globl  GNAME(alloc_to_ecx)
486         TYPE(GNAME(alloc_to_ecx))
487         .align  align_4byte,0x90
488 GNAME(alloc_to_ecx):
489         pushl   %eax    # Save eax and edx as C could destroy them.
490         pushl   %edx
491         pushl   %ecx    # Push the size.
492         call    GNAME(alloc)
493         addl    $4,%esp # Pop the size arg.
494         movl    %eax,%ecx       # Set up the destination.
495         popl    %edx    # Restore eax and edx.
496         popl    %eax
497         ret
498         SIZE(GNAME(alloc_to_ecx))
499
500         .globl  GNAME(alloc_8_to_ecx)
501         TYPE(GNAME(alloc_8_to_ecx))
502         .align  align_4byte,0x90
503 GNAME(alloc_8_to_ecx):
504         pushl   %eax    # Save eax and edx as C could destroy them.
505         pushl   %edx
506         pushl   $8      # Push the size.
507         call    GNAME(alloc)
508         addl    $4,%esp # Pop the size arg.
509         movl    %eax,%ecx       # Set up the destination.
510         popl    %edx    # Restore eax and edx.
511         popl    %eax
512         ret
513         SIZE(GNAME(alloc_8_to_ecx))
514
515         .globl  GNAME(alloc_16_to_ecx)
516         TYPE(GNAME(alloc_16_to_ecx))
517         .align  align_4byte,0x90
518 GNAME(alloc_16_to_ecx):
519         pushl   %eax    # Save eax and edx as C could destroy them.
520         pushl   %edx
521         pushl   $16     # Push the size.
522         call    GNAME(alloc)
523         addl    $4,%esp # Pop the size arg.
524         movl    %eax,%ecx       # Set up the destination.
525         popl    %edx    # Restore eax and edx.
526         popl    %eax
527         ret
528         SIZE(GNAME(alloc_16_to_ecx))
529
530
531         .globl  GNAME(alloc_to_edx)
532         TYPE(GNAME(alloc_to_edx))
533         .align  align_4byte,0x90
534 GNAME(alloc_to_edx):
535         pushl   %eax    # Save eax and ecx as C could destroy them.
536         pushl   %ecx
537         pushl   %edx    # Push the size.
538         call    GNAME(alloc)
539         addl    $4,%esp # Pop the size arg.
540         movl    %eax,%edx       # Set up the destination.
541         popl    %ecx    # Restore eax and ecx.
542         popl    %eax
543         ret
544         SIZE(GNAME(alloc_to_edx))
545
546         .globl  GNAME(alloc_8_to_edx)
547         TYPE(GNAME(alloc_8_to_edx))
548         .align  align_4byte,0x90
549 GNAME(alloc_8_to_edx):
550         pushl   %eax    # Save eax and ecx as C could destroy them.
551         pushl   %ecx
552         pushl   $8      # Push the size.
553         call    GNAME(alloc)
554         addl    $4,%esp # Pop the size arg.
555         movl    %eax,%edx       # Set up the destination.
556         popl    %ecx    # Restore eax and ecx.
557         popl    %eax
558         ret
559         SIZE(GNAME(alloc_8_to_edx))
560
561         .globl  GNAME(alloc_16_to_edx)
562         TYPE(GNAME(alloc_16_to_edx))
563         .align  align_4byte,0x90
564 GNAME(alloc_16_to_edx):
565         pushl   %eax    # Save eax and ecx as C could destroy them.
566         pushl   %ecx
567         pushl   $16     # Push the size.
568         call    GNAME(alloc)
569         addl    $4,%esp # Pop the size arg.
570         movl    %eax,%edx       # Set up the destination.
571         popl    %ecx    # Restore eax and ecx.
572         popl    %eax
573         ret
574         SIZE(GNAME(alloc_16_to_edx))
575
576
577
578         .globl  GNAME(alloc_to_ebx)
579         TYPE(GNAME(alloc_to_ebx))
580         .align  align_4byte,0x90
581 GNAME(alloc_to_ebx):
582         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
583         pushl   %ecx
584         pushl   %edx
585         pushl   %ebx    # Push the size.
586         call    GNAME(alloc)
587         addl    $4,%esp # Pop the size arg.
588         movl    %eax,%ebx       # Set up the destination.
589         popl    %edx    # Restore eax, ecx and edx.
590         popl    %ecx
591         popl    %eax
592         ret
593         SIZE(GNAME(alloc_to_ebx))
594
595         .globl  GNAME(alloc_8_to_ebx)
596         TYPE(GNAME(alloc_8_to_ebx))
597         .align  align_4byte,0x90
598 GNAME(alloc_8_to_ebx):
599         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
600         pushl   %ecx
601         pushl   %edx
602         pushl   $8      # Push the size.
603         call    GNAME(alloc)
604         addl    $4,%esp # Pop the size arg.
605         movl    %eax,%ebx       # Set up the destination.
606         popl    %edx    # Restore eax, ecx and edx.
607         popl    %ecx
608         popl    %eax
609         ret
610         SIZE(GNAME(alloc_8_to_ebx))
611
612         .globl  GNAME(alloc_16_to_ebx)
613         TYPE(GNAME(alloc_16_to_ebx))
614         .align  align_4byte,0x90
615 GNAME(alloc_16_to_ebx):
616         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
617         pushl   %ecx
618         pushl   %edx
619         pushl   $16     # Push the size
620         call    GNAME(alloc)
621         addl    $4,%esp # pop the size arg.
622         movl    %eax,%ebx       # setup the destination.
623         popl    %edx    # Restore eax, ecx and edx.
624         popl    %ecx
625         popl    %eax
626         ret
627         SIZE(GNAME(alloc_16_to_ebx))
628
629
630
631         .globl  GNAME(alloc_to_esi)
632         TYPE(GNAME(alloc_to_esi))
633         .align  align_4byte,0x90
634 GNAME(alloc_to_esi):
635         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
636         pushl   %ecx
637         pushl   %edx
638         pushl   %esi    # Push the size
639         call    GNAME(alloc)
640         addl    $4,%esp # pop the size arg.
641         movl    %eax,%esi       # setup the destination.
642         popl    %edx    # Restore eax, ecx and edx.
643         popl    %ecx
644         popl    %eax
645         ret
646         SIZE(GNAME(alloc_to_esi))
647
648         .globl  GNAME(alloc_8_to_esi)
649         TYPE(GNAME(alloc_8_to_esi))
650         .align  align_4byte,0x90
651 GNAME(alloc_8_to_esi):
652         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
653         pushl   %ecx
654         pushl   %edx
655         pushl   $8      # Push the size
656         call    GNAME(alloc)
657         addl    $4,%esp # pop the size arg.
658         movl    %eax,%esi       # setup the destination.
659         popl    %edx    # Restore eax, ecx and edx.
660         popl    %ecx
661         popl    %eax
662         ret
663         SIZE(GNAME(alloc_8_to_esi))
664
665         .globl  GNAME(alloc_16_to_esi)
666         TYPE(GNAME(alloc_16_to_esi))
667         .align  align_4byte,0x90
668 GNAME(alloc_16_to_esi):
669         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
670         pushl   %ecx
671         pushl   %edx
672         pushl   $16     # Push the size
673         call    GNAME(alloc)
674         addl    $4,%esp # pop the size arg.
675         movl    %eax,%esi       # setup the destination.
676         popl    %edx    # Restore eax, ecx and edx.
677         popl    %ecx
678         popl    %eax
679         ret
680         SIZE(GNAME(alloc_16_to_esi))
681
682
683         .globl  GNAME(alloc_to_edi)
684         TYPE(GNAME(alloc_to_edi))
685         .align  align_4byte,0x90
686 GNAME(alloc_to_edi):
687         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
688         pushl   %ecx
689         pushl   %edx
690         pushl   %edi    # Push the size
691         call    GNAME(alloc)
692         addl    $4,%esp # pop the size arg.
693         movl    %eax,%edi       # setup the destination.
694         popl    %edx    # Restore eax, ecx and edx.
695         popl    %ecx
696         popl    %eax
697         ret
698         SIZE(GNAME(alloc_to_edi))
699
700         .globl  GNAME(alloc_8_to_edi)
701         TYPE(GNAME(alloc_8_to_edi))
702         .align  align_4byte,0x90
703 GNAME(alloc_8_to_edi):
704         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
705         pushl   %ecx
706         pushl   %edx
707         pushl   $8      # Push the size
708         call    GNAME(alloc)
709         addl    $4,%esp # pop the size arg.
710         movl    %eax,%edi       # setup the destination.
711         popl    %edx    # Restore eax, ecx and edx.
712         popl    %ecx
713         popl    %eax
714         ret
715         SIZE(GNAME(alloc_8_to_edi))
716
717         .globl  GNAME(alloc_16_to_edi)
718         TYPE(GNAME(alloc_16_to_edi))
719         .align  align_4byte,0x90
720 GNAME(alloc_16_to_edi):
721         pushl   %eax    # Save eax, ecx, and edx as C could destroy them.
722         pushl   %ecx
723         pushl   %edx
724         pushl   $16     # Push the size
725         call    GNAME(alloc)
726         addl    $4,%esp # pop the size arg.
727         movl    %eax,%edi       # setup the destination.
728         popl    %edx    # Restore eax, ecx and edx.
729         popl    %ecx
730         popl    %eax
731         ret
732         SIZE(GNAME(alloc_16_to_edi))
733
734         
735 /* Called from lisp when an inline allocation overflows.
736    Every register except the result needs to be preserved.
737    We depend on C to preserve ebx, esi, edi, and ebp.
738    But where necessary must save eax, ecx, edx. */
739
740 #ifdef LISP_FEATURE_SB_THREAD
741 #define START_REGION %fs:THREAD_ALLOC_REGION_OFFSET
742 #else
743 #define START_REGION GNAME(boxed_region)
744 #endif
745                 
746 /* This routine handles an overflow with eax=crfp+size. So the
747    size=eax-crfp. */
748         .align  align_4byte
749         .globl  GNAME(alloc_overflow_eax)
750         TYPE(GNAME(alloc_overflow_eax))
751 GNAME(alloc_overflow_eax):
752         pushl   %ecx            # Save ecx
753         pushl   %edx            # Save edx
754         /* Calculate the size for the allocation. */
755         subl    START_REGION,%eax
756         pushl   %eax            # Push the size
757         call    GNAME(alloc)
758         addl    $4,%esp # pop the size arg.
759         popl    %edx    # Restore edx.
760         popl    %ecx    # Restore ecx.
761         ret
762         SIZE(GNAME(alloc_overflow_eax))
763
764         .align  align_4byte
765         .globl  GNAME(alloc_overflow_ecx)
766         TYPE(GNAME(alloc_overflow_ecx))
767 GNAME(alloc_overflow_ecx):
768         pushl   %eax            # Save eax
769         pushl   %edx            # Save edx
770         /* Calculate the size for the allocation. */
771         subl    START_REGION,%ecx
772         pushl   %ecx            # Push the size
773         call    GNAME(alloc)
774         addl    $4,%esp # pop the size arg.
775         movl    %eax,%ecx       # setup the destination.
776         popl    %edx    # Restore edx.
777         popl    %eax    # Restore eax.
778         ret
779         SIZE(GNAME(alloc_overflow_ecx))
780
781         .align  align_4byte
782         .globl  GNAME(alloc_overflow_edx)
783         TYPE(GNAME(alloc_overflow_edx))
784 GNAME(alloc_overflow_edx):
785         pushl   %eax            # Save eax
786         pushl   %ecx            # Save ecx
787         /* Calculate the size for the allocation. */
788         subl    START_REGION,%edx
789         pushl   %edx            # Push the size
790         call    GNAME(alloc)
791         addl    $4,%esp # pop the size arg.
792         movl    %eax,%edx       # setup the destination.
793         popl    %ecx    # Restore ecx.
794         popl    %eax    # Restore eax.
795         ret
796         SIZE(GNAME(alloc_overflow_edx))
797
798 /* This routine handles an overflow with ebx=crfp+size. So the
799    size=ebx-crfp. */
800         .align  align_4byte
801         .globl  GNAME(alloc_overflow_ebx)
802         TYPE(GNAME(alloc_overflow_ebx))
803 GNAME(alloc_overflow_ebx):
804         pushl   %eax            # Save eax
805         pushl   %ecx            # Save ecx
806         pushl   %edx            # Save edx
807         /* Calculate the size for the allocation. */
808         subl    START_REGION,%ebx
809         pushl   %ebx            # Push the size
810         call    GNAME(alloc)
811         addl    $4,%esp # pop the size arg.
812         movl    %eax,%ebx       # setup the destination.
813         popl    %edx    # Restore edx.
814         popl    %ecx    # Restore ecx.
815         popl    %eax    # Restore eax.
816         ret
817         SIZE(GNAME(alloc_overflow_ebx))
818
819 /* This routine handles an overflow with esi=crfp+size. So the
820    size=esi-crfp. */
821         .align  align_4byte
822         .globl  GNAME(alloc_overflow_esi)
823         TYPE(GNAME(alloc_overflow_esi))
824 GNAME(alloc_overflow_esi):
825         pushl   %eax            # Save eax
826         pushl   %ecx            # Save ecx
827         pushl   %edx            # Save edx
828         /* Calculate the size for the allocation. */
829         subl    START_REGION,%esi
830         pushl   %esi            # Push the size
831         call    GNAME(alloc)
832         addl    $4,%esp # pop the size arg.
833         movl    %eax,%esi       # setup the destination.
834         popl    %edx    # Restore edx.
835         popl    %ecx    # Restore ecx.
836         popl    %eax    # Restore eax.
837         ret
838         SIZE(GNAME(alloc_overflow_esi))
839
840         .align  align_4byte
841         .globl  GNAME(alloc_overflow_edi)
842         TYPE(GNAME(alloc_overflow_edi))
843 GNAME(alloc_overflow_edi):
844         pushl   %eax            # Save eax
845         pushl   %ecx            # Save ecx
846         pushl   %edx            # Save edx
847         /* Calculate the size for the allocation. */
848         subl    START_REGION,%edi
849         pushl   %edi            # Push the size
850         call    GNAME(alloc)
851         addl    $4,%esp # pop the size arg.
852         movl    %eax,%edi       # setup the destination.
853         popl    %edx    # Restore edx.
854         popl    %ecx    # Restore ecx.
855         popl    %eax    # Restore eax.
856         ret
857         SIZE(GNAME(alloc_overflow_edi))
858
859
860 #ifdef LISP_FEATURE_WIN32
861         /* The guts of the exception-handling system doesn't use
862          * frame pointers, which manages to throw off backtraces
863          * rather badly.  So here we grab the (known-good) EBP
864          * and EIP from the exception context and use it to fake
865          * up a stack frame which will skip over the system SEH
866          * code. */
867         .align  align_4byte
868         .globl  GNAME(exception_handler_wrapper)
869         TYPE(GNAME(exception_handler_wrapper))
870 GNAME(exception_handler_wrapper):
871         /* Context layout is: */
872         /* 7 dwords before FSA. (0x1c) */
873         /* 8 dwords and 0x50 bytes in the FSA. (0x70/0x8c) */
874         /* 4 dwords segregs. (0x10/0x9c) */
875         /* 6 dwords non-stack GPRs. (0x18/0xb4) */
876         /* EBP (at 0xb4) */
877         /* EIP (at 0xb8) */
878 #define CONTEXT_EBP_OFFSET 0xb4
879 #define CONTEXT_EIP_OFFSET 0xb8
880         /* some other stuff we don't care about. */
881         pushl   %ebp
882         movl    0x10(%esp), %ebp        /* context */
883         pushl   CONTEXT_EIP_OFFSET(%ebp)
884         pushl   CONTEXT_EBP_OFFSET(%ebp)
885         movl    %esp, %ebp
886         pushl   0x1c(%esp)
887         pushl   0x1c(%esp)
888         pushl   0x1c(%esp)
889         pushl   0x1c(%esp)
890         call    GNAME(handle_exception)
891         lea     8(%ebp), %esp
892         popl    %ebp
893         ret
894         SIZE(GNAME(exception_handler_wrapper))
895 #endif
896
897 #ifdef LISP_FEATURE_DARWIN
898         .align align_4byte
899         .globl GNAME(call_into_lisp_tramp)
900         TYPE(GNAME(call_into_lisp_tramp))
901 GNAME(call_into_lisp_tramp):
902         /* 1. build the stack frame from the block that's pointed to by ECX
903            2. free the block
904            3. set ECX to 0
905            4. call the function via call_into_lisp
906         */
907         pushl   0(%ecx)          /* return address */
908
909         pushl   %ebp
910         movl    %esp, %ebp
911
912         pushl   32(%ecx)         /* eflags */
913         pushl   28(%ecx)         /* EAX */
914         pushl   20(%ecx)         /* ECX */
915         pushl   16(%ecx)         /* EDX */
916         pushl   24(%ecx)         /* EBX */
917         pushl   $0                /* popal is going to ignore esp */
918         pushl   %ebp              /* is this right?? */
919         pushl   12(%ecx)         /* ESI */
920         pushl   8(%ecx)          /* EDI */
921         pushl   $0                /* args for call_into_lisp */
922         pushl   $0
923         pushl   4(%ecx)          /* function to call */
924
925         /* free our save block */
926         pushl   %ecx              /* reserve sufficient space on stack for args */
927         pushl   %ecx
928         andl    $0xfffffff0, %esp  /* align stack */
929         movl    $0x40, 4(%esp)
930         movl    %ecx, (%esp)
931         call    GNAME(os_invalidate)
932
933         /* call call_into_lisp */
934         leal    -48(%ebp), %esp
935         call    GNAME(call_into_lisp)
936
937         /* Clean up our mess */
938         leal    -36(%ebp), %esp
939         popal
940         popfl
941         leave
942         ret
943         
944         SIZE(call_into_lisp_tramp)
945 #endif
946         
947         .align  align_4byte,0x90
948         .globl  GNAME(post_signal_tramp)
949         TYPE(GNAME(post_signal_tramp))
950 GNAME(post_signal_tramp):
951         /* this is notionally the second half of a function whose first half
952          * doesn't exist.  This is where call_into_lisp returns when called 
953          * using return_to_lisp_function */
954         addl $12,%esp   /* clear call_into_lisp args from stack */
955         popal           /* restore registers */
956         popfl
957 #ifdef LISP_FEATURE_DARWIN
958         /* skip two padding words */
959         addl $8,%esp
960 #endif
961         leave
962         ret
963         SIZE(GNAME(post_signal_tramp))
964
965
966         /* fast_bzero implementations and code to detect which implementation
967          * to use.
968          */
969 \f
970         .globl GNAME(fast_bzero_pointer)
971         .data
972         .align  align_4byte
973 GNAME(fast_bzero_pointer):
974         /* Variable containing a pointer to the bzero function to use.
975          * Initially points to a basic function.  Change this variable
976          * to fast_bzero_detect if OS supports SSE.  */
977         .long GNAME(fast_bzero_base)
978 \f
979         .text
980         .align  align_8byte,0x90
981         .globl GNAME(fast_bzero)
982         TYPE(GNAME(fast_bzero))
983 GNAME(fast_bzero):        
984         /* Indirect function call */
985         jmp *GNAME(fast_bzero_pointer)
986         SIZE(GNAME(fast_bzero))
987         
988 \f      
989         .text
990         .align  align_8byte,0x90
991         .globl GNAME(fast_bzero_detect)
992         TYPE(GNAME(fast_bzero_detect))
993 GNAME(fast_bzero_detect):
994         /* Decide whether to use SSE, MMX or REP version */
995         push %eax /* CPUID uses EAX-EDX */
996         push %ebx
997         push %ecx
998         push %edx
999         mov $1, %eax
1000         cpuid
1001         test $0x04000000, %edx    /* SSE2 needed for MOVNTDQ */
1002         jnz Lsse2
1003         /* Originally there was another case here for using the
1004          * MOVNTQ instruction for processors that supported MMX but
1005          * not SSE2. This turned out to be a loss especially on
1006          * Athlons (where this instruction is apparently microcoded
1007          * somewhat slowly). So for simplicity revert to REP STOSL
1008          * for all non-SSE2 processors.
1009          */
1010 Lbase:
1011         movl $(GNAME(fast_bzero_base)), GNAME(fast_bzero_pointer)
1012         jmp Lrestore
1013 Lsse2:
1014         movl $(GNAME(fast_bzero_sse)), GNAME(fast_bzero_pointer)
1015         jmp Lrestore
1016         
1017 Lrestore:
1018         pop %edx
1019         pop %ecx
1020         pop %ebx
1021         pop %eax
1022         jmp *GNAME(fast_bzero_pointer)
1023         
1024         SIZE(GNAME(fast_bzero_detect))
1025         
1026 \f
1027         .text
1028         .align  align_8byte,0x90
1029         .globl GNAME(fast_bzero_sse)
1030         TYPE(GNAME(fast_bzero_sse))
1031         
1032 GNAME(fast_bzero_sse):
1033         /* A fast routine for zero-filling blocks of memory that are
1034          * guaranteed to start and end at a 4096-byte aligned address.
1035          */        
1036         push %esi                 /* Save temporary registers */
1037         push %edi
1038         mov 16(%esp), %esi        /* Parameter: amount of bytes to fill */
1039         mov 12(%esp), %edi        /* Parameter: start address */
1040         shr $6, %esi              /* Amount of 64-byte blocks to copy */
1041         jz Lend_sse               /* If none, stop */
1042         movups %xmm7, -16(%esp)   /* Save XMM register */
1043         xorps  %xmm7, %xmm7       /* Zero the XMM register */
1044         jmp Lloop_sse
1045         .align align_16byte
1046 Lloop_sse:
1047
1048         /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
1049          * non-caching double-quadword moving variant, i.e. the memory areas
1050          * we're touching are not fetched into the L1 cache, since we're just
1051          * going to overwrite the memory soon anyway.
1052          */
1053         movntdq %xmm7, 0(%edi)
1054         movntdq %xmm7, 16(%edi)
1055         movntdq %xmm7, 32(%edi)
1056         movntdq %xmm7, 48(%edi)
1057  
1058         add $64, %edi /* Advance pointer */
1059         dec %esi      /* Decrement 64-byte block count */
1060         jnz Lloop_sse
1061         movups -16(%esp), %xmm7 /* Restore the XMM register */
1062         sfence        /* Ensure that weakly ordered writes are flushed. */
1063 Lend_sse:
1064         mov 12(%esp), %esi      /* Parameter: start address */
1065         prefetcht0 0(%esi)      /* Prefetch the start of the block into cache,
1066                                  * since it's likely to be used immediately. */
1067         pop %edi      /* Restore temp registers */
1068         pop %esi
1069         ret
1070         SIZE(GNAME(fast_bzero_sse))
1071                 
1072 \f
1073         .text
1074         .align  align_8byte,0x90
1075         .globl GNAME(fast_bzero_base)
1076         TYPE(GNAME(fast_bzero_base))
1077         
1078 GNAME(fast_bzero_base):
1079         /* A fast routine for zero-filling blocks of memory that are
1080          * guaranteed to start and end at a 4096-byte aligned address.
1081          */        
1082         push %eax                 /* Save temporary registers */
1083         push %ecx
1084         push %edi
1085         mov 20(%esp), %ecx        /* Parameter: amount of bytes to fill */
1086         mov 16(%esp), %edi        /* Parameter: start address */
1087         xor %eax, %eax            /* Zero EAX */
1088         shr $2, %ecx              /* Amount of 4-byte blocks to copy */
1089         jz  Lend_base
1090         cld                       /* Set direction of STOSL to increment */
1091
1092         rep
1093         stosl                     /* Store EAX to *EDI, ECX times, incrementing
1094                                    * EDI by 4 after each store */
1095         
1096 Lend_base:        
1097         pop %edi                  /* Restore temp registers */
1098         pop %ecx
1099         pop %eax
1100         ret
1101         SIZE(GNAME(fast_bzero_base))
1102         
1103 \f       
1104         END()
1105