Adding bindings for target triple and data layout.
[oota-llvm.git] / test / Bindings / Ocaml / vmcore.ml
index 434536b79e4e6f50f7bec549507811a27409397b..6b103cb8fc985c51987c1550e8a5cc14a832d978 100644 (file)
@@ -1,4 +1,4 @@
-(* RUN: %ocamlc llvm.cma llvm_bitwriter.cma %s -o %t
+(* RUN: %ocamlc -warn-error A llvm.cma llvm_analysis.cma llvm_bitwriter.cma %s -o %t
  * RUN: ./%t %t.bc
  * RUN: llvm-dis < %t.bc > %t.ll
  *)
@@ -21,11 +21,9 @@ let group name =
 
 let insist cond =
   incr case_num;
-  let msg = if cond then "    pass " else begin
-    exit_status := 10;
-    "    FAIL "
-  end in
-  prerr_endline (msg ^ (string_of_int !case_num))
+  if not cond then exit_status := 10;
+  prerr_endline ("    " ^ (string_of_int !case_num) ^ if cond then ""
+                                                              else " FAIL")
 
 let suite name f =
   prerr_endline (name ^ ":");
@@ -38,6 +36,25 @@ let filename = Sys.argv.(1)
 let m = create_module filename
 
 
+(*===-- Target ------------------------------------------------------------===*)
+
+let test_target () =
+  begin group "triple";
+    (* RUN: grep "i686-apple-darwin8" < %t.ll
+     *)
+    let trip = "i686-apple-darwin8" in
+    set_target_triple trip m;
+    insist (trip = target_triple m)
+  end;
+  
+  begin group "layout";
+    (* RUN: grep "bogus" < %t.ll
+     *)
+    let layout = "bogus" in
+    set_data_layout layout m;
+    insist (layout = data_layout m)
+  end
+
 (*===-- Types -------------------------------------------------------------===*)
 
 let test_types () =
@@ -45,13 +62,13 @@ let test_types () =
    *)
   group "void";
   insist (define_type_name "Ty01" void_type m);
-  insist (Void_type == classify_type void_type);
+  insist (TypeKind.Void == classify_type void_type);
 
   (* RUN: grep {Ty02.*i1} < %t.ll
    *)
   group "i1";
   insist (define_type_name "Ty02" i1_type m);
-  insist (Integer_type == classify_type i1_type);
+  insist (TypeKind.Integer == classify_type i1_type);
 
   (* RUN: grep {Ty03.*i32} < %t.ll
    *)
@@ -68,20 +85,20 @@ let test_types () =
    *)
   group "float";
   insist (define_type_name "Ty05" float_type m);
-  insist (Float_type == classify_type float_type);
+  insist (TypeKind.Float == classify_type float_type);
 
   (* RUN: grep {Ty06.*double} < %t.ll
    *)
   group "double";
   insist (define_type_name "Ty06" double_type m);
-  insist (Double_type == classify_type double_type);
+  insist (TypeKind.Double == classify_type double_type);
 
   (* RUN: grep {Ty07.*i32.*i1, double} < %t.ll
    *)
   group "function";
   let ty = function_type i32_type [| i1_type; double_type |] in
   insist (define_type_name "Ty07" ty m);
-  insist (Function_type = classify_type ty);
+  insist (TypeKind.Function = classify_type ty);
   insist (not (is_var_arg ty));
   insist (i32_type == return_type ty);
   insist (double_type == (param_types ty).(1));
@@ -100,15 +117,26 @@ let test_types () =
   insist (define_type_name "Ty09" ty m);
   insist (7 = array_length ty);
   insist (i8_type == element_type ty);
-  insist (Array_type == classify_type ty);
+  insist (TypeKind.Array == classify_type ty);
   
