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/IR/DebugInfoMetadata.h"
13 #include "llvm/IR/DiagnosticInfo.h"
14 #include "llvm/IR/DiagnosticPrinter.h"
15 #include "llvm/IR/GlobalVariable.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Bitcode/BitcodeWriterPass.h"
20 #include "llvm/Support/Signals.h"
22 #include "llvm/IR/CallSite.h"
24 #if LLVM_VERSION_GE(5, 0)
25 #include "llvm/ADT/Optional.h"
32 //===----------------------------------------------------------------------===
34 // This file defines alternate interfaces to core functions that are more
35 // readily callable by Rust's FFI.
37 //===----------------------------------------------------------------------===
40 using namespace llvm::sys;
41 using namespace llvm::object;
43 // LLVMAtomicOrdering is already an enum - don't create another
45 static AtomicOrdering fromRust(LLVMAtomicOrdering Ordering) {
47 case LLVMAtomicOrderingNotAtomic:
48 return AtomicOrdering::NotAtomic;
49 case LLVMAtomicOrderingUnordered:
50 return AtomicOrdering::Unordered;
51 case LLVMAtomicOrderingMonotonic:
52 return AtomicOrdering::Monotonic;
53 case LLVMAtomicOrderingAcquire:
54 return AtomicOrdering::Acquire;
55 case LLVMAtomicOrderingRelease:
56 return AtomicOrdering::Release;
57 case LLVMAtomicOrderingAcquireRelease:
58 return AtomicOrdering::AcquireRelease;
59 case LLVMAtomicOrderingSequentiallyConsistent:
60 return AtomicOrdering::SequentiallyConsistent;
63 report_fatal_error("Invalid LLVMAtomicOrdering value!");
66 static LLVM_THREAD_LOCAL char *LastError;
68 // Custom error handler for fatal LLVM errors.
70 // Notably it exits the process with code 101, unlike LLVM's default of 1.
71 static void FatalErrorHandler(void *UserData,
72 const std::string& Reason,
74 // Do the same thing that the default error handler does.
75 std::cerr << "LLVM ERROR: " << Reason << std::endl;
77 // Since this error handler exits the process, we have to run any cleanup that
78 // LLVM would run after handling the error. This might change with an LLVM
80 sys::RunInterruptHandlers();
85 extern "C" void LLVMRustInstallFatalErrorHandler() {
86 install_fatal_error_handler(FatalErrorHandler);
89 extern "C" LLVMMemoryBufferRef
90 LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
91 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOr =
92 MemoryBuffer::getFile(Path, -1, false);
94 LLVMRustSetLastError(BufOr.getError().message().c_str());
97 return wrap(BufOr.get().release());
100 extern "C" char *LLVMRustGetLastError(void) {
101 char *Ret = LastError;
106 extern "C" void LLVMRustSetLastError(const char *Err) {
107 free((void *)LastError);
108 LastError = strdup(Err);
111 extern "C" LLVMContextRef LLVMRustContextCreate(bool shouldDiscardNames) {
112 auto ctx = new LLVMContext();
113 ctx->setDiscardValueNames(shouldDiscardNames);
117 extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
118 const char *Triple) {
119 unwrap(M)->setTargetTriple(Triple::normalize(Triple));
122 extern "C" void LLVMRustPrintPassTimings() {
123 raw_fd_ostream OS(2, false); // stderr.
124 TimerGroup::printAll(OS);
127 extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M,
129 return wrap(unwrap(M)->getNamedValue(Name));
132 extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
134 LLVMTypeRef FunctionTy) {
136 unwrap(M)->getOrInsertFunction(Name, unwrap<FunctionType>(FunctionTy)));
139 extern "C" LLVMValueRef
140 LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
141 return wrap(unwrap(M)->getOrInsertGlobal(Name, unwrap(Ty)));
144 extern "C" LLVMValueRef
145 LLVMRustInsertPrivateGlobal(LLVMModuleRef M, LLVMTypeRef Ty) {
146 return wrap(new GlobalVariable(*unwrap(M),
149 GlobalValue::PrivateLinkage,
153 extern "C" LLVMTypeRef LLVMRustMetadataTypeInContext(LLVMContextRef C) {
154 return wrap(Type::getMetadataTy(*unwrap(C)));
157 static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
160 return Attribute::AlwaysInline;
162 return Attribute::ByVal;
164 return Attribute::Cold;
166 return Attribute::InlineHint;
168 return Attribute::MinSize;
170 return Attribute::Naked;
172 return Attribute::NoAlias;
174 return Attribute::NoCapture;
176 return Attribute::NoInline;
178 return Attribute::NonNull;
180 return Attribute::NoRedZone;
182 return Attribute::NoReturn;
184 return Attribute::NoUnwind;
185 case OptimizeForSize:
186 return Attribute::OptimizeForSize;
188 return Attribute::ReadOnly;
190 return Attribute::SExt;
192 return Attribute::StructRet;
194 return Attribute::UWTable;
196 return Attribute::ZExt;
198 return Attribute::InReg;
200 return Attribute::SanitizeThread;
201 case SanitizeAddress:
202 return Attribute::SanitizeAddress;
204 return Attribute::SanitizeMemory;
206 return Attribute::NonLazyBind;
208 report_fatal_error("bad AttributeKind");
211 extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned Index,
212 LLVMRustAttribute RustAttr) {
213 CallSite Call = CallSite(unwrap<Instruction>(Instr));
214 Attribute Attr = Attribute::get(Call->getContext(), fromRust(RustAttr));
215 #if LLVM_VERSION_GE(5, 0)
216 Call.addAttribute(Index, Attr);
219 Call.setAttributes(Call.getAttributes().addAttributes(
220 Call->getContext(), Index,
221 AttributeSet::get(Call->getContext(), Index, B)));
225 extern "C" void LLVMRustAddAlignmentCallSiteAttr(LLVMValueRef Instr,
228 CallSite Call = CallSite(unwrap<Instruction>(Instr));
230 B.addAlignmentAttr(Bytes);
231 #if LLVM_VERSION_GE(5, 0)
232 Call.setAttributes(Call.getAttributes().addAttributes(
233 Call->getContext(), Index, B));
235 Call.setAttributes(Call.getAttributes().addAttributes(
236 Call->getContext(), Index,
237 AttributeSet::get(Call->getContext(), Index, B)));
241 extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
244 CallSite Call = CallSite(unwrap<Instruction>(Instr));
246 B.addDereferenceableAttr(Bytes);
247 #if LLVM_VERSION_GE(5, 0)
248 Call.setAttributes(Call.getAttributes().addAttributes(
249 Call->getContext(), Index, B));
251 Call.setAttributes(Call.getAttributes().addAttributes(
252 Call->getContext(), Index,
253 AttributeSet::get(Call->getContext(), Index, B)));
257 extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr,
260 CallSite Call = CallSite(unwrap<Instruction>(Instr));
262 B.addDereferenceableOrNullAttr(Bytes);
263 #if LLVM_VERSION_GE(5, 0)
264 Call.setAttributes(Call.getAttributes().addAttributes(
265 Call->getContext(), Index, B));
267 Call.setAttributes(Call.getAttributes().addAttributes(
268 Call->getContext(), Index,
269 AttributeSet::get(Call->getContext(), Index, B)));
273 extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn, unsigned Index,
274 LLVMRustAttribute RustAttr) {
275 Function *A = unwrap<Function>(Fn);
276 Attribute Attr = Attribute::get(A->getContext(), fromRust(RustAttr));
278 #if LLVM_VERSION_GE(5, 0)
279 A->addAttributes(Index, B);
281 A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
285 extern "C" void LLVMRustAddAlignmentAttr(LLVMValueRef Fn,
288 Function *A = unwrap<Function>(Fn);
290 B.addAlignmentAttr(Bytes);
291 #if LLVM_VERSION_GE(5, 0)
292 A->addAttributes(Index, B);
294 A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
298 extern "C" void LLVMRustAddDereferenceableAttr(LLVMValueRef Fn, unsigned Index,
300 Function *A = unwrap<Function>(Fn);
302 B.addDereferenceableAttr(Bytes);
303 #if LLVM_VERSION_GE(5, 0)
304 A->addAttributes(Index, B);
306 A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
310 extern "C" void LLVMRustAddDereferenceableOrNullAttr(LLVMValueRef Fn,
313 Function *A = unwrap<Function>(Fn);
315 B.addDereferenceableOrNullAttr(Bytes);
316 #if LLVM_VERSION_GE(5, 0)
317 A->addAttributes(Index, B);
319 A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
323 extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
327 Function *F = unwrap<Function>(Fn);
329 B.addAttribute(Name, Value);
330 #if LLVM_VERSION_GE(5, 0)
331 F->addAttributes(Index, B);
333 F->addAttributes(Index, AttributeSet::get(F->getContext(), Index, B));
337 extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
339 LLVMRustAttribute RustAttr) {
340 Function *F = unwrap<Function>(Fn);
341 Attribute Attr = Attribute::get(F->getContext(), fromRust(RustAttr));
343 auto PAL = F->getAttributes();
344 #if LLVM_VERSION_GE(5, 0)
345 auto PALNew = PAL.removeAttributes(F->getContext(), Index, B);
347 const AttributeSet PALNew = PAL.removeAttributes(
348 F->getContext(), Index, AttributeSet::get(F->getContext(), Index, B));
350 F->setAttributes(PALNew);
353 // enable fpmath flag UnsafeAlgebra
354 extern "C" void LLVMRustSetHasUnsafeAlgebra(LLVMValueRef V) {
355 if (auto I = dyn_cast<Instruction>(unwrap<Value>(V))) {
356 #if LLVM_VERSION_GE(6, 0)
359 I->setHasUnsafeAlgebra(true);
364 extern "C" LLVMValueRef
365 LLVMRustBuildAtomicLoad(LLVMBuilderRef B, LLVMValueRef Source, const char *Name,
366 LLVMAtomicOrdering Order) {
367 LoadInst *LI = new LoadInst(unwrap(Source), 0);
368 LI->setAtomic(fromRust(Order));
369 return wrap(unwrap(B)->Insert(LI, Name));
372 extern "C" LLVMValueRef LLVMRustBuildAtomicStore(LLVMBuilderRef B,
375 LLVMAtomicOrdering Order) {
376 StoreInst *SI = new StoreInst(unwrap(V), unwrap(Target));
377 SI->setAtomic(fromRust(Order));
378 return wrap(unwrap(B)->Insert(SI));
381 extern "C" LLVMValueRef
382 LLVMRustBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Target,
383 LLVMValueRef Old, LLVMValueRef Source,
384 LLVMAtomicOrdering Order,
385 LLVMAtomicOrdering FailureOrder, LLVMBool Weak) {
386 AtomicCmpXchgInst *ACXI = unwrap(B)->CreateAtomicCmpXchg(
387 unwrap(Target), unwrap(Old), unwrap(Source), fromRust(Order),
388 fromRust(FailureOrder));
393 enum class LLVMRustSynchronizationScope {
399 #if LLVM_VERSION_GE(5, 0)
400 static SyncScope::ID fromRust(LLVMRustSynchronizationScope Scope) {
402 case LLVMRustSynchronizationScope::SingleThread:
403 return SyncScope::SingleThread;
404 case LLVMRustSynchronizationScope::CrossThread:
405 return SyncScope::System;
407 report_fatal_error("bad SynchronizationScope.");
411 static SynchronizationScope fromRust(LLVMRustSynchronizationScope Scope) {
413 case LLVMRustSynchronizationScope::SingleThread:
415 case LLVMRustSynchronizationScope::CrossThread:
418 report_fatal_error("bad SynchronizationScope.");
423 extern "C" LLVMValueRef
424 LLVMRustBuildAtomicFence(LLVMBuilderRef B, LLVMAtomicOrdering Order,
425 LLVMRustSynchronizationScope Scope) {
426 return wrap(unwrap(B)->CreateFence(fromRust(Order), fromRust(Scope)));
429 enum class LLVMRustAsmDialect {
435 static InlineAsm::AsmDialect fromRust(LLVMRustAsmDialect Dialect) {
437 case LLVMRustAsmDialect::Att:
438 return InlineAsm::AD_ATT;
439 case LLVMRustAsmDialect::Intel:
440 return InlineAsm::AD_Intel;
442 report_fatal_error("bad AsmDialect.");
446 extern "C" LLVMValueRef LLVMRustInlineAsm(LLVMTypeRef Ty, char *AsmString,
448 LLVMBool HasSideEffects,
449 LLVMBool IsAlignStack,
450 LLVMRustAsmDialect Dialect) {
451 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty), AsmString, Constraints,
452 HasSideEffects, IsAlignStack, fromRust(Dialect)));
455 extern "C" bool LLVMRustInlineAsmVerify(LLVMTypeRef Ty,
457 return InlineAsm::Verify(unwrap<FunctionType>(Ty), Constraints);
460 extern "C" void LLVMRustAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
461 unwrap(M)->appendModuleInlineAsm(StringRef(Asm));
464 typedef DIBuilder *LLVMRustDIBuilderRef;
466 #if LLVM_VERSION_LT(5, 0)
467 typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
470 DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)
472 inline Metadata **unwrap(LLVMMetadataRef *Vals) {
473 return reinterpret_cast<Metadata **>(Vals);
478 template <typename DIT> DIT *unwrapDIPtr(LLVMMetadataRef Ref) {
479 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
482 #define DIDescriptor DIScope
483 #define DIArray DINodeArray
484 #define unwrapDI unwrapDIPtr
486 // These values **must** match debuginfo::DIFlags! They also *happen*
487 // to match LLVM, but that isn't required as we do giant sets of
488 // matching below. The value shouldn't be directly passed to LLVM.
489 enum class LLVMRustDIFlags : uint32_t {
494 FlagFwdDecl = (1 << 2),
495 FlagAppleBlock = (1 << 3),
496 FlagBlockByrefStruct = (1 << 4),
497 FlagVirtual = (1 << 5),
498 FlagArtificial = (1 << 6),
499 FlagExplicit = (1 << 7),
500 FlagPrototyped = (1 << 8),
501 FlagObjcClassComplete = (1 << 9),
502 FlagObjectPointer = (1 << 10),
503 FlagVector = (1 << 11),
504 FlagStaticMember = (1 << 12),
505 FlagLValueReference = (1 << 13),
506 FlagRValueReference = (1 << 14),
507 FlagExternalTypeRef = (1 << 15),
508 FlagIntroducedVirtual = (1 << 18),
509 FlagBitField = (1 << 19),
510 FlagNoReturn = (1 << 20),
511 FlagMainSubprogram = (1 << 21),
512 // Do not add values that are not supported by the minimum LLVM
513 // version we support! see llvm/include/llvm/IR/DebugInfoFlags.def
516 inline LLVMRustDIFlags operator&(LLVMRustDIFlags A, LLVMRustDIFlags B) {
517 return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(A) &
518 static_cast<uint32_t>(B));
521 inline LLVMRustDIFlags operator|(LLVMRustDIFlags A, LLVMRustDIFlags B) {
522 return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(A) |
523 static_cast<uint32_t>(B));
526 inline LLVMRustDIFlags &operator|=(LLVMRustDIFlags &A, LLVMRustDIFlags B) {
530 inline bool isSet(LLVMRustDIFlags F) { return F != LLVMRustDIFlags::FlagZero; }
532 inline LLVMRustDIFlags visibility(LLVMRustDIFlags F) {
533 return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(F) & 0x3);
536 static DINode::DIFlags fromRust(LLVMRustDIFlags Flags) {
537 DINode::DIFlags Result = DINode::DIFlags::FlagZero;
539 switch (visibility(Flags)) {
540 case LLVMRustDIFlags::FlagPrivate:
541 Result |= DINode::DIFlags::FlagPrivate;
543 case LLVMRustDIFlags::FlagProtected:
544 Result |= DINode::DIFlags::FlagProtected;
546 case LLVMRustDIFlags::FlagPublic:
547 Result |= DINode::DIFlags::FlagPublic;
550 // The rest are handled below
554 if (isSet(Flags & LLVMRustDIFlags::FlagFwdDecl)) {
555 Result |= DINode::DIFlags::FlagFwdDecl;
557 if (isSet(Flags & LLVMRustDIFlags::FlagAppleBlock)) {
558 Result |= DINode::DIFlags::FlagAppleBlock;
560 if (isSet(Flags & LLVMRustDIFlags::FlagBlockByrefStruct)) {
561 Result |= DINode::DIFlags::FlagBlockByrefStruct;
563 if (isSet(Flags & LLVMRustDIFlags::FlagVirtual)) {
564 Result |= DINode::DIFlags::FlagVirtual;
566 if (isSet(Flags & LLVMRustDIFlags::FlagArtificial)) {
567 Result |= DINode::DIFlags::FlagArtificial;
569 if (isSet(Flags & LLVMRustDIFlags::FlagExplicit)) {
570 Result |= DINode::DIFlags::FlagExplicit;
572 if (isSet(Flags & LLVMRustDIFlags::FlagPrototyped)) {
573 Result |= DINode::DIFlags::FlagPrototyped;
575 if (isSet(Flags & LLVMRustDIFlags::FlagObjcClassComplete)) {
576 Result |= DINode::DIFlags::FlagObjcClassComplete;
578 if (isSet(Flags & LLVMRustDIFlags::FlagObjectPointer)) {
579 Result |= DINode::DIFlags::FlagObjectPointer;
581 if (isSet(Flags & LLVMRustDIFlags::FlagVector)) {
582 Result |= DINode::DIFlags::FlagVector;
584 if (isSet(Flags & LLVMRustDIFlags::FlagStaticMember)) {
585 Result |= DINode::DIFlags::FlagStaticMember;
587 if (isSet(Flags & LLVMRustDIFlags::FlagLValueReference)) {
588 Result |= DINode::DIFlags::FlagLValueReference;
590 if (isSet(Flags & LLVMRustDIFlags::FlagRValueReference)) {
591 Result |= DINode::DIFlags::FlagRValueReference;
593 #if LLVM_VERSION_LE(4, 0)
594 if (isSet(Flags & LLVMRustDIFlags::FlagExternalTypeRef)) {
595 Result |= DINode::DIFlags::FlagExternalTypeRef;
598 if (isSet(Flags & LLVMRustDIFlags::FlagIntroducedVirtual)) {
599 Result |= DINode::DIFlags::FlagIntroducedVirtual;
601 if (isSet(Flags & LLVMRustDIFlags::FlagBitField)) {
602 Result |= DINode::DIFlags::FlagBitField;
604 if (isSet(Flags & LLVMRustDIFlags::FlagNoReturn)) {
605 Result |= DINode::DIFlags::FlagNoReturn;
607 if (isSet(Flags & LLVMRustDIFlags::FlagMainSubprogram)) {
608 Result |= DINode::DIFlags::FlagMainSubprogram;
614 extern "C" uint32_t LLVMRustDebugMetadataVersion() {
615 return DEBUG_METADATA_VERSION;
618 extern "C" uint32_t LLVMRustVersionMinor() { return LLVM_VERSION_MINOR; }
620 extern "C" uint32_t LLVMRustVersionMajor() { return LLVM_VERSION_MAJOR; }
622 extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M, const char *Name,
624 unwrap(M)->addModuleFlag(Module::Warning, Name, Value);
627 extern "C" LLVMValueRef LLVMRustMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
628 return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
631 extern "C" LLVMRustDIBuilderRef LLVMRustDIBuilderCreate(LLVMModuleRef M) {
632 return new DIBuilder(*unwrap(M));
635 extern "C" void LLVMRustDIBuilderDispose(LLVMRustDIBuilderRef Builder) {
639 extern "C" void LLVMRustDIBuilderFinalize(LLVMRustDIBuilderRef Builder) {
643 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateCompileUnit(
644 LLVMRustDIBuilderRef Builder, unsigned Lang, LLVMMetadataRef FileRef,
645 const char *Producer, bool isOptimized, const char *Flags,
646 unsigned RuntimeVer, const char *SplitName) {
647 auto *File = unwrapDI<DIFile>(FileRef);
649 return wrap(Builder->createCompileUnit(Lang, File, Producer, isOptimized,
650 Flags, RuntimeVer, SplitName));
653 extern "C" LLVMMetadataRef
654 LLVMRustDIBuilderCreateFile(LLVMRustDIBuilderRef Builder, const char *Filename,
655 const char *Directory) {
656 return wrap(Builder->createFile(Filename, Directory));
659 extern "C" LLVMMetadataRef
660 LLVMRustDIBuilderCreateSubroutineType(LLVMRustDIBuilderRef Builder,
661 LLVMMetadataRef File,
662 LLVMMetadataRef ParameterTypes) {
663 return wrap(Builder->createSubroutineType(
664 DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
667 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFunction(
668 LLVMRustDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
669 const char *LinkageName, LLVMMetadataRef File, unsigned LineNo,
670 LLVMMetadataRef Ty, bool IsLocalToUnit, bool IsDefinition,
671 unsigned ScopeLine, LLVMRustDIFlags Flags, bool IsOptimized,
672 LLVMValueRef Fn, LLVMMetadataRef TParam, LLVMMetadataRef Decl) {
673 DITemplateParameterArray TParams =
674 DITemplateParameterArray(unwrap<MDTuple>(TParam));
675 DISubprogram *Sub = Builder->createFunction(
676 unwrapDI<DIScope>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
677 LineNo, unwrapDI<DISubroutineType>(Ty), IsLocalToUnit, IsDefinition,
678 ScopeLine, fromRust(Flags), IsOptimized, TParams,
679 unwrapDIPtr<DISubprogram>(Decl));
680 unwrap<Function>(Fn)->setSubprogram(Sub);
684 extern "C" LLVMMetadataRef
685 LLVMRustDIBuilderCreateBasicType(LLVMRustDIBuilderRef Builder, const char *Name,
686 uint64_t SizeInBits, uint32_t AlignInBits,
688 return wrap(Builder->createBasicType(Name, SizeInBits, Encoding));
691 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreatePointerType(
692 LLVMRustDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
693 uint64_t SizeInBits, uint32_t AlignInBits, const char *Name) {
694 return wrap(Builder->createPointerType(unwrapDI<DIType>(PointeeTy),
695 SizeInBits, AlignInBits,
696 #if LLVM_VERSION_GE(5, 0)
697 /* DWARFAddressSpace */ None,
702 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateStructType(
703 LLVMRustDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
704 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
705 uint32_t AlignInBits, LLVMRustDIFlags Flags,
706 LLVMMetadataRef DerivedFrom, LLVMMetadataRef Elements,
707 unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
708 const char *UniqueId) {
709 return wrap(Builder->createStructType(
710 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
711 SizeInBits, AlignInBits, fromRust(Flags), unwrapDI<DIType>(DerivedFrom),
712 DINodeArray(unwrapDI<MDTuple>(Elements)), RunTimeLang,
713 unwrapDI<DIType>(VTableHolder), UniqueId));
716 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateMemberType(
717 LLVMRustDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
718 LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
719 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMRustDIFlags Flags,
720 LLVMMetadataRef Ty) {
721 return wrap(Builder->createMemberType(unwrapDI<DIDescriptor>(Scope), Name,
722 unwrapDI<DIFile>(File), LineNo,
723 SizeInBits, AlignInBits, OffsetInBits,
724 fromRust(Flags), unwrapDI<DIType>(Ty)));
727 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateLexicalBlock(
728 LLVMRustDIBuilderRef Builder, LLVMMetadataRef Scope,
729 LLVMMetadataRef File, unsigned Line, unsigned Col) {
730 return wrap(Builder->createLexicalBlock(unwrapDI<DIDescriptor>(Scope),
731 unwrapDI<DIFile>(File), Line, Col));
734 extern "C" LLVMMetadataRef
735 LLVMRustDIBuilderCreateLexicalBlockFile(LLVMRustDIBuilderRef Builder,
736 LLVMMetadataRef Scope,
737 LLVMMetadataRef File) {
738 return wrap(Builder->createLexicalBlockFile(unwrapDI<DIDescriptor>(Scope),
739 unwrapDI<DIFile>(File)));
742 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateStaticVariable(
743 LLVMRustDIBuilderRef Builder, LLVMMetadataRef Context, const char *Name,
744 const char *LinkageName, LLVMMetadataRef File, unsigned LineNo,
745 LLVMMetadataRef Ty, bool IsLocalToUnit, LLVMValueRef V,
746 LLVMMetadataRef Decl = nullptr, uint32_t AlignInBits = 0) {
747 llvm::GlobalVariable *InitVal = cast<llvm::GlobalVariable>(unwrap(V));
749 llvm::DIExpression *InitExpr = nullptr;
750 if (llvm::ConstantInt *IntVal = llvm::dyn_cast<llvm::ConstantInt>(InitVal)) {
751 InitExpr = Builder->createConstantValueExpression(
752 IntVal->getValue().getSExtValue());
753 } else if (llvm::ConstantFP *FPVal =
754 llvm::dyn_cast<llvm::ConstantFP>(InitVal)) {
755 InitExpr = Builder->createConstantValueExpression(
756 FPVal->getValueAPF().bitcastToAPInt().getZExtValue());
759 llvm::DIGlobalVariableExpression *VarExpr = Builder->createGlobalVariableExpression(
760 unwrapDI<DIDescriptor>(Context), Name, LinkageName,
761 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), IsLocalToUnit,
762 InitExpr, unwrapDIPtr<MDNode>(Decl), AlignInBits);
764 InitVal->setMetadata("dbg", VarExpr);
766 return wrap(VarExpr);
769 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariable(
770 LLVMRustDIBuilderRef Builder, unsigned Tag, LLVMMetadataRef Scope,
771 const char *Name, LLVMMetadataRef File, unsigned LineNo,
772 LLVMMetadataRef Ty, bool AlwaysPreserve, LLVMRustDIFlags Flags,
773 unsigned ArgNo, uint32_t AlignInBits) {
774 if (Tag == 0x100) { // DW_TAG_auto_variable
775 return wrap(Builder->createAutoVariable(
776 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNo,
777 unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags), AlignInBits));
779 return wrap(Builder->createParameterVariable(
780 unwrapDI<DIDescriptor>(Scope), Name, ArgNo, unwrapDI<DIFile>(File),
781 LineNo, unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags)));
785 extern "C" LLVMMetadataRef
786 LLVMRustDIBuilderCreateArrayType(LLVMRustDIBuilderRef Builder, uint64_t Size,
787 uint32_t AlignInBits, LLVMMetadataRef Ty,
788 LLVMMetadataRef Subscripts) {
790 Builder->createArrayType(Size, AlignInBits, unwrapDI<DIType>(Ty),
791 DINodeArray(unwrapDI<MDTuple>(Subscripts))));
794 extern "C" LLVMMetadataRef
795 LLVMRustDIBuilderGetOrCreateSubrange(LLVMRustDIBuilderRef Builder, int64_t Lo,
797 return wrap(Builder->getOrCreateSubrange(Lo, Count));
800 extern "C" LLVMMetadataRef
801 LLVMRustDIBuilderGetOrCreateArray(LLVMRustDIBuilderRef Builder,
802 LLVMMetadataRef *Ptr, unsigned Count) {
803 Metadata **DataValue = unwrap(Ptr);
805 Builder->getOrCreateArray(ArrayRef<Metadata *>(DataValue, Count)).get());
808 extern "C" LLVMValueRef LLVMRustDIBuilderInsertDeclareAtEnd(
809 LLVMRustDIBuilderRef Builder, LLVMValueRef V, LLVMMetadataRef VarInfo,
810 int64_t *AddrOps, unsigned AddrOpsCount, LLVMValueRef DL,
811 LLVMBasicBlockRef InsertAtEnd) {
812 return wrap(Builder->insertDeclare(
813 unwrap(V), unwrap<DILocalVariable>(VarInfo),
814 Builder->createExpression(llvm::ArrayRef<int64_t>(AddrOps, AddrOpsCount)),
815 DebugLoc(cast<MDNode>(unwrap<MetadataAsValue>(DL)->getMetadata())),
816 unwrap(InsertAtEnd)));
819 extern "C" LLVMMetadataRef
820 LLVMRustDIBuilderCreateEnumerator(LLVMRustDIBuilderRef Builder,
821 const char *Name, uint64_t Val) {
822 return wrap(Builder->createEnumerator(Name, Val));
825 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateEnumerationType(
826 LLVMRustDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
827 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
828 uint32_t AlignInBits, LLVMMetadataRef Elements,
829 LLVMMetadataRef ClassTy) {
830 return wrap(Builder->createEnumerationType(
831 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
832 SizeInBits, AlignInBits, DINodeArray(unwrapDI<MDTuple>(Elements)),
833 unwrapDI<DIType>(ClassTy)));
836 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateUnionType(
837 LLVMRustDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
838 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
839 uint32_t AlignInBits, LLVMRustDIFlags Flags, LLVMMetadataRef Elements,
840 unsigned RunTimeLang, const char *UniqueId) {
841 return wrap(Builder->createUnionType(
842 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
843 SizeInBits, AlignInBits, fromRust(Flags),
844 DINodeArray(unwrapDI<MDTuple>(Elements)), RunTimeLang, UniqueId));
847 extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateTemplateTypeParameter(
848 LLVMRustDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
849 LLVMMetadataRef Ty, LLVMMetadataRef File, unsigned LineNo,
851 return wrap(Builder->createTemplateTypeParameter(
852 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIType>(Ty)));
855 extern "C" LLVMMetadataRef
856 LLVMRustDIBuilderCreateNameSpace(LLVMRustDIBuilderRef Builder,
857 LLVMMetadataRef Scope, const char *Name,
858 LLVMMetadataRef File, unsigned LineNo) {
859 return wrap(Builder->createNameSpace(
860 unwrapDI<DIDescriptor>(Scope), Name
861 #if LLVM_VERSION_LT(5, 0)
863 unwrapDI<DIFile>(File), LineNo
865 #if LLVM_VERSION_GE(4, 0)
867 false // ExportSymbols (only relevant for C++ anonymous namespaces)
873 LLVMRustDICompositeTypeSetTypeArray(LLVMRustDIBuilderRef Builder,
874 LLVMMetadataRef CompositeTy,
875 LLVMMetadataRef TyArray) {
876 DICompositeType *Tmp = unwrapDI<DICompositeType>(CompositeTy);
877 Builder->replaceArrays(Tmp, DINodeArray(unwrap<MDTuple>(TyArray)));
880 extern "C" LLVMValueRef
881 LLVMRustDIBuilderCreateDebugLocation(LLVMContextRef ContextRef, unsigned Line,
882 unsigned Column, LLVMMetadataRef Scope,
883 LLVMMetadataRef InlinedAt) {
884 LLVMContext &Context = *unwrap(ContextRef);
886 DebugLoc debug_loc = DebugLoc::get(Line, Column, unwrapDIPtr<MDNode>(Scope),
887 unwrapDIPtr<MDNode>(InlinedAt));
889 return wrap(MetadataAsValue::get(Context, debug_loc.getAsMDNode()));
892 extern "C" int64_t LLVMRustDIBuilderCreateOpDeref() {
893 return dwarf::DW_OP_deref;
896 extern "C" int64_t LLVMRustDIBuilderCreateOpPlusUconst() {
897 #if LLVM_VERSION_GE(5, 0)
898 return dwarf::DW_OP_plus_uconst;
900 // older LLVM used `plus` to behave like `plus_uconst`.
901 return dwarf::DW_OP_plus;
905 extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
906 RawRustStringOstream OS(Str);
907 unwrap<llvm::Type>(Ty)->print(OS);
910 extern "C" void LLVMRustWriteValueToString(LLVMValueRef V,
912 RawRustStringOstream OS(Str);
917 unwrap<llvm::Value>(V)->getType()->print(OS);
919 unwrap<llvm::Value>(V)->print(OS);
924 // Note that the two following functions look quite similar to the
925 // LLVMGetSectionName function. Sadly, it appears that this function only
926 // returns a char* pointer, which isn't guaranteed to be null-terminated. The
927 // function provided by LLVM doesn't return the length, so we've created our own
928 // function which returns the length as well as the data pointer.
930 // For an example of this not returning a null terminated string, see
931 // lib/Object/COFFObjectFile.cpp in the getSectionName function. One of the
932 // branches explicitly creates a StringRef without a null terminator, and then
935 inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
936 return reinterpret_cast<section_iterator *>(SI);
939 extern "C" size_t LLVMRustGetSectionName(LLVMSectionIteratorRef SI,
942 if (std::error_code EC = (*unwrap(SI))->getName(Ret))
943 report_fatal_error(EC.message());
948 // LLVMArrayType function does not support 64-bit ElementCount
949 extern "C" LLVMTypeRef LLVMRustArrayType(LLVMTypeRef ElementTy,
950 uint64_t ElementCount) {
951 return wrap(ArrayType::get(unwrap(ElementTy), ElementCount));
954 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
956 extern "C" void LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef Str) {
957 RawRustStringOstream OS(Str);
958 unwrap(T)->print(OS);
961 extern "C" void LLVMRustUnpackOptimizationDiagnostic(
962 LLVMDiagnosticInfoRef DI, RustStringRef PassNameOut,
963 LLVMValueRef *FunctionOut, unsigned* Line, unsigned* Column,
964 RustStringRef FilenameOut, RustStringRef MessageOut) {
965 // Undefined to call this not on an optimization diagnostic!
966 llvm::DiagnosticInfoOptimizationBase *Opt =
967 static_cast<llvm::DiagnosticInfoOptimizationBase *>(unwrap(DI));
969 RawRustStringOstream PassNameOS(PassNameOut);
970 PassNameOS << Opt->getPassName();
971 *FunctionOut = wrap(&Opt->getFunction());
973 RawRustStringOstream FilenameOS(FilenameOut);
974 #if LLVM_VERSION_GE(5,0)
975 DiagnosticLocation loc = Opt->getLocation();
977 *Line = loc.getLine();
978 *Column = loc.getColumn();
979 FilenameOS << loc.getFilename();
982 const DebugLoc &loc = Opt->getDebugLoc();
984 *Line = loc.getLine();
985 *Column = loc.getCol();
986 FilenameOS << cast<DIScope>(loc.getScope())->getFilename();
990 RawRustStringOstream MessageOS(MessageOut);
991 MessageOS << Opt->getMsg();
995 LLVMRustUnpackInlineAsmDiagnostic(LLVMDiagnosticInfoRef DI, unsigned *CookieOut,
996 LLVMTwineRef *MessageOut,
997 LLVMValueRef *InstructionOut) {
998 // Undefined to call this not on an inline assembly diagnostic!
999 llvm::DiagnosticInfoInlineAsm *IA =
1000 static_cast<llvm::DiagnosticInfoInlineAsm *>(unwrap(DI));
1002 *CookieOut = IA->getLocCookie();
1003 *MessageOut = wrap(&IA->getMsgStr());
1004 *InstructionOut = wrap(IA->getInstruction());
1007 extern "C" void LLVMRustWriteDiagnosticInfoToString(LLVMDiagnosticInfoRef DI,
1008 RustStringRef Str) {
1009 RawRustStringOstream OS(Str);
1010 DiagnosticPrinterRawOStream DP(OS);
1011 unwrap(DI)->print(DP);
1014 enum class LLVMRustDiagnosticKind {
1018 DebugMetadataVersion,
1021 OptimizationRemarkMissed,
1022 OptimizationRemarkAnalysis,
1023 OptimizationRemarkAnalysisFPCommute,
1024 OptimizationRemarkAnalysisAliasing,
1025 OptimizationRemarkOther,
1026 OptimizationFailure,
1031 static LLVMRustDiagnosticKind toRust(DiagnosticKind Kind) {
1034 return LLVMRustDiagnosticKind::InlineAsm;
1036 return LLVMRustDiagnosticKind::StackSize;
1037 case DK_DebugMetadataVersion:
1038 return LLVMRustDiagnosticKind::DebugMetadataVersion;
1039 case DK_SampleProfile:
1040 return LLVMRustDiagnosticKind::SampleProfile;
1041 case DK_OptimizationRemark:
1042 return LLVMRustDiagnosticKind::OptimizationRemark;
1043 case DK_OptimizationRemarkMissed:
1044 return LLVMRustDiagnosticKind::OptimizationRemarkMissed;
1045 case DK_OptimizationRemarkAnalysis:
1046 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysis;
1047 case DK_OptimizationRemarkAnalysisFPCommute:
1048 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysisFPCommute;
1049 case DK_OptimizationRemarkAnalysisAliasing:
1050 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysisAliasing;
1052 return LLVMRustDiagnosticKind::PGOProfile;
1054 return LLVMRustDiagnosticKind::Linker;
1056 return (Kind >= DK_FirstRemark && Kind <= DK_LastRemark)
1057 ? LLVMRustDiagnosticKind::OptimizationRemarkOther
1058 : LLVMRustDiagnosticKind::Other;
1062 extern "C" LLVMRustDiagnosticKind
1063 LLVMRustGetDiagInfoKind(LLVMDiagnosticInfoRef DI) {
1064 return toRust((DiagnosticKind)unwrap(DI)->getKind());
1066 // This is kept distinct from LLVMGetTypeKind, because when
1067 // a new type kind is added, the Rust-side enum must be
1068 // updated or UB will result.
1069 extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {
1070 switch (unwrap(Ty)->getTypeID()) {
1071 case Type::VoidTyID:
1072 return LLVMVoidTypeKind;
1073 case Type::HalfTyID:
1074 return LLVMHalfTypeKind;
1075 case Type::FloatTyID:
1076 return LLVMFloatTypeKind;
1077 case Type::DoubleTyID:
1078 return LLVMDoubleTypeKind;
1079 case Type::X86_FP80TyID:
1080 return LLVMX86_FP80TypeKind;
1081 case Type::FP128TyID:
1082 return LLVMFP128TypeKind;
1083 case Type::PPC_FP128TyID:
1084 return LLVMPPC_FP128TypeKind;
1085 case Type::LabelTyID:
1086 return LLVMLabelTypeKind;
1087 case Type::MetadataTyID:
1088 return LLVMMetadataTypeKind;
1089 case Type::IntegerTyID:
1090 return LLVMIntegerTypeKind;
1091 case Type::FunctionTyID:
1092 return LLVMFunctionTypeKind;
1093 case Type::StructTyID:
1094 return LLVMStructTypeKind;
1095 case Type::ArrayTyID:
1096 return LLVMArrayTypeKind;
1097 case Type::PointerTyID:
1098 return LLVMPointerTypeKind;
1099 case Type::VectorTyID:
1100 return LLVMVectorTypeKind;
1101 case Type::X86_MMXTyID:
1102 return LLVMX86_MMXTypeKind;
1103 case Type::TokenTyID:
1104 return LLVMTokenTypeKind;
1106 report_fatal_error("Unhandled TypeID.");
1109 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SMDiagnostic, LLVMSMDiagnosticRef)
1111 extern "C" void LLVMRustSetInlineAsmDiagnosticHandler(
1112 LLVMContextRef C, LLVMContext::InlineAsmDiagHandlerTy H, void *CX) {
1113 unwrap(C)->setInlineAsmDiagnosticHandler(H, CX);
1116 extern "C" void LLVMRustWriteSMDiagnosticToString(LLVMSMDiagnosticRef D,
1117 RustStringRef Str) {
1118 RawRustStringOstream OS(Str);
1119 unwrap(D)->print("", OS);
1122 extern "C" LLVMValueRef LLVMRustBuildCleanupPad(LLVMBuilderRef B,
1123 LLVMValueRef ParentPad,
1125 LLVMValueRef *LLArgs,
1127 Value **Args = unwrap(LLArgs);
1128 if (ParentPad == nullptr) {
1129 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
1130 ParentPad = wrap(Constant::getNullValue(Ty));
1132 return wrap(unwrap(B)->CreateCleanupPad(
1133 unwrap(ParentPad), ArrayRef<Value *>(Args, ArgCount), Name));
1136 extern "C" LLVMValueRef LLVMRustBuildCleanupRet(LLVMBuilderRef B,
1137 LLVMValueRef CleanupPad,
1138 LLVMBasicBlockRef UnwindBB) {
1139 CleanupPadInst *Inst = cast<CleanupPadInst>(unwrap(CleanupPad));
1140 return wrap(unwrap(B)->CreateCleanupRet(Inst, unwrap(UnwindBB)));
1143 extern "C" LLVMValueRef
1144 LLVMRustBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
1145 unsigned ArgCount, LLVMValueRef *LLArgs, const char *Name) {
1146 Value **Args = unwrap(LLArgs);
1147 return wrap(unwrap(B)->CreateCatchPad(
1148 unwrap(ParentPad), ArrayRef<Value *>(Args, ArgCount), Name));
1151 extern "C" LLVMValueRef LLVMRustBuildCatchRet(LLVMBuilderRef B,
1153 LLVMBasicBlockRef BB) {
1154 return wrap(unwrap(B)->CreateCatchRet(cast<CatchPadInst>(unwrap(Pad)),
1158 extern "C" LLVMValueRef LLVMRustBuildCatchSwitch(LLVMBuilderRef B,
1159 LLVMValueRef ParentPad,
1160 LLVMBasicBlockRef BB,
1161 unsigned NumHandlers,
1163 if (ParentPad == nullptr) {
1164 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
1165 ParentPad = wrap(Constant::getNullValue(Ty));
1167 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(BB),
1168 NumHandlers, Name));
1171 extern "C" void LLVMRustAddHandler(LLVMValueRef CatchSwitchRef,
1172 LLVMBasicBlockRef Handler) {
1173 Value *CatchSwitch = unwrap(CatchSwitchRef);
1174 cast<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Handler));
1177 extern "C" OperandBundleDef *LLVMRustBuildOperandBundleDef(const char *Name,
1178 LLVMValueRef *Inputs,
1179 unsigned NumInputs) {
1180 return new OperandBundleDef(Name, makeArrayRef(unwrap(Inputs), NumInputs));
1183 extern "C" void LLVMRustFreeOperandBundleDef(OperandBundleDef *Bundle) {
1187 extern "C" LLVMValueRef LLVMRustBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1188 LLVMValueRef *Args, unsigned NumArgs,
1189 OperandBundleDef *Bundle,
1191 unsigned Len = Bundle ? 1 : 0;
1192 ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, Len);
1193 return wrap(unwrap(B)->CreateCall(
1194 unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs), Bundles, Name));
1197 extern "C" LLVMValueRef
1198 LLVMRustBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
1199 unsigned NumArgs, LLVMBasicBlockRef Then,
1200 LLVMBasicBlockRef Catch, OperandBundleDef *Bundle,
1202 unsigned Len = Bundle ? 1 : 0;
1203 ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, Len);
1204 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1205 makeArrayRef(unwrap(Args), NumArgs),
1209 extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
1210 LLVMBasicBlockRef BB) {
1211 auto Point = unwrap(BB)->getFirstInsertionPt();
1212 unwrap(B)->SetInsertPoint(unwrap(BB), Point);
1215 extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V,
1217 Triple TargetTriple(unwrap(M)->getTargetTriple());
1218 GlobalObject *GV = unwrap<GlobalObject>(V);
1219 if (!TargetTriple.isOSBinFormatMachO()) {
1220 GV->setComdat(unwrap(M)->getOrInsertComdat(Name));
1224 extern "C" void LLVMRustUnsetComdat(LLVMValueRef V) {
1225 GlobalObject *GV = unwrap<GlobalObject>(V);
1226 GV->setComdat(nullptr);
1229 enum class LLVMRustLinkage {
1230 ExternalLinkage = 0,
1231 AvailableExternallyLinkage = 1,
1232 LinkOnceAnyLinkage = 2,
1233 LinkOnceODRLinkage = 3,
1236 AppendingLinkage = 6,
1237 InternalLinkage = 7,
1239 ExternalWeakLinkage = 9,
1243 static LLVMRustLinkage toRust(LLVMLinkage Linkage) {
1245 case LLVMExternalLinkage:
1246 return LLVMRustLinkage::ExternalLinkage;
1247 case LLVMAvailableExternallyLinkage:
1248 return LLVMRustLinkage::AvailableExternallyLinkage;
1249 case LLVMLinkOnceAnyLinkage:
1250 return LLVMRustLinkage::LinkOnceAnyLinkage;
1251 case LLVMLinkOnceODRLinkage:
1252 return LLVMRustLinkage::LinkOnceODRLinkage;
1253 case LLVMWeakAnyLinkage:
1254 return LLVMRustLinkage::WeakAnyLinkage;
1255 case LLVMWeakODRLinkage:
1256 return LLVMRustLinkage::WeakODRLinkage;
1257 case LLVMAppendingLinkage:
1258 return LLVMRustLinkage::AppendingLinkage;
1259 case LLVMInternalLinkage:
1260 return LLVMRustLinkage::InternalLinkage;
1261 case LLVMPrivateLinkage:
1262 return LLVMRustLinkage::PrivateLinkage;
1263 case LLVMExternalWeakLinkage:
1264 return LLVMRustLinkage::ExternalWeakLinkage;
1265 case LLVMCommonLinkage:
1266 return LLVMRustLinkage::CommonLinkage;
1268 report_fatal_error("Invalid LLVMRustLinkage value!");
1272 static LLVMLinkage fromRust(LLVMRustLinkage Linkage) {
1274 case LLVMRustLinkage::ExternalLinkage:
1275 return LLVMExternalLinkage;
1276 case LLVMRustLinkage::AvailableExternallyLinkage:
1277 return LLVMAvailableExternallyLinkage;
1278 case LLVMRustLinkage::LinkOnceAnyLinkage:
1279 return LLVMLinkOnceAnyLinkage;
1280 case LLVMRustLinkage::LinkOnceODRLinkage:
1281 return LLVMLinkOnceODRLinkage;
1282 case LLVMRustLinkage::WeakAnyLinkage:
1283 return LLVMWeakAnyLinkage;
1284 case LLVMRustLinkage::WeakODRLinkage:
1285 return LLVMWeakODRLinkage;
1286 case LLVMRustLinkage::AppendingLinkage:
1287 return LLVMAppendingLinkage;
1288 case LLVMRustLinkage::InternalLinkage:
1289 return LLVMInternalLinkage;
1290 case LLVMRustLinkage::PrivateLinkage:
1291 return LLVMPrivateLinkage;
1292 case LLVMRustLinkage::ExternalWeakLinkage:
1293 return LLVMExternalWeakLinkage;
1294 case LLVMRustLinkage::CommonLinkage:
1295 return LLVMCommonLinkage;
1297 report_fatal_error("Invalid LLVMRustLinkage value!");
1300 extern "C" LLVMRustLinkage LLVMRustGetLinkage(LLVMValueRef V) {
1301 return toRust(LLVMGetLinkage(V));
1304 extern "C" void LLVMRustSetLinkage(LLVMValueRef V,
1305 LLVMRustLinkage RustLinkage) {
1306 LLVMSetLinkage(V, fromRust(RustLinkage));
1309 // Returns true if both high and low were successfully set. Fails in case constant wasn’t any of
1310 // the common sizes (1, 8, 16, 32, 64, 128 bits)
1311 extern "C" bool LLVMRustConstInt128Get(LLVMValueRef CV, bool sext, uint64_t *high, uint64_t *low)
1313 auto C = unwrap<llvm::ConstantInt>(CV);
1314 if (C->getBitWidth() > 128) { return false; }
1317 AP = C->getValue().sextOrSelf(128);
1319 AP = C->getValue().zextOrSelf(128);
1321 *low = AP.getLoBits(64).getZExtValue();
1322 *high = AP.getHiBits(64).getZExtValue();
1326 enum class LLVMRustVisibility {
1332 static LLVMRustVisibility toRust(LLVMVisibility Vis) {
1334 case LLVMDefaultVisibility:
1335 return LLVMRustVisibility::Default;
1336 case LLVMHiddenVisibility:
1337 return LLVMRustVisibility::Hidden;
1338 case LLVMProtectedVisibility:
1339 return LLVMRustVisibility::Protected;
1341 report_fatal_error("Invalid LLVMRustVisibility value!");
1344 static LLVMVisibility fromRust(LLVMRustVisibility Vis) {
1346 case LLVMRustVisibility::Default:
1347 return LLVMDefaultVisibility;
1348 case LLVMRustVisibility::Hidden:
1349 return LLVMHiddenVisibility;
1350 case LLVMRustVisibility::Protected:
1351 return LLVMProtectedVisibility;
1353 report_fatal_error("Invalid LLVMRustVisibility value!");
1356 extern "C" LLVMRustVisibility LLVMRustGetVisibility(LLVMValueRef V) {
1357 return toRust(LLVMGetVisibility(V));
1360 // Oh hey, a binding that makes sense for once? (because LLVM’s own do not)
1361 extern "C" LLVMValueRef LLVMRustBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1362 LLVMTypeRef DestTy, bool isSigned) {
1363 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), isSigned, ""));
1366 extern "C" void LLVMRustSetVisibility(LLVMValueRef V,
1367 LLVMRustVisibility RustVisibility) {
1368 LLVMSetVisibility(V, fromRust(RustVisibility));
1371 struct LLVMRustModuleBuffer {
1375 extern "C" LLVMRustModuleBuffer*
1376 LLVMRustModuleBufferCreate(LLVMModuleRef M) {
1377 auto Ret = llvm::make_unique<LLVMRustModuleBuffer>();
1379 raw_string_ostream OS(Ret->data);
1381 legacy::PassManager PM;
1382 PM.add(createBitcodeWriterPass(OS));
1386 return Ret.release();
1390 LLVMRustModuleBufferFree(LLVMRustModuleBuffer *Buffer) {
1394 extern "C" const void*
1395 LLVMRustModuleBufferPtr(const LLVMRustModuleBuffer *Buffer) {
1396 return Buffer->data.data();
1400 LLVMRustModuleBufferLen(const LLVMRustModuleBuffer *Buffer) {
1401 return Buffer->data.length();
1405 LLVMRustModuleCost(LLVMModuleRef M) {
1406 auto f = unwrap(M)->functions();
1407 return std::distance(std::begin(f), std::end(f));
1410 // Vector reductions:
1411 #if LLVM_VERSION_GE(5, 0)
1412 extern "C" LLVMValueRef
1413 LLVMRustBuildVectorReduceFAdd(LLVMBuilderRef B, LLVMValueRef Acc, LLVMValueRef Src) {
1414 return wrap(unwrap(B)->CreateFAddReduce(unwrap(Acc),unwrap(Src)));
1416 extern "C" LLVMValueRef
1417 LLVMRustBuildVectorReduceFMul(LLVMBuilderRef B, LLVMValueRef Acc, LLVMValueRef Src) {
1418 return wrap(unwrap(B)->CreateFMulReduce(unwrap(Acc),unwrap(Src)));
1420 extern "C" LLVMValueRef
1421 LLVMRustBuildVectorReduceAdd(LLVMBuilderRef B, LLVMValueRef Src) {
1422 return wrap(unwrap(B)->CreateAddReduce(unwrap(Src)));
1424 extern "C" LLVMValueRef
1425 LLVMRustBuildVectorReduceMul(LLVMBuilderRef B, LLVMValueRef Src) {
1426 return wrap(unwrap(B)->CreateMulReduce(unwrap(Src)));
1428 extern "C" LLVMValueRef
1429 LLVMRustBuildVectorReduceAnd(LLVMBuilderRef B, LLVMValueRef Src) {
1430 return wrap(unwrap(B)->CreateAndReduce(unwrap(Src)));
1432 extern "C" LLVMValueRef
1433 LLVMRustBuildVectorReduceOr(LLVMBuilderRef B, LLVMValueRef Src) {
1434 return wrap(unwrap(B)->CreateOrReduce(unwrap(Src)));
1436 extern "C" LLVMValueRef
1437 LLVMRustBuildVectorReduceXor(LLVMBuilderRef B, LLVMValueRef Src) {
1438 return wrap(unwrap(B)->CreateXorReduce(unwrap(Src)));
1440 extern "C" LLVMValueRef
1441 LLVMRustBuildVectorReduceMin(LLVMBuilderRef B, LLVMValueRef Src, bool IsSigned) {
1442 return wrap(unwrap(B)->CreateIntMinReduce(unwrap(Src), IsSigned));
1444 extern "C" LLVMValueRef
1445 LLVMRustBuildVectorReduceMax(LLVMBuilderRef B, LLVMValueRef Src, bool IsSigned) {
1446 return wrap(unwrap(B)->CreateIntMaxReduce(unwrap(Src), IsSigned));
1448 extern "C" LLVMValueRef
1449 LLVMRustBuildVectorReduceFMin(LLVMBuilderRef B, LLVMValueRef Src, bool NoNaN) {
1450 return wrap(unwrap(B)->CreateFPMinReduce(unwrap(Src), NoNaN));
1452 extern "C" LLVMValueRef
1453 LLVMRustBuildVectorReduceFMax(LLVMBuilderRef B, LLVMValueRef Src, bool NoNaN) {
1454 return wrap(unwrap(B)->CreateFPMaxReduce(unwrap(Src), NoNaN));
1459 extern "C" LLVMValueRef
1460 LLVMRustBuildVectorReduceFAdd(LLVMBuilderRef, LLVMValueRef, LLVMValueRef) {
1463 extern "C" LLVMValueRef
1464 LLVMRustBuildVectorReduceFMul(LLVMBuilderRef, LLVMValueRef, LLVMValueRef) {
1467 extern "C" LLVMValueRef
1468 LLVMRustBuildVectorReduceAdd(LLVMBuilderRef, LLVMValueRef) {
1471 extern "C" LLVMValueRef
1472 LLVMRustBuildVectorReduceMul(LLVMBuilderRef, LLVMValueRef) {
1475 extern "C" LLVMValueRef
1476 LLVMRustBuildVectorReduceAnd(LLVMBuilderRef, LLVMValueRef) {
1479 extern "C" LLVMValueRef
1480 LLVMRustBuildVectorReduceOr(LLVMBuilderRef, LLVMValueRef) {
1483 extern "C" LLVMValueRef
1484 LLVMRustBuildVectorReduceXor(LLVMBuilderRef, LLVMValueRef) {
1487 extern "C" LLVMValueRef
1488 LLVMRustBuildVectorReduceMin(LLVMBuilderRef, LLVMValueRef, bool) {
1491 extern "C" LLVMValueRef
1492 LLVMRustBuildVectorReduceMax(LLVMBuilderRef, LLVMValueRef, bool) {
1495 extern "C" LLVMValueRef
1496 LLVMRustBuildVectorReduceFMin(LLVMBuilderRef, LLVMValueRef, bool) {
1499 extern "C" LLVMValueRef
1500 LLVMRustBuildVectorReduceFMax(LLVMBuilderRef, LLVMValueRef, bool) {
1505 #if LLVM_VERSION_LT(4, 0)
1506 extern "C" LLVMValueRef
1507 LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1508 LLVMValueRef RHS, const char *Name) {
1509 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
1513 #if LLVM_VERSION_GE(6, 0)
1514 extern "C" LLVMValueRef
1515 LLVMRustBuildMinNum(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS) {
1516 return wrap(unwrap(B)->CreateMinNum(unwrap(LHS),unwrap(RHS)));
1518 extern "C" LLVMValueRef
1519 LLVMRustBuildMaxNum(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS) {
1520 return wrap(unwrap(B)->CreateMaxNum(unwrap(LHS),unwrap(RHS)));
1523 extern "C" LLVMValueRef
1524 LLVMRustBuildMinNum(LLVMBuilderRef, LLVMValueRef, LLVMValueRef) {
1527 extern "C" LLVMValueRef
1528 LLVMRustBuildMaxNum(LLVMBuilderRef, LLVMValueRef, LLVMValueRef) {