; Standard macro set for NASM -*- nasm -*-

; Macros to make NASM ignore some TASM directives before the first include
; directive.

    %idefine IDEAL
    %idefine JUMPS
    %idefine P386
    %idefine P486
    %idefine P586
    %idefine END

; This is a magic token which indicates the end of the TASM macros
*END*TASM*MACROS*

; Note that although some user-level forms of directives are defined
; here, not all of them are: the user-level form of a format-specific
; directive should be defined in the module for that directive.

; These two need to be defined, though the actual definitions will
; be constantly updated during preprocessing.
%define __FILE__
%define __LINE__

%define __SECT__ [section .text] ; it ought to be defined, even if as nothing

%imacro section 1+.nolist
%define __SECT__ [section %1]
	  __SECT__
%endmacro
%imacro segment 1+.nolist
%define __SECT__ [segment %1]
	  __SECT__
%endmacro

%imacro absolute 1+.nolist
%define __SECT__ [absolute %1]
	  __SECT__
%endmacro

%imacro struc 1.nolist
%push struc
%define %$strucname %1
[absolute 0]
%$strucname:			; allow definition of `.member' to work sanely
%endmacro 
%imacro endstruc 0.nolist
%{$strucname}_size:
%pop
__SECT__
%endmacro

%imacro istruc 1.nolist
%push istruc
%define %$strucname %1
%$strucstart:
%endmacro
%imacro at 1-2+.nolist
	  times %1-($-%$strucstart) db 0
	  %2
%endmacro
%imacro iend 0.nolist
	  times %{$strucname}_size-($-%$strucstart) db 0
%pop
%endmacro

%imacro align 1-2+.nolist nop
%ifidni %2,nop
	  [align %1]
%else
	  times ($$-$) & ((%1)-1) %2
%endif
%endmacro
%imacro alignb 1-2+.nolist resb 1
	  times ($$-$) & ((%1)-1) %2
%endmacro

%imacro extern 1-*.nolist
%rep %0
[extern %1]
%rotate 1
%endrep
%endmacro

%imacro bits 1+.nolist
[bits %1]
%endmacro

%imacro use16 0.nolist
[bits 16]
%endmacro
%imacro use32 0.nolist
[bits 32]
%endmacro
%imacro use64 0.nolist
[bits 64]
%endmacro

%imacro global 1-*.nolist
%rep %0
[global %1]
%rotate 1
%endrep
%endmacro

%imacro common 1-*.nolist
%rep %0
[common %1]
%rotate 1
%endrep
%endmacro

%imacro cpu 1+.nolist
[cpu %1]
%endmacro

%imacro default 1+.nolist
[default %1]
%endmacro

; NASM compatibility shim
%define __OUTPUT_FORMAT__ __YASM_OBJFMT__

%ifidn __YASM_OBJFMT__,bin
%imacro org 1+.nolist
[org %1]
%endmacro
%endif

%ifidn __YASM_OBJFMT__,win32
%imacro export 1+.nolist
[export %1]
%endmacro
%endif

%ifidn __YASM_OBJFMT__,win64
%define __YASM_WIN64__
%endif

%ifidn __YASM_OBJFMT__,x64
%define __YASM_WIN64__
%endif

%ifdef __YASM_WIN64__
%undef __YASM_WIN64__

%imacro export 1+.nolist
[export %1]
%endmacro

; Raw exception handling operations
%imacro proc_frame 1+.nolist
%1:
[proc_frame %1]
%endmacro

%if 0
; Disable these as they're too closely named to the macroized ones.
; MASM needs a preceding . to use these, so it seems reasonable for
; us to similarly require the [].
%imacro pushreg 1.nolist
[pushreg %1]
%endmacro

%imacro setframe 1-2.nolist
[setframe %1 %2]
%endmacro

%imacro allocstack 1.nolist
[allocstack %1]
%endmacro

%imacro savereg 2.nolist
[savereg %1 %2]
%endmacro

%imacro savexmm128 2.nolist
[savexmm128 %1 %2]
%endmacro

%imacro pushframe 0-1.nolist
[pushframe %1]
%endmacro

%imacro endprolog 0.nolist
[endprolog]
%endmacro
%endif

%imacro endproc_frame 0.nolist
[endproc_frame]
%endmacro

; Complex (macro) exception handling operations
; Mimics many macros provided by MASM's macamd64.inc
%imacro push_reg 1
push %1
[pushreg %1]
%endmacro

%imacro rex_push_reg 1
db 0x48
push %1
[pushreg %1]
%endmacro

%imacro push_eflags 0
pushfq
[allocstack 8]
%endmacro

%imacro rex_push_eflags 0
db 0x48
pushfq
[allocstack 8]
%endmacro

%imacro alloc_stack 1
sub rsp, %1
[allocstack %1]
%endmacro

%imacro save_reg 2
mov [rsp+%2], %1
[savereg %1 %2]
%endmacro

%imacro save_xmm128 2
movdqa [rsp+%2], %1
[savexmm128 %1 %2]
%endmacro

%imacro push_frame 0-1.nolist
[pushframe %1]
%endmacro

%imacro set_frame 1-2
%if %0==1
mov %1, rsp
%else
lea %1, [rsp+%2]
%endif
[setframe %1 %2]
%endmacro

%imacro end_prolog 0.nolist
[endprolog]
%endmacro

%imacro end_prologue 0.nolist
[endprolog]
%endmacro

%endif

%ifidn __YASM_OBJFMT__,elf
%define __YASM_ELF__
%endif

%ifidn __YASM_OBJFMT__,elf32
%define __YASM_ELF__
%endif

%ifidn __YASM_OBJFMT__,elf64
%define __YASM_ELF__
%endif

%ifdef __YASM_ELF__
%undef __YASM_ELF__
%imacro type 1+.nolist
[type %1]
%endmacro
%imacro size 1+.nolist
[size %1]
%endmacro
%imacro weak 1+.nolist
[weak %1]
%endmacro
%endif

%ifidn __YASM_OBJFMT__,rdf
%imacro library 1+.nolist
[library %1]
%endmacro
%imacro module 1+.nolist
[module %1]
%endmacro
%endif

