Tests of the ocaml (and thus C) bindings for constants.
authorGordon Henriksen <gordonhenriksen@mac.com>
Tue, 18 Sep 2007 18:07:51 +0000 (18:07 +0000)
committerGordon Henriksen <gordonhenriksen@mac.com>
Tue, 18 Sep 2007 18:07:51 +0000 (18:07 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42101 91177308-0d34-0410-b5e6-96231b3b80d8

bindings/ocaml/llvm/llvm.ml
bindings/ocaml/llvm/llvm.mli
bindings/ocaml/llvm/llvm_ocaml.c
include/llvm-c/Core.h
lib/VMCore/Core.cpp
test/Bindings/Ocaml/vmcore.ml

index 18a9e2154a34ccd2d6eaaae7be8b78548a57fbfe..b8a62dcb681caea4c70561b626989971abba3d4a 100644 (file)
@@ -141,14 +141,18 @@ external value_name : llvalue -> string = "llvm_value_name"
 external set_value_name : string -> llvalue -> unit = "llvm_set_value_name"
 
 (*--... Operations on constants of (mostly) any type .......................--*)
 external set_value_name : string -> llvalue -> unit = "llvm_set_value_name"
 
 (*--... Operations on constants of (mostly) any type .......................--*)
+external is_constant : llvalue -> bool = "llvm_is_constant"
 external make_null : lltype -> llvalue = "llvm_make_null"
 external make_null : lltype -> llvalue = "llvm_make_null"
-external make_all_ones : lltype -> llvalue = "llvm_make_all_ones"
+external make_all_ones : (*int|vec*)lltype -> llvalue = "llvm_make_all_ones"
 external make_undef : lltype -> llvalue = "llvm_make_undef"
 external is_null : llvalue -> bool = "llvm_is_null"
 external make_undef : lltype -> llvalue = "llvm_make_undef"
 external is_null : llvalue -> bool = "llvm_is_null"
+external is_undef : llvalue -> bool = "llvm_is_undef"
 
 (*--... Operations on scalar constants .....................................--*)
 external make_int_constant : lltype -> int -> bool -> llvalue
                            = "llvm_make_int_constant"
 
 (*--... Operations on scalar constants .....................................--*)
 external make_int_constant : lltype -> int -> bool -> llvalue
                            = "llvm_make_int_constant"
+external make_int64_constant : lltype -> Int64.t -> bool -> llvalue
+                             = "llvm_make_int64_constant"
 external make_real_constant : lltype -> float -> llvalue
                             = "llvm_make_real_constant"
 
 external make_real_constant : lltype -> float -> llvalue
                             = "llvm_make_real_constant"
 
index 7e14cd78bb696cb51969c55bafee553a655bfe6f..0e38ca0ef2f2bc72d940c0b0ca2fff7676f7454f 100644 (file)
@@ -124,14 +124,18 @@ external value_name : llvalue -> string = "llvm_value_name"
 external set_value_name : string -> llvalue -> unit = "llvm_set_value_name"
 
 (*--... Operations on constants of (mostly) any type .......................--*)
 external set_value_name : string -> llvalue -> unit = "llvm_set_value_name"
 
 (*--... Operations on constants of (mostly) any type .......................--*)
+external is_constant : llvalue -> bool = "llvm_is_constant"
 external make_null : lltype -> llvalue = "llvm_make_null"
 external make_null : lltype -> llvalue = "llvm_make_null"
-external make_all_ones : lltype -> llvalue = "llvm_make_all_ones"
+external make_all_ones : (*int|vec*)lltype -> llvalue = "llvm_make_all_ones"
 external make_undef : lltype -> llvalue = "llvm_make_undef"
 external is_null : llvalue -> bool = "llvm_is_null"
 external make_undef : lltype -> llvalue = "llvm_make_undef"
 external is_null : llvalue -> bool = "llvm_is_null"
+external is_undef : llvalue -> bool = "llvm_is_undef"
 
 (*--... Operations on scalar constants .....................................--*)
 external make_int_constant : lltype -> int -> bool -> llvalue
                            = "llvm_make_int_constant"
 
 (*--... Operations on scalar constants .....................................--*)
 external make_int_constant : lltype -> int -> bool -> llvalue
                            = "llvm_make_int_constant"
+external make_int64_constant : lltype -> Int64.t -> bool -> llvalue
+                             = "llvm_make_int64_constant"
 external make_real_constant : lltype -> float -> llvalue
                             = "llvm_make_real_constant"
 
 external make_real_constant : lltype -> float -> llvalue
                             = "llvm_make_real_constant"
 
index 3191cb0563b080c6a52d4c60112636f7b7462707..ae07fb20fdc5e71583f287b918bca1b0ca6b9466 100644 (file)
@@ -247,11 +247,21 @@ CAMLprim value llvm_make_undef(value Ty) {
   return (value) LLVMGetUndef((LLVMTypeRef) Ty);
 }
 
   return (value) LLVMGetUndef((LLVMTypeRef) Ty);
 }
 
