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 | CVTSI...
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 | W...
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 ...
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 le...
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 . ...
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 . l...
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 ( In...
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_r...
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...
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_m...