1. Calascibetta Romain
  2. TyLLVM

Commits

Jacques-Pascal Deplaix  committed 82055ad

Add utils functions to create values

  • Participants
  • Parent commits fd542c1
  • Branches master

Comments (0)

Files changed (5)

File examples/hello.ml

View file
  • Ignore whitespace
+let c c = LLVM_types.int 8 (int_of_char c)
+
 let () =
   LLVM.print_context_module
     (LLVM.new_global
        ~link_type:(Some `Internal)
        ~constant:true
        "msg"
-       (LLVM_types.Array (13, LLVM_types.Int 8))
+       (LLVM_types.Value (LLVM_types.array 13 (LLVM_types.Int 8) [c 'h'; c 'e'; c 'l'; c 'l'; c 'o'; c ' '; c 'w'; c 'o'; c 'r'; c 'l'; c 'd'; LLVM_types.int 8 0]))
     );
   LLVM.print_context_module
     (LLVM.new_declaration
       (LLVM_types.Int 32)
       "main"
       [||]
-      [LLVM.Ret (LLVM_types.Int 32, 0, string_of_int)]
+      [LLVM.Ret (LLVM_types.int 32 0)]
     )

File src/LLVM.ml

View file
  • Ignore whitespace
   | Global of
     (string * thread_local option *
     address * link_type option * bool *
-    'a LLVM_types.t * string option * int option)
+    'a LLVM_types.either * string option * int option)
   | Declaration of
     ('a LLVM_types.t * string * 'b LLVM_types.t array *
     [`External | `Dllimport | `Extern_weak ] option)
   in let string_constant = match constant with
     | true                  -> " constant"
     | false                 -> ""
-  in let string_data_type = " " ^ (LLVM_types.to_string data_type)
+  in let string_data_type = match data_type with
+    | LLVM_types.Value v -> " " ^ LLVM_types.value_to_string v
+    | LLVM_types.Type t -> " " ^ LLVM_types.to_string t
   in let string_section = match section with
     | None                  -> ""
     | Some a                -> ", section \"" ^ a ^ "\""

File src/LLVM.mli

View file
  • Ignore whitespace
   | Global of
     (string * thread_local option *
     address * link_type option * bool *
-    'a LLVM_types.t * string option * int option)
+    'a LLVM_types.either * string option * int option)
   | Declaration of
     ('a LLVM_types.t * string * 'b LLVM_types.t array *
     [`External | `Dllimport | `Extern_weak ] option)
   ?constant: bool ->
   ?section: string option ->
   ?alignment: int option ->
-  string -> 'a LLVM_types.t -> ('a, 'b, 'c) context_module
+  string -> 'a LLVM_types.either -> ('a, 'b, 'c) context_module
 
 val new_declaration:
   ?link_type: [< `External | `Dllimport | `Extern_weak ] option ->

File src/LLVM_types.ml

View file
  • Ignore whitespace
 
 and _ t =
   | Int : int -> int t
-  | Array : int * 'a t -> 'a t array t
+  | Array : int * 'a t -> 'a value list t
   | Pointer : 'a t -> 'a t t
   | FunctionPointer : 'a f -> 'a f t
 
-type 'a value = 'a t * 'a * ('a -> string)
+and 'a value = 'a t * string
+
+type 'a either =
+  | Value of 'a value
+  | Type of 'a t
 
 let rec to_string : type a. a t -> string = fun data ->
   let rec aux : type a. string -> a f -> string = fun acc -> function
     | Pointer t -> to_string t ^ "*"
     | FunctionPointer f -> aux "" f
 
-let value_to_string (t, x, f) = to_string t ^ " " ^ f x
+let value_to_string (t, value) = to_string t ^ " " ^ value
+
+let int x value = (Int x, string_of_int value)
+let array n t value =
+  (Array (n, t), "[" ^ String.concat ", " (List.map value_to_string value) ^ "]")
+let pointer t value = (Pointer t, to_string value)

File src/LLVM_types.mli

View file
  • Ignore whitespace
 
 and _ t =
   | Int : int -> int t
-  | Array : int * 'a t -> 'a t array t
+  | Array : int * 'a t -> 'a value list t
   | Pointer : 'a t -> 'a t t
   | FunctionPointer : 'a f -> 'a f t
 
-type 'a value = 'a t * 'a * ('a -> string)
+and 'a value = 'a t * string
+
+type 'a either =
+  | Value of 'a value
+  | Type of 'a t
 
 val to_string : 'a t -> string
 val value_to_string : 'a value -> string
+
+val int : int -> int -> int value
+val array : int -> 'a t -> 'a value list -> 'a value list value
+val pointer : 'a t -> 'a t -> 'a t value