+/* llvalue -> bool */
+CAMLprim value llvm_is_constant(value Ty) {
+  return Val_bool(LLVMIsConstant((LLVMValueRef) Ty));
+}
+
 /* llvalue -> bool */
 CAMLprim value llvm_is_null(value Val) {
   return Val_bool(LLVMIsNull((LLVMValueRef) Val));
 }
 
 /* llvalue -> bool */
 CAMLprim value llvm_is_null(value Val) {
   return Val_bool(LLVMIsNull((LLVMValueRef) Val));
 }
 
+/* llvalue -> bool */
+CAMLprim value llvm_is_undef(value Ty) {
+  return Val_bool(LLVMIsUndef((LLVMValueRef) Ty));
+}
+
 /*--... Operations on scalar constants .....................................--*/
 
 /* lltype -> int -> bool -> llvalue */
 /*--... Operations on scalar constants .....................................--*/
 
 /* lltype -> int -> bool -> llvalue */
@@ -266,6 +276,12 @@ CAMLprim value llvm_make_int_constant(value IntTy, value N, value SExt) {
   return (value) LLVMGetIntConstant((LLVMTypeRef) IntTy, N2, Bool_val(SExt));
 }
 
   return (value) LLVMGetIntConstant((LLVMTypeRef) IntTy, N2, Bool_val(SExt));
 }
 
+/* lltype -> Int64.t -> bool -> llvalue */
+CAMLprim value llvm_make_int64_constant(value IntTy, value N, value SExt) {
+  return (value) LLVMGetIntConstant((LLVMTypeRef) IntTy, Int64_val(N),
+                                    Bool_val(SExt));
+}
+
 /* lltype -> float -> llvalue */
 CAMLprim value llvm_make_real_constant(value RealTy, value N) {
   return (value) LLVMGetRealConstant((LLVMTypeRef) RealTy, Double_val(N));
 /* lltype -> float -> llvalue */
 CAMLprim value llvm_make_real_constant(value RealTy, value N) {
   return (value) LLVMGetRealConstant((LLVMTypeRef) RealTy, Double_val(N));
@@ -276,7 +292,7 @@ CAMLprim value llvm_make_real_constant(value RealTy, value N) {
 /* string -> bool -> llvalue */
 CAMLprim value llvm_make_string_constant(value Str, value NullTerminate) {
   return (value) LLVMGetStringConstant(String_val(Str),
 /* string -> bool -> llvalue */
 CAMLprim value llvm_make_string_constant(value Str, value NullTerminate) {
   return (value) LLVMGetStringConstant(String_val(Str),
-                                       Wosize_val(Str),
+                                       caml_string_length(Str),
                                        Bool_val(NullTerminate) == 0);
 }
 
                                        Bool_val(NullTerminate) == 0);
 }
 
index 1442a237d99d3d2b0188d5ce0653adcf17cd9fd3..9aa5f07761c81020da427c383838ca86bafaa3ca 100644 (file)
@@ -176,7 +176,9 @@ void LLVMSetValueName(LLVMValueRef Val, const char *Name);
 LLVMValueRef LLVMGetNull(LLVMTypeRef Ty); /* all zeroes */
 LLVMValueRef LLVMGetAllOnes(LLVMTypeRef Ty); /* only for int/vector */
 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty);
 LLVMValueRef LLVMGetNull(LLVMTypeRef Ty); /* all zeroes */
 LLVMValueRef LLVMGetAllOnes(LLVMTypeRef Ty); /* only for int/vector */
 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty);
+int LLVMIsConstant(LLVMValueRef Val);
 int LLVMIsNull(LLVMValueRef Val);
 int LLVMIsNull(LLVMValueRef Val);
+int LLVMIsUndef(LLVMValueRef Val);
 
 /* Operations on scalar constants */
 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
 
 /* Operations on scalar constants */
 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
index 297027fd3148667a5505a34f89c4a89446746831..b809db2ec722647fc7287e393787040ea9c26095 100644 (file)
@@ -201,12 +201,20 @@ LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
   return wrap(UndefValue::get(unwrap(Ty)));
 }
 
   return wrap(UndefValue::get(unwrap(Ty)));
 }
 
