text
stringlengths
0
601k
let emit_FMUL = emit_FXXX 1
let emit_FSUB = emit_FXXX 4
let emit_FSUBR = emit_FXXX 5
let emit_FDIV = emit_FXXX 6
let emit_FDIVR = emit_FXXX 7
let emit_FILD b = function | Mem { typ = QWORD } as rm -> emit_mod_rm_reg b no_rex [ 0xDF ] rm 5 | Mem { typ = DWORD } as rm -> emit_mod_rm_reg b no_rex [ 0xDB ] rm 0 | Mem { typ = WORD } as rm -> emit_mod_rm_reg b no_rex [ 0xDF ] rm 0 | _ -> assert false
let emit_FISTP b = function | Mem { typ = WORD } as rm -> emit_mod_rm_reg b no_rex [ 0xDF ] rm 3 | Mem { typ = DWORD } as rm -> emit_mod_rm_reg b no_rex [ 0xDB ] rm 3 | Mem { typ = QWORD } as rm -> emit_mod_rm_reg b no_rex [ 0xDF ] rm 7 | _ -> assert false
let emit_FNSTCW b = function | Mem { typ = NONE | WORD } as rm -> emit_mod_rm_reg b no_rex [ 0x9B ; 0xD9 ] rm 7 | _ -> assert false
let emit_FNSTSW b = function | Reg16 RAX -> buf_opcodes b [ 0xDF ; 0xE0 ] | Mem { typ = NONE | WORD } as rm -> emit_mod_rm_reg b no_rex [ 0xDD ] rm 7 | _ -> assert false
let emit_FXXXP opcode b a1 a2 = match ( a1 , a2 ) with | Regf ( ST i ) , Regf ( ST 0 ) -> buf_opcodes b [ 0xDE ; opcode + i ] | _ -> assert false
let emit_FADDP b = emit_FXXXP 0xC0 b
let emit_FMULP b = emit_FXXXP 0xC8 b
let emit_FSUBRP b = emit_FXXXP 0xE0 b
let emit_FSUBP b = emit_FXXXP 0xE8 b
let emit_FDIVRP b = emit_FXXXP 0xF0 b
let emit_FDIVP b = emit_FXXXP 0xF8 b
let emit_XCHG b src dst = match ( dst , src ) with | ( ( Reg64 _ | Mem _ | Mem64_RIP _ ) as rm ) , Reg64 reg | Reg64 reg , ( ( Mem _ | Mem64_RIP _ ) as rm ) -> emit_mod_rm_reg b rexw [ 0x87 ] rm ( rd_of_reg64 reg ) | ( ( Reg32 _ | Mem _ | Mem64_RIP _ ) as rm ) , Reg32 reg | Reg32 reg , ( ( Mem _ | Mem64_RIP _ ) as rm ) -> emit_mod_rm_reg b no_rex [ 0x87 ] rm ( rd_of_reg64 reg ) | ( ( Reg16 _ | Mem _ | Mem64_RIP _ ) as rm ) , Reg16 reg | Reg16 reg , ( ( Mem _ | Mem64_RIP _ ) as rm ) -> emit_mod_rm_reg b rex [ 0x66 ; 0x87 ] rm ( rd_of_reg64 reg ) | ( ( ( Reg8L _ | Reg8H _ | Mem _ | Mem64_RIP _ ) as rm ) , ( ( Reg8L _ | Reg8H _ ) as reg ) ) | ( ( Reg8L _ | Reg8H _ ) as reg ) , ( ( Mem _ | Mem64_RIP _ ) as rm ) -> emit_mod_rm_reg b no_rex [ 0x86 ] rm ( rd_of_reg8 reg ) | _ -> assert false
let assemble_instr b loc = function | ADD ( src , dst ) -> emit_ADD b dst src | ADDSD ( src , dst ) -> emit_addsd b dst src | AND ( src , dst ) -> emit_AND b dst src | ANDPD ( src , dst ) -> emit_andpd b dst src | BSWAP arg -> emit_BSWAP b arg | CALL dst -> emit_call b dst | CVTSI2SD ( src , dst ) -> emit_CVTSI2SD b dst src | CVTSD2SI ( src , dst ) -> emit_CVTSD2SI b dst src | CVTTSD2SI ( src , dst ) -> emit_CVTTSD2SI b dst src | CVTSD2SS ( src , dst ) -> emit_CVTSD2SS b dst src | CVTSS2SD ( src , dst ) -> emit_CVTSS2SD b dst src | COMISD ( src , dst ) -> emit_comisd b dst src | CQO -> emit_cqto b | CMP ( src , dst ) -> emit_CMP b dst src | CMOV ( condition , src , dst ) -> emit_cmov b condition dst src | CDQ -> buf_int8 b 0x99 | DIVSD ( src , dst ) -> emit_divsd b dst src | DEC dst -> emit_DEC b [ dst ] | FCOMPP -> buf_opcodes b [ 0xDE ; 0xD9 ] | FLD1 -> buf_opcodes b [ 0xD9 ; 0xE8 ] | FLDLG2 -> buf_opcodes b [ 0xD9 ; 0xEC ] | FLDLN2 -> buf_opcodes b [ 0xD9 ; 0xED ] | FLDZ -> buf_opcodes b [ 0xD9 ; 0xEE ] | FPATAN -> buf_opcodes b [ 0xD9 ; 0xF3 ] | FCOS -> buf_opcodes b [ 0xD9 ; 0xFF ] | FYL2X -> buf_opcodes b [ 0xD9 ; 0xF1 ] | FSIN -> buf_opcodes b [ 0xD9 ; 0xFE ] | FSQRT -> buf_opcodes b [ 0xD9 ; 0xFA ] | FPTAN -> buf_opcodes b [ 0xD9 ; 0xF2 ] | FSTP dst -> emit_FSTP b dst | FXCH arg -> emit_FXCH b arg | FCOMP arg -> emit_FCOMP b arg | FNSTSW arg -> emit_FNSTSW b arg | FNSTCW arg -> emit_FNSTCW b arg | FCHS -> buf_opcodes b [ 0xD9 ; 0xE0 ] | FABS -> buf_opcodes b [ 0xD9 ; 0xE1 ] | FADD arg -> emit_FADD b arg | FSUB arg -> emit_FSUB b arg | FMUL arg -> emit_FMUL b arg | FDIV arg -> emit_FDIV b arg | FDIVR arg -> emit_FDIVR b arg | FSUBR arg -> emit_FSUBR b arg | FILD arg -> emit_FILD b arg | FISTP arg -> emit_FISTP b arg | FLD arg -> emit_FLD b arg | FLDCW arg -> emit_FLDCW b arg | FADDP ( src , dst ) -> emit_FADDP b dst src | FSUBP ( src , dst ) -> emit_FSUBP b dst src | FMULP ( src , dst ) -> emit_FMULP b dst src | FDIVP ( src , dst ) -> emit_FDIVP b dst src | FSUBRP ( src , dst ) -> emit_FSUBRP b dst src | FDIVRP ( src , dst ) -> emit_FDIVRP b dst src | HLT -> buf_int8 b 0xF4 | INC dst -> emit_inc b dst | IMUL ( src , dst ) -> emit_imul b dst src | IDIV dst -> emit_idiv b dst | J ( condition , dst ) -> emit_j b ! loc condition dst | JMP dst -> emit_jmp b ! loc dst | LEAVE -> emit_leave b | LEA ( src , dst ) -> emit_LEA b dst src | MOV ( src , dst ) -> emit_MOV b dst src | MOVAPD ( src , dst ) -> emit_movapd b dst src | MOVLPD ( src , dst ) -> emit_movlpd b dst src | MOVSD ( src , dst ) -> emit_movsd b dst src | MOVSS ( src , dst ) -> emit_movss b dst src | MULSD ( src , dst ) -> emit_mulsd b dst src | MOVSX ( src , dst ) -> emit_movsx b dst src | MOVZX ( src , dst ) -> emit_MOVZX b dst src | MOVSXD ( src , dst ) -> emit_movsxd b dst src | NEG dst -> emit_neg b dst | NOP -> buf_int8 b 0x90 | OR ( src , dst ) -> emit_OR b dst src | PUSH dst -> emit_push b dst | POP dst -> emit_pop b dst | RET -> emit_ret b | ROUNDSD ( rounding , src , dst ) -> emit_roundsd b dst rounding src | SAL ( src , dst ) -> emit_SAL b dst src | SAR ( src , dst ) -> emit_SAR b dst src | SHR ( src , dst ) -> emit_SHR b dst src | SUBSD ( src , dst ) -> emit_subsd b dst src | SQRTSD ( src , dst ) -> emit_sqrtsd b dst src | SUB ( src , dst ) -> emit_SUB b dst src | SET ( condition , dst ) -> emit_set b condition dst | TEST ( src , dst ) -> emit_test b dst src | UCOMISD ( src , dst ) -> emit_ucomisd b dst src | XCHG ( src , dst ) -> emit_XCHG b dst src | XOR ( src , dst ) -> emit_XOR b dst src | XORPD ( src , dst ) -> emit_xorpd b dst src
let assemble_line b loc ins = try match ins with | Ins instr -> assemble_instr b loc instr ; incr loc | Comment _ -> ( ) | Global s -> ( get_symbol b s ) . sy_global <- true | Quad ( Const n ) -> buf_int64L b n | Quad cst -> record_local_reloc b ( RelocConstant ( cst , B64 ) ) ; buf_int64L b 0L | Long ( Const n ) -> buf_int32L b n | Long cst -> record_local_reloc b ( RelocConstant ( cst , B32 ) ) ; buf_int32L b 0L | Word ( Const n ) -> buf_int16L b n | Word cst -> record_local_reloc b ( RelocConstant ( cst , B16 ) ) ; buf_int16L b 0L | Byte ( Const n ) -> buf_int8L b n | Byte cst -> record_local_reloc b ( RelocConstant ( cst , B8 ) ) ; buf_int8L b 0L | NewLabel ( s , _ ) -> declare_label b s | Bytes s -> Buffer . add_string b . buf s | External ( _ , _ ) -> ( ) | Set ( _ , _ ) -> assert false | Section _ -> assert false | Mode386 -> assert ( system = S_win32 ) | Model _ -> assert ( system = S_win32 ) | Cfi_startproc -> ( ) | Cfi_endproc -> ( ) | Cfi_adjust_cfa_offset _ -> ( ) | File _ -> ( ) | Loc _ -> ( ) | Private_extern _ -> assert false | Indirect_symbol _ -> assert false | Type ( lbl , kind ) -> ( get_symbol b lbl ) . sy_type <- Some kind | Size ( lbl , cst ) -> ( match eval_const b ( Buffer . length b . buf ) cst with | Rint n -> ( get_symbol b lbl ) . sy_size <- Some ( Int64 . to_int n ) | _ -> assert false ) | Align ( data , n ) -> ( let pos = Buffer . length b . buf in let current = pos mod n in if current > 0 then let n = n - current in if data then for _ = 1 to n do buf_int8 b 0x00 done else match n with | 0 -> ( ) | 1 -> buf_int8 b 0x90 | 2 -> buf_opcodes b [ 0x66 ; 0x90 ] | 3 -> buf_opcodes b [ 0x0f ; 0x1f ; 0x00 ] | 4 -> buf_opcodes b [ 0x0f ; 0x1f ; 0x40 ; 0x00 ] | 5 -> buf_opcodes b [ 0x0f ; 0x1f ; 0x44 ; 0x00 ; 0x00 ] | 6 -> buf_opcodes b [ 0x66 ; 0x0f ; 0x1f ; 0x44 ] ; buf_int16L b 0L | 7 -> buf_opcodes b [ 0x0f ; 0x1f ; 0x80 ] ; buf_int32L b 0L | _ -> for _ = 9 to n do buf_int8 b 0x66 done ; buf_opcodes b [ 0x0f ; 0x1f ; 0x84 ; 0x00 ] ; buf_int32L b 0L ) | Space n -> for _ = 1 to n do buf_int8 b 0 done with e -> Printf . eprintf " Exception % s :\ n " %! ( Printexc . to_string e ) ; raise e
let add_patch b pos size v = b . patches <- ( pos , size , v ) :: b . patches
let assemble_section arch section = ( match arch with X86 -> instr_size := 5 | X64 -> instr_size := 6 ) ; forced_long_jumps := IntSet . empty ; local_labels := String . Map . empty ; let icount = ref 0 in Array . iter section . sec_instrs ~ f ( : function | NewLabel ( lbl , _ ) -> local_labels := String . Map . add ~ key : lbl ~ data :! icount ! local_labels | Ins _ -> incr icount | _ -> ( ) ) ; let passes = ref 0 in let rec iter_assemble ( ) = incr passes ; let b = new_buffer section in local_relocs := [ ] ; let loc = ref 0 in Array . iter ~ f ( : assemble_line b loc ) section . sec_instrs ; let retry = ref false in let do_local_reloc pos = function | RelocShortJump ( label , loc ) -> let source_pos = pos + 1 in let target_pos = label_pos b label in let n = target_pos - source_pos in if n >= - 128 && n < 128 then add_patch b pos B8 ( Int64 . of_int n ) else ( forced_long_jumps := IntSet . add loc ! forced_long_jumps ; retry := true ) | RelocCall label | RelocLongJump label -> let source_pos = pos + 4 in let target_pos = label_pos b label in let n = target_pos - source_pos in add_patch b pos B32 ( Int64 . of_int n ) | RelocConstant ( cst , data_size ) -> ( let v = eval_const b pos cst in match ( v , data_size ) with | Rint n , _ -> add_patch b pos data_size n | Rabs ( lbl , offset ) , B32 -> record_reloc b pos ( Relocation . Kind . DIR32 ( lbl , offset ) ) | Rabs ( lbl , offset ) , B64 -> record_reloc b pos ( Relocation . Kind . DIR64 ( lbl , offset ) ) | Rrel ( lbl , offset ) , B32 -> record_reloc b pos ( Relocation . Kind . REL32 ( lbl , Int64 . add offset 4L ) ) | Rrel _ , _ -> assert false | Rabs _ , _ -> assert false ) in List . iter ! local_relocs ~ f ( : fun ( pos , local_reloc ) -> do_local_reloc pos local_reloc ) ; if ! retry then iter_assemble ( ) else b in iter_assemble ( )
let size b = Buffer . length b . buf
let add_patch ~ offset ~ size ~ data t = add_patch t offset size data
let contents b = let buf = Buffer . to_bytes b . buf in List . iter b . patches ~ f ( : fun ( pos , nbits , v ) -> match nbits with | B64 -> str_int64L buf pos v | B32 -> str_int32L buf pos v | B16 -> str_int16L buf pos v | B8 -> str_int8L buf pos v ) ; Bytes . to_string buf
let relocations b = b . relocations
let labels b = b . labels
let print_reg b f r = Buffer . add_char b ' ' ; % Buffer . add_string b ( f r )
let opt_displ b displ = if displ = 0 then ( ) else if displ > 0 then bprintf b " +% d " displ else bprintf b " % d " displ
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 begin match sym with | None -> if displ <> 0 || scale = 0 then Buffer . add_string b ( Int . to_string displ ) | Some s -> Buffer . add_string b s ; opt_displ b displ end ; if scale <> 0 then begin Buffer . add_char b ' ( ' ; begin match base with | None -> ( ) | Some base -> print_reg b string_of_register base end ; if base != None || scale <> 1 then Buffer . add_char b ' , ' ; print_reg b string_of_register idx ; if scale <> 1 then bprintf b " , % s " ( Int . to_string scale ) ; Buffer . add_char b ' ) ' end
let arg b = function | Sym x -> Buffer . add_char b ' ' ; $ Buffer . add_string b x | Imm x -> bprintf b " $% Ld " x | Reg8L x -> print_reg b string_of_reg8l x | Reg8H x -> print_reg b string_of_reg8h x | Reg16 x -> print_reg b string_of_reg16 x | Reg32 x -> print_reg b string_of_reg32 x | Reg64 x -> print_reg b string_of_reg64 x | Regf x -> print_reg b string_of_registerf x | Mem addr -> arg_mem b addr | Mem64_RIP ( _ , s , displ ) -> bprintf b " % s % a ( %% rip ) " s opt_displ displ
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 " . " | 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 " 0x % Lx " n | ConstAdd ( c1 , c2 ) -> bprintf b " ( % a + % a ) " scst c1 scst c2 | ConstSub ( c1 , c2 ) -> bprintf b " ( % a - % a ) " scst c1 scst c2
let typeof = function | Mem { typ ; _ } | Mem64_RIP ( typ , _ , _ ) -> typ | Reg8L _ | Reg8H _ -> BYTE | Reg16 _ -> WORD | Reg32 _ -> DWORD | Reg64 _ -> QWORD | Imm _ | Sym _ -> NONE | Regf _ -> assert false
let suf arg = match typeof arg with | BYTE -> " b " | WORD -> " w " | DWORD | REAL8 -> " l " | QWORD -> " q " | REAL4 -> " s " | NONE -> " " | OWORD | NEAR | PROC -> assert false
let i0 b s = bprintf b " \ t % s " s
let i1 b s x = bprintf b " \ t % s \ t % a " s arg x
let i1_s b s x = bprintf b " \ t % s % s \ t % a " s ( suf x ) arg x
let i2 b s x y = bprintf b " \ t % s \ t % a , % a " s arg x arg y
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