text
stringlengths
0
601k
let leaq = bicmd " leaq "
let sete = slcmd " sete "
let setne = slcmd " setne "
let setl = slcmd " setl "
let setle = slcmd " setle "
let setg = slcmd " setg "
let setge = slcmd " setge "
let cmpl = bicmd " cmpl "
let addl = bicmd " addl "
let addq = bicmd " addq "
let subl = bicmd " subl "
let subq = bicmd " subq "
let imul = bicmd " imul "
let imulq = bicmd " imulq "
let mulq = bicmd " mulq "
let idivl = slcmd " idivl "
let idivq = slcmd " idivq "
let divq = slcmd " divq "
let ands = bicmd " and "
let andb = bicmd " andb "
let ors = bicmd " or "
let orl = bicmd " orl "
let xor = bicmd " xor "
let sall = bicmd " sall "
let sarl = bicmd " sarl "
let neg = slcmd " neg "
let nnot = slcmd " nnot "
let jz = slcmd " jz "
let je = slcmd " je "
let jmp = slcmd " jmp "
let call = slcmd " call "
let nop = nacmd " nop "
let enter = bicmd " enter "
let leave = nacmd " leave "
let ret = nacmd " ret "
let retq = nacmd " retq "
type condition = X86_ast . condition = | L | GE | LE | G | B | AE | BE | A | E | NE | O | NO | S | NS | P | NP
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 )