From 37ed9c199ca639565f6ce88105f9e39e898d82d0 Mon Sep 17 00:00:00 2001 From: Stephen Hines Date: Mon, 1 Dec 2014 14:51:49 -0800 Subject: Update aosp/master LLVM for rebase to r222494. Change-Id: Ic787f5e0124df789bd26f3f24680f45e678eef2d --- test/Bindings/Go/go.test | 3 + test/Bindings/Go/lit.local.cfg | 57 ++ test/Bindings/OCaml/analysis.ml | 54 ++ test/Bindings/OCaml/bitreader.ml | 79 ++ test/Bindings/OCaml/bitwriter.ml | 49 ++ test/Bindings/OCaml/core.ml | 1507 ++++++++++++++++++++++++++++++++ test/Bindings/OCaml/executionengine.ml | 105 +++ test/Bindings/OCaml/ext_exc.ml | 22 + test/Bindings/OCaml/ipo.ml | 72 ++ test/Bindings/OCaml/irreader.ml | 59 ++ test/Bindings/OCaml/linker.ml | 63 ++ test/Bindings/OCaml/lit.local.cfg | 7 + test/Bindings/OCaml/passmgr_builder.ml | 64 ++ test/Bindings/OCaml/scalar_opts.ml | 92 ++ test/Bindings/OCaml/target.ml | 116 +++ test/Bindings/OCaml/vectorize.ml | 56 ++ test/Bindings/Ocaml/analysis.ml | 54 -- test/Bindings/Ocaml/bitreader.ml | 79 -- test/Bindings/Ocaml/bitwriter.ml | 48 - test/Bindings/Ocaml/executionengine.ml | 118 --- test/Bindings/Ocaml/ext_exc.ml | 20 - test/Bindings/Ocaml/ipo_opts.ml | 72 -- test/Bindings/Ocaml/irreader.ml | 59 -- test/Bindings/Ocaml/linker.ml | 63 -- test/Bindings/Ocaml/lit.local.cfg | 5 - test/Bindings/Ocaml/passmgr_builder.ml | 64 -- test/Bindings/Ocaml/scalar_opts.ml | 87 -- test/Bindings/Ocaml/target.ml | 116 --- test/Bindings/Ocaml/vectorize_opts.ml | 56 -- test/Bindings/Ocaml/vmcore.ml | 1421 ------------------------------ test/Bindings/llvm-c/disassemble.test | 26 +- test/Bindings/llvm-c/objectfile.ll | 2 + 32 files changed, 2427 insertions(+), 2268 deletions(-) create mode 100644 test/Bindings/Go/go.test create mode 100644 test/Bindings/Go/lit.local.cfg create mode 100644 test/Bindings/OCaml/analysis.ml create mode 100644 test/Bindings/OCaml/bitreader.ml create mode 100644 test/Bindings/OCaml/bitwriter.ml create mode 100644 test/Bindings/OCaml/core.ml create mode 100644 test/Bindings/OCaml/executionengine.ml create mode 100644 test/Bindings/OCaml/ext_exc.ml create mode 100644 test/Bindings/OCaml/ipo.ml create mode 100644 test/Bindings/OCaml/irreader.ml create mode 100644 test/Bindings/OCaml/linker.ml create mode 100644 test/Bindings/OCaml/lit.local.cfg create mode 100644 test/Bindings/OCaml/passmgr_builder.ml create mode 100644 test/Bindings/OCaml/scalar_opts.ml create mode 100644 test/Bindings/OCaml/target.ml create mode 100644 test/Bindings/OCaml/vectorize.ml delete mode 100644 test/Bindings/Ocaml/analysis.ml delete mode 100644 test/Bindings/Ocaml/bitreader.ml delete mode 100644 test/Bindings/Ocaml/bitwriter.ml delete mode 100644 test/Bindings/Ocaml/executionengine.ml delete mode 100644 test/Bindings/Ocaml/ext_exc.ml delete mode 100644 test/Bindings/Ocaml/ipo_opts.ml delete mode 100644 test/Bindings/Ocaml/irreader.ml delete mode 100644 test/Bindings/Ocaml/linker.ml delete mode 100644 test/Bindings/Ocaml/lit.local.cfg delete mode 100644 test/Bindings/Ocaml/passmgr_builder.ml delete mode 100644 test/Bindings/Ocaml/scalar_opts.ml delete mode 100644 test/Bindings/Ocaml/target.ml delete mode 100644 test/Bindings/Ocaml/vectorize_opts.ml delete mode 100644 test/Bindings/Ocaml/vmcore.ml create mode 100644 test/Bindings/llvm-c/objectfile.ll (limited to 'test/Bindings') diff --git a/test/Bindings/Go/go.test b/test/Bindings/Go/go.test new file mode 100644 index 0000000..3951483 --- /dev/null +++ b/test/Bindings/Go/go.test @@ -0,0 +1,3 @@ +; RUN: llvm-go test llvm.org/llvm/bindings/go/llvm + +; REQUIRES: shell diff --git a/test/Bindings/Go/lit.local.cfg b/test/Bindings/Go/lit.local.cfg new file mode 100644 index 0000000..e86595b --- /dev/null +++ b/test/Bindings/Go/lit.local.cfg @@ -0,0 +1,57 @@ +import os +import pipes +import shlex +import sys + +if not 'go' in config.root.llvm_bindings: + config.unsupported = True + +def find_executable(executable, path=None): + if path is None: + path = os.environ['PATH'] + paths = path.split(os.pathsep) + base, ext = os.path.splitext(executable) + + if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'): + executable = executable + '.exe' + + if not os.path.isfile(executable): + for p in paths: + f = os.path.join(p, executable) + if os.path.isfile(f): + return f + return None + else: + return executable + +# Resolve certain symlinks in the first word of compiler. +# +# This is a Go-specific hack. cgo and other Go tools check $CC and $CXX for the +# substring 'clang' to determine if the compiler is Clang. This won't work if +# $CC is cc and cc is a symlink pointing to clang, as it is on Darwin. +# +# Go tools also have problems with ccache, so we disable it. +def fixup_compiler_path(compiler): + args = shlex.split(compiler) + if args[0].endswith('ccache'): + args = args[1:] + + path = find_executable(args[0]) + + try: + if path.endswith('/cc') and os.readlink(path) == 'clang': + args[0] = path[:len(path)-2] + 'clang' + except (AttributeError, OSError): + pass + + try: + if path.endswith('/c++') and os.readlink(path) == 'clang++': + args[0] = path[:len(path)-3] + 'clang++' + except (AttributeError, OSError): + pass + + return ' '.join([pipes.quote(arg) for arg in args]) + +config.environment['CC'] = fixup_compiler_path(config.host_cc) +config.environment['CXX'] = fixup_compiler_path(config.host_cxx) +config.environment['CGO_LDFLAGS'] = config.host_ldflags diff --git a/test/Bindings/OCaml/analysis.ml b/test/Bindings/OCaml/analysis.ml new file mode 100644 index 0000000..e935ee8 --- /dev/null +++ b/test/Bindings/OCaml/analysis.ml @@ -0,0 +1,54 @@ +(* RUN: cp %s %T/analysis.ml + * RUN: %ocamlc -g -warn-error A -package llvm.analysis -linkpkg %T/analysis.ml -o %t + * RUN: %t + * RUN: %ocamlopt -g -warn-error A -package llvm.analysis -linkpkg %T/analysis.ml -o %t + * RUN: %t + * XFAIL: vg_leak + *) + +open Llvm +open Llvm_analysis + +(* Note that this takes a moment to link, so it's best to keep the number of + individual tests low. *) + +let context = global_context () + +let test x = if not x then exit 1 else () + +let bomb msg = + prerr_endline msg; + exit 2 + +let _ = + let fty = function_type (void_type context) [| |] in + let m = create_module context "valid_m" in + let fn = define_function "valid_fn" fty m in + let at_entry = builder_at_end context (entry_block fn) in + ignore (build_ret_void at_entry); + + + (* Test that valid constructs verify. *) + begin match verify_module m with + Some msg -> bomb "valid module failed verification!" + | None -> () + end; + + if not (verify_function fn) then bomb "valid function failed verification!"; + + + (* Test that invalid constructs do not verify. + A basic block can contain only one terminator instruction. *) + ignore (build_ret_void at_entry); + + begin match verify_module m with + Some msg -> () + | None -> bomb "invalid module passed verification!" + end; + + if verify_function fn then bomb "invalid function passed verification!"; + + + dispose_module m + + (* Don't bother to test assert_valid_{module,function}. *) diff --git a/test/Bindings/OCaml/bitreader.ml b/test/Bindings/OCaml/bitreader.ml new file mode 100644 index 0000000..57cfd04 --- /dev/null +++ b/test/Bindings/OCaml/bitreader.ml @@ -0,0 +1,79 @@ +(* RUN: cp %s %T/bitreader.ml + * RUN: %ocamlc -g -warn-error A -package llvm.bitreader -package llvm.bitwriter -linkpkg %T/bitreader.ml -o %t + * RUN: %t %t.bc + * RUN: %ocamlopt -g -warn-error A -package llvm.bitreader -package llvm.bitwriter -linkpkg %T/bitreader.ml -o %t + * RUN: %t %t.bc + * RUN: llvm-dis < %t.bc + * XFAIL: vg_leak + *) + +(* Note that this takes a moment to link, so it's best to keep the number of + individual tests low. *) + +let context = Llvm.global_context () + +let test x = if not x then exit 1 else () + +let _ = + let fn = Sys.argv.(1) in + let m = Llvm.create_module context "ocaml_test_module" in + + test (Llvm_bitwriter.write_bitcode_file m fn); + + Llvm.dispose_module m; + + (* parse_bitcode *) + begin + let mb = Llvm.MemoryBuffer.of_file fn in + begin try + let m = Llvm_bitreader.parse_bitcode context mb in + Llvm.dispose_module m + with x -> + Llvm.MemoryBuffer.dispose mb; + raise x + end + end; + + (* MemoryBuffer.of_file *) + test begin try + let mb = Llvm.MemoryBuffer.of_file (fn ^ ".bogus") in + Llvm.MemoryBuffer.dispose mb; + false + with Llvm.IoError _ -> + true + end; + + (* get_module *) + begin + let mb = Llvm.MemoryBuffer.of_file fn in + let m = begin try + Llvm_bitreader.get_module context mb + with x -> + Llvm.MemoryBuffer.dispose mb; + raise x + end in + Llvm.dispose_module m + end; + + (* corrupt the bitcode *) + let fn = fn ^ ".txt" in + begin let oc = open_out fn in + output_string oc "not a bitcode file\n"; + close_out oc + end; + + (* test get_module exceptions *) + test begin + try + let mb = Llvm.MemoryBuffer.of_file fn in + let m = begin try + Llvm_bitreader.get_module context mb + with x -> + Llvm.MemoryBuffer.dispose mb; + raise x + end in + Llvm.dispose_module m; + false + with Llvm_bitreader.Error _ -> + true + end diff --git a/test/Bindings/OCaml/bitwriter.ml b/test/Bindings/OCaml/bitwriter.ml new file mode 100644 index 0000000..7c803f6 --- /dev/null +++ b/test/Bindings/OCaml/bitwriter.ml @@ -0,0 +1,49 @@ +(* RUN: cp %s %T/bitwriter.ml + * RUN: %ocamlc -g -w -3 -warn-error A -package llvm.bitreader -package llvm.bitwriter -linkpkg %T/bitwriter.ml -o %t + * RUN: %t %t.bc + * RUN: %ocamlopt -g -w -3 -warn-error A -package llvm.bitreader -package llvm.bitwriter -linkpkg %T/bitwriter.ml -o %t + * RUN: %t %t.bc + * RUN: llvm-dis < %t.bc + * XFAIL: vg_leak + *) + +(* Note that this takes a moment to link, so it's best to keep the number of + individual tests low. *) + +let context = Llvm.global_context () + +let test x = if not x then exit 1 else () + +let read_file name = + let ic = open_in_bin name in + let len = in_channel_length ic in + let buf = String.create len in + + test ((input ic buf 0 len) = len); + + close_in ic; + + buf + +let temp_bitcode ?unbuffered m = + let temp_name, temp_oc = Filename.open_temp_file ~mode:[Open_binary] "" "" in + + test (Llvm_bitwriter.output_bitcode ?unbuffered temp_oc m); + flush temp_oc; + + let temp_buf = read_file temp_name in + + close_out temp_oc; + + temp_buf + +let _ = + let m = Llvm.create_module context "ocaml_test_module" in + + test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1)); + let file_buf = read_file Sys.argv.(1) in + + test (file_buf = temp_bitcode m); + test (file_buf = temp_bitcode ~unbuffered:false m); + test (file_buf = temp_bitcode ~unbuffered:true m); + test (file_buf = Llvm.MemoryBuffer.as_string (Llvm_bitwriter.write_bitcode_to_memory_buffer m)) diff --git a/test/Bindings/OCaml/core.ml b/test/Bindings/OCaml/core.ml new file mode 100644 index 0000000..c08351e --- /dev/null +++ b/test/Bindings/OCaml/core.ml @@ -0,0 +1,1507 @@ +(* RUN: cp %s %T/core.ml + * RUN: %ocamlc -g -warn-error A -package llvm.analysis -package llvm.bitwriter -linkpkg %T/core.ml -o %t + * RUN: %t %t.bc + * RUN: %ocamlopt -g -warn-error A -package llvm.analysis -package llvm.bitwriter -linkpkg %T/core.ml -o %t + * RUN: %t %t.bc + * RUN: llvm-dis < %t.bc > %t.ll + * RUN: FileCheck %s < %t.ll + * Do a second pass for things that shouldn't be anywhere. + * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t.ll + * XFAIL: vg_leak + *) + +(* Note: It takes several seconds for ocamlopt 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 - really just to help find which line is busted *) +let exit_status = ref 0 +let suite_name = ref "" +let group_name = ref "" +let case_num = ref 0 +let print_checkpoints = false +let context = global_context () +let i1_type = Llvm.i1_type context +let i8_type = Llvm.i8_type context +let i16_type = Llvm.i16_type context +let i32_type = Llvm.i32_type context +let i64_type = Llvm.i64_type context +let void_type = Llvm.void_type context +let float_type = Llvm.float_type context +let double_type = Llvm.double_type context +let fp128_type = Llvm.fp128_type context + +let group name = + group_name := !suite_name ^ "/" ^ name; + case_num := 0; + if print_checkpoints then + prerr_endline (" " ^ name ^ "...") + +let insist cond = + incr case_num; + if not cond then + exit_status := 10; + match print_checkpoints, cond with + | false, true -> () + | false, false -> + prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num)) + | true, true -> + prerr_endline (" " ^ (string_of_int !case_num)) + | true, false -> + prerr_endline (" " ^ (string_of_int !case_num) ^ " FAIL") + +let suite name f = + suite_name := name; + if print_checkpoints then + prerr_endline (name ^ ":"); + f () + + +(*===-- Fixture -----------------------------------------------------------===*) + +let filename = Sys.argv.(1) +let m = create_module context filename + + +(*===-- Conversion --------------------------------------------------------===*) + +let test_conversion () = + insist ("i32" = (string_of_lltype i32_type)); + let c = const_int i32_type 42 in + insist ("i32 42" = (string_of_llvalue c)) + + +(*===-- Target ------------------------------------------------------------===*) + +let test_target () = + begin group "triple"; + let trip = "i686-apple-darwin8" in + set_target_triple trip m; + insist (trip = target_triple m) + end; + + begin group "layout"; + let layout = "e" in + set_data_layout layout m; + insist (layout = data_layout m) + end + (* CHECK: target datalayout = "e" + * CHECK: target triple = "i686-apple-darwin8" + *) + + +(*===-- Constants ---------------------------------------------------------===*) + +let test_constants () = + (* CHECK: const_int{{.*}}i32{{.*}}-1 + *) + group "int"; + let c = const_int i32_type (-1) in + ignore (define_global "const_int" c m); + insist (i32_type = type_of c); + insist (is_constant c); + insist (Some (-1L) = int64_of_const c); + + (* CHECK: const_sext_int{{.*}}i64{{.*}}-1 + *) + group "sext int"; + let c = const_int i64_type (-1) in + ignore (define_global "const_sext_int" c m); + insist (i64_type = type_of c); + insist (Some (-1L) = int64_of_const c); + + (* CHECK: const_zext_int64{{.*}}i64{{.*}}4294967295 + *) + group "zext int64"; + let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in + ignore (define_global "const_zext_int64" c m); + insist (i64_type = type_of c); + insist (Some 4294967295L = int64_of_const c); + + (* CHECK: const_int_string{{.*}}i32{{.*}}-1 + *) + group "int string"; + let c = const_int_of_string i32_type "-1" 10 in + ignore (define_global "const_int_string" c m); + insist (i32_type = type_of c); + insist (None = (string_of_const c)); + insist (None = float_of_const c); + insist (Some (-1L) = int64_of_const c); + + (* CHECK: const_int64{{.*}}i64{{.*}}9223372036854775807 + *) + group "max int64"; + let c = const_of_int64 i64_type 9223372036854775807L true in + ignore (define_global "const_int64" c m) ; + insist (i64_type = type_of c); + insist (Some 9223372036854775807L = int64_of_const c); + + if Sys.word_size = 64; then begin + group "long int"; + let c = const_int i64_type (1 lsl 61) in + insist (c = const_of_int64 i64_type (Int64.of_int (1 lsl 61)) false) + end; + + (* CHECK: @const_string = global {{.*}}c"cruel\00world" + *) + group "string"; + let c = const_string context "cruel\000world" in + ignore (define_global "const_string" c m); + insist ((array_type i8_type 11) = type_of c); + insist ((Some "cruel\000world") = (string_of_const c)); + + (* CHECK: const_stringz{{.*}}"hi\00again\00" + *) + group "stringz"; + let c = const_stringz context "hi\000again" in + ignore (define_global "const_stringz" c m); + insist ((array_type i8_type 9) = type_of c); + + (* CHECK: const_single{{.*}}2.75 + * CHECK: const_double{{.*}}3.1459 + * CHECK: const_double_string{{.*}}2 + * CHECK: const_fake_fp128{{.*}}0xL00000000000000004000000000000000 + * CHECK: const_fp128_string{{.*}}0xLF3CB1CCF26FBC178452FB4EC7F91973F + *) + begin group "real"; + let cs = const_float float_type 2.75 in + ignore (define_global "const_single" cs m); + insist (float_type = type_of cs); + insist (float_of_const cs = Some 2.75); + + let cd = const_float double_type 3.1459 in + ignore (define_global "const_double" cd m); + insist (double_type = type_of cd); + insist (float_of_const cd = Some 3.1459); + + let cd = const_float_of_string double_type "2" in + ignore (define_global "const_double_string" cd m); + insist (double_type = type_of cd); + insist (float_of_const cd = Some 2.); + + let cd = const_float fp128_type 2. in + ignore (define_global "const_fake_fp128" cd m); + insist (fp128_type = type_of cd); + insist (float_of_const cd = Some 2.); + + let cd = const_float_of_string fp128_type "1e400" in + ignore (define_global "const_fp128_string" cd m); + insist (fp128_type = type_of cd); + insist (float_of_const cd = None); + end; + + let one = const_int i16_type 1 in + let two = const_int i16_type 2 in + let three = const_int i32_type 3 in + let four = const_int i32_type 4 in + + (* CHECK: const_array{{.*}}[i32 3, i32 4] + *) + group "array"; + let c = const_array i32_type [| three; four |] in + ignore (define_global "const_array" c m); + insist ((array_type i32_type 2) = (type_of c)); + insist (three = (const_element c 0)); + insist (four = (const_element c 1)); + + (* CHECK: const_vector{{.*}} + *) + group "vector"; + let c = const_vector [| one; two; one; two; + one; two; one; two |] in + ignore (define_global "const_vector" c m); + insist ((vector_type i16_type 8) = (type_of c)); + + (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4 + *) + group "structure"; + let c = const_struct context [| one; two; three; four |] in + ignore (define_global "const_structure" c m); + insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |]) + = (type_of c)); + + (* CHECK: const_null{{.*}}zeroinit + *) + group "null"; + let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type; + double_type |]) in + ignore (define_global "const_null" c m); + + (* CHECK: const_all_ones{{.*}}-1 + *) + group "all ones"; + let c = const_all_ones i64_type in + ignore (define_global "const_all_ones" c m); + + group "pointer null"; begin + (* CHECK: const_pointer_null = global i64* null + *) + let c = const_pointer_null (pointer_type i64_type) in + ignore (define_global "const_pointer_null" c m); + end; + + (* CHECK: const_undef{{.*}}undef + *) + group "undef"; + let c = undef i1_type in + ignore (define_global "const_undef" c m); + insist (i1_type = type_of c); + insist (is_undef c); + + group "constant arithmetic"; + (* CHECK: @const_neg = global i64 sub + * CHECK: @const_nsw_neg = global i64 sub nsw + * CHECK: @const_nuw_neg = global i64 sub nuw + * CHECK: @const_fneg = global double fsub + * CHECK: @const_not = global i64 xor + * CHECK: @const_add = global i64 add + * CHECK: @const_nsw_add = global i64 add nsw + * CHECK: @const_nuw_add = global i64 add nuw + * CHECK: @const_fadd = global double fadd + * CHECK: @const_sub = global i64 sub + * CHECK: @const_nsw_sub = global i64 sub nsw + * CHECK: @const_nuw_sub = global i64 sub nuw + * CHECK: @const_fsub = global double fsub + * CHECK: @const_mul = global i64 mul + * CHECK: @const_nsw_mul = global i64 mul nsw + * CHECK: @const_nuw_mul = global i64 mul nuw + * CHECK: @const_fmul = global double fmul + * CHECK: @const_udiv = global i64 udiv + * CHECK: @const_sdiv = global i64 sdiv + * CHECK: @const_exact_sdiv = global i64 sdiv exact + * CHECK: @const_fdiv = global double fdiv + * CHECK: @const_urem = global i64 urem + * CHECK: @const_srem = global i64 srem + * CHECK: @const_frem = global double frem + * CHECK: @const_and = global i64 and + * CHECK: @const_or = global i64 or + * CHECK: @const_xor = global i64 xor + * CHECK: @const_icmp = global i1 icmp sle + * CHECK: @const_fcmp = global i1 fcmp ole + *) + let void_ptr = pointer_type i8_type in + let five = const_int i64_type 5 in + let ffive = const_uitofp five double_type in + let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in + let foldbomb = const_ptrtoint foldbomb_gv i64_type in + let ffoldbomb = const_uitofp foldbomb double_type in + ignore (define_global "const_neg" (const_neg foldbomb) m); + ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m); + ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m); + ignore (define_global "const_fneg" (const_fneg ffoldbomb) m); + ignore (define_global "const_not" (const_not foldbomb) m); + ignore (define_global "const_add" (const_add foldbomb five) m); + ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m); + ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m); + ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m); + ignore (define_global "const_sub" (const_sub foldbomb five) m); + ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m); + ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m); + ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m); + ignore (define_global "const_mul" (const_mul foldbomb five) m); + ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m); + ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m); + ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m); + ignore (define_global "const_udiv" (const_udiv foldbomb five) m); + ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m); + ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m); + ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m); + ignore (define_global "const_urem" (const_urem foldbomb five) m); + ignore (define_global "const_srem" (const_srem foldbomb five) m); + ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m); + ignore (define_global "const_and" (const_and foldbomb five) m); + ignore (define_global "const_or" (const_or foldbomb five) m); + ignore (define_global "const_xor" (const_xor foldbomb five) m); + ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m); + ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m); + + group "constant casts"; + (* CHECK: const_trunc{{.*}}trunc + * CHECK: const_sext{{.*}}sext + * CHECK: const_zext{{.*}}zext + * CHECK: const_fptrunc{{.*}}fptrunc + * CHECK: const_fpext{{.*}}fpext + * CHECK: const_uitofp{{.*}}uitofp + * CHECK: const_sitofp{{.*}}sitofp + * CHECK: const_fptoui{{.*}}fptoui + * CHECK: const_fptosi{{.*}}fptosi + * CHECK: const_ptrtoint{{.*}}ptrtoint + * CHECK: const_inttoptr{{.*}}inttoptr + * CHECK: const_bitcast{{.*}}bitcast + * CHECK: const_intcast{{.*}}zext + *) + let i128_type = integer_type context 128 in + ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five) + i8_type) m); + ignore (define_global "const_sext" (const_sext foldbomb i128_type) m); + ignore (define_global "const_zext" (const_zext foldbomb i128_type) m); + ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m); + ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m); + ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m); + ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m); + ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m); + ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m); + ignore (define_global "const_ptrtoint" (const_ptrtoint + (const_gep (const_null (pointer_type i8_type)) + [| const_int i32_type 1 |]) + i32_type) m); + ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five) + void_ptr) m); + ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m); + ignore (define_global "const_intcast" + (const_intcast foldbomb i128_type ~is_signed:false) m); + + group "misc constants"; + (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null + * CHECK: const_gep{{.*}}getelementptr + * CHECK: const_select{{.*}}select + * CHECK: const_extractelement{{.*}}extractelement + * CHECK: const_insertelement{{.*}}insertelement + * CHECK: const_shufflevector = global <4 x i32> + *) + ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m); + ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m); + ignore (define_global "const_select" (const_select + (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 + let one = const_int i32_type 1 in + ignore (define_global "const_extractelement" (const_extractelement + (const_vector [| zero; one; zero; one |]) + (const_trunc foldbomb i32_type)) m); + ignore (define_global "const_insertelement" (const_insertelement + (const_vector [| zero; one; zero; one |]) + zero (const_trunc foldbomb i32_type)) m); + ignore (define_global "const_shufflevector" (const_shufflevector + (const_vector [| zero; one |]) + (const_vector [| one; zero |]) + (const_vector [| const_int i32_type 0; const_int i32_type 1; + const_int i32_type 2; const_int i32_type 3 |])) m); + + group "asm"; begin + let ft = function_type void_type [| i32_type; i32_type; i32_type |] in + ignore (const_inline_asm + ft + "" + "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}" + true + false) + end; + + group "recursive struct"; begin + let nsty = named_struct_type context "rec" in + let pty = pointer_type nsty in + struct_set_body nsty [| i32_type; pty |] false; + let elts = [| const_int i32_type 4; const_pointer_null pty |] in + let grec_init = const_named_struct nsty elts in + ignore (define_global "grec" grec_init m); + ignore (string_of_lltype nsty); + end + + +(*===-- Global Values -----------------------------------------------------===*) + +let test_global_values () = + let (++) x f = f x; x in + let zero32 = const_null i32_type in + + (* CHECK: GVal01 + *) + group "naming"; + let g = define_global "TEMPORARY" zero32 m in + insist ("TEMPORARY" = value_name g); + set_value_name "GVal01" g; + insist ("GVal01" = value_name g); + + (* CHECK: GVal02{{.*}}linkonce + *) + group "linkage"; + let g = define_global "GVal02" zero32 m ++ + set_linkage Linkage.Link_once in + insist (Linkage.Link_once = linkage g); + + (* CHECK: GVal03{{.*}}Hanalei + *) + group "section"; + let g = define_global "GVal03" zero32 m ++ + set_section "Hanalei" in + insist ("Hanalei" = section g); + + (* CHECK: GVal04{{.*}}hidden + *) + group "visibility"; + let g = define_global "GVal04" zero32 m ++ + set_visibility Visibility.Hidden in + insist (Visibility.Hidden = visibility g); + + (* CHECK: GVal05{{.*}}align 128 + *) + group "alignment"; + let g = define_global "GVal05" zero32 m ++ + set_alignment 128 in + insist (128 = alignment g); + + (* CHECK: GVal06{{.*}}dllexport + *) + group "dll_storage_class"; + let g = define_global "GVal06" zero32 m ++ + set_dll_storage_class DLLStorageClass.DLLExport in + insist (DLLStorageClass.DLLExport = dll_storage_class g) + + +(*===-- Global Variables --------------------------------------------------===*) + +let test_global_variables () = + let (++) x f = f x; x in + let forty_two32 = const_int i32_type 42 in + + group "declarations"; begin + (* CHECK: @GVar01 = external global i32 + * CHECK: @QGVar01 = external addrspace(3) global i32 + *) + 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); + + insist (None == lookup_global "QGVar01" m); + let g = declare_qualified_global i32_type "QGVar01" 3 m in + insist (is_declaration g); + insist (qualified_pointer_type float_type 3 == + type_of (declare_qualified_global float_type "QGVar01" 3 m)); + insist (g == declare_qualified_global i32_type "QGVar01" 3 m); + insist (match lookup_global "QGVar01" m with Some x -> x = g + | None -> false); + end; + + group "definitions"; begin + (* CHECK: @GVar02 = global i32 42 + * CHECK: @GVar03 = global i32 42 + * CHECK: @QGVar02 = addrspace(3) global i32 42 + * CHECK: @QGVar03 = addrspace(3) global i32 42 + *) + let g = define_global "GVar02" forty_two32 m in + let g2 = declare_global i32_type "GVar03" m ++ + set_initializer forty_two32 in + insist (not (is_declaration g)); + insist (not (is_declaration g2)); + insist ((global_initializer g) == (global_initializer g2)); + + let g = define_qualified_global "QGVar02" forty_two32 3 m in + let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++ + set_initializer forty_two32 in + insist (not (is_declaration g)); + insist (not (is_declaration g2)); + insist ((global_initializer g) == (global_initializer g2)); + end; + + (* CHECK: GVar04{{.*}}thread_local + *) + group "threadlocal"; + let g = define_global "GVar04" forty_two32 m ++ + set_thread_local true in + insist (is_thread_local g); + + (* CHECK: GVar05{{.*}}thread_local(initialexec) + *) + group "threadlocal_mode"; + let g = define_global "GVar05" forty_two32 m ++ + set_thread_local_mode ThreadLocalMode.InitialExec in + insist ((thread_local_mode g) = ThreadLocalMode.InitialExec); + + (* CHECK: GVar06{{.*}}externally_initialized + *) + group "externally_initialized"; + let g = define_global "GVar06" forty_two32 m ++ + set_externally_initialized true in + insist (is_externally_initialized g); + + (* CHECK-NOWHERE-NOT: GVar07 + *) + group "delete"; + let g = define_global "GVar07" forty_two32 m in + delete_global g; + + (* CHECK: ConstGlobalVar{{.*}}constant + *) + group "constant"; + let g = define_global "ConstGlobalVar" forty_two32 m in + insist (not (is_global_constant g)); + set_global_constant true g; + insist (is_global_constant g); + + begin group "iteration"; + let m = create_module context "temp" in + + insist (At_end m = global_begin m); + insist (At_start m = global_end m); + + let g1 = declare_global i32_type "One" m in + let g2 = declare_global i32_type "Two" m in + + insist (Before g1 = global_begin m); + insist (Before g2 = global_succ g1); + insist (At_end m = global_succ g2); + + insist (After g2 = global_end m); + insist (After g1 = global_pred g2); + insist (At_start m = global_pred g1); + + let lf s x = s ^ "->" ^ value_name x in + insist ("->One->Two" = fold_left_globals lf "" m); + + let rf x s = value_name x ^ "<-" ^ s in + insist ("One<-Two<-" = fold_right_globals rf m ""); + + dispose_module m + end + +(* String globals built below are emitted here. + * CHECK: build_global_string{{.*}}stringval + *) + + +(*===-- Uses --------------------------------------------------------------===*) + +let test_uses () = + let ty = function_type i32_type [| i32_type; i32_type |] in + let fn = define_function "use_function" ty m in + let b = builder_at_end context (entry_block fn) in + + let p1 = param fn 0 in + let p2 = param fn 1 in + let v1 = build_add p1 p2 "v1" b in + let v2 = build_add p1 v1 "v2" b in + let _ = build_add v1 v2 "v3" b in + + let lf s u = value_name (user u) ^ "->" ^ s in + insist ("v2->v3->" = fold_left_uses lf "" v1); + let rf u s = value_name (user u) ^ "<-" ^ s in + insist ("v3<-v2<-" = fold_right_uses rf v1 ""); + + let lf s u = value_name (used_value u) ^ "->" ^ s in + insist ("v1->v1->" = fold_left_uses lf "" v1); + + let rf u s = value_name (used_value u) ^ "<-" ^ s in + insist ("v1<-v1<-" = fold_right_uses rf v1 ""); + + ignore (build_unreachable b) + + +(*===-- Users -------------------------------------------------------------===*) + +let test_users () = + let ty = function_type i32_type [| i32_type; i32_type |] in + let fn = define_function "user_function" ty m in + let b = builder_at_end context (entry_block fn) in + + let p1 = param fn 0 in + let p2 = param fn 1 in + let a3 = build_alloca i32_type "user_alloca" b in + let p3 = build_load a3 "user_load" b in + let i = build_add p1 p2 "sum" b in + + insist ((num_operands i) = 2); + insist ((operand i 0) = p1); + insist ((operand i 1) = p2); + + set_operand i 1 p3; + insist ((operand i 1) != p2); + insist ((operand i 1) = p3); + + ignore (build_unreachable b) + + +(*===-- Aliases -----------------------------------------------------------===*) + +let test_aliases () = + (* CHECK: @alias = alias i32* @aliasee + *) + let forty_two32 = const_int i32_type 42 in + let v = define_global "aliasee" forty_two32 m in + ignore (add_alias m (pointer_type i32_type) v "alias") + + +(*===-- Functions ---------------------------------------------------------===*) + +let test_functions () = + let ty = function_type i32_type [| i32_type; i64_type |] in + let ty2 = function_type i8_type [| i8_type; i64_type |] in + + (* CHECK: declare i32 @Fn1(i32, i64) + *) + begin group "declare"; + insist (None = lookup_function "Fn1" m); + let fn = declare_function "Fn1" ty m in + 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); + insist (m == global_parent fn) + end; + + (* CHECK-NOWHERE-NOT: Fn2 + *) + group "delete"; + let fn = declare_function "Fn2" ty m in + delete_function fn; + + (* CHECK: define{{.*}}Fn3 + *) + group "define"; + let fn = define_function "Fn3" ty m in + insist (not (is_declaration fn)); + insist (1 = Array.length (basic_blocks fn)); + ignore (build_unreachable (builder_at_end context (entry_block fn))); + + (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2 + *) + group "params"; + let fn = define_function "Fn4" ty m in + let params = params fn in + insist (2 = Array.length params); + insist (params.(0) = param fn 0); + insist (params.(1) = param fn 1); + insist (i32_type = type_of params.(0)); + insist (i64_type = type_of params.(1)); + set_value_name "Param1" params.(0); + set_value_name "Param2" params.(1); + ignore (build_unreachable (builder_at_end context (entry_block fn))); + + (* CHECK: fastcc{{.*}}Fn5 + *) + group "callconv"; + let fn = define_function "Fn5" ty m in + 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 context (entry_block fn))); + + begin group "gc"; + (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack + *) + let fn = define_function "Fn6" ty m in + insist (None = gc fn); + set_gc (Some "ocaml") fn; + insist (Some "ocaml" = gc fn); + set_gc None fn; + insist (None = gc fn); + set_gc (Some "shadowstack") fn; + ignore (build_unreachable (builder_at_end context (entry_block fn))); + end; + + begin group "iteration"; + let m = create_module context "temp" in + + insist (At_end m = function_begin m); + insist (At_start m = function_end m); + + let f1 = define_function "One" ty m in + let f2 = define_function "Two" ty m in + + insist (Before f1 = function_begin m); + insist (Before f2 = function_succ f1); + insist (At_end m = function_succ f2); + + insist (After f2 = function_end m); + insist (After f1 = function_pred f2); + insist (At_start m = function_pred f1); + + let lf s x = s ^ "->" ^ value_name x in + insist ("->One->Two" = fold_left_functions lf "" m); + + let rf x s = value_name x ^ "<-" ^ s in + insist ("One<-Two<-" = fold_right_functions rf m ""); + + dispose_module m + end + + +(*===-- Params ------------------------------------------------------------===*) + +let test_params () = + begin group "iteration"; + let m = create_module context "temp" in + + let vf = define_function "void" (function_type void_type [| |]) m in + + insist (At_end vf = param_begin vf); + insist (At_start vf = param_end vf); + + let ty = function_type void_type [| i32_type; i32_type |] in + let f = define_function "f" ty m in + let p1 = param f 0 in + let p2 = param f 1 in + set_value_name "One" p1; + set_value_name "Two" p2; + add_param_attr p1 Attribute.Sext; + add_param_attr p2 Attribute.Noalias; + remove_param_attr p2 Attribute.Noalias; + add_function_attr f Attribute.Nounwind; + add_function_attr f Attribute.Noreturn; + remove_function_attr f Attribute.Noreturn; + + insist (Before p1 = param_begin f); + insist (Before p2 = param_succ p1); + insist (At_end f = param_succ p2); + + insist (After p2 = param_end f); + insist (After p1 = param_pred p2); + insist (At_start f = param_pred p1); + + let lf s x = s ^ "->" ^ value_name x in + insist ("->One->Two" = fold_left_params lf "" f); + + let rf x s = value_name x ^ "<-" ^ s in + insist ("One<-Two<-" = fold_right_params rf f ""); + + dispose_module m + end + + +(*===-- Basic Blocks ------------------------------------------------------===*) + +let test_basic_blocks () = + let ty = function_type void_type [| |] in + + (* CHECK: Bb1 + *) + group "entry"; + let fn = declare_function "X" ty m in + let bb = append_block context "Bb1" fn in + insist (bb = entry_block fn); + ignore (build_unreachable (builder_at_end context bb)); + + (* CHECK-NOWHERE-NOT: Bb2 + *) + group "delete"; + let fn = declare_function "X2" ty m in + let bb = append_block context "Bb2" fn in + delete_block bb; + + group "insert"; + let fn = declare_function "X3" ty m in + let bbb = append_block context "b" fn in + let bba = insert_block context "a" bbb in + insist ([| bba; bbb |] = basic_blocks fn); + ignore (build_unreachable (builder_at_end context bba)); + ignore (build_unreachable (builder_at_end context bbb)); + + (* CHECK: Bb3 + *) + group "name/value"; + let fn = define_function "X4" ty m in + let bb = entry_block fn in + ignore (build_unreachable (builder_at_end context bb)); + let bbv = value_of_block bb in + set_value_name "Bb3" bbv; + insist ("Bb3" = value_name bbv); + + group "casts"; + let fn = define_function "X5" ty m in + let bb = entry_block fn in + ignore (build_unreachable (builder_at_end context 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))); + + begin group "iteration"; + let m = create_module context "temp" in + let f = declare_function "Temp" (function_type i32_type [| |]) m in + + insist (At_end f = block_begin f); + insist (At_start f = block_end f); + + let b1 = append_block context "One" f in + let b2 = append_block context "Two" f in + + insist (Before b1 = block_begin f); + insist (Before b2 = block_succ b1); + insist (At_end f = block_succ b2); + + insist (After b2 = block_end f); + insist (After b1 = block_pred b2); + insist (At_start f = block_pred b1); + + let lf s x = s ^ "->" ^ value_name (value_of_block x) in + insist ("->One->Two" = fold_left_blocks lf "" f); + + let rf x s = value_name (value_of_block x) ^ "<-" ^ s in + insist ("One<-Two<-" = fold_right_blocks rf f ""); + + dispose_module m + end + + +(*===-- Instructions ------------------------------------------------------===*) + +let test_instructions () = + begin group "iteration"; + let m = create_module context "temp" in + let fty = function_type void_type [| i32_type; i32_type |] in + let f = define_function "f" fty m in + let bb = entry_block f in + let b = builder_at context (At_end bb) in + + insist (At_end bb = instr_begin bb); + insist (At_start bb = instr_end bb); + + let i1 = build_add (param f 0) (param f 1) "One" b in + let i2 = build_sub (param f 0) (param f 1) "Two" b in + + insist (Before i1 = instr_begin bb); + insist (Before i2 = instr_succ i1); + insist (At_end bb = instr_succ i2); + + insist (After i2 = instr_end bb); + insist (After i1 = instr_pred i2); + insist (At_start bb = instr_pred i1); + + let lf s x = s ^ "->" ^ value_name x in + insist ("->One->Two" = fold_left_instrs lf "" bb); + + let rf x s = value_name x ^ "<-" ^ s in + insist ("One<-Two<-" = fold_right_instrs rf bb ""); + + dispose_module m + end; + + group "clone instr"; + begin + (* CHECK: %clone = add i32 %0, 2 + *) + let fty = function_type void_type [| i32_type |] in + let fn = define_function "BuilderParent" fty m in + let bb = entry_block fn in + let b = builder_at_end context bb in + let p = param fn 0 in + let sum = build_add p p "sum" b in + let y = const_int i32_type 2 in + let clone = instr_clone sum in + set_operand clone 0 p; + set_operand clone 1 y; + insert_into_builder clone "clone" b; + ignore (build_ret_void b) + end + + +(*===-- Builder -----------------------------------------------------------===*) + +let test_builder () = + let (++) x f = f x; x in + + begin group "parent"; + insist (try + ignore (insertion_block (builder context)); + false + with Not_found -> + true); + + let fty = function_type void_type [| i32_type |] in + let fn = define_function "BuilderParent" fty m in + let bb = entry_block fn in + let b = builder_at_end context bb in + let p = param fn 0 in + let sum = build_add p p "sum" b in + ignore (build_ret_void b); + + insist (fn = block_parent bb); + insist (fn = param_parent p); + insist (bb = instr_parent sum); + insist (bb = insertion_block b) + end; + + group "ret void"; + begin + (* CHECK: ret void + *) + let fty = function_type void_type [| |] in + let fn = declare_function "X6" fty m in + let b = builder_at_end context (append_block context "Bb01" fn) in + ignore (build_ret_void b) + end; + + group "ret aggregate"; + begin + (* CHECK: ret { i8, i64 } { i8 4, i64 5 } + *) + let sty = struct_type context [| i8_type; i64_type |] in + let fty = function_type sty [| |] in + let fn = declare_function "XA6" fty m in + let b = builder_at_end context (append_block context "Bb01" fn) in + let agg = [| const_int i8_type 4; const_int i64_type 5 |] in + ignore (build_aggregate_ret agg b) + end; + + (* The rest of the tests will use one big function. *) + let fty = function_type i32_type [| i32_type; i32_type |] in + let fn = define_function "X7" fty m in + let atentry = builder_at_end context (entry_block fn) in + let p1 = param fn 0 ++ set_value_name "P1" in + let p2 = param fn 1 ++ set_value_name "P2" in + let f1 = build_uitofp p1 float_type "F1" atentry in + let f2 = build_uitofp p2 float_type "F2" atentry in + + let bb00 = append_block context "Bb00" fn in + ignore (build_unreachable (builder_at_end context bb00)); + + group "function attribute"; + begin + ignore (add_function_attr fn Attribute.UWTable); + (* CHECK: X7{{.*}}#0 + * #0 is uwtable, defined at EOF. + *) + insist ([Attribute.UWTable] = function_attr fn); + end; + + group "casts"; begin + let void_ptr = pointer_type i8_type in + + (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8 + * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8 + * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8 + * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32 + * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32 + * CHECK-DAG: %build_sext = sext i32 %build_zext to i64 + * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64 + * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64 + * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float + * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double + * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32 + * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64 + * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float + * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float + * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double + * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double + * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to i8* + * CHECK-DAG: %build_ptrtoint = ptrtoint i8* %build_inttoptr to i64 + * CHECK-DAG: %build_ptrtoint2 = ptrtoint i8* %build_inttoptr to i64 + * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double + * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double + * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double + * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double + * CHECK-DAG: %build_pointercast = bitcast i8* %build_inttoptr to i16* + *) + let inst28 = build_trunc p1 i8_type "build_trunc" atentry in + let inst29 = build_zext inst28 i32_type "build_zext" atentry in + let inst30 = build_sext inst29 i64_type "build_sext" atentry in + let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in + let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in + ignore(build_fptoui inst31 i32_type "build_fptoui" atentry); + ignore(build_fptosi inst32 i64_type "build_fptosi" atentry); + let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in + ignore(build_fpext inst35 double_type "build_fpext" atentry); + let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in + let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in + ignore(build_bitcast inst38 double_type "build_bitcast" atentry); + ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry); + ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry); + ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry); + ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry); + + ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry); + ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry); + ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry); + ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry); + ignore(build_intcast inst29 i64_type "build_sext3" atentry); + ignore(build_intcast p1 i8_type "build_trunc3" atentry); + ignore(build_fpcast inst35 double_type "build_fpext2" atentry); + ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry); + end; + + group "comparisons"; begin + (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2 + * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1 + * CHECK: %build_fcmp_false = fcmp false float %F1, %F2 + * CHECK: %build_fcmp_true = fcmp true float %F2, %F1 + * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null + * CHECK: %build_is_not_null = icmp ne i8* %X1, null + * CHECK: %build_ptrdiff + *) + let c = build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry in + insist (Some Icmp.Ne = icmp_predicate c); + insist (None = fcmp_predicate c); + + let c = build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry in + insist (Some Icmp.Sle = icmp_predicate c); + insist (None = fcmp_predicate c); + + let c = build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry in + (* insist (Some Fcmp.False = fcmp_predicate c); *) + insist (None = icmp_predicate c); + + let c = build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry in + (* insist (Some Fcmp.True = fcmp_predicate c); *) + insist (None = icmp_predicate c); + + let g0 = declare_global (pointer_type i8_type) "g0" m in + let g1 = declare_global (pointer_type i8_type) "g1" m in + let p0 = build_load g0 "X0" atentry in + let p1 = build_load g1 "X1" atentry in + ignore (build_is_null p0 "build_is_null" atentry); + ignore (build_is_not_null p1 "build_is_not_null" atentry); + ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry); + end; + + group "miscellaneous"; begin + (* CHECK: %build_call = tail call cc63 i32 @{{.*}}(i32 signext %P2, i32 %P1) + * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2 + * CHECK: %build_va_arg = va_arg i8** null, i32 + * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2 + * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2 + * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> + * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0 + * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1 + *) + let ci = build_call fn [| p2; p1 |] "build_call" atentry in + insist (CallConv.c = instruction_call_conv ci); + set_instruction_call_conv 63 ci; + insist (63 = instruction_call_conv ci); + insist (not (is_tail_call ci)); + set_tail_call true ci; + insist (is_tail_call ci); + add_instruction_param_attr ci 1 Attribute.Sext; + add_instruction_param_attr ci 2 Attribute.Noalias; + remove_instruction_param_attr ci 2 Attribute.Noalias; + + let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in + ignore (build_select inst46 p1 p2 "build_select" atentry); + ignore (build_va_arg + (const_null (pointer_type (pointer_type i8_type))) + i32_type "build_va_arg" atentry); + + (* Set up some vector vregs. *) + 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 sty = struct_type context [| i32_type; i8_type |] in + + ignore (build_extractelement vec1 p2 "build_extractelement" atentry); + ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry); + ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry); + + let p = build_alloca sty "ba" atentry in + let agg = build_load p "bl" atentry in + let agg0 = build_insertvalue agg (const_int i32_type 1) 0 + "build_insertvalue0" atentry in + let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1 + "build_insertvalue1" atentry in + ignore (build_extractvalue agg1 1 "build_extractvalue" atentry) + end; + + group "metadata"; begin + (* CHECK: %metadata = add i32 %P1, %P2, !test !1 + * !1 is metadata emitted at EOF. + *) + let i = build_add p1 p2 "metadata" atentry in + insist ((has_metadata i) = false); + + let m1 = const_int i32_type 1 in + let m2 = mdstring context "metadata test" in + let md = mdnode context [| m1; m2 |] in + + let kind = mdkind_id context "test" in + set_metadata i kind md; + + insist ((has_metadata i) = true); + insist ((metadata i kind) = Some md); + + clear_metadata i kind; + + insist ((has_metadata i) = false); + insist ((metadata i kind) = None); + + set_metadata i kind md + end; + + group "named metadata"; begin + (* !llvm.module.flags is emitted at EOF. *) + let n1 = const_int i32_type 1 in + let n2 = mdstring context "Debug Info Version" in + let n3 = const_int i32_type 2 in + let md = mdnode context [| n1; n2; n3 |] in + add_named_metadata_operand m "llvm.module.flags" md; + + insist ((get_named_metadata m "llvm.module.flags") = [| md |]) + end; + + group "dbg"; begin + (* CHECK: %dbg = add i32 %P1, %P2, !dbg !2 + * !2 is metadata emitted at EOF. + *) + insist ((current_debug_location atentry) = None); + + let m_line = const_int i32_type 2 in + let m_col = const_int i32_type 3 in + let m_scope = mdnode context [| |] in + let m_inlined = mdnode context [| |] in + let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in + set_current_debug_location atentry md; + + insist ((current_debug_location atentry) = Some md); + + let i = build_add p1 p2 "dbg" atentry in + insist ((has_metadata i) = true); + + clear_current_debug_location atentry + end; + + group "ret"; begin + (* CHECK: ret{{.*}}P1 + *) + let ret = build_ret p1 atentry in + position_before ret atentry + end; + + (* see test/Feature/exception.ll *) + let bblpad = append_block context "Bblpad" fn in + let rt = struct_type context [| pointer_type i8_type; i32_type |] in + let ft = var_arg_function_type i32_type [||] in + let personality = declare_function "__gxx_personality_v0" ft m in + let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in + let ztid = declare_global (pointer_type i8_type) "_ZTId" m in + let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in + begin + set_global_constant true ztic; + set_global_constant true ztid; + set_global_constant true ztipkc; + let lp = build_landingpad rt personality 0 "lpad" + (builder_at_end context bblpad) in begin + set_cleanup lp true; + add_clause lp ztic; + insist((pointer_type (pointer_type i8_type)) = type_of ztid); + let ety = pointer_type (pointer_type i8_type) in + add_clause lp (const_array ety [| ztipkc; ztid |]); + ignore (build_resume lp (builder_at_end context bblpad)); + end; + (* CHECK: landingpad{{.*}}personality{{.*}}__gxx_personality_v0 + * CHECK: cleanup + * CHECK: catch{{.*}}i8**{{.*}}@_ZTIc + * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId + * CHECK: resume + * *) + end; + + group "br"; begin + (* CHECK: br{{.*}}Bb02 + *) + let bb02 = append_block context "Bb02" fn in + let b = builder_at_end context bb02 in + let br = build_br bb02 b in + insist (successors br = [| bb02 |]) ; + insist (is_conditional br = false) ; + insist (get_branch br = Some (`Unconditional bb02)) ; + end; + + group "cond_br"; begin + (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00 + *) + let bb03 = append_block context "Bb03" fn in + let b = builder_at_end context bb03 in + let cond = build_trunc p1 i1_type "build_br" b in + let br = build_cond_br cond bb03 bb00 b in + insist (num_successors br = 2) ; + insist (successor br 0 = bb03) ; + insist (successor br 1 = bb00) ; + insist (is_conditional br = true) ; + insist (get_branch br = Some (`Conditional (cond, bb03, bb00))) ; + end; + + group "switch"; begin + (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3 + * CHECK: 2,{{.*}}SwiBlock2 + *) + let bb1 = append_block context "SwiBlock1" fn in + let bb2 = append_block context "SwiBlock2" fn in + ignore (build_unreachable (builder_at_end context bb2)); + let bb3 = append_block context "SwiBlock3" fn in + ignore (build_unreachable (builder_at_end context bb3)); + let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin + ignore (add_case si (const_int i32_type 2) bb2); + insist (switch_default_dest si = bb3); + end; + insist (num_successors si = 2) ; + insist (get_branch si = None) ; + end; + + group "malloc/free"; begin + (* CHECK: call{{.*}}@malloc(i32 ptrtoint + * CHECK: call{{.*}}@free(i8* + * CHECK: call{{.*}}@malloc(i32 % + *) + let bb1 = append_block context "MallocBlock1" fn in + let m1 = (build_malloc (pointer_type i32_type) "m1" + (builder_at_end context bb1)) in + ignore (build_free m1 (builder_at_end context bb1)); + ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1)); + ignore (build_unreachable (builder_at_end context bb1)); + end; + + group "indirectbr"; begin + (* CHECK: indirectbr i8* blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3] + *) + let bb1 = append_block context "IBRBlock1" fn in + + let bb2 = append_block context "IBRBlock2" fn in + ignore (build_unreachable (builder_at_end context bb2)); + + let bb3 = append_block context "IBRBlock3" fn in + ignore (build_unreachable (builder_at_end context bb3)); + + let addr = block_address fn bb2 in + let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in + ignore (add_destination ibr bb2); + ignore (add_destination ibr bb3) + end; + + group "invoke"; begin + (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2 + * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad + *) + let bb04 = append_block context "Bb04" fn in + let b = builder_at_end context bb04 in + ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b) + end; + + group "unreachable"; begin + (* CHECK: unreachable + *) + let bb06 = append_block context "Bb06" fn in + let b = builder_at_end context bb06 in + ignore (build_unreachable b) + end; + + group "arithmetic"; begin + let bb07 = append_block context "Bb07" fn in + let b = builder_at_end context bb07 in + + (* CHECK: %build_add = add i32 %P1, %P2 + * CHECK: %build_nsw_add = add nsw i32 %P1, %P2 + * CHECK: %build_nuw_add = add nuw i32 %P1, %P2 + * CHECK: %build_fadd = fadd float %F1, %F2 + * CHECK: %build_sub = sub i32 %P1, %P2 + * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2 + * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2 + * CHECK: %build_fsub = fsub float %F1, %F2 + * CHECK: %build_mul = mul i32 %P1, %P2 + * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2 + * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2 + * CHECK: %build_fmul = fmul float %F1, %F2 + * CHECK: %build_udiv = udiv i32 %P1, %P2 + * CHECK: %build_sdiv = sdiv i32 %P1, %P2 + * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2 + * CHECK: %build_fdiv = fdiv float %F1, %F2 + * CHECK: %build_urem = urem i32 %P1, %P2 + * CHECK: %build_srem = srem i32 %P1, %P2 + * CHECK: %build_frem = frem float %F1, %F2 + * CHECK: %build_shl = shl i32 %P1, %P2 + * CHECK: %build_lshl = lshr i32 %P1, %P2 + * CHECK: %build_ashl = ashr i32 %P1, %P2 + * CHECK: %build_and = and i32 %P1, %P2 + * CHECK: %build_or = or i32 %P1, %P2 + * CHECK: %build_xor = xor i32 %P1, %P2 + * CHECK: %build_neg = sub i32 0, %P1 + * CHECK: %build_nsw_neg = sub nsw i32 0, %P1 + * CHECK: %build_nuw_neg = sub nuw i32 0, %P1 + * CHECK: %build_fneg = fsub float {{.*}}0{{.*}}, %F1 + * CHECK: %build_not = xor i32 %P1, -1 + *) + ignore (build_add p1 p2 "build_add" b); + ignore (build_nsw_add p1 p2 "build_nsw_add" b); + ignore (build_nuw_add p1 p2 "build_nuw_add" b); + ignore (build_fadd f1 f2 "build_fadd" b); + ignore (build_sub p1 p2 "build_sub" b); + ignore (build_nsw_sub p1 p2 "build_nsw_sub" b); + ignore (build_nuw_sub p1 p2 "build_nuw_sub" b); + ignore (build_fsub f1 f2 "build_fsub" b); + ignore (build_mul p1 p2 "build_mul" b); + ignore (build_nsw_mul p1 p2 "build_nsw_mul" b); + ignore (build_nuw_mul p1 p2 "build_nuw_mul" b); + ignore (build_fmul f1 f2 "build_fmul" b); + ignore (build_udiv p1 p2 "build_udiv" b); + ignore (build_sdiv p1 p2 "build_sdiv" b); + ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b); + ignore (build_fdiv f1 f2 "build_fdiv" b); + ignore (build_urem p1 p2 "build_urem" b); + ignore (build_srem p1 p2 "build_srem" b); + ignore (build_frem f1 f2 "build_frem" b); + ignore (build_shl p1 p2 "build_shl" b); + ignore (build_lshr p1 p2 "build_lshl" b); + ignore (build_ashr p1 p2 "build_ashl" b); + ignore (build_and p1 p2 "build_and" b); + ignore (build_or p1 p2 "build_or" b); + ignore (build_xor p1 p2 "build_xor" b); + ignore (build_neg p1 "build_neg" b); + ignore (build_nsw_neg p1 "build_nsw_neg" b); + ignore (build_nuw_neg p1 "build_nuw_neg" b); + ignore (build_fneg f1 "build_fneg" b); + ignore (build_not p1 "build_not" b); + ignore (build_unreachable b) + end; + + group "memory"; begin + let bb08 = append_block context "Bb08" fn in + let b = builder_at_end context bb08 in + + (* CHECK: %build_alloca = alloca i32 + * CHECK: %build_array_alloca = alloca i32, i32 %P2 + * CHECK: %build_load = load volatile i32* %build_array_alloca, align 4 + * CHECK: store volatile i32 %P2, i32* %build_alloca, align 4 + * CHECK: %build_gep = getelementptr i32* %build_array_alloca, i32 %P2 + * CHECK: %build_in_bounds_gep = getelementptr inbounds i32* %build_array_alloca, i32 %P2 + * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1 + * CHECK: %build_atomicrmw = atomicrmw xchg i8* %p, i8 42 seq_cst + *) + let alloca = build_alloca i32_type "build_alloca" b in + let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in + + let load = build_load array_alloca "build_load" b in + ignore(set_alignment 4 load); + ignore(set_volatile true load); + insist(true = is_volatile load); + insist(4 = alignment load); + + let store = build_store p2 alloca b in + ignore(set_volatile true store); + ignore(set_alignment 4 store); + insist(true = is_volatile store); + insist(4 = alignment store); + ignore(build_gep array_alloca [| p2 |] "build_gep" b); + ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b); + + let sty = struct_type context [| i32_type; i8_type |] in + let alloca2 = build_alloca sty "build_alloca2" b in + ignore(build_struct_gep alloca2 1 "build_struct_gep" b); + + let p = build_alloca i8_type "p" b in + ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42) + AtomicOrdering.SequentiallyConsistent false "build_atomicrmw" + b); + + ignore(build_unreachable b) + end; + + group "string"; begin + let bb09 = append_block context "Bb09" fn in + let b = builder_at_end context bb09 in + let p = build_alloca (pointer_type i8_type) "p" b in + (* build_global_string is emitted above. + * CHECK: store{{.*}}build_global_string1{{.*}}p + * *) + ignore (build_global_string "stringval" "build_global_string" b); + let g = build_global_stringptr "stringval" "build_global_string1" b in + ignore (build_store g p b); + ignore(build_unreachable b); + end; + + group "phi"; begin + (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2 + *) + let b1 = append_block context "PhiBlock1" fn in + let b2 = append_block context "PhiBlock2" fn in + + let jb = append_block context "PhiJoinBlock" fn in + ignore (build_br jb (builder_at_end context b1)); + ignore (build_br jb (builder_at_end context b2)); + let at_jb = builder_at_end context 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 + +(* End-of-file checks for things like metdata and attributes. + * CHECK: attributes #0 = {{.*}}uwtable{{.*}} + * CHECK: !llvm.module.flags = !{!0} + * CHECK: !0 = metadata !{i32 1, metadata !"Debug Info Version", i32 2} + * CHECK: !1 = metadata !{i32 1, metadata !"metadata test"} + * CHECK: !2 = metadata !{i32 2, i32 3, metadata !3, metadata !3} + *) + +(*===-- Pass Managers -----------------------------------------------------===*) + +let test_pass_manager () = + let (++) x f = ignore (f x); x in + + begin group "module pass manager"; + ignore (PassManager.create () + ++ PassManager.run_module m + ++ PassManager.dispose) + end; + + begin group "function pass manager"; + let fty = function_type void_type [| |] in + let fn = define_function "FunctionPassManager" fty m in + ignore (build_ret_void (builder_at_end context (entry_block fn))); + + ignore (PassManager.create_function m + ++ PassManager.initialize + ++ PassManager.run_function fn + ++ PassManager.finalize + ++ PassManager.dispose) + end + + +(*===-- Memory Buffer -----------------------------------------------------===*) + +let test_memory_buffer () = + group "memory buffer"; + let buf = MemoryBuffer.of_string "foobar" in + insist ((MemoryBuffer.as_string buf) = "foobar") + + +(*===-- 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); + + dispose_module m + + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + suite "conversion" test_conversion; + suite "target" test_target; + suite "constants" test_constants; + suite "global values" test_global_values; + suite "global variables" test_global_variables; + suite "uses" test_uses; + suite "users" test_users; + suite "aliases" test_aliases; + suite "functions" test_functions; + suite "params" test_params; + suite "basic blocks" test_basic_blocks; + suite "instructions" test_instructions; + suite "builder" test_builder; + suite "pass manager" test_pass_manager; + suite "memory buffer" test_memory_buffer; + suite "writer" test_writer; (* Keep this last; it disposes m. *) + exit !exit_status diff --git a/test/Bindings/OCaml/executionengine.ml b/test/Bindings/OCaml/executionengine.ml new file mode 100644 index 0000000..893f988 --- /dev/null +++ b/test/Bindings/OCaml/executionengine.ml @@ -0,0 +1,105 @@ +(* RUN: cp %s %T/executionengine.ml + * RUN: %ocamlc -g -warn-error A -package llvm.executionengine -linkpkg %T/executionengine.ml -o %t + * RUN: %t + * RUN: %ocamlopt -g -warn-error A -package llvm.executionengine -linkpkg %T/executionengine.ml -o %t + * RUN: %t + * REQUIRES: native, object-emission + * XFAIL: vg_leak + *) + +open Llvm +open Llvm_executionengine +open Llvm_target + +(* Note that this takes a moment to link, so it's best to keep the number of + individual tests low. *) + +let context = global_context () +let i8_type = Llvm.i8_type context +let i32_type = Llvm.i32_type context +let i64_type = Llvm.i64_type context +let double_type = Llvm.double_type context + +let () = + assert (Llvm_executionengine.initialize ()) + +let bomb msg = + prerr_endline msg; + exit 2 + +let define_getglobal m pg = + let fn = define_function "getglobal" (function_type i32_type [||]) m in + let b = builder_at_end (global_context ()) (entry_block fn) in + let g = build_call pg [||] "" b in + ignore (build_ret g b); + fn + +let define_plus m = + let fn = define_function "plus" (function_type i32_type [| i32_type; + i32_type |]) m in + let b = builder_at_end (global_context ()) (entry_block fn) in + let add = build_add (param fn 0) (param fn 1) "sum" b in + ignore (build_ret add b); + fn + +let test_executionengine () = + let open Ctypes in + + (* create *) + let m = create_module (global_context ()) "test_module" in + let ee = create m in + + (* add plus *) + let plus = define_plus m in + + (* add module *) + let m2 = create_module (global_context ()) "test_module2" in + add_module m2 ee; + + (* add global mapping *) + (* BROKEN: see PR20656 *) + (* let g = declare_function "g" (function_type i32_type [||]) m2 in + let cg = coerce (Foreign.funptr (void @-> returning int32_t)) (ptr void) + (fun () -> 42l) in + add_global_mapping g cg ee; + + (* check g *) + let cg' = get_pointer_to_global g (ptr void) ee in + if 0 <> ptr_compare cg cg' then bomb "int pointers to g differ"; + + (* add getglobal *) + let getglobal = define_getglobal m2 g in*) + + (* run_static_ctors *) + run_static_ctors ee; + + (* call plus *) + let cplusty = Foreign.funptr (int32_t @-> int32_t @-> returning int32_t) in + let cplus = get_pointer_to_global plus cplusty ee in + if 4l <> cplus 2l 2l then bomb "plus didn't work"; + + (* call getglobal *) + (* let cgetglobalty = Foreign.funptr (void @-> returning int32_t) in + let cgetglobal = get_pointer_to_global getglobal cgetglobalty ee in + if 42l <> cgetglobal () then bomb "getglobal didn't work"; *) + + (* remove_module *) + remove_module m2 ee; + dispose_module m2; + + (* run_static_dtors *) + run_static_dtors ee; + + (* Show that the data layout binding links and runs.*) + let dl = data_layout ee in + + (* Demonstrate that a garbage pointer wasn't returned. *) + let ty = DataLayout.intptr_type context dl in + if ty != i32_type && ty != i64_type then bomb "target_data did not work"; + + (* dispose *) + dispose ee + +let () = + test_executionengine (); + Gc.compact () diff --git a/test/Bindings/OCaml/ext_exc.ml b/test/Bindings/OCaml/ext_exc.ml new file mode 100644 index 0000000..2b44803 --- /dev/null +++ b/test/Bindings/OCaml/ext_exc.ml @@ -0,0 +1,22 @@ +(* RUN: cp %s %T/ext_exc.ml + * RUN: %ocamlc -g -warn-error A -package llvm.bitreader -linkpkg %T/ext_exc.ml -o %t + * RUN: %t + * RUN: %ocamlopt -g -warn-error A -package llvm.bitreader -linkpkg %T/ext_exc.ml -o %t + * RUN: %t + * XFAIL: vg_leak + *) + +let context = Llvm.global_context () + +(* this used to crash, we must not use 'external' in .mli files, but 'val' if we + * want the let _ bindings executed, see http://caml.inria.fr/mantis/view.php?id=4166 *) +let _ = + try + ignore (Llvm_bitreader.get_module context (Llvm.MemoryBuffer.of_stdin ())) + with + Llvm_bitreader.Error _ -> ();; +let _ = + try + ignore (Llvm.MemoryBuffer.of_file "/path/to/nonexistent/file") + with + Llvm.IoError _ -> ();; diff --git a/test/Bindings/OCaml/ipo.ml b/test/Bindings/OCaml/ipo.ml new file mode 100644 index 0000000..fc728b9 --- /dev/null +++ b/test/Bindings/OCaml/ipo.ml @@ -0,0 +1,72 @@ +(* RUN: cp %s %T/ipo_opts.ml + * RUN: %ocamlc -g -warn-error A -package llvm.ipo -linkpkg %T/ipo_opts.ml -o %t + * RUN: %t %t.bc + * RUN: %ocamlopt -g -warn-error A -package llvm.ipo -linkpkg %T/ipo_opts.ml -o %t + * RUN: %t %t.bc + * XFAIL: vg_leak + *) + +(* Note: It takes several seconds for ocamlopt 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_ipo +open Llvm_target + +let context = global_context () +let void_type = Llvm.void_type context +let i8_type = Llvm.i8_type context + +(* Tiny unit test framework - really just to help find which line is busted *) +let print_checkpoints = false + +let suite name f = + if print_checkpoints then + prerr_endline (name ^ ":"); + f () + + +(*===-- Fixture -----------------------------------------------------------===*) + +let filename = Sys.argv.(1) +let m = create_module context filename + + +(*===-- Transforms --------------------------------------------------------===*) + +let test_transforms () = + let (++) x f = f x; x in + + let fty = function_type i8_type [| |] in + let fn = define_function "fn" fty m in + let fn2 = define_function "fn2" fty m in begin + ignore (build_ret (const_int i8_type 4) (builder_at_end context (entry_block fn))); + let b = builder_at_end context (entry_block fn2) in + ignore (build_ret (build_call fn [| |] "" b) b); + end; + + ignore (PassManager.create () + ++ add_argument_promotion + ++ add_constant_merge + ++ add_dead_arg_elimination + ++ add_function_attrs + ++ add_function_inlining + ++ add_always_inliner + ++ add_global_dce + ++ add_global_optimizer + ++ add_ipc_propagation + ++ add_prune_eh + ++ add_ipsccp + ++ add_internalize ~all_but_main:true + ++ add_strip_dead_prototypes + ++ add_strip_symbols + ++ PassManager.run_module m + ++ PassManager.dispose) + + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + suite "transforms" test_transforms; + dispose_module m diff --git a/test/Bindings/OCaml/irreader.ml b/test/Bindings/OCaml/irreader.ml new file mode 100644 index 0000000..e1771e7 --- /dev/null +++ b/test/Bindings/OCaml/irreader.ml @@ -0,0 +1,59 @@ +(* RUN: cp %s %T/irreader.ml + * RUN: %ocamlc -g -warn-error A -package llvm.irreader -linkpkg %T/irreader.ml -o %t + * RUN: %t + * RUN: %ocamlopt -g -warn-error A -package llvm.irreader -linkpkg %T/irreader.ml -o %t + * RUN: %t + * XFAIL: vg_leak + *) + +(* Note: It takes several seconds for ocamlopt 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_irreader + +let context = global_context () + +(* Tiny unit test framework - really just to help find which line is busted *) +let print_checkpoints = false + +let suite name f = + if print_checkpoints then + prerr_endline (name ^ ":"); + f () + +let _ = + Printexc.record_backtrace true + +let insist cond = + if not cond then failwith "insist" + + +(*===-- IR Reader ---------------------------------------------------------===*) + +let test_irreader () = + begin + let buf = MemoryBuffer.of_string "@foo = global i32 42" in + let m = parse_ir context buf in + match lookup_global "foo" m with + | Some foo -> + insist ((global_initializer foo) = (const_int (i32_type context) 42)) + | None -> + failwith "global" + end; + + begin + let buf = MemoryBuffer.of_string "@foo = global garble" in + try + ignore (parse_ir context buf); + failwith "parsed" + with Llvm_irreader.Error _ -> + () + end + + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + suite "irreader" test_irreader diff --git a/test/Bindings/OCaml/linker.ml b/test/Bindings/OCaml/linker.ml new file mode 100644 index 0000000..00064b0 --- /dev/null +++ b/test/Bindings/OCaml/linker.ml @@ -0,0 +1,63 @@ +(* RUN: cp %s %T/linker.ml + * RUN: %ocamlc -g -warn-error A -package llvm.linker -linkpkg %T/linker.ml -o %t + * RUN: %t + * RUN: %ocamlopt -g -warn-error A -package llvm.linker -linkpkg %T/linker.ml -o %t + * RUN: %t + * XFAIL: vg_leak + *) + +(* Note: It takes several seconds for ocamlopt 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_linker + +let context = global_context () +let void_type = Llvm.void_type context + +(* Tiny unit test framework - really just to help find which line is busted *) +let print_checkpoints = false + +let suite name f = + if print_checkpoints then + prerr_endline (name ^ ":"); + f () + + +(*===-- Linker -----------------------------------------------------------===*) + +let test_linker () = + let fty = function_type void_type [| |] in + + let make_module name = + let m = create_module context name in + let fn = define_function ("fn_" ^ name) fty m in + ignore (build_ret_void (builder_at_end context (entry_block fn))); + m + in + + let m1 = make_module "one" + and m2 = make_module "two" in + link_modules m1 m2 Mode.PreserveSource; + dispose_module m1; + dispose_module m2; + + let m1 = make_module "one" + and m2 = make_module "two" in + link_modules m1 m2 Mode.DestroySource; + dispose_module m1; + + let m1 = make_module "one" + and m2 = make_module "one" in + try + link_modules m1 m2 Mode.PreserveSource; + failwith "must raise" + with Error _ -> + dispose_module m1; + dispose_module m2 + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + suite "linker" test_linker diff --git a/test/Bindings/OCaml/lit.local.cfg b/test/Bindings/OCaml/lit.local.cfg new file mode 100644 index 0000000..bca5d39 --- /dev/null +++ b/test/Bindings/OCaml/lit.local.cfg @@ -0,0 +1,7 @@ +config.suffixes = ['.ml'] + +if not 'ocaml' in config.root.llvm_bindings: + config.unsupported = True + +if config.root.have_ocaml_ounit != '1': + config.unsupported = True diff --git a/test/Bindings/OCaml/passmgr_builder.ml b/test/Bindings/OCaml/passmgr_builder.ml new file mode 100644 index 0000000..5dd9d4e --- /dev/null +++ b/test/Bindings/OCaml/passmgr_builder.ml @@ -0,0 +1,64 @@ +(* RUN: cp %s %T/passmgr_builder.ml + * RUN: %ocamlc -g -warn-error A -package llvm.passmgr_builder -linkpkg %T/passmgr_builder.ml -o %t + * RUN: %t %t.bc + * RUN: %ocamlopt -g -warn-error A -package llvm.passmgr_builder -linkpkg %T/passmgr_builder.ml -o %t + * RUN: %t %t.bc + * XFAIL: vg_leak + *) + +(* Note: It takes several seconds for ocamlopt 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_passmgr_builder + +let context = global_context () +let void_type = Llvm.void_type context + +(* Tiny unit test framework - really just to help find which line is busted *) +let print_checkpoints = false + +let suite name f = + if print_checkpoints then + prerr_endline (name ^ ":"); + f () + + +(*===-- Fixture -----------------------------------------------------------===*) + +let filename = Sys.argv.(1) +let m = create_module context filename + + +(*===-- Pass Manager Builder ----------------------------------------------===*) + +let test_pmbuilder () = + let (++) x f = ignore (f x); x in + + let module_passmgr = PassManager.create () in + let func_passmgr = PassManager.create_function m in + let lto_passmgr = PassManager.create () in + + ignore (Llvm_passmgr_builder.create () + ++ set_opt_level 3 + ++ set_size_level 1 + ++ set_disable_unit_at_a_time false + ++ set_disable_unroll_loops false + ++ use_inliner_with_threshold 10 + ++ populate_function_pass_manager func_passmgr + ++ populate_module_pass_manager module_passmgr + ++ populate_lto_pass_manager lto_passmgr + ~internalize:false ~run_inliner:false); + Gc.compact (); + + PassManager.dispose module_passmgr; + PassManager.dispose func_passmgr; + PassManager.dispose lto_passmgr + + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + suite "pass manager builder" test_pmbuilder; + dispose_module m diff --git a/test/Bindings/OCaml/scalar_opts.ml b/test/Bindings/OCaml/scalar_opts.ml new file mode 100644 index 0000000..3017fb1 --- /dev/null +++ b/test/Bindings/OCaml/scalar_opts.ml @@ -0,0 +1,92 @@ +(* RUN: cp %s %T/scalar_opts.ml + * RUN: %ocamlc -g -warn-error A -package llvm.scalar_opts -linkpkg %T/scalar_opts.ml -o %t + * RUN: %t %t.bc + * RUN: %ocamlopt -g -warn-error A -package llvm.scalar_opts -linkpkg %T/scalar_opts.ml -o %t + * RUN: %t %t.bc + * XFAIL: vg_leak + *) + +(* Note: It takes several seconds for ocamlopt 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_scalar_opts +open Llvm_target + +let context = global_context () +let void_type = Llvm.void_type context + +(* Tiny unit test framework - really just to help find which line is busted *) +let print_checkpoints = false + +let suite name f = + if print_checkpoints then + prerr_endline (name ^ ":"); + f () + + +(*===-- Fixture -----------------------------------------------------------===*) + +let filename = Sys.argv.(1) +let m = create_module context filename + + +(*===-- Transforms --------------------------------------------------------===*) + +let test_transforms () = + let (++) x f = f x; x in + + let fty = function_type void_type [| |] in + let fn = define_function "fn" fty m in + ignore (build_ret_void (builder_at_end context (entry_block fn))); + + ignore (PassManager.create_function m + ++ add_aggressive_dce + ++ add_alignment_from_assumptions + ++ add_cfg_simplification + ++ add_dead_store_elimination + ++ add_scalarizer + ++ add_merged_load_store_motion + ++ add_gvn + ++ add_ind_var_simplification + ++ add_instruction_combination + ++ add_jump_threading + ++ add_licm + ++ add_loop_deletion + ++ add_loop_idiom + ++ add_loop_rotation + ++ add_loop_reroll + ++ add_loop_unroll + ++ add_loop_unswitch + ++ add_memcpy_opt + ++ add_partially_inline_lib_calls + ++ add_lower_switch + ++ add_memory_to_register_promotion + ++ add_reassociation + ++ add_sccp + ++ add_scalar_repl_aggregation + ++ add_scalar_repl_aggregation_ssa + ++ add_scalar_repl_aggregation_with_threshold 4 + ++ add_lib_call_simplification + ++ add_tail_call_elimination + ++ add_constant_propagation + ++ add_memory_to_register_demotion + ++ add_verifier + ++ add_correlated_value_propagation + ++ add_early_cse + ++ add_lower_expect_intrinsic + ++ add_type_based_alias_analysis + ++ add_scoped_no_alias_alias_analysis + ++ add_basic_alias_analysis + ++ PassManager.initialize + ++ PassManager.run_function fn + ++ PassManager.finalize + ++ PassManager.dispose) + + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + suite "transforms" test_transforms; + dispose_module m diff --git a/test/Bindings/OCaml/target.ml b/test/Bindings/OCaml/target.ml new file mode 100644 index 0000000..41faefa --- /dev/null +++ b/test/Bindings/OCaml/target.ml @@ -0,0 +1,116 @@ +(* RUN: cp %s %T/target.ml + * RUN: %ocamlc -g -warn-error A -package llvm.target -package llvm.all_backends -linkpkg %T/target.ml -o %t + * RUN: %ocamlopt -g -warn-error A -package llvm.target -package llvm.all_backends -linkpkg %T/target.ml -o %t + * RUN: %t %t.bc + * XFAIL: vg_leak + *) + +(* Note: It takes several seconds for ocamlopt 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_target + +let () = Llvm_all_backends.initialize () + +let context = global_context () +let i32_type = Llvm.i32_type context +let i64_type = Llvm.i64_type context + +(* Tiny unit test framework - really just to help find which line is busted *) +let print_checkpoints = false + +let _ = + Printexc.record_backtrace true + +let assert_equal a b = + if a <> b then failwith "assert_equal" + + +(*===-- Fixture -----------------------------------------------------------===*) + +let filename = Sys.argv.(1) +let m = create_module context filename + +let target = Target.by_triple (Target.default_triple ()) + +let machine = TargetMachine.create (Target.default_triple ()) target + +(*===-- Data Layout -------------------------------------------------------===*) + +let test_target_data () = + let module DL = DataLayout in + let layout = "e-p:32:32-f64:32:64-v64:32:64-v128:32:128-n32-S32" in + let dl = DL.of_string layout in + let sty = struct_type context [| i32_type; i64_type |] in + + assert_equal (DL.as_string dl) layout; + assert_equal (DL.byte_order dl) Endian.Little; + assert_equal (DL.pointer_size dl) 4; + assert_equal (DL.intptr_type context dl) i32_type; + assert_equal (DL.qualified_pointer_size 0 dl) 4; + assert_equal (DL.qualified_intptr_type context 0 dl) i32_type; + assert_equal (DL.size_in_bits sty dl) (Int64.of_int 96); + assert_equal (DL.store_size sty dl) (Int64.of_int 12); + assert_equal (DL.abi_size sty dl) (Int64.of_int 12); + assert_equal (DL.stack_align sty dl) 4; + assert_equal (DL.preferred_align sty dl) 8; + assert_equal (DL.preferred_align_of_global (declare_global sty "g" m) dl) 8; + assert_equal (DL.element_at_offset sty (Int64.of_int 1) dl) 0; + assert_equal (DL.offset_of_element sty 1 dl) (Int64.of_int 4); + + let pm = PassManager.create () in + ignore (DL.add_to_pass_manager pm dl) + + +(*===-- Target ------------------------------------------------------------===*) + +let test_target () = + let module T = Target in + ignore (T.succ target); + ignore (T.name target); + ignore (T.description target); + ignore (T.has_jit target); + ignore (T.has_target_machine target); + ignore (T.has_asm_backend target) + + +(*===-- Target Machine ----------------------------------------------------===*) + +let test_target_machine () = + let module TM = TargetMachine in + assert_equal (TM.target machine) target; + assert_equal (TM.triple machine) (Target.default_triple ()); + assert_equal (TM.cpu machine) ""; + assert_equal (TM.features machine) ""; + ignore (TM.data_layout machine); + TM.set_verbose_asm true machine; + let pm = PassManager.create () in + TM.add_analysis_passes pm machine + + +(*===-- Code Emission -----------------------------------------------------===*) + +let test_code_emission () = + TargetMachine.emit_to_file m CodeGenFileType.ObjectFile filename machine; + try + TargetMachine.emit_to_file m CodeGenFileType.ObjectFile + "/nonexistent/file" machine; + failwith "must raise" + with Llvm_target.Error _ -> + (); + + let buf = TargetMachine.emit_to_memory_buffer m CodeGenFileType.ObjectFile + machine in + Llvm.MemoryBuffer.dispose buf + + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + test_target_data (); + test_target (); + test_target_machine (); + test_code_emission (); + dispose_module m diff --git a/test/Bindings/OCaml/vectorize.ml b/test/Bindings/OCaml/vectorize.ml new file mode 100644 index 0000000..c5b03b5 --- /dev/null +++ b/test/Bindings/OCaml/vectorize.ml @@ -0,0 +1,56 @@ +(* RUN: cp %s %T/vectorize_opts.ml + * RUN: %ocamlc -g -warn-error A -package llvm.vectorize -linkpkg %T/vectorize_opts.ml -o %t + * RUN: %t %t.bc + * RUN: %ocamlopt -g -warn-error A -package llvm.vectorize -linkpkg %T/vectorize_opts.ml -o %t + * RUN: %t %t.bc + * XFAIL: vg_leak + *) + +(* Note: It takes several seconds for ocamlopt 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_vectorize +open Llvm_target + +let context = global_context () +let void_type = Llvm.void_type context + +(* Tiny unit test framework - really just to help find which line is busted *) +let print_checkpoints = false + +let suite name f = + if print_checkpoints then + prerr_endline (name ^ ":"); + f () + + +(*===-- Fixture -----------------------------------------------------------===*) + +let filename = Sys.argv.(1) +let m = create_module context filename + + +(*===-- Transforms --------------------------------------------------------===*) + +let test_transforms () = + let (++) x f = f x; x in + + let fty = function_type void_type [| |] in + let fn = define_function "fn" fty m in + ignore (build_ret_void (builder_at_end context (entry_block fn))); + + ignore (PassManager.create () + ++ add_bb_vectorize + ++ add_loop_vectorize + ++ add_slp_vectorize + ++ PassManager.run_module m + ++ PassManager.dispose) + + +(*===-- Driver ------------------------------------------------------------===*) + +let _ = + suite "transforms" test_transforms; + dispose_module m diff --git a/test/Bindings/Ocaml/analysis.ml b/test/Bindings/Ocaml/analysis.ml deleted file mode 100644 index c02645c..0000000 --- a/test/Bindings/Ocaml/analysis.ml +++ /dev/null @@ -1,54 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa %t.builddir/analysis.ml -o %t - * RUN: %t - * XFAIL: vg_leak - *) - -open Llvm -open Llvm_analysis - -(* Note that this takes a moment to link, so it's best to keep the number of - individual tests low. *) - -let context = global_context () - -let test x = if not x then exit 1 else () - -let bomb msg = - prerr_endline msg; - exit 2 - -let _ = - let fty = function_type (void_type context) [| |] in - let m = create_module context "valid_m" in - let fn = define_function "valid_fn" fty m in - let at_entry = builder_at_end context (entry_block fn) in - ignore (build_ret_void at_entry); - - - (* Test that valid constructs verify. *) - begin match verify_module m with - Some msg -> bomb "valid module failed verification!" - | None -> () - end; - - if not (verify_function fn) then bomb "valid function failed verification!"; - - - (* Test that invalid constructs do not verify. - A basic block can contain only one terminator instruction. *) - ignore (build_ret_void at_entry); - - begin match verify_module m with - Some msg -> () - | None -> bomb "invalid module passed verification!" - end; - - if verify_function fn then bomb "invalid function passed verification!"; - - - dispose_module m - - (* Don't bother to test assert_valid_{module,function}. *) diff --git a/test/Bindings/Ocaml/bitreader.ml b/test/Bindings/Ocaml/bitreader.ml deleted file mode 100644 index f1d202a..0000000 --- a/test/Bindings/Ocaml/bitreader.ml +++ /dev/null @@ -1,79 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_bitreader.cmxa llvm_bitwriter.cmxa %t.builddir/bitreader.ml -o %t - * RUN: %t %t.bc - * RUN: llvm-dis < %t.bc - * XFAIL: vg_leak - *) - -(* Note that this takes a moment to link, so it's best to keep the number of - individual tests low. *) - -let context = Llvm.global_context () - -let test x = if not x then exit 1 else () - -let _ = - let fn = Sys.argv.(1) in - let m = Llvm.create_module context "ocaml_test_module" in - - test (Llvm_bitwriter.write_bitcode_file m fn); - - Llvm.dispose_module m; - - (* parse_bitcode *) - begin - let mb = Llvm.MemoryBuffer.of_file fn in - begin try - let m = Llvm_bitreader.parse_bitcode context mb in - Llvm.dispose_module m - with x -> - Llvm.MemoryBuffer.dispose mb; - raise x - end - end; - - (* MemoryBuffer.of_file *) - test begin try - let mb = Llvm.MemoryBuffer.of_file (fn ^ ".bogus") in - Llvm.MemoryBuffer.dispose mb; - false - with Llvm.IoError _ -> - true - end; - - (* get_module *) - begin - let mb = Llvm.MemoryBuffer.of_file fn in - let m = begin try - Llvm_bitreader.get_module context mb - with x -> - Llvm.MemoryBuffer.dispose mb; - raise x - end in - Llvm.dispose_module m - end; - - (* corrupt the bitcode *) - let fn = fn ^ ".txt" in - begin let oc = open_out fn in - output_string oc "not a bitcode file\n"; - close_out oc - end; - - (* test get_module exceptions *) - test begin - try - let mb = Llvm.MemoryBuffer.of_file fn in - let m = begin try - Llvm_bitreader.get_module context mb - with x -> - Llvm.MemoryBuffer.dispose mb; - raise x - end in - Llvm.dispose_module m; - false - with Llvm_bitreader.Error _ -> - true - end diff --git a/test/Bindings/Ocaml/bitwriter.ml b/test/Bindings/Ocaml/bitwriter.ml deleted file mode 100644 index ae456cf..0000000 --- a/test/Bindings/Ocaml/bitwriter.ml +++ /dev/null @@ -1,48 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A unix.cmxa llvm.cmxa llvm_bitwriter.cmxa %t.builddir/bitwriter.ml -o %t - * RUN: %t %t.bc - * RUN: llvm-dis < %t.bc - * XFAIL: vg_leak - *) - -(* Note that this takes a moment to link, so it's best to keep the number of - individual tests low. *) - -let context = Llvm.global_context () - -let test x = if not x then exit 1 else () - -let read_file name = - let ic = open_in_bin name in - let len = in_channel_length ic in - let buf = String.create len in - - test ((input ic buf 0 len) = len); - - close_in ic; - - buf - -let temp_bitcode ?unbuffered m = - let temp_name, temp_oc = Filename.open_temp_file ~mode:[Open_binary] "" "" in - - test (Llvm_bitwriter.output_bitcode ?unbuffered temp_oc m); - flush temp_oc; - - let temp_buf = read_file temp_name in - - close_out temp_oc; - - temp_buf - -let _ = - let m = Llvm.create_module context "ocaml_test_module" in - - test (Llvm_bitwriter.write_bitcode_file m Sys.argv.(1)); - let file_buf = read_file Sys.argv.(1) in - - test (file_buf = temp_bitcode m); - test (file_buf = temp_bitcode ~unbuffered:false m); - test (file_buf = temp_bitcode ~unbuffered:true m) diff --git a/test/Bindings/Ocaml/executionengine.ml b/test/Bindings/Ocaml/executionengine.ml deleted file mode 100644 index 8e24949..0000000 --- a/test/Bindings/Ocaml/executionengine.ml +++ /dev/null @@ -1,118 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_target.cmxa llvm_executionengine.cmxa %t.builddir/executionengine.ml -o %t - * RUN: %t - * XFAIL: vg_leak - *) - -open Llvm -open Llvm_executionengine -open Llvm_target - -(* Note that this takes a moment to link, so it's best to keep the number of - individual tests low. *) - -let context = global_context () -let i8_type = Llvm.i8_type context -let i32_type = Llvm.i32_type context -let i64_type = Llvm.i64_type context -let double_type = Llvm.double_type context - -let bomb msg = - prerr_endline msg; - exit 2 - -let define_main_fn m retval = - let fn = - let str_arr_type = pointer_type (pointer_type i8_type) in - define_function "main" (function_type i32_type [| i32_type; - str_arr_type; - str_arr_type |]) m in - let b = builder_at_end (global_context ()) (entry_block fn) in - ignore (build_ret (const_int i32_type retval) b); - fn - -let define_plus m = - let fn = define_function "plus" (function_type i32_type [| i32_type; - i32_type |]) m in - let b = builder_at_end (global_context ()) (entry_block fn) in - let add = build_add (param fn 0) (param fn 1) "sum" b in - ignore (build_ret add b) - -let test_genericvalue () = - let tu = (1, 2) in - let ptrgv = GenericValue.of_pointer tu in - assert (tu = GenericValue.as_pointer ptrgv); - - let fpgv = GenericValue.of_float double_type 2. in - assert (2. = GenericValue.as_float double_type fpgv); - - let intgv = GenericValue.of_int i32_type 3 in - assert (3 = GenericValue.as_int intgv); - - let i32gv = GenericValue.of_int32 i32_type (Int32.of_int 4) in - assert ((Int32.of_int 4) = GenericValue.as_int32 i32gv); - - let nigv = GenericValue.of_nativeint i32_type (Nativeint.of_int 5) in - assert ((Nativeint.of_int 5) = GenericValue.as_nativeint nigv); - - let i64gv = GenericValue.of_int64 i64_type (Int64.of_int 6) in - assert ((Int64.of_int 6) = GenericValue.as_int64 i64gv) - -let test_executionengine () = - (* create *) - let m = create_module (global_context ()) "test_module" in - let main = define_main_fn m 42 in - - let m2 = create_module (global_context ()) "test_module2" in - define_plus m2; - - let ee = ExecutionEngine.create m in - ExecutionEngine.add_module m2 ee; - - (* run_static_ctors *) - ExecutionEngine.run_static_ctors ee; - - (* run_function_as_main *) - let res = ExecutionEngine.run_function_as_main main [|"test"|] [||] ee in - if 42 != res then bomb "main did not return 42"; - - (* free_machine_code *) - ExecutionEngine.free_machine_code main ee; - - (* find_function *) - match ExecutionEngine.find_function "dne" ee with - | Some _ -> raise (Failure "find_function 'dne' failed") - | None -> - - match ExecutionEngine.find_function "plus" ee with - | None -> raise (Failure "find_function 'plus' failed") - | Some plus -> - - (* run_function *) - let res = ExecutionEngine.run_function plus - [| GenericValue.of_int i32_type 2; - GenericValue.of_int i32_type 2 |] - ee in - if 4 != GenericValue.as_int res then bomb "plus did not work"; - - (* remove_module *) - Llvm.dispose_module (ExecutionEngine.remove_module m2 ee); - - (* run_static_dtors *) - ExecutionEngine.run_static_dtors ee; - - (* Show that the data layout binding links and runs.*) - let dl = ExecutionEngine.data_layout ee in - - (* Demonstrate that a garbage pointer wasn't returned. *) - let ty = DataLayout.intptr_type context dl in - if ty != i32_type && ty != i64_type then bomb "target_data did not work"; - - (* dispose *) - ExecutionEngine.dispose ee - -let _ = - test_genericvalue (); - test_executionengine () diff --git a/test/Bindings/Ocaml/ext_exc.ml b/test/Bindings/Ocaml/ext_exc.ml deleted file mode 100644 index 9afc3c3..0000000 --- a/test/Bindings/Ocaml/ext_exc.ml +++ /dev/null @@ -1,20 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_bitreader.cmxa llvm_executionengine.cmxa %t.builddir/ext_exc.ml -o %t - * RUN: %t ();; -let _ = - try - ignore (Llvm.MemoryBuffer.of_file "/path/to/nonexistent/file") - with - Llvm.IoError _ -> ();; diff --git a/test/Bindings/Ocaml/ipo_opts.ml b/test/Bindings/Ocaml/ipo_opts.ml deleted file mode 100644 index e0bcbe5..0000000 --- a/test/Bindings/Ocaml/ipo_opts.ml +++ /dev/null @@ -1,72 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_ipo.cmxa llvm_target.cmxa %t.builddir/ipo_opts.ml -o %t - * RUN: %t %t.bc - * XFAIL: vg_leak - *) - -(* Note: It takes several seconds for ocamlopt 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_ipo -open Llvm_target - -let context = global_context () -let void_type = Llvm.void_type context -let i8_type = Llvm.i8_type context - -(* Tiny unit test framework - really just to help find which line is busted *) -let print_checkpoints = false - -let suite name f = - if print_checkpoints then - prerr_endline (name ^ ":"); - f () - - -(*===-- Fixture -----------------------------------------------------------===*) - -let filename = Sys.argv.(1) -let m = create_module context filename - - -(*===-- Transforms --------------------------------------------------------===*) - -let test_transforms () = - let (++) x f = ignore (f x); x in - - let fty = function_type i8_type [| |] in - let fn = define_function "fn" fty m in - let fn2 = define_function "fn2" fty m in begin - ignore (build_ret (const_int i8_type 4) (builder_at_end context (entry_block fn))); - let b = builder_at_end context (entry_block fn2) in - ignore (build_ret (build_call fn [| |] "" b) b); - end; - - ignore (PassManager.create () - ++ add_argument_promotion - ++ add_constant_merge - ++ add_dead_arg_elimination - ++ add_function_attrs - ++ add_function_inlining - ++ add_always_inliner - ++ add_global_dce - ++ add_global_optimizer - ++ add_ipc_propagation - ++ add_prune_eh - ++ add_ipsccp - ++ add_internalize - ++ add_strip_dead_prototypes - ++ add_strip_symbols - ++ PassManager.run_module m - ++ PassManager.dispose) - - -(*===-- Driver ------------------------------------------------------------===*) - -let _ = - suite "transforms" test_transforms; - dispose_module m diff --git a/test/Bindings/Ocaml/irreader.ml b/test/Bindings/Ocaml/irreader.ml deleted file mode 100644 index 3511c2b..0000000 --- a/test/Bindings/Ocaml/irreader.ml +++ /dev/null @@ -1,59 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -g -warn-error A llvm.cmxa llvm_irreader.cmxa %t.builddir/irreader.ml -o %t - * RUN: %t - * XFAIL: vg_leak - *) - -(* Note: It takes several seconds for ocamlopt 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_irreader - -let context = global_context () - -(* Tiny unit test framework - really just to help find which line is busted *) -let print_checkpoints = false - -let suite name f = - if print_checkpoints then - prerr_endline (name ^ ":"); - f () - -let _ = - Printexc.record_backtrace true - -let insist cond = - if not cond then failwith "insist" - - -(*===-- IR Reader ---------------------------------------------------------===*) - -let test_irreader () = - begin - let buf = MemoryBuffer.of_string "@foo = global i32 42" in - let m = parse_ir context buf in - match lookup_global "foo" m with - | Some foo -> - insist ((global_initializer foo) = (const_int (i32_type context) 42)) - | None -> - failwith "global" - end; - - begin - let buf = MemoryBuffer.of_string "@foo = global garble" in - try - ignore (parse_ir context buf); - failwith "parsed" - with Llvm_irreader.Error _ -> - () - end - - -(*===-- Driver ------------------------------------------------------------===*) - -let _ = - suite "irreader" test_irreader diff --git a/test/Bindings/Ocaml/linker.ml b/test/Bindings/Ocaml/linker.ml deleted file mode 100644 index 9359ae9..0000000 --- a/test/Bindings/Ocaml/linker.ml +++ /dev/null @@ -1,63 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_linker.cmxa %t.builddir/linker.ml -o %t - * RUN: %t - * XFAIL: vg_leak - *) - -(* Note: It takes several seconds for ocamlopt 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_linker - -let context = global_context () -let void_type = Llvm.void_type context - -(* Tiny unit test framework - really just to help find which line is busted *) -let print_checkpoints = false - -let suite name f = - if print_checkpoints then - prerr_endline (name ^ ":"); - f () - - -(*===-- Linker -----------------------------------------------------------===*) - -let test_linker () = - let fty = function_type void_type [| |] in - - let make_module name = - let m = create_module context name in - let fn = define_function ("fn_" ^ name) fty m in - ignore (build_ret_void (builder_at_end context (entry_block fn))); - m - in - - let m1 = make_module "one" - and m2 = make_module "two" in - link_modules m1 m2 Mode.PreserveSource; - dispose_module m1; - dispose_module m2; - - let m1 = make_module "one" - and m2 = make_module "two" in - link_modules m1 m2 Mode.DestroySource; - dispose_module m1; - - let m1 = make_module "one" - and m2 = make_module "one" in - try - link_modules m1 m2 Mode.PreserveSource; - failwith "must raise" - with Error _ -> - dispose_module m1; - dispose_module m2 - -(*===-- Driver ------------------------------------------------------------===*) - -let _ = - suite "linker" test_linker diff --git a/test/Bindings/Ocaml/lit.local.cfg b/test/Bindings/Ocaml/lit.local.cfg deleted file mode 100644 index c38d89a..0000000 --- a/test/Bindings/Ocaml/lit.local.cfg +++ /dev/null @@ -1,5 +0,0 @@ -config.suffixes = ['.ml'] - -bindings = set([s.strip() for s in config.root.llvm_bindings.split(',')]) -if not 'ocaml' in bindings: - config.unsupported = True diff --git a/test/Bindings/Ocaml/passmgr_builder.ml b/test/Bindings/Ocaml/passmgr_builder.ml deleted file mode 100644 index 1a3102f..0000000 --- a/test/Bindings/Ocaml/passmgr_builder.ml +++ /dev/null @@ -1,64 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_passmgr_builder.cmxa %t.builddir/passmgr_builder.ml -o %t - * RUN: %t %t.bc - * XFAIL: vg_leak - *) - -(* Note: It takes several seconds for ocamlopt 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_passmgr_builder - -let context = global_context () -let void_type = Llvm.void_type context - -(* Tiny unit test framework - really just to help find which line is busted *) -let print_checkpoints = false - -let suite name f = - if print_checkpoints then - prerr_endline (name ^ ":"); - f () - - -(*===-- Fixture -----------------------------------------------------------===*) - -let filename = Sys.argv.(1) -let m = create_module context filename - - -(*===-- Pass Manager Builder ----------------------------------------------===*) - -let test_pmbuilder () = - let (++) x f = ignore (f x); x in - - let module_passmgr = PassManager.create () in - let func_passmgr = PassManager.create_function m in - let lto_passmgr = PassManager.create () in - - ignore (Llvm_passmgr_builder.create () - ++ set_opt_level 3 - ++ set_size_level 1 - ++ set_disable_unit_at_a_time false - ++ set_disable_unroll_loops false - ++ use_inliner_with_threshold 10 - ++ populate_function_pass_manager func_passmgr - ++ populate_module_pass_manager module_passmgr - ++ populate_lto_pass_manager lto_passmgr - ~internalize:false ~run_inliner:false); - Gc.compact (); - - PassManager.dispose module_passmgr; - PassManager.dispose func_passmgr; - PassManager.dispose lto_passmgr - - -(*===-- Driver ------------------------------------------------------------===*) - -let _ = - suite "pass manager builder" test_pmbuilder; - dispose_module m diff --git a/test/Bindings/Ocaml/scalar_opts.ml b/test/Bindings/Ocaml/scalar_opts.ml deleted file mode 100644 index 39913e4..0000000 --- a/test/Bindings/Ocaml/scalar_opts.ml +++ /dev/null @@ -1,87 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_scalar_opts.cmxa llvm_target.cmxa %t.builddir/scalar_opts.ml -o %t - * RUN: %t %t.bc - * XFAIL: vg_leak - *) - -(* Note: It takes several seconds for ocamlopt 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_scalar_opts -open Llvm_target - -let context = global_context () -let void_type = Llvm.void_type context - -(* Tiny unit test framework - really just to help find which line is busted *) -let print_checkpoints = false - -let suite name f = - if print_checkpoints then - prerr_endline (name ^ ":"); - f () - - -(*===-- Fixture -----------------------------------------------------------===*) - -let filename = Sys.argv.(1) -let m = create_module context filename - - -(*===-- Transforms --------------------------------------------------------===*) - -let test_transforms () = - let (++) x f = ignore (f x); x in - - let fty = function_type void_type [| |] in - let fn = define_function "fn" fty m in - ignore (build_ret_void (builder_at_end context (entry_block fn))); - - ignore (PassManager.create_function m - ++ add_verifier - ++ add_constant_propagation - ++ add_sccp - ++ add_dead_store_elimination - ++ add_aggressive_dce - ++ add_scalar_repl_aggregation - ++ add_scalar_repl_aggregation_ssa - ++ add_scalar_repl_aggregation_with_threshold 4 - ++ add_ind_var_simplification - ++ add_instruction_combination - ++ add_licm - ++ add_loop_unswitch - ++ add_loop_unroll - ++ add_loop_rotation - ++ add_memory_to_register_promotion - ++ add_memory_to_register_demotion - ++ add_reassociation - ++ add_jump_threading - ++ add_cfg_simplification - ++ add_tail_call_elimination - ++ add_gvn - ++ add_memcpy_opt - ++ add_loop_deletion - ++ add_loop_idiom - ++ add_lib_call_simplification - ++ add_correlated_value_propagation - ++ add_early_cse - ++ add_lower_expect_intrinsic - ++ add_type_based_alias_analysis - ++ add_basic_alias_analysis - ++ add_partially_inline_lib_calls - ++ add_verifier - ++ PassManager.initialize - ++ PassManager.run_function fn - ++ PassManager.finalize - ++ PassManager.dispose) - - -(*===-- Driver ------------------------------------------------------------===*) - -let _ = - suite "transforms" test_transforms; - dispose_module m diff --git a/test/Bindings/Ocaml/target.ml b/test/Bindings/Ocaml/target.ml deleted file mode 100644 index 0a2283a..0000000 --- a/test/Bindings/Ocaml/target.ml +++ /dev/null @@ -1,116 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -g -warn-error A llvm.cmxa llvm_target.cmxa llvm_executionengine.cmxa %t.builddir/target.ml -o %t - * RUN: %t %t.bc - * REQUIRES: native, object-emission - * XFAIL: vg_leak - *) - -(* Note: It takes several seconds for ocamlopt 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_target - -let _ = Llvm_executionengine.initialize_native_target () - -let context = global_context () -let i32_type = Llvm.i32_type context -let i64_type = Llvm.i64_type context - -(* Tiny unit test framework - really just to help find which line is busted *) -let print_checkpoints = false - -let _ = - Printexc.record_backtrace true - -let assert_equal a b = - if a <> b then failwith "assert_equal" - - -(*===-- Fixture -----------------------------------------------------------===*) - -let filename = Sys.argv.(1) -let m = create_module context filename - -let target = Target.by_triple (Target.default_triple ()) - -let machine = TargetMachine.create (Target.default_triple ()) target - -(*===-- Data Layout -------------------------------------------------------===*) - -let test_target_data () = - let module DL = DataLayout in - let layout = "e-p:32:32-f64:32:64-v64:32:64-v128:32:128-n32-S32" in - let dl = DL.of_string layout in - let sty = struct_type context [| i32_type; i64_type |] in - - assert_equal (DL.as_string dl) layout; - assert_equal (DL.byte_order dl) Endian.Little; - assert_equal (DL.pointer_size dl) 4; - assert_equal (DL.intptr_type context dl) i32_type; - assert_equal (DL.qualified_pointer_size 0 dl) 4; - assert_equal (DL.qualified_intptr_type context 0 dl) i32_type; - assert_equal (DL.size_in_bits sty dl) (Int64.of_int 96); - assert_equal (DL.store_size sty dl) (Int64.of_int 12); - assert_equal (DL.abi_size sty dl) (Int64.of_int 12); - assert_equal (DL.stack_align sty dl) 4; - assert_equal (DL.preferred_align sty dl) 8; - assert_equal (DL.preferred_align_of_global (declare_global sty "g" m) dl) 8; - assert_equal (DL.element_at_offset sty (Int64.of_int 1) dl) 0; - assert_equal (DL.offset_of_element sty 1 dl) (Int64.of_int 4); - - let pm = PassManager.create () in - ignore (DL.add_to_pass_manager pm dl) - - -(*===-- Target ------------------------------------------------------------===*) - -let test_target () = - let module T = Target in - ignore (T.succ target); - ignore (T.name target); - ignore (T.description target); - ignore (T.has_jit target); - ignore (T.has_target_machine target); - ignore (T.has_asm_backend target) - - -(*===-- Target Machine ----------------------------------------------------===*) - -let test_target_machine () = - let module TM = TargetMachine in - assert_equal (TM.target machine) target; - assert_equal (TM.triple machine) (Target.default_triple ()); - assert_equal (TM.cpu machine) ""; - assert_equal (TM.features machine) ""; - ignore (TM.data_layout machine); - TM.set_verbose_asm true machine - - -(*===-- Code Emission -----------------------------------------------------===*) - -let test_code_emission () = - TargetMachine.emit_to_file m CodeGenFileType.ObjectFile filename machine; - try - TargetMachine.emit_to_file m CodeGenFileType.ObjectFile - "/nonexistent/file" machine; - failwith "must raise" - with Llvm_target.Error _ -> - (); - - let buf = TargetMachine.emit_to_memory_buffer m CodeGenFileType.ObjectFile - machine in - Llvm.MemoryBuffer.dispose buf - - -(*===-- Driver ------------------------------------------------------------===*) - -let _ = - test_target_data (); - test_target (); - test_target_machine (); - (* test_code_emission (); *) (* broken without AsmParser support *) - dispose_module m diff --git a/test/Bindings/Ocaml/vectorize_opts.ml b/test/Bindings/Ocaml/vectorize_opts.ml deleted file mode 100644 index 5ef985d..0000000 --- a/test/Bindings/Ocaml/vectorize_opts.ml +++ /dev/null @@ -1,56 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_vectorize.cmxa llvm_target.cmxa %t.builddir/vectorize_opts.ml -o %t - * RUN: %t %t.bc - * XFAIL: vg_leak - *) - -(* Note: It takes several seconds for ocamlopt 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_vectorize -open Llvm_target - -let context = global_context () -let void_type = Llvm.void_type context - -(* Tiny unit test framework - really just to help find which line is busted *) -let print_checkpoints = false - -let suite name f = - if print_checkpoints then - prerr_endline (name ^ ":"); - f () - - -(*===-- Fixture -----------------------------------------------------------===*) - -let filename = Sys.argv.(1) -let m = create_module context filename - - -(*===-- Transforms --------------------------------------------------------===*) - -let test_transforms () = - let (++) x f = ignore (f x); x in - - let fty = function_type void_type [| |] in - let fn = define_function "fn" fty m in - ignore (build_ret_void (builder_at_end context (entry_block fn))); - - ignore (PassManager.create () - ++ add_bb_vectorize - ++ add_loop_vectorize - ++ add_slp_vectorize - ++ PassManager.run_module m - ++ PassManager.dispose) - - -(*===-- Driver ------------------------------------------------------------===*) - -let _ = - suite "transforms" test_transforms; - dispose_module m diff --git a/test/Bindings/Ocaml/vmcore.ml b/test/Bindings/Ocaml/vmcore.ml deleted file mode 100644 index f014116..0000000 --- a/test/Bindings/Ocaml/vmcore.ml +++ /dev/null @@ -1,1421 +0,0 @@ -(* RUN: rm -rf %t.builddir - * RUN: mkdir -p %t.builddir - * RUN: cp %s %t.builddir - * RUN: %ocamlopt -warn-error A llvm.cmxa llvm_analysis.cmxa llvm_bitwriter.cmxa %t.builddir/vmcore.ml -o %t - * RUN: %t %t.bc - * RUN: llvm-dis < %t.bc > %t.ll - * RUN: FileCheck %s < %t.ll - * Do a second pass for things that shouldn't be anywhere. - * RUN: FileCheck -check-prefix=CHECK-NOWHERE %s < %t.ll - * XFAIL: vg_leak - *) - -(* Note: It takes several seconds for ocamlopt 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 - really just to help find which line is busted *) -let exit_status = ref 0 -let suite_name = ref "" -let group_name = ref "" -let case_num = ref 0 -let print_checkpoints = false -let context = global_context () -let i1_type = Llvm.i1_type context -let i8_type = Llvm.i8_type context -let i16_type = Llvm.i16_type context -let i32_type = Llvm.i32_type context -let i64_type = Llvm.i64_type context -let void_type = Llvm.void_type context -let float_type = Llvm.float_type context -let double_type = Llvm.double_type context -let fp128_type = Llvm.fp128_type context - -let group name = - group_name := !suite_name ^ "/" ^ name; - case_num := 0; - if print_checkpoints then - prerr_endline (" " ^ name ^ "...") - -let insist cond = - incr case_num; - if not cond then - exit_status := 10; - match print_checkpoints, cond with - | false, true -> () - | false, false -> - prerr_endline ("FAILED: " ^ !suite_name ^ "/" ^ !group_name ^ " #" ^ (string_of_int !case_num)) - | true, true -> - prerr_endline (" " ^ (string_of_int !case_num)) - | true, false -> - prerr_endline (" " ^ (string_of_int !case_num) ^ " FAIL") - -let suite name f = - suite_name := name; - if print_checkpoints then - prerr_endline (name ^ ":"); - f () - - -(*===-- Fixture -----------------------------------------------------------===*) - -let filename = Sys.argv.(1) -let m = create_module context filename - - -(*===-- Conversion --------------------------------------------------------===*) - -let test_conversion () = - insist ("i32" = (string_of_lltype i32_type)); - let c = const_int i32_type 42 in - insist ("i32 42" = (string_of_llvalue c)) - - -(*===-- Target ------------------------------------------------------------===*) - -let test_target () = - begin group "triple"; - let trip = "i686-apple-darwin8" in - set_target_triple trip m; - insist (trip = target_triple m) - end; - - begin group "layout"; - let layout = "e" in - set_data_layout layout m; - insist (layout = data_layout m) - end - (* CHECK: target datalayout = "e" - * CHECK: target triple = "i686-apple-darwin8" - *) - - -(*===-- Constants ---------------------------------------------------------===*) - -let test_constants () = - (* CHECK: const_int{{.*}}i32{{.*}}-1 - *) - group "int"; - let c = const_int i32_type (-1) in - ignore (define_global "const_int" c m); - insist (i32_type = type_of c); - insist (is_constant c); - - (* CHECK: const_sext_int{{.*}}i64{{.*}}-1 - *) - group "sext int"; - let c = const_int i64_type (-1) in - ignore (define_global "const_sext_int" c m); - insist (i64_type = type_of c); - - (* CHECK: const_zext_int64{{.*}}i64{{.*}}4294967295 - *) - group "zext int64"; - let c = const_of_int64 i64_type (Int64.of_string "4294967295") false in - ignore (define_global "const_zext_int64" c m); - insist (i64_type = type_of c); - - (* CHECK: const_int_string{{.*}}i32{{.*}}-1 - *) - group "int string"; - let c = const_int_of_string i32_type "-1" 10 in - ignore (define_global "const_int_string" c m); - insist (i32_type = type_of c); - - (* CHECK: @const_string = global {{.*}}c"cruel\00world" - *) - group "string"; - let c = const_string context "cruel\000world" in - ignore (define_global "const_string" c m); - insist ((array_type i8_type 11) = type_of c); - - (* CHECK: const_stringz{{.*}}"hi\00again\00" - *) - group "stringz"; - let c = const_stringz context "hi\000again" in - ignore (define_global "const_stringz" c m); - insist ((array_type i8_type 9) = type_of c); - - (* CHECK: const_single{{.*}}2.75 - * CHECK: const_double{{.*}}3.1459 - * CHECK: const_double_string{{.*}}1.25 - *) - begin group "real"; - let cs = const_float float_type 2.75 in - ignore (define_global "const_single" cs m); - insist (float_type = type_of cs); - - let cd = const_float double_type 3.1459 in - ignore (define_global "const_double" cd m); - insist (double_type = type_of cd); - - let cd = const_float_of_string double_type "1.25" in - ignore (define_global "const_double_string" cd m); - insist (double_type = type_of cd) - end; - - let one = const_int i16_type 1 in - let two = const_int i16_type 2 in - let three = const_int i32_type 3 in - let four = const_int i32_type 4 in - - (* CHECK: const_array{{.*}}[i32 3, i32 4] - *) - group "array"; - let c = const_array i32_type [| three; four |] in - ignore (define_global "const_array" c m); - insist ((array_type i32_type 2) = (type_of c)); - - (* CHECK: const_vector{{.*}} - *) - group "vector"; - let c = const_vector [| one; two; one; two; - one; two; one; two |] in - ignore (define_global "const_vector" c m); - insist ((vector_type i16_type 8) = (type_of c)); - - (* CHECK: const_structure{{.*.}}i16 1, i16 2, i32 3, i32 4 - *) - group "structure"; - let c = const_struct context [| one; two; three; four |] in - ignore (define_global "const_structure" c m); - insist ((struct_type context [| i16_type; i16_type; i32_type; i32_type |]) - = (type_of c)); - - (* CHECK: const_null{{.*}}zeroinit - *) - group "null"; - let c = const_null (packed_struct_type context [| i1_type; i8_type; i64_type; - double_type |]) in - ignore (define_global "const_null" c m); - - (* CHECK: const_all_ones{{.*}}-1 - *) - group "all ones"; - let c = const_all_ones i64_type in - ignore (define_global "const_all_ones" c m); - - group "pointer null"; begin - (* CHECK: const_pointer_null = global i64* null - *) - let c = const_pointer_null (pointer_type i64_type) in - ignore (define_global "const_pointer_null" c m); - end; - - (* CHECK: const_undef{{.*}}undef - *) - group "undef"; - let c = undef i1_type in - ignore (define_global "const_undef" c m); - insist (i1_type = type_of c); - insist (is_undef c); - - group "constant arithmetic"; - (* CHECK: @const_neg = global i64 sub - * CHECK: @const_nsw_neg = global i64 sub nsw - * CHECK: @const_nuw_neg = global i64 sub nuw - * CHECK: @const_fneg = global double fsub - * CHECK: @const_not = global i64 xor - * CHECK: @const_add = global i64 add - * CHECK: @const_nsw_add = global i64 add nsw - * CHECK: @const_nuw_add = global i64 add nuw - * CHECK: @const_fadd = global double fadd - * CHECK: @const_sub = global i64 sub - * CHECK: @const_nsw_sub = global i64 sub nsw - * CHECK: @const_nuw_sub = global i64 sub nuw - * CHECK: @const_fsub = global double fsub - * CHECK: @const_mul = global i64 mul - * CHECK: @const_nsw_mul = global i64 mul nsw - * CHECK: @const_nuw_mul = global i64 mul nuw - * CHECK: @const_fmul = global double fmul - * CHECK: @const_udiv = global i64 udiv - * CHECK: @const_sdiv = global i64 sdiv - * CHECK: @const_exact_sdiv = global i64 sdiv exact - * CHECK: @const_fdiv = global double fdiv - * CHECK: @const_urem = global i64 urem - * CHECK: @const_srem = global i64 srem - * CHECK: @const_frem = global double frem - * CHECK: @const_and = global i64 and - * CHECK: @const_or = global i64 or - * CHECK: @const_xor = global i64 xor - * CHECK: @const_icmp = global i1 icmp sle - * CHECK: @const_fcmp = global i1 fcmp ole - *) - let void_ptr = pointer_type i8_type in - let five = const_int i64_type 5 in - let ffive = const_uitofp five double_type in - let foldbomb_gv = define_global "FoldBomb" (const_null i8_type) m in - let foldbomb = const_ptrtoint foldbomb_gv i64_type in - let ffoldbomb = const_uitofp foldbomb double_type in - ignore (define_global "const_neg" (const_neg foldbomb) m); - ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m); - ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m); - ignore (define_global "const_fneg" (const_fneg ffoldbomb) m); - ignore (define_global "const_not" (const_not foldbomb) m); - ignore (define_global "const_add" (const_add foldbomb five) m); - ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m); - ignore (define_global "const_nuw_add" (const_nuw_add foldbomb five) m); - ignore (define_global "const_fadd" (const_fadd ffoldbomb ffive) m); - ignore (define_global "const_sub" (const_sub foldbomb five) m); - ignore (define_global "const_nsw_sub" (const_nsw_sub foldbomb five) m); - ignore (define_global "const_nuw_sub" (const_nuw_sub foldbomb five) m); - ignore (define_global "const_fsub" (const_fsub ffoldbomb ffive) m); - ignore (define_global "const_mul" (const_mul foldbomb five) m); - ignore (define_global "const_nsw_mul" (const_nsw_mul foldbomb five) m); - ignore (define_global "const_nuw_mul" (const_nuw_mul foldbomb five) m); - ignore (define_global "const_fmul" (const_fmul ffoldbomb ffive) m); - ignore (define_global "const_udiv" (const_udiv foldbomb five) m); - ignore (define_global "const_sdiv" (const_sdiv foldbomb five) m); - ignore (define_global "const_exact_sdiv" (const_exact_sdiv foldbomb five) m); - ignore (define_global "const_fdiv" (const_fdiv ffoldbomb ffive) m); - ignore (define_global "const_urem" (const_urem foldbomb five) m); - ignore (define_global "const_srem" (const_srem foldbomb five) m); - ignore (define_global "const_frem" (const_frem ffoldbomb ffive) m); - ignore (define_global "const_and" (const_and foldbomb five) m); - ignore (define_global "const_or" (const_or foldbomb five) m); - ignore (define_global "const_xor" (const_xor foldbomb five) m); - ignore (define_global "const_icmp" (const_icmp Icmp.Sle foldbomb five) m); - ignore (define_global "const_fcmp" (const_fcmp Fcmp.Ole ffoldbomb ffive) m); - - group "constant casts"; - (* CHECK: const_trunc{{.*}}trunc - * CHECK: const_sext{{.*}}sext - * CHECK: const_zext{{.*}}zext - * CHECK: const_fptrunc{{.*}}fptrunc - * CHECK: const_fpext{{.*}}fpext - * CHECK: const_uitofp{{.*}}uitofp - * CHECK: const_sitofp{{.*}}sitofp - * CHECK: const_fptoui{{.*}}fptoui - * CHECK: const_fptosi{{.*}}fptosi - * CHECK: const_ptrtoint{{.*}}ptrtoint - * CHECK: const_inttoptr{{.*}}inttoptr - * CHECK: const_bitcast{{.*}}bitcast - * CHECK: const_intcast{{.*}}zext - *) - let i128_type = integer_type context 128 in - ignore (define_global "const_trunc" (const_trunc (const_add foldbomb five) - i8_type) m); - ignore (define_global "const_sext" (const_sext foldbomb i128_type) m); - ignore (define_global "const_zext" (const_zext foldbomb i128_type) m); - ignore (define_global "const_fptrunc" (const_fptrunc ffoldbomb float_type) m); - ignore (define_global "const_fpext" (const_fpext ffoldbomb fp128_type) m); - ignore (define_global "const_uitofp" (const_uitofp foldbomb double_type) m); - ignore (define_global "const_sitofp" (const_sitofp foldbomb double_type) m); - ignore (define_global "const_fptoui" (const_fptoui ffoldbomb i32_type) m); - ignore (define_global "const_fptosi" (const_fptosi ffoldbomb i32_type) m); - ignore (define_global "const_ptrtoint" (const_ptrtoint - (const_gep (const_null (pointer_type i8_type)) - [| const_int i32_type 1 |]) - i32_type) m); - ignore (define_global "const_inttoptr" (const_inttoptr (const_add foldbomb five) - void_ptr) m); - ignore (define_global "const_bitcast" (const_bitcast ffoldbomb i64_type) m); - ignore (define_global "const_intcast" - (const_intcast foldbomb i128_type ~is_signed:false) m); - - group "misc constants"; - (* CHECK: const_size_of{{.*}}getelementptr{{.*}}null - * CHECK: const_gep{{.*}}getelementptr - * CHECK: const_select{{.*}}select - * CHECK: const_extractelement{{.*}}extractelement - * CHECK: const_insertelement{{.*}}insertelement - * CHECK: const_shufflevector = global <4 x i32> - *) - ignore (define_global "const_size_of" (size_of (pointer_type i8_type)) m); - ignore (define_global "const_gep" (const_gep foldbomb_gv [| five |]) m); - ignore (define_global "const_select" (const_select - (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 - let one = const_int i32_type 1 in - ignore (define_global "const_extractelement" (const_extractelement - (const_vector [| zero; one; zero; one |]) - (const_trunc foldbomb i32_type)) m); - ignore (define_global "const_insertelement" (const_insertelement - (const_vector [| zero; one; zero; one |]) - zero (const_trunc foldbomb i32_type)) m); - ignore (define_global "const_shufflevector" (const_shufflevector - (const_vector [| zero; one |]) - (const_vector [| one; zero |]) - (const_vector [| const_int i32_type 0; const_int i32_type 1; - const_int i32_type 2; const_int i32_type 3 |])) m); - - group "asm"; begin - let ft = function_type void_type [| i32_type; i32_type; i32_type |] in - ignore (const_inline_asm - ft - "" - "{cx},{ax},{di},~{dirflag},~{fpsr},~{flags},~{edi},~{ecx}" - true - false) - end; - - group "recursive struct"; begin - let nsty = named_struct_type context "rec" in - let pty = pointer_type nsty in - struct_set_body nsty [| i32_type; pty |] false; - let elts = [| const_int i32_type 4; const_pointer_null pty |] in - let grec_init = const_named_struct nsty elts in - ignore (define_global "grec" grec_init m); - ignore (string_of_lltype nsty); - end - - -(*===-- Global Values -----------------------------------------------------===*) - -let test_global_values () = - let (++) x f = f x; x in - let zero32 = const_null i32_type in - - (* CHECK: GVal01 - *) - group "naming"; - let g = define_global "TEMPORARY" zero32 m in - insist ("TEMPORARY" = value_name g); - set_value_name "GVal01" g; - insist ("GVal01" = value_name g); - - (* CHECK: GVal02{{.*}}linkonce - *) - group "linkage"; - let g = define_global "GVal02" zero32 m ++ - set_linkage Linkage.Link_once in - insist (Linkage.Link_once = linkage g); - - (* CHECK: GVal03{{.*}}Hanalei - *) - group "section"; - let g = define_global "GVal03" zero32 m ++ - set_section "Hanalei" in - insist ("Hanalei" = section g); - - (* CHECK: GVal04{{.*}}hidden - *) - group "visibility"; - let g = define_global "GVal04" zero32 m ++ - set_visibility Visibility.Hidden in - insist (Visibility.Hidden = visibility g); - - (* CHECK: GVal05{{.*}}align 128 - *) - 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 forty_two32 = const_int i32_type 42 in - - group "declarations"; begin - (* CHECK: @GVar01 = external global i32 - * CHECK: @QGVar01 = external addrspace(3) global i32 - *) - 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); - - insist (None == lookup_global "QGVar01" m); - let g = declare_qualified_global i32_type "QGVar01" 3 m in - insist (is_declaration g); - insist (qualified_pointer_type float_type 3 == - type_of (declare_qualified_global float_type "QGVar01" 3 m)); - insist (g == declare_qualified_global i32_type "QGVar01" 3 m); - insist (match lookup_global "QGVar01" m with Some x -> x = g - | None -> false); - end; - - group "definitions"; begin - (* CHECK: @GVar02 = global i32 42 - * CHECK: @GVar03 = global i32 42 - * CHECK: @QGVar02 = addrspace(3) global i32 42 - * CHECK: @QGVar03 = addrspace(3) global i32 42 - *) - let g = define_global "GVar02" forty_two32 m in - let g2 = declare_global i32_type "GVar03" m ++ - set_initializer forty_two32 in - insist (not (is_declaration g)); - insist (not (is_declaration g2)); - insist ((global_initializer g) == (global_initializer g2)); - - let g = define_qualified_global "QGVar02" forty_two32 3 m in - let g2 = declare_qualified_global i32_type "QGVar03" 3 m ++ - set_initializer forty_two32 in - insist (not (is_declaration g)); - insist (not (is_declaration g2)); - insist ((global_initializer g) == (global_initializer g2)); - end; - - (* CHECK: GVar04{{.*}}thread_local - *) - group "threadlocal"; - let g = define_global "GVar04" forty_two32 m ++ - set_thread_local true in - insist (is_thread_local g); - - (* CHECK: GVar05{{.*}}thread_local(initialexec) - *) - group "threadlocal_mode"; - let g = define_global "GVar05" forty_two32 m ++ - set_thread_local_mode ThreadLocalMode.InitialExec in - insist ((thread_local_mode g) = ThreadLocalMode.InitialExec); - - (* CHECK: GVar06{{.*}}externally_initialized - *) - group "externally_initialized"; - let g = define_global "GVar06" forty_two32 m ++ - set_externally_initialized true in - insist (is_externally_initialized g); - - (* CHECK-NOWHERE-NOT: GVar07 - *) - group "delete"; - let g = define_global "GVar07" forty_two32 m in - delete_global g; - - (* CHECK: ConstGlobalVar{{.*}}constant - *) - group "constant"; - let g = define_global "ConstGlobalVar" forty_two32 m in - insist (not (is_global_constant g)); - set_global_constant true g; - insist (is_global_constant g); - - begin group "iteration"; - let m = create_module context "temp" in - - insist (At_end m = global_begin m); - insist (At_start m = global_end m); - - let g1 = declare_global i32_type "One" m in - let g2 = declare_global i32_type "Two" m in - - insist (Before g1 = global_begin m); - insist (Before g2 = global_succ g1); - insist (At_end m = global_succ g2); - - insist (After g2 = global_end m); - insist (After g1 = global_pred g2); - insist (At_start m = global_pred g1); - - let lf s x = s ^ "->" ^ value_name x in - insist ("->One->Two" = fold_left_globals lf "" m); - - let rf x s = value_name x ^ "<-" ^ s in - insist ("One<-Two<-" = fold_right_globals rf m ""); - - dispose_module m - end - -(* String globals built below are emitted here. - * CHECK: build_global_string{{.*}}stringval - *) - - -(*===-- Uses --------------------------------------------------------------===*) - -let test_uses () = - let ty = function_type i32_type [| i32_type; i32_type |] in - let fn = define_function "use_function" ty m in - let b = builder_at_end context (entry_block fn) in - - let p1 = param fn 0 in - let p2 = param fn 1 in - let v1 = build_add p1 p2 "v1" b in - let v2 = build_add p1 v1 "v2" b in - let _ = build_add v1 v2 "v3" b in - - let lf s u = value_name (user u) ^ "->" ^ s in - insist ("v2->v3->" = fold_left_uses lf "" v1); - let rf u s = value_name (user u) ^ "<-" ^ s in - insist ("v3<-v2<-" = fold_right_uses rf v1 ""); - - let lf s u = value_name (used_value u) ^ "->" ^ s in - insist ("v1->v1->" = fold_left_uses lf "" v1); - - let rf u s = value_name (used_value u) ^ "<-" ^ s in - insist ("v1<-v1<-" = fold_right_uses rf v1 ""); - - ignore (build_unreachable b) - - -(*===-- Users -------------------------------------------------------------===*) - -let test_users () = - let ty = function_type i32_type [| i32_type; i32_type |] in - let fn = define_function "user_function" ty m in - let b = builder_at_end context (entry_block fn) in - - let p1 = param fn 0 in - let p2 = param fn 1 in - let a3 = build_alloca i32_type "user_alloca" b in - let p3 = build_load a3 "user_load" b in - let i = build_add p1 p2 "sum" b in - - insist ((num_operands i) = 2); - insist ((operand i 0) = p1); - insist ((operand i 1) = p2); - - set_operand i 1 p3; - insist ((operand i 1) != p2); - insist ((operand i 1) = p3); - - ignore (build_unreachable b) - - -(*===-- Aliases -----------------------------------------------------------===*) - -let test_aliases () = - (* CHECK: @alias = alias i32* @aliasee - *) - let forty_two32 = const_int i32_type 42 in - let v = define_global "aliasee" forty_two32 m in - ignore (add_alias m (pointer_type i32_type) v "alias") - - -(*===-- Functions ---------------------------------------------------------===*) - -let test_functions () = - let ty = function_type i32_type [| i32_type; i64_type |] in - let ty2 = function_type i8_type [| i8_type; i64_type |] in - - (* CHECK: declare i32 @Fn1(i32, i64) - *) - begin group "declare"; - insist (None = lookup_function "Fn1" m); - let fn = declare_function "Fn1" ty m in - 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); - insist (m == global_parent fn) - end; - - (* CHECK-NOWHERE-NOT: Fn2 - *) - group "delete"; - let fn = declare_function "Fn2" ty m in - delete_function fn; - - (* CHECK: define{{.*}}Fn3 - *) - group "define"; - let fn = define_function "Fn3" ty m in - insist (not (is_declaration fn)); - insist (1 = Array.length (basic_blocks fn)); - ignore (build_unreachable (builder_at_end context (entry_block fn))); - - (* CHECK: define{{.*}}Fn4{{.*}}Param1{{.*}}Param2 - *) - group "params"; - let fn = define_function "Fn4" ty m in - let params = params fn in - insist (2 = Array.length params); - insist (params.(0) = param fn 0); - insist (params.(1) = param fn 1); - insist (i32_type = type_of params.(0)); - insist (i64_type = type_of params.(1)); - set_value_name "Param1" params.(0); - set_value_name "Param2" params.(1); - ignore (build_unreachable (builder_at_end context (entry_block fn))); - - (* CHECK: fastcc{{.*}}Fn5 - *) - group "callconv"; - let fn = define_function "Fn5" ty m in - 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 context (entry_block fn))); - - begin group "gc"; - (* CHECK: Fn6{{.*}}gc{{.*}}shadowstack - *) - let fn = define_function "Fn6" ty m in - insist (None = gc fn); - set_gc (Some "ocaml") fn; - insist (Some "ocaml" = gc fn); - set_gc None fn; - insist (None = gc fn); - set_gc (Some "shadowstack") fn; - ignore (build_unreachable (builder_at_end context (entry_block fn))); - end; - - begin group "iteration"; - let m = create_module context "temp" in - - insist (At_end m = function_begin m); - insist (At_start m = function_end m); - - let f1 = define_function "One" ty m in - let f2 = define_function "Two" ty m in - - insist (Before f1 = function_begin m); - insist (Before f2 = function_succ f1); - insist (At_end m = function_succ f2); - - insist (After f2 = function_end m); - insist (After f1 = function_pred f2); - insist (At_start m = function_pred f1); - - let lf s x = s ^ "->" ^ value_name x in - insist ("->One->Two" = fold_left_functions lf "" m); - - let rf x s = value_name x ^ "<-" ^ s in - insist ("One<-Two<-" = fold_right_functions rf m ""); - - dispose_module m - end - - -(*===-- Params ------------------------------------------------------------===*) - -let test_params () = - begin group "iteration"; - let m = create_module context "temp" in - - let vf = define_function "void" (function_type void_type [| |]) m in - - insist (At_end vf = param_begin vf); - insist (At_start vf = param_end vf); - - let ty = function_type void_type [| i32_type; i32_type |] in - let f = define_function "f" ty m in - let p1 = param f 0 in - let p2 = param f 1 in - set_value_name "One" p1; - set_value_name "Two" p2; - add_param_attr p1 Attribute.Sext; - add_param_attr p2 Attribute.Noalias; - remove_param_attr p2 Attribute.Noalias; - add_function_attr f Attribute.Nounwind; - add_function_attr f Attribute.Noreturn; - remove_function_attr f Attribute.Noreturn; - - insist (Before p1 = param_begin f); - insist (Before p2 = param_succ p1); - insist (At_end f = param_succ p2); - - insist (After p2 = param_end f); - insist (After p1 = param_pred p2); - insist (At_start f = param_pred p1); - - let lf s x = s ^ "->" ^ value_name x in - insist ("->One->Two" = fold_left_params lf "" f); - - let rf x s = value_name x ^ "<-" ^ s in - insist ("One<-Two<-" = fold_right_params rf f ""); - - dispose_module m - end - - -(*===-- Basic Blocks ------------------------------------------------------===*) - -let test_basic_blocks () = - let ty = function_type void_type [| |] in - - (* CHECK: Bb1 - *) - group "entry"; - let fn = declare_function "X" ty m in - let bb = append_block context "Bb1" fn in - insist (bb = entry_block fn); - ignore (build_unreachable (builder_at_end context bb)); - - (* CHECK-NOWHERE-NOT: Bb2 - *) - group "delete"; - let fn = declare_function "X2" ty m in - let bb = append_block context "Bb2" fn in - delete_block bb; - - group "insert"; - let fn = declare_function "X3" ty m in - let bbb = append_block context "b" fn in - let bba = insert_block context "a" bbb in - insist ([| bba; bbb |] = basic_blocks fn); - ignore (build_unreachable (builder_at_end context bba)); - ignore (build_unreachable (builder_at_end context bbb)); - - (* CHECK: Bb3 - *) - group "name/value"; - let fn = define_function "X4" ty m in - let bb = entry_block fn in - ignore (build_unreachable (builder_at_end context bb)); - let bbv = value_of_block bb in - set_value_name "Bb3" bbv; - insist ("Bb3" = value_name bbv); - - group "casts"; - let fn = define_function "X5" ty m in - let bb = entry_block fn in - ignore (build_unreachable (builder_at_end context 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))); - - begin group "iteration"; - let m = create_module context "temp" in - let f = declare_function "Temp" (function_type i32_type [| |]) m in - - insist (At_end f = block_begin f); - insist (At_start f = block_end f); - - let b1 = append_block context "One" f in - let b2 = append_block context "Two" f in - - insist (Before b1 = block_begin f); - insist (Before b2 = block_succ b1); - insist (At_end f = block_succ b2); - - insist (After b2 = block_end f); - insist (After b1 = block_pred b2); - insist (At_start f = block_pred b1); - - let lf s x = s ^ "->" ^ value_name (value_of_block x) in - insist ("->One->Two" = fold_left_blocks lf "" f); - - let rf x s = value_name (value_of_block x) ^ "<-" ^ s in - insist ("One<-Two<-" = fold_right_blocks rf f ""); - - dispose_module m - end - - -(*===-- Instructions ------------------------------------------------------===*) - -let test_instructions () = - begin group "iteration"; - let m = create_module context "temp" in - let fty = function_type void_type [| i32_type; i32_type |] in - let f = define_function "f" fty m in - let bb = entry_block f in - let b = builder_at context (At_end bb) in - - insist (At_end bb = instr_begin bb); - insist (At_start bb = instr_end bb); - - let i1 = build_add (param f 0) (param f 1) "One" b in - let i2 = build_sub (param f 0) (param f 1) "Two" b in - - insist (Before i1 = instr_begin bb); - insist (Before i2 = instr_succ i1); - insist (At_end bb = instr_succ i2); - - insist (After i2 = instr_end bb); - insist (After i1 = instr_pred i2); - insist (At_start bb = instr_pred i1); - - let lf s x = s ^ "->" ^ value_name x in - insist ("->One->Two" = fold_left_instrs lf "" bb); - - let rf x s = value_name x ^ "<-" ^ s in - insist ("One<-Two<-" = fold_right_instrs rf bb ""); - - dispose_module m - end - - -(*===-- Builder -----------------------------------------------------------===*) - -let test_builder () = - let (++) x f = f x; x in - - begin group "parent"; - insist (try - ignore (insertion_block (builder context)); - false - with Not_found -> - true); - - let fty = function_type void_type [| i32_type |] in - let fn = define_function "BuilderParent" fty m in - let bb = entry_block fn in - let b = builder_at_end context bb in - let p = param fn 0 in - let sum = build_add p p "sum" b in - ignore (build_ret_void b); - - insist (fn = block_parent bb); - insist (fn = param_parent p); - insist (bb = instr_parent sum); - insist (bb = insertion_block b) - end; - - group "ret void"; - begin - (* CHECK: ret void - *) - let fty = function_type void_type [| |] in - let fn = declare_function "X6" fty m in - let b = builder_at_end context (append_block context "Bb01" fn) in - ignore (build_ret_void b) - end; - - group "ret aggregate"; - begin - (* CHECK: ret { i8, i64 } { i8 4, i64 5 } - *) - let sty = struct_type context [| i8_type; i64_type |] in - let fty = function_type sty [| |] in - let fn = declare_function "XA6" fty m in - let b = builder_at_end context (append_block context "Bb01" fn) in - let agg = [| const_int i8_type 4; const_int i64_type 5 |] in - ignore (build_aggregate_ret agg b) - end; - - (* The rest of the tests will use one big function. *) - let fty = function_type i32_type [| i32_type; i32_type |] in - let fn = define_function "X7" fty m in - let atentry = builder_at_end context (entry_block fn) in - let p1 = param fn 0 ++ set_value_name "P1" in - let p2 = param fn 1 ++ set_value_name "P2" in - let f1 = build_uitofp p1 float_type "F1" atentry in - let f2 = build_uitofp p2 float_type "F2" atentry in - - let bb00 = append_block context "Bb00" fn in - ignore (build_unreachable (builder_at_end context bb00)); - - group "function attribute"; - begin - ignore (add_function_attr fn Attribute.UWTable); - (* CHECK: X7{{.*}}#0 - * #0 is uwtable, defined at EOF. - *) - insist ([Attribute.UWTable] = function_attr fn); - end; - - group "casts"; begin - let void_ptr = pointer_type i8_type in - - (* CHECK-DAG: %build_trunc = trunc i32 %P1 to i8 - * CHECK-DAG: %build_trunc2 = trunc i32 %P1 to i8 - * CHECK-DAG: %build_trunc3 = trunc i32 %P1 to i8 - * CHECK-DAG: %build_zext = zext i8 %build_trunc to i32 - * CHECK-DAG: %build_zext2 = zext i8 %build_trunc to i32 - * CHECK-DAG: %build_sext = sext i32 %build_zext to i64 - * CHECK-DAG: %build_sext2 = sext i32 %build_zext to i64 - * CHECK-DAG: %build_sext3 = sext i32 %build_zext to i64 - * CHECK-DAG: %build_uitofp = uitofp i64 %build_sext to float - * CHECK-DAG: %build_sitofp = sitofp i32 %build_zext to double - * CHECK-DAG: %build_fptoui = fptoui float %build_uitofp to i32 - * CHECK-DAG: %build_fptosi = fptosi double %build_sitofp to i64 - * CHECK-DAG: %build_fptrunc = fptrunc double %build_sitofp to float - * CHECK-DAG: %build_fptrunc2 = fptrunc double %build_sitofp to float - * CHECK-DAG: %build_fpext = fpext float %build_fptrunc to double - * CHECK-DAG: %build_fpext2 = fpext float %build_fptrunc to double - * CHECK-DAG: %build_inttoptr = inttoptr i32 %P1 to i8* - * CHECK-DAG: %build_ptrtoint = ptrtoint i8* %build_inttoptr to i64 - * CHECK-DAG: %build_ptrtoint2 = ptrtoint i8* %build_inttoptr to i64 - * CHECK-DAG: %build_bitcast = bitcast i64 %build_ptrtoint to double - * CHECK-DAG: %build_bitcast2 = bitcast i64 %build_ptrtoint to double - * CHECK-DAG: %build_bitcast3 = bitcast i64 %build_ptrtoint to double - * CHECK-DAG: %build_bitcast4 = bitcast i64 %build_ptrtoint to double - * CHECK-DAG: %build_pointercast = bitcast i8* %build_inttoptr to i16* - *) - let inst28 = build_trunc p1 i8_type "build_trunc" atentry in - let inst29 = build_zext inst28 i32_type "build_zext" atentry in - let inst30 = build_sext inst29 i64_type "build_sext" atentry in - let inst31 = build_uitofp inst30 float_type "build_uitofp" atentry in - let inst32 = build_sitofp inst29 double_type "build_sitofp" atentry in - ignore(build_fptoui inst31 i32_type "build_fptoui" atentry); - ignore(build_fptosi inst32 i64_type "build_fptosi" atentry); - let inst35 = build_fptrunc inst32 float_type "build_fptrunc" atentry in - ignore(build_fpext inst35 double_type "build_fpext" atentry); - let inst37 = build_inttoptr p1 void_ptr "build_inttoptr" atentry in - let inst38 = build_ptrtoint inst37 i64_type "build_ptrtoint" atentry in - ignore(build_bitcast inst38 double_type "build_bitcast" atentry); - ignore(build_zext_or_bitcast inst38 double_type "build_bitcast2" atentry); - ignore(build_sext_or_bitcast inst38 double_type "build_bitcast3" atentry); - ignore(build_trunc_or_bitcast inst38 double_type "build_bitcast4" atentry); - ignore(build_pointercast inst37 (pointer_type i16_type) "build_pointercast" atentry); - - ignore(build_zext_or_bitcast inst28 i32_type "build_zext2" atentry); - ignore(build_sext_or_bitcast inst29 i64_type "build_sext2" atentry); - ignore(build_trunc_or_bitcast p1 i8_type "build_trunc2" atentry); - ignore(build_pointercast inst37 i64_type "build_ptrtoint2" atentry); - ignore(build_intcast inst29 i64_type "build_sext3" atentry); - ignore(build_intcast p1 i8_type "build_trunc3" atentry); - ignore(build_fpcast inst35 double_type "build_fpext2" atentry); - ignore(build_fpcast inst32 float_type "build_fptrunc2" atentry); - end; - - group "comparisons"; begin - (* CHECK: %build_icmp_ne = icmp ne i32 %P1, %P2 - * CHECK: %build_icmp_sle = icmp sle i32 %P2, %P1 - * CHECK: %build_fcmp_false = fcmp false float %F1, %F2 - * CHECK: %build_fcmp_true = fcmp true float %F2, %F1 - * CHECK: %build_is_null{{.*}}= icmp eq{{.*}}%X0,{{.*}}null - * CHECK: %build_is_not_null = icmp ne i8* %X1, null - * CHECK: %build_ptrdiff - *) - ignore (build_icmp Icmp.Ne p1 p2 "build_icmp_ne" atentry); - ignore (build_icmp Icmp.Sle p2 p1 "build_icmp_sle" atentry); - ignore (build_fcmp Fcmp.False f1 f2 "build_fcmp_false" atentry); - ignore (build_fcmp Fcmp.True f2 f1 "build_fcmp_true" atentry); - let g0 = declare_global (pointer_type i8_type) "g0" m in - let g1 = declare_global (pointer_type i8_type) "g1" m in - let p0 = build_load g0 "X0" atentry in - let p1 = build_load g1 "X1" atentry in - ignore (build_is_null p0 "build_is_null" atentry); - ignore (build_is_not_null p1 "build_is_not_null" atentry); - ignore (build_ptrdiff p1 p0 "build_ptrdiff" atentry); - end; - - group "miscellaneous"; begin - (* CHECK: %build_call = tail call cc63 i32 @{{.*}}(i32 signext %P2, i32 %P1) - * CHECK: %build_select = select i1 %build_icmp, i32 %P1, i32 %P2 - * CHECK: %build_va_arg = va_arg i8** null, i32 - * CHECK: %build_extractelement = extractelement <4 x i32> %Vec1, i32 %P2 - * CHECK: %build_insertelement = insertelement <4 x i32> %Vec1, i32 %P1, i32 %P2 - * CHECK: %build_shufflevector = shufflevector <4 x i32> %Vec1, <4 x i32> %Vec2, <4 x i32> - * CHECK: %build_insertvalue0 = insertvalue{{.*}}%bl, i32 1, 0 - * CHECK: %build_extractvalue = extractvalue{{.*}}%build_insertvalue1, 1 - *) - let ci = build_call fn [| p2; p1 |] "build_call" atentry in - insist (CallConv.c = instruction_call_conv ci); - set_instruction_call_conv 63 ci; - insist (63 = instruction_call_conv ci); - insist (not (is_tail_call ci)); - set_tail_call true ci; - insist (is_tail_call ci); - add_instruction_param_attr ci 1 Attribute.Sext; - add_instruction_param_attr ci 2 Attribute.Noalias; - remove_instruction_param_attr ci 2 Attribute.Noalias; - - let inst46 = build_icmp Icmp.Eq p1 p2 "build_icmp" atentry in - ignore (build_select inst46 p1 p2 "build_select" atentry); - ignore (build_va_arg - (const_null (pointer_type (pointer_type i8_type))) - i32_type "build_va_arg" atentry); - - (* Set up some vector vregs. *) - 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 sty = struct_type context [| i32_type; i8_type |] in - - ignore (build_extractelement vec1 p2 "build_extractelement" atentry); - ignore (build_insertelement vec1 p1 p2 "build_insertelement" atentry); - ignore (build_shufflevector vec1 vec2 t3 "build_shufflevector" atentry); - - let p = build_alloca sty "ba" atentry in - let agg = build_load p "bl" atentry in - let agg0 = build_insertvalue agg (const_int i32_type 1) 0 - "build_insertvalue0" atentry in - let agg1 = build_insertvalue agg0 (const_int i8_type 2) 1 - "build_insertvalue1" atentry in - ignore (build_extractvalue agg1 1 "build_extractvalue" atentry) - end; - - group "metadata"; begin - (* CHECK: %metadata = add i32 %P1, %P2, !test !1 - * !1 is metadata emitted at EOF. - *) - let i = build_add p1 p2 "metadata" atentry in - insist ((has_metadata i) = false); - - let m1 = const_int i32_type 1 in - let m2 = mdstring context "metadata test" in - let md = mdnode context [| m1; m2 |] in - - let kind = mdkind_id context "test" in - set_metadata i kind md; - - insist ((has_metadata i) = true); - insist ((metadata i kind) = Some md); - - clear_metadata i kind; - - insist ((has_metadata i) = false); - insist ((metadata i kind) = None); - - set_metadata i kind md - end; - - group "named metadata"; begin - (* !llvm.module.flags is emitted at EOF. *) - let n1 = const_int i32_type 1 in - let n2 = mdstring context "Debug Info Version" in - let md = mdnode context [| n1; n2; n1 |] in - add_named_metadata_operand m "llvm.module.flags" md; - - insist ((get_named_metadata m "llvm.module.flags") = [| md |]) - end; - - group "dbg"; begin - (* CHECK: %dbg = add i32 %P1, %P2, !dbg !2 - * !2 is metadata emitted at EOF. - *) - insist ((current_debug_location atentry) = None); - - let m_line = const_int i32_type 2 in - let m_col = const_int i32_type 3 in - let m_scope = mdnode context [| |] in - let m_inlined = mdnode context [| |] in - let md = mdnode context [| m_line; m_col; m_scope; m_inlined |] in - set_current_debug_location atentry md; - - insist ((current_debug_location atentry) = Some md); - - let i = build_add p1 p2 "dbg" atentry in - insist ((has_metadata i) = true); - - clear_current_debug_location atentry - end; - - group "ret"; begin - (* CHECK: ret{{.*}}P1 - *) - let ret = build_ret p1 atentry in - position_before ret atentry - end; - - (* see test/Feature/exception.ll *) - let bblpad = append_block context "Bblpad" fn in - let rt = struct_type context [| pointer_type i8_type; i32_type |] in - let ft = var_arg_function_type i32_type [||] in - let personality = declare_function "__gxx_personality_v0" ft m in - let ztic = declare_global (pointer_type i8_type) "_ZTIc" m in - let ztid = declare_global (pointer_type i8_type) "_ZTId" m in - let ztipkc = declare_global (pointer_type i8_type) "_ZTIPKc" m in - begin - set_global_constant true ztic; - set_global_constant true ztid; - set_global_constant true ztipkc; - let lp = build_landingpad rt personality 0 "lpad" - (builder_at_end context bblpad) in begin - set_cleanup lp true; - add_clause lp ztic; - insist((pointer_type (pointer_type i8_type)) = type_of ztid); - let ety = pointer_type (pointer_type i8_type) in - add_clause lp (const_array ety [| ztipkc; ztid |]); - ignore (build_resume lp (builder_at_end context bblpad)); - end; - (* CHECK: landingpad{{.*}}personality{{.*}}__gxx_personality_v0 - * CHECK: cleanup - * CHECK: catch{{.*}}i8**{{.*}}@_ZTIc - * CHECK: filter{{.*}}@_ZTIPKc{{.*}}@_ZTId - * CHECK: resume - * *) - end; - - group "br"; begin - (* CHECK: br{{.*}}Bb02 - *) - let bb02 = append_block context "Bb02" fn in - let b = builder_at_end context bb02 in - ignore (build_br bb02 b) - end; - - group "cond_br"; begin - (* CHECK: br{{.*}}build_br{{.*}}Bb03{{.*}}Bb00 - *) - let bb03 = append_block context "Bb03" fn in - let b = builder_at_end context bb03 in - let cond = build_trunc p1 i1_type "build_br" b in - ignore (build_cond_br cond bb03 bb00 b) - end; - - group "switch"; begin - (* CHECK: switch{{.*}}P1{{.*}}SwiBlock3 - * CHECK: 2,{{.*}}SwiBlock2 - *) - let bb1 = append_block context "SwiBlock1" fn in - let bb2 = append_block context "SwiBlock2" fn in - ignore (build_unreachable (builder_at_end context bb2)); - let bb3 = append_block context "SwiBlock3" fn in - ignore (build_unreachable (builder_at_end context bb3)); - let si = build_switch p1 bb3 1 (builder_at_end context bb1) in begin - ignore (add_case si (const_int i32_type 2) bb2); - insist (switch_default_dest si = bb3); - end; - end; - - group "malloc/free"; begin - (* CHECK: call{{.*}}@malloc(i32 ptrtoint - * CHECK: call{{.*}}@free(i8* - * CHECK: call{{.*}}@malloc(i32 % - *) - let bb1 = append_block context "MallocBlock1" fn in - let m1 = (build_malloc (pointer_type i32_type) "m1" - (builder_at_end context bb1)) in - ignore (build_free m1 (builder_at_end context bb1)); - ignore (build_array_malloc i32_type p1 "m2" (builder_at_end context bb1)); - ignore (build_unreachable (builder_at_end context bb1)); - end; - - group "indirectbr"; begin - (* CHECK: indirectbr i8* blockaddress(@X7, %IBRBlock2), [label %IBRBlock2, label %IBRBlock3] - *) - let bb1 = append_block context "IBRBlock1" fn in - - let bb2 = append_block context "IBRBlock2" fn in - ignore (build_unreachable (builder_at_end context bb2)); - - let bb3 = append_block context "IBRBlock3" fn in - ignore (build_unreachable (builder_at_end context bb3)); - - let addr = block_address fn bb2 in - let ibr = build_indirect_br addr 2 (builder_at_end context bb1) in - ignore (add_destination ibr bb2); - ignore (add_destination ibr bb3) - end; - - group "invoke"; begin - (* CHECK: build_invoke{{.*}}invoke{{.*}}P1{{.*}}P2 - * CHECK: to{{.*}}Bb04{{.*}}unwind{{.*}}Bblpad - *) - let bb04 = append_block context "Bb04" fn in - let b = builder_at_end context bb04 in - ignore (build_invoke fn [| p1; p2 |] bb04 bblpad "build_invoke" b) - end; - - group "unreachable"; begin - (* CHECK: unreachable - *) - let bb06 = append_block context "Bb06" fn in - let b = builder_at_end context bb06 in - ignore (build_unreachable b) - end; - - group "arithmetic"; begin - let bb07 = append_block context "Bb07" fn in - let b = builder_at_end context bb07 in - - (* CHECK: %build_add = add i32 %P1, %P2 - * CHECK: %build_nsw_add = add nsw i32 %P1, %P2 - * CHECK: %build_nuw_add = add nuw i32 %P1, %P2 - * CHECK: %build_fadd = fadd float %F1, %F2 - * CHECK: %build_sub = sub i32 %P1, %P2 - * CHECK: %build_nsw_sub = sub nsw i32 %P1, %P2 - * CHECK: %build_nuw_sub = sub nuw i32 %P1, %P2 - * CHECK: %build_fsub = fsub float %F1, %F2 - * CHECK: %build_mul = mul i32 %P1, %P2 - * CHECK: %build_nsw_mul = mul nsw i32 %P1, %P2 - * CHECK: %build_nuw_mul = mul nuw i32 %P1, %P2 - * CHECK: %build_fmul = fmul float %F1, %F2 - * CHECK: %build_udiv = udiv i32 %P1, %P2 - * CHECK: %build_sdiv = sdiv i32 %P1, %P2 - * CHECK: %build_exact_sdiv = sdiv exact i32 %P1, %P2 - * CHECK: %build_fdiv = fdiv float %F1, %F2 - * CHECK: %build_urem = urem i32 %P1, %P2 - * CHECK: %build_srem = srem i32 %P1, %P2 - * CHECK: %build_frem = frem float %F1, %F2 - * CHECK: %build_shl = shl i32 %P1, %P2 - * CHECK: %build_lshl = lshr i32 %P1, %P2 - * CHECK: %build_ashl = ashr i32 %P1, %P2 - * CHECK: %build_and = and i32 %P1, %P2 - * CHECK: %build_or = or i32 %P1, %P2 - * CHECK: %build_xor = xor i32 %P1, %P2 - * CHECK: %build_neg = sub i32 0, %P1 - * CHECK: %build_nsw_neg = sub nsw i32 0, %P1 - * CHECK: %build_nuw_neg = sub nuw i32 0, %P1 - * CHECK: %build_fneg = fsub float {{.*}}0{{.*}}, %F1 - * CHECK: %build_not = xor i32 %P1, -1 - *) - ignore (build_add p1 p2 "build_add" b); - ignore (build_nsw_add p1 p2 "build_nsw_add" b); - ignore (build_nuw_add p1 p2 "build_nuw_add" b); - ignore (build_fadd f1 f2 "build_fadd" b); - ignore (build_sub p1 p2 "build_sub" b); - ignore (build_nsw_sub p1 p2 "build_nsw_sub" b); - ignore (build_nuw_sub p1 p2 "build_nuw_sub" b); - ignore (build_fsub f1 f2 "build_fsub" b); - ignore (build_mul p1 p2 "build_mul" b); - ignore (build_nsw_mul p1 p2 "build_nsw_mul" b); - ignore (build_nuw_mul p1 p2 "build_nuw_mul" b); - ignore (build_fmul f1 f2 "build_fmul" b); - ignore (build_udiv p1 p2 "build_udiv" b); - ignore (build_sdiv p1 p2 "build_sdiv" b); - ignore (build_exact_sdiv p1 p2 "build_exact_sdiv" b); - ignore (build_fdiv f1 f2 "build_fdiv" b); - ignore (build_urem p1 p2 "build_urem" b); - ignore (build_srem p1 p2 "build_srem" b); - ignore (build_frem f1 f2 "build_frem" b); - ignore (build_shl p1 p2 "build_shl" b); - ignore (build_lshr p1 p2 "build_lshl" b); - ignore (build_ashr p1 p2 "build_ashl" b); - ignore (build_and p1 p2 "build_and" b); - ignore (build_or p1 p2 "build_or" b); - ignore (build_xor p1 p2 "build_xor" b); - ignore (build_neg p1 "build_neg" b); - ignore (build_nsw_neg p1 "build_nsw_neg" b); - ignore (build_nuw_neg p1 "build_nuw_neg" b); - ignore (build_fneg f1 "build_fneg" b); - ignore (build_not p1 "build_not" b); - ignore (build_unreachable b) - end; - - group "memory"; begin - let bb08 = append_block context "Bb08" fn in - let b = builder_at_end context bb08 in - - (* CHECK: %build_alloca = alloca i32 - * CHECK: %build_array_alloca = alloca i32, i32 %P2 - * CHECK: %build_load = load volatile i32* %build_array_alloca, align 4 - * CHECK: store volatile i32 %P2, i32* %build_alloca, align 4 - * CHECK: %build_gep = getelementptr i32* %build_array_alloca, i32 %P2 - * CHECK: %build_in_bounds_gep = getelementptr inbounds i32* %build_array_alloca, i32 %P2 - * CHECK: %build_struct_gep = getelementptr inbounds{{.*}}%build_alloca2, i32 0, i32 1 - * CHECK: %build_atomicrmw = atomicrmw xchg i8* %p, i8 42 seq_cst - *) - let alloca = build_alloca i32_type "build_alloca" b in - let array_alloca = build_array_alloca i32_type p2 "build_array_alloca" b in - - let load = build_load array_alloca "build_load" b in - ignore(set_alignment 4 load); - ignore(set_volatile true load); - insist(true = is_volatile load); - insist(4 = alignment load); - - let store = build_store p2 alloca b in - ignore(set_volatile true store); - ignore(set_alignment 4 store); - insist(true = is_volatile store); - insist(4 = alignment store); - ignore(build_gep array_alloca [| p2 |] "build_gep" b); - ignore(build_in_bounds_gep array_alloca [| p2 |] "build_in_bounds_gep" b); - - let sty = struct_type context [| i32_type; i8_type |] in - let alloca2 = build_alloca sty "build_alloca2" b in - ignore(build_struct_gep alloca2 1 "build_struct_gep" b); - - let p = build_alloca i8_type "p" b in - ignore(build_atomicrmw AtomicRMWBinOp.Xchg p (const_int i8_type 42) - AtomicOrdering.SequentiallyConsistent false "build_atomicrmw" - b); - - ignore(build_unreachable b) - end; - - group "string"; begin - let bb09 = append_block context "Bb09" fn in - let b = builder_at_end context bb09 in - let p = build_alloca (pointer_type i8_type) "p" b in - (* build_global_string is emitted above. - * CHECK: store{{.*}}build_global_string1{{.*}}p - * *) - ignore (build_global_string "stringval" "build_global_string" b); - let g = build_global_stringptr "stringval" "build_global_string1" b in - ignore (build_store g p b); - ignore(build_unreachable b); - end; - - group "phi"; begin - (* CHECK: PhiNode{{.*}}P1{{.*}}PhiBlock1{{.*}}P2{{.*}}PhiBlock2 - *) - let b1 = append_block context "PhiBlock1" fn in - let b2 = append_block context "PhiBlock2" fn in - - let jb = append_block context "PhiJoinBlock" fn in - ignore (build_br jb (builder_at_end context b1)); - ignore (build_br jb (builder_at_end context b2)); - let at_jb = builder_at_end context 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 - -(* End-of-file checks for things like metdata and attributes. - * CHECK: attributes #0 = {{.*}}uwtable{{.*}} - * CHECK: !llvm.module.flags = !{!0} - * CHECK: !0 = metadata !{i32 1, metadata !"Debug Info Version", i32 1} - * CHECK: !1 = metadata !{i32 1, metadata !"metadata test"} - * CHECK: !2 = metadata !{i32 2, i32 3, metadata !3, metadata !3} - *) - -(*===-- Pass Managers -----------------------------------------------------===*) - -let test_pass_manager () = - let (++) x f = ignore (f x); x in - - begin group "module pass manager"; - ignore (PassManager.create () - ++ PassManager.run_module m - ++ PassManager.dispose) - end; - - begin group "function pass manager"; - let fty = function_type void_type [| |] in - let fn = define_function "FunctionPassManager" fty m in - ignore (build_ret_void (builder_at_end context (entry_block fn))); - - ignore (PassManager.create_function m - ++ PassManager.initialize - ++ PassManager.run_function fn - ++ PassManager.finalize - ++ PassManager.dispose) - end - - -(*===-- Memory Buffer -----------------------------------------------------===*) - -let test_memory_buffer () = - group "memory buffer"; - let buf = MemoryBuffer.of_string "foobar" in - insist ((MemoryBuffer.as_string buf) = "foobar") - - -(*===-- 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); - - dispose_module m - - -(*===-- Driver ------------------------------------------------------------===*) - -let _ = - suite "conversion" test_conversion; - suite "target" test_target; - suite "constants" test_constants; - suite "global values" test_global_values; - suite "global variables" test_global_variables; - suite "uses" test_uses; - suite "users" test_users; - suite "aliases" test_aliases; - suite "functions" test_functions; - suite "params" test_params; - suite "basic blocks" test_basic_blocks; - suite "instructions" test_instructions; - suite "builder" test_builder; - suite "pass manager" test_pass_manager; - suite "memory buffer" test_memory_buffer; - suite "writer" test_writer; (* Keep this last; it disposes m. *) - exit !exit_status diff --git a/test/Bindings/llvm-c/disassemble.test b/test/Bindings/llvm-c/disassemble.test index 201e914..bb7a9a0 100644 --- a/test/Bindings/llvm-c/disassemble.test +++ b/test/Bindings/llvm-c/disassemble.test @@ -1,15 +1,27 @@ ; RUN: llvm-c-test --disassemble < %s | FileCheck %s +armv8-linux-gnu +crypto 02 00 81 e0 02 03 b0 f3 +;CHECK: triple: armv8-linux-gnu, features: +crypto +;CHECK: 02 00 81 e0 add r0, r1, r2 +;CHECK: 02 03 b0 f3 aese.8 q0, q1 -arm-linux-android 44 26 1f e5 0c 10 4b e2 02 20 81 e0 -;CHECK: triple: arm-linux-android +armv8-linux-gnu -crypto 02 00 81 e0 02 03 b0 f3 +;CHECK: triple: armv8-linux-gnu, features: -crypto +;CHECK: 02 00 81 e0 add r0, r1, r2 +;CHECK: 02 ??? +;CHECK: 03 ??? +;CHECK: b0 ??? +;CHECK: f3 ??? + +arm-linux-android NULL 44 26 1f e5 0c 10 4b e2 02 20 81 e0 +;CHECK: triple: arm-linux-android, features: NULL ;CHECK: ldr r2, [pc, #-1604] ;CHECK: sub r1, r11, #12 ;CHECK: 02 20 81 e0 ;CHECK: add r2, r1, r2 -x86_64-linux-unknown 48 83 c4 38 5b 5d 41 5c 41 5d 41 5e 41 5f c3 -;CHECK: triple: x86_64-linux-unknown +x86_64-linux-unknown NULL 48 83 c4 38 5b 5d 41 5c 41 5d 41 5e 41 5f c3 +;CHECK: triple: x86_64-linux-unknown, features: NULL ;CHECK: addq $56, %rsp ;CHECK: popq %rbx ;CHECK: popq %rbp @@ -19,11 +31,13 @@ x86_64-linux-unknown 48 83 c4 38 5b 5d 41 5c 41 5d 41 5e 41 5f c3 ;CHECK: popq %r15 ;CHECK: ret -i686-apple-darwin 0f b7 4c 24 0a e8 29 ce ff ff +i686-apple-darwin NULL 0f b7 4c 24 0a e8 29 ce ff ff +;CHECK: triple: i686-apple-darwin, features: NULL ;CHECK: movzwl 10(%esp), %ecx ;CHECK: calll -12759 -i686-linux-unknown dd 44 24 04 d9 e1 c3 +i686-linux-unknown NULL dd 44 24 04 d9 e1 c3 +;CHECK: triple: i686-linux-unknown, features: NULL ;CHECK: fldl 4(%esp) ;CHECK: fabs ;CHECK: ret diff --git a/test/Bindings/llvm-c/objectfile.ll b/test/Bindings/llvm-c/objectfile.ll new file mode 100644 index 0000000..b6cb4a0 --- /dev/null +++ b/test/Bindings/llvm-c/objectfile.ll @@ -0,0 +1,2 @@ +; RUN: not llvm-c-test --object-list-sections < /dev/null +; This used to cause a segfault -- cgit v1.1