1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 #include "llvm/Object/Archive.h"
13 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/IR/DiagnosticInfo.h"
15 #include "llvm/IR/DiagnosticPrinter.h"
17 #include "llvm/IR/CallSite.h"
19 //===----------------------------------------------------------------------===
21 // This file defines alternate interfaces to core functions that are more
22 // readily callable by Rust's FFI.
24 //===----------------------------------------------------------------------===
27 using namespace llvm::sys;
28 using namespace llvm::object;
30 static char *LastError;
32 extern "C" LLVMMemoryBufferRef
33 LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
34 ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(Path,
38 LLVMRustSetLastError(buf_or.getError().message().c_str());
41 return wrap(buf_or.get().release());
44 extern "C" char *LLVMRustGetLastError(void) {
45 char *ret = LastError;
50 void LLVMRustSetLastError(const char *err) {
51 free((void*) LastError);
52 LastError = strdup(err);
56 LLVMRustSetNormalizedTarget(LLVMModuleRef M, const char *triple) {
57 unwrap(M)->setTargetTriple(Triple::normalize(triple));
60 extern "C" LLVMValueRef LLVMRustConstSmallInt(LLVMTypeRef IntTy, unsigned N,
61 LLVMBool SignExtend) {
62 return LLVMConstInt(IntTy, (unsigned long long)N, SignExtend);
65 extern "C" LLVMValueRef LLVMRustConstInt(LLVMTypeRef IntTy,
68 LLVMBool SignExtend) {
69 unsigned long long N = N_hi;
72 return LLVMConstInt(IntTy, N, SignExtend);
75 extern "C" void LLVMRustPrintPassTimings() {
76 raw_fd_ostream OS (2, false); // stderr.
77 TimerGroup::printAll(OS);
80 extern "C" LLVMValueRef LLVMGetNamedValue(LLVMModuleRef M,
82 return wrap(unwrap(M)->getNamedValue(Name));
85 extern "C" LLVMValueRef LLVMGetOrInsertFunction(LLVMModuleRef M,
87 LLVMTypeRef FunctionTy) {
88 return wrap(unwrap(M)->getOrInsertFunction(Name,
89 unwrap<FunctionType>(FunctionTy)));
92 extern "C" LLVMValueRef LLVMGetOrInsertGlobal(LLVMModuleRef M,
95 return wrap(unwrap(M)->getOrInsertGlobal(Name, unwrap(Ty)));
98 extern "C" LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) {
99 return wrap(Type::getMetadataTy(*unwrap(C)));
102 extern "C" void LLVMAddCallSiteAttribute(LLVMValueRef Instr, unsigned index, uint64_t Val) {
103 CallSite Call = CallSite(unwrap<Instruction>(Instr));
107 Call.getAttributes().addAttributes(Call->getContext(), index,
108 AttributeSet::get(Call->getContext(),
113 extern "C" void LLVMAddDereferenceableCallSiteAttr(LLVMValueRef Instr, unsigned idx, uint64_t b) {
114 CallSite Call = CallSite(unwrap<Instruction>(Instr));
116 B.addDereferenceableAttr(b);
118 Call.getAttributes().addAttributes(Call->getContext(), idx,
119 AttributeSet::get(Call->getContext(),
123 extern "C" void LLVMAddFunctionAttribute(LLVMValueRef Fn, unsigned index,
125 Function *A = unwrap<Function>(Fn);
128 A->addAttributes(index, AttributeSet::get(A->getContext(), index, B));
131 extern "C" void LLVMAddDereferenceableAttr(LLVMValueRef Fn, unsigned index, uint64_t bytes) {
132 Function *A = unwrap<Function>(Fn);
134 B.addDereferenceableAttr(bytes);
135 A->addAttributes(index, AttributeSet::get(A->getContext(), index, B));
138 extern "C" void LLVMAddFunctionAttrString(LLVMValueRef Fn, unsigned index, const char *Name) {
139 Function *F = unwrap<Function>(Fn);
141 B.addAttribute(Name);
142 F->addAttributes(index, AttributeSet::get(F->getContext(), index, B));
145 extern "C" void LLVMAddFunctionAttrStringValue(LLVMValueRef Fn, unsigned index,
148 Function *F = unwrap<Function>(Fn);
150 B.addAttribute(Name, Value);
151 F->addAttributes(index, AttributeSet::get(F->getContext(), index, B));
154 extern "C" void LLVMRemoveFunctionAttributes(LLVMValueRef Fn, unsigned index, uint64_t Val) {
155 Function *A = unwrap<Function>(Fn);
156 const AttributeSet PAL = A->getAttributes();
158 const AttributeSet PALnew =
159 PAL.removeAttributes(A->getContext(), index,
160 AttributeSet::get(A->getContext(), index, B));
161 A->setAttributes(PALnew);
164 extern "C" void LLVMRemoveFunctionAttrString(LLVMValueRef fn, unsigned index, const char *Name) {
165 Function *f = unwrap<Function>(fn);
166 LLVMContext &C = f->getContext();
168 B.addAttribute(Name);
169 AttributeSet to_remove = AttributeSet::get(C, index, B);
171 AttributeSet attrs = f->getAttributes();
172 f->setAttributes(attrs.removeAttributes(f->getContext(),
177 // enable fpmath flag UnsafeAlgebra
178 extern "C" void LLVMRustSetHasUnsafeAlgebra(LLVMValueRef V) {
179 if (auto I = dyn_cast<Instruction>(unwrap<Value>(V))) {
180 I->setHasUnsafeAlgebra(true);
184 extern "C" LLVMValueRef LLVMBuildAtomicLoad(LLVMBuilderRef B,
187 AtomicOrdering order,
188 unsigned alignment) {
189 LoadInst* li = new LoadInst(unwrap(source),0);
190 li->setAtomic(order);
191 li->setAlignment(alignment);
192 return wrap(unwrap(B)->Insert(li, Name));
195 extern "C" LLVMValueRef LLVMBuildAtomicStore(LLVMBuilderRef B,
198 AtomicOrdering order,
199 unsigned alignment) {
200 StoreInst* si = new StoreInst(unwrap(val),unwrap(target));
201 si->setAtomic(order);
202 si->setAlignment(alignment);
203 return wrap(unwrap(B)->Insert(si));
206 extern "C" LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B,
210 AtomicOrdering order,
211 AtomicOrdering failure_order,
213 AtomicCmpXchgInst* acxi = unwrap(B)->CreateAtomicCmpXchg(unwrap(target),
221 extern "C" LLVMValueRef LLVMBuildAtomicFence(LLVMBuilderRef B,
222 AtomicOrdering order,
223 SynchronizationScope scope) {
224 return wrap(unwrap(B)->CreateFence(order, scope));
227 extern "C" void LLVMSetDebug(int Enabled) {
233 extern "C" LLVMValueRef LLVMInlineAsm(LLVMTypeRef Ty,
236 LLVMBool HasSideEffects,
237 LLVMBool IsAlignStack,
239 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty), AsmString,
240 Constraints, HasSideEffects,
241 IsAlignStack, (InlineAsm::AsmDialect) Dialect));
244 typedef DIBuilder* DIBuilderRef;
246 typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
249 DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)
251 inline Metadata **unwrap(LLVMMetadataRef *Vals) {
252 return reinterpret_cast<Metadata**>(Vals);
256 template<typename DIT>
257 DIT* unwrapDIptr(LLVMMetadataRef ref) {
258 return (DIT*) (ref ? unwrap<MDNode>(ref) : NULL);
261 #define DIDescriptor DIScope
262 #define DIArray DINodeArray
263 #define unwrapDI unwrapDIptr
265 extern "C" uint32_t LLVMRustDebugMetadataVersion() {
266 return DEBUG_METADATA_VERSION;
269 extern "C" uint32_t LLVMVersionMinor() {
270 return LLVM_VERSION_MINOR;
273 extern "C" uint32_t LLVMVersionMajor() {
274 return LLVM_VERSION_MAJOR;
277 extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M,
280 unwrap(M)->addModuleFlag(Module::Warning, name, value);
283 extern "C" DIBuilderRef LLVMDIBuilderCreate(LLVMModuleRef M) {
284 return new DIBuilder(*unwrap(M));
287 extern "C" void LLVMDIBuilderDispose(DIBuilderRef Builder) {
291 extern "C" void LLVMDIBuilderFinalize(DIBuilderRef Builder) {
295 extern "C" LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
296 DIBuilderRef Builder,
300 const char* Producer,
304 const char* SplitName) {
305 return wrap(Builder->createCompileUnit(Lang,
315 extern "C" LLVMMetadataRef LLVMDIBuilderCreateFile(
316 DIBuilderRef Builder,
317 const char* Filename,
318 const char* Directory) {
319 return wrap(Builder->createFile(Filename, Directory));
322 extern "C" LLVMMetadataRef LLVMDIBuilderCreateSubroutineType(
323 DIBuilderRef Builder,
324 LLVMMetadataRef File,
325 LLVMMetadataRef ParameterTypes) {
326 return wrap(Builder->createSubroutineType(
327 #if LLVM_VERSION_MINOR == 7
328 unwrapDI<DIFile>(File),
330 DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
333 extern "C" LLVMMetadataRef LLVMDIBuilderCreateFunction(
334 DIBuilderRef Builder,
335 LLVMMetadataRef Scope,
337 const char* LinkageName,
338 LLVMMetadataRef File,
347 LLVMMetadataRef TParam,
348 LLVMMetadataRef Decl) {
349 #if LLVM_VERSION_MINOR >= 8
350 DITemplateParameterArray TParams =
351 DITemplateParameterArray(unwrap<MDTuple>(TParam));
352 DISubprogram *Sub = Builder->createFunction(
353 unwrapDI<DIScope>(Scope), Name, LinkageName,
354 unwrapDI<DIFile>(File), LineNo,
355 unwrapDI<DISubroutineType>(Ty), isLocalToUnit, isDefinition, ScopeLine,
358 unwrapDIptr<DISubprogram>(Decl));
359 unwrap<Function>(Fn)->setSubprogram(Sub);
362 return wrap(Builder->createFunction(
363 unwrapDI<DIScope>(Scope), Name, LinkageName,
364 unwrapDI<DIFile>(File), LineNo,
365 unwrapDI<DISubroutineType>(Ty), isLocalToUnit, isDefinition, ScopeLine,
367 unwrap<Function>(Fn),
368 unwrapDIptr<MDNode>(TParam),
369 unwrapDIptr<MDNode>(Decl)));
373 extern "C" LLVMMetadataRef LLVMDIBuilderCreateBasicType(
374 DIBuilderRef Builder,
377 uint64_t AlignInBits,
379 return wrap(Builder->createBasicType(
381 AlignInBits, Encoding));
384 extern "C" LLVMMetadataRef LLVMDIBuilderCreatePointerType(
385 DIBuilderRef Builder,
386 LLVMMetadataRef PointeeTy,
388 uint64_t AlignInBits,
390 return wrap(Builder->createPointerType(
391 unwrapDI<DIType>(PointeeTy), SizeInBits, AlignInBits, Name));
394 extern "C" LLVMMetadataRef LLVMDIBuilderCreateStructType(
395 DIBuilderRef Builder,
396 LLVMMetadataRef Scope,
398 LLVMMetadataRef File,
401 uint64_t AlignInBits,
403 LLVMMetadataRef DerivedFrom,
404 LLVMMetadataRef Elements,
405 unsigned RunTimeLang,
406 LLVMMetadataRef VTableHolder,
407 const char *UniqueId) {
408 return wrap(Builder->createStructType(
409 unwrapDI<DIDescriptor>(Scope),
411 unwrapDI<DIFile>(File),
416 unwrapDI<DIType>(DerivedFrom),
417 DINodeArray(unwrapDI<MDTuple>(Elements)),
419 unwrapDI<DIType>(VTableHolder),
424 extern "C" LLVMMetadataRef LLVMDIBuilderCreateMemberType(
425 DIBuilderRef Builder,
426 LLVMMetadataRef Scope,
428 LLVMMetadataRef File,
431 uint64_t AlignInBits,
432 uint64_t OffsetInBits,
434 LLVMMetadataRef Ty) {
435 return wrap(Builder->createMemberType(
436 unwrapDI<DIDescriptor>(Scope), Name,
437 unwrapDI<DIFile>(File), LineNo,
438 SizeInBits, AlignInBits, OffsetInBits, Flags,
439 unwrapDI<DIType>(Ty)));
442 extern "C" LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
443 DIBuilderRef Builder,
444 LLVMMetadataRef Scope,
445 LLVMMetadataRef File,
448 return wrap(Builder->createLexicalBlock(
449 unwrapDI<DIDescriptor>(Scope),
450 unwrapDI<DIFile>(File), Line, Col
454 extern "C" LLVMMetadataRef LLVMDIBuilderCreateStaticVariable(
455 DIBuilderRef Builder,
456 LLVMMetadataRef Context,
458 const char* LinkageName,
459 LLVMMetadataRef File,
464 LLVMMetadataRef Decl = NULL) {
465 return wrap(Builder->createGlobalVariable(unwrapDI<DIDescriptor>(Context),
468 unwrapDI<DIFile>(File),
470 unwrapDI<DIType>(Ty),
472 cast<Constant>(unwrap(Val)),
473 unwrapDIptr<MDNode>(Decl)));
476 extern "C" LLVMMetadataRef LLVMDIBuilderCreateVariable(
477 DIBuilderRef Builder,
479 LLVMMetadataRef Scope,
481 LLVMMetadataRef File,
487 unsigned AddrOpsCount,
489 #if LLVM_VERSION_MINOR >= 8
490 if (Tag == 0x100) { // DW_TAG_auto_variable
491 return wrap(Builder->createAutoVariable(
492 unwrapDI<DIDescriptor>(Scope), Name,
493 unwrapDI<DIFile>(File),
495 unwrapDI<DIType>(Ty), AlwaysPreserve, Flags));
497 return wrap(Builder->createParameterVariable(
498 unwrapDI<DIDescriptor>(Scope), Name, ArgNo,
499 unwrapDI<DIFile>(File),
501 unwrapDI<DIType>(Ty), AlwaysPreserve, Flags));
504 return wrap(Builder->createLocalVariable(Tag,
505 unwrapDI<DIDescriptor>(Scope), Name,
506 unwrapDI<DIFile>(File),
508 unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo));
512 extern "C" LLVMMetadataRef LLVMDIBuilderCreateArrayType(
513 DIBuilderRef Builder,
515 uint64_t AlignInBits,
517 LLVMMetadataRef Subscripts) {
518 return wrap(Builder->createArrayType(Size, AlignInBits,
519 unwrapDI<DIType>(Ty),
520 DINodeArray(unwrapDI<MDTuple>(Subscripts))
524 extern "C" LLVMMetadataRef LLVMDIBuilderCreateVectorType(
525 DIBuilderRef Builder,
527 uint64_t AlignInBits,
529 LLVMMetadataRef Subscripts) {
530 return wrap(Builder->createVectorType(Size, AlignInBits,
531 unwrapDI<DIType>(Ty),
532 DINodeArray(unwrapDI<MDTuple>(Subscripts))
536 extern "C" LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(
537 DIBuilderRef Builder,
540 return wrap(Builder->getOrCreateSubrange(Lo, Count));
543 extern "C" LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(
544 DIBuilderRef Builder,
545 LLVMMetadataRef* Ptr,
547 Metadata **DataValue = unwrap(Ptr);
548 return wrap(Builder->getOrCreateArray(
549 ArrayRef<Metadata*>(DataValue, Count)).get());
552 extern "C" LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
553 DIBuilderRef Builder,
555 LLVMMetadataRef VarInfo,
557 unsigned AddrOpsCount,
559 LLVMBasicBlockRef InsertAtEnd) {
560 return wrap(Builder->insertDeclare(
562 unwrap<DILocalVariable>(VarInfo),
563 Builder->createExpression(
564 llvm::ArrayRef<int64_t>(AddrOps, AddrOpsCount)),
565 DebugLoc(cast<MDNode>(unwrap<MetadataAsValue>(DL)->getMetadata())),
566 unwrap(InsertAtEnd)));
569 extern "C" LLVMValueRef LLVMDIBuilderInsertDeclareBefore(
570 DIBuilderRef Builder,
572 LLVMMetadataRef VarInfo,
574 unsigned AddrOpsCount,
576 LLVMValueRef InsertBefore) {
577 return wrap(Builder->insertDeclare(
579 unwrap<DILocalVariable>(VarInfo),
580 Builder->createExpression(
581 llvm::ArrayRef<int64_t>(AddrOps, AddrOpsCount)),
582 DebugLoc(cast<MDNode>(unwrap<MetadataAsValue>(DL)->getMetadata())),
583 unwrap<Instruction>(InsertBefore)));
586 extern "C" LLVMMetadataRef LLVMDIBuilderCreateEnumerator(
587 DIBuilderRef Builder,
591 return wrap(Builder->createEnumerator(Name, Val));
594 extern "C" LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
595 DIBuilderRef Builder,
596 LLVMMetadataRef Scope,
598 LLVMMetadataRef File,
601 uint64_t AlignInBits,
602 LLVMMetadataRef Elements,
603 LLVMMetadataRef ClassType)
605 return wrap(Builder->createEnumerationType(
606 unwrapDI<DIDescriptor>(Scope),
608 unwrapDI<DIFile>(File),
612 DINodeArray(unwrapDI<MDTuple>(Elements)),
613 unwrapDI<DIType>(ClassType)));
616 extern "C" LLVMMetadataRef LLVMDIBuilderCreateUnionType(
617 DIBuilderRef Builder,
618 LLVMMetadataRef Scope,
620 LLVMMetadataRef File,
623 uint64_t AlignInBits,
625 LLVMMetadataRef Elements,
626 unsigned RunTimeLang,
627 const char* UniqueId)
629 return wrap(Builder->createUnionType(
630 unwrapDI<DIDescriptor>(Scope),
632 unwrapDI<DIFile>(File),
637 DINodeArray(unwrapDI<MDTuple>(Elements)),
643 extern "C" LLVMMetadataRef LLVMDIBuilderCreateTemplateTypeParameter(
644 DIBuilderRef Builder,
645 LLVMMetadataRef Scope,
648 LLVMMetadataRef File,
652 return wrap(Builder->createTemplateTypeParameter(
653 unwrapDI<DIDescriptor>(Scope),
659 extern "C" int64_t LLVMDIBuilderCreateOpDeref()
661 return dwarf::DW_OP_deref;
664 extern "C" int64_t LLVMDIBuilderCreateOpPlus()
666 return dwarf::DW_OP_plus;
669 extern "C" LLVMMetadataRef LLVMDIBuilderCreateNameSpace(
670 DIBuilderRef Builder,
671 LLVMMetadataRef Scope,
673 LLVMMetadataRef File,
676 return wrap(Builder->createNameSpace(
677 unwrapDI<DIDescriptor>(Scope),
679 unwrapDI<DIFile>(File),
683 extern "C" void LLVMDICompositeTypeSetTypeArray(
684 DIBuilderRef Builder,
685 LLVMMetadataRef CompositeType,
686 LLVMMetadataRef TypeArray)
688 DICompositeType *tmp = unwrapDI<DICompositeType>(CompositeType);
689 Builder->replaceArrays(tmp, DINodeArray(unwrap<MDTuple>(TypeArray)));
692 extern "C" LLVMValueRef LLVMDIBuilderCreateDebugLocation(
693 LLVMContextRef Context,
696 LLVMMetadataRef Scope,
697 LLVMMetadataRef InlinedAt) {
699 LLVMContext& context = *unwrap(Context);
701 DebugLoc debug_loc = DebugLoc::get(Line,
703 unwrapDIptr<MDNode>(Scope),
704 unwrapDIptr<MDNode>(InlinedAt));
706 return wrap(MetadataAsValue::get(context, debug_loc.getAsMDNode()));
709 extern "C" void LLVMWriteTypeToString(LLVMTypeRef Type, RustStringRef str) {
710 raw_rust_string_ostream os(str);
711 unwrap<llvm::Type>(Type)->print(os);
714 extern "C" void LLVMWriteValueToString(LLVMValueRef Value, RustStringRef str) {
715 raw_rust_string_ostream os(str);
717 unwrap<llvm::Value>(Value)->getType()->print(os);
719 unwrap<llvm::Value>(Value)->print(os);
724 LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
725 Module *Dst = unwrap(dst);
726 std::unique_ptr<MemoryBuffer> buf = MemoryBuffer::getMemBufferCopy(StringRef(bc, len));
727 ErrorOr<std::unique_ptr<Module>> Src =
728 llvm::getLazyBitcodeModule(std::move(buf), Dst->getContext());
730 LLVMRustSetLastError(Src.getError().message().c_str());
736 raw_string_ostream Stream(Err);
737 DiagnosticPrinterRawOStream DP(Stream);
738 #if LLVM_VERSION_MINOR >= 8
739 if (Linker::linkModules(*Dst, std::move(Src.get()))) {
741 if (Linker::LinkModules(Dst, Src->get(), [&](const DiagnosticInfo &DI) { DI.print(DP); })) {
743 LLVMRustSetLastError(Err.c_str());
750 LLVMRustSetDLLStorageClass(LLVMValueRef Value,
751 GlobalValue::DLLStorageClassTypes Class) {
752 GlobalValue *V = unwrap<GlobalValue>(Value);
753 V->setDLLStorageClass(Class);
756 // Note that the two following functions look quite similar to the
757 // LLVMGetSectionName function. Sadly, it appears that this function only
758 // returns a char* pointer, which isn't guaranteed to be null-terminated. The
759 // function provided by LLVM doesn't return the length, so we've created our own
760 // function which returns the length as well as the data pointer.
762 // For an example of this not returning a null terminated string, see
763 // lib/Object/COFFObjectFile.cpp in the getSectionName function. One of the
764 // branches explicitly creates a StringRef without a null terminator, and then
767 inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
768 return reinterpret_cast<section_iterator*>(SI);
772 LLVMRustGetSectionName(LLVMSectionIteratorRef SI, const char **ptr) {
774 if (std::error_code ec = (*unwrap(SI))->getName(ret))
775 report_fatal_error(ec.message());
780 // LLVMArrayType function does not support 64-bit ElementCount
781 extern "C" LLVMTypeRef
782 LLVMRustArrayType(LLVMTypeRef ElementType, uint64_t ElementCount) {
783 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
786 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
787 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DebugLoc, LLVMDebugLocRef)
790 LLVMWriteTwineToString(LLVMTwineRef T, RustStringRef str) {
791 raw_rust_string_ostream os(str);
792 unwrap(T)->print(os);
796 LLVMUnpackOptimizationDiagnostic(
797 LLVMDiagnosticInfoRef di,
798 const char **pass_name_out,
799 LLVMValueRef *function_out,
800 LLVMDebugLocRef *debugloc_out,
801 LLVMTwineRef *message_out)
803 // Undefined to call this not on an optimization diagnostic!
804 llvm::DiagnosticInfoOptimizationBase *opt
805 = static_cast<llvm::DiagnosticInfoOptimizationBase*>(unwrap(di));
807 *pass_name_out = opt->getPassName();
808 *function_out = wrap(&opt->getFunction());
809 *debugloc_out = wrap(&opt->getDebugLoc());
810 *message_out = wrap(&opt->getMsg());
814 LLVMUnpackInlineAsmDiagnostic(
815 LLVMDiagnosticInfoRef di,
816 unsigned *cookie_out,
817 LLVMTwineRef *message_out,
818 LLVMValueRef *instruction_out)
820 // Undefined to call this not on an inline assembly diagnostic!
821 llvm::DiagnosticInfoInlineAsm *ia
822 = static_cast<llvm::DiagnosticInfoInlineAsm*>(unwrap(di));
824 *cookie_out = ia->getLocCookie();
825 *message_out = wrap(&ia->getMsgStr());
826 *instruction_out = wrap(ia->getInstruction());
829 extern "C" void LLVMWriteDiagnosticInfoToString(LLVMDiagnosticInfoRef di, RustStringRef str) {
830 raw_rust_string_ostream os(str);
831 DiagnosticPrinterRawOStream dp(os);
832 unwrap(di)->print(dp);
835 extern "C" int LLVMGetDiagInfoKind(LLVMDiagnosticInfoRef di) {
836 return unwrap(di)->getKind();
839 extern "C" void LLVMWriteDebugLocToString(
844 raw_rust_string_ostream os(str);
845 unwrap(dl)->print(os);
848 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SMDiagnostic, LLVMSMDiagnosticRef)
850 extern "C" void LLVMSetInlineAsmDiagnosticHandler(
852 LLVMContext::InlineAsmDiagHandlerTy H,
855 unwrap(C)->setInlineAsmDiagnosticHandler(H, CX);
858 extern "C" void LLVMWriteSMDiagnosticToString(LLVMSMDiagnosticRef d, RustStringRef str) {
859 raw_rust_string_ostream os(str);
860 unwrap(d)->print("", os);
863 extern "C" LLVMValueRef
864 LLVMRustBuildLandingPad(LLVMBuilderRef Builder,
870 return LLVMBuildLandingPad(Builder, Ty, PersFn, NumClauses, Name);
873 extern "C" LLVMValueRef
874 LLVMRustBuildCleanupPad(LLVMBuilderRef Builder,
875 LLVMValueRef ParentPad,
877 LLVMValueRef *LLArgs,
879 #if LLVM_VERSION_MINOR >= 8
880 Value **Args = unwrap(LLArgs);
881 if (ParentPad == NULL) {
882 Type *Ty = Type::getTokenTy(unwrap(Builder)->getContext());
883 ParentPad = wrap(Constant::getNullValue(Ty));
885 return wrap(unwrap(Builder)->CreateCleanupPad(unwrap(ParentPad),
886 ArrayRef<Value*>(Args, ArgCnt),
893 extern "C" LLVMValueRef
894 LLVMRustBuildCleanupRet(LLVMBuilderRef Builder,
895 LLVMValueRef CleanupPad,
896 LLVMBasicBlockRef UnwindBB) {
897 #if LLVM_VERSION_MINOR >= 8
898 CleanupPadInst *Inst = cast<CleanupPadInst>(unwrap(CleanupPad));
899 return wrap(unwrap(Builder)->CreateCleanupRet(Inst, unwrap(UnwindBB)));
905 extern "C" LLVMValueRef
906 LLVMRustBuildCatchPad(LLVMBuilderRef Builder,
907 LLVMValueRef ParentPad,
909 LLVMValueRef *LLArgs,
911 #if LLVM_VERSION_MINOR >= 8
912 Value **Args = unwrap(LLArgs);
913 return wrap(unwrap(Builder)->CreateCatchPad(unwrap(ParentPad),
914 ArrayRef<Value*>(Args, ArgCnt),
921 extern "C" LLVMValueRef
922 LLVMRustBuildCatchRet(LLVMBuilderRef Builder,
924 LLVMBasicBlockRef BB) {
925 #if LLVM_VERSION_MINOR >= 8
926 return wrap(unwrap(Builder)->CreateCatchRet(cast<CatchPadInst>(unwrap(Pad)),
933 extern "C" LLVMValueRef
934 LLVMRustBuildCatchSwitch(LLVMBuilderRef Builder,
935 LLVMValueRef ParentPad,
936 LLVMBasicBlockRef BB,
937 unsigned NumHandlers,
939 #if LLVM_VERSION_MINOR >= 8
940 if (ParentPad == NULL) {
941 Type *Ty = Type::getTokenTy(unwrap(Builder)->getContext());
942 ParentPad = wrap(Constant::getNullValue(Ty));
944 return wrap(unwrap(Builder)->CreateCatchSwitch(unwrap(ParentPad),
954 LLVMRustAddHandler(LLVMValueRef CatchSwitchRef,
955 LLVMBasicBlockRef Handler) {
956 #if LLVM_VERSION_MINOR >= 8
957 Value *CatchSwitch = unwrap(CatchSwitchRef);
958 cast<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Handler));
963 LLVMRustSetPersonalityFn(LLVMBuilderRef B,
964 LLVMValueRef Personality) {
965 #if LLVM_VERSION_MINOR >= 8
966 unwrap(B)->GetInsertBlock()
968 ->setPersonalityFn(cast<Function>(unwrap(Personality)));
972 #if LLVM_VERSION_MINOR >= 8
973 extern "C" OperandBundleDef*
974 LLVMRustBuildOperandBundleDef(const char *Name,
975 LLVMValueRef *Inputs,
976 unsigned NumInputs) {
977 return new OperandBundleDef(Name, makeArrayRef(unwrap(Inputs), NumInputs));
981 LLVMRustFreeOperandBundleDef(OperandBundleDef* Bundle) {
985 extern "C" LLVMValueRef
986 LLVMRustBuildCall(LLVMBuilderRef B,
990 OperandBundleDef *Bundle,
992 unsigned len = Bundle ? 1 : 0;
993 ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, len);
994 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
995 makeArrayRef(unwrap(Args), NumArgs),
1000 extern "C" LLVMValueRef
1001 LLVMRustBuildInvoke(LLVMBuilderRef B,
1005 LLVMBasicBlockRef Then,
1006 LLVMBasicBlockRef Catch,
1007 OperandBundleDef *Bundle,
1009 unsigned len = Bundle ? 1 : 0;
1010 ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, len);
1011 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1012 makeArrayRef(unwrap(Args), NumArgs),
1018 LLVMRustBuildOperandBundleDef(const char *Name,
1019 LLVMValueRef *Inputs,
1020 unsigned NumInputs) {
1025 LLVMRustFreeOperandBundleDef(void* Bundle) {
1028 extern "C" LLVMValueRef
1029 LLVMRustBuildCall(LLVMBuilderRef B,
1035 return LLVMBuildCall(B, Fn, Args, NumArgs, Name);
1038 extern "C" LLVMValueRef
1039 LLVMRustBuildInvoke(LLVMBuilderRef B,
1043 LLVMBasicBlockRef Then,
1044 LLVMBasicBlockRef Catch,
1047 return LLVMBuildInvoke(B, Fn, Args, NumArgs, Then, Catch, Name);
1051 extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B, LLVMBasicBlockRef BB) {
1052 auto point = unwrap(BB)->getFirstInsertionPt();
1053 unwrap(B)->SetInsertPoint(unwrap(BB), point);
1056 extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V, const char *Name) {
1057 Triple TargetTriple(unwrap(M)->getTargetTriple());
1058 GlobalObject *GV = unwrap<GlobalObject>(V);
1059 if (!TargetTriple.isOSBinFormatMachO()) {
1060 GV->setComdat(unwrap(M)->getOrInsertComdat(Name));
1064 extern "C" void LLVMRustUnsetComdat(LLVMValueRef V) {
1065 GlobalObject *GV = unwrap<GlobalObject>(V);
1066 GV->setComdat(nullptr);