-  (* RUN: grep {Ty10.*float\*} < %t.ll
-   *)
-  group "pointer";
-  let ty = pointer_type float_type in
-  insist (define_type_name "Ty10" ty m);
-  insist (float_type == element_type ty);
-  insist (Pointer_type == classify_type ty);
+  begin group "pointer";
+    (* RUN: grep {UnqualPtrTy.*float\*} < %t.ll
+     *)
+    let ty = pointer_type float_type in
+    insist (define_type_name "UnqualPtrTy" ty m);
+    insist (float_type == element_type ty);
+    insist (0 == address_space ty);
+    insist (TypeKind.Pointer == classify_type ty)
+  end;
+  
+  begin group "qualified_pointer";
+    (* RUN: grep {QualPtrTy.*i8.*3.*\*} < %t.ll
+     *)
+    let ty = qualified_pointer_type i8_type 3 in
+    insist (define_type_name "QualPtrTy" ty m);
+    insist (i8_type == element_type ty);
+    insist (3 == address_space ty)
+  end;
   
   (* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll
    *)
@@ -131,7 +159,17 @@ let test_types () =
   group "delete";
   let ty = opaque_type () in
   insist (define_type_name "Ty13" ty m);
-  delete_type_name "Ty13" m
+  delete_type_name "Ty13" m;
+  
+  (* RUN: grep -v {RecursiveTy.*RecursiveTy} < %t.ll
+   *)
+  group "recursive";
+  let ty = opaque_type () in
+  let th = handle_to_type ty in
+  refine_type ty (pointer_type ty);
+  let ty = type_of_handle th in
+  insist (define_type_name "RecursiveTy" ty m);
+  insist (ty == element_type ty)
 
 
 (*===-- Constants ---------------------------------------------------------===*)
@@ -267,8 +305,8 @@ let test_constants () =
   ignore (define_global "ConstAnd" (const_and foldbomb five) m);
   ignore (define_global "ConstOr" (const_or foldbomb five) m);
   ignore (define_global "ConstXor" (const_xor foldbomb five) m);
-  ignore (define_global "ConstICmp" (const_icmp Icmp_sle foldbomb five) m);
-  ignore (define_global "ConstFCmp" (const_fcmp Fcmp_ole ffoldbomb ffive) m);
+  ignore (define_global "ConstICmp" (const_icmp Icmp.Sle foldbomb five) m);
+  ignore (define_global "ConstFCmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m);
   
   group "constant casts";
   (* RUN: grep {ConstTrunc.*trunc} < %t.ll
@@ -314,7 +352,7 @@ let test_constants () =
   ignore (define_global "ConstSizeOf" (size_of (pointer_type i8_type)) m);
   ignore (define_global "ConstGEP" (const_gep foldbomb_gv [| five |]) m);
   ignore (define_global "ConstSelect" (const_select
-    (const_icmp Icmp_sle foldbomb five)
+    (const_icmp Icmp.Sle foldbomb five)
     (const_int i8_type (-1))
     (const_int i8_type 0)) m);
   let zero = const_int i32_type 0 in
@@ -349,8 +387,8 @@ let test_global_values () =
    *)
   group "linkage";
   let g = define_global "GVal02" zero32 m ++
-          set_linkage Link_once_linkage in
-  insist (Link_once_linkage = linkage g);
+          set_linkage Linkage.Link_once in
+  insist (Linkage.Link_once = linkage g);
 
   (* RUN: grep {GVal03.*Hanalei} < %t.ll
    *)
@@ -363,8 +401,8 @@ let test_global_values () =
    *)
   group "visibility";
   let g = define_global "GVal04" zero32 m ++
-          set_visibility Hidden_visibility in
-  insist (Hidden_visibility = visibility g);
+          set_visibility Visibility.Hidden in
+  insist (Visibility.Hidden = visibility g);
   
   (* RUN: grep {GVal05.*align 128} < %t.ll
    *)
@@ -383,8 +421,14 @@ let test_global_variables () =
   (* RUN: grep {GVar01.*external} < %t.ll
    *)
   group "declarations";
