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"
16 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/CallSite.h"
20 //===----------------------------------------------------------------------===
22 // This file defines alternate interfaces to core functions that are more
23 // readily callable by Rust's FFI.
25 //===----------------------------------------------------------------------===
28 using namespace llvm::sys;
29 using namespace llvm::object;
31 // LLVMAtomicOrdering is already an enum - don't create another
33 static AtomicOrdering from_rust(LLVMAtomicOrdering Ordering) {
35 case LLVMAtomicOrderingNotAtomic:
36 return AtomicOrdering::NotAtomic;
37 case LLVMAtomicOrderingUnordered:
38 return AtomicOrdering::Unordered;
39 case LLVMAtomicOrderingMonotonic:
40 return AtomicOrdering::Monotonic;
41 case LLVMAtomicOrderingAcquire:
42 return AtomicOrdering::Acquire;
43 case LLVMAtomicOrderingRelease:
44 return AtomicOrdering::Release;
45 case LLVMAtomicOrderingAcquireRelease:
46 return AtomicOrdering::AcquireRelease;
47 case LLVMAtomicOrderingSequentiallyConsistent:
48 return AtomicOrdering::SequentiallyConsistent;
51 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
55 static char *LastError;
57 extern "C" LLVMMemoryBufferRef
58 LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
59 ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(Path,
63 LLVMRustSetLastError(buf_or.getError().message().c_str());
66 return wrap(buf_or.get().release());
69 extern "C" char *LLVMRustGetLastError(void) {
70 char *ret = LastError;
75 void LLVMRustSetLastError(const char *err) {
76 free((void*) LastError);
77 LastError = strdup(err);
81 LLVMRustSetNormalizedTarget(LLVMModuleRef M, const char *triple) {
82 unwrap(M)->setTargetTriple(Triple::normalize(triple));
85 extern "C" void LLVMRustPrintPassTimings() {
86 raw_fd_ostream OS (2, false); // stderr.
87 TimerGroup::printAll(OS);
90 extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M,
92 return wrap(unwrap(M)->getNamedValue(Name));
95 extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
97 LLVMTypeRef FunctionTy) {
98 return wrap(unwrap(M)->getOrInsertFunction(Name,
99 unwrap<FunctionType>(FunctionTy)));
102 extern "C" LLVMValueRef LLVMRustGetOrInsertGlobal(LLVMModuleRef M,
105 return wrap(unwrap(M)->getOrInsertGlobal(Name, unwrap(Ty)));
108 extern "C" LLVMTypeRef LLVMRustMetadataTypeInContext(LLVMContextRef C) {
109 return wrap(Type::getMetadataTy(*unwrap(C)));
112 extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned index, uint64_t Val) {
113 CallSite Call = CallSite(unwrap<Instruction>(Instr));
117 Call.getAttributes().addAttributes(Call->getContext(), index,
118 AttributeSet::get(Call->getContext(),
123 extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
127 CallSite Call = CallSite(unwrap<Instruction>(Instr));
129 B.addDereferenceableAttr(b);
131 Call.getAttributes().addAttributes(Call->getContext(), idx,
132 AttributeSet::get(Call->getContext(),
136 extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn,
140 Function *A = unwrap<Function>(Fn);
143 A->addAttributes(index, AttributeSet::get(A->getContext(), index, B));
146 extern "C" void LLVMRustAddDereferenceableAttr(LLVMValueRef Fn,
150 Function *A = unwrap<Function>(Fn);
152 B.addDereferenceableAttr(bytes);
153 A->addAttributes(index, AttributeSet::get(A->getContext(), index, B));
156 extern "C" void LLVMRustAddFunctionAttrString(LLVMValueRef Fn,
160 Function *F = unwrap<Function>(Fn);
162 B.addAttribute(Name);
163 F->addAttributes(index, AttributeSet::get(F->getContext(), index, B));
166 extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
170 Function *F = unwrap<Function>(Fn);
172 B.addAttribute(Name, Value);
173 F->addAttributes(index, AttributeSet::get(F->getContext(), index, B));
176 extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
180 Function *A = unwrap<Function>(Fn);
181 const AttributeSet PAL = A->getAttributes();
183 const AttributeSet PALnew =
184 PAL.removeAttributes(A->getContext(), index,
185 AttributeSet::get(A->getContext(), index, B));
186 A->setAttributes(PALnew);
189 extern "C" void LLVMRustRemoveFunctionAttrString(LLVMValueRef fn,
193 Function *f = unwrap<Function>(fn);
194 LLVMContext &C = f->getContext();
196 B.addAttribute(Name);
197 AttributeSet to_remove = AttributeSet::get(C, index, B);
199 AttributeSet attrs = f->getAttributes();
200 f->setAttributes(attrs.removeAttributes(f->getContext(),
205 // enable fpmath flag UnsafeAlgebra
206 extern "C" void LLVMRustSetHasUnsafeAlgebra(LLVMValueRef V) {
207 if (auto I = dyn_cast<Instruction>(unwrap<Value>(V))) {
208 I->setHasUnsafeAlgebra(true);
212 extern "C" LLVMValueRef LLVMRustBuildAtomicLoad(LLVMBuilderRef B,
215 LLVMAtomicOrdering order,
216 unsigned alignment) {
217 LoadInst* li = new LoadInst(unwrap(source),0);
218 li->setAtomic(from_rust(order));
219 li->setAlignment(alignment);
220 return wrap(unwrap(B)->Insert(li, Name));
223 extern "C" LLVMValueRef LLVMRustBuildAtomicStore(LLVMBuilderRef B,
226 LLVMAtomicOrdering order,
227 unsigned alignment) {
228 StoreInst* si = new StoreInst(unwrap(val),unwrap(target));
229 si->setAtomic(from_rust(order));
230 si->setAlignment(alignment);
231 return wrap(unwrap(B)->Insert(si));
234 extern "C" LLVMValueRef LLVMRustBuildAtomicCmpXchg(LLVMBuilderRef B,
238 LLVMAtomicOrdering order,
239 LLVMAtomicOrdering failure_order,
241 AtomicCmpXchgInst* acxi = unwrap(B)->CreateAtomicCmpXchg(
246 from_rust(failure_order));
251 enum class LLVMRustSynchronizationScope {
257 static SynchronizationScope
258 from_rust(LLVMRustSynchronizationScope scope)
261 case LLVMRustSynchronizationScope::SingleThread:
263 case LLVMRustSynchronizationScope::CrossThread:
270 extern "C" LLVMValueRef LLVMRustBuildAtomicFence(
272 LLVMAtomicOrdering order,
273 LLVMRustSynchronizationScope scope)
275 return wrap(unwrap(B)->CreateFence(from_rust(order), from_rust(scope)));
278 extern "C" void LLVMRustSetDebug(int Enabled) {
284 extern "C" LLVMValueRef LLVMRustInlineAsm(LLVMTypeRef Ty,
287 LLVMBool HasSideEffects,
288 LLVMBool IsAlignStack,
290 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty), AsmString,
291 Constraints, HasSideEffects,
292 IsAlignStack, (InlineAsm::AsmDialect) Dialect));
295 typedef DIBuilder* LLVMRustDIBuilderRef;
297 typedef struct LLVMOpaqueMetadata *LLVMRustMetadataRef;
300 DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMRustMetadataRef)
302 inline Metadata **unwrap(LLVMRustMetadataRef *Vals) {
303 return reinterpret_cast<Metadata**>(Vals);
307 template<typename DIT>
308 DIT* unwrapDIptr(LLVMRustMetadataRef ref) {
309 return (DIT*) (ref ? unwrap<MDNode>(ref) : NULL);
312 #define DIDescriptor DIScope
313 #define DIArray DINodeArray
314 #define unwrapDI unwrapDIptr
316 extern "C" uint32_t LLVMRustDebugMetadataVersion() {
317 return DEBUG_METADATA_VERSION;
320 extern "C" uint32_t LLVMRustVersionMinor() {
321 return LLVM_VERSION_MINOR;
324 extern "C" uint32_t LLVMRustVersionMajor() {
325 return LLVM_VERSION_MAJOR;
328 extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M,
331 unwrap(M)->addModuleFlag(Module::Warning, name, value);
334 extern "C" LLVMRustDIBuilderRef LLVMRustDIBuilderCreate(LLVMModuleRef M) {
335 return new DIBuilder(*unwrap(M));
338 extern "C" void LLVMRustDIBuilderDispose(LLVMRustDIBuilderRef Builder) {
342 extern "C" void LLVMRustDIBuilderFinalize(LLVMRustDIBuilderRef Builder) {
346 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateCompileUnit(
347 LLVMRustDIBuilderRef Builder,
351 const char* Producer,
355 const char* SplitName) {
356 return wrap(Builder->createCompileUnit(Lang,
366 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateFile(
367 LLVMRustDIBuilderRef Builder,
368 const char* Filename,
369 const char* Directory) {
370 return wrap(Builder->createFile(Filename, Directory));
373 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateSubroutineType(
374 LLVMRustDIBuilderRef Builder,
375 LLVMRustMetadataRef File,
376 LLVMRustMetadataRef ParameterTypes) {
377 return wrap(Builder->createSubroutineType(
378 #if LLVM_VERSION_MINOR == 7
379 unwrapDI<DIFile>(File),
381 DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
384 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateFunction(
385 LLVMRustDIBuilderRef Builder,
386 LLVMRustMetadataRef Scope,
388 const char* LinkageName,
389 LLVMRustMetadataRef File,
391 LLVMRustMetadataRef Ty,
398 LLVMRustMetadataRef TParam,
399 LLVMRustMetadataRef Decl) {
400 #if LLVM_VERSION_MINOR >= 8
401 DITemplateParameterArray TParams =
402 DITemplateParameterArray(unwrap<MDTuple>(TParam));
403 DISubprogram *Sub = Builder->createFunction(
404 unwrapDI<DIScope>(Scope), Name, LinkageName,
405 unwrapDI<DIFile>(File), LineNo,
406 unwrapDI<DISubroutineType>(Ty), isLocalToUnit, isDefinition, ScopeLine,
409 unwrapDIptr<DISubprogram>(Decl));
410 unwrap<Function>(Fn)->setSubprogram(Sub);
413 return wrap(Builder->createFunction(
414 unwrapDI<DIScope>(Scope), Name, LinkageName,
415 unwrapDI<DIFile>(File), LineNo,
416 unwrapDI<DISubroutineType>(Ty), isLocalToUnit, isDefinition, ScopeLine,
418 unwrap<Function>(Fn),
419 unwrapDIptr<MDNode>(TParam),
420 unwrapDIptr<MDNode>(Decl)));
424 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateBasicType(
425 LLVMRustDIBuilderRef Builder,
428 uint64_t AlignInBits,
430 return wrap(Builder->createBasicType(
432 AlignInBits, Encoding));
435 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreatePointerType(
436 LLVMRustDIBuilderRef Builder,
437 LLVMRustMetadataRef PointeeTy,
439 uint64_t AlignInBits,
441 return wrap(Builder->createPointerType(
442 unwrapDI<DIType>(PointeeTy), SizeInBits, AlignInBits, Name));
445 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateStructType(
446 LLVMRustDIBuilderRef Builder,
447 LLVMRustMetadataRef Scope,
449 LLVMRustMetadataRef File,
452 uint64_t AlignInBits,
454 LLVMRustMetadataRef DerivedFrom,
455 LLVMRustMetadataRef Elements,
456 unsigned RunTimeLang,
457 LLVMRustMetadataRef VTableHolder,
458 const char *UniqueId) {
459 return wrap(Builder->createStructType(
460 unwrapDI<DIDescriptor>(Scope),
462 unwrapDI<DIFile>(File),
467 unwrapDI<DIType>(DerivedFrom),
468 DINodeArray(unwrapDI<MDTuple>(Elements)),
470 unwrapDI<DIType>(VTableHolder),
475 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateMemberType(
476 LLVMRustDIBuilderRef Builder,
477 LLVMRustMetadataRef Scope,
479 LLVMRustMetadataRef File,
482 uint64_t AlignInBits,
483 uint64_t OffsetInBits,
485 LLVMRustMetadataRef Ty) {
486 return wrap(Builder->createMemberType(
487 unwrapDI<DIDescriptor>(Scope), Name,
488 unwrapDI<DIFile>(File), LineNo,
489 SizeInBits, AlignInBits, OffsetInBits, Flags,
490 unwrapDI<DIType>(Ty)));
493 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateLexicalBlock(
494 LLVMRustDIBuilderRef Builder,
495 LLVMRustMetadataRef Scope,
496 LLVMRustMetadataRef File,
499 return wrap(Builder->createLexicalBlock(
500 unwrapDI<DIDescriptor>(Scope),
501 unwrapDI<DIFile>(File), Line, Col
505 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateStaticVariable(
506 LLVMRustDIBuilderRef Builder,
507 LLVMRustMetadataRef Context,
509 const char* LinkageName,
510 LLVMRustMetadataRef File,
512 LLVMRustMetadataRef Ty,
515 LLVMRustMetadataRef Decl = NULL) {
516 return wrap(Builder->createGlobalVariable(unwrapDI<DIDescriptor>(Context),
519 unwrapDI<DIFile>(File),
521 unwrapDI<DIType>(Ty),
523 cast<Constant>(unwrap(Val)),
524 unwrapDIptr<MDNode>(Decl)));
527 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateVariable(
528 LLVMRustDIBuilderRef Builder,
530 LLVMRustMetadataRef Scope,
532 LLVMRustMetadataRef File,
534 LLVMRustMetadataRef Ty,
538 unsigned AddrOpsCount,
540 #if LLVM_VERSION_MINOR >= 8
541 if (Tag == 0x100) { // DW_TAG_auto_variable
542 return wrap(Builder->createAutoVariable(
543 unwrapDI<DIDescriptor>(Scope), Name,
544 unwrapDI<DIFile>(File),
546 unwrapDI<DIType>(Ty), AlwaysPreserve, Flags));
548 return wrap(Builder->createParameterVariable(
549 unwrapDI<DIDescriptor>(Scope), Name, ArgNo,
550 unwrapDI<DIFile>(File),
552 unwrapDI<DIType>(Ty), AlwaysPreserve, Flags));
555 return wrap(Builder->createLocalVariable(Tag,
556 unwrapDI<DIDescriptor>(Scope), Name,
557 unwrapDI<DIFile>(File),
559 unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo));
563 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateArrayType(
564 LLVMRustDIBuilderRef Builder,
566 uint64_t AlignInBits,
567 LLVMRustMetadataRef Ty,
568 LLVMRustMetadataRef Subscripts) {
569 return wrap(Builder->createArrayType(Size, AlignInBits,
570 unwrapDI<DIType>(Ty),
571 DINodeArray(unwrapDI<MDTuple>(Subscripts))
575 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateVectorType(
576 LLVMRustDIBuilderRef Builder,
578 uint64_t AlignInBits,
579 LLVMRustMetadataRef Ty,
580 LLVMRustMetadataRef Subscripts) {
581 return wrap(Builder->createVectorType(Size, AlignInBits,
582 unwrapDI<DIType>(Ty),
583 DINodeArray(unwrapDI<MDTuple>(Subscripts))
587 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderGetOrCreateSubrange(
588 LLVMRustDIBuilderRef Builder,
591 return wrap(Builder->getOrCreateSubrange(Lo, Count));
594 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderGetOrCreateArray(
595 LLVMRustDIBuilderRef Builder,
596 LLVMRustMetadataRef* Ptr,
598 Metadata **DataValue = unwrap(Ptr);
599 return wrap(Builder->getOrCreateArray(
600 ArrayRef<Metadata*>(DataValue, Count)).get());
603 extern "C" LLVMValueRef LLVMRustDIBuilderInsertDeclareAtEnd(
604 LLVMRustDIBuilderRef Builder,
606 LLVMRustMetadataRef VarInfo,
608 unsigned AddrOpsCount,
610 LLVMBasicBlockRef InsertAtEnd) {
611 return wrap(Builder->insertDeclare(
613 unwrap<DILocalVariable>(VarInfo),
614 Builder->createExpression(
615 llvm::ArrayRef<int64_t>(AddrOps, AddrOpsCount)),
616 DebugLoc(cast<MDNode>(unwrap<MetadataAsValue>(DL)->getMetadata())),
617 unwrap(InsertAtEnd)));
620 extern "C" LLVMValueRef LLVMRustDIBuilderInsertDeclareBefore(
621 LLVMRustDIBuilderRef Builder,
623 LLVMRustMetadataRef VarInfo,
625 unsigned AddrOpsCount,
627 LLVMValueRef InsertBefore) {
628 return wrap(Builder->insertDeclare(
630 unwrap<DILocalVariable>(VarInfo),
631 Builder->createExpression(
632 llvm::ArrayRef<int64_t>(AddrOps, AddrOpsCount)),
633 DebugLoc(cast<MDNode>(unwrap<MetadataAsValue>(DL)->getMetadata())),
634 unwrap<Instruction>(InsertBefore)));
637 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateEnumerator(
638 LLVMRustDIBuilderRef Builder,
642 return wrap(Builder->createEnumerator(Name, Val));
645 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateEnumerationType(
646 LLVMRustDIBuilderRef Builder,
647 LLVMRustMetadataRef Scope,
649 LLVMRustMetadataRef File,
652 uint64_t AlignInBits,
653 LLVMRustMetadataRef Elements,
654 LLVMRustMetadataRef ClassType)
656 return wrap(Builder->createEnumerationType(
657 unwrapDI<DIDescriptor>(Scope),
659 unwrapDI<DIFile>(File),
663 DINodeArray(unwrapDI<MDTuple>(Elements)),
664 unwrapDI<DIType>(ClassType)));
667 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateUnionType(
668 LLVMRustDIBuilderRef Builder,
669 LLVMRustMetadataRef Scope,
671 LLVMRustMetadataRef File,
674 uint64_t AlignInBits,
676 LLVMRustMetadataRef Elements,
677 unsigned RunTimeLang,
678 const char* UniqueId)
680 return wrap(Builder->createUnionType(
681 unwrapDI<DIDescriptor>(Scope),
683 unwrapDI<DIFile>(File),
688 DINodeArray(unwrapDI<MDTuple>(Elements)),
694 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateTemplateTypeParameter(
695 LLVMRustDIBuilderRef Builder,
696 LLVMRustMetadataRef Scope,
698 LLVMRustMetadataRef Ty,
699 LLVMRustMetadataRef File,
703 return wrap(Builder->createTemplateTypeParameter(
704 unwrapDI<DIDescriptor>(Scope),
710 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateNameSpace(
711 LLVMRustDIBuilderRef Builder,
712 LLVMRustMetadataRef Scope,
714 LLVMRustMetadataRef File,
717 return wrap(Builder->createNameSpace(
718 unwrapDI<DIDescriptor>(Scope),
720 unwrapDI<DIFile>(File),
724 extern "C" void LLVMRustDICompositeTypeSetTypeArray(
725 LLVMRustDIBuilderRef Builder,
726 LLVMRustMetadataRef CompositeType,
727 LLVMRustMetadataRef TypeArray)
729 DICompositeType *tmp = unwrapDI<DICompositeType>(CompositeType);
730 Builder->replaceArrays(tmp, DINodeArray(unwrap<MDTuple>(TypeArray)));
733 extern "C" LLVMValueRef LLVMRustDIBuilderCreateDebugLocation(
734 LLVMContextRef Context,
737 LLVMRustMetadataRef Scope,
738 LLVMRustMetadataRef InlinedAt)
740 LLVMContext& context = *unwrap(Context);
742 DebugLoc debug_loc = DebugLoc::get(Line,
744 unwrapDIptr<MDNode>(Scope),
745 unwrapDIptr<MDNode>(InlinedAt));
747 return wrap(MetadataAsValue::get(context, debug_loc.getAsMDNode()));
750 extern "C" int64_t LLVMRustDIBuilderCreateOpDeref()
752 return dwarf::DW_OP_deref;
755 extern "C" int64_t LLVMRustDIBuilderCreateOpPlus()
757 return dwarf::DW_OP_plus;
760 extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Type, RustStringRef str) {
761 raw_rust_string_ostream os(str);
762 unwrap<llvm::Type>(Type)->print(os);
765 extern "C" void LLVMRustWriteValueToString(LLVMValueRef Value, RustStringRef str) {
766 raw_rust_string_ostream os(str);
768 unwrap<llvm::Value>(Value)->getType()->print(os);
770 unwrap<llvm::Value>(Value)->print(os);
775 LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
776 Module *Dst = unwrap(dst);
777 std::unique_ptr<MemoryBuffer> buf = MemoryBuffer::getMemBufferCopy(StringRef(bc, len));
778 ErrorOr<std::unique_ptr<Module>> Src =
779 llvm::getLazyBitcodeModule(std::move(buf), Dst->getContext());
781 LLVMRustSetLastError(Src.getError().message().c_str());
787 raw_string_ostream Stream(Err);
788 DiagnosticPrinterRawOStream DP(Stream);
789 #if LLVM_VERSION_MINOR >= 8
790 if (Linker::linkModules(*Dst, std::move(Src.get()))) {
792 if (Linker::LinkModules(Dst, Src->get(), [&](const DiagnosticInfo &DI) { DI.print(DP); })) {
794 LLVMRustSetLastError(Err.c_str());
800 enum class LLVMRustDLLStorageClassTypes {
807 static GlobalValue::DLLStorageClassTypes
808 from_rust(LLVMRustDLLStorageClassTypes Class)
811 case LLVMRustDLLStorageClassTypes::Default:
812 return GlobalValue::DefaultStorageClass;
813 case LLVMRustDLLStorageClassTypes::DllImport:
814 return GlobalValue::DLLImportStorageClass;
815 case LLVMRustDLLStorageClassTypes::DllExport:
816 return GlobalValue::DLLExportStorageClass;
823 LLVMRustSetDLLStorageClass(LLVMValueRef Value,
824 LLVMRustDLLStorageClassTypes Class) {
825 GlobalValue *V = unwrap<GlobalValue>(Value);
826 V->setDLLStorageClass(from_rust(Class));
829 // Note that the two following functions look quite similar to the
830 // LLVMGetSectionName function. Sadly, it appears that this function only
831 // returns a char* pointer, which isn't guaranteed to be null-terminated. The
832 // function provided by LLVM doesn't return the length, so we've created our own
833 // function which returns the length as well as the data pointer.
835 // For an example of this not returning a null terminated string, see
836 // lib/Object/COFFObjectFile.cpp in the getSectionName function. One of the
837 // branches explicitly creates a StringRef without a null terminator, and then
840 inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
841 return reinterpret_cast<section_iterator*>(SI);
845 LLVMRustGetSectionName(LLVMSectionIteratorRef SI, const char **ptr) {
847 if (std::error_code ec = (*unwrap(SI))->getName(ret))
848 report_fatal_error(ec.message());
853 // LLVMArrayType function does not support 64-bit ElementCount
854 extern "C" LLVMTypeRef
855 LLVMRustArrayType(LLVMTypeRef ElementType, uint64_t ElementCount) {
856 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
859 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
860 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DebugLoc, LLVMDebugLocRef)
863 LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef str) {
864 raw_rust_string_ostream os(str);
865 unwrap(T)->print(os);
869 LLVMRustUnpackOptimizationDiagnostic(
870 LLVMDiagnosticInfoRef di,
871 const char **pass_name_out,
872 LLVMValueRef *function_out,
873 LLVMDebugLocRef *debugloc_out,
874 LLVMTwineRef *message_out)
876 // Undefined to call this not on an optimization diagnostic!
877 llvm::DiagnosticInfoOptimizationBase *opt
878 = static_cast<llvm::DiagnosticInfoOptimizationBase*>(unwrap(di));
880 *pass_name_out = opt->getPassName();
881 *function_out = wrap(&opt->getFunction());
882 *debugloc_out = wrap(&opt->getDebugLoc());
883 *message_out = wrap(&opt->getMsg());
887 LLVMRustUnpackInlineAsmDiagnostic(
888 LLVMDiagnosticInfoRef di,
889 unsigned *cookie_out,
890 LLVMTwineRef *message_out,
891 LLVMValueRef *instruction_out)
893 // Undefined to call this not on an inline assembly diagnostic!
894 llvm::DiagnosticInfoInlineAsm *ia
895 = static_cast<llvm::DiagnosticInfoInlineAsm*>(unwrap(di));
897 *cookie_out = ia->getLocCookie();
898 *message_out = wrap(&ia->getMsgStr());
899 *instruction_out = wrap(ia->getInstruction());
902 extern "C" void LLVMRustWriteDiagnosticInfoToString(LLVMDiagnosticInfoRef di, RustStringRef str) {
903 raw_rust_string_ostream os(str);
904 DiagnosticPrinterRawOStream dp(os);
905 unwrap(di)->print(dp);
908 enum class LLVMRustDiagnosticKind {
912 DebugMetadataVersion,
915 OptimizationRemarkMissed,
916 OptimizationRemarkAnalysis,
917 OptimizationRemarkAnalysisFPCommute,
918 OptimizationRemarkAnalysisAliasing,
919 OptimizationRemarkOther,
923 static LLVMRustDiagnosticKind
924 to_rust(DiagnosticKind kind)
928 return LLVMRustDiagnosticKind::InlineAsm;
930 return LLVMRustDiagnosticKind::StackSize;
931 case DK_DebugMetadataVersion:
932 return LLVMRustDiagnosticKind::DebugMetadataVersion;
933 case DK_SampleProfile:
934 return LLVMRustDiagnosticKind::SampleProfile;
935 case DK_OptimizationRemark:
936 return LLVMRustDiagnosticKind::OptimizationRemark;
937 case DK_OptimizationRemarkMissed:
938 return LLVMRustDiagnosticKind::OptimizationRemarkMissed;
939 case DK_OptimizationRemarkAnalysis:
940 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysis;
941 #if LLVM_VERSION_MINOR >= 8
942 case DK_OptimizationRemarkAnalysisFPCommute:
943 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysisFPCommute;
944 case DK_OptimizationRemarkAnalysisAliasing:
945 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysisAliasing;
948 #if LLVM_VERSION_MINOR >= 9
949 return (kind >= DK_FirstRemark && kind <= DK_LastRemark) ?
950 LLVMRustDiagnosticKind::OptimizationRemarkOther :
951 LLVMRustDiagnosticKind::Other;
953 return LLVMRustDiagnosticKind::Other;
959 extern "C" LLVMRustDiagnosticKind LLVMRustGetDiagInfoKind(LLVMDiagnosticInfoRef di) {
960 return to_rust((DiagnosticKind) unwrap(di)->getKind());
963 extern "C" void LLVMRustWriteDebugLocToString(
968 raw_rust_string_ostream os(str);
969 unwrap(dl)->print(os);
972 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SMDiagnostic, LLVMSMDiagnosticRef)
974 // FIXME(type-audit): assume this function-pointer type does not change
975 extern "C" void LLVMRustSetInlineAsmDiagnosticHandler(
977 LLVMContext::InlineAsmDiagHandlerTy H,
980 unwrap(C)->setInlineAsmDiagnosticHandler(H, CX);
983 extern "C" void LLVMRustWriteSMDiagnosticToString(LLVMSMDiagnosticRef d,
985 raw_rust_string_ostream os(str);
986 unwrap(d)->print("", os);
989 extern "C" LLVMValueRef
990 LLVMRustBuildLandingPad(LLVMBuilderRef Builder,
996 return LLVMBuildLandingPad(Builder, Ty, PersFn, NumClauses, Name);
999 extern "C" LLVMValueRef
1000 LLVMRustBuildCleanupPad(LLVMBuilderRef Builder,
1001 LLVMValueRef ParentPad,
1003 LLVMValueRef *LLArgs,
1005 #if LLVM_VERSION_MINOR >= 8
1006 Value **Args = unwrap(LLArgs);
1007 if (ParentPad == NULL) {
1008 Type *Ty = Type::getTokenTy(unwrap(Builder)->getContext());
1009 ParentPad = wrap(Constant::getNullValue(Ty));
1011 return wrap(unwrap(Builder)->CreateCleanupPad(unwrap(ParentPad),
1012 ArrayRef<Value*>(Args, ArgCnt),
1019 extern "C" LLVMValueRef
1020 LLVMRustBuildCleanupRet(LLVMBuilderRef Builder,
1021 LLVMValueRef CleanupPad,
1022 LLVMBasicBlockRef UnwindBB) {
1023 #if LLVM_VERSION_MINOR >= 8
1024 CleanupPadInst *Inst = cast<CleanupPadInst>(unwrap(CleanupPad));
1025 return wrap(unwrap(Builder)->CreateCleanupRet(Inst, unwrap(UnwindBB)));
1033 extern "C" LLVMValueRef
1034 LLVMRustBuildCatchPad(LLVMBuilderRef Builder,
1035 LLVMValueRef ParentPad,
1037 LLVMValueRef *LLArgs,
1039 #if LLVM_VERSION_MINOR >= 8
1040 Value **Args = unwrap(LLArgs);
1041 return wrap(unwrap(Builder)->CreateCatchPad(unwrap(ParentPad),
1042 ArrayRef<Value*>(Args, ArgCnt),
1049 extern "C" LLVMValueRef
1050 LLVMRustBuildCatchRet(LLVMBuilderRef Builder,
1052 LLVMBasicBlockRef BB) {
1053 #if LLVM_VERSION_MINOR >= 8
1054 return wrap(unwrap(Builder)->CreateCatchRet(cast<CatchPadInst>(unwrap(Pad)),
1061 extern "C" LLVMValueRef
1062 LLVMRustBuildCatchSwitch(LLVMBuilderRef Builder,
1063 LLVMValueRef ParentPad,
1064 LLVMBasicBlockRef BB,
1065 unsigned NumHandlers,
1067 #if LLVM_VERSION_MINOR >= 8
1068 if (ParentPad == NULL) {
1069 Type *Ty = Type::getTokenTy(unwrap(Builder)->getContext());
1070 ParentPad = wrap(Constant::getNullValue(Ty));
1072 return wrap(unwrap(Builder)->CreateCatchSwitch(unwrap(ParentPad),
1082 LLVMRustAddHandler(LLVMValueRef CatchSwitchRef,
1083 LLVMBasicBlockRef Handler) {
1084 #if LLVM_VERSION_MINOR >= 8
1085 Value *CatchSwitch = unwrap(CatchSwitchRef);
1086 cast<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Handler));
1091 LLVMRustSetPersonalityFn(LLVMBuilderRef B,
1092 LLVMValueRef Personality) {
1093 #if LLVM_VERSION_MINOR >= 8
1094 unwrap(B)->GetInsertBlock()
1096 ->setPersonalityFn(cast<Function>(unwrap(Personality)));
1100 #if LLVM_VERSION_MINOR >= 8
1101 extern "C" OperandBundleDef*
1102 LLVMRustBuildOperandBundleDef(const char *Name,
1103 LLVMValueRef *Inputs,
1104 unsigned NumInputs) {
1105 return new OperandBundleDef(Name, makeArrayRef(unwrap(Inputs), NumInputs));
1109 LLVMRustFreeOperandBundleDef(OperandBundleDef* Bundle) {
1113 extern "C" LLVMValueRef
1114 LLVMRustBuildCall(LLVMBuilderRef B,
1118 OperandBundleDef *Bundle,
1120 unsigned len = Bundle ? 1 : 0;
1121 ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, len);
1122 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
1123 makeArrayRef(unwrap(Args), NumArgs),
1128 extern "C" LLVMValueRef
1129 LLVMRustBuildInvoke(LLVMBuilderRef B,
1133 LLVMBasicBlockRef Then,
1134 LLVMBasicBlockRef Catch,
1135 OperandBundleDef *Bundle,
1137 unsigned len = Bundle ? 1 : 0;
1138 ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, len);
1139 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1140 makeArrayRef(unwrap(Args), NumArgs),
1146 LLVMRustBuildOperandBundleDef(const char *Name,
1147 LLVMValueRef *Inputs,
1148 unsigned NumInputs) {
1153 LLVMRustFreeOperandBundleDef(void* Bundle) {
1156 extern "C" LLVMValueRef
1157 LLVMRustBuildCall(LLVMBuilderRef B,
1163 return LLVMBuildCall(B, Fn, Args, NumArgs, Name);
1166 extern "C" LLVMValueRef
1167 LLVMRustBuildInvoke(LLVMBuilderRef B,
1171 LLVMBasicBlockRef Then,
1172 LLVMBasicBlockRef Catch,
1175 return LLVMBuildInvoke(B, Fn, Args, NumArgs, Then, Catch, Name);
1179 extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B, LLVMBasicBlockRef BB) {
1180 auto point = unwrap(BB)->getFirstInsertionPt();
1181 unwrap(B)->SetInsertPoint(unwrap(BB), point);
1184 extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V, const char *Name) {
1185 Triple TargetTriple(unwrap(M)->getTargetTriple());
1186 GlobalObject *GV = unwrap<GlobalObject>(V);
1187 if (!TargetTriple.isOSBinFormatMachO()) {
1188 GV->setComdat(unwrap(M)->getOrInsertComdat(Name));
1192 extern "C" void LLVMRustUnsetComdat(LLVMValueRef V) {
1193 GlobalObject *GV = unwrap<GlobalObject>(V);
1194 GV->setComdat(nullptr);