text
stringlengths
12
786k
let help_action ( ) = raise ( Stop ( Unknown " - help " ) )
let add_help speclist = let add1 = try ignore ( assoc3 " - help " speclist ) ; [ ] with Not_found -> [ " - help " , Unit help_action , " Display this list of options " ] and add2 = try ignore ( assoc3 " -- help " speclist ) ; [ ] with Not_found -> [ " -- help " , Unit help_action , " Display this list of options " ] in speclist @ ( add1 @ add2 )
let usage_b buf speclist errmsg = bprintf buf " % s \ n " errmsg ; List . iter ( print_spec buf ) ( add_help speclist )
let usage_string speclist errmsg = let b = Buffer . create 200 in usage_b b speclist errmsg ; Buffer . contents b
let usage speclist errmsg = eprintf " % s " ( usage_string speclist errmsg )
let current = ref 0
let bool_of_string_opt x = try Some ( bool_of_string x ) with Invalid_argument _ -> None
let int_of_string_opt x = try Some ( int_of_string x ) with Failure _ -> None
let float_of_string_opt x = try Some ( float_of_string x ) with Failure _ -> None
let parse_and_expand_argv_dynamic_aux allow_expand current argv speclist anonfun errmsg = let initpos = ! current in let convert_error error = let b = Buffer . create 200 in let progname = if initpos < ( Array . length ! argv ) then ! argv . ( initpos ) else " ( ) " ? in begin match error with | Unknown " - help " -> ( ) | Unknown " -- help " -> ( ) | Unknown s -> bprintf b " % s : unknown option ' % s ' . \ n " progname s | Missing s -> bprintf b " % s : option ' % s ' needs an argument . \ n " progname s | Wrong ( opt , arg , expected ) -> bprintf b " % s : wrong argument ' % s ' ; option ' % s ' expects % s . \ n " progname arg opt expected | Message s -> bprintf b " % s : % s . \ n " progname s end ; usage_b b ! speclist errmsg ; if error = Unknown " - help " || error = Unknown " -- help " then Help ( Buffer . contents b ) else Bad ( Buffer . contents b ) in incr current ; while ! current < ( Array . length ! argv ) do begin try let s = ! argv . ( ! current ) in if String . length s >= 1 && s . [ 0 ] = ' ' - then begin let action , follow = try assoc3 s ! speclist , None with Not_found -> try let keyword , arg = split s in assoc3 keyword ! speclist , Some arg with Not_found -> raise ( Stop ( Unknown s ) ) in let no_arg ( ) = match follow with | None -> ( ) | Some arg -> raise ( Stop ( Wrong ( s , arg , " no argument " ) ) ) in let get_arg ( ) = match follow with | None -> if ! current + 1 < ( Array . length ! argv ) then ! argv . ( ! current + 1 ) else raise ( Stop ( Missing s ) ) | Some arg -> arg in let consume_arg ( ) = match follow with | None -> incr current | Some _ -> ( ) in let rec treat_action = function | Unit f -> no_arg ( ) ; f ( ) ; | Bool f -> let arg = get_arg ( ) in begin match bool_of_string_opt arg with | None -> raise ( Stop ( Wrong ( s , arg , " a boolean " ) ) ) | Some s -> f s end ; consume_arg ( ) ; | Set r -> no_arg ( ) ; r := true ; | Clear r -> no_arg ( ) ; r := false ; | String f -> let arg = get_arg ( ) in f arg ; consume_arg ( ) ; | Symbol ( symb , f ) -> let arg = get_arg ( ) in if List . mem arg symb then begin f arg ; consume_arg ( ) ; end else begin raise ( Stop ( Wrong ( s , arg , " one of : " ^ ( make_symlist " " " " " " symb ) ) ) ) end | Set_string r -> r := get_arg ( ) ; consume_arg ( ) ; | Int f -> let arg = get_arg ( ) in begin match int_of_string_opt arg with | None -> raise ( Stop ( Wrong ( s , arg , " an integer " ) ) ) | Some x -> f x end ; consume_arg ( ) ; | Set_int r -> let arg = get_arg ( ) in begin match int_of_string_opt arg with | None -> raise ( Stop ( Wrong ( s , arg , " an integer " ) ) ) | Some x -> r := x end ; consume_arg ( ) ; | Float f -> let arg = get_arg ( ) in begin match float_of_string_opt arg with | None -> raise ( Stop ( Wrong ( s , arg , " a float " ) ) ) | Some x -> f x end ; consume_arg ( ) ; | Set_float r -> let arg = get_arg ( ) in begin match float_of_string_opt arg with | None -> raise ( Stop ( Wrong ( s , arg , " a float " ) ) ) | Some x -> r := x end ; consume_arg ( ) ; | Tuple specs -> no_arg ( ) ; List . iter treat_action specs ; | Rest f -> no_arg ( ) ; while ! current < ( Array . length ! argv ) - 1 do f ! argv . ( ! current + 1 ) ; consume_arg ( ) ; done ; | Rest_all f -> no_arg ( ) ; let acc = ref [ ] in while ! current < Array . length ! argv - 1 do acc := ! argv . ( ! current + 1 ) :: ! acc ; consume_arg ( ) ; done ; f ( List . rev ! acc ) | Expand f -> if not allow_expand then raise ( Invalid_argument " Arg . Expand is is only allowed with \ Arg . parse_and_expand_argv_dynamic " ) ; let arg = get_arg ( ) in let newarg = f arg in consume_arg ( ) ; let before = Array . sub ! argv 0 ( ! current + 1 ) and after = Array . sub ! argv ( ! current + 1 ) ( ( Array . length ! argv ) - ! current - 1 ) in argv := Array . concat [ before ; newarg ; after ] ; in treat_action action end else anonfun s with | Bad m -> raise ( convert_error ( Message m ) ) ; | Stop e -> raise ( convert_error e ) ; end ; incr current done
let parse_and_expand_argv_dynamic current argv speclist anonfun errmsg = parse_and_expand_argv_dynamic_aux true current argv speclist anonfun errmsg
let parse_argv_dynamic ( ? current = current ) argv speclist anonfun errmsg = parse_and_expand_argv_dynamic_aux false current ( ref argv ) speclist anonfun errmsg
let parse_argv ( ? current = current ) argv speclist anonfun errmsg = parse_argv_dynamic ~ current : current argv ( ref speclist ) anonfun errmsg
let parse l f msg = try parse_argv Sys . argv l f msg with | Bad msg -> eprintf " % s " msg ; exit 2 | Help msg -> printf " % s " msg ; exit 0
let parse_dynamic l f msg = try parse_argv_dynamic Sys . argv l f msg with | Bad msg -> eprintf " % s " msg ; exit 2 | Help msg -> printf " % s " msg ; exit 0
let parse_expand l f msg = try let argv = ref Sys . argv in let spec = ref l in let current = ref ( ! current ) in parse_and_expand_argv_dynamic current argv spec f msg with | Bad msg -> eprintf " % s " msg ; exit 2 | Help msg -> printf " % s " msg ; exit 0
let second_word s = let len = String . length s in let rec loop n = if n >= len then len else if s . [ n ] = ' ' then loop ( n + 1 ) else n in match String . index s ' \ t ' with | n -> loop ( n + 1 ) | exception Not_found -> begin match String . index s ' ' with | n -> loop ( n + 1 ) | exception Not_found -> len end
let max_arg_len cur ( kwd , spec , doc ) = match spec with | Symbol _ -> max cur ( String . length kwd ) | _ -> max cur ( String . length kwd + second_word doc )
let replace_leading_tab s = let seen = ref false in String . map ( function ' \ t ' when not ! seen -> seen := true ; ' ' | c -> c ) s
let add_padding len ksd = match ksd with | ( _ , _ , " " ) -> ksd | ( kwd , ( Symbol _ as spec ) , msg ) -> let cutcol = second_word msg in let spaces = String . make ( ( max 0 ( len - cutcol ) ) + 3 ) ' ' in ( kwd , spec , " \ n " ^ spaces ^ replace_leading_tab msg ) | ( kwd , spec , msg ) -> let cutcol = second_word msg in let kwd_len = String . length kwd in let diff = len - kwd_len - cutcol in if diff <= 0 then ( kwd , spec , replace_leading_tab msg ) else let spaces = String . make diff ' ' in let prefix = String . sub ( replace_leading_tab msg ) 0 cutcol in let suffix = String . sub msg cutcol ( String . length msg - cutcol ) in ( kwd , spec , prefix ^ spaces ^ suffix )
let align ( ? limit = max_int ) speclist = let completed = add_help speclist in let len = List . fold_left max_arg_len 0 completed in let len = min len limit in List . map ( add_padding len ) completed
let trim_cr s = let len = String . length s in if len > 0 && String . get s ( len - 1 ) = ' \ r ' then String . sub s 0 ( len - 1 ) else s
let read_aux trim sep file = let ic = open_in_bin file in let buf = Buffer . create 200 in let words = ref [ ] in let stash ( ) = let word = Buffer . contents buf in let word = if trim then trim_cr word else word in words := word :: ! words ; Buffer . clear buf in begin try while true do let c = input_char ic in if c = sep then stash ( ) else Buffer . add_char buf c done with End_of_file -> ( ) end ; if Buffer . length buf > 0 then stash ( ) ; close_in ic ; Array . of_list ( List . rev ! words )
let read_arg = read_aux true ' \ n '
let read_arg0 = read_aux false ' \ x00 '
let write_aux sep file args = let oc = open_out_bin file in Array . iter ( fun s -> fprintf oc " % s % c " s sep ) args ; close_out oc
let write_arg = write_aux ' \ n '
let write_arg0 = write_aux ' \ x00 '
let get_error_when_null_denominator ( ) = ! error_when_null_denominator_flag error_when_null_denominator_flag := choice ; ;
let get_normalize_ratio ( ) = ! normalize_ratio_flag
let get_normalize_ratio_when_printing ( ) = ! normalize_ratio_when_printing_flag normalize_ratio_when_printing_flag := choice ; ;
let get_floating_precision ( ) = ! floating_precision
let get_approx_printing ( ) = ! approx_printing_flag
let arith_print_string s = print_string s ; print_string " --> " ; ;
let arith_print_bool = function true -> print_string " ON " ; ;
let arith_status ( ) = print_newline ( ) ; arith_print_string " Normalization during computation " ; arith_print_bool ( get_normalize_ratio ( ) ) ; print_newline ( ) ; print_string " ( returned by get_normalize_ratio ( ) ) " ; print_newline ( ) ; print_string " ( modifiable with set_normalize_ratio < your choice ) " ; > print_newline ( ) ; print_newline ( ) ; arith_print_string " Normalization when printing " ; arith_print_bool ( get_normalize_ratio_when_printing ( ) ) ; print_newline ( ) ; print_string " ( returned by get_normalize_ratio_when_printing ( ) ) " ; print_newline ( ) ; print_string " ( modifiable with set_normalize_ratio_when_printing < your choice ) " ; > print_newline ( ) ; print_newline ( ) ; arith_print_string " Floating point approximation when printing rational numbers " ; arith_print_bool ( get_approx_printing ( ) ) ; print_newline ( ) ; print_string " ( returned by get_approx_printing ( ) ) " ; print_newline ( ) ; print_string " ( modifiable with set_approx_printing < your choice ) " ; > print_newline ( ) ; ( if ( get_approx_printing ( ) ) then ( print_string " Default precision = " ; print_int ( get_floating_precision ( ) ) ; print_newline ( ) ; print_string " ( returned by get_floating_precision ( ) ) " ; print_newline ( ) ; print_string " ( modifiable with set_floating_precision < your choice ) " ; > print_newline ( ) ; print_newline ( ) ) else print_newline ( ) ) ; arith_print_string " Error when a rational denominator is null " ; arith_print_bool ( get_error_when_null_denominator ( ) ) ; print_newline ( ) ; print_string " ( returned by get_error_when_null_denominator ( ) ) " ; print_newline ( ) ; print_string " ( modifiable with set_error_when_null_denominator < your choice ) " ; > print_newline ( ) ; ;
type ' a t = ' a array ' a array -> int -> ' a array -> int -> int -> unit = " caml_array_blit " ' a array -> int -> int -> ' a -> unit = " caml_array_fill "
module Floatarray = struct external create : int -> floatarray = " caml_floatarray_create " external length : floatarray -> int = " % floatarray_length " external get : floatarray -> int -> float = " % floatarray_safe_get " external set : floatarray -> int -> float -> unit = " % floatarray_safe_set " external unsafe_get : floatarray -> int -> float = " % floatarray_unsafe_get " external unsafe_set : floatarray -> int -> float -> unit = " % floatarray_unsafe_set " end
let init l f = if l = 0 then [ ] || else if l < 0 then invalid_arg " Array . init " else let res = create l ( f 0 ) in for i = 1 to pred l do unsafe_set res i ( f i ) done ; res
let make_matrix sx sy init = let res = create sx [ ] || in for x = 0 to pred sx do unsafe_set res x ( create sy init ) done ; res
let copy a = let l = length a in if l = 0 then [ ] || else unsafe_sub a 0 l
let append a1 a2 = let l1 = length a1 in if l1 = 0 then copy a2 else if length a2 = 0 then unsafe_sub a1 0 l1 else append_prim a1 a2
let sub a ofs len = if ofs < 0 || len < 0 || ofs > length a - len then invalid_arg " Array . sub " else unsafe_sub a ofs len
let fill a ofs len v = if ofs < 0 || len < 0 || ofs > length a - len then invalid_arg " Array . fill " else unsafe_fill a ofs len v
let blit a1 ofs1 a2 ofs2 len = if len < 0 || ofs1 < 0 || ofs1 > length a1 - len || ofs2 < 0 || ofs2 > length a2 - len then invalid_arg " Array . blit " else unsafe_blit a1 ofs1 a2 ofs2 len
let iter f a = for i = 0 to length a - 1 do f ( unsafe_get a i ) done
let iter2 f a b = if length a <> length b then invalid_arg " Array . iter2 : arrays must have the same length " else for i = 0 to length a - 1 do f ( unsafe_get a i ) ( unsafe_get b i ) done
let map f a = let l = length a in if l = 0 then [ ] || else begin let r = create l ( f ( unsafe_get a 0 ) ) in for i = 1 to l - 1 do unsafe_set r i ( f ( unsafe_get a i ) ) done ; r end
let map2 f a b = let la = length a in let lb = length b in if la <> lb then invalid_arg " Array . map2 : arrays must have the same length " else begin if la = 0 then [ ] || else begin let r = create la ( f ( unsafe_get a 0 ) ( unsafe_get b 0 ) ) in for i = 1 to la - 1 do unsafe_set r i ( f ( unsafe_get a i ) ( unsafe_get b i ) ) done ; r end end
let iteri f a = for i = 0 to length a - 1 do f i ( unsafe_get a i ) done
let mapi f a = let l = length a in if l = 0 then [ ] || else begin let r = create l ( f 0 ( unsafe_get a 0 ) ) in for i = 1 to l - 1 do unsafe_set r i ( f i ( unsafe_get a i ) ) done ; r end
let to_list a = let rec tolist i res = if i < 0 then res else tolist ( i - 1 ) ( unsafe_get a i :: res ) in tolist ( length a - 1 ) [ ]
let rec list_length accu = function | [ ] -> accu | _ :: t -> list_length ( succ accu ) t
let of_list = function [ ] -> [ ] || | hd :: tl as l -> let a = create ( list_length 0 l ) hd in let rec fill i = function [ ] -> a | hd :: tl -> unsafe_set a i hd ; fill ( i + 1 ) tl in fill 1 tl
let fold_left f x a = let r = ref x in for i = 0 to length a - 1 do r := f ! r ( unsafe_get a i ) done ; ! r
let fold_right f a x = let r = ref x in for i = length a - 1 downto 0 do r := f ( unsafe_get a i ) ! r done ; ! r
let exists p a = let n = length a in let rec loop i = if i = n then false else if p ( unsafe_get a i ) then true else loop ( succ i ) in loop 0
let for_all p a = let n = length a in let rec loop i = if i = n then true else if p ( unsafe_get a i ) then loop ( succ i ) else false in loop 0
let for_all2 p l1 l2 = let n1 = length l1 and n2 = length l2 in if n1 <> n2 then invalid_arg " Array . for_all2 " else let rec loop i = if i = n1 then true else if p ( unsafe_get l1 i ) ( unsafe_get l2 i ) then loop ( succ i ) else false in loop 0
let exists2 p l1 l2 = let n1 = length l1 and n2 = length l2 in if n1 <> n2 then invalid_arg " Array . exists2 " else let rec loop i = if i = n1 then false else if p ( unsafe_get l1 i ) ( unsafe_get l2 i ) then true else loop ( succ i ) in loop 0
let mem x a = let n = length a in let rec loop i = if i = n then false else if compare ( unsafe_get a i ) x = 0 then true else loop ( succ i ) in loop 0
let memq x a = let n = length a in let rec loop i = if i = n then false else if x == ( unsafe_get a i ) then true else loop ( succ i ) in loop 0
let sort cmp a = let maxson l i = let i31 = i + i + i + 1 in let x = ref i31 in if i31 + 2 < l then begin if cmp ( get a i31 ) ( get a ( i31 + 1 ) ) < 0 then x := i31 + 1 ; if cmp ( get a ! x ) ( get a ( i31 + 2 ) ) < 0 then x := i31 + 2 ; ! x end else if i31 + 1 < l && cmp ( get a i31 ) ( get a ( i31 + 1 ) ) < 0 then i31 + 1 else if i31 < l then i31 else raise ( Bottom i ) in let rec trickledown l i e = let j = maxson l i in if cmp ( get a j ) e > 0 then begin set a i ( get a j ) ; trickledown l j e ; end else begin set a i e ; end ; in let trickle l i e = try trickledown l i e with Bottom i -> set a i e in let rec bubbledown l i = let j = maxson l i in set a i ( get a j ) ; bubbledown l j in let bubble l i = try bubbledown l i with Bottom i -> i in let rec trickleup i e = let father = ( i - 1 ) / 3 in assert ( i <> father ) ; if cmp ( get a father ) e < 0 then begin set a i ( get a father ) ; if father > 0 then trickleup father e else set a 0 e ; end else begin set a i e ; end ; in let l = length a in for i = ( l + 1 ) / 3 - 1 downto 0 do trickle l i ( get a i ) ; done ; for i = l - 1 downto 2 do let e = ( get a i ) in set a i ( get a 0 ) ; trickleup ( bubble i 0 ) e ; done ; if l > 1 then ( let e = ( get a 1 ) in set a 1 ( get a 0 ) ; set a 0 e )
let stable_sort cmp a = let merge src1ofs src1len src2 src2ofs src2len dst dstofs = let src1r = src1ofs + src1len and src2r = src2ofs + src2len in let rec loop i1 s1 i2 s2 d = if cmp s1 s2 <= 0 then begin set dst d s1 ; let i1 = i1 + 1 in if i1 < src1r then loop i1 ( get a i1 ) i2 s2 ( d + 1 ) else blit src2 i2 dst ( d + 1 ) ( src2r - i2 ) end else begin set dst d s2 ; let i2 = i2 + 1 in if i2 < src2r then loop i1 s1 i2 ( get src2 i2 ) ( d + 1 ) else blit a i1 dst ( d + 1 ) ( src1r - i1 ) end in loop src1ofs ( get a src1ofs ) src2ofs ( get src2 src2ofs ) dstofs ; in let isortto srcofs dst dstofs len = for i = 0 to len - 1 do let e = ( get a ( srcofs + i ) ) in let j = ref ( dstofs + i - 1 ) in while ( ! j >= dstofs && cmp ( get dst ! j ) e > 0 ) do set dst ( ! j + 1 ) ( get dst ! j ) ; decr j ; done ; set dst ( ! j + 1 ) e ; done ; in let rec sortto srcofs dst dstofs len = if len <= cutoff then isortto srcofs dst dstofs len else begin let l1 = len / 2 in let l2 = len - l1 in sortto ( srcofs + l1 ) dst ( dstofs + l1 ) l2 ; sortto srcofs a ( srcofs + l2 ) l1 ; merge ( srcofs + l2 ) l1 dst ( dstofs + l1 ) l2 dst dstofs ; end ; in let l = length a in if l <= cutoff then isortto 0 a 0 l else begin let l1 = l / 2 in let l2 = l - l1 in let t = make l2 ( get a 0 ) in sortto l1 t 0 l2 ; sortto 0 a l2 l1 ; merge l2 l1 t 0 l2 a 0 ; end
let to_seq a = let rec aux i ( ) = if i < length a then let x = unsafe_get a i in Seq . Cons ( x , aux ( i + 1 ) ) else Seq . Nil in aux 0
let to_seqi a = let rec aux i ( ) = if i < length a then let x = unsafe_get a i in Seq . Cons ( ( i , x ) , aux ( i + 1 ) ) else Seq . Nil in aux 0
let of_rev_list = function [ ] -> [ ] || | hd :: tl as l -> let len = list_length 0 l in let a = create len hd in let rec fill i = function [ ] -> a | hd :: tl -> unsafe_set a i hd ; fill ( i - 1 ) tl in fill ( len - 2 ) tl
let of_seq i = let l = Seq . fold_left ( fun acc x -> x :: acc ) [ ] i in of_rev_list l
let bigarray n = [ | ] |
let test1 ( ) = let a = bigarray 12345 in Gc . full_major ( ) ; for i = 0 to Array . length a - 1 do if a . ( i ) <> 12345 + i then print_string " Test1 : error \ n " done
let testcopy a = Array . copy a = a
let test2 ( ) = if not ( testcopy [ | 1 ; 2 ; 3 ; 4 ; 5 ] ) | then print_string " Test2 : failed on int array \ n " ; if not ( testcopy [ | 1 . 2 ; 2 . 3 ; 3 . 4 ; 4 . 5 ] ) | then print_string " Test2 : failed on float array \ n " ; if not ( testcopy [ " | un " ; " deux " ; " trois " ] ) | then print_string " Test2 : failed on string array \ n " ; if not ( testcopy ( bigarray 42 ) ) then print_string " Test2 : failed on big array \ n "
module AbstractFloat = ( struct type t = float let to_float x = x let from_float x = x end : sig type t val to_float : t -> float val from_float : float -> t end )
let test3 ( ) = let t1 = AbstractFloat . from_float 1 . 0 and t2 = AbstractFloat . from_float 2 . 0 and t3 = AbstractFloat . from_float 3 . 0 in let v = [ | t1 ; t2 ; t3 ] | in let w = Array . make 2 t1 in let u = Array . copy v in if not ( AbstractFloat . to_float v . ( 0 ) = 1 . 0 && AbstractFloat . to_float v . ( 1 ) = 2 . 0 && AbstractFloat . to_float v . ( 2 ) = 3 . 0 ) then print_string " Test3 : failed on v \ n " ; if not ( AbstractFloat . to_float w . ( 0 ) = 1 . 0 && AbstractFloat . to_float w . ( 1 ) = 1 . 0 ) then print_string " Test3 : failed on w \ n " ; if not ( AbstractFloat . to_float u . ( 0 ) = 1 . 0 && AbstractFloat . to_float u . ( 1 ) = 2 . 0 && AbstractFloat . to_float u . ( 2 ) = 3 . 0 ) then print_string " Test3 : failed on u \ n "
let test4 ( ) = let a = bigarray 0 in let b = Array . sub a 50 10 in if b <> [ | 50 ; 51 ; 52 ; 53 ; 54 ; 55 ; 56 ; 57 ; 58 ; 59 ] | then print_string " Test4 : failed \ n "
let test5 ( ) = if Array . append [ | 1 ; 2 ; 3 ] | [ | 4 ; 5 ] | <> [ | 1 ; 2 ; 3 ; 4 ; 5 ] | then print_string " Test5 : failed on int arrays \ n " ; if Array . append [ | 1 . 0 ; 2 . 0 ; 3 . 0 ] | [ | 4 . 0 ; 5 . 0 ] | <> [ | 1 . 0 ; 2 . 0 ; 3 . 0 ; 4 . 0 ; 5 . 0 ] | then print_string " Test5 : failed on float arrays \ n "
let test6 ( ) = let a = [ | 0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ] | in let b = Array . concat [ a ; a ; a ; a ; a ; a ; a ; a ; a ; a ] in if not ( Array . length b = 100 && b . ( 6 ) = 6 && b . ( 42 ) = 2 && b . ( 99 ) = 9 ) then print_string " Test6 : failed \ n "
let test7 ( ) = let a = Array . make 10 " a " in let b = [ | " b1 " ; " b2 " ; " b3 " ] | in Array . blit b 0 a 5 3 ; if a <> [ " | a " ; " a " ; " a " ; " a " ; " a " ; " b1 " ; " b2 " ; " b3 " ; " a " ; " a " ] | || b <> [ " | b1 " ; " b2 " ; " b3 " ] | then print_string " Test7 : failed ( 1 ) \ n " ; Array . blit a 5 a 6 4 ; if a <> [ " | a " ; " a " ; " a " ; " a " ; " a " ; " b1 " ; " b1 " ; " b2 " ; " b3 " ; " a " ] | then print_string " Test7 : failed ( 2 ) \ n "
let test8 ( ) = ( try ignore ( Array . sub [ ] || 0 1 ) ; print_string " Test 8 . 1 : failed \ n " with Invalid_argument _ -> ( ) ) ; ( try ignore ( Array . sub [ | 3 ; 4 ] | 1 ( - 1 ) ) ; print_string " Test 8 . 2 : failed \ n " with Invalid_argument _ -> ( ) ) ; ( try ignore ( Array . sub [ | 3 ; 4 ] | max_int 1 ) ; print_string " Test 8 . 3 : failed \ n " with Invalid_argument _ -> ( ) ) ; ( try ignore ( Array . sub [ | 3 ; 4 ] | ( - 1 ) 1 ) ; print_string " Test 8 . 4 : failed \ n " with Invalid_argument _ -> ( ) )
let _ = test1 ( ) ; test2 ( ) ; test3 ( ) ; test4 ( ) ; test5 ( ) ; test6 ( ) ; test7 ( ) ; test8 ( ) ; exit 0
type error = | Assembler_error of string | Mismatched_for_pack of string option | Asm_generation of string * Emitaux . error
let cmm_invariants ppf fd_cmm = let print_fundecl = if ! Clflags . dump_cmm then Printcmm . fundecl else fun ppf fdecl -> Format . fprintf ppf " % s " fdecl . fun_name in if ! Clflags . cmm_invariants && Cmm_invariants . run ppf fd_cmm then Misc . fatal_errorf " Cmm invariants failed on following fundecl . :@% a . " @ print_fundecl fd_cmm ; fd_cmm
let liveness phrase = Liveness . fundecl phrase ; phrase
let dump_if ppf flag message phrase = if ! flag then Printmach . phase message ppf phrase
let pass_dump_if ppf flag message phrase = dump_if ppf flag message phrase ; phrase
let pass_dump_linear_if ppf flag message phrase = if ! flag then fprintf ppf " *** % s . @% a . " @ message Printlinear . fundecl phrase ; phrase
let start_from_emit = ref true
let should_save_before_emit ( ) = should_save_ir_after Compiler_pass . Scheduling && ( not ! start_from_emit )
let linear_unit_info = { Linear_format . unit_name = " " ; items = [ ] ; for_pack = None ; }
let reset ( ) = start_from_emit := false ; if should_save_before_emit ( ) then begin linear_unit_info . unit_name <- Compilenv . current_unit_name ( ) ; linear_unit_info . items <- [ ] ; linear_unit_info . for_pack <- ! Clflags . for_package ; end
let save_data dl = if should_save_before_emit ( ) then begin linear_unit_info . items <- Linear_format . ( Data dl ) :: linear_unit_info . items end ; dl
let save_linear f = if should_save_before_emit ( ) then begin linear_unit_info . items <- Linear_format . ( Func f ) :: linear_unit_info . items end ; f
let write_linear prefix = if should_save_before_emit ( ) then begin let filename = Compiler_pass . ( to_output_filename Scheduling ~ prefix ) in linear_unit_info . items <- List . rev linear_unit_info . items ; Linear_format . save filename linear_unit_info end
let should_emit ( ) = not ( should_stop_after Compiler_pass . Scheduling )
let if_emit_do f x = if should_emit ( ) then f x else ( )
let emit_begin_assembly = if_emit_do Emit . begin_assembly
let emit_end_assembly = if_emit_do Emit . end_assembly
let emit_data = if_emit_do Emit . data
let emit_fundecl fd = if should_emit ( ) then begin try Profile . record ~ accumulate : true " emit " Emit . fundecl fd with Emitaux . Error e -> raise ( Error ( Asm_generation ( fd . Linear . fun_name , e ) ) ) end
let rec regalloc ~ ppf_dump round fd = if round > 50 then fatal_error ( fd . Mach . fun_name ^ " : function too complex , cannot complete register allocation " ) ; dump_if ppf_dump dump_live " Liveness analysis " fd ; let num_stack_slots = if ! use_linscan then begin Interval . build_intervals fd ; if ! dump_interval then Printmach . intervals ppf_dump ( ) ; Linscan . allocate_registers ( ) end else begin Interf . build_graph fd ; if ! dump_interf then Printmach . interferences ppf_dump ( ) ; if ! dump_prefer then Printmach . preferences ppf_dump ( ) ; Coloring . allocate_registers ( ) end in dump_if ppf_dump dump_regalloc " After register allocation " fd ; let ( newfd , redo_regalloc ) = Reload . fundecl fd num_stack_slots in dump_if ppf_dump dump_reload " After insertion of reloading code " newfd ; if redo_regalloc then begin Reg . reinit ( ) ; Liveness . fundecl newfd ; regalloc ~ ppf_dump ( round + 1 ) newfd end else newfd