From 344be5fbecec9908bab611eafeae0549ba3be6d7 Mon Sep 17 00:00:00 2001 From: Gordon Henriksen Date: Tue, 18 Sep 2007 18:07:51 +0000 Subject: [PATCH] Tests of the ocaml (and thus C) bindings for constants. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42101 91177308-0d34-0410-b5e6-96231b3b80d8 --- bindings/ocaml/llvm/llvm.ml | 6 +- bindings/ocaml/llvm/llvm.mli | 6 +- bindings/ocaml/llvm/llvm_ocaml.c | 18 ++++- include/llvm-c/Core.h | 2 + lib/VMCore/Core.cpp | 8 ++ test/Bindings/Ocaml/vmcore.ml | 122 +++++++++++++++++++++++++++---- 6 files changed, 143 insertions(+), 19 deletions(-) diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index 18a9e2154a3..b8a62dcb681 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -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 is_constant : llvalue -> bool = "llvm_is_constant" 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 is_undef : llvalue -> bool = "llvm_is_undef" (*--... 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" diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index 7e14cd78bb6..0e38ca0ef2f 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -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 is_constant : llvalue -> bool = "llvm_is_constant" 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 is_undef : llvalue -> bool = "llvm_is_undef" (*--... 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" diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index 3191cb0563b..ae07fb20fdc 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -247,11 +247,21 @@ CAMLprim value llvm_make_undef(value 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_undef(value Ty) { + return Val_bool(LLVMIsUndef((LLVMValueRef) Ty)); +} + /*--... 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)); } +/* 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)); @@ -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), - Wosize_val(Str), + caml_string_length(Str), Bool_val(NullTerminate) == 0); } diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 1442a237d99..9aa5f07761c 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -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); +int LLVMIsConstant(LLVMValueRef Val); int LLVMIsNull(LLVMValueRef Val); +int LLVMIsUndef(LLVMValueRef Val); /* Operations on scalar constants */ LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N, diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index 297027fd314..b809db2ec72 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -201,12 +201,20 @@ LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) { return wrap(UndefValue::get(unwrap(Ty))); } +int LLVMIsConstant(LLVMValueRef Ty) { + return isa(unwrap(Ty)); +} + int LLVMIsNull(LLVMValueRef Val) { if (Constant *C = dyn_cast(unwrap(Val))) return C->isNullValue(); return false; } +int LLVMIsUndef(LLVMValueRef Val) { + return isa(unwrap(Val)); +} + /*--.. Operations on scalar constants ......................................--*/ LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N, diff --git a/test/Bindings/Ocaml/vmcore.ml b/test/Bindings/Ocaml/vmcore.ml index 1fd0d19d421..ef274a887d0 100644 --- a/test/Bindings/Ocaml/vmcore.ml +++ b/test/Bindings/Ocaml/vmcore.ml @@ -11,27 +11,21 @@ open Llvm 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 all_done () = - prerr_endline ""; - exit !exit_status - let group name = - prerr_endline ""; case_num := 0; - prerr_string (" " ^ name ^ "... ") + prerr_endline (" " ^ name ^ "...") 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 ^ ":"); @@ -133,6 +127,102 @@ let test_types () = 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 () = @@ -143,8 +233,6 @@ let test_global_values () = *) 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); @@ -210,6 +298,7 @@ let test_global_variables () = (* RUN: grep -v {GVar05} < %t.ll *) + group "delete"; let g = define_global "GVar05" fourty_two32 m in delete_global g @@ -227,7 +316,8 @@ let test_writer () = 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; - all_done () + exit !exit_status -- 2.34.1