diff options
author | Gordon Henriksen <gordonhenriksen@mac.com> | 2007-09-18 12:49:39 +0000 |
---|---|---|
committer | Gordon Henriksen <gordonhenriksen@mac.com> | 2007-09-18 12:49:39 +0000 |
commit | 8ef426baa36639458f6777309db25c1768dc9c8a (patch) | |
tree | 45ef09f427e18bbf1afd9fd552a3d9fbf0f9e398 /test | |
parent | 54c7e1216415172ac1a2aaafa32668c97f845a2c (diff) | |
download | external_llvm-8ef426baa36639458f6777309db25c1768dc9c8a.zip external_llvm-8ef426baa36639458f6777309db25c1768dc9c8a.tar.gz external_llvm-8ef426baa36639458f6777309db25c1768dc9c8a.tar.bz2 |
Adding ocaml language bindings for the vmcore and bitwriter libraries. These are
built atop the C language bindings, and user programs can link with them as
such:
# Bytecode
ocamlc -cc g++ llvm.cma llvmbitwriter.cma -o example example.ml
# Native
ocamlopt -cc g++ llvm.cmxa llvmbitwriter.cmxa -o example.opt example.ml
The vmcore.ml test exercises most/all of the APIs thus far bound. Unfortunately,
they're not yet numerous enough to write hello world. But:
$ cat example.ml
(* example.ml *)
open Llvm
open Llvm_bitwriter
let _ =
let filename = Sys.argv.(1) in
let m = create_module filename in
let v = make_int_constant i32_type 42 false in
let g = define_global "hello_world" v m in
if not (write_bitcode_file m filename) then exit 1;
dispose_module m;
$ ocamlc -cc g++ llvm.cma llvm_bitwriter.cma -o example example.ml
File "example.ml", line 11, characters 6-7:
Warning Y: unused variable g.
$ ./example example.bc
$ llvm-dis < example.bc
; ModuleID = '<stdin>'
@hello_world = global i32 42 ; <i32*> [#uses=0]
The ocaml test cases provide effective tests for the C interfaces.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42093 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r-- | test/Bindings/Ocaml/bitwriter.ml | 16 | ||||
-rw-r--r-- | test/Bindings/Ocaml/ocaml.exp | 3 | ||||
-rw-r--r-- | test/Bindings/Ocaml/vmcore.ml | 233 | ||||
-rw-r--r-- | test/lib/llvm.exp | 4 |
4 files changed, 255 insertions, 1 deletions
diff --git a/test/Bindings/Ocaml/bitwriter.ml b/test/Bindings/Ocaml/bitwriter.ml new file mode 100644 index 0000000..5421e09 --- /dev/null +++ b/test/Bindings/Ocaml/bitwriter.ml @@ -0,0 +1,16 @@ +(* RUN: %ocamlc llvm.cma llvm_bitwriter.cma %s -o %t + * RUN: ./%t %t.bc + * RUN: llvm-dis < %t.bc | grep caml_int_ty + *) + +(* Note that this takes a moment to link, so it's best to keep the number of + individual tests low. *) + +let test x = if not x then exit 1 else () + +let _ = + let m = Llvm.create_module "ocaml_test_module" in + + ignore (Llvm.add_type_name "caml_int_ty" Llvm.i32_type m); + + test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1)) diff --git a/test/Bindings/Ocaml/ocaml.exp b/test/Bindings/Ocaml/ocaml.exp new file mode 100644 index 0000000..5f84695 --- /dev/null +++ b/test/Bindings/Ocaml/ocaml.exp @@ -0,0 +1,3 @@ +load_lib llvm.exp + +RunLLVMTests [lsort [glob -nocomplain $srcdir/$subdir/*.{ll,llx,c,cpp,tr,ml}]] diff --git a/test/Bindings/Ocaml/vmcore.ml b/test/Bindings/Ocaml/vmcore.ml new file mode 100644 index 0000000..1fd0d19 --- /dev/null +++ b/test/Bindings/Ocaml/vmcore.ml @@ -0,0 +1,233 @@ +(* RUN: %ocamlc llvm.cma llvm_bitwriter.cma %s -o %t + * RUN: ./%t %t.bc + * RUN: llvm-dis < %t.bc > %t.ll + *) + +(* Note: It takes several seconds for ocamlc to link an executable with + libLLVMCore.a, so it's better to write a big test than a bunch of + little ones. *) + +open Llvm +open Llvm_bitwriter + + +(* Tiny unit test framework *) +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 ^ "... ") + +let insist cond = + incr case_num; + prerr_char ' '; + if not cond then begin + exit_status := 10; + prerr_char '!' + end; + prerr_int !case_num + +let suite name f = + prerr_endline (name ^ ":"); + f () + + +(*===-- Fixture -----------------------------------------------------------===*) + +let filename = Sys.argv.(1) +let m = create_module filename + + +(*===-- Types -------------------------------------------------------------===*) + +let test_types () = + (* RUN: grep {Ty01.*void} < %t.ll + *) + group "void"; + insist (add_type_name "Ty01" void_type m); + insist (Void_type == classify_type void_type); + + (* RUN: grep {Ty02.*i1} < %t.ll + *) + group "i1"; + insist (add_type_name "Ty02" i1_type m); + insist (Integer_type == classify_type i1_type); + + (* RUN: grep {Ty03.*i32} < %t.ll + *) + group "i32"; + insist (add_type_name "Ty03" i32_type m); + + (* RUN: grep {Ty04.*i42} < %t.ll + *) + group "i42"; + let ty = make_integer_type 42 in + insist (add_type_name "Ty04" ty m); + + (* RUN: grep {Ty05.*float} < %t.ll + *) + group "float"; + insist (add_type_name "Ty05" float_type m); + insist (Float_type == classify_type float_type); + + (* RUN: grep {Ty06.*double} < %t.ll + *) + group "double"; + insist (add_type_name "Ty06" double_type m); + insist (Double_type == classify_type double_type); + + (* RUN: grep {Ty07.*i32.*i1, double} < %t.ll + *) + group "function"; + let ty = make_function_type i32_type [| i1_type; double_type |] false in + insist (add_type_name "Ty07" ty m); + insist (Function_type = classify_type ty); + insist (not (is_var_arg ty)); + insist (i32_type == return_type ty); + insist (double_type == (param_types ty).(1)); + + (* RUN: grep {Ty08.*\.\.\.} < %t.ll + *) + group "vararg"; + let ty = make_function_type void_type [| i32_type |] true in + insist (add_type_name "Ty08" ty m); + insist (is_var_arg ty); + + (* RUN: grep {Ty09.*\\\[7 x i8\\\]} < %t.ll + *) + group "array"; + let ty = make_array_type i8_type 7 in + insist (add_type_name "Ty09" ty m); + insist (7 = array_length ty); + insist (i8_type == element_type ty); + insist (Array_type == classify_type ty); + + (* RUN: grep {Ty10.*float\*} < %t.ll + *) + group "pointer"; + let ty = make_pointer_type float_type in + insist (add_type_name "Ty10" ty m); + insist (float_type == element_type ty); + insist (Pointer_type == classify_type ty); + + (* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll + *) + group "vector"; + let ty = make_vector_type i16_type 4 in + insist (add_type_name "Ty11" ty m); + insist (i16_type == element_type ty); + insist (4 = vector_size ty); + + (* RUN: grep {Ty12.*opaque} < %t.ll + *) + group "opaque"; + let ty = make_opaque_type () in + insist (add_type_name "Ty12" ty m); + insist (ty == ty); + insist (ty <> make_opaque_type ()) + + +(*===-- Global Values -----------------------------------------------------===*) + +let test_global_values () = + let (++) x f = f x; x in + let zero32 = make_null i32_type in + + (* RUN: grep {GVal01} < %t.ll + *) + 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); + + (* RUN: grep {GVal02.*linkonce} < %t.ll + *) + group "linkage"; + let g = define_global "GVal02" zero32 m ++ + set_linkage Link_once_linkage in + insist (Link_once_linkage = linkage g); + + (* RUN: grep {GVal03.*Hanalei} < %t.ll + *) + group "section"; + let g = define_global "GVal03" zero32 m ++ + set_section "Hanalei" in + insist ("Hanalei" = section g); + + (* RUN: grep {GVal04.*hidden} < %t.ll + *) + group "visibility"; + let g = define_global "GVal04" zero32 m ++ + set_visibility Hidden_visibility in + insist (Hidden_visibility = visibility g); + + (* RUN: grep {GVal05.*align 128} < %t.ll + *) + group "alignment"; + let g = define_global "GVal05" zero32 m ++ + set_alignment 128 in + insist (128 = alignment g) + + +(*===-- Global Variables --------------------------------------------------===*) + +let test_global_variables () = + let (++) x f = f x; x in + let fourty_two32 = make_int_constant i32_type 42 false in + + (* RUN: grep {GVar01.*external} < %t.ll + *) + group "declarations"; + let g = declare_global i32_type "GVar01" m in + insist (is_declaration g); + + (* RUN: grep {GVar02.*42} < %t.ll + * RUN: grep {GVar03.*42} < %t.ll + *) + group "definitions"; + let g = define_global "GVar02" fourty_two32 m in + let g2 = declare_global i32_type "GVar03" m ++ + set_initializer fourty_two32 in + insist (not (is_declaration g)); + insist (not (is_declaration g2)); + insist ((global_initializer g) == (global_initializer g2)); + + (* RUN: grep {GVar04.*thread_local} < %t.ll + *) + group "threadlocal"; + let g = define_global "GVar04" fourty_two32 m ++ + set_thread_local true in + insist (is_thread_local g); + + (* RUN: grep -v {GVar05} < %t.ll + *) + let g = define_global "GVar05" fourty_two32 m in + delete_global g + + +(*===-- Writer ------------------------------------------------------------===*) + +let test_writer () = + group "writer"; + insist (write_bitcode_file m filename); + + dispose_module m + + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + suite "types" test_types; + suite "global values" test_global_values; + suite "global variables" test_global_variables; + suite "writer" test_writer; + all_done () diff --git a/test/lib/llvm.exp b/test/lib/llvm.exp index 9a85a1c..921bcb5 100644 --- a/test/lib/llvm.exp +++ b/test/lib/llvm.exp @@ -43,7 +43,7 @@ proc execOneLine { test PRS outcome lineno line } { # cases. proc substitute { line test tmpFile } { global srcroot objroot srcdir objdir subdir target_triplet prcontext - global llvmgcc llvmgxx llvmgcc_version llvmgccmajvers + global llvmgcc llvmgxx llvmgcc_version llvmgccmajvers ocamlc global gccpath gxxpath compile_c compile_cxx link shlibext llvmlibsdir set path [file join $srcdir $subdir] @@ -64,6 +64,8 @@ proc substitute { line test tmpFile } { regsub -all {%link} $new_line "$link" new_line #replace %shlibext with shared library extension regsub -all {%shlibext} $new_line "$shlibext" new_line + #replace %ocamlc with ocaml compiler command + regsub -all {%ocamlc} $new_line "$ocamlc" new_line #replace %llvmlibsdir with configure library directory regsub -all {%llvmlibsdir} $new_line "$llvmlibsdir" new_line #replace %p with path to source, |