Source

TyLLVM / LLVM_types.ml

Full commit
type 'a t = {data : 'a}

type llvm_int = [`Int of (int * Big_int.big_int option)]
type 'a llvm_array = [`Array of (int * 'a t)]
type 'a llvm_pointer = [`Pointer of ('a t * int option)]
type ('a, 'b) llvm_function_pointer = [`Function_pointer of ('a t array * 'b t * int option)]

let int i x = {data = `Int (i, x)}
let array i x = {data = `Array (i, x)}
let pointer t x = {data = `Pointer (t, x)}
let function_pointer params return_type x = {data = `Function_pointer (params, return_type, x)}

let rec to_string data =
  let rec aux acc arr = function
    | 0 -> acc
    | n -> aux (acc ^ ", " ^ to_string arr.(n).data) arr (pred n)
  in
  match data with
    | `Int (i, _) -> "i" ^ string_of_int i
    | `Pointer (t, _) -> to_string t.data ^ "*"
    | `Array (i, t) ->
        "[" ^ string_of_int i ^ " x " ^ to_string t.data ^ "]"
    | `Function_pointer (a, t, _) ->
        to_string t.data ^ " @("
        ^ aux
          (if Array.length a > 0 then to_string a.(0).data else "")
          a
          (Array.length a - 1)
        ^ ") *"

let to_string data = to_string data.data