llvm.org GIT mirror llvm / c0491ac
Bindings for the verifier. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42707 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 11 years ago
13 changed file(s) with 300 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
136136 9F7794870C73D51000551F9C /* LLVMBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LLVMBuilder.h; sourceTree = ""; };
137137 9F7794880C73D51000551F9C /* MemoryBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryBuffer.h; sourceTree = ""; };
138138 9F7794890C73D51000551F9C /* Streams.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Streams.h; sourceTree = ""; };
139 9F7C23E50CB81C2100498408 /* Analysis.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Analysis.h; sourceTree = ""; };
140 9F7C23E60CB81C2B00498408 /* Analysis.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Analysis.cpp; sourceTree = ""; };
141 9F7C240C0CB81ECD00498408 /* analysis_ocaml.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = analysis_ocaml.c; sourceTree = ""; };
142 9F7C240D0CB81ECD00498408 /* llvm_analysis.ml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = llvm_analysis.ml; sourceTree = ""; };
143 9F7C240E0CB81ECD00498408 /* llvm_analysis.mli */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = llvm_analysis.mli; sourceTree = ""; };
139144 9FA638D90C77B184007F12AE /* AutoUpgrade.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = AutoUpgrade.h; sourceTree = ""; };
140145 9FA638DA0C77B184007F12AE /* GlobalAlias.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = GlobalAlias.h; sourceTree = ""; };
141146 9FA638DB0C77B1AB007F12AE /* APInt.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = APInt.h; sourceTree = ""; };
165170 9FE25D900CAB166D005383FC /* APFloat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APFloat.h; sourceTree = ""; };
166171 9FE25D910CAB166D005383FC /* SparseBitVector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SparseBitVector.h; sourceTree = ""; };
167172 9FE25D920CAB169F005383FC /* RegisterCoalescer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RegisterCoalescer.h; sourceTree = ""; };
168 9FE25D930CAB16D8005383FC /* PostDominatorCalculation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PostDominatorCalculation.h; sourceTree = ""; };
169173 9FE25D940CAB16FB005383FC /* RegisterCoalescer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RegisterCoalescer.cpp; sourceTree = ""; };
170174 9FE25D950CAB1724005383FC /* APFloat.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = APFloat.cpp; sourceTree = ""; };
171175 9FE25D960CAB1759005383FC /* TargetCallingConv.td */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = TargetCallingConv.td; sourceTree = ""; };
11551159 path = MSIL;
11561160 sourceTree = "";
11571161 };
1162 9F7C240B0CB81ECD00498408 /* analysis */ = {
1163 isa = PBXGroup;
1164 children = (
1165 9F7C240C0CB81ECD00498408 /* analysis_ocaml.c */,
1166 9F7C240D0CB81ECD00498408 /* llvm_analysis.ml */,
1167 9F7C240E0CB81ECD00498408 /* llvm_analysis.mli */,
1168 );
1169 path = analysis;
1170 sourceTree = "";
1171 };
11581172 9FA638E90C77B252007F12AE /* IPO */ = {
11591173 isa = PBXGroup;
11601174 children = (
11751189 9FD3E56F0CA0116100E54D15 /* ocaml */ = {
11761190 isa = PBXGroup;
11771191 children = (
1192 9F7C240B0CB81ECD00498408 /* analysis */,
11781193 9FD3E5700CA0116100E54D15 /* bitwriter */,
11791194 9FD3E57A0CA0116100E54D15 /* llvm */,
11801195 );
12161231 CF8F1B480B64F7AB00BB4199 /* include/llvm-c */ = {
12171232 isa = PBXGroup;
12181233 children = (
1234 9F7C23E50CB81C2100498408 /* Analysis.h */,
12191235 9FD3E58D0CA0125F00E54D15 /* BitWriter.h */,
12201236 9FD3E58E0CA0125F00E54D15 /* Core.h */,
12211237 CF8F1B490B64F7AB00BB4199 /* LinkTimeOptimizer.h */,
13801396 DE66ECBD08ABEC0700323D32 /* lib/Analysis */ = {
13811397 isa = PBXGroup;
13821398 children = (
1383 9FE25D930CAB16D8005383FC /* PostDominatorCalculation.h */,
1384 9F68EB010C77AD02004AA152 /* LoopPass.cpp */,
1385 9F68EB020C77AD02004AA152 /* MemoryDependenceAnalysis.cpp */,
13861399 DE66ED1A08ABEC0800323D32 /* IPA */,
13871400 DE66ECBE08ABEC0700323D32 /* AliasAnalysis.cpp */,
13881401 DE66ECBF08ABEC0700323D32 /* AliasAnalysisCounter.cpp */,
13891402 DE66ECC008ABEC0700323D32 /* AliasAnalysisEvaluator.cpp */,
13901403 CF8F1B4D0B64F80700BB4199 /* AliasDebugger.cpp */,
13911404 DE66ECC108ABEC0700323D32 /* AliasSetTracker.cpp */,
1405 9F7C23E60CB81C2B00498408 /* Analysis.cpp */,
13921406 DE66ECC208ABEC0700323D32 /* BasicAliasAnalysis.cpp */,
13931407 DE66ECC308ABEC0700323D32 /* CFGPrinter.cpp */,
13941408 CF73C0B0098A523C00627152 /* ConstantFolding.cpp */,
13971411 DE66ED1908ABEC0800323D32 /* IntervalPartition.cpp */,
13981412 DE66ED3308ABEC0800323D32 /* LoadValueNumbering.cpp */,
13991413 DE66ED3408ABEC0800323D32 /* LoopInfo.cpp */,
1414 9F68EB010C77AD02004AA152 /* LoopPass.cpp */,
1415 9F68EB020C77AD02004AA152 /* MemoryDependenceAnalysis.cpp */,
14001416 DE66ED3608ABEC0800323D32 /* PostDominators.cpp */,
14011417 DE66ED3708ABEC0800323D32 /* ProfileInfo.cpp */,
14021418 DE66ED3808ABEC0800323D32 /* ProfileInfoLoader.cpp */,
77 ##===----------------------------------------------------------------------===##
88
99 LEVEL := ../..
10 DIRS = llvm bitwriter
10 DIRS = llvm bitwriter analysis
1111
1212 include $(LEVEL)/Makefile.common
88 #
99 # An ocaml library is a unique project type in the context of LLVM, so rules are
1010 # here rather than in Makefile.rules.
11 #
12 # Reference materials on installing ocaml libraries:
13 #
14 # https://fedoraproject.org/wiki/Packaging/OCaml
15 # http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
1116 #
1217 ##===----------------------------------------------------------------------===##
1318
187192 $(Verb) $(MKDIR) $(PROJ_libocamldir)
188193 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
189194 $(Verb) for i in $(UsedLibNames); do \
190 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
191195 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
192196 done
193197
195199 $(Echo) "Uninstalling $(DestCMA)"
196200 -$(Verb) $(RM) -f $(DestCMA)
197201 $(Verb) for i in $(UsedLibNames); do \
198 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
199202 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
200203 done
201204
0 ##===- bindings/ocaml/analysis/Makefile --------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file was developed by Gordon Henriksen and is distributed under the
5 # University of Illinois Open Source License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 #
9 # This is the makefile for the Objective Caml Llvm_analysis interface.
10 #
11 ##===----------------------------------------------------------------------===##
12
13 LEVEL := ../../..
14 LIBRARYNAME := llvm_analysis
15 DONT_BUILD_RELINKED := 1
16 UsedComponents := analysis
17 UsedOcamlInterfaces := llvm
18
19 include ../Makefile.ocaml
0 /*===-- analysis_ocaml.c - LLVM Ocaml Glue ----------------------*- C++ -*-===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file was developed by Gordon Henriksen and is distributed under the *|
5 |* University of Illinois Open Source License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file glues LLVM's ocaml interface to its C interface. These functions *|
10 |* are by and large transparent wrappers to the corresponding C functions. *|
11 |* *|
12 |* Note that these functions intentionally take liberties with the CAMLparamX *|
13 |* macros, since most of the parameters are not GC heap objects. *|
14 |* *|
15 \*===----------------------------------------------------------------------===*/
16
17 #include "llvm-c/Analysis.h"
18 #include "caml/alloc.h"
19 #include "caml/mlvalues.h"
20 #include "caml/memory.h"
21
22
23 /* Llvm.llmodule -> string option */
24 CAMLprim value llvm_verify_module(LLVMModuleRef M) {
25 CAMLparam0();
26 CAMLlocal2(String, Option);
27
28 char *Message;
29 int Result = LLVMVerifyModule(M, LLVMReturnStatusAction, &Message);
30
31 if (0 == Result) {
32 Option = Val_int(0);
33 } else {
34 Option = alloc(1, 1);
35 String = copy_string(Message);
36 Store_field(Option, 0, String);
37 }
38
39 LLVMDisposeVerifierMessage(Message);
40
41 CAMLreturn(Option);
42 }
43
44 /* Llvm.llvalue -> bool */
45 CAMLprim value llvm_verify_function(LLVMValueRef Fn) {
46 return Val_bool(LLVMVerifyFunction(Fn, LLVMReturnStatusAction) == 0);
47 }
48
49 /* Llvm.llmodule -> unit */
50 CAMLprim value llvm_assert_valid_module(LLVMModuleRef M) {
51 LLVMVerifyModule(M, LLVMAbortProcessAction, 0);
52 return Val_unit;
53 }
54
55 /* Llvm.llvalue -> unit */
56 CAMLprim value llvm_assert_valid_function(LLVMValueRef Fn) {
57 LLVMVerifyFunction(Fn, LLVMAbortProcessAction);
58 return Val_unit;
59 }
0 (*===-- llvm_analysis.ml - LLVM Ocaml Interface -----------------*- C++ -*-===*
1 *
2 * The LLVM Compiler Infrastructure
3 *
4 * This file was developed by Gordon Henriksen and is distributed under the
5 * University of Illinois Open Source License. See LICENSE.TXT for details.
6 *
7 *===----------------------------------------------------------------------===
8 *
9 * This interface provides an ocaml API for LLVM IR analyses, the classes in
10 * the Analysis library.
11 *
12 *===----------------------------------------------------------------------===*)
13
14
15 external verify_module : Llvm.llmodule -> string option = "llvm_verify_module"
16
17 external verify_function : Llvm.llvalue -> bool = "llvm_verify_function"
18
19 external assert_valid_module : Llvm.llmodule -> unit
20 = "llvm_assert_valid_module"
21
22 external assert_valid_function : Llvm.llvalue -> unit
23 = "llvm_assert_valid_function"
0 (*===-- llvm_analysis.mli - LLVM Ocaml Interface ----------------*- C++ -*-===*
1 *
2 * The LLVM Compiler Infrastructure
3 *
4 * This file was developed by Gordon Henriksen and is distributed under the
5 * University of Illinois Open Source License. See LICENSE.TXT for details.
6 *
7 *===----------------------------------------------------------------------===
8 *
9 * This interface provides an ocaml API for LLVM IR analyses, the classes in
10 * the Analysis library.
11 *
12 *===----------------------------------------------------------------------===*)
13
14
15 external verify_module : Llvm.llmodule -> string option = "llvm_verify_module"
16
17 external verify_function : Llvm.llvalue -> bool = "llvm_verify_function"
18
19 external assert_valid_module : Llvm.llmodule -> unit
20 = "llvm_assert_valid_module"
21
22 external assert_valid_function : Llvm.llvalue -> unit
23 = "llvm_assert_valid_function"
None ##===- bindings/ocaml/llvm/Makefile ------------------------*- Makefile -*-===##
0 ##===- bindings/ocaml/bitwriter/Makefile -------------------*- Makefile -*-===##
11 #
22 # The LLVM Compiler Infrastructure
33 #
4 # This file was developed by the LLVM research group and is distributed under
5 # the University of Illinois Open Source License. See LICENSE.TXT for details.
4 # This file was developed by Gordon Henriksen and is distributed under the
5 # University of Illinois Open Source License. See LICENSE.TXT for details.
66 #
77 ##===----------------------------------------------------------------------===##
88 #
9 # This is the makefile for the llvm-ml interface. Reference materials on
10 # installing ocaml libraries:
11 #
12 # https://fedoraproject.org/wiki/Packaging/OCaml
13 # http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
9 # This is the makefile for the Objective Caml Llvm_bitwriter interface.
1410 #
1511 ##===----------------------------------------------------------------------===##
1612
11 #
22 # The LLVM Compiler Infrastructure
33 #
4 # This file was developed by the LLVM research group and is distributed under
5 # the University of Illinois Open Source License. See LICENSE.TXT for details.
4 # This file was developed by Gordon Henriksen and is distributed under the
5 # University of Illinois Open Source License. See LICENSE.TXT for details.
66 #
77 ##===----------------------------------------------------------------------===##
88 #
9 # This is the makefile for the llvm-ml interface. Reference materials on
10 # installing ocaml libraries:
11 #
12 # https://fedoraproject.org/wiki/Packaging/OCaml
13 # http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
9 # This is the makefile for the Objective Caml Llvm interface.
1410 #
1511 ##===----------------------------------------------------------------------===##
1612
0 /*===-- llvm-c/Analysis.h - Analysis Library C Interface --------*- C++ -*-===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file was developed by Gordon Henriksen and is distributed under the *|
5 |* University of Illinois Open Source License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This header declares the C interface to libLLVMAnalysis.a, which *|
10 |* implements various analyses of the LLVM IR. *|
11 |* *|
12 |* Many exotic languages can interoperate with C code but have a harder time *|
13 |* with C++ due to name mangling. So in addition to C, this interface enables *|
14 |* tools written in such languages. *|
15 |* *|
16 \*===----------------------------------------------------------------------===*/
17
18 #ifndef LLVM_C_ANALYSIS_H
19 #define LLVM_C_ANALYSIS_H
20
21 #include "llvm-c/Core.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27
28 typedef enum {
29 LLVMAbortProcessAction, /* verifier will print to stderr and abort() */
30 LLVMPrintMessageAction, /* verifier will print to stderr and return 1 */
31 LLVMReturnStatusAction /* verifier will just return 1 */
32 } LLVMVerifierFailureAction;
33
34
35 /* Verifies that a module is valid, taking the specified action if not.
36 Optionally returns a human-readable description of any invalid constructs. */
37 int LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action,
38 char **OutMessage);
39
40 /* Disposes of the message allocated by the verifier, if any. */
41 void LLVMDisposeVerifierMessage(char *Message);
42
43 /* Verifies that a single function is valid, taking the specified action. Useful
44 for debugging. */
45 int LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action);
46
47
48 #ifdef __cplusplus
49 }
50 #endif
51
52 #endif
0 //===-- Analysis.cpp ------------------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Gordon Henriksen and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm-c/Analysis.h"
10 #include "llvm/Analysis/Verifier.h"
11 #include
12
13 using namespace llvm;
14
15 int LLVMVerifyModule(LLVMModuleRef M, LLVMVerifierFailureAction Action,
16 char **OutMessages) {
17 std::string Messages;
18
19 int Result = verifyModule(*unwrap(M),
20 static_cast(Action),
21 OutMessages? &Messages : 0);
22
23 if (OutMessages)
24 *OutMessages = strdup(Messages.c_str());
25
26 return Result;
27 }
28
29 void LLVMDisposeVerifierMessage(char *Message) {
30 free(Message);
31 }
32
33 int LLVMVerifyFunction(LLVMValueRef Fn, LLVMVerifierFailureAction Action) {
34 return verifyFunction(*unwrap(Fn),
35 static_cast(Action));
36 }
37
11 //
22 // The LLVM Compiler Infrastructure
33 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
4 // This file was developed by Gordon Henriksen and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
66 //
77 //===----------------------------------------------------------------------===//
88
0 (* RUN: %ocamlc llvm.cma llvm_analysis.cma %s -o %t
1 * RUN: ./%t %t.bc
2 *)
3
4 open Llvm
5 open Llvm_analysis
6
7 (* Note that this takes a moment to link, so it's best to keep the number of
8 individual tests low. *)
9
10 let test x = if not x then exit 1 else ()
11
12 let bomb msg =
13 prerr_endline msg;
14 exit 2
15
16 let _ =
17 let fty = function_type void_type [| |] in
18 let m = create_module "valid_m" in
19 let fn = define_function "valid_fn" fty m in
20 let at_entry = builder_at_end (entry_block fn) in
21 ignore (build_ret_void at_entry);
22
23
24 (* Test that valid constructs verify. *)
25 match verify_module m with
26 Some msg -> bomb "valid module failed verification!"
27 | None -> ();
28
29 if not (verify_function fn) then bomb "valid function failed verification!";
30
31
32 (* Test that invalid constructs do not verify.
33 A basic block can contain only one terminator instruction. *)
34 ignore (build_ret_void at_entry);
35
36 match verify_module m with
37 Some msg -> ()
38 | None -> bomb "invalid module passed verification!";
39
40 if verify_function fn then bomb "invalid function passed verification!";
41
42
43 dispose_module m
44
45 (* Don't bother to test assert_valid_{module,function}. *)