Commits

Calascibetta Romain committed 5b2dd11

First step for definition of function

Comments (0)

Files changed (3)

   | Sspstrong
   | Uwtable
 
+type instr =
+  | Ret
+
 type context_module =
   | Global of
     (string * thread_local option *
   | Declaration of
     (data_type * string * data_type array *
     [`External | `Dllimport | `Extern_weak ] option)
-
+  | Definition of
+    ((data_type * string * data_type array) * instr list)
 
 let string_of_link_type = function
-  | `Private                 -> "private"
-  | `Linker_private          -> "linker_private"
-  | `Linker_private_weak     -> "linker_private_weak"
-  | `Internal                -> "internal"
-  | `Available_externally    -> "available_externally"
-  | `Linkonce                -> "linkonce"
-  | `Weak                    -> "weak"
-  | `Common                  -> "common"
-  | `Appending               -> "appending"
-  | `Extern_weak             -> "extern_weak"
-  | `Linkonce_odr            -> "linkonce_odr"
-  | `Weak_odr                -> "weak_odr"
-  | `Linkonce_odr_auto_hide  -> "linkonce_odr_auto_hide"
-  | `External                -> "external"
-  | `Dllimport               -> "dllimport"
-  | `Dllexport               -> "dllexport"
+  | `Private                -> "private"
+  | `Linker_private         -> "linker_private"
+  | `Linker_private_weak    -> "linker_private_weak"
+  | `Internal               -> "internal"
+  | `Available_externally   -> "available_externally"
+  | `Linkonce               -> "linkonce"
+  | `Weak                   -> "weak"
+  | `Common                 -> "common"
+  | `Appending              -> "appending"
+  | `Extern_weak            -> "extern_weak"
+  | `Linkonce_odr           -> "linkonce_odr"
+  | `Weak_odr               -> "weak_odr"
+  | `Linkonce_odr_auto_hide -> "linkonce_odr_auto_hide"
+  | `External               -> "external"
+  | `Dllimport              -> "dllimport"
+  | `Dllexport              -> "dllexport"
 
 let rec string_of_data_type ?f:(name = "") data =
   let rec aux acc arr = function
       (Array.length a - 1))
     ^ ") *"
 
+let string_of_instr = function
+  | Ret                     -> "\tret"
+
 let string_of_address = function
   | Addrspace i             -> "addr(" ^ (string_of_int i) ^ ")"
   | Unnamed_addr            -> "unnamed_addr"
 
 let string_of_declaration
   (type_return, id, type_arguments, link_type) =
-  let rec aux acc arr = function
-    | 0 -> acc
-    | n -> aux (acc ^ ", " ^ (string_of_data_type arr.(n))) arr (n - 1)
+  let rec aux acc arr n =
+    if n <= 0 then acc
+    else aux (acc ^ ", " ^ (string_of_data_type arr.(n))) arr (n - 1)
   in let string_type_return = (string_of_data_type type_return)
   in let string_type_arguments =
     aux (if Array.length type_arguments > 0
   in "declare " ^ string_type_return ^ " @" ^ id
   ^ "(" ^ string_type_arguments ^ ")" ^ string_link_type
 
+let string_of_definition
+  ((type_return, id, type_arguments), instr_list) =
+  let rec aux acc arr n =
+    if n <= 0 then acc
+    else aux (acc ^ ", " ^ (string_of_data_type arr.(n))) arr (n - 1)
+  in let string_type_return = (string_of_data_type type_return)
+  in let string_type_arguments =
+    aux (if Array.length type_arguments > 0
+      then (string_of_data_type type_arguments.(0))
+      else "") type_arguments (Array.length type_arguments - 1)
+  in let string_instr_list =
+    List.fold_left (fun a x -> a ^ (string_of_instr x) ^ "\n") "" instr_list
+  in "define " ^ string_type_return ^ " @" ^ id
+  ^ "(" ^ string_type_arguments ^ ")" ^ " {\n" ^ string_instr_list ^ "}"
+
 let print_global a = print_string (string_of_global a)
 
 let new_global
   Declaration (type_return, id, type_arguments,
     (link_type :> [ `External | `Dllimport | `Extern_weak] option))
 
+let print_definition a = print_string (string_of_definition a)
+
+let new_definition
+  type_return id type_arguments instr_list =
+  Definition ((type_return, id, type_arguments), instr_list)
+
 let print_context_module e =
   let aux = function
-    | Global a -> print_global a
-    | Declaration a -> print_declaration a
+    | Global a              -> print_global a
+    | Declaration a         -> print_declaration a
+    | Definition a          -> print_definition a
   in aux e; print_newline ()
   | Sspstrong
   | Uwtable
 
+type instr =
+  | Ret
+
 type context_module =
   | Global of
     (string * thread_local option *
   | Declaration of
     (data_type * string * data_type array *
     [`External | `Dllimport | `Extern_weak ] option)
+  | Definition of
+    ((data_type * string * data_type array) * instr list)
 
 (*
  *  Global:
   ?link_type: [< `External | `Dllimport | `Extern_weak ] option ->
   data_type -> string -> data_type array -> context_module
 
+val new_definition:
+  data_type -> string -> data_type array -> instr list -> context_module
+
 val print_context_module: context_module -> unit
   LLVM.print_context_module
     (LLVM.new_declaration
       (LLVM.Int 32) "puts" [| LLVM.Pointer (LLVM.Int 8); LLVM.Int 32 |]
-      ~link_type: (Some `External))
+      ~link_type: (Some `External));
+  LLVM.print_context_module
+    (LLVM.new_definition
+      (LLVM.Int 32) "main" [||] [LLVM.Ret])