+int LLVMIsConstant(LLVMValueRef Ty) {
+  return isa<Constant>(unwrap(Ty));
+}
+
 int LLVMIsNull(LLVMValueRef Val) {
   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
     return C->isNullValue();
   return false;
 }
 
 int LLVMIsNull(LLVMValueRef Val) {
   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
     return C->isNullValue();
   return false;
 }
 
+int LLVMIsUndef(LLVMValueRef Val) {
+  return isa<UndefValue>(unwrap(Val));
+}
+
 /*--.. Operations on scalar constants ......................................--*/
 
 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
 /*--.. Operations on scalar constants ......................................--*/
 
 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
index 1fd0d19d4214e5efd760652f6f4c38c926e287ab..ef274a887d0b6057050be1e3c973f881b60c5057 100644 (file)
@@ -11,27 +11,21 @@ open Llvm
 open Llvm_bitwriter
 
 
 open Llvm_bitwriter
 
 
-(* Tiny unit test framework *)
+(* Tiny unit test framework - really just to help find which line is busted *)
 let exit_status = ref 0
 let case_num = ref 0
 
 let exit_status = ref 0
 let case_num = ref 0
 
-let all_done () =
-  prerr_endline "";
-  exit !exit_status
-
 let group name =
 let group name =
-  prerr_endline "";
   case_num := 0;
   case_num := 0;
-  prerr_string ("  " ^ name ^ "... ")
+  prerr_endline ("  " ^ name ^ "...")
 
 let insist cond =
   incr case_num;
 
 let insist cond =
   incr case_num;
-  prerr_char ' ';
-  if not cond then begin
-     exit_status := 10;
-     prerr_char '!'
-  end;
-  prerr_int !case_num
+  let msg = if cond then "    pass " else begin
+    exit_status := 10;
+    "    FAIL "
+  end in
+  prerr_endline (msg ^ (string_of_int !case_num))
 
 let suite name f =
   prerr_endline (name ^ ":");
 
 let suite name f =
   prerr_endline (name ^ ":");
@@ -133,6 +127,102 @@ let test_types () =
   insist (ty <> make_opaque_type ())
 
 
   insist (ty <> make_opaque_type ())
 
 
