diff options
author | Stephen Hines <srhines@google.com> | 2015-03-23 12:10:34 -0700 |
---|---|---|
committer | Stephen Hines <srhines@google.com> | 2015-03-23 12:10:34 -0700 |
commit | ebe69fe11e48d322045d5949c83283927a0d790b (patch) | |
tree | c92f1907a6b8006628a4b01615f38264d29834ea /bindings | |
parent | b7d2e72b02a4cb8034f32f8247a2558d2434e121 (diff) | |
download | external_llvm-ebe69fe11e48d322045d5949c83283927a0d790b.zip external_llvm-ebe69fe11e48d322045d5949c83283927a0d790b.tar.gz external_llvm-ebe69fe11e48d322045d5949c83283927a0d790b.tar.bz2 |
Update aosp/master LLVM for rebase to r230699.
Change-Id: I2b5be30509658cb8266be782de0ab24f9099f9b9
Diffstat (limited to 'bindings')
47 files changed, 725 insertions, 313 deletions
diff --git a/bindings/go/llvm/DIBuilderBindings.cpp b/bindings/go/llvm/DIBuilderBindings.cpp index 94fa96f..a7d75a3 100644 --- a/bindings/go/llvm/DIBuilderBindings.cpp +++ b/bindings/go/llvm/DIBuilderBindings.cpp @@ -12,21 +12,21 @@ //===----------------------------------------------------------------------===// #include "DIBuilderBindings.h" - -#include "llvm/IR/Module.h" +#include "IRBindings.h" #include "llvm/IR/DIBuilder.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Module.h" using namespace llvm; +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef) + namespace { -template <typename T> -T unwrapDI(LLVMValueRef v) { +template <typename T> T unwrapDI(LLVMMetadataRef v) { return v ? T(unwrap<MDNode>(v)) : T(); } } -DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef) - LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) { Module *m = unwrap(mref); return wrap(new DIBuilder(*m)); @@ -39,49 +39,50 @@ void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) { void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); } -LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref, - unsigned Lang, const char *File, - const char *Dir, - const char *Producer, int Optimized, - const char *Flags, - unsigned RuntimeVersion) { +LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref, + unsigned Lang, const char *File, + const char *Dir, + const char *Producer, + int Optimized, const char *Flags, + unsigned RuntimeVersion) { DIBuilder *D = unwrap(Dref); DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized, Flags, RuntimeVersion); return wrap(CU); } -LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File, - const char *Dir) { +LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File, + const char *Dir) { DIBuilder *D = unwrap(Dref); DIFile F = D->createFile(File, Dir); return wrap(F); } -LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref, - LLVMValueRef Scope, - LLVMValueRef File, unsigned Line, - unsigned Column) { +LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref, + LLVMMetadataRef Scope, + LLVMMetadataRef File, + unsigned Line, + unsigned Column) { DIBuilder *D = unwrap(Dref); DILexicalBlock LB = D->createLexicalBlock( unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Line, Column); return wrap(LB); } -LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref, - LLVMValueRef Scope, - LLVMValueRef File, - unsigned Discriminator) { +LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref, + LLVMMetadataRef Scope, + LLVMMetadataRef File, + unsigned Discriminator) { DIBuilder *D = unwrap(Dref); DILexicalBlockFile LBF = D->createLexicalBlockFile( unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Discriminator); return wrap(LBF); } -LLVMValueRef LLVMDIBuilderCreateFunction( - LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name, - const char *LinkageName, LLVMValueRef File, unsigned Line, - LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition, +LLVMMetadataRef LLVMDIBuilderCreateFunction( + LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name, + const char *LinkageName, LLVMMetadataRef File, unsigned Line, + LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition, unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Func) { DIBuilder *D = unwrap(Dref); DISubprogram SP = D->createFunction( @@ -91,10 +92,10 @@ LLVMValueRef LLVMDIBuilderCreateFunction( return wrap(SP); } -LLVMValueRef LLVMDIBuilderCreateLocalVariable( - LLVMDIBuilderRef Dref, unsigned Tag, LLVMValueRef Scope, const char *Name, - LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve, - unsigned Flags, unsigned ArgNo) { +LLVMMetadataRef LLVMDIBuilderCreateLocalVariable( + LLVMDIBuilderRef Dref, unsigned Tag, LLVMMetadataRef Scope, + const char *Name, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, + int AlwaysPreserve, unsigned Flags, unsigned ArgNo) { DIBuilder *D = unwrap(Dref); DIVariable V = D->createLocalVariable( Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, @@ -102,39 +103,41 @@ LLVMValueRef LLVMDIBuilderCreateLocalVariable( return wrap(V); } -LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref, - const char *Name, uint64_t SizeInBits, - uint64_t AlignInBits, - unsigned Encoding) { +LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref, + const char *Name, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Encoding) { DIBuilder *D = unwrap(Dref); DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding); return wrap(T); } -LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref, - LLVMValueRef PointeeType, - uint64_t SizeInBits, - uint64_t AlignInBits, - const char *Name) { +LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref, + LLVMMetadataRef PointeeType, + uint64_t SizeInBits, + uint64_t AlignInBits, + const char *Name) { DIBuilder *D = unwrap(Dref); DIDerivedType T = D->createPointerType(unwrapDI<DIType>(PointeeType), SizeInBits, AlignInBits, Name); return wrap(T); } -LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, - LLVMValueRef File, - LLVMValueRef ParameterTypes) { +LLVMMetadataRef +LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File, + LLVMMetadataRef ParameterTypes) { DIBuilder *D = unwrap(Dref); DICompositeType CT = D->createSubroutineType( unwrapDI<DIFile>(File), unwrapDI<DITypeArray>(ParameterTypes)); return wrap(CT); } -LLVMValueRef LLVMDIBuilderCreateStructType( - LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name, - LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits, - unsigned Flags, LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes) { +LLVMMetadataRef LLVMDIBuilderCreateStructType( + LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits, + uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom, + LLVMMetadataRef ElementTypes) { DIBuilder *D = unwrap(Dref); DICompositeType CT = D->createStructType( unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, @@ -143,10 +146,12 @@ LLVMValueRef LLVMDIBuilderCreateStructType( return wrap(CT); } -LLVMValueRef LLVMDIBuilderCreateMemberType( - LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name, - LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits, - uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty) { +LLVMMetadataRef +LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, + const char *Name, LLVMMetadataRef File, + unsigned Line, uint64_t SizeInBits, + uint64_t AlignInBits, uint64_t OffsetInBits, + unsigned Flags, LLVMMetadataRef Ty) { DIBuilder *D = unwrap(Dref); DIDerivedType DT = D->createMemberType( unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line, @@ -154,11 +159,11 @@ LLVMValueRef LLVMDIBuilderCreateMemberType( return wrap(DT); } -LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref, - uint64_t SizeInBits, - uint64_t AlignInBits, - LLVMValueRef ElementType, - LLVMValueRef Subscripts) { +LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref, + uint64_t SizeInBits, + uint64_t AlignInBits, + LLVMMetadataRef ElementType, + LLVMMetadataRef Subscripts) { DIBuilder *D = unwrap(Dref); DICompositeType CT = D->createArrayType(SizeInBits, AlignInBits, unwrapDI<DIType>(ElementType), @@ -166,9 +171,10 @@ LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref, return wrap(CT); } -LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty, - const char *Name, LLVMValueRef File, - unsigned Line, LLVMValueRef Context) { +LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, + LLVMMetadataRef Ty, const char *Name, + LLVMMetadataRef File, unsigned Line, + LLVMMetadataRef Context) { DIBuilder *D = unwrap(Dref); DIDerivedType DT = D->createTypedef(unwrapDI<DIType>(Ty), Name, unwrapDI<DIFile>(File), Line, @@ -176,34 +182,35 @@ LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty, return wrap(DT); } -LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo, - int64_t Count) { +LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, + int64_t Lo, int64_t Count) { DIBuilder *D = unwrap(Dref); DISubrange S = D->getOrCreateSubrange(Lo, Count); return wrap(S); } -LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref, - LLVMValueRef *Data, size_t Length) { +LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref, + LLVMMetadataRef *Data, + size_t Length) { DIBuilder *D = unwrap(Dref); - Value **DataValue = unwrap(Data); - ArrayRef<Value *> Elements(DataValue, Length); + Metadata **DataValue = unwrap(Data); + ArrayRef<Metadata *> Elements(DataValue, Length); DIArray A = D->getOrCreateArray(Elements); return wrap(A); } -LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref, - LLVMValueRef *Data, - size_t Length) { +LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref, + LLVMMetadataRef *Data, + size_t Length) { DIBuilder *D = unwrap(Dref); - Value **DataValue = unwrap(Data); - ArrayRef<Value *> Elements(DataValue, Length); + Metadata **DataValue = unwrap(Data); + ArrayRef<Metadata *> Elements(DataValue, Length); DITypeArray A = D->getOrCreateTypeArray(Elements); return wrap(A); } -LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr, - size_t Length) { +LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, + int64_t *Addr, size_t Length) { DIBuilder *D = unwrap(Dref); DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length)); return wrap(Expr); @@ -211,8 +218,8 @@ LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr, LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref, LLVMValueRef Storage, - LLVMValueRef VarInfo, - LLVMValueRef Expr, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, LLVMBasicBlockRef Block) { DIBuilder *D = unwrap(Dref); Instruction *Instr = @@ -220,3 +227,15 @@ LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref, unwrapDI<DIExpression>(Expr), unwrap(Block)); return wrap(Instr); } + +LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref, + LLVMValueRef Val, uint64_t Offset, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, + LLVMBasicBlockRef Block) { + DIBuilder *D = unwrap(Dref); + Instruction *Instr = D->insertDbgValueIntrinsic( + unwrap(Val), Offset, unwrapDI<DIVariable>(VarInfo), + unwrapDI<DIExpression>(Expr), unwrap(Block)); + return wrap(Instr); +} diff --git a/bindings/go/llvm/DIBuilderBindings.h b/bindings/go/llvm/DIBuilderBindings.h index e6fe02a..e268b3c 100644 --- a/bindings/go/llvm/DIBuilderBindings.h +++ b/bindings/go/llvm/DIBuilderBindings.h @@ -14,6 +14,7 @@ #ifndef LLVM_BINDINGS_GO_LLVM_DIBUILDERBINDINGS_H #define LLVM_BINDINGS_GO_LLVM_DIBUILDERBINDINGS_H +#include "IRBindings.h" #include "llvm-c/Core.h" #ifdef __cplusplus @@ -31,93 +32,104 @@ LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef m); void LLVMDIBuilderDestroy(LLVMDIBuilderRef d); void LLVMDIBuilderFinalize(LLVMDIBuilderRef d); -LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef D, - unsigned Language, const char *File, - const char *Dir, - const char *Producer, int Optimized, - const char *Flags, - unsigned RuntimeVersion); - -LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef D, const char *File, - const char *Dir); - -LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef D, - LLVMValueRef Scope, - LLVMValueRef File, unsigned Line, - unsigned Column); - -LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef D, - LLVMValueRef Scope, - LLVMValueRef File, - unsigned Discriminator); - -LLVMValueRef LLVMDIBuilderCreateFunction( - LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, - const char *LinkageName, LLVMValueRef File, unsigned Line, - LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition, +LLVMMetadataRef +LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef D, unsigned Language, + const char *File, const char *Dir, + const char *Producer, int Optimized, + const char *Flags, unsigned RuntimeVersion); + +LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef D, const char *File, + const char *Dir); + +LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef D, + LLVMMetadataRef Scope, + LLVMMetadataRef File, + unsigned Line, unsigned Column); + +LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef D, + LLVMMetadataRef Scope, + LLVMMetadataRef File, + unsigned Discriminator); + +LLVMMetadataRef LLVMDIBuilderCreateFunction( + LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name, + const char *LinkageName, LLVMMetadataRef File, unsigned Line, + LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition, unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Function); -LLVMValueRef LLVMDIBuilderCreateLocalVariable( - LLVMDIBuilderRef D, unsigned Tag, LLVMValueRef Scope, const char *Name, - LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve, +LLVMMetadataRef LLVMDIBuilderCreateLocalVariable( + LLVMDIBuilderRef D, unsigned Tag, LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve, unsigned Flags, unsigned ArgNo); -LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef D, const char *Name, - uint64_t SizeInBits, - uint64_t AlignInBits, - unsigned Encoding); - -LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef D, - LLVMValueRef PointeeType, - uint64_t SizeInBits, - uint64_t AlignInBits, - const char *Name); - -LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef D, - LLVMValueRef File, - LLVMValueRef ParameterTypes); - -LLVMValueRef LLVMDIBuilderCreateStructType( - LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, LLVMValueRef File, - unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags, - LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes); - -LLVMValueRef LLVMDIBuilderCreateMemberType( - LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, LLVMValueRef File, - unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits, - uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty); - -LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef D, - uint64_t SizeInBits, - uint64_t AlignInBits, - LLVMValueRef ElementType, - LLVMValueRef Subscripts); - -LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef D, LLVMValueRef Ty, - const char *Name, LLVMValueRef File, - unsigned Line, LLVMValueRef Context); - -LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef D, int64_t Lo, - int64_t Count); - -LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef D, - LLVMValueRef *Data, size_t Length); - -LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef D, - LLVMValueRef *Data, - size_t Length); - -LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr, - size_t Length); +LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef D, + const char *Name, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Encoding); + +LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef D, + LLVMMetadataRef PointeeType, + uint64_t SizeInBits, + uint64_t AlignInBits, + const char *Name); + +LLVMMetadataRef +LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef D, LLVMMetadataRef File, + LLVMMetadataRef ParameterTypes); + +LLVMMetadataRef LLVMDIBuilderCreateStructType( + LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits, + uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom, + LLVMMetadataRef ElementTypes); + +LLVMMetadataRef +LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef D, LLVMMetadataRef Scope, + const char *Name, LLVMMetadataRef File, + unsigned Line, uint64_t SizeInBits, + uint64_t AlignInBits, uint64_t OffsetInBits, + unsigned Flags, LLVMMetadataRef Ty); + +LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef D, + uint64_t SizeInBits, + uint64_t AlignInBits, + LLVMMetadataRef ElementType, + LLVMMetadataRef Subscripts); + +LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef D, + LLVMMetadataRef Ty, const char *Name, + LLVMMetadataRef File, unsigned Line, + LLVMMetadataRef Context); + +LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef D, int64_t Lo, + int64_t Count); + +LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef D, + LLVMMetadataRef *Data, + size_t Length); + +LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef D, + LLVMMetadataRef *Data, + size_t Length); + +LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, + int64_t *Addr, size_t Length); LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef D, LLVMValueRef Storage, - LLVMValueRef VarInfo, - LLVMValueRef Expr, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, LLVMBasicBlockRef Block); +LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef D, LLVMValueRef Val, + uint64_t Offset, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, + LLVMBasicBlockRef Block); + #ifdef __cplusplus -} // extern "C" +} // extern "C" #endif #endif diff --git a/bindings/go/llvm/IRBindings.cpp b/bindings/go/llvm/IRBindings.cpp index 67a54a2..fb451ef 100644 --- a/bindings/go/llvm/IRBindings.cpp +++ b/bindings/go/llvm/IRBindings.cpp @@ -12,9 +12,12 @@ //===----------------------------------------------------------------------===// #include "IRBindings.h" - #include "llvm/IR/Attributes.h" +#include "llvm/IR/DebugLoc.h" #include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" using namespace llvm; @@ -45,3 +48,54 @@ void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA) { AttributeSet::FunctionIndex, B)); Func->setAttributes(PALnew); } + +LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) { + return wrap(ConstantAsMetadata::get(unwrap<Constant>(C))); +} + +LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) { + return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen))); +} + +LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs, + unsigned Count) { + return wrap( + MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count))); +} + +LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs, + unsigned Count) { + return wrap(MDTuple::getTemporary(*unwrap(C), + ArrayRef<Metadata *>(unwrap(MDs), Count)) + .release()); +} + +void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name, + LLVMMetadataRef Val) { + NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name); + if (!N) + return; + if (!Val) + return; + N->addOperand(unwrap<MDNode>(Val)); +} + +void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) { + MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr; + unwrap<Instruction>(Inst)->setMetadata(KindID, N); +} + +void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New) { + auto *Node = unwrap<MDTuple>(MD); + assert(Node->isTemporary() && "Expected temporary node"); + Node->replaceAllUsesWith(unwrap<MDNode>(New)); + MDNode::deleteTemporary(Node); +} + +void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line, + unsigned Col, LLVMMetadataRef Scope, + LLVMMetadataRef InlinedAt) { + unwrap(Bref)->SetCurrentDebugLocation( + DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr, + InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr)); +} diff --git a/bindings/go/llvm/IRBindings.h b/bindings/go/llvm/IRBindings.h index cc63e4e..a53e178 100644 --- a/bindings/go/llvm/IRBindings.h +++ b/bindings/go/llvm/IRBindings.h @@ -15,12 +15,19 @@ #define LLVM_BINDINGS_GO_LLVM_IRBINDINGS_H #include "llvm-c/Core.h" +#ifdef __cplusplus +#include "llvm/IR/Metadata.h" +#include "llvm/Support/CBindingWrapping.h" +#endif + #include <stdint.h> #ifdef __cplusplus extern "C" { #endif +typedef struct LLVMOpaqueMetadata *LLVMMetadataRef; + // These functions duplicate the LLVM*FunctionAttr functions in the stable C // API. We cannot use the existing functions because they take 32-bit attribute // values, and the Go bindings expose all of the LLVM attributes, some of which @@ -30,8 +37,37 @@ void LLVMAddFunctionAttr2(LLVMValueRef Fn, uint64_t PA); uint64_t LLVMGetFunctionAttr2(LLVMValueRef Fn); void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA); +LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef Val); + +LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen); +LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs, + unsigned Count); +LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs, + unsigned Count); + +void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name, + LLVMMetadataRef Val); +void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD); + +void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New); + +void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line, + unsigned Col, LLVMMetadataRef Scope, + LLVMMetadataRef InlinedAt); + #ifdef __cplusplus } + +namespace llvm { + +DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef) + +inline Metadata **unwrap(LLVMMetadataRef *Vals) { + return reinterpret_cast<Metadata**>(Vals); +} + +} + #endif #endif diff --git a/bindings/go/llvm/InstrumentationBindings.cpp b/bindings/go/llvm/InstrumentationBindings.cpp index b604abb..8b7bafa 100644 --- a/bindings/go/llvm/InstrumentationBindings.cpp +++ b/bindings/go/llvm/InstrumentationBindings.cpp @@ -12,10 +12,9 @@ //===----------------------------------------------------------------------===// #include "InstrumentationBindings.h" - #include "llvm-c/Core.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" -#include "llvm/PassManager.h" #include "llvm/Transforms/Instrumentation.h" using namespace llvm; @@ -37,6 +36,11 @@ void LLVMAddMemorySanitizerPass(LLVMPassManagerRef PM) { } void LLVMAddDataFlowSanitizerPass(LLVMPassManagerRef PM, - const char *ABIListFile) { - unwrap(PM)->add(createDataFlowSanitizerPass(ABIListFile)); + int ABIListFilesNum, + const char **ABIListFiles) { + std::vector<std::string> ABIListFilesVec; + for (int i = 0; i != ABIListFilesNum; ++i) { + ABIListFilesVec.push_back(ABIListFiles[i]); + } + unwrap(PM)->add(createDataFlowSanitizerPass(ABIListFilesVec)); } diff --git a/bindings/go/llvm/InstrumentationBindings.h b/bindings/go/llvm/InstrumentationBindings.h index e8dbd59..97af2d5 100644 --- a/bindings/go/llvm/InstrumentationBindings.h +++ b/bindings/go/llvm/InstrumentationBindings.h @@ -28,8 +28,8 @@ void LLVMAddAddressSanitizerFunctionPass(LLVMPassManagerRef PM); void LLVMAddAddressSanitizerModulePass(LLVMPassManagerRef PM); void LLVMAddThreadSanitizerPass(LLVMPassManagerRef PM); void LLVMAddMemorySanitizerPass(LLVMPassManagerRef PM); -void LLVMAddDataFlowSanitizerPass(LLVMPassManagerRef PM, - const char *ABIListFile); +void LLVMAddDataFlowSanitizerPass(LLVMPassManagerRef PM, int ABIListFilesNum, + const char **ABIListFiles); #ifdef __cplusplus } diff --git a/bindings/go/llvm/SupportBindings.cpp b/bindings/go/llvm/SupportBindings.cpp index df5f865..5e251b7 100644 --- a/bindings/go/llvm/SupportBindings.cpp +++ b/bindings/go/llvm/SupportBindings.cpp @@ -12,7 +12,6 @@ //===----------------------------------------------------------------------===// #include "SupportBindings.h" - #include "llvm/Support/DynamicLibrary.h" #include <stdlib.h> #include <string.h> diff --git a/bindings/go/llvm/dibuilder.go b/bindings/go/llvm/dibuilder.go index 1d07e98..3b1a1a6 100644 --- a/bindings/go/llvm/dibuilder.go +++ b/bindings/go/llvm/dibuilder.go @@ -121,7 +121,7 @@ type DICompileUnit struct { } // CreateCompileUnit creates compile unit debug metadata. -func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Value { +func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Metadata { file := C.CString(cu.File) defer C.free(unsafe.Pointer(file)) dir := C.CString(cu.Dir) @@ -139,28 +139,28 @@ func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Value { flags, C.unsigned(cu.RuntimeVersion), ) - return Value{C: result} + return Metadata{C: result} } // CreateCompileUnit creates file debug metadata. -func (d *DIBuilder) CreateFile(filename, dir string) Value { +func (d *DIBuilder) CreateFile(filename, dir string) Metadata { cfilename := C.CString(filename) defer C.free(unsafe.Pointer(cfilename)) cdir := C.CString(dir) defer C.free(unsafe.Pointer(cdir)) result := C.LLVMDIBuilderCreateFile(d.ref, cfilename, cdir) - return Value{C: result} + return Metadata{C: result} } // DILexicalBlock holds the values for creating lexical block debug metadata. type DILexicalBlock struct { - File Value + File Metadata Line int Column int } // CreateCompileUnit creates lexical block debug metadata. -func (d *DIBuilder) CreateLexicalBlock(diScope Value, b DILexicalBlock) Value { +func (d *DIBuilder) CreateLexicalBlock(diScope Metadata, b DILexicalBlock) Metadata { result := C.LLVMDIBuilderCreateLexicalBlock( d.ref, diScope.C, @@ -168,22 +168,22 @@ func (d *DIBuilder) CreateLexicalBlock(diScope Value, b DILexicalBlock) Value { C.unsigned(b.Line), C.unsigned(b.Column), ) - return Value{C: result} + return Metadata{C: result} } -func (d *DIBuilder) CreateLexicalBlockFile(diScope Value, diFile Value, discriminator int) Value { +func (d *DIBuilder) CreateLexicalBlockFile(diScope Metadata, diFile Metadata, discriminator int) Metadata { result := C.LLVMDIBuilderCreateLexicalBlockFile(d.ref, diScope.C, diFile.C, C.unsigned(discriminator)) - return Value{C: result} + return Metadata{C: result} } // DIFunction holds the values for creating function debug metadata. type DIFunction struct { Name string LinkageName string - File Value + File Metadata Line int - Type Value + Type Metadata LocalToUnit bool IsDefinition bool ScopeLine int @@ -193,7 +193,7 @@ type DIFunction struct { } // CreateCompileUnit creates function debug metadata. -func (d *DIBuilder) CreateFunction(diScope Value, f DIFunction) Value { +func (d *DIBuilder) CreateFunction(diScope Metadata, f DIFunction) Metadata { name := C.CString(f.Name) defer C.free(unsafe.Pointer(name)) linkageName := C.CString(f.LinkageName) @@ -213,16 +213,16 @@ func (d *DIBuilder) CreateFunction(diScope Value, f DIFunction) Value { boolToCInt(f.Optimized), f.Function.C, ) - return Value{C: result} + return Metadata{C: result} } // DILocalVariable holds the values for creating local variable debug metadata. type DILocalVariable struct { Tag dwarf.Tag Name string - File Value + File Metadata Line int - Type Value + Type Metadata AlwaysPreserve bool Flags int @@ -232,7 +232,7 @@ type DILocalVariable struct { } // CreateLocalVariable creates local variable debug metadata. -func (d *DIBuilder) CreateLocalVariable(scope Value, v DILocalVariable) Value { +func (d *DIBuilder) CreateLocalVariable(scope Metadata, v DILocalVariable) Metadata { name := C.CString(v.Name) defer C.free(unsafe.Pointer(name)) result := C.LLVMDIBuilderCreateLocalVariable( @@ -247,7 +247,7 @@ func (d *DIBuilder) CreateLocalVariable(scope Value, v DILocalVariable) Value { C.unsigned(v.Flags), C.unsigned(v.ArgNo), ) - return Value{C: result} + return Metadata{C: result} } // DIBasicType holds the values for creating basic type debug metadata. @@ -259,7 +259,7 @@ type DIBasicType struct { } // CreateBasicType creates basic type debug metadata. -func (d *DIBuilder) CreateBasicType(t DIBasicType) Value { +func (d *DIBuilder) CreateBasicType(t DIBasicType) Metadata { name := C.CString(t.Name) defer C.free(unsafe.Pointer(name)) result := C.LLVMDIBuilderCreateBasicType( @@ -269,19 +269,19 @@ func (d *DIBuilder) CreateBasicType(t DIBasicType) Value { C.uint64_t(t.AlignInBits), C.unsigned(t.Encoding), ) - return Value{C: result} + return Metadata{C: result} } // DIPointerType holds the values for creating pointer type debug metadata. type DIPointerType struct { - Pointee Value + Pointee Metadata SizeInBits uint64 AlignInBits uint64 // optional Name string // optional } // CreateBasicType creates basic type debug metadata. -func (d *DIBuilder) CreatePointerType(t DIPointerType) Value { +func (d *DIBuilder) CreatePointerType(t DIPointerType) Metadata { name := C.CString(t.Name) defer C.free(unsafe.Pointer(name)) result := C.LLVMDIBuilderCreatePointerType( @@ -291,40 +291,40 @@ func (d *DIBuilder) CreatePointerType(t DIPointerType) Value { C.uint64_t(t.AlignInBits), name, ) - return Value{C: result} + return Metadata{C: result} } // DISubroutineType holds the values for creating subroutine type debug metadata. type DISubroutineType struct { // File is the file in which the subroutine type is defined. - File Value + File Metadata // Parameters contains the subroutine parameter types, // including the return type at the 0th index. - Parameters []Value + Parameters []Metadata } // CreateSubroutineType creates subroutine type debug metadata. -func (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Value { +func (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Metadata { params := d.getOrCreateTypeArray(t.Parameters) result := C.LLVMDIBuilderCreateSubroutineType(d.ref, t.File.C, params.C) - return Value{C: result} + return Metadata{C: result} } // DIStructType holds the values for creating struct type debug metadata. type DIStructType struct { Name string - File Value + File Metadata Line int SizeInBits uint64 AlignInBits uint64 Flags int - DerivedFrom Value - Elements []Value + DerivedFrom Metadata + Elements []Metadata } // CreateStructType creates struct type debug metadata. -func (d *DIBuilder) CreateStructType(scope Value, t DIStructType) Value { +func (d *DIBuilder) CreateStructType(scope Metadata, t DIStructType) Metadata { elements := d.getOrCreateArray(t.Elements) name := C.CString(t.Name) defer C.free(unsafe.Pointer(name)) @@ -340,23 +340,23 @@ func (d *DIBuilder) CreateStructType(scope Value, t DIStructType) Value { t.DerivedFrom.C, elements.C, ) - return Value{C: result} + return Metadata{C: result} } // DIMemberType holds the values for creating member type debug metadata. type DIMemberType struct { Name string - File Value + File Metadata Line int SizeInBits uint64 AlignInBits uint64 OffsetInBits uint64 Flags int - Type Value + Type Metadata } // CreateMemberType creates struct type debug metadata. -func (d *DIBuilder) CreateMemberType(scope Value, t DIMemberType) Value { +func (d *DIBuilder) CreateMemberType(scope Metadata, t DIMemberType) Metadata { name := C.CString(t.Name) defer C.free(unsafe.Pointer(name)) result := C.LLVMDIBuilderCreateMemberType( @@ -371,7 +371,7 @@ func (d *DIBuilder) CreateMemberType(scope Value, t DIMemberType) Value { C.unsigned(t.Flags), t.Type.C, ) - return Value{C: result} + return Metadata{C: result} } // DISubrange describes an integer value range. @@ -384,13 +384,13 @@ type DISubrange struct { type DIArrayType struct { SizeInBits uint64 AlignInBits uint64 - ElementType Value + ElementType Metadata Subscripts []DISubrange } // CreateArrayType creates struct type debug metadata. -func (d *DIBuilder) CreateArrayType(t DIArrayType) Value { - subscriptsSlice := make([]Value, len(t.Subscripts)) +func (d *DIBuilder) CreateArrayType(t DIArrayType) Metadata { + subscriptsSlice := make([]Metadata, len(t.Subscripts)) for i, s := range t.Subscripts { subscriptsSlice[i] = d.getOrCreateSubrange(s.Lo, s.Count) } @@ -402,20 +402,20 @@ func (d *DIBuilder) CreateArrayType(t DIArrayType) Value { t.ElementType.C, subscripts.C, ) - return Value{C: result} + return Metadata{C: result} } // DITypedef holds the values for creating typedef type debug metadata. type DITypedef struct { - Type Value + Type Metadata Name string - File Value + File Metadata Line int - Context Value + Context Metadata } // CreateTypedef creates typedef type debug metadata. -func (d *DIBuilder) CreateTypedef(t DITypedef) Value { +func (d *DIBuilder) CreateTypedef(t DITypedef) Metadata { name := C.CString(t.Name) defer C.free(unsafe.Pointer(name)) result := C.LLVMDIBuilderCreateTypedef( @@ -426,64 +426,63 @@ func (d *DIBuilder) CreateTypedef(t DITypedef) Value { C.unsigned(t.Line), t.Context.C, ) - return Value{C: result} + return Metadata{C: result} } // getOrCreateSubrange gets a metadata node for the specified subrange, // creating if required. -func (d *DIBuilder) getOrCreateSubrange(lo, count int64) Value { +func (d *DIBuilder) getOrCreateSubrange(lo, count int64) Metadata { result := C.LLVMDIBuilderGetOrCreateSubrange(d.ref, C.int64_t(lo), C.int64_t(count)) - return Value{C: result} + return Metadata{C: result} } // getOrCreateArray gets a metadata node containing the specified values, // creating if required. -func (d *DIBuilder) getOrCreateArray(values []Value) Value { +func (d *DIBuilder) getOrCreateArray(values []Metadata) Metadata { if len(values) == 0 { - return Value{} - } - var data *C.LLVMValueRef - length := len(values) - if length > 0 { - data = &values[0].C + return Metadata{} } + data, length := llvmMetadataRefs(values) result := C.LLVMDIBuilderGetOrCreateArray(d.ref, data, C.size_t(length)) - return Value{C: result} + return Metadata{C: result} } // getOrCreateTypeArray gets a metadata node for a type array containing the // specified values, creating if required. -func (d *DIBuilder) getOrCreateTypeArray(values []Value) Value { +func (d *DIBuilder) getOrCreateTypeArray(values []Metadata) Metadata { if len(values) == 0 { - return Value{} - } - var data *C.LLVMValueRef - length := len(values) - if length > 0 { - data = &values[0].C + return Metadata{} } + data, length := llvmMetadataRefs(values) result := C.LLVMDIBuilderGetOrCreateTypeArray(d.ref, data, C.size_t(length)) - return Value{C: result} + return Metadata{C: result} } // CreateExpression creates a new descriptor for the specified // variable which has a complex address expression for its address. -func (d *DIBuilder) CreateExpression(addr []int64) Value { +func (d *DIBuilder) CreateExpression(addr []int64) Metadata { var data *C.int64_t if len(addr) > 0 { data = (*C.int64_t)(unsafe.Pointer(&addr[0])) } result := C.LLVMDIBuilderCreateExpression(d.ref, data, C.size_t(len(addr))) - return Value{C: result} + return Metadata{C: result} } // InsertDeclareAtEnd inserts a call to llvm.dbg.declare at the end of the // specified basic block for the given value and associated debug metadata. -func (d *DIBuilder) InsertDeclareAtEnd(v, diVarInfo, expr Value, bb BasicBlock) Value { +func (d *DIBuilder) InsertDeclareAtEnd(v Value, diVarInfo, expr Metadata, bb BasicBlock) Value { result := C.LLVMDIBuilderInsertDeclareAtEnd(d.ref, v.C, diVarInfo.C, expr.C, bb.C) return Value{C: result} } +// InsertValueAtEnd inserts a call to llvm.dbg.value at the end of the +// specified basic block for the given value and associated debug metadata. +func (d *DIBuilder) InsertValueAtEnd(v Value, diVarInfo, expr Metadata, offset uint64, bb BasicBlock) Value { + result := C.LLVMDIBuilderInsertValueAtEnd(d.ref, v.C, C.uint64_t(offset), diVarInfo.C, expr.C, bb.C) + return Value{C: result} +} + func boolToCInt(v bool) C.int { if v { return 1 diff --git a/bindings/go/llvm/executionengine.go b/bindings/go/llvm/executionengine.go index 26b7524..94d4e83 100644 --- a/bindings/go/llvm/executionengine.go +++ b/bindings/go/llvm/executionengine.go @@ -30,11 +30,25 @@ type GenericValue struct { type ExecutionEngine struct { C C.LLVMExecutionEngineRef } + type MCJITCompilerOptions struct { - OptLevel uint - CodeModel CodeModel - NoFramePointerElim bool - EnableFastISel bool + C C.struct_LLVMMCJITCompilerOptions +} + +func (options *MCJITCompilerOptions) SetMCJITOptimizationLevel(level uint) { + options.C.OptLevel = C.uint(level) +} + +func (options *MCJITCompilerOptions) SetMCJITNoFramePointerElim(nfp bool) { + options.C.NoFramePointerElim = boolToLLVMBool(nfp) +} + +func (options *MCJITCompilerOptions) SetMCJITEnableFastISel(fastisel bool) { + options.C.EnableFastISel = boolToLLVMBool(fastisel) +} + +func (options *MCJITCompilerOptions) SetMCJITCodeModel(CodeModel CodeModel) { + options.C.CodeModel = C.LLVMCodeModel(CodeModel) } // helpers @@ -96,15 +110,15 @@ func NewInterpreter(m Module) (ee ExecutionEngine, err error) { return } +func NewMCJITCompilerOptions() MCJITCompilerOptions { + var options C.struct_LLVMMCJITCompilerOptions + C.LLVMInitializeMCJITCompilerOptions(&options, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{}))) + return MCJITCompilerOptions{options} +} + func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) { var cmsg *C.char - copts := C.struct_LLVMMCJITCompilerOptions{ - OptLevel: C.unsigned(options.OptLevel), - CodeModel: C.LLVMCodeModel(options.CodeModel), - NoFramePointerElim: boolToLLVMBool(options.NoFramePointerElim), - EnableFastISel: boolToLLVMBool(options.EnableFastISel), - } - fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &copts, C.size_t(unsafe.Sizeof(copts)), &cmsg) + fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &options.C, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})), &cmsg) if fail != 0 { ee.C = nil err = errors.New(C.GoString(cmsg)) diff --git a/bindings/go/llvm/executionengine_test.go b/bindings/go/llvm/executionengine_test.go index 1a3fd45..2b6a3ca 100644 --- a/bindings/go/llvm/executionengine_test.go +++ b/bindings/go/llvm/executionengine_test.go @@ -66,7 +66,12 @@ func TestFactorial(t *testing.T) { return } - engine, err := NewMCJITCompiler(mod, MCJITCompilerOptions{OptLevel: 2}) + options := NewMCJITCompilerOptions() + options.SetMCJITOptimizationLevel(2) + options.SetMCJITEnableFastISel(true) + options.SetMCJITNoFramePointerElim(true) + options.SetMCJITCodeModel(CodeModelJITDefault) + engine, err := NewMCJITCompiler(mod, options) if err != nil { t.Errorf("Error creating JIT: %s", err) return diff --git a/bindings/go/llvm/ir.go b/bindings/go/llvm/ir.go index 7834f5c..e5916a1 100644 --- a/bindings/go/llvm/ir.go +++ b/bindings/go/llvm/ir.go @@ -55,6 +55,9 @@ type ( Use struct { C C.LLVMUseRef } + Metadata struct { + C C.LLVMMetadataRef + } Attribute uint64 Opcode C.LLVMOpcode TypeKind C.LLVMTypeKind @@ -80,6 +83,9 @@ func (c Use) IsNil() bool { return c.C == nil } // helpers func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) } func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) } +func llvmMetadataRefPtr(t *Metadata) *C.LLVMMetadataRef { + return (*C.LLVMMetadataRef)(unsafe.Pointer(t)) +} func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef { return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t)) } @@ -99,6 +105,15 @@ func llvmValueRefs(values []Value) (*C.LLVMValueRef, C.unsigned) { return pt, ptlen } +func llvmMetadataRefs(mds []Metadata) (*C.LLVMMetadataRef, C.unsigned) { + var pt *C.LLVMMetadataRef + ptlen := C.unsigned(len(mds)) + if ptlen > 0 { + pt = llvmMetadataRefPtr(&mds[0]) + } + return pt, ptlen +} + //------------------------------------------------------------------------- // llvm.Attribute //------------------------------------------------------------------------- @@ -421,10 +436,10 @@ func (m Module) SetInlineAsm(asm string) { C.LLVMSetModuleInlineAsm(m.C, casm) } -func (m Module) AddNamedMetadataOperand(name string, operand Value) { +func (m Module) AddNamedMetadataOperand(name string, operand Metadata) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) - C.LLVMAddNamedMetadataOperand(m.C, cname, operand.C) + C.LLVMAddNamedMetadataOperand2(m.C, cname, operand.C) } func (m Module) Context() (c Context) { @@ -628,8 +643,8 @@ func (v Value) Metadata(kind int) (rv Value) { rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind)) return } -func (v Value) SetMetadata(kind int, node Value) { - C.LLVMSetMetadata(v.C, C.unsigned(kind), node.C) +func (v Value) SetMetadata(kind int, node Metadata) { + C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C) } // Conversion functions. @@ -723,26 +738,24 @@ func (v Value) IsUndef() bool { return C.LLVMIsUndef(v.C) != 0 } func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return } // Operations on metadata -func (c Context) MDString(str string) (v Value) { +func (c Context) MDString(str string) (md Metadata) { cstr := C.CString(str) defer C.free(unsafe.Pointer(cstr)) - v.C = C.LLVMMDStringInContext(c.C, cstr, C.unsigned(len(str))) + md.C = C.LLVMMDString2(c.C, cstr, C.unsigned(len(str))) return } -func MDString(str string) (v Value) { - cstr := C.CString(str) - defer C.free(unsafe.Pointer(cstr)) - v.C = C.LLVMMDString(cstr, C.unsigned(len(str))) +func (c Context) MDNode(mds []Metadata) (md Metadata) { + ptr, nvals := llvmMetadataRefs(mds) + md.C = C.LLVMMDNode2(c.C, ptr, nvals) return } -func (c Context) MDNode(vals []Value) (v Value) { - ptr, nvals := llvmValueRefs(vals) - v.C = C.LLVMMDNodeInContext(c.C, ptr, nvals) +func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) { + ptr, nvals := llvmMetadataRefs(mds) + md.C = C.LLVMTemporaryMDNode(c.C, ptr, nvals) return } -func MDNode(vals []Value) (v Value) { - ptr, nvals := llvmValueRefs(vals) - v.C = C.LLVMMDNode(ptr, nvals) +func (v Value) ConstantAsMetadata() (md Metadata) { + md.C = C.LLVMConstantAsMetadata(v.C) return } @@ -1188,8 +1201,9 @@ func (b Builder) InsertWithName(instr Value, name string) { func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) } // Metadata -func (b Builder) SetCurrentDebugLocation(v Value) { C.LLVMSetCurrentDebugLocation(b.C, v.C) } -func (b Builder) CurrentDebugLocation() (v Value) { v.C = C.LLVMGetCurrentDebugLocation(b.C); return } +func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) { + C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C) +} func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) } func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value { f := module.NamedFunction("llvm.dbg.declare") @@ -1822,3 +1836,11 @@ func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassMan // the module provider. // See llvm::PassManagerBase::~PassManagerBase. func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) } + +//------------------------------------------------------------------------- +// llvm.Metadata +//------------------------------------------------------------------------- + +func (md Metadata) ReplaceAllUsesWith(new Metadata) { + C.LLVMMetadataReplaceAllUsesWith(md.C, new.C) +} diff --git a/bindings/go/llvm/linker.go b/bindings/go/llvm/linker.go index 31e9ad2..64d794e 100644 --- a/bindings/go/llvm/linker.go +++ b/bindings/go/llvm/linker.go @@ -20,16 +20,9 @@ package llvm import "C" import "errors" -type LinkerMode C.LLVMLinkerMode - -const ( - LinkerDestroySource = C.LLVMLinkerDestroySource - LinkerPreserveSource = C.LLVMLinkerPreserveSource -) - -func LinkModules(Dest, Src Module, Mode LinkerMode) error { +func LinkModules(Dest, Src Module) error { var cmsg *C.char - failed := C.LLVMLinkModules(Dest.C, Src.C, C.LLVMLinkerMode(Mode), &cmsg) + failed := C.LLVMLinkModules(Dest.C, Src.C, 0, &cmsg) if failed != 0 { err := errors.New(C.GoString(cmsg)) C.LLVMDisposeMessage(cmsg) diff --git a/bindings/go/llvm/transforms_instrumentation.go b/bindings/go/llvm/transforms_instrumentation.go index 9b191b2..73e2732 100644 --- a/bindings/go/llvm/transforms_instrumentation.go +++ b/bindings/go/llvm/transforms_instrumentation.go @@ -36,8 +36,11 @@ func (pm PassManager) AddMemorySanitizerPass() { C.LLVMAddMemorySanitizerPass(pm.C) } -func (pm PassManager) AddDataFlowSanitizerPass(abilist string) { - cabilist := C.CString(abilist) - defer C.free(unsafe.Pointer(cabilist)) - C.LLVMAddDataFlowSanitizerPass(pm.C, cabilist) +func (pm PassManager) AddDataFlowSanitizerPass(abilist []string) { + abiliststrs := make([]*C.char, len(abilist)) + for i, arg := range abilist { + abiliststrs[i] = C.CString(arg) + defer C.free(unsafe.Pointer(abiliststrs[i])) + } + C.LLVMAddDataFlowSanitizerPass(pm.C, C.int(len(abilist)), &abiliststrs[0]) } diff --git a/bindings/ocaml/CMakeLists.txt b/bindings/ocaml/CMakeLists.txt new file mode 100644 index 0000000..2058368 --- /dev/null +++ b/bindings/ocaml/CMakeLists.txt @@ -0,0 +1,11 @@ +add_subdirectory(llvm) +add_subdirectory(all_backends) +add_subdirectory(analysis) +add_subdirectory(backends) +add_subdirectory(bitreader) +add_subdirectory(bitwriter) +add_subdirectory(irreader) +add_subdirectory(linker) +add_subdirectory(target) +add_subdirectory(transforms) +add_subdirectory(executionengine) diff --git a/bindings/ocaml/Makefile.ocaml b/bindings/ocaml/Makefile.ocaml index 5e00cf5..1f65a7b 100644 --- a/bindings/ocaml/Makefile.ocaml +++ b/bindings/ocaml/Makefile.ocaml @@ -32,6 +32,12 @@ endif include $(LEVEL)/Makefile.common +# Used in out-of-tree builds of OCaml bindings only. +ifdef SYSTEM_LLVM_CONFIG +LLVM_CONFIG = $(SYSTEM_LLVM_CONFIG) +LLVMLibsOptions += $(shell $(LLVM_CONFIG) --ldflags) +endif + # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=. PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR) @@ -65,6 +71,10 @@ OCAMLRPATH := $(RPATH) -Wl,'$$ORIGIN/../../lib' endif endif +# See http://caml.inria.fr/mantis/view.php?id=6642 +OCAMLORIGIN := -ccopt -L'$$CAMLORIGIN/..' \ + -ccopt $(RPATH) -ccopt -Wl,'$$CAMLORIGIN/..' + # Tools OCAMLCFLAGS += -I $(OcamlDir) $(addprefix -package ,$(FindlibPackages)) @@ -92,16 +102,18 @@ Compile.CMX := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o) ifdef OCAMLSTUBS # -dllib is engaged with ocamlc builds, $(OCAMLSTUBFLAGS) in ocamlc -custom builds. Archive.CMA := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLSTUBFLAGS) \ - $(OCAMLDEBUGFLAG) -o) + $(OCAMLDEBUGFLAG) $(OCAMLORIGIN) -o) else Archive.CMA := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \ - -o) + $(OCAMLORIGIN) -o) endif ifdef OCAMLSTUBS -Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLSTUBFLAGS) $(OCAMLDEBUGFLAG) -o) +Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLSTUBFLAGS) $(OCAMLDEBUGFLAG) \ + $(OCAMLORIGIN) -o) else -Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o) +Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \ + $(OCAMLORIGIN) -o) endif # Source files @@ -237,8 +249,8 @@ uninstall-local:: uninstall-shared $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir $(Echo) "Building $(BuildMode) $(notdir $@)" - $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \ - -o $@ $(ObjectsO) + $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) -o $@ $(ObjectsO) \ + $(LLVMLibsOptions) clean-shared:: -$(Verb) $(RM) -f $(SharedLib) @@ -255,8 +267,9 @@ uninstall-shared:: endif -##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===## +##===- Deposit dependent libraries adjacent to OCaml libs -----------------===## +ifndef SYSTEM_LLVM_CONFIG all-local:: build-deplibs clean-local:: clean-deplibs install-local:: install-deplibs @@ -281,7 +294,7 @@ install-deplibs: uninstall-deplibs: $(Verb) $(RM) -f $(DestLibs) - +endif ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===## diff --git a/bindings/ocaml/all_backends/CMakeLists.txt b/bindings/ocaml/all_backends/CMakeLists.txt new file mode 100644 index 0000000..716a49c --- /dev/null +++ b/bindings/ocaml/all_backends/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_all_backends + OCAML llvm_all_backends + OCAMLDEP llvm + C all_backends_ocaml + LLVM ${LLVM_TARGETS_TO_BUILD}) diff --git a/bindings/ocaml/analysis/CMakeLists.txt b/bindings/ocaml/analysis/CMakeLists.txt new file mode 100644 index 0000000..f8ca84d --- /dev/null +++ b/bindings/ocaml/analysis/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_analysis + OCAML llvm_analysis + OCAMLDEP llvm + C analysis_ocaml + LLVM analysis) diff --git a/bindings/ocaml/backends/CMakeLists.txt b/bindings/ocaml/backends/CMakeLists.txt new file mode 100644 index 0000000..a980638 --- /dev/null +++ b/bindings/ocaml/backends/CMakeLists.txt @@ -0,0 +1,27 @@ +foreach(TARGET ${LLVM_TARGETS_TO_BUILD}) + set(OCAML_LLVM_TARGET ${TARGET}) + + foreach( ext ml mli ) + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/llvm_backend.${ext}.in" + "${CMAKE_CURRENT_BINARY_DIR}/llvm_${TARGET}.${ext}") + endforeach() + + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/backend_ocaml.c" + "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_ocaml.c") + + add_ocaml_library(llvm_${TARGET} + OCAML llvm_${TARGET} + C ${TARGET}_ocaml + CFLAGS -DTARGET=${TARGET} + LLVM ${TARGET} + NOCOPY) + + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/META.llvm_backend.in" + "${LLVM_LIBRARY_DIR}/ocaml/META.llvm_${TARGET}") + + install(FILES "${LLVM_LIBRARY_DIR}/ocaml/META.llvm_${TARGET}" + DESTINATION lib/ocaml) +endforeach() diff --git a/bindings/ocaml/bitreader/CMakeLists.txt b/bindings/ocaml/bitreader/CMakeLists.txt new file mode 100644 index 0000000..8d16103 --- /dev/null +++ b/bindings/ocaml/bitreader/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_bitreader + OCAML llvm_bitreader + OCAMLDEP llvm + C bitreader_ocaml + LLVM bitreader) diff --git a/bindings/ocaml/bitwriter/CMakeLists.txt b/bindings/ocaml/bitwriter/CMakeLists.txt new file mode 100644 index 0000000..5a14498 --- /dev/null +++ b/bindings/ocaml/bitwriter/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_bitwriter + OCAML llvm_bitwriter + OCAMLDEP llvm + C bitwriter_ocaml + LLVM bitwriter) diff --git a/bindings/ocaml/executionengine/CMakeLists.txt b/bindings/ocaml/executionengine/CMakeLists.txt new file mode 100644 index 0000000..ae9af08 --- /dev/null +++ b/bindings/ocaml/executionengine/CMakeLists.txt @@ -0,0 +1,6 @@ +add_ocaml_library(llvm_executionengine + OCAML llvm_executionengine + OCAMLDEP llvm llvm_target + C executionengine_ocaml + LLVM executionengine mcjit native + PKG ctypes) diff --git a/bindings/ocaml/executionengine/executionengine_ocaml.c b/bindings/ocaml/executionengine/executionengine_ocaml.c index 0557efc..b799250 100644 --- a/bindings/ocaml/executionengine/executionengine_ocaml.c +++ b/bindings/ocaml/executionengine/executionengine_ocaml.c @@ -115,8 +115,12 @@ CAMLprim value llvm_ee_add_global_mapping(LLVMValueRef Global, value Ptr, return Val_unit; } -/* Llvm.llvalue -> llexecutionengine -> int64 */ -CAMLprim value llvm_ee_get_pointer_to_global(LLVMValueRef Global, - LLVMExecutionEngineRef EE) { - return caml_copy_int64((int64_t) LLVMGetPointerToGlobal(EE, Global)); +CAMLprim value llvm_ee_get_global_value_address(value Name, + LLVMExecutionEngineRef EE) { + return caml_copy_int64((int64_t) LLVMGetGlobalValueAddress(EE, String_val(Name))); +} + +CAMLprim value llvm_ee_get_function_address(value Name, + LLVMExecutionEngineRef EE) { + return caml_copy_int64((int64_t) LLVMGetFunctionAddress(EE, String_val(Name))); } diff --git a/bindings/ocaml/executionengine/llvm_executionengine.ml b/bindings/ocaml/executionengine/llvm_executionengine.ml index c0ff330..34031be 100644 --- a/bindings/ocaml/executionengine/llvm_executionengine.ml +++ b/bindings/ocaml/executionengine/llvm_executionengine.ml @@ -45,15 +45,27 @@ external data_layout : llexecutionengine -> Llvm_target.DataLayout.t = "llvm_ee_get_data_layout" external add_global_mapping_ : Llvm.llvalue -> int64 -> llexecutionengine -> unit = "llvm_ee_add_global_mapping" -external get_pointer_to_global_ : Llvm.llvalue -> llexecutionengine -> int64 - = "llvm_ee_get_pointer_to_global" +external get_global_value_address_ : string -> llexecutionengine -> int64 + = "llvm_ee_get_global_value_address" +external get_function_address_ : string -> llexecutionengine -> int64 + = "llvm_ee_get_function_address" let add_global_mapping llval ptr ee = add_global_mapping_ llval (Ctypes.raw_address_of_ptr (Ctypes.to_voidp ptr)) ee -let get_pointer_to_global llval typ ee = - Ctypes.coerce (let open Ctypes in ptr void) typ - (Ctypes.ptr_of_raw_address (get_pointer_to_global_ llval ee)) +let get_global_value_address name typ ee = + let vptr = get_global_value_address_ name ee in + if Int64.to_int vptr <> 0 then + let open Ctypes in !@ (coerce (ptr void) (ptr typ) (ptr_of_raw_address vptr)) + else + raise (Error ("Value " ^ name ^ " not found")) + +let get_function_address name typ ee = + let fptr = get_function_address_ name ee in + if Int64.to_int fptr <> 0 then + let open Ctypes in coerce (ptr void) typ (ptr_of_raw_address fptr) + else + raise (Error ("Function " ^ name ^ " not found")) (* The following are not bound. Patches are welcome. target_machine : llexecutionengine -> Llvm_target.TargetMachine.t diff --git a/bindings/ocaml/executionengine/llvm_executionengine.mli b/bindings/ocaml/executionengine/llvm_executionengine.mli index b07151d..bc076be 100644 --- a/bindings/ocaml/executionengine/llvm_executionengine.mli +++ b/bindings/ocaml/executionengine/llvm_executionengine.mli @@ -76,9 +76,18 @@ val data_layout : llexecutionengine -> Llvm_target.DataLayout.t All uses of [gv] in the compiled code will refer to [ptr]. *) val add_global_mapping : Llvm.llvalue -> 'a Ctypes.ptr -> llexecutionengine -> unit -(** [get_pointer_to_global gv typ ee] returns the value of the global - variable [gv] in the execution engine [ee] as type [typ], which may - be a pointer type (e.g. [int ptr typ]) for global variables or - a function (e.g. [(int -> int) typ]) type for functions, and which - will be live as long as [gv] and [ee] are. *) -val get_pointer_to_global : Llvm.llvalue -> 'a Ctypes.typ -> llexecutionengine -> 'a +(** [get_global_value_address id typ ee] returns a pointer to the + identifier [id] as type [typ], which will be a pointer type for a + value, and which will be live as long as [id] and [ee] + are. Caution: this function finalizes, i.e. forces code + generation, all loaded modules. Further modifications to the + modules will not have any effect. *) +val get_global_value_address : string -> 'a Ctypes.typ -> llexecutionengine -> 'a + +(** [get_function_address fn typ ee] returns a pointer to the function + [fn] as type [typ], which will be a pointer type for a function + (e.g. [(int -> int) typ]), and which will be live as long as [fn] + and [ee] are. Caution: this function finalizes, i.e. forces code + generation, all loaded modules. Further modifications to the + modules will not have any effect. *) +val get_function_address : string -> 'a Ctypes.typ -> llexecutionengine -> 'a diff --git a/bindings/ocaml/irreader/CMakeLists.txt b/bindings/ocaml/irreader/CMakeLists.txt new file mode 100644 index 0000000..87d269b --- /dev/null +++ b/bindings/ocaml/irreader/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_irreader + OCAML llvm_irreader + OCAMLDEP llvm + C irreader_ocaml + LLVM irreader) diff --git a/bindings/ocaml/linker/CMakeLists.txt b/bindings/ocaml/linker/CMakeLists.txt new file mode 100644 index 0000000..b6bc8ac --- /dev/null +++ b/bindings/ocaml/linker/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_linker + OCAML llvm_linker + OCAMLDEP llvm + C linker_ocaml + LLVM linker) diff --git a/bindings/ocaml/linker/linker_ocaml.c b/bindings/ocaml/linker/linker_ocaml.c index ed37777..3b8512a 100644 --- a/bindings/ocaml/linker/linker_ocaml.c +++ b/bindings/ocaml/linker/linker_ocaml.c @@ -23,11 +23,11 @@ void llvm_raise(value Prototype, char *Message); -/* llmodule -> llmodule -> Mode.t -> unit */ -CAMLprim value llvm_link_modules(LLVMModuleRef Dst, LLVMModuleRef Src, value Mode) { +/* llmodule -> llmodule -> unit */ +CAMLprim value llvm_link_modules(LLVMModuleRef Dst, LLVMModuleRef Src) { char* Message; - if (LLVMLinkModules(Dst, Src, Int_val(Mode), &Message)) + if (LLVMLinkModules(Dst, Src, 0, &Message)) llvm_raise(*caml_named_value("Llvm_linker.Error"), Message); return Val_unit; diff --git a/bindings/ocaml/linker/llvm_linker.ml b/bindings/ocaml/linker/llvm_linker.ml index 5854d70..3044abd 100644 --- a/bindings/ocaml/linker/llvm_linker.ml +++ b/bindings/ocaml/linker/llvm_linker.ml @@ -11,11 +11,5 @@ exception Error of string let () = Callback.register_exception "Llvm_linker.Error" (Error "") -module Mode = struct - type t = - | DestroySource - | PreserveSource -end - -external link_modules : Llvm.llmodule -> Llvm.llmodule -> Mode.t -> unit +external link_modules : Llvm.llmodule -> Llvm.llmodule -> unit = "llvm_link_modules" diff --git a/bindings/ocaml/linker/llvm_linker.mli b/bindings/ocaml/linker/llvm_linker.mli index 4def7a8..06c3b92 100644 --- a/bindings/ocaml/linker/llvm_linker.mli +++ b/bindings/ocaml/linker/llvm_linker.mli @@ -14,13 +14,6 @@ exception Error of string -(** Linking mode. *) -module Mode : sig - type t = - | DestroySource - | PreserveSource -end - (** [link_modules dst src mode] links [src] into [dst], raising [Error] if the linking fails. *) -val link_modules : Llvm.llmodule -> Llvm.llmodule -> Mode.t -> unit
\ No newline at end of file +val link_modules : Llvm.llmodule -> Llvm.llmodule -> unit
\ No newline at end of file diff --git a/bindings/ocaml/llvm/CMakeLists.txt b/bindings/ocaml/llvm/CMakeLists.txt new file mode 100644 index 0000000..4956fa4 --- /dev/null +++ b/bindings/ocaml/llvm/CMakeLists.txt @@ -0,0 +1,11 @@ +add_ocaml_library(llvm + OCAML llvm + C llvm_ocaml + LLVM core support) + +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/META.llvm.in" + "${LLVM_LIBRARY_DIR}/ocaml/META.llvm") + +install(FILES "${LLVM_LIBRARY_DIR}/ocaml/META.llvm" + DESTINATION lib/ocaml) diff --git a/bindings/ocaml/llvm/META.llvm.in b/bindings/ocaml/llvm/META.llvm.in index f9808c7..92896e3 100644 --- a/bindings/ocaml/llvm/META.llvm.in +++ b/bindings/ocaml/llvm/META.llvm.in @@ -61,6 +61,14 @@ package "scalar_opts" ( archive(native) = "llvm_scalar_opts.cmxa" ) +package "transform_utils" ( + requires = "llvm" + version = "@PACKAGE_VERSION@" + description = "Transform utilities for LLVM" + archive(byte) = "llvm_transform_utils.cma" + archive(native) = "llvm_transform_utils.cmxa" +) + package "vectorize" ( requires = "llvm" version = "@PACKAGE_VERSION@" diff --git a/bindings/ocaml/llvm/Makefile b/bindings/ocaml/llvm/Makefile index fb682c7..c0785a1 100644 --- a/bindings/ocaml/llvm/Makefile +++ b/bindings/ocaml/llvm/Makefile @@ -13,7 +13,7 @@ LEVEL := ../../.. LIBRARYNAME := llvm -UsedComponents := core transformutils +UsedComponents := core UsedOcamlLibs := llvm ExtraLibs := -lstdc++ diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index 0df4d40..9a3cb1f 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -313,7 +313,6 @@ external mdkind_id : llcontext -> string -> llmdkind = "llvm_mdkind_id" (*===-- Modules -----------------------------------------------------------===*) external create_module : llcontext -> string -> llmodule = "llvm_create_module" external dispose_module : llmodule -> unit = "llvm_dispose_module" -external clone_module : llmodule -> llmodule = "LLVMCloneModule" external target_triple: llmodule -> string = "llvm_target_triple" external set_target_triple: string -> llmodule -> unit @@ -460,6 +459,7 @@ external clear_metadata : llvalue -> llmdkind -> unit = "llvm_clear_metadata" (*--... Operations on metadata .......,.....................................--*) external mdstring : llcontext -> string -> llvalue = "llvm_mdstring" external mdnode : llcontext -> llvalue array -> llvalue = "llvm_mdnode" +external mdnull : llcontext -> llvalue = "llvm_mdnull" external get_mdstring : llvalue -> string option = "llvm_get_mdstring" external get_named_metadata : llmodule -> string -> llvalue array = "llvm_get_namedmd" @@ -1300,6 +1300,8 @@ external build_fcmp : Fcmp.t -> llvalue -> llvalue -> string -> (*--... Miscellaneous instructions .........................................--*) external build_phi : (llvalue * llbasicblock) list -> string -> llbuilder -> llvalue = "llvm_build_phi" +external build_empty_phi : lltype -> string -> llbuilder -> llvalue + = "llvm_build_empty_phi" external build_call : llvalue -> llvalue array -> string -> llbuilder -> llvalue = "llvm_build_call" external build_select : llvalue -> llvalue -> llvalue -> string -> llbuilder -> diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index e5e90c3..dcda027 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -431,9 +431,6 @@ val create_module : llcontext -> string -> llmodule [llvm::Module::~Module]. *) val dispose_module : llmodule -> unit -(** [clone_module m] returns an exact copy of module [m]. *) -val clone_module : llmodule -> llmodule - (** [target_triple m] is the target specifier for the module [m], something like [i686-apple-darwin8]. See the method [llvm::Module::getTargetTriple]. *) val target_triple: llmodule -> string @@ -822,6 +819,9 @@ val mdstring : llcontext -> string -> llvalue See the method [llvm::MDNode::get]. *) val mdnode : llcontext -> llvalue array -> llvalue +(** [mdnull c ] returns a null MDNode in context [c]. *) +val mdnull : llcontext -> llvalue + (** [get_mdstring v] returns the MDString. See the method [llvm::MDString::getString] *) val get_mdstring : llvalue -> string option @@ -2422,6 +2422,12 @@ val build_fcmp : Fcmp.t -> llvalue -> llvalue -> string -> val build_phi : (llvalue * llbasicblock) list -> string -> llbuilder -> llvalue +(** [build_empty_phi ty name b] creates a + [%name = phi %ty] instruction at the position specified by + the instruction builder [b]. [ty] is the type of the instruction. + See the method [llvm::LLVMBuilder::CreatePHI]. *) +val build_empty_phi : lltype -> string -> llbuilder -> llvalue + (** [build_call fn args name b] creates a [%name = call %fn(args...)] instruction at the position specified by the instruction builder [b]. diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index 63c235d..3889f92 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -666,6 +666,11 @@ CAMLprim LLVMValueRef llvm_mdnode(LLVMContextRef C, value ElementVals) { Wosize_val(ElementVals)); } +/* llcontext -> llvalue */ +CAMLprim LLVMValueRef llvm_mdnull(LLVMContextRef C) { + return NULL; +} + /* llvalue -> string option */ CAMLprim value llvm_get_mdstring(LLVMValueRef V) { CAMLparam0(); @@ -2186,6 +2191,15 @@ CAMLprim LLVMValueRef llvm_build_phi(value Incoming, value Name, value B) { return PhiNode; } +/* lltype -> string -> llbuilder -> value */ +CAMLprim LLVMValueRef llvm_build_empty_phi(LLVMTypeRef Type, value Name, value B) { + LLVMValueRef PhiNode; + + return LLVMBuildPhi(Builder_val(B), Type, String_val(Name)); + + return PhiNode; +} + /* llvalue -> llvalue array -> string -> llbuilder -> llvalue */ CAMLprim LLVMValueRef llvm_build_call(LLVMValueRef Fn, value Params, value Name, value B) { diff --git a/bindings/ocaml/target/CMakeLists.txt b/bindings/ocaml/target/CMakeLists.txt new file mode 100644 index 0000000..adee0fc --- /dev/null +++ b/bindings/ocaml/target/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_target + OCAML llvm_target + OCAMLDEP llvm + C target_ocaml + LLVM target) diff --git a/bindings/ocaml/transforms/CMakeLists.txt b/bindings/ocaml/transforms/CMakeLists.txt new file mode 100644 index 0000000..beb8694 --- /dev/null +++ b/bindings/ocaml/transforms/CMakeLists.txt @@ -0,0 +1,5 @@ +add_subdirectory(ipo) +add_subdirectory(passmgr_builder) +add_subdirectory(scalar_opts) +add_subdirectory(utils) +add_subdirectory(vectorize) diff --git a/bindings/ocaml/transforms/Makefile b/bindings/ocaml/transforms/Makefile index f3637a6..15bffb4 100644 --- a/bindings/ocaml/transforms/Makefile +++ b/bindings/ocaml/transforms/Makefile @@ -8,7 +8,7 @@ ##===----------------------------------------------------------------------===## LEVEL := ../../.. -DIRS = scalar_opts ipo vectorize passmgr_builder +DIRS = ipo passmgr_builder scalar_opts utils vectorize ocamldoc: $(Verb) for i in $(DIRS) ; do \ diff --git a/bindings/ocaml/transforms/ipo/CMakeLists.txt b/bindings/ocaml/transforms/ipo/CMakeLists.txt new file mode 100644 index 0000000..4b8784f --- /dev/null +++ b/bindings/ocaml/transforms/ipo/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_ipo + OCAML llvm_ipo + OCAMLDEP llvm + C ipo_ocaml + LLVM ipo) diff --git a/bindings/ocaml/transforms/passmgr_builder/CMakeLists.txt b/bindings/ocaml/transforms/passmgr_builder/CMakeLists.txt new file mode 100644 index 0000000..b012863 --- /dev/null +++ b/bindings/ocaml/transforms/passmgr_builder/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_passmgr_builder + OCAML llvm_passmgr_builder + OCAMLDEP llvm + C passmgr_builder_ocaml + LLVM ipo) diff --git a/bindings/ocaml/transforms/scalar_opts/CMakeLists.txt b/bindings/ocaml/transforms/scalar_opts/CMakeLists.txt new file mode 100644 index 0000000..98c7c68 --- /dev/null +++ b/bindings/ocaml/transforms/scalar_opts/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_scalar_opts + OCAML llvm_scalar_opts + OCAMLDEP llvm + C scalar_opts_ocaml + LLVM scalaropts) diff --git a/bindings/ocaml/transforms/utils/CMakeLists.txt b/bindings/ocaml/transforms/utils/CMakeLists.txt new file mode 100644 index 0000000..37f3eb7 --- /dev/null +++ b/bindings/ocaml/transforms/utils/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_transform_utils + OCAML llvm_transform_utils + OCAMLDEP llvm + C transform_utils_ocaml + LLVM transformutils) diff --git a/bindings/ocaml/transforms/utils/Makefile b/bindings/ocaml/transforms/utils/Makefile new file mode 100644 index 0000000..76a6f0b --- /dev/null +++ b/bindings/ocaml/transforms/utils/Makefile @@ -0,0 +1,19 @@ +##===- bindings/ocaml/transforms/utils/Makefile ------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +# +# This is the makefile for the Objective Caml Llvm_vectorize interface. +# +##===----------------------------------------------------------------------===## + +LEVEL := ../../../.. +LIBRARYNAME := llvm_transform_utils +UsedComponents := transformutils +UsedOcamlInterfaces := llvm + +include ../../Makefile.ocaml diff --git a/bindings/ocaml/transforms/utils/llvm_transform_utils.ml b/bindings/ocaml/transforms/utils/llvm_transform_utils.ml new file mode 100644 index 0000000..135efe2 --- /dev/null +++ b/bindings/ocaml/transforms/utils/llvm_transform_utils.ml @@ -0,0 +1,10 @@ +(*===-- llvm_transform_utils.ml - LLVM OCaml Interface --------*- OCaml -*-===* + * + * The LLVM Compiler Infrastructure + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + *===----------------------------------------------------------------------===*) + +external clone_module : Llvm.llmodule -> Llvm.llmodule = "llvm_clone_module" diff --git a/bindings/ocaml/transforms/utils/llvm_transform_utils.mli b/bindings/ocaml/transforms/utils/llvm_transform_utils.mli new file mode 100644 index 0000000..1c2b07c --- /dev/null +++ b/bindings/ocaml/transforms/utils/llvm_transform_utils.mli @@ -0,0 +1,17 @@ +(*===-- llvm_transform_utils.mli - LLVM OCaml Interface -------*- OCaml -*-===* + * + * The LLVM Compiler Infrastructure + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + *===----------------------------------------------------------------------===*) + +(** Transform Utilities. + + This interface provides an OCaml API for LLVM transform utilities, the + classes in the [LLVMTransformUtils] library. *) + +(** [clone_module m] returns an exact copy of module [m]. + See the [llvm::CloneModule] function. *) +external clone_module : Llvm.llmodule -> Llvm.llmodule = "llvm_clone_module" diff --git a/bindings/ocaml/transforms/utils/transform_utils_ocaml.c b/bindings/ocaml/transforms/utils/transform_utils_ocaml.c new file mode 100644 index 0000000..75b2052 --- /dev/null +++ b/bindings/ocaml/transforms/utils/transform_utils_ocaml.c @@ -0,0 +1,31 @@ +/*===-- vectorize_ocaml.c - LLVM OCaml Glue ---------------------*- C++ -*-===*\ +|* *| +|* The LLVM Compiler Infrastructure *| +|* *| +|* This file is distributed under the University of Illinois Open Source *| +|* License. See LICENSE.TXT for details. *| +|* *| +|*===----------------------------------------------------------------------===*| +|* *| +|* This file glues LLVM's OCaml interface to its C interface. These functions *| +|* are by and large transparent wrappers to the corresponding C functions. *| +|* *| +|* Note that these functions intentionally take liberties with the CAMLparamX *| +|* macros, since most of the parameters are not GC heap objects. *| +|* *| +\*===----------------------------------------------------------------------===*/ + +#include "llvm-c/Core.h" +#include "caml/mlvalues.h" +#include "caml/misc.h" + +/* + * Do not move directly into external. This function is here to pull in + * -lLLVMTransformUtils, which would otherwise be not linked on static builds, + * as ld can't see the reference from OCaml code. + */ + +/* llmodule -> llmodule */ +CAMLprim LLVMModuleRef llvm_clone_module(LLVMModuleRef M) { + return LLVMCloneModule(M); +} diff --git a/bindings/ocaml/transforms/vectorize/CMakeLists.txt b/bindings/ocaml/transforms/vectorize/CMakeLists.txt new file mode 100644 index 0000000..af0ffce --- /dev/null +++ b/bindings/ocaml/transforms/vectorize/CMakeLists.txt @@ -0,0 +1,5 @@ +add_ocaml_library(llvm_vectorize + OCAML llvm_vectorize + OCAMLDEP llvm + C vectorize_ocaml + LLVM vectorize) |