text
stringlengths
12
786k
let i2_s b s x y = bprintf b " \ t % s % s \ t % a , % a " s ( suf y ) arg x arg y
let i2_ss b s x y = bprintf b " \ t % s % s % s \ t % a , % a " s ( suf x ) ( suf y ) arg x arg y
let i1_call_jmp b s = function | Mem { arch = X86 ; idx = _ ; scale = 0 ; base = None ; sym = Some _ ; _ } as x -> i1 b s x | Reg32 _ | Reg64 _ | Mem _ | Mem64_RIP _ as x -> bprintf b " \ t % s \ t *% a " s arg x | Sym x -> bprintf b " \ t % s \ t % s " s x | _ -> assert false
let print_instr b = function | ADD ( arg1 , arg2 ) -> i2_s b " add " arg1 arg2 | ADDSD ( arg1 , arg2 ) -> i2 b " addsd " arg1 arg2 | AND ( arg1 , arg2 ) -> i2_s b " and " arg1 arg2 | ANDPD ( arg1 , arg2 ) -> i2 b " andpd " arg1 arg2 | BSWAP arg -> i1 b " bswap " arg | CALL arg -> i1_call_jmp b " call " arg | CDQ -> i0 b " cltd " | CMOV ( c , arg1 , arg2 ) -> i2 b ( " cmov " ^ string_of_condition c ) arg1 arg2 | CMP ( arg1 , arg2 ) -> i2_s b " cmp " arg1 arg2 | COMISD ( arg1 , arg2 ) -> i2 b " comisd " arg1 arg2 | CQO -> i0 b " cqto " | CVTSD2SI ( arg1 , arg2 ) -> i2 b " cvtsd2si " arg1 arg2 | CVTSD2SS ( arg1 , arg2 ) -> i2 b " cvtsd2ss " arg1 arg2 | CVTSI2SD ( arg1 , arg2 ) -> i2 b ( " cvtsi2sd " ^ suf arg1 ) arg1 arg2 | CVTSS2SD ( arg1 , arg2 ) -> i2 b " cvtss2sd " arg1 arg2 | CVTTSD2SI ( arg1 , arg2 ) -> i2_s b " cvttsd2si " arg1 arg2 | DEC arg -> i1_s b " dec " arg | DIVSD ( arg1 , arg2 ) -> i2 b " divsd " arg1 arg2 | FABS -> i0 b " fabs " | FADD arg -> i1_s b " fadd " arg | FADDP ( arg1 , arg2 ) -> i2 b " faddp " arg1 arg2 | FCHS -> i0 b " fchs " | FCOMP arg -> i1_s b " fcomp " arg | FCOMPP -> i0 b " fcompp " | FCOS -> i0 b " fcos " | FDIV arg -> i1_s b " fdiv " arg | FDIVP ( Regf ( ST 0 ) , arg2 ) -> i2 b " fdivrp " ( Regf ( ST 0 ) ) arg2 | FDIVP ( arg1 , arg2 ) -> i2 b " fdivp " arg1 arg2 | FDIVR arg -> i1_s b " fdivr " arg | FDIVRP ( Regf ( ST 0 ) , arg2 ) -> i2 b " fdivp " ( Regf ( ST 0 ) ) arg2 | FDIVRP ( arg1 , arg2 ) -> i2 b " fdivrp " arg1 arg2 | FILD arg -> i1_s b " fild " arg | FISTP arg -> i1_s b " fistp " arg | FLD ( Mem { typ = REAL4 ; _ } as arg ) -> i1 b " flds " arg | FLD arg -> i1 b " fldl " arg | FLD1 -> i0 b " fld1 " | FLDCW arg -> i1 b " fldcw " arg | FLDLG2 -> i0 b " fldlg2 " | FLDLN2 -> i0 b " fldln2 " | FLDZ -> i0 b " fldz " | FMUL arg -> i1_s b " fmul " arg | FMULP ( arg1 , arg2 ) -> i2 b " fmulp " arg1 arg2 | FNSTCW arg -> i1 b " fnstcw " arg | FNSTSW arg -> i1 b " fnstsw " arg | FPATAN -> i0 b " fpatan " | FPTAN -> i0 b " fptan " | FSIN -> i0 b " fsin " | FSQRT -> i0 b " fsqrt " | FSTP ( Mem { typ = REAL4 ; _ } as arg ) -> i1 b " fstps " arg | FSTP arg -> i1 b " fstpl " arg | FSUB arg -> i1_s b " fsub " arg | FSUBP ( Regf ( ST 0 ) , arg2 ) -> i2 b " fsubrp " ( Regf ( ST 0 ) ) arg2 | FSUBP ( arg1 , arg2 ) -> i2 b " fsubp " arg1 arg2 | FSUBR arg -> i1_s b " fsubr " arg | FSUBRP ( Regf ( ST 0 ) , arg2 ) -> i2 b " fsubp " ( Regf ( ST 0 ) ) arg2 | FSUBRP ( arg1 , arg2 ) -> i2 b " fsubrp " arg1 arg2 | FXCH arg -> i1 b " fxch " arg | FYL2X -> i0 b " fyl2x " | HLT -> i0 b " hlt " | IDIV arg -> i1_s b " idiv " arg | IMUL ( arg , None ) -> i1_s b " imul " arg | IMUL ( arg1 , Some arg2 ) -> i2_s b " imul " arg1 arg2 | INC arg -> i1_s b " inc " arg | J ( c , arg ) -> i1_call_jmp b ( " j " ^ string_of_condition c ) arg | JMP arg -> i1_call_jmp b " jmp " arg | LEA ( arg1 , arg2 ) -> i2_s b " lea " arg1 arg2 | LEAVE -> i0 b " leave " | MOV ( ( Imm n as arg1 ) , ( Reg64 _ as arg2 ) ) when not ( n <= 0x7FFF_FFFFL && n >= - 0x8000_0000L ) -> i2 b " movabsq " arg1 arg2 | MOV ( ( Sym _ as arg1 ) , ( Reg64 _ as arg2 ) ) when windows -> i2 b " movabsq " arg1 arg2 | MOV ( arg1 , arg2 ) -> i2_s b " mov " arg1 arg2 | MOVAPD ( arg1 , arg2 ) -> i2 b " movapd " arg1 arg2 | MOVLPD ( arg1 , arg2 ) -> i2 b " movlpd " arg1 arg2 | MOVSD ( arg1 , arg2 ) -> i2 b " movsd " arg1 arg2 | MOVSS ( arg1 , arg2 ) -> i2 b " movss " arg1 arg2 | MOVSX ( arg1 , arg2 ) -> i2_ss b " movs " arg1 arg2 | MOVSXD ( arg1 , arg2 ) -> i2 b " movslq " arg1 arg2 | MOVZX ( arg1 , arg2 ) -> i2_ss b " movz " arg1 arg2 | MULSD ( arg1 , arg2 ) -> i2 b " mulsd " arg1 arg2 | NEG arg -> i1 b " neg " arg | NOP -> i0 b " nop " | OR ( arg1 , arg2 ) -> i2_s b " or " arg1 arg2 | POP arg -> i1_s b " pop " arg | PUSH arg -> i1_s b " push " arg | RET -> i0 b " ret " | ROUNDSD ( r , arg1 , arg2 ) -> i2 b ( string_of_rounding r ) arg1 arg2 | SAL ( arg1 , arg2 ) -> i2_s b " sal " arg1 arg2 | SAR ( arg1 , arg2 ) -> i2_s b " sar " arg1 arg2 | SET ( c , arg ) -> i1 b ( " set " ^ string_of_condition c ) arg | SHR ( arg1 , arg2 ) -> i2_s b " shr " arg1 arg2 | SQRTSD ( arg1 , arg2 ) -> i2 b " sqrtsd " arg1 arg2 | SUB ( arg1 , arg2 ) -> i2_s b " sub " arg1 arg2 | SUBSD ( arg1 , arg2 ) -> i2 b " subsd " arg1 arg2 | TEST ( arg1 , arg2 ) -> i2_s b " test " arg1 arg2 | UCOMISD ( arg1 , arg2 ) -> i2 b " ucomisd " arg1 arg2 | XCHG ( arg1 , arg2 ) -> i2 b " xchg " arg1 arg2 | XOR ( arg1 , arg2 ) -> i2_s b " xor " arg1 arg2 | XORPD ( arg1 , arg2 ) -> i2 b " xorpd " arg1 arg2
let print_line b = function | Ins instr -> print_instr b instr | Align ( _data , n ) -> let n = if system = S_macosx then Misc . log2 n else n in bprintf b " \ t . align \ t % d " n | Byte n -> bprintf b " \ t . byte \ t % a " cst n | Bytes s -> if system = S_solaris then buf_bytes_directive b " . byte " s else bprintf b " \ t . ascii \ t " \% s " " \ ( string_of_string_literal s ) | Comment s -> bprintf b " \ t \ t \ t \ t /* % s " */ s | Global s -> bprintf b " \ t . globl \ t % s " s ; | Long n -> bprintf b " \ t . long \ t % a " cst n | NewLabel ( s , _ ) -> bprintf b " % s " : s | Quad n -> bprintf b " \ t . quad \ t % a " cst n | Section ( [ " . data " ] , _ , _ ) -> bprintf b " \ t . data " | Section ( [ " . text " ] , _ , _ ) -> bprintf b " \ t . text " | Section ( name , flags , args ) -> bprintf b " \ t . section % s " ( String . concat " , " name ) ; begin match flags with | None -> ( ) | Some flags -> bprintf b " , % S " flags end ; begin match args with | [ ] -> ( ) | _ -> bprintf b " , % s " ( String . concat " , " args ) end | Space n -> if system = S_solaris then bprintf b " \ t . zero \ t % d " n else bprintf b " \ t . space \ t % d " n | Word n -> if system = S_solaris then bprintf b " \ t . value \ t % a " cst n else bprintf b " \ t . word \ t % a " cst n | Cfi_adjust_cfa_offset n -> bprintf b " \ t . cfi_adjust_cfa_offset % d " n | Cfi_endproc -> bprintf b " \ t . cfi_endproc " | Cfi_startproc -> bprintf b " \ t . cfi_startproc " | File ( file_num , file_name ) -> bprintf b " \ t . file \ t % d \ t " \% s " " \ file_num ( X86_proc . string_of_string_literal file_name ) | Indirect_symbol s -> bprintf b " \ t . indirect_symbol % s " s | Loc ( file_num , line , col ) -> if col >= 0 then bprintf b " \ t . loc \ t % d \ t % d \ t % d " file_num line col else bprintf b " \ t . loc \ t % d \ t % d " file_num line | Private_extern s -> bprintf b " \ t . private_extern % s " s | Set ( arg1 , arg2 ) -> bprintf b " \ t . set % s , % a " arg1 cst arg2 | Size ( s , c ) -> bprintf b " \ t . size % s , % a " s cst c | Type ( s , typ ) -> bprintf b " \ t . type % s , % s " s typ | External _ | Mode386 | Model _ -> assert false
let generate_asm oc lines = let b = Buffer . create 10000 in output_string oc " \ t . file " " \\\ n " ; List . iter ( fun i -> Buffer . clear b ; print_line b i ; Buffer . add_char b ' \ n ' ; Buffer . output_buffer oc b ; ) lines
let string_of_datatype = function | QWORD -> " QWORD " | OWORD -> " OWORD " | NONE -> assert false | REAL4 -> " REAL4 " | REAL8 -> " REAL8 " | BYTE -> " BYTE " | WORD -> " WORD " | DWORD -> " DWORD " | NEAR -> " NEAR " | PROC -> " PROC "
let string_of_datatype_ptr = function | QWORD -> " QWORD PTR " | OWORD -> " OWORD PTR " | NONE -> " " | REAL4 -> " REAL4 PTR " | REAL8 -> " REAL8 PTR " | BYTE -> " BYTE PTR " | WORD -> " WORD PTR " | DWORD -> " DWORD PTR " | NEAR -> " NEAR PTR " | PROC -> " PROC PTR "
let arg_mem b { arch ; typ ; idx ; scale ; base ; sym ; displ } = let string_of_register = match arch with | X86 -> string_of_reg32 | X64 -> string_of_reg64 in Buffer . add_string b ( string_of_datatype_ptr typ ) ; Buffer . add_char b ' [ ' ; begin match sym with | None -> ( ) | Some s -> Buffer . add_string b s end ; if scale <> 0 then begin if sym <> None then Buffer . add_char b ' ' ; + Buffer . add_string b ( string_of_register idx ) ; if scale <> 1 then bprintf b " *% d " scale ; end ; begin match base with | None -> ( ) | Some r -> assert ( scale > 0 ) ; Buffer . add_char b ' ' ; + Buffer . add_string b ( string_of_register r ) ; end ; begin if displ > 0 then bprintf b " +% d " displ else if displ < 0 then bprintf b " % d " displ end ; Buffer . add_char b ' ] '
let arg b = function | Sym s -> bprintf b " OFFSET % s " s | Imm n when n <= 0x7FFF_FFFFL && n >= - 0x8000_0000L -> bprintf b " % Ld " n | Imm int -> bprintf b " 0 % LxH " int | Reg8L x -> Buffer . add_string b ( string_of_reg8l x ) | Reg8H x -> Buffer . add_string b ( string_of_reg8h x ) | Reg16 x -> Buffer . add_string b ( string_of_reg16 x ) | Reg32 x -> Buffer . add_string b ( string_of_reg32 x ) | Reg64 x -> Buffer . add_string b ( string_of_reg64 x ) | Regf x -> Buffer . add_string b ( string_of_registerf x ) | Mem64_RIP ( typ , s , displ ) -> bprintf b " % s % s " ( string_of_datatype_ptr typ ) s ; if displ > 0 then bprintf b " +% d " displ else if displ < 0 then bprintf b " % d " displ | Mem addr -> arg_mem b addr
let rec cst b = function | ConstLabel _ | Const _ | ConstThis as c -> scst b c | ConstAdd ( c1 , c2 ) -> bprintf b " % a + % a " scst c1 scst c2 | ConstSub ( c1 , c2 ) -> bprintf b " % a - % a " scst c1 scst c2 | ConstThis -> Buffer . add_string b " THIS BYTE " | ConstLabel l -> Buffer . add_string b l | Const n when n <= 0x7FFF_FFFFL && n >= - 0x8000_0000L -> Buffer . add_string b ( Int64 . to_string n ) | Const n -> bprintf b " 0 % LxH " n | ConstAdd ( c1 , c2 ) -> bprintf b " ( % a + % a ) " scst c1 scst c2 | ConstSub ( c1 , c2 ) -> bprintf b " ( % a - % a ) " scst c1 scst c2
let i0 b s = bprintf b " \ t % s " s
let i1 b s x = bprintf b " \ t % s \ t % a " s arg x
let i2 b s x y = bprintf b " \ t % s \ t % a , % a " s arg y arg x
let i1_call_jmp b s = function | Sym x -> bprintf b " \ t % s \ t % s " s x | x -> i1 b s x
let print_instr b = function | ADD ( arg1 , arg2 ) -> i2 b " add " arg1 arg2 | ADDSD ( arg1 , arg2 ) -> i2 b " addsd " arg1 arg2 | AND ( arg1 , arg2 ) -> i2 b " and " arg1 arg2 | ANDPD ( arg1 , arg2 ) -> i2 b " andpd " arg1 arg2 | BSWAP arg -> i1 b " bswap " arg | CALL arg -> i1_call_jmp b " call " arg | CDQ -> i0 b " cdq " | CMOV ( c , arg1 , arg2 ) -> i2 b ( " cmov " ^ string_of_condition c ) arg1 arg2 | CMP ( arg1 , arg2 ) -> i2 b " cmp " arg1 arg2 | COMISD ( arg1 , arg2 ) -> i2 b " comisd " arg1 arg2 | CQO -> i0 b " cqo " | CVTSD2SI ( arg1 , arg2 ) -> i2 b " cvtsd2si " arg1 arg2 | CVTSD2SS ( arg1 , arg2 ) -> i2 b " cvtsd2ss " arg1 arg2 | CVTSI2SD ( arg1 , arg2 ) -> i2 b " cvtsi2sd " arg1 arg2 | CVTSS2SD ( arg1 , arg2 ) -> i2 b " cvtss2sd " arg1 arg2 | CVTTSD2SI ( arg1 , arg2 ) -> i2 b " cvttsd2si " arg1 arg2 | DEC arg -> i1 b " dec " arg | DIVSD ( arg1 , arg2 ) -> i2 b " divsd " arg1 arg2 | FABS -> i0 b " fabs " | FADD arg -> i1 b " fadd " arg | FADDP ( arg1 , arg2 ) -> i2 b " faddp " arg1 arg2 | FCHS -> i0 b " fchs " | FCOMP arg -> i1 b " fcomp " arg | FCOMPP -> i0 b " fcompp " | FCOS -> i0 b " fcos " | FDIV arg -> i1 b " fdiv " arg | FDIVP ( arg1 , arg2 ) -> i2 b " fdivp " arg1 arg2 | FDIVR arg -> i1 b " fdivr " arg | FDIVRP ( arg1 , arg2 ) -> i2 b " fdivrp " arg1 arg2 | FILD arg -> i1 b " fild " arg | FISTP arg -> i1 b " fistp " arg | FLD arg -> i1 b " fld " arg | FLD1 -> i0 b " fld1 " | FLDCW arg -> i1 b " fldcw " arg | FLDLG2 -> i0 b " fldlg2 " | FLDLN2 -> i0 b " fldln2 " | FLDZ -> i0 b " fldz " | FMUL arg -> i1 b " fmul " arg | FMULP ( arg1 , arg2 ) -> i2 b " fmulp " arg1 arg2 | FNSTCW arg -> i1 b " fnstcw " arg | FNSTSW arg -> i1 b " fnstsw " arg | FPATAN -> i0 b " fpatan " | FPTAN -> i0 b " fptan " | FSIN -> i0 b " fsin " | FSQRT -> i0 b " fsqrt " | FSTP arg -> i1 b " fstp " arg | FSUB arg -> i1 b " fsub " arg | FSUBP ( arg1 , arg2 ) -> i2 b " fsubp " arg1 arg2 | FSUBR arg -> i1 b " fsubr " arg | FSUBRP ( arg1 , arg2 ) -> i2 b " fsubrp " arg1 arg2 | FXCH arg -> i1 b " fxch " arg | FYL2X -> i0 b " fyl2x " | HLT -> assert false | IDIV arg -> i1 b " idiv " arg | IMUL ( arg , None ) -> i1 b " imul " arg | IMUL ( arg1 , Some arg2 ) -> i2 b " imul " arg1 arg2 | INC arg -> i1 b " inc " arg | J ( c , arg ) -> i1_call_jmp b ( " j " ^ string_of_condition c ) arg | JMP arg -> i1_call_jmp b " jmp " arg | LEA ( arg1 , arg2 ) -> i2 b " lea " arg1 arg2 | LEAVE -> i0 b " leave " | MOV ( Imm n as arg1 , Reg64 r ) when n >= 0x8000_0000L && n <= 0xFFFF_FFFFL -> i2 b " mov " arg1 ( Reg32 r ) | MOV ( arg1 , arg2 ) -> i2 b " mov " arg1 arg2 | MOVAPD ( arg1 , arg2 ) -> i2 b " movapd " arg1 arg2 | MOVLPD ( arg1 , arg2 ) -> i2 b " movlpd " arg1 arg2 | MOVSD ( arg1 , arg2 ) -> i2 b " movsd " arg1 arg2 | MOVSS ( arg1 , arg2 ) -> i2 b " movss " arg1 arg2 | MOVSX ( arg1 , arg2 ) -> i2 b " movsx " arg1 arg2 | MOVSXD ( arg1 , arg2 ) -> i2 b " movsxd " arg1 arg2 | MOVZX ( arg1 , arg2 ) -> i2 b " movzx " arg1 arg2 | MULSD ( arg1 , arg2 ) -> i2 b " mulsd " arg1 arg2 | NEG arg -> i1 b " neg " arg | NOP -> i0 b " nop " | OR ( arg1 , arg2 ) -> i2 b " or " arg1 arg2 | POP arg -> i1 b " pop " arg | PUSH arg -> i1 b " push " arg | RET -> i0 b " ret " | ROUNDSD ( r , arg1 , arg2 ) -> i2 b ( string_of_rounding r ) arg1 arg2 | SAL ( arg1 , arg2 ) -> i2 b " sal " arg1 arg2 | SAR ( arg1 , arg2 ) -> i2 b " sar " arg1 arg2 | SET ( c , arg ) -> i1 b ( " set " ^ string_of_condition c ) arg | SHR ( arg1 , arg2 ) -> i2 b " shr " arg1 arg2 | SQRTSD ( arg1 , arg2 ) -> i2 b " sqrtsd " arg1 arg2 | SUB ( arg1 , arg2 ) -> i2 b " sub " arg1 arg2 | SUBSD ( arg1 , arg2 ) -> i2 b " subsd " arg1 arg2 | TEST ( arg1 , arg2 ) -> i2 b " test " arg1 arg2 | UCOMISD ( arg1 , arg2 ) -> i2 b " ucomisd " arg1 arg2 | XCHG ( arg1 , arg2 ) -> i2 b " xchg " arg1 arg2 | XOR ( arg1 , arg2 ) -> i2 b " xor " arg1 arg2 | XORPD ( arg1 , arg2 ) -> i2 b " xorpd " arg1 arg2
let print_line b = function | Ins instr -> print_instr b instr | Align ( _data , n ) -> bprintf b " \ tALIGN \ t % d " n | Byte n -> bprintf b " \ tBYTE \ t % a " cst n | Bytes s -> buf_bytes_directive b " BYTE " s | Comment s -> bprintf b " ; % s " s | Global s -> bprintf b " \ tPUBLIC \ t % s " s | Long n -> bprintf b " \ tDWORD \ t % a " cst n | NewLabel ( s , NONE ) -> bprintf b " % s " : s | NewLabel ( s , ptr ) -> bprintf b " % s LABEL % s " s ( string_of_datatype ptr ) | Quad n -> bprintf b " \ tQWORD \ t % a " cst n | Section ( [ " . data " ] , None , [ ] ) -> bprintf b " \ t . DATA " | Section ( [ " . text " ] , None , [ ] ) -> bprintf b " \ t . CODE " | Section _ -> assert false | Space n -> bprintf b " \ tBYTE \ t % d DUP ( ) " ? n | Word n -> bprintf b " \ tWORD \ t % a " cst n | External ( s , ptr ) -> bprintf b " \ tEXTRN \ t % s : % s " s ( string_of_datatype ptr ) | Mode386 -> bprintf b " \ t . 386 " | Model name -> bprintf b " \ t . MODEL % s " name | Cfi_adjust_cfa_offset _ | Cfi_endproc | Cfi_startproc | File _ | Indirect_symbol _ | Loc _ | Private_extern _ | Set _ | Size _ | Type _ -> assert false
let generate_asm oc lines = let b = Buffer . create 10000 in List . iter ( fun i -> Buffer . clear b ; print_line b i ; Buffer . add_char b ' \ n ' ; Buffer . output_buffer oc b ) lines ; output_string oc " \ tEND \ n "
type system = | S_macosx | S_gnu | S_cygwin | S_solaris | S_win32 | S_linux_elf | S_bsd_elf | S_beos | S_mingw | S_win64 | S_linux | S_mingw64 | S_unknown
let system = match Config . system with | " macosx " -> S_macosx | " solaris " -> S_solaris | " win32 " -> S_win32 | " linux_elf " -> S_linux_elf | " bsd_elf " -> S_bsd_elf | " beos " -> S_beos | " gnu " -> S_gnu | " cygwin " -> S_cygwin | " mingw " -> S_mingw | " mingw64 " -> S_mingw64 | " win64 " -> S_win64 | " linux " -> S_linux | _ -> S_unknown
let windows = match system with | S_mingw64 | S_cygwin | S_win64 -> true | _ -> false
let string_of_string_literal s = let b = Buffer . create ( String . length s + 2 ) in let last_was_escape = ref false in for i = 0 to String . length s - 1 do let c = s . [ i ] in if c >= ' 0 ' && c <= ' 9 ' then if ! last_was_escape then Printf . bprintf b " \\% o " ( Char . code c ) else Buffer . add_char b c else if c >= ' ' && c <= ' ' ~ && c <> ' " ' && c <> ' ' \\ then begin Buffer . add_char b c ; last_was_escape := false end else begin Printf . bprintf b " \\% o " ( Char . code c ) ; last_was_escape := true end done ; Buffer . contents b
let string_of_symbol prefix s = let spec = ref false in for i = 0 to String . length s - 1 do match String . unsafe_get s i with | ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' | ' _ ' -> ( ) | _ -> spec := true ; done ; if not ! spec then if prefix = " " then s else prefix ^ s else let b = Buffer . create ( String . length s + 10 ) in Buffer . add_string b prefix ; String . iter ( function | ( ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' | ' _ ' ) as c -> Buffer . add_char b c | c -> Printf . bprintf b " $% 02x " ( Char . code c ) ) s ; Buffer . contents b
let buf_bytes_directive b directive s = let pos = ref 0 in for i = 0 to String . length s - 1 do if ! pos = 0 then begin if i > 0 then Buffer . add_char b ' \ n ' ; Buffer . add_char b ' \ t ' ; Buffer . add_string b directive ; Buffer . add_char b ' \ t ' ; end else Buffer . add_char b ' , ' ; Printf . bprintf b " % d " ( Char . code s . [ i ] ) ; incr pos ; if ! pos >= 16 then begin pos := 0 end done
let string_of_reg64 = function | RAX -> " rax " | RBX -> " rbx " | RDI -> " rdi " | RSI -> " rsi " | RDX -> " rdx " | RCX -> " rcx " | RBP -> " rbp " | RSP -> " rsp " | R8 -> " r8 " | R9 -> " r9 " | R10 -> " r10 " | R11 -> " r11 " | R12 -> " r12 " | R13 -> " r13 " | R14 -> " r14 " | R15 -> " r15 "
let string_of_reg8l = function | RAX -> " al " | RBX -> " bl " | RCX -> " cl " | RDX -> " dl " | RSP -> " spl " | RBP -> " bpl " | RSI -> " sil " | RDI -> " dil " | R8 -> " r8b " | R9 -> " r9b " | R10 -> " r10b " | R11 -> " r11b " | R12 -> " r12b " | R13 -> " r13b " | R14 -> " r14b " | R15 -> " r15b "
let string_of_reg8h = function | AH -> " ah " | BH -> " bh " | CH -> " ch " | DH -> " dh "
let string_of_reg16 = function | RAX -> " ax " | RBX -> " bx " | RCX -> " cx " | RDX -> " dx " | RSP -> " sp " | RBP -> " bp " | RSI -> " si " | RDI -> " di " | R8 -> " r8w " | R9 -> " r9w " | R10 -> " r10w " | R11 -> " r11w " | R12 -> " r12w " | R13 -> " r13w " | R14 -> " r14w " | R15 -> " r15w "
let string_of_reg32 = function | RAX -> " eax " | RBX -> " ebx " | RCX -> " ecx " | RDX -> " edx " | RSP -> " esp " | RBP -> " ebp " | RSI -> " esi " | RDI -> " edi " | R8 -> " r8d " | R9 -> " r9d " | R10 -> " r10d " | R11 -> " r11d " | R12 -> " r12d " | R13 -> " r13d " | R14 -> " r14d " | R15 -> " r15d "
let string_of_registerf = function | XMM n -> Printf . sprintf " xmm % d " n | TOS -> Printf . sprintf " tos " | ST n -> Printf . sprintf " st ( % d ) " n
let string_of_condition = function | E -> " e " | AE -> " ae " | A -> " a " | GE -> " ge " | G -> " g " | NE -> " ne " | B -> " b " | BE -> " be " | L -> " l " | LE -> " le " | NP -> " np " | P -> " p " | NS -> " ns " | S -> " s " | NO -> " no " | O -> " o "
let string_of_rounding = function | RoundDown -> " roundsd . down " | RoundUp -> " roundsd . up " | RoundTruncate -> " roundsd . trunc " | RoundNearest -> " roundsd . near "
let internal_assembler = ref None
let register_internal_assembler f = internal_assembler := Some f
let masm = match system with | S_win32 | S_win64 -> true | _ -> false
let use_plt = match system with | S_macosx | S_mingw64 | S_cygwin | S_win64 -> false | _ -> ! Clflags . dlcode
let binary_content = ref None
let compile infile outfile = if masm then Ccomp . command ( Config . asm ^ Filename . quote outfile ^ " " ^ Filename . quote infile ^ ( if ! Clflags . verbose then " " else " > NUL " ) ) else Ccomp . command ( Config . asm ^ " " ^ ( String . concat " " ( Misc . debug_prefix_map_flags ( ) ) ) ^ " - o " ^ Filename . quote outfile ^ " " ^ Filename . quote infile )
let assemble_file infile outfile = match ! binary_content with | None -> compile infile outfile | Some content -> content outfile ; binary_content := None ; 0
let asm_code = ref [ ]
let directive dir = asm_code := dir :: ! asm_code
let emit ins = directive ( Ins ins )
let reset_asm_code ( ) = asm_code := [ ]
let generate_code asm = let instrs = List . rev ! asm_code in begin match asm with | Some f -> f instrs | None -> ( ) end ; begin match ! internal_assembler with | Some f -> binary_content := Some ( f instrs ) | None -> binary_content := None end
let name s_l s_opt s_l ' = let first = String . concat ~ sep " , " : s_l in let mid = match s_opt with None -> " " | Some s -> Printf . sprintf " , % S " s in let last = match s_l ' with [ ] -> " " | l -> " , " ^ String . concat ~ sep " , " : l in first ^ mid ^ last
type t = { name : string ; instructions : X86_ast . asm_line list }
let assemble ~ arch { name ; instructions } = let section = { X86_emitter . sec_name = name ; sec_instrs = Array . of_list instructions } in X86_emitter . assemble_section arch section
module Map = struct type nonrec t = X86_ast . asm_line list String . Map . t let append key l t = String . Map . update ~ key t ~ f ( : function | None -> Some l | Some l ' -> Some ( l ' @ l ) ) let from_program l = let open X86_ast in let rec aux acc current_section current_instrs l = let add_current ( ) = append current_section ( List . rev current_instrs ) acc in match l with | [ ] -> add_current ( ) | Section ( s_l , s_opt , s_l ' ) :: tl -> let acc = add_current ( ) in let current_section = name s_l s_opt s_l ' in let current_instrs = [ ] in aux acc current_section current_instrs tl | ( _ as instr ) :: tl -> aux acc current_section ( instr :: current_instrs ) tl in match l with | [ ] -> String . Map . empty | Section ( s_l , s_opt , s_l ' ) :: tl -> let current_section = name s_l s_opt s_l ' in let current_instrs = [ ] in aux String . Map . empty current_section current_instrs tl | _line :: _ -> failwithf " Invalid program , should start with section " end
type ' a xarray = { mutable len : int ; mutable buf : ' a array ; default : ' a } ' a
type ' a t = ' a xarray
let expand x len = if Array . length x . buf >= len then ( ) else let buf ' = Array . make ( 2 * len ) len x . default in Array . blit x . buf 0 buf ' 0 x . len ; x . buf <- buf '
let get x i = if 0 <= i && i < x . len then x . buf ( . i ) i else invalid_arg " XArray . get "
let set x i e = if i < x . len then x . buf ( . i ) i <- e else begin expand x ( i + 1 ) 1 ; if x . len < i then Array . fill x . buf x . len ( i - x . len ) len x . default ; x . buf ( . i ) i <- e ; x . len <- i + 1 end
let length x = x . len
let init ? bufsize len def f = let buf = Array . make ( match bufsize with None -> len | Some n -> n ) n def in for i = 0 to len - 1 do buf ( . i ) i <- f i done ; { len = len ; buf = buf ; default = def } def
let nth _ i = i
let first _ = 0
let last x = length x - 1
let out_of_range x i = i < 0 || x . len <= i
let next _ i = i + 1
let prev _ i = i - 1
let move _ i n = i + n
let compare_index _ i j = i - j
let make ? bufsize len default = let buf = Array . make ( match bufsize with None -> len | Some n -> n ) n default in { len = len ; buf = buf ; default = default } default
let clear x = x . len <- 0
let reset x = x . len <- 0 ; x . buf <- Array . make 0 x . default
let copy x = { len = x . len ; buf = Array . copy x . buf ; default = x . default } default
let sub x pos len = { len = len ; buf = Array . sub x . buf pos len ; default = x . default } default
let add_element x e = expand x ( x . len + 1 ) 1 ; x . buf ( . x . len ) len <- e ; x . len <- x . len + 1
let add_array x a = expand x ( x . len + Array . length a ) a ; Array . blit a 0 x . buf x . len ( Array . length a ) a ; x . len <- x . len + Array . length a
let add_xarray x1 x2 = expand x1 ( x1 . len + x2 . len ) len ; Array . blit x2 . buf 0 x1 . buf x1 . len x2 . len ; x1 . len <- x1 . len + x2 . len
let shrink x len = x . len <- min x . len ( max 0 len ) len
let append x1 x2 = let buf = Array . make ( x1 . len + x2 . len ) len x1 . default in Array . blit x1 . buf 0 buf 0 x1 . len ; Array . blit x2 . buf 0 buf x1 . len x2 . len ; { len = x1 . len + x2 . len ; buf = buf ; default = x1 . default } default
let array_of x = Array . sub x . buf 0 x . len
let iter proc x = for i = 0 to x . len - 1 do proc x . buf ( . i ) i done
type ' a expr = ( ' a , Biniou . biniou_repr ) Mapping . mapping
type ' a def = ( ' a , Biniou . biniou_repr ) Mapping . def
type ' a grouped_defs = ( bool * ' a def list ) list
type name = ( loc * string )
type names = { field_names : name list list ; variant_names : name list list ; }
let rec extract_names_from_expr acc ( x : ' a expr ) = match x with Unit _ | Bool _ | Int _ | Float _ | String _ -> acc | Sum ( _ , va , _ , _ ) -> let l , ( fn , vn ) = Array . fold_left extract_names_from_variant ( [ ] , acc ) va in ( fn , List . rev l :: vn ) | Record ( _ , fa , _ , _ ) -> let l , ( fn , vn ) = Array . fold_left extract_names_from_field ( [ ] , acc ) fa in ( List . rev l :: fn , vn ) | Tuple ( _ , ca , _ , _ ) -> Array . fold_left extract_names_from_cell acc ca | List ( _ , x , _ , _ ) | Option ( _ , x , _ , _ ) | Nullable ( _ , x , _ , _ ) | Wrap ( _ , x , _ , _ ) -> extract_names_from_expr acc x | Name ( _ , _ , l , _ , _ ) -> List . fold_left extract_names_from_expr acc l | External ( _ , _ , l , _ , _ ) -> List . fold_left extract_names_from_expr acc l | Tvar _ -> acc let l = ( x . var_loc , x . var_cons ) :: l in match x . var_arg with None -> ( l , acc ) | Some x -> ( l , extract_names_from_expr acc x ) let l = ( x . f_loc , x . f_name ) :: l in ( l , extract_names_from_expr acc x . f_value ) extract_names_from_expr acc x . cel_value
let extract_ocaml_names_from_defs l = let fn , vn = List . fold_left ( fun acc def -> match def . def_value with None -> acc | Some x -> extract_names_from_expr acc x ) ( [ ] , [ ] ) l in { field_names = List . rev fn ; variant_names = List . rev vn ; }
let check_duplicate_hashes kind l = let tbl = Hashtbl . create 100 in List . iter ( fun ( loc , s ) -> let h = Bi_io . hash_name s in try let loc0 , s0 = Hashtbl . find tbl h in Error . error2 loc0 ( sprintf " Definition of % s % s . " kind s0 ) loc ( sprintf " \ kind s s0 s h ) with Not_found -> Hashtbl . add tbl h ( loc , s ) ) l
let check_hashes x = List . iter ( check_duplicate_hashes " record field name " ) x . field_names ; List . iter ( check_duplicate_hashes " variant name " ) x . variant_names
let check ( l : ' a grouped_defs ) = extract_ocaml_names_from_defs ( List . concat_map snd l ) |> check_hashes let tbl = Hashtbl . create ( 2 * List . length l ) in List . iter ( fun s -> let h = Bi_io . hash_name s in let r = try Hashtbl . find tbl h with Not_found -> let r = ref [ ] in Hashtbl . add tbl h r ; r in r := s :: ! r ) l ; let clashes = Hashtbl . fold ( fun h r acc -> let l = ! r in if List . length l >= 2 then List . rev l :: acc else acc ) tbl [ ] in let clashes = List . sort compare clashes in List . iter ( fun l -> print_endline ( String . concat " " l ) ) clashes ) *
type auth = [ | ` Fingerprint of string | ` Trust_anchor of string
type t = { version : int ; jid : Xjid . full_jid ; priority : int option ; hostname : string option ; port : int option ; password : string option ; authenticator : auth ; otr_config : Otr . State . config ; dsa : Mirage_crypto_pk . Dsa . priv ; certificate_hostname : string option ; notification_callback : string option ; log_top : bool ; muc_max_stanzas : int option ; }
let dsa_of_cfg_sexp t = match t with | Sexp . List l -> ( List . fold_left ( fun ( dsa , cfg ) cfg -> function | Sexp . List [ Sexp . Atom " otr_config " ; Sexp . List data ] -> List . fold_left ( fun ( dsa , cfg ) cfg -> function | Sexp . List [ Sexp . Atom " policies " ; _ ] as p -> ( dsa , p :: cfg ) cfg | Sexp . List [ Sexp . Atom " versions " ; _ ] as v -> ( dsa , v :: cfg ) cfg | Sexp . List [ Sexp . Atom " dsa " ; d ] -> ( Some ( Mirage_crypto_pk . Dsa . priv_of_sexp d ) d , cfg ) cfg | _ -> raise ( Invalid_argument " broken sexp while trying to find dsa ) ) " ( dsa , cfg ) cfg data | _ -> ( dsa , cfg ) cfg ) cfg ( None , [ ] ) l ) l | _ -> raise ( Invalid_argument " couldn ' t parse sexp ( dsa ) dsa ) "
let t_of_sexp dsa t = let dsa , otr_cfg = match dsa , dsa_of_cfg_sexp t with | None , ( Some dsa , cfg ) cfg -> dsa , cfg | Some x , ( _ , cfg ) cfg -> x , cfg | _ -> raise ( Invalid_argument " broken config ) " in let otr_config = Otr . State . config_of_sexp ( Sexp . List otr_cfg ) otr_cfg in match t with | Sexp . List l -> ( match List . fold_left ( fun ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas -> function | Sexp . List [ Sexp . Atom " version " ; v ] -> assert ( ver = None ) None ; let version = int_of_sexp v in ( Some version , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " jid " ; Sexp . Atom v ] -> assert ( jid = None ) None ; let jid = match Xjid . string_to_full_jid v with | None -> raise ( Invalid_argument " parse error in jid ) " | Some x -> x in ( ver , Some jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " priority " ; p ] -> assert ( prio = None ) None ; let prio = option_of_sexp int_of_sexp p in ( ver , jid , Some prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " hostname " ; h ] -> assert ( host = None ) None ; let host = option_of_sexp string_of_sexp h in ( ver , jid , prio , Some host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " port " ; p ] -> assert ( port = None ) None ; let port = match ver with | Some x when x < 3 -> let p = int_of_sexp p in if p = 5222 then None else Some p | _ -> option_of_sexp int_of_sexp p in ( ver , jid , prio , host , Some port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " password " ; password ] -> assert ( pass = None ) None ; let pass = match ver with | Some x when x < 3 -> string_of_sexp password | _ -> raise ( Invalid_argument " password stored in V3 + file ) " in ( ver , jid , prio , host , port , Some pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " trust_anchor " ; trust_anchor ] -> assert ( auth = None ) None ; ( match ver with | Some 0 -> let auth = Some ( ` Trust_anchor ( string_of_sexp trust_anchor ) trust_anchor ) trust_anchor in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Some 1 -> ( match option_of_sexp string_of_sexp trust_anchor with | None -> ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Some x -> ( ver , jid , prio , host , port , pass , Some ( ` Trust_anchor x ) x , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas ) | _ -> raise ( Invalid_argument " unexpected element : trust_anchor ) " ) | Sexp . List [ Sexp . Atom " tls_fingerprint " ; tls_fp ] -> assert ( auth = None ) None ; ( match ver with | Some 1 -> ( match option_of_sexp string_of_sexp tls_fp with | None -> ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Some x -> ( ver , jid , prio , host , port , pass , Some ( ` Fingerprint x ) x , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas ) | _ -> raise ( Invalid_argument " unexpected element : tls_fingerprint ) " ) | Sexp . List [ Sexp . Atom " authenticator " ; authenticator ] -> assert ( auth = None ) None ; ( ver , jid , prio , host , port , pass , Some ( auth_of_sexp authenticator ) authenticator , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " otr_config " ; _ ] -> ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " certificate_hostname " ; c ] -> assert ( cert = None ) None ; let cert = option_of_sexp string_of_sexp c in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " notification_callback " ; c ] -> assert ( notification = None ) None ; let notification = option_of_sexp string_of_sexp c in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " log_top " ; c ] -> let log_top = bool_of_sexp c in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " muc_max_stanzas " ; p ] -> assert ( muc_max_stanzas = None ) None ; let mms = option_of_sexp int_of_sexp p in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , mms ) mms | _ -> ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas ) muc_max_stanzas ( None , None , None , None , None , None , None , None , None , false , None ) None l with | Some version , Some jid , Some priority , Some hostname , Some port , password , Some authenticator , certificate_hostname , notification_callback , log_top , muc_max_stanzas -> { version ; jid ; priority ; hostname ; port ; password ; authenticator ; otr_config ; dsa ; certificate_hostname ; notification_callback ; log_top ; muc_max_stanzas } | Some version , Some jid , None , Some hostname , Some port , password , Some authenticator , certificate_hostname , notification_callback , log_top , muc_max_stanzas -> { version ; jid ; priority = None ; hostname ; port ; password ; authenticator ; otr_config ; dsa ; certificate_hostname ; notification_callback ; log_top ; muc_max_stanzas } | Some version , Some jid , Some priority , None , Some port , password , Some authenticator , certificate_hostname , notification_callback , log_top , muc_max_stanzas -> { version ; jid ; priority ; hostname = None ; port ; password ; authenticator ; otr_config ; dsa ; certificate_hostname ; notification_callback ; log_top ; muc_max_stanzas } | Some version , Some jid , None , None , Some port , password , Some authenticator , certificate_hostname , notification_callback , log_top , muc_max_stanzas -> { version ; jid ; priority = None ; hostname = None ; port ; password ; authenticator ; otr_config ; dsa ; certificate_hostname ; notification_callback ; log_top ; muc_max_stanzas } | _ -> raise ( Invalid_argument " broken config ) " ) | _ -> raise ( Invalid_argument " broken config ) "
let record kvs = Sexp . List List ( . map ( fun ( k , v ) v -> ( Sexp . List [ Sexp . Atom k ; v ] v ) v ) v kvs ) kvs
let sexp_of_t t = record [ " version " , sexp_of_int t . version ; " jid " , sexp_of_string ( Xjid . full_jid_to_string t . jid ) jid ; " hostname " , sexp_of_option sexp_of_string t . hostname ; " port " , sexp_of_option sexp_of_int t . port ; " priority " , sexp_of_option sexp_of_int t . priority ; " authenticator " , sexp_of_auth t . authenticator ; " otr_config " , Otr . State . sexp_of_config t . otr_config ; " certificate_hostname " , sexp_of_option sexp_of_string t . certificate_hostname ; " notification_callback " , sexp_of_option sexp_of_string t . notification_callback ; " log_top " , sexp_of_bool t . log_top ; " muc_max_stanzas " , sexp_of_option sexp_of_int t . muc_max_stanzas ; ]
let load_config dsa bytes = t_of_sexp dsa ( Sexp . of_string bytes ) bytes
let store_config t = Bytes . of_string ( Sexp . to_string_hum ( sexp_of_t t ) t ) t
let create kind n = FFT . Array1 . create kind fortran_layout n
let log2 = log 2 .
let minpow2 l = truncate ( 2 . ( ** ceil ( log ( float l ) . / log2 ) ) )
let copy0 ? n ? ofsx ( x : D . vec ) ( ? ofsy = 1 ) ( y : D . vec ) = let n = match n with None -> Array1 . dim x | Some n -> n in for i = 1 to ofsy - 1 do y . { i } <- 0 . done ; ignore ( D . copy ~ n ? ofsx x ~ ofsy ~ y ) ; for i = ofsy + n to Array1 . dim y do y . { i } <- 0 . done
type scale = Biased | Unbiased | Coeff
let xcorr ? maxlag ? scale ( a : D . vec ) ( b : D . vec ) = let dima = Array1 . dim a and dimb = Array1 . dim b in if dima < 1 || dimb < 1 then invalid_arg " xcorr : array size < 1 " ; let m = max dima dimb in let maxlag = match maxlag with Some i -> i | None -> m - 1 in let ilag0 = maxlag + 1 in let dim_xcorr = 2 * maxlag + 1 in let idx0 = max m ( maxlag + 1 ) in let dim_correlation = 2 * idx0 - 1 in let fill0 xcorr = for i = 1 to ilag0 - m do xcorr . { i } <- 0 . done ; for i = ilag0 + m to dim_xcorr do xcorr . { i } <- 0 . done ; xcorr in let xcorr = if a == b then begin let n = minpow2 ( 2 * m - 1 ) in let x = create FFT . float n and y = create FFT . complex ( n / 2 + 1 ) in let fft = FFT . Array1 . r2c x y and ifft = FFT . Array1 . c2r y x in copy0 a x ; FFT . exec fft ; for i = 1 to Array1 . dim y do y . { i } <- { Complex . re = Complex . norm2 y . { i } ; im = 0 . } done ; FFT . exec ifft ; D . scal ( 1 . . / float n ) x ; let xcorr = create FFT . float dim_xcorr in let l = min m ( maxlag + 1 ) in ignore ( D . copy ~ n : l x ~ ofsy : ilag0 ~ y : xcorr ) ; ignore ( D . copy ~ n ( : l - 1 ) ~ ofsx ( : n - l + 2 ) x ~ ofsy ( : ilag0 - l + 1 ) ~ y : xcorr ) ; fill0 xcorr end else begin if dima < 10 * dimb && dimb < 10 * dima then ( let n = minpow2 dim_correlation in let n_complex = n / 2 + 1 in let x = create FFT . float n and a ' = create FFT . complex n_complex and b ' = create FFT . complex n_complex in let ffta = FFT . Array1 . r2c x a ' and fftb = FFT . Array1 . r2c x b ' in let ifft = FFT . Array1 . c2r a ' x in copy0 a ~ ofsy : idx0 x ; FFT . exec ffta ; copy0 b x ; FFT . exec fftb ; for i = 1 to n_complex do a ' . { i } <- Complex . mul a ' . { i } ( Complex . conj b ' . { i } ) ; done ; FFT . exec ifft ; D . scal ( 1 . . / float n ) x ; let xcorr = Array1 . sub x ( max 1 ( idx0 - maxlag ) ) dim_xcorr in fill0 xcorr ) else ( let a , b , permuted = if Array1 . dim a >= Array1 . dim b then a , b , false else b , a , true in let dima = Array1 . dim a and dimb = Array1 . dim b in let l = dimb in let lcorr = l + dimb - 1 in let n = minpow2 lcorr in let n_complex = n / 2 + 1 in let x = create FFT . float n and b ' = create FFT . complex n_complex in let fftb = FFT . Array1 . r2c x b ' in copy0 b x ; FFT . exec fftb ; for i = 1 to n_complex do b ' . { i } <- Complex . conj b ' . { i } ; done ; let c = create FFT . float dim_correlation in Array1 . fill c 0 . ; let inc_seg = if permuted then - 1 else 1 in let idx_seg = if permuted then idx0 - l + 2 else idx0 - dimb in let shift_seg = if permuted then ( fun i -> idx_seg - i ) else ( fun i -> idx_seg + i ) in let y = create FFT . complex n_complex in let fft_xy = FFT . Array1 . r2c x y and fft_yx = FFT . Array1 . c2r y x in let i = ref 1 in while ! i <= dima do let l ' = min l ( dima - ! i + 1 ) in copy0 ~ n : l ' ~ ofsx :! i a ~ ofsy : dimb x ; FFT . exec fft_xy ; ignore ( Z . Vec . mul y b ' ~ z : y ) ; FFT . exec fft_yx ; D . axpy x c ~ ofsy ( : shift_seg ! i ) ~ incy : inc_seg ~ n ( : l ' + dimb - 1 ) ; i := ! i + l ; done ; D . scal ( 1 . . / float n ) c ; let xcorr = Array1 . sub c ( max 1 ( idx0 - maxlag ) ) dim_xcorr in fill0 xcorr ) end in ( match scale with | None -> ( ) | Some Biased -> D . scal ( 1 . . / float m ) xcorr | Some Unbiased -> let l = min ( m - 1 ) maxlag in for i = ilag0 - l to ilag0 + l do xcorr . { i } <- xcorr . { i } . / float ( m - abs ( ilag0 - i ) ) done | Some Coeff -> if a == b then D . scal ( 1 . . / xcorr . { ilag0 } ) xcorr else D . scal ( 1 . . / ( D . nrm2 a . * D . nrm2 b ) ) xcorr ) ; xcorr
let ( ) = Lacaml . Io . pp_float_el_default := ( fun ppf el -> fprintf ppf " . % 4g " el )