+(*===-- Constants ---------------------------------------------------------===*)
+
+let test_constants () =
+  (* RUN: grep {Const01.*i32.*-1} < %t.ll
+   *)
+  group "int";
+  let c = make_int_constant i32_type (-1) true in
+  ignore (define_global "Const01" c m);
+  insist (i32_type = type_of c);
+  insist (is_constant c);
+
+  (* RUN: grep {Const02.*i64.*-1} < %t.ll
+   *)
+  group "sext int";
+  let c = make_int_constant i64_type (-1) true in
+  ignore (define_global "Const02" c m);
+  insist (i64_type = type_of c);
+
+  (* RUN: grep {Const03.*i64.*4294967295} < %t.ll
+   *)
+  group "zext int64";
+  let c = make_int64_constant i64_type (Int64.of_string "4294967295") false in
+  ignore (define_global "Const03" c m);
+  insist (i64_type = type_of c);
+
+  (* RUN: grep {Const04.*"cruel\\\\00world"} < %t.ll
+   *)
+  group "string";
+  let c = make_string_constant "cruel\x00world" false in
+  ignore (define_global "Const04" c m);
+  insist ((make_array_type i8_type 11) = type_of c);
+
+  (* RUN: grep {Const05.*"hi\\\\00again\\\\00"} < %t.ll
+   *)
+  group "string w/ null";
+  let c = make_string_constant "hi\x00again" true in
+  ignore (define_global "Const05" c m);
+  insist ((make_array_type i8_type 9) = type_of c);
+
+  (* RUN: grep {Const06.*3.1459} < %t.ll
+   *)
+  group "real";
+  let c = make_real_constant double_type 3.1459 in
+  ignore (define_global "Const06" c m);
+  insist (double_type = type_of c);
+  
+  let one = make_int_constant i16_type 1 true in
+  let two = make_int_constant i16_type 2 true in
+  let three = make_int_constant i32_type 3 true in
+  let four = make_int_constant i32_type 4 true in
+  
+  (* RUN: grep {Const07.*\\\[ i32 3, i32 4 \\\]} < %t.ll
+   *)
+  group "array";
+  let c = make_array_constant i32_type [| three; four |] in
+  ignore (define_global "Const07" c m);
+  insist ((make_array_type i32_type 2) = (type_of c));
+  
+  (* RUN: grep {Const08.*< i16 1, i16 2.* >} < %t.ll
+   *)
+  group "vector";
+  let c = make_vector_constant [| one; two; one; two;
+                                  one; two; one; two |] in
+  ignore (define_global "Const08" c m);
+  insist ((make_vector_type i16_type 8) = (type_of c));
+  
+  (* RUN: grep {Const09.*\{ i16, i16, i32, i32 \} \{} < %t.ll
+   *)
+  group "structure";
+  let c = make_struct_constant [| one; two; three; four |] false in
+  ignore (define_global "Const09" c m);
+  insist ((make_struct_type [| i16_type; i16_type; i32_type; i32_type |] false)
+        = (type_of c));
+  
+  (* RUN: grep {Const10.*zeroinit} < %t.ll
+   *)
+  group "null";
+  let c = make_null (make_struct_type [| i1_type; i8_type;
+                                         i64_type; double_type |] true) in
+  ignore (define_global "Const10" c m);
+  
+  (* RUN: grep {Const11.*-1} < %t.ll
+   *)
+  group "all ones";
+  let c = make_all_ones i64_type in
+  ignore (define_global "Const11" c m);
+  
+  (* RUN: grep {Const12.*undef} < %t.ll
+   *)
+  group "undef";
+  let c = make_undef i1_type in
+  ignore (define_global "Const12" c m);
+  insist (i1_type = type_of c);
+  insist (is_undef c)
+
+
 (*===-- Global Values -----------------------------------------------------===*)
 
 let test_global_values () =
 (*===-- Global Values -----------------------------------------------------===*)
 
 let test_global_values () =
@@ -143,8 +233,6 @@ let test_global_values () =
    *)
   group "naming";
   let g = define_global "TEMPORARY" zero32 m in
    *)
   group "naming";
   let g = define_global "TEMPORARY" zero32 m in
-  prerr_endline "";
-  prerr_endline (value_name g);
   insist ("TEMPORARY" = value_name g);
   set_value_name "GVal01" g;
   insist ("GVal01" = value_name g);
   insist ("TEMPORARY" = value_name g);
   set_value_name "GVal01" g;
   insist ("GVal01" = value_name g);
@@ -210,6 +298,7 @@ let test_global_variables () =
 
   (* RUN: grep -v {GVar05} < %t.ll
    *)
 
   (* RUN: grep -v {GVar05} < %t.ll
    *)
+  group "delete";
   let g = define_global "GVar05" fourty_two32 m in
   delete_global g
 
   let g = define_global "GVar05" fourty_two32 m in
   delete_global g
 
@@ -227,7 +316,8 @@ let test_writer () =
 
 let _ =
   suite "types"            test_types;
 
 let _ =
   suite "types"            test_types;
+  suite "constants"        test_constants;
   suite "global values"    test_global_values;
   suite "global variables" test_global_variables;
   suite "writer"           test_writer;
   suite "global values"    test_global_values;
   suite "global variables" test_global_variables;
   suite "writer"           test_writer;
-  all_done ()
+  exit !exit_status