text
stringlengths
12
786k
type constant = | Const_closure of is_global * Clambda . ufunction list * Clambda . uconstant list | Const_table of is_global * Cmm . data_item list
type t = { mutable constants : constant S . Map . t ; mutable data_items : Cmm . data_item list list ; structured_constants : ( string , Clambda . ustructured_constant ) Hashtbl . t ; functions : Clambda . ufunction Queue . t ; }
let empty = { constants = S . Map . empty ; data_items = [ ] ; functions = Queue . create ( ) ; structured_constants = Hashtbl . create 16 ; }
let add_constant sym cst = state . constants <- S . Map . add sym cst state . constants
let add_data_items items = state . data_items <- items :: state . data_items
let add_function func = Queue . add func state . functions
let get_and_clear_constants ( ) = let constants = state . constants in state . constants <- S . Map . empty ; constants
let get_and_clear_data_items ( ) = let data_items = List . concat ( List . rev state . data_items ) in state . data_items <- [ ] ; data_items
let next_function ( ) = match Queue . take state . functions with | exception Queue . Empty -> None | func -> Some func
let no_more_functions ( ) = Queue . is_empty state . functions
let set_structured_constants l = Hashtbl . clear state . structured_constants ; List . iter ( fun ( c : Clambda . preallocated_constant ) -> Hashtbl . add state . structured_constants c . symbol c . definition ) l
let add_structured_constant sym cst = Hashtbl . replace state . structured_constants sym cst
let get_structured_constant s = Hashtbl . find_opt state . structured_constants s
let structured_constant_of_sym s = match Compilenv . structured_constant_of_symbol s with | None -> get_structured_constant s | Some _ as r -> r
let bind name arg fn = match arg with Cvar _ | Cconst_int _ | Cconst_natint _ | Cconst_symbol _ -> fn arg | _ -> let id = V . create_local name in Clet ( VP . create id , arg , fn ( Cvar id ) )
let bind_load name arg fn = match arg with | Cop ( Cload _ , [ Cvar _ ] , _ ) -> fn arg | _ -> bind name arg fn
let bind_nonvar name arg fn = match arg with Cconst_int _ | Cconst_natint _ | Cconst_symbol _ -> fn arg | _ -> let id = V . create_local name in Clet ( VP . create id , arg , fn ( Cvar id ) )
let caml_black = Nativeint . shift_left ( Nativeint . of_int 3 ) 8
let caml_local = Nativeint . shift_left ( Nativeint . of_int 2 ) 8
let floatarray_tag dbg = Cconst_int ( Obj . double_array_tag , dbg )
let block_header tag sz = Nativeint . add ( Nativeint . shift_left ( Nativeint . of_int sz ) 10 ) ( Nativeint . of_int tag )
let black_block_header tag sz = Nativeint . logor ( block_header tag sz ) caml_black
let local_block_header tag sz = Nativeint . logor ( block_header tag sz ) caml_local
let white_closure_header sz = block_header Obj . closure_tag sz
let black_closure_header sz = black_block_header Obj . closure_tag sz
let local_closure_header sz = local_block_header Obj . closure_tag sz
let infix_header ofs = block_header Obj . infix_tag ofs
let float_header = block_header Obj . double_tag ( size_float / size_addr )
let float_local_header = local_block_header Obj . double_tag ( size_float / size_addr )
let floatarray_header len = assert ( len >= 0 ) ; if len = 0 then block_header 0 0 else block_header Obj . double_array_tag ( len * size_float / size_addr )
let string_header len = block_header Obj . string_tag ( ( len + size_addr ) / size_addr )
let boxedint32_header = block_header Obj . custom_tag 2
let boxedint64_header = block_header Obj . custom_tag ( 1 + 8 / size_addr )
let boxedintnat_header = block_header Obj . custom_tag 2
let boxedint32_local_header = local_block_header Obj . custom_tag 2
let boxedint64_local_header = local_block_header Obj . custom_tag ( 1 + 8 / size_addr )
let boxedintnat_local_header = local_block_header Obj . custom_tag 2
let pos_arity_in_closinfo = 8 * size_addr - 8
let closure_info ~ arity ~ startenv = let arity = match arity with | Lambda . Tupled , n -> - n | Lambda . Curried _ , n -> n in assert ( - 128 <= arity && arity <= 127 ) ; assert ( 0 <= startenv && startenv < 1 lsl ( pos_arity_in_closinfo - 1 ) ) ; Nativeint . ( add (...
let alloc_float_header mode dbg = match mode with | Lambda . Alloc_heap -> Cconst_natint ( float_header , dbg ) | Lambda . Alloc_local -> Cconst_natint ( float_local_header , dbg )
let alloc_floatarray_header len dbg = Cconst_natint ( floatarray_header len , dbg )
let alloc_closure_header ~ mode sz dbg = match ( mode : Lambda . alloc_mode ) with | Alloc_heap -> Cconst_natint ( white_closure_header sz , dbg ) | Alloc_local -> Cconst_natint ( local_closure_header sz , dbg )
let alloc_infix_header ofs dbg = Cconst_natint ( infix_header ofs , dbg )
let alloc_closure_info ~ arity ~ startenv dbg = Cconst_natint ( closure_info ~ arity ~ startenv , dbg )
let alloc_boxedint32_header mode dbg = match mode with | Lambda . Alloc_heap -> Cconst_natint ( boxedint32_header , dbg ) | Lambda . Alloc_local -> Cconst_natint ( boxedint32_local_header , dbg )
let alloc_boxedint64_header mode dbg = match mode with | Lambda . Alloc_heap -> Cconst_natint ( boxedint64_header , dbg ) | Lambda . Alloc_local -> Cconst_natint ( boxedint64_local_header , dbg )
let alloc_boxedintnat_header mode dbg = match mode with | Lambda . Alloc_heap -> Cconst_natint ( boxedintnat_header , dbg ) | Lambda . Alloc_local -> Cconst_natint ( boxedintnat_local_header , dbg )
let max_repr_int = max_int asr 1
let min_repr_int = min_int asr 1
let int_const dbg n = if n <= max_repr_int && n >= min_repr_int then Cconst_int ( ( n lsl 1 ) + 1 , dbg ) else Cconst_natint ( Nativeint . add ( Nativeint . shift_left ( Nativeint . of_int n ) 1 ) 1n , dbg )
let natint_const_untagged dbg n = if n > Nativeint . of_int max_int || n < Nativeint . of_int min_int then Cconst_natint ( n , dbg ) else Cconst_int ( Nativeint . to_int n , dbg )
let cint_const n = Cint ( Nativeint . add ( Nativeint . shift_left ( Nativeint . of_int n ) 1 ) 1n )
let targetint_const n = Targetint . add ( Targetint . shift_left ( Targetint . of_int n ) 1 ) Targetint . one
let add_no_overflow n x c dbg = let d = n + x in if d = 0 then c else Cop ( Caddi , [ c ; Cconst_int ( d , dbg ) ] , dbg )
let rec add_const c n dbg = if n = 0 then c else match c with | Cconst_int ( x , _ ) when Misc . no_overflow_add x n -> Cconst_int ( x + n , dbg ) | Cop ( Caddi , [ Cconst_int ( x , _ ) ; c ] , _ ) when Misc . no_overflow_add n x -> add_no_overflow n x c dbg | Cop ( Cad...
let incr_int c dbg = add_const c 1 dbg
let decr_int c dbg = add_const c ( - 1 ) dbg
let rec add_int c1 c2 dbg = match ( c1 , c2 ) with | ( Cconst_int ( n , _ ) , c ) | ( c , Cconst_int ( n , _ ) ) -> add_const c n dbg | ( Cop ( Caddi , [ c1 ; Cconst_int ( n1 , _ ) ] , _ ) , c2 ) -> add_const ( add_int c1 c2 dbg ) n1 dbg | ( c1 , ...
let rec sub_int c1 c2 dbg = match ( c1 , c2 ) with | ( c1 , Cconst_int ( n2 , _ ) ) when n2 <> min_int -> add_const c1 ( - n2 ) dbg | ( c1 , Cop ( Caddi , [ c2 ; Cconst_int ( n2 , _ ) ] , _ ) ) when n2 <> min_int -> add_const ( sub_int c1 c2 dbg ) ( - n2...
let rec lsl_int c1 c2 dbg = match ( c1 , c2 ) with | ( Cop ( Clsl , [ c ; Cconst_int ( n1 , _ ) ] , _ ) , Cconst_int ( n2 , _ ) ) when n1 > 0 && n2 > 0 && n1 + n2 < size_int * 8 -> Cop ( Clsl , [ c ; Cconst_int ( n1 + n2 , dbg ) ] , dbg ) | ( ...
let is_power2 n = n = 1 lsl Misc . log2 n
let rec mul_int c1 c2 dbg = match ( c1 , c2 ) with | ( c , Cconst_int ( 0 , _ ) ) | ( Cconst_int ( 0 , _ ) , c ) -> Csequence ( c , Cconst_int ( 0 , dbg ) ) | ( c , Cconst_int ( 1 , _ ) ) | ( Cconst_int ( 1 , _ ) , c ) -> c | ( c , C...
let ignore_low_bit_int = function Cop ( Caddi , [ ( Cop ( Clsl , [ _ ; Cconst_int ( n , _ ) ] , _ ) as c ) ; Cconst_int ( 1 , _ ) ] , _ ) when n > 0 -> c | Cop ( Cor , [ c ; Cconst_int ( 1 , _ ) ] , _ ) -> c | c -> c
let ignore_high_bit_int = function Cop ( Casr , [ Cop ( Clsl , [ c ; Cconst_int ( 1 , _ ) ] , _ ) ; Cconst_int ( 1 , _ ) ] , _ ) -> c | c -> c
let lsr_int c1 c2 dbg = match c2 with Cconst_int ( 0 , _ ) -> c1 | Cconst_int ( n , _ ) when n > 0 -> Cop ( Clsr , [ ignore_low_bit_int c1 ; c2 ] , dbg ) | _ -> Cop ( Clsr , [ c1 ; c2 ] , dbg )
let asr_int c1 c2 dbg = match c2 with Cconst_int ( 0 , _ ) -> c1 | Cconst_int ( n , _ ) when n > 0 -> Cop ( Casr , [ ignore_low_bit_int c1 ; c2 ] , dbg ) | _ -> Cop ( Casr , [ c1 ; c2 ] , dbg )
let tag_int i dbg = match i with Cconst_int ( n , _ ) -> int_const dbg n | Cop ( Casr , [ c ; Cconst_int ( n , _ ) ] , _ ) when n > 0 -> Cop ( Cor , [ asr_int c ( Cconst_int ( n - 1 , dbg ) ) dbg ; Cconst_int ( 1 , dbg ) ] , dbg ) | c -> incr_int ( ...
let untag_int i dbg = match i with Cconst_int ( n , _ ) -> Cconst_int ( n asr 1 , dbg ) | Cop ( Cor , [ Cop ( Casr , [ c ; Cconst_int ( n , _ ) ] , _ ) ; Cconst_int ( 1 , _ ) ] , _ ) when n > 0 && n < size_int * 8 - 1 -> Cop ( Casr , [ c ; C...
let mk_if_then_else dbg cond ifso_dbg ifso ifnot_dbg ifnot = match cond with | Cconst_int ( 0 , _ ) -> ifnot | Cconst_int ( 1 , _ ) -> ifso | _ -> Cifthenelse ( cond , ifso_dbg , ifso , ifnot_dbg , ifnot , dbg )
let mk_not dbg cmm = match cmm with | Cop ( Caddi , [ Cop ( Clsl , [ c ; Cconst_int ( 1 , _ ) ] , _ ) ; Cconst_int ( 1 , _ ) ] , dbg ' ) -> begin match c with | Cop ( Ccmpi cmp , [ c1 ; c2 ] , dbg ' ' ) -> tag_int ( Cop ( Ccmpi ( negate_integer...
let mk_compare_ints dbg a1 a2 = match ( a1 , a2 ) with | Cconst_int ( c1 , _ ) , Cconst_int ( c2 , _ ) -> int_const dbg ( Int . compare c1 c2 ) | Cconst_natint ( c1 , _ ) , Cconst_natint ( c2 , _ ) -> int_const dbg ( Nativeint . compare c1 c2 ) | Cconst_int ( c1...
let mk_compare_floats dbg a1 a2 = bind " float_cmp " a2 ( fun a2 -> bind " float_cmp " a1 ( fun a1 -> let op1 = Cop ( Ccmpf ( CFgt ) , [ a1 ; a2 ] , dbg ) in let op2 = Cop ( Ccmpf ( CFlt ) , [ a1 ; a2 ] , dbg ) in let op3 = Cop ( Ccmpf ( CFeq ) , [ a1 ; ...
let create_loop body dbg = let cont = Lambda . next_raise_count ( ) in let call_cont = Cexit ( cont , [ ] ) in let body = Csequence ( body , call_cont ) in Ccatch ( Recursive , [ cont , [ ] , body , dbg ] , call_cont )
let ucompare x y = Nativeint . ( compare ( add x min_int ) ( add y min_int ) )
let udivmod n d = Nativeint . ( if d < 0n then if ucompare n d < 0 then ( 0n , n ) else ( 1n , sub n d ) else begin let q = shift_left ( div ( shift_right_logical n 1 ) d ) 1 in let r = sub n ( mul q d ) in if ucompare r d >= 0 then ( succ q , sub r d ) else ( q , r ) ...
let divimm_parameters d = Nativeint . ( assert ( d > 0n ) ; let twopsm1 = min_int in let nc = sub ( pred twopsm1 ) ( snd ( udivmod twopsm1 d ) ) in let rec loop p ( q1 , r1 ) ( q2 , r2 ) = let p = p + 1 in let q1 = shift_left q1 1 and r1 = shift_left r1 1 in let ( q1 , ...
let raise_symbol dbg symb = Cop ( Craise Lambda . Raise_regular , [ Cconst_symbol ( symb , dbg ) ] , dbg )
let rec div_int c1 c2 is_safe dbg = match ( c1 , c2 ) with ( c1 , Cconst_int ( 0 , _ ) ) -> Csequence ( c1 , raise_symbol dbg " caml_exn_Division_by_zero " ) | ( c1 , Cconst_int ( 1 , _ ) ) -> c1 | ( Cconst_int ( n1 , _ ) , Cconst_int ( n2 , _ ) ) ->...
let mod_int c1 c2 is_safe dbg = match ( c1 , c2 ) with ( c1 , Cconst_int ( 0 , _ ) ) -> Csequence ( c1 , raise_symbol dbg " caml_exn_Division_by_zero " ) | ( c1 , Cconst_int ( ( 1 | ( - 1 ) ) , _ ) ) -> Csequence ( c1 , Cconst_int ( 0 , dbg ) ) | ...
let is_different_from x = function Cconst_int ( n , _ ) -> n <> x | Cconst_natint ( n , _ ) -> n <> Nativeint . of_int x | _ -> false
let safe_divmod_bi mkop is_safe mkm1 c1 c2 bi dbg = bind " divisor " c2 ( fun c2 -> bind " dividend " c1 ( fun c1 -> let c = mkop c1 c2 is_safe dbg in if Arch . division_crashes_on_overflow && ( size_int = 4 || bi <> Primitive . Pint32 ) && not ( is_different_from ( - 1 ) c2 ) th...
let safe_div_bi is_safe = safe_divmod_bi div_int is_safe ( fun c1 dbg -> Cop ( Csubi , [ Cconst_int ( 0 , dbg ) ; c1 ] , dbg ) )
let safe_mod_bi is_safe = safe_divmod_bi mod_int is_safe ( fun _ dbg -> Cconst_int ( 0 , dbg ) )
let test_bool dbg cmm = match cmm with | Cop ( Caddi , [ Cop ( Clsl , [ c ; Cconst_int ( 1 , _ ) ] , _ ) ; Cconst_int ( 1 , _ ) ] , _ ) -> c | Cconst_int ( n , dbg ) -> if n = 1 then Cconst_int ( 0 , dbg ) else Cconst_int ( 1 , dbg ) | c -> Cop ...
let box_float dbg m c = Cop ( Calloc m , [ alloc_float_header m dbg ; c ] , dbg )
let unbox_float dbg = map_tail ( function | Cop ( Calloc _ , [ Cconst_natint ( hdr , _ ) ; c ] , _ ) when Nativeint . equal hdr float_header -> c | Cconst_symbol ( s , _dbg ) as cmm -> begin match Cmmgen_state . structured_constant_of_sym s with | Some ( Uconst_float x ) -...
let box_complex dbg c_re c_im = Cop ( Calloc Lambda . alloc_heap , [ alloc_floatarray_header 2 dbg ; c_re ; c_im ] , dbg )
let complex_re c dbg = Cop ( Cload ( Double , Immutable ) , [ c ] , dbg )
let complex_im c dbg = Cop ( Cload ( Double , Immutable ) , [ Cop ( Cadda , [ c ; Cconst_int ( size_float , dbg ) ] , dbg ) ] , dbg )
let return_unit dbg c = Csequence ( c , Cconst_int ( 1 , dbg ) )
let rec remove_unit = function Cconst_int ( 1 , _ ) -> Ctuple [ ] | Csequence ( c , Cconst_int ( 1 , _ ) ) -> c | Csequence ( c1 , c2 ) -> Csequence ( c1 , remove_unit c2 ) | Cifthenelse ( cond , ifso_dbg , ifso , ifnot_dbg , ifnot , dbg ) -> Cifthenelse ( c...
let field_address ptr n dbg = if n = 0 then ptr else Cop ( Cadda , [ ptr ; Cconst_int ( n * size_addr , dbg ) ] , dbg )
let get_field_gen mut ptr n dbg = Cop ( Cload ( Word_val , mut ) , [ field_address ptr n dbg ] , dbg )
let set_field ptr n newval init dbg = Cop ( Cstore ( Word_val , init ) , [ field_address ptr n dbg ; newval ] , dbg )
let non_profinfo_mask = if Config . profinfo then ( 1 lsl ( 64 - Config . profinfo_width ) ) - 1 else 0
let get_header ptr dbg = Cop ( Cload ( Word_int , Mutable ) , [ Cop ( Cadda , [ ptr ; Cconst_int ( - size_int , dbg ) ] , dbg ) ] , dbg )
let get_header_without_profinfo ptr dbg = if Config . profinfo then Cop ( Cand , [ get_header ptr dbg ; Cconst_int ( non_profinfo_mask , dbg ) ] , dbg ) else get_header ptr dbg
let tag_offset = if big_endian then - 1 else - size_int
let get_tag ptr dbg = if Proc . word_addressed then Cop ( Cand , [ get_header ptr dbg ; Cconst_int ( 255 , dbg ) ] , dbg ) else Cop ( Cload ( Byte_unsigned , Mutable ) , [ Cop ( Cadda , [ ptr ; Cconst_int ( tag_offset , dbg ) ] , dbg ) ] , dbg )
let get_size ptr dbg = Cop ( Clsr , [ get_header_without_profinfo ptr dbg ; Cconst_int ( 10 , dbg ) ] , dbg )