+  insist (None == lookup_global "GVar01" m);
   let g = declare_global i32_type "GVar01" m in
   insist (is_declaration g);
+  insist (pointer_type float_type ==
+            type_of (declare_global float_type "GVar01" m));
+  insist (g == declare_global i32_type "GVar01" m);
+  insist (match lookup_global "GVar01" m with Some x -> x = g
+                                            | None -> false);
   
   (* RUN: grep {GVar02.*42} < %t.ll
    * RUN: grep {GVar03.*42} < %t.ll
@@ -408,22 +452,36 @@ let test_global_variables () =
    *)
   group "delete";
   let g = define_global "GVar05" fourty_two32 m in
-  delete_global g
+  delete_global g;
+
+  (* RUN: grep -v {ConstGlobalVar.*constant} < %t.ll
+   *)
+  group "constant";
+  let g = define_global "ConstGlobalVar" fourty_two32 m in
+  insist (not (is_global_constant g));
+  set_global_constant true g;
+  insist (is_global_constant g)
 
 
 (*===-- Functions ---------------------------------------------------------===*)
 
 let test_functions () =
   let ty = function_type i32_type [| i32_type; i64_type |] in
-  let pty = pointer_type ty in
+  let ty2 = function_type i8_type [| i8_type; i64_type |] in
   
   (* RUN: grep {declare i32 @Fn1\(i32, i64\)} < %t.ll
    *)
   group "declare";
+  insist (None = lookup_function "Fn1" m);
   let fn = declare_function "Fn1" ty m in
-  insist (pty = type_of fn);
+  insist (pointer_type ty = type_of fn);
   insist (is_declaration fn);
   insist (0 = Array.length (basic_blocks fn));
+  insist (pointer_type ty2 == type_of (declare_function "Fn1" ty2 m));
+  insist (fn == declare_function "Fn1" ty m);
+  insist (None <> lookup_function "Fn1" m);
+  insist (match lookup_function "Fn1" m with Some x -> x = fn
+                                           | None -> false);
   
   (* RUN: grep -v {Fn2} < %t.ll
    *)
@@ -437,7 +495,7 @@ let test_functions () =
   let fn = define_function "Fn3" ty m in
   insist (not (is_declaration fn));
   insist (1 = Array.length (basic_blocks fn));
-  (* this function is not valid because init bb lacks a terminator *)
+  ignore (build_unreachable (builder_at_end (entry_block fn)));
   
   (* RUN: grep {define.*Fn4.*Param1.*Param2} < %t.ll
    *)
@@ -451,15 +509,29 @@ let test_functions () =
   insist (i64_type = type_of params.(1));
   set_value_name "Param1" params.(0);
   set_value_name "Param2" params.(1);
-  (* this function is not valid because init bb lacks a terminator *)
+  ignore (build_unreachable (builder_at_end (entry_block fn)));
   
   (* RUN: grep {fastcc.*Fn5} < %t.ll
    *)
   group "callconv";
   let fn = define_function "Fn5" ty m in
-  insist (ccc = function_call_conv fn);
-  set_function_call_conv fastcc fn;
-  insist (fastcc = function_call_conv fn)
+  insist (CallConv.c = function_call_conv fn);
+  set_function_call_conv CallConv.fast fn;
+  insist (CallConv.fast = function_call_conv fn);
+  ignore (build_unreachable (builder_at_end (entry_block fn)));
+  
+  begin group "collector";
+    (* RUN: grep {Fn6.*gc.*shadowstack} < %t.ll
+     *)
+    let fn = define_function "Fn6" ty m in
+    insist (None = collector fn);
+    set_collector (Some "ocaml") fn;
+    insist (Some "ocaml" = collector fn);
+    set_collector None fn;
+    insist (None = collector fn);
+    set_collector (Some "shadowstack") fn;
+    ignore (build_unreachable (builder_at_end (entry_block fn)));
+  end
 
 
 (*===-- Basic Blocks ------------------------------------------------------===*)
