text
stringlengths
12
786k
type rounding = X86_ast . rounding = | RoundUp | RoundDown | RoundNearest | RoundTruncate
type constant = X86_ast . constant = | Const of int64 | ConstThis | ConstLabel of string | ConstAdd of constant * constant | ConstSub of constant * constant
type data_type = X86_ast . data_type = | NONE | REAL4 | REAL8 | BYTE | WORD | DWORD | QWORD | OWORD | NEAR | PROC
type reg64 = X86_ast . reg64 = | RAX | RBX | RCX | RDX | RSP | RBP | RSI | RDI | R8 | R9 | R10 | R11 | R12 | R13 | R14 | R15
type reg8h = X86_ast . reg8h = AH | BH | CH | DH [ @@ deriving eq , ord , show ]
type registerf = X86_ast . registerf = XMM of int | TOS | ST of int
type arch = X86_ast . arch = X64 | X86 [ @@ deriving eq , ord , show ]
type addr = X86_ast . addr = { arch : arch ; typ : data_type ; idx : reg64 ; scale : int ; base : reg64 option ; sym : string option ; displ : int ; }
type arg = X86_ast . arg = | Imm of int64 | Sym of string | Reg8L of reg64 | Reg8H of reg8h | Reg16 of reg64 | Reg32 of reg64 | Reg64 of reg64 | Regf of registerf | Mem of addr | Mem64_RIP of data_type * string * int
type instruction = X86_ast . instruction = | ADD of arg * arg | ADDSD of arg * arg | AND of arg * arg | ANDPD of arg * arg | BSWAP of arg | CALL of arg | CDQ | CMOV of condition * arg * arg | CMP of arg * arg | COMISD of arg * arg | CQO | CVTSD2SI of arg * arg | CVTSD2SS of arg * arg | CVTSI2SD of arg * arg | CVTSS2SD of arg * arg | CVTTSD2SI of arg * arg | DEC of arg | DIVSD of arg * arg | FABS | FADD of arg | FADDP of arg * arg | FCHS | FCOMP of arg | FCOMPP | FCOS | FDIV of arg | FDIVP of arg * arg | FDIVR of arg | FDIVRP of arg * arg | FILD of arg | FISTP of arg | FLD of arg | FLD1 | FLDCW of arg | FLDLG2 | FLDLN2 | FLDZ | FMUL of arg | FMULP of arg * arg | FNSTCW of arg | FNSTSW of arg | FPATAN | FPTAN | FSIN | FSQRT | FSTP of arg | FSUB of arg | FSUBP of arg * arg | FSUBR of arg | FSUBRP of arg * arg | FXCH of arg | FYL2X | HLT | IDIV of arg | IMUL of arg * arg option | INC of arg | J of condition * arg | JMP of arg | LEA of arg * arg | LEAVE | MOV of arg * arg | MOVAPD of arg * arg | MOVLPD of arg * arg | MOVSD of arg * arg | MOVSS of arg * arg | MOVSX of arg * arg | MOVSXD of arg * arg | MOVZX of arg * arg | MULSD of arg * arg | NEG of arg | NOP | OR of arg * arg | POP of arg | PUSH of arg | RET | ROUNDSD of rounding * arg * arg | SAL of arg * arg | SAR of arg * arg | SET of condition * arg | SHR of arg * arg | SQRTSD of arg * arg | SUB of arg * arg | SUBSD of arg * arg | TEST of arg * arg | UCOMISD of arg * arg | XCHG of arg * arg | XOR of arg * arg | XORPD of arg * arg
type asm_line = X86_ast . asm_line = | Ins of instruction | Align of bool * int | Byte of constant | Bytes of string | Comment of string | Global of string | Long of constant | NewLabel of string * data_type | Quad of constant | Section of string list * string option * string list | Space of int | Word of constant | External of string * data_type | Mode386 | Model of string | Cfi_adjust_cfa_offset of int | Cfi_endproc | Cfi_startproc | File of int * string | Indirect_symbol of string | Loc of int * int * int | Private_extern of string | Set of string * constant | Size of string * constant | Type of string * string
type asm_program = asm_line list [ @@ deriving eq , ord , show ]
let sym s = Sym s
let nat n = Imm ( Int64 . of_nativeint n )
let int n = Imm ( Int64 . of_int n )
let const_32 n = Const ( Int64 . of_int32 n )
let const_nat n = Const ( Int64 . of_nativeint n )
let const n = Const ( Int64 . of_int n )
let al = Reg8L RAX
let ah = Reg8H AH
let cl = Reg8L RCX
let ax = Reg16 RAX
let rax = Reg64 RAX
let r10 = Reg64 R10
let r11 = Reg64 R11
let r13 = Reg64 R13
let r14 = Reg64 R14
let r15 = Reg64 R15
let rsp = Reg64 RSP
let rbp = Reg64 RBP
let xmm15 = Regf ( XMM 15 )
let eax = Reg32 RAX
let ebx = Reg32 RBX
let ecx = Reg32 RCX
let edx = Reg32 RDX
let ebp = Reg32 RBP
let esp = Reg32 RSP
let st0 = Regf ( ST 0 )
let st1 = Regf ( ST 1 )
let mem32 typ ( ? scale = 1 ) ? base ? sym displ idx = assert ( scale >= 0 ) ; Mem { arch = X86 ; typ ; idx ; scale ; base ; sym ; displ }
let mem64 typ ( ? scale = 1 ) ? base ? sym displ idx = assert ( scale > 0 ) ; Mem { arch = X64 ; typ ; idx ; scale ; base ; sym ; displ }
let mem64_rip typ ( ? ofs = 0 ) s = Mem64_RIP ( typ , s , ofs )
module D = struct let section segment flags args = directive ( Section ( segment , flags , args ) ) let align n = directive ( Align ( false , n ) ) let byte n = directive ( Byte n ) let bytes s = directive ( Bytes s ) let cfi_adjust_cfa_offset n = directive ( Cfi_adjust_cfa_offset n ) let cfi_endproc ( ) = directive Cfi_endproc let cfi_startproc ( ) = directive Cfi_startproc let comment s = directive ( Comment s ) let data ( ) = section [ " . data " ] None [ ] let extrn s ptr = directive ( External ( s , ptr ) ) let file ~ file_num ~ file_name = directive ( File ( file_num , file_name ) ) let global s = directive ( Global s ) let indirect_symbol s = directive ( Indirect_symbol s ) let label ( ? typ = NONE ) s = directive ( NewLabel ( s , typ ) ) let loc ~ file_num ~ line ~ col = directive ( Loc ( file_num , line , col ) ) let long cst = directive ( Long cst ) let mode386 ( ) = directive Mode386 let model name = directive ( Model name ) let private_extern s = directive ( Private_extern s ) let qword cst = directive ( Quad cst ) let setvar ( x , y ) = directive ( Set ( x , y ) ) let size name cst = directive ( Size ( name , cst ) ) let space n = directive ( Space n ) let text ( ) = section [ " . text " ] None [ ] let type_ name typ = directive ( Type ( name , typ ) ) let word cst = directive ( Word cst ) end
module I = struct let add x y = emit ( ADD ( x , y ) ) let addsd x y = emit ( ADDSD ( x , y ) ) let and_ x y = emit ( AND ( x , y ) ) let andpd x y = emit ( ANDPD ( x , y ) ) let bswap x = emit ( BSWAP x ) let call x = emit ( CALL x ) let cdq ( ) = emit CDQ let cmp x y = emit ( CMP ( x , y ) ) let comisd x y = emit ( COMISD ( x , y ) ) let cqo ( ) = emit CQO let cvtsd2ss x y = emit ( CVTSD2SS ( x , y ) ) let cvtsi2sd x y = emit ( CVTSI2SD ( x , y ) ) let cvtss2sd x y = emit ( CVTSS2SD ( x , y ) ) let cvttsd2si x y = emit ( CVTTSD2SI ( x , y ) ) let dec x = emit ( DEC x ) let divsd x y = emit ( DIVSD ( x , y ) ) let fabs ( ) = emit FABS let fadd x = emit ( FADD x ) let faddp x y = emit ( FADDP ( x , y ) ) let fchs ( ) = emit FCHS let fcomp x = emit ( FCOMP x ) let fcompp ( ) = emit FCOMPP let fcos ( ) = emit FCOS let fdiv x = emit ( FDIV x ) let fdivp x y = emit ( FDIVP ( x , y ) ) let fdivr x = emit ( FDIVR x ) let fdivrp x y = emit ( FDIVRP ( x , y ) ) let fild x = emit ( FILD x ) let fistp x = emit ( FISTP x ) let fld x = emit ( FLD x ) let fld1 ( ) = emit FLD1 let fldcw x = emit ( FLDCW x ) let fldlg2 ( ) = emit FLDLG2 let fldln2 ( ) = emit FLDLN2 let fldz ( ) = emit FLDZ let fmul x = emit ( FMUL x ) let fmulp x y = emit ( FMULP ( x , y ) ) let fnstcw x = emit ( FNSTCW x ) let fnstsw x = emit ( FNSTSW x ) let fpatan ( ) = emit FPATAN let fptan ( ) = emit FPTAN let fsin ( ) = emit FSIN let fsqrt ( ) = emit FSQRT let fstp x = emit ( FSTP x ) let fsub x = emit ( FSUB x ) let fsubp x y = emit ( FSUBP ( x , y ) ) let fsubr x = emit ( FSUBR x ) let fsubrp x y = emit ( FSUBRP ( x , y ) ) let fxch x = emit ( FXCH x ) let fyl2x ( ) = emit FYL2X let hlt ( ) = emit HLT let idiv x = emit ( IDIV x ) let imul x y = emit ( IMUL ( x , y ) ) let inc x = emit ( INC x ) let j cond x = emit ( J ( cond , x ) ) let ja = j A let jae = j AE let jb = j B let jbe = j BE let je = j E let jg = j G let jmp x = emit ( JMP x ) let jne = j NE let jp = j P let lea x y = emit ( LEA ( x , y ) ) let mov x y = emit ( MOV ( x , y ) ) let movapd x y = emit ( MOVAPD ( x , y ) ) let movsd x y = emit ( MOVSD ( x , y ) ) let movss x y = emit ( MOVSS ( x , y ) ) let movsx x y = emit ( MOVSX ( x , y ) ) let movsxd x y = emit ( MOVSXD ( x , y ) ) let movzx x y = emit ( MOVZX ( x , y ) ) let mulsd x y = emit ( MULSD ( x , y ) ) let nop ( ) = emit NOP let or_ x y = emit ( OR ( x , y ) ) let pop x = emit ( POP x ) let push x = emit ( PUSH x ) let ret ( ) = emit RET let sal x y = emit ( SAL ( x , y ) ) let sar x y = emit ( SAR ( x , y ) ) let set cond x = emit ( SET ( cond , x ) ) let shr x y = emit ( SHR ( x , y ) ) let sqrtsd x y = emit ( SQRTSD ( x , y ) ) let sub x y = emit ( SUB ( x , y ) ) let subsd x y = emit ( SUBSD ( x , y ) ) let test x y = emit ( TEST ( x , y ) ) let ucomisd x y = emit ( UCOMISD ( x , y ) ) let xchg x y = emit ( XCHG ( x , y ) ) let xor x y = emit ( XOR ( x , y ) ) let xorpd x y = emit ( XORPD ( x , y ) ) end
type section = { sec_name : string ; mutable sec_instrs : X86_ast_helpers . asm_line array ; }
type data_size = B8 | B16 | B32 | B64 [ @@ deriving eq , ord , show ]
module IntSet = Set . Make ( struct type t = int let compare : t -> t -> int = compare end )
let print_old_arg ppf = function | Imm _ -> Format . fprintf ppf " Imm " | Reg8L _ -> Format . fprintf ppf " Reg8L " | Reg8H _ -> Format . fprintf ppf " Reg8H " | Reg16 _ -> Format . fprintf ppf " Reg16 " | Reg32 _ -> Format . fprintf ppf " Reg32 " | Reg64 _ -> Format . fprintf ppf " Reg64 " | Regf _ -> Format . fprintf ppf " Regf " | Mem _ -> Format . fprintf ppf " Mem " | Mem64_RIP _ -> Format . fprintf ppf " Mem64_RIP " | Sym _ -> Format . fprintf ppf " Sym "
module Relocation = struct module Kind = struct type t = | REL32 of string * int64 | DIR32 of string * int64 | DIR64 of string * int64 [ @@ deriving eq , ord , show ] end type t = { offset_from_section_beginning : int ; kind : Kind . t } [ @@ deriving eq , ord , show ] end
type symbol = { sy_name : string ; mutable sy_type : string option ; mutable sy_size : int option ; mutable sy_global : bool ; mutable sy_sec : section ; [ @ printer fun fmt _ -> Format . fprintf fmt " . . . " ] mutable sy_pos : int option ; mutable sy_num : int option ; }
type buffer = { sec : section ; buf : Buffer . t ; mutable labels : symbol String . Map . t ; mutable patches : ( int * data_size * int64 ) list ; mutable relocations : Relocation . t list ; }
type local_reloc = | RelocCall of string | RelocShortJump of string * int | RelocLongJump of string | RelocConstant of X86_ast_helpers . constant * data_size
type result = | Rint of int64 | Rabs of string * int64 | Rrel of string * int64
let get_symbol b s = try String . Map . find s b . labels with Not_found -> let sy = { sy_name = s ; sy_type = None ; sy_size = None ; sy_pos = None ; sy_global = false ; sy_num = None ; sy_sec = b . sec ; } in b . labels <- String . Map . add ~ key : s ~ data : sy b . labels ; sy
let buf_int8 b i = Buffer . add_char b . buf ( char_of_int ( i land 0xff ) )
let buf_int8L b iL = buf_int8 b ( Int64 . to_int iL )
let buf_int16L b iL = buf_int8L b iL ; buf_int8L b ( Int64 . shift_right iL 8 )
let buf_int32L b iL = buf_int16L b iL ; buf_int16L b ( Int64 . shift_right iL 16 )
let buf_int64L b iL = buf_int32L b iL ; buf_int32L b ( Int64 . shift_right iL 32 )
let str_int8L s pos v = Bytes . set s pos ( char_of_int ( Int64 . to_int v land 0xff ) )
let str_int16L s pos v = str_int8L s pos v ; str_int8L s ( pos + 1 ) ( Int64 . shift_right_logical v 8 )
let str_int32L s pos v = str_int16L s pos v ; str_int16L s ( pos + 2 ) ( Int64 . shift_right_logical v 16 )
let str_int64L s pos v = str_int32L s pos v ; str_int32L s ( pos + 4 ) ( Int64 . shift_right_logical v 32 )
let local_relocs = ref [ ]
let local_labels = ref String . Map . empty
let forced_long_jumps = ref IntSet . empty
let instr_size = ref 4
let new_buffer sec = { sec ; buf = Buffer . create 10000 ; labels = String . Map . empty ; relocations = [ ] ; patches = [ ] ; }
let label_pos b lbl = match ( String . Map . find lbl b . labels ) . sy_pos with | None -> raise Not_found | Some pos -> pos
let eval_const b current_pos cst = let rec eval = function | Const n -> Rint n | ConstThis -> Rabs ( " " , 0L ) | ConstLabel lbl -> Rabs ( lbl , 0L ) | ConstSub ( c1 , c2 ) -> ( let c1 = eval c1 and c2 = eval c2 in match ( c1 , c2 ) with | Rint n1 , Rint n2 -> Rint ( Int64 . sub n1 n2 ) | Rabs ( s , n1 ) , Rint n2 -> Rabs ( s , Int64 . sub n1 n2 ) | Rrel ( s , n1 ) , Rint n2 -> Rrel ( s , Int64 . sub n1 n2 ) | Rabs ( " " , n1 ) , Rabs ( " " , n2 ) -> Rint ( Int64 . sub n1 n2 ) | Rabs ( " " , n1 ) , Rabs ( s2 , n2 ) -> ( try let sy2 = String . Map . find s2 b . labels in match sy2 . sy_pos with | Some pos2 -> let pos2 = Int64 . of_int pos2 in Rint ( Int64 . sub ( Int64 . add n1 ( Int64 . of_int current_pos ) ) ( Int64 . add pos2 n2 ) ) | _ -> assert false with Not_found -> assert false ) | Rabs ( s , n1 ) , Rabs ( " " , n2 ) -> ( try let sy = String . Map . find s b . labels in match sy . sy_pos with | Some pos -> let pos = Int64 . of_int pos in Rint ( Int64 . sub ( Int64 . add pos n1 ) ( Int64 . add n2 ( Int64 . of_int current_pos ) ) ) | _ -> assert false with Not_found -> Rrel ( s , Int64 . sub n1 n2 ) ) | Rabs ( s1 , n1 ) , Rabs ( s2 , n2 ) -> ( try let sy2 = String . Map . find s2 b . labels in try let sy1 = String . Map . find s1 b . labels in assert ( sy1 . sy_sec == sy2 . sy_sec ) ; match ( sy1 . sy_pos , sy2 . sy_pos ) with | Some pos1 , Some pos2 -> let pos1 = Int64 . of_int pos1 in let pos2 = Int64 . of_int pos2 in Rint ( Int64 . sub ( Int64 . add pos1 n1 ) ( Int64 . add pos2 n2 ) ) | _ -> assert false with Not_found -> ( match sy2 . sy_pos with | Some pos2 -> let pos2 = Int64 . of_int pos2 in Rrel ( s1 , Int64 . sub ( Int64 . add n1 ( Int64 . of_int current_pos ) ) ( Int64 . add pos2 n2 ) ) | _ -> assert false ) with Not_found -> assert false ) | _ -> assert false ) | ConstAdd ( c1 , c2 ) -> ( let c1 = eval c1 and c2 = eval c2 in match ( c1 , c2 ) with | Rint n1 , Rint n2 -> Rint ( Int64 . add n1 n2 ) | Rabs ( s , n1 ) , Rint n2 | Rint n2 , Rabs ( s , n1 ) -> Rabs ( s , Int64 . add n1 n2 ) | Rrel ( s , n1 ) , Rint n2 | Rint n2 , Rrel ( s , n1 ) -> Rrel ( s , Int64 . add n1 n2 ) | Rrel ( s , n1 ) , Rabs ( " " , n2 ) -> Rabs ( s , Int64 . add n1 n2 ) | _ -> assert false ) in try let r = eval cst in r with e -> Printf . eprintf " Error in eval_const : exception % S \ n " %! ( Printexc . to_string e ) ; raise e
let is_imm32L n = n < 0x8000_0000L && n >= - 0x8000_0000L
let is_imm8L x = x < 128L && x >= - 128L
let rd_of_regf regf = match regf with | XMM n -> n | TOS -> assert false | ST _st -> assert false
let rd_of_reg64 = function | RAX -> 0 | RCX -> 1 | RDX -> 2 | RBX -> 3 | RSP -> 4 | RBP -> 5 | RSI -> 6 | RDI -> 7 | R8 -> 8 | R9 -> 9 | R10 -> 10 | R11 -> 11 | R12 -> 12 | R13 -> 13 | R14 -> 14 | R15 -> 15
let rd_of_reg8 = function | Reg8L r -> rd_of_reg64 r | Reg8H AH -> 4 | Reg8H CH -> 5 | Reg8H DH -> 6 | Reg8H BH -> 7 | _ -> assert false
let cd_of_condition condition = match condition with | O -> 0 | NO -> 1 | B -> 2 | AE -> 3 | E -> 4 | NE -> 5 | BE -> 6 | A -> 7 | S -> 8 | NS -> 9 | P -> 10 | NP -> 11 | L -> 12 | GE -> 13 | LE -> 14 | G -> 15
let rexr_reg reg = if reg > 7 then rexr else 0
let rexw = rex lor 0b00001000
let rexx_index reg = if reg > 7 then rexx else 0
let rexb_opcode reg = if reg > 7 then rexb else 0
let rexb_rm reg = if reg > 7 then rexb else 0
let rexb_base reg = if reg > 7 then rexb else 0
let reg7 reg = reg land 0x07
let rex_of_reg8 = function Reg8L ( RSP | RBP | RSI | RDI ) -> rex | _ -> 0
let rex_of_reg16 = function | RAX | RCX | RDX | RBX | RSP | RBP | RSI | RDI -> 0 | R8 | R9 | R10 | R11 | R12 | R13 | R14 | R15 -> rex
let mod_rm_reg m rm reg = ( m lsl 6 ) + reg7 rm + ( reg7 reg lsl 3 )
let sib scale index base = let scale = match scale with 1 -> 0 | 2 -> 1 | 4 -> 2 | 8 -> 3 | _ -> assert false in ( scale lsl 6 ) lor ( reg7 index lsl 3 ) lor reg7 base
let record_reloc b offset_from_section_beginning kind = b . relocations <- { Relocation . offset_from_section_beginning ; kind } :: b . relocations
let declare_label b s = let sy = get_symbol b s in assert ( sy . sy_pos = None ) ; let pos = Buffer . length b . buf in sy . sy_pos <- Some pos
let buf_opcodes b opcodes = List . iter ~ f ( : fun opcode -> buf_int8 b opcode ) opcodes
let arch64 = Config . architecture = " amd64 "
let emit_rex b rexcode = if arch64 && rexcode <> 0 then buf_int8 b ( rexcode lor rex )
let buf_int32_imm b = function | Imm n -> assert ( is_imm32L n ) ; buf_int32L b n | Sym symbol -> record_reloc b ( Buffer . length b . buf ) ( Relocation . Kind . DIR32 ( symbol , 0L ) ) ; buf_int32L b 0L | _ -> assert false
type offset_exp = OImm8 of int64 | OImm32 of string option * int64
let sym32 b sym = record_reloc b ( Buffer . length b . buf ) ( Relocation . Kind . DIR32 ( sym , 0L ) ) ; buf_int32L b 0L
let sym64 b sym = record_reloc b ( Buffer . length b . buf ) ( Relocation . Kind . DIR64 ( sym , 0L ) ) ; buf_int64L b 0L
let buf_sym b sym offset = match sym with | None -> buf_int32L b offset | Some lbl -> record_reloc b ( Buffer . length b . buf ) ( Relocation . Kind . DIR32 ( lbl , offset ) ) ; buf_int32L b 0L
let emit_mod_rm_reg b rex opcodes rm reg = match rm with | Reg32 rm -> let rm = rd_of_reg64 rm in emit_rex b ( rex lor rexr_reg reg lor rexb_rm rm ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b11 rm reg ) | Reg64 rm -> let rm = rd_of_reg64 rm in emit_rex b ( rex lor rexr_reg reg lor rexb_rm rm ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b11 rm reg ) | ( Reg8L _ | Reg8H _ ) as reg8 -> let rm = rd_of_reg8 reg8 in emit_rex b ( rex lor rex_of_reg8 reg8 lor rexr_reg reg lor rexb_rm rm ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b11 rm reg ) | Reg16 reg16 -> let rm = rd_of_reg64 reg16 in emit_rex b ( rex lor rex_of_reg16 reg16 lor rexr_reg reg lor rexb_rm rm ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b11 rm reg ) | Regf rm -> let rm = rd_of_regf rm in emit_rex b ( rex lor rexr_reg reg lor rexb_rm rm ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b11 rm reg ) | Mem64_RIP ( _ , symbol , offset ) -> emit_rex b ( rex lor rexr_reg reg ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b00 0b101 reg ) ; record_reloc b ( Buffer . length b . buf ) ( Relocation . Kind . REL32 ( symbol , Int64 . of_int offset ) ) ; buf_int32L b 0L | Mem { arch ; typ = _ ; idx ; scale ; base ; sym ; displ } -> ( let offset = let displ = Int64 . of_int displ in match sym with | None -> if is_imm8L displ then OImm8 displ else if is_imm32L displ then OImm32 ( None , displ ) else assert false | Some s -> OImm32 ( Some s , displ ) in let idx_reg = idx in let idx = rd_of_reg64 idx in if scale = 0 then ( assert ( base = None && arch = X86 ) ; match offset with | OImm8 _ -> assert false | OImm32 ( sym , offset ) -> buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b00 0b101 reg ) ; buf_sym b sym offset ) else match base with | None -> ( match ( idx_reg , scale , offset ) with | ( RSP | R12 ) , 1 , OImm8 0L -> emit_rex b ( rex lor rexr_reg reg lor rexb_base idx ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b00 idx reg ) ; buf_int8 b ( sib 1 0b100 idx ) | ( RSP | R12 ) , 1 , OImm8 offset8 -> emit_rex b ( rex lor rexr_reg reg lor rexb_base idx ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b01 0b100 reg ) ; buf_int8 b ( sib 1 0b100 idx ) ; buf_int8L b offset8 | ( RSP | R12 ) , 1 , OImm32 ( sym , offset ) -> emit_rex b ( rex lor rexr_reg reg lor rexb_base idx ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b10 0b100 reg ) ; buf_int8 b ( sib 1 0b100 idx ) ; buf_sym b sym offset | ( RBP | R13 ) , 1 , OImm8 _ -> ( emit_rex b ( rex lor rexr_reg reg lor rexb_base idx ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b01 idx reg ) ; match offset with | OImm8 offset8 -> buf_int8L b offset8 | _ -> assert false ) | _ , 1 , OImm8 0L -> emit_rex b ( rex lor rexr_reg reg lor rexb_rm idx ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b00 idx reg ) | _ , 1 , OImm8 offset8 -> emit_rex b ( rex lor rexr_reg reg lor rexb_rm idx ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b01 idx reg ) ; buf_int8L b offset8 | _ , 1 , OImm32 ( sym , offset ) -> emit_rex b ( rex lor rexr_reg reg lor rexb_rm idx ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b10 idx reg ) ; buf_sym b sym offset | _ , _ , _ -> ( emit_rex b ( rex lor rexr_reg reg lor rexx_index idx ) ; buf_opcodes b opcodes ; buf_int8 b ( mod_rm_reg 0b00 0b100 reg ) ; buf_int8 b ( sib scale idx 0b101 ) ; match offset with | OImm8 offset8 -> buf_int32L b offset8 | OImm32 ( sym , offset ) -> buf_sym b sym offset ) ) | Some base_reg -> ( assert ( scale = 1 || scale = 2 || scale = 4 || scale = 8 ) ; let base = rd_of_reg64 base_reg in emit_rex b ( rex lor rexr_reg reg lor rexx_index idx lor rexb_base base ) ; buf_opcodes b opcodes ; match ( base_reg , offset ) with | ( RBP | R13 ) , OImm8 0L -> buf_int8 b ( mod_rm_reg 0b01 0b100 reg ) ; buf_int8 b ( sib scale idx base ) ; buf_int8 b 0 | _ , OImm8 0L -> buf_int8 b ( mod_rm_reg 0b00 0b100 reg ) ; buf_int8 b ( sib scale idx base ) | _ , OImm8 offset -> buf_int8 b ( mod_rm_reg 0b01 0b100 reg ) ; buf_int8 b ( sib scale idx base ) ; buf_int8L b offset | _ , OImm32 ( sym , offset ) -> buf_int8 b ( mod_rm_reg 0b10 0b100 reg ) ; buf_int8 b ( sib scale idx base ) ; buf_sym b sym offset ) ) | Imm _ | Sym _ -> assert false
let emit_movlpd b dst src = match ( dst , src ) with | Regf reg , ( ( Mem _ | Mem64_RIP _ ) as rm ) -> buf_int8 b 0x66 ; emit_mod_rm_reg b 0 [ 0x0f ; 0x12 ] rm ( rd_of_regf reg ) | ( ( Mem _ | Mem64_RIP _ ) as rm ) , Regf reg -> buf_int8 b 0x66 ; emit_mod_rm_reg b 0 [ 0x0f ; 0x13 ] rm ( rd_of_regf reg ) | _ -> assert false
let emit_movapd b dst src = match ( dst , src ) with | Regf reg , ( ( Regf _ | Mem _ | Mem64_RIP _ ) as rm ) -> buf_int8 b 0x66 ; emit_mod_rm_reg b 0 [ 0x0f ; 0x28 ] rm ( rd_of_regf reg ) | ( ( Mem _ | Mem64_RIP _ ) as rm ) , Regf reg -> buf_int8 b 0x66 ; emit_mod_rm_reg b 0 [ 0x0f ; 0x29 ] rm ( rd_of_regf reg ) | _ -> assert false