2009-12-02 11:24:00 -05:00
|
|
|
; Copyright (C) 2009, 2008 Brian Gladman
|
|
|
|
;
|
|
|
|
; All rights reserved.
|
|
|
|
;
|
|
|
|
; Redistribution and use in source and binary forms, with or without
|
|
|
|
; modification, are permitted provided that the following conditions are
|
|
|
|
; met:
|
|
|
|
; 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
; this list of conditions and the following disclaimer.
|
|
|
|
;
|
|
|
|
; 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
; notice, this list of conditions and the following disclaimer in the
|
|
|
|
; documentation and/or other materials provided with the distribution.
|
|
|
|
;
|
|
|
|
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
; ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
; PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
; HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
;
|
2008-11-07 15:27:10 -05:00
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
; Standardised register numbering scheme
|
2008-11-07 15:27:10 -05:00
|
|
|
|
2009-01-10 10:15:37 -05:00
|
|
|
%define r0 rax
|
|
|
|
%define r1 rdx
|
|
|
|
%define r2 rcx
|
|
|
|
%define r3 rbx
|
|
|
|
%define r4 rsi
|
|
|
|
%define r5 rdi
|
|
|
|
%define r6 rbp
|
|
|
|
%define r7 rsp
|
|
|
|
|
|
|
|
%define r0d eax
|
|
|
|
%define r1d edx
|
|
|
|
%define r2d ecx
|
|
|
|
%define r3d ebx
|
|
|
|
%define r4d esi
|
|
|
|
%define r5d edi
|
|
|
|
%define r6d ebp
|
|
|
|
%define r7d esp
|
|
|
|
|
|
|
|
%define r0w ax
|
|
|
|
%define r1w dx
|
|
|
|
%define r2w cx
|
|
|
|
%define r3w bx
|
|
|
|
%define r4w si
|
|
|
|
%define r5w di
|
|
|
|
%define r6w bp
|
|
|
|
%define r7w sp
|
|
|
|
|
|
|
|
%define r0b al
|
|
|
|
%define r1b dl
|
|
|
|
%define r2b cl
|
|
|
|
%define r3b bl
|
|
|
|
%define r4b sil
|
|
|
|
%define r5b dil
|
|
|
|
%define r6b bpl
|
|
|
|
%define r7b spl
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
; Standard macro for alignment (used to allow easy subsititution of
|
|
|
|
; alternative padding schemes)
|
|
|
|
|
2009-03-24 10:40:39 -04:00
|
|
|
%macro xalign 1
|
|
|
|
align %1
|
|
|
|
%endmacro
|
|
|
|
|
2009-05-06 14:20:52 -04:00
|
|
|
; YASM macros for handling Windows Prologues and Epilogues
|
|
|
|
;
|
|
|
|
; Copyright 2008, 2009 Brian Gladman
|
|
|
|
;
|
|
|
|
; Windows x64 prologue macro
|
|
|
|
;
|
|
|
|
; prologue name, stack_slots, register_save_list
|
|
|
|
;
|
|
|
|
; name: routine name
|
|
|
|
; register_save_list: comma separated list of registers to save
|
|
|
|
; stack_slots: stack space needed in 8 byte units
|
|
|
|
|
|
|
|
; Windows x64 epilogue macro
|
|
|
|
;
|
|
|
|
; epilogue register_save_list
|
|
|
|
;
|
|
|
|
; register_save_list: comma separated list of registers to save
|
|
|
|
; in same order used in prologue
|
|
|
|
;
|
|
|
|
; On return the macro variable 'stack_use' gives the total number
|
|
|
|
; of bytes used on the stack. This allows the function parameters
|
|
|
|
; to be accessed at [rsp + stack_use + 8 * n] where n starts at 1
|
|
|
|
; (for n = 1..4 this is shadow space for a register parameter)
|
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
%macro FRAME_PROC 2-*
|
2009-01-10 10:15:37 -05:00
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
global __g%1
|
|
|
|
|
|
|
|
%ifdef DLL
|
|
|
|
export __g%1
|
|
|
|
%endif
|
|
|
|
|
|
|
|
PROC_FRAME __g%1
|
2009-01-10 10:15:37 -05:00
|
|
|
%rotate 1
|
|
|
|
|
|
|
|
%if %1 < 0
|
|
|
|
%error Negative stack allocation not allowed
|
|
|
|
%else
|
|
|
|
%if (%0 & 1) == (%1 & 1)
|
|
|
|
%assign stack_use 8 * (%1 + 1)
|
|
|
|
%else
|
|
|
|
%assign stack_use 8 * %1
|
|
|
|
%endif
|
|
|
|
%endif
|
|
|
|
%rotate 1
|
|
|
|
|
|
|
|
%if %0 > 2
|
|
|
|
%rep %0 - 2
|
|
|
|
push_reg %1
|
|
|
|
%rotate 1
|
|
|
|
%endrep
|
|
|
|
%endif
|
|
|
|
|
|
|
|
%if stack_use > 0
|
|
|
|
alloc_stack stack_use
|
|
|
|
%endif
|
|
|
|
|
|
|
|
%assign stack_use stack_use + 8 * (%0 - 2)
|
|
|
|
END_PROLOGUE
|
|
|
|
|
2008-11-07 15:27:10 -05:00
|
|
|
%endmacro
|
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
%macro END_PROC 0-*
|
2009-01-10 10:15:37 -05:00
|
|
|
|
|
|
|
add rsp, stack_use - 8 * %0
|
|
|
|
%if %0 > 0
|
|
|
|
%rep %0
|
|
|
|
%rotate -1
|
|
|
|
pop %1
|
|
|
|
%endrep
|
|
|
|
%endif
|
2008-11-07 15:27:10 -05:00
|
|
|
ret
|
2009-01-10 10:15:37 -05:00
|
|
|
ENDPROC_FRAME
|
|
|
|
|
2008-11-07 15:27:10 -05:00
|
|
|
%endmacro
|
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
%macro LEAF_PROC 1
|
|
|
|
|
|
|
|
global __g%1
|
|
|
|
|
|
|
|
%ifdef DLL
|
|
|
|
export __g%1
|
2008-11-07 15:27:10 -05:00
|
|
|
%endif
|
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
__g%1:
|
2008-11-07 15:27:10 -05:00
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
%endmacro
|
2009-01-10 10:15:37 -05:00
|
|
|
|
|
|
|
; Macros for using assembler code using the GCC Calling
|
|
|
|
; Conventions in Windows.
|
|
|
|
|
|
|
|
; Note that these macros loads integer parameters that
|
|
|
|
; are 32-bits as 64-bit integers with no zero or sign
|
|
|
|
; extension. For such parameters either 'mov rxd, rxd'
|
2009-03-07 10:00:35 -05:00
|
|
|
; (for unsigned) or 'movsxd rx, rxd' (for signed) will
|
|
|
|
; be needed before they are used as 64-bit integers.
|
|
|
|
; These macros move the first six integer parameters
|
|
|
|
; from Microsoft ABI calling calling conventions to
|
|
|
|
; those used by GCC:
|
2009-01-10 10:15:37 -05:00
|
|
|
;
|
|
|
|
; function( MSVC --> GCC
|
|
|
|
; p1, rcx rdi
|
|
|
|
; p2, rdx rsi
|
|
|
|
; p3, r8 rdx
|
|
|
|
; p4, r9 rcx
|
|
|
|
; p5, [rsp+40] r8
|
|
|
|
; p6, [rsp+48] r9
|
|
|
|
;
|
2009-03-07 10:00:35 -05:00
|
|
|
; As already discussed, care must be taken with 32-bit
|
2009-01-10 10:15:37 -05:00
|
|
|
; values in 64-bit registers or on the stack because
|
|
|
|
; the upper 32-bits of such parameters are undefined.
|
|
|
|
;
|
2009-05-01 15:03:56 -04:00
|
|
|
; WIN64_GCC_PROC name, n_parms, (frame | leaf)
|
2009-01-10 10:15:37 -05:00
|
|
|
;
|
2009-03-07 10:00:35 -05:00
|
|
|
; WIN64_GCC_END frame | leaf
|
2009-01-10 10:15:37 -05:00
|
|
|
;
|
|
|
|
; name subroutine name
|
2009-05-06 14:20:52 -04:00
|
|
|
; n_parms number of parameters (default 6)
|
|
|
|
; type frame or leaf function (default frame)
|
|
|
|
;
|
|
|
|
; These defines are also used:
|
2009-01-10 10:15:37 -05:00
|
|
|
;
|
2009-05-06 14:20:52 -04:00
|
|
|
; reg_save_list list of registers to be saved
|
|
|
|
; and restored
|
|
|
|
; stack_slots number of 8 byte slots needed
|
|
|
|
; on the stack (excluding the
|
|
|
|
; register save/restore space)
|
2009-01-10 10:15:37 -05:00
|
|
|
|
2009-05-01 15:03:56 -04:00
|
|
|
%macro WIN64_GCC_PROC 1-3 6, frame
|
2009-01-10 10:15:37 -05:00
|
|
|
|
2009-05-01 15:03:56 -04:00
|
|
|
%ifidn %3, frame
|
2009-01-10 10:15:37 -05:00
|
|
|
|
|
|
|
%ifndef reg_save_list
|
|
|
|
%define reg_save_list rsi, rdi
|
|
|
|
%endif
|
|
|
|
|
|
|
|
%ifndef stack_slots
|
|
|
|
%define stack_slots 0
|
|
|
|
%endif
|
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
FRAME_PROC %1, stack_slots, reg_save_list
|
2009-05-01 15:03:56 -04:00
|
|
|
|
|
|
|
%elifidn %3, leaf
|
|
|
|
|
|
|
|
LEAF_PROC %1
|
|
|
|
|
|
|
|
%else
|
|
|
|
|
|
|
|
%error no (or wrong) function type defined
|
|
|
|
|
|
|
|
%endif
|
|
|
|
|
2009-03-07 10:00:35 -05:00
|
|
|
%if %2 > 0
|
2009-01-10 10:15:37 -05:00
|
|
|
mov rdi, rcx
|
|
|
|
%endif
|
2009-03-07 10:00:35 -05:00
|
|
|
%if %2 > 1
|
2009-01-10 10:15:37 -05:00
|
|
|
mov rsi, rdx
|
|
|
|
%endif
|
2009-03-07 10:00:35 -05:00
|
|
|
%if %2 > 2
|
2009-01-10 10:15:37 -05:00
|
|
|
mov rdx, r8
|
|
|
|
%endif
|
2009-03-07 10:00:35 -05:00
|
|
|
%if %2 > 3
|
2009-01-10 10:15:37 -05:00
|
|
|
mov rcx, r9
|
|
|
|
%endif
|
2009-03-07 10:00:35 -05:00
|
|
|
%if %2 > 4
|
2009-01-10 10:15:37 -05:00
|
|
|
mov r8, [rsp + stack_use + 40]
|
|
|
|
%endif
|
2009-03-07 10:00:35 -05:00
|
|
|
%if %2 > 5
|
2009-01-10 10:15:37 -05:00
|
|
|
mov r9, [rsp + stack_use + 48]
|
|
|
|
%endif
|
|
|
|
|
|
|
|
%endmacro
|
|
|
|
|
2009-05-01 15:03:56 -04:00
|
|
|
%macro WIN64_GCC_END 0-1 frame
|
2009-01-10 10:15:37 -05:00
|
|
|
|
2009-05-01 15:03:56 -04:00
|
|
|
%ifidn %1, frame
|
|
|
|
END_PROC reg_save_list
|
|
|
|
%elifidn %1, leaf
|
2009-01-10 10:15:37 -05:00
|
|
|
ret
|
|
|
|
%else
|
|
|
|
%error no (or wrong) function type defined
|
|
|
|
%endif
|
|
|
|
|
|
|
|
%endmacro
|