text stringlengths 12 786k |
|---|
let rec combine l1 l2 = match ( l1 , l2 ) with ( [ ] , [ ] ) -> [ ] | ( a1 :: l1 , a2 :: l2 ) -> ( a1 , a2 ) :: combine l1 l2 | ( _ , _ ) -> invalid_arg " List . combine " |
let rec merge cmp l1 l2 = match l1 , l2 with | [ ] , l2 -> l2 | l1 , [ ] -> l1 | h1 :: t1 , h2 :: t2 -> if cmp h1 h2 <= 0 then h1 :: merge cmp t1 l2 else h2 :: merge cmp l1 t2 |
let stable_sort cmp l = let rec rev_merge l1 l2 accu = match l1 , l2 with | [ ] , l2 -> rev_append l2 accu | l1 , [ ] -> rev_append l1 accu | h1 :: t1 , h2 :: t2 -> if cmp h1 h2 <= 0 then rev_merge t1 l2 ( h1 :: accu ) else rev_merge l1 t2 ( h2 :: accu ) in let rec rev_merge_rev l1... |
let sort_uniq cmp l = let rec rev_merge l1 l2 accu = match l1 , l2 with | [ ] , l2 -> rev_append l2 accu | l1 , [ ] -> rev_append l1 accu | h1 :: t1 , h2 :: t2 -> let c = cmp h1 h2 in if c = 0 then rev_merge t1 t2 ( h1 :: accu ) else if c < 0 then rev_merge t1 l2 ( h1 :: accu ) ... |
let rec compare_lengths l1 l2 = match l1 , l2 with | [ ] , [ ] -> 0 | [ ] , _ -> - 1 | _ , [ ] -> 1 | _ :: l1 , _ :: l2 -> compare_lengths l1 l2 ; ; |
let rec compare_length_with l n = match l with | [ ] -> if n = 0 then 0 else if n > 0 then - 1 else 1 | _ :: l -> if n <= 0 then 1 else compare_length_with l ( n - 1 ) ; ; |
let rec equal eq l1 l2 = match l1 , l2 with | [ ] , [ ] -> true | [ ] , _ :: _ | _ :: _ , [ ] -> false | a1 :: l1 , a2 :: l2 -> eq a1 a2 && equal eq l1 l2 |
let rec compare cmp l1 l2 = match l1 , l2 with | [ ] , [ ] -> 0 | [ ] , _ :: _ -> - 1 | _ :: _ , [ ] -> 1 | a1 :: l1 , a2 :: l2 -> let c = cmp a1 a2 in if c <> 0 then c else compare cmp l1 l2 |
let to_seq l = let rec aux l ( ) = match l with | [ ] -> Seq . Nil | x :: tail -> Seq . Cons ( x , aux tail ) in aux l |
let of_seq seq = let rec direct depth seq : _ list = if depth = 0 then Seq . fold_left ( fun acc x -> x :: acc ) [ ] seq |> rev else match seq ( ) with | Seq . Nil -> [ ] | Seq . Cons ( x , next ) -> x :: direct ( depth - 1 ) next in direct 500 seq |
module type Integer = sig type t val zero : t val logand : t -> t -> t val of_int : int -> t val to_int : t -> int val shift_right_logical : t -> int -> t val shift_left : t -> int -> t val add : t -> t -> t end |
module LittleEndian = struct let set_int ( type t ) ( module T : Integer with type t = t ) size buffer offset v = let open T in let rec inner acc = function | n when n = size -> ( ) | n -> let ch = logand acc ( of_int 0xff ) |> to_int |> Char . chr in Bytes . set buffer ( n + offset... |
module BigEndian = struct let set_int ( type t ) ( module T : Integer with type t = t ) size buffer offset v = let open T in let rec inner acc = function | n when n = size -> ( ) | n -> let ch = logand acc ( of_int 0xff ) |> to_int |> Char . chr in Bytes . set buffer ( ( size - n ... |
let get_int64 = match Sys . big_endian with true -> BigEndian . get_int64 | false -> LittleEndian . get_int64 |
let get_int32 = match Sys . big_endian with true -> BigEndian . get_int32 | false -> LittleEndian . get_int32 |
let set_int64 = match Sys . big_endian with true -> BigEndian . set_int64 | false -> LittleEndian . set_int64 |
let set_int32 = match Sys . big_endian with true -> BigEndian . set_int32 | false -> LittleEndian . set_int32 |
module Test = struct let test _ = let ( _ : bool ) = let buffer = Bytes . create 8 in let v = 1234567876543L in set_int64 buffer 0 v ; get_int64 ( Bytes . to_string buffer ) 0 = v || failwith " int64 " in let ( _ : bool ) = let buffer = Bytes . create 4 in let v = 12345673l ... |
let live_at_exit = ref [ ] |
let find_live_at_exit k = try List . assoc k ! live_at_exit with | Not_found -> Misc . fatal_error " Liveness . find_live_at_exit " |
let live_at_raise = ref Reg . Set . empty |
let rec live i finally = match i . desc with Iend -> i . live <- finally ; finally | Ireturn | Iop ( Itailcall_ind ) | Iop ( Itailcall_imm _ ) -> i . live <- Reg . Set . empty ; Reg . set_of_array i . arg | Iop op -> let after = live i . next finally in if operation_is_pure op &... |
let reset ( ) = live_at_raise := Reg . Set . empty ; live_at_exit := [ ] |
let fundecl f = let initially_live = live f . fun_body Reg . Set . empty in let wrong_live = Reg . Set . diff initially_live ( Reg . set_of_array f . fun_args ) in if not ( Reg . Set . is_empty wrong_live ) then begin Misc . fatal_errorf " [ @ Liveness . fundecl :@\ n % a ] " ... |
let label_counter = ref 0 |
let c ( ) = label_counter := ! label_counter + 1 ; string_of_int ! label_counter |
let types = Hashtbl . create 10 |
let is_addr = function Address _ -> true | _ -> false |
let is_float = function Double -> true | _ -> false |
let is_int = function Integer _ -> true | _ -> false |
let vars = Hashtbl . create 10 |
let is_def = Hashtbl . mem vars |
let translate_op = function | Caddi -> " add " | Csubi -> " sub " | Cmuli -> " mul " | Cdivi -> " sdiv " | Cmodi -> " srem " | Cand -> " and " | Cor -> " or " | Cxor -> " xor " | Clsl -> " shl " | Clsr -> " lshr " | Casr -> " ashr " | Caddf -> " fad... |
let translate_mem = function | Byte_unsigned | Byte_signed -> Integer 8 | Sixteen_unsigned | Sixteen_signed -> Integer 16 | Thirtytwo_unsigned | Thirtytwo_signed -> Integer 32 | Word -> int_type | Single | Cmm . Double | Double_u -> Double |
let translate_fcomp = function | Ceq -> " oeq " | Cne -> " one " | Clt -> " olt " | Cle -> " ole " | Cgt -> " ogt " | Cge -> " oge " |
let translate_icomp = function | Ceq -> " eq " | Cne -> " ne " | Clt -> " slt " | Cle -> " sle " | Cgt -> " sgt " | Cge -> " sge " |
let translate_ucomp = function | Ceq -> " eq " | Cne -> " ne " | Clt -> " ult " | Cle -> " ule " | Cgt -> " ugt " | Cge -> " uge " |
let translate_symbol s = let result = ref " " in for i = 0 to String . length s - 1 do let c = s . [ i ] in match c with ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' | ' _ ' -> result := ! result ^ Printf . sprintf " % c " c | _ -> re... |
let translate_machtype = function | [ | Addr ] | -> addr_type | [ | Int ] | -> addr_type | [ | Float ] | -> addr_type | _ -> error " unknown type " |
let rec cmm_to_string = function | Cconst_int i -> " ( int " ^ string_of_int i ^ " ) " | Cconst_natint i -> " ( int " ^ Nativeint . to_string i ^ " ) " | Cconst_float f -> " ( float " ^ f ^ " ) " | Cconst_symbol s -> " ( symb " ^ s ^ " ) " | Cconst_poi... |
let cast value dest_typ = let typ = typeof value in match typ , dest_typ with | ( Integer i , Integer j ) -> if i == j then value else if i < j then Lzext ( value , typ , dest_typ ) else Ltrunc ( value , typ , dest_typ ) | ( Integer i , Address _ ) -> if i == size_int then Lintto... |
let alloca name typ = Hashtbl . add vars name ( ) ; Hashtbl . add types name ( Address typ ) ; Lalloca ( name , typ ) |
let const x y = x |
let load addr = Lload ( cast addr ( if is_addr ( typeof addr ) then typeof addr else ( Address ( typeof addr ) ) ) ) |
let local_load addr typ = Llocal_load ( addr , typ ) |
let store value addr = Lstore ( cast value ( deref ( typeof addr ) ) , addr ) |
let store_non_void arg addr = if typeof arg == Void then arg else store arg addr |
let load_if_necessary typ value = if typ == typeof value then ( value ) else if typ == Address ( typeof value ) then ( load value ) else ( cast value typ ) |
let binop op typ left right = Lbinop ( op , typ , load_if_necessary typ left , load_if_necessary typ right ) |
let comp op typ left right = Lcomp ( op , typ , load_if_necessary typ left , load_if_necessary typ right ) |
let getelementptr addr offset = Lgetelementptr ( const addr addr_type , offset ) |
let load_exn_ptr ( ) = local_load " % exn_ptr " ( Address addr_type ) |
let load_young_ptr ( ) = local_load " % young_ptr " ( Address addr_type ) |
let call fn args = Lcall ( Return_type , fn , load_exn_ptr ( ) :: load_young_ptr ( ) :: args ) |
let ccall typ fn arg_list = Lccall ( typ , fn , arg_list ) |
let voidcall fn args = Lcall ( Void , fn , load_exn_ptr ( ) :: load_young_ptr ( ) :: args ) |
let return value = Lreturn ( const value addr_type , addr_type ) |
let rec caml_type expect = function | Cconst_int _ -> int_type | Cconst_natint _ -> int_type | Cconst_float _ -> Double | Cconst_symbol _ -> addr_type | Cconst_pointer _ -> addr_type | Cconst_natpointer _ -> addr_type | Cvar id -> let name = Ident . unique_name id in if expect != Any && not ( ... |
let rec compile_expr instr = match instr with | Cconst_int i -> Lconst ( string_of_int i , int_type ) | Cconst_natint i -> Lconst ( Nativeint . to_string i , int_type ) | Cconst_float f -> Lconst ( f , Double ) | Cconst_symbol s -> add_const s ; Lconst ( " " @ ^ translate_symbol s... |
let argument_list = List . map ( fun ( id , _ ) -> " . % param . " ^ id , addr_type ) |
let compile_fundecl fd_cmm = label_counter := 0 ; let name = fd_cmm . fun_name in let args = fd_cmm . fun_args in let body = fd_cmm . fun_body in Printf . printf " compiling % s \ n " name ; Hashtbl . clear types ; ignore ( caml_type Any body ) ; let args = ( " exn_ptr " , add... |
let data d = emit_function_declarations ( ) ; emit_constant_declarations ( ) ; functions := [ ] ; constants := [ ] |
type ' a error = Just of ' a | Error of string |
let error str = raise ( Llvm_error str ) |
let emit_string s = emit_string ( s ^ " \ n " ) |
let counter = ref 0 |
let size_int = 8 * Arch . size_int |
let size_float = 8 * Arch . size_float |
type llvm_type = | Integer of int | Double | Address of llvm_type | Return_type | Void | Any | Function of llvm_type * llvm_type list |
let int_type = Integer size_int |
let addr_type = Address int_type |
let float_sized_int = Integer size_float |
let counter_inc ( ) = counter := ! counter + 1 |
let c ( ) = counter_inc ( ) ; " . " ^ string_of_int ! counter |
let ret_type = " { i " ^ string_of_int size_int ^ " , * i " ^ string_of_int size_int ^ " , * i " ^ string_of_int size_int ^ " } " * |
let rec typename = function | Integer i -> " i " ^ string_of_int i | Double -> " double " | Address typ -> typename typ ^ " " * | Return_type -> ret_type | Void -> " void " | Any -> " any " | Function ( ret , args ) -> typename ret ^ " ( " ^ String . concat " , ... |
let deref typ = match typ with | Address typ -> typ | _ -> error ( " trying to dereference non - pointer type " ^ typename typ ) |
type llvm_instr = | Llocal_load of string * llvm_type | Llocal_store of string * llvm_type | Lalias of string * llvm_instr | Lbinop of string * llvm_type * llvm_instr * llvm_instr | Lcomp of string * llvm_type * llvm_instr * llvm_instr | Lunop of string * llvm_type * llvm_instr | Lalloca of string *... |
let rec typeof = function | Llocal_load ( _ , typ ) -> deref typ | Llocal_store ( _ , typ ) -> typ | Lalias ( _ , value ) -> typeof value | Lbinop ( _ , typ , _ , _ ) -> typ | Lcomp ( _ , _ , _ , _ ) -> Integer 1 | Lunop ( _ , typ , _ ) -> typ | L... |
let ( ) @@ a b = Lseq ( a , b ) |
let translate_symbol s = let result = ref " " in for i = 0 to String . length s - 1 do let c = s . [ i ] in match c with ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' | ' _ ' -> result := ! result ^ Printf . sprintf " % c " c | _ -> re... |
let types = Hashtbl . create 10 |
let llvm_instrs : llvm_instr list ref = ref [ ] |
let ( ) >>= value fn = match value with | Just value -> fn value | Error s -> Error s |
let just fn = fun x -> Just ( fn x ) |
let translate_symbol s = let result = ref " " in for i = 0 to String . length s - 1 do let c = s . [ i ] in match c with ' ' -> result := ! result ^ " _ " | _ -> result := ! result ^ Printf . sprintf " % c " c done ; ! result |
let rec emit_llvm instr = let ( ) ++ a b = match a with Just a -> begin match b with Just b -> Just ( a , b ) | Error e -> Error e end | Error e -> Error e in let emit_op op typ arg = let res = " " % ^ translate_symbol op ^ " _res " ^ c ( ) in emit_string ( " \ t " ^ res ^... |
let header = [ " ; vim : set ft = llvm " ; : " declare double @ fabs ( double ) " ; " declare void @ llvm . stackrestore ( i8 ) " ; * " declare i8 * @ llvm . stacksave ( ) " ; " @ ret_ptr = global i8 0 " ; " declare void @ caml_raise_exn ( i64 , * i6... |
let emit_header ( ) = List . iter emit_string header |
let constants : string list ref = ref [ ] |
let functions : ( string * string * string list ) list ref = ref [ ] |
let add_const str = if List . exists ( fun x -> String . compare x str == 0 ) ! constants then ( ) else constants := str :: ! constants |
let add_function ( ret , str , args ) = if List . exists ( fun ( _ , x , _ ) -> String . compare x str == 0 ) ! functions then ( ) else functions := ( typename ret , str , List . map ( fun _ -> " i " ^ string_of_int size_int ^ " " ) * args ) :: ! function... |
let emit_function_declarations ( ) = List . iter ( fun ( ret_type , name , args ) -> emit_string ( " declare " ^ ret_type ^ " " @ ^ name ^ " ( " ^ String . concat " , " args ^ " ) nounwind " ) ) ! functions |
let emit_constant_declarations ( ) = List . iter ( fun name -> emit_string ( " " @ ^ name ^ " = external global " ^ typename int_type ) ) ! constants |
let rec to_string = function | Llocal_load ( name , typ ) -> " ( local_load " ^ typename typ ^ " " ^ name ^ " ) " | Llocal_store ( name , typ ) -> " ( local_store " ^ typename typ ^ " " ^ name ^ " ) " | Lalias ( name , foo ) -> " ( alias " ^ name ... |
module type Thing = sig type t val equal : t -> t -> bool val print : Format . formatter -> t -> unit end |
module type S = sig type key type ' + a t val empty : ' a t val is_empty : ' a t -> bool val add : key -> ' a -> ' a t -> ' a t val singleton : key -> ' a -> ' a t val disjoint_union : ' a t -> ' a t -> ' a t val disjoint_union_many : ' a t list -> ' a t val iter : ( key -... |
module Make ( T : Thing ) : S with type key = T . t = struct type key = T . t type ' + a t = ( key * ' a ) list let empty = [ ] let is_empty m = m = [ ] let add k v m = ( k , v ) :: m let singleton k v = [ k , v ] let disjoint_union m1 m2 = m1 @ m2 let disjoint_union_... |
type error = | Load_failure of Dynlink . error | Unbound_identifier of Longident . t | Unavailable_module of string * Longident . t | Wrong_type of Longident . t | No_active_printer of Longident . t |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.