@@ -473,6 +545,7 @@ let test_basic_blocks () =
   let fn = declare_function "X" ty m in
   let bb = append_block "Bb1" fn in
   insist (bb = entry_block fn);
+  ignore (build_unreachable (builder_at_end bb));
   
   (* RUN: grep -v Bb2 < %t.ll
    *)
@@ -483,15 +556,18 @@ let test_basic_blocks () =
   
   group "insert";
   let fn = declare_function "X3" ty m in
-  let bbb = append_block "" fn in
-  let bba = insert_block "" bbb in
+  let bbb = append_block "b" fn in
+  let bba = insert_block "a" bbb in
   insist ([| bba; bbb |] = basic_blocks fn);
+  ignore (build_unreachable (builder_at_end bba));
+  ignore (build_unreachable (builder_at_end bbb));
   
   (* RUN: grep Bb3 < %t.ll
    *)
   group "name/value";
   let fn = define_function "X4" ty m in
   let bb = entry_block fn in
+  ignore (build_unreachable (builder_at_end bb));
   let bbv = value_of_block bb in
   set_value_name "Bb3" bbv;
   insist ("Bb3" = value_name bbv);
@@ -499,6 +575,7 @@ let test_basic_blocks () =
   group "casts";
   let fn = define_function "X5" ty m in
   let bb = entry_block fn in
+  ignore (build_unreachable (builder_at_end bb));
   insist (bb = block_of_value (value_of_block bb));
   insist (value_is_block (value_of_block bb));
   insist (not (value_is_block (const_null i32_type)))
@@ -620,7 +697,8 @@ let test_builder () =
     let inst16 = build_or   p1 inst15 "Inst16" b in
     let inst17 = build_xor  p1 inst16 "Inst17" b in
     let inst18 = build_neg  inst17    "Inst18" b in
-         ignore (build_not  inst18    "Inst19" b)
+         ignore (build_not  inst18    "Inst19" b);
+         ignore (build_unreachable b)
   end;
   
   group "memory"; begin
@@ -643,7 +721,8 @@ let test_builder () =
           ignore(build_free inst20 b);
           ignore(build_load inst21 "Inst25" b);
           ignore(build_store p2 inst22 b);
-          ignore(build_gep inst23 [| p2 |] "Inst27" b)
+          ignore(build_gep inst23 [| p2 |] "Inst27" b);
+          ignore(build_unreachable b)
   end;
   
   group "casts"; begin
@@ -682,10 +761,10 @@ let test_builder () =
      * RUN: grep {Inst42.*fcmp.*false.*F1.*F2} < %t.ll
      * RUN: grep {Inst43.*fcmp.*true.*F2.*F1} < %t.ll
      *)
-    ignore (build_icmp Icmp_ne    p1 p2 "Inst40" atentry);
-    ignore (build_icmp Icmp_sle   p2 p1 "Inst41" atentry);
-    ignore (build_fcmp Fcmp_false f1 f2 "Inst42" atentry);
-    ignore (build_fcmp Fcmp_true  f2 f1 "Inst43" atentry)
+    ignore (build_icmp Icmp.Ne    p1 p2 "Inst40" atentry);
+    ignore (build_icmp Icmp.Sle   p2 p1 "Inst41" atentry);
+    ignore (build_fcmp Fcmp.False f1 f2 "Inst42" atentry);
+    ignore (build_fcmp Fcmp.True  f2 f1 "Inst43" atentry)
   end;
   
   group "miscellaneous"; begin
@@ -694,37 +773,66 @@ let test_builder () =
      * RUN: grep {Inst48.*va_arg.*null.*i32} < %t.ll
      * RUN: grep {Inst49.*extractelement.*Vec1.*P2} < %t.ll
      * RUN: grep {Inst50.*insertelement.*Vec1.*P1.*P2} < %t.ll
-     * RUN: grep {Inst51.*shufflevector.*Vec1.*Vec2.*Vec3} < %t.ll
+     * RUN: grep {Inst51.*shufflevector.*Vec1.*Vec2.*1.*1.*0.*0} < %t.ll
      *)
-    
-    (* TODO: %Inst44 = Phi *)
-    
          ignore (build_call fn [| p2; p1 |] "Inst45" atentry);
-    let inst46 = build_icmp Icmp_eq p1 p2 "Inst46" atentry in
+    let inst46 = build_icmp Icmp.Eq p1 p2 "Inst46" atentry in
          ignore (build_select inst46 p1 p2 "Inst47" atentry);
          ignore (build_va_arg
                   (const_null (pointer_type (pointer_type i8_type)))
                   i32_type "Inst48" atentry);
     
     (* Set up some vector vregs. *)
-    let one = const_int i32_type (-1) in
-    let zero = const_int i32_type 1 in
+    let one  = const_int i32_type 1 in
+    let zero = const_int i32_type 0 in
     let t1 = const_vector [| one; zero; one; zero |] in
     let t2 = const_vector [| zero; one; zero; one |] in
     let t3 = const_vector [| one; one; zero; zero |] in
     let vec1 = build_insertelement t1 p1 p2 "Vec1" atentry in
     let vec2 = build_insertelement t2 p1 p2 "Vec2" atentry in
-    let vec3 = build_insertelement t3 p1 p2 "Vec3" atentry in
     
     ignore (build_extractelement vec1 p2 "Inst49" atentry);
     ignore (build_insertelement vec1 p1 p2 "Inst50" atentry);
-    ignore (build_shufflevector vec1 vec2 vec3 "Inst51" atentry);
+    ignore (build_shufflevector vec1 vec2 t3 "Inst51" atentry);
+  end;
+  
+  group "phi"; begin
+    (* RUN: grep {PhiNode.*P1.*PhiBlock1.*P2.*PhiBlock2} < %t.ll
+     *)
+    let b1 = append_block "PhiBlock1" fn in
+    let b2 = append_block "PhiBlock2" fn in
+    
+    let jb = append_block "PhiJoinBlock" fn in
+    ignore (build_br jb (builder_at_end b1));
+    ignore (build_br jb (builder_at_end b2));
+    let at_jb = builder_at_end jb in
+    
+    let phi = build_phi [(p1, b1)] "PhiNode" at_jb in
+    insist ([(p1, b1)] = incoming phi);
+    
+    add_incoming (p2, b2) phi;
+    insist ([(p1, b1); (p2, b2)] = incoming phi);
+    
+    ignore (build_unreachable at_jb);
   end
 
 
+(*===-- Module Provider ---------------------------------------------------===*)
+
+let test_module_provider () =
+  let m = create_module "test" in
+  let mp = ModuleProvider.create m in
+  ModuleProvider.dispose mp
+
+
 (*===-- Writer ------------------------------------------------------------===*)
 
 let test_writer () =
+  group "valid";
+  insist (match Llvm_analysis.verify_module m with
+          | None -> true
+          | Some msg -> prerr_string msg; false);
+
   group "writer";
   insist (write_bitcode_file m filename);
   
@@ -734,6 +842,7 @@ let test_writer () =
 (*===-- Driver ------------------------------------------------------------===*)
 
 let _ =
+  suite "target"           test_target;
   suite "types"            test_types;
   suite "constants"        test_constants;
   suite "global values"    test_global_values;
@@ -741,5 +850,6 @@ let _ =
   suite "functions"        test_functions;
   suite "basic blocks"     test_basic_blocks;
   suite "builder"          test_builder;
+  suite "module provider"  test_module_provider;
   suite "writer"           test_writer;
   exit !exit_status