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/Instructions.h"
16 #include "llvm/Object/Archive.h"
17 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/IR/CallSite.h"
21 //===----------------------------------------------------------------------===
23 // This file defines alternate interfaces to core functions that are more
24 // readily callable by Rust's FFI.
26 //===----------------------------------------------------------------------===
29 using namespace llvm::sys;
30 using namespace llvm::object;
32 // LLVMAtomicOrdering is already an enum - don't create another
34 static AtomicOrdering fromRust(LLVMAtomicOrdering Ordering) {
36 case LLVMAtomicOrderingNotAtomic:
37 return AtomicOrdering::NotAtomic;
38 case LLVMAtomicOrderingUnordered:
39 return AtomicOrdering::Unordered;
40 case LLVMAtomicOrderingMonotonic:
41 return AtomicOrdering::Monotonic;
42 case LLVMAtomicOrderingAcquire:
43 return AtomicOrdering::Acquire;
44 case LLVMAtomicOrderingRelease:
45 return AtomicOrdering::Release;
46 case LLVMAtomicOrderingAcquireRelease:
47 return AtomicOrdering::AcquireRelease;
48 case LLVMAtomicOrderingSequentiallyConsistent:
49 return AtomicOrdering::SequentiallyConsistent;
52 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
55 static char *LastError;
57 extern "C" LLVMMemoryBufferRef
58 LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
59 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOr =
60 MemoryBuffer::getFile(Path, -1, false);
62 LLVMRustSetLastError(BufOr.getError().message().c_str());
65 return wrap(BufOr.get().release());
68 extern "C" char *LLVMRustGetLastError(void) {
69 char *Ret = LastError;
74 void LLVMRustSetLastError(const char *Err) {
75 free((void *)LastError);
76 LastError = strdup(Err);
79 extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
81 unwrap(M)->setTargetTriple(Triple::normalize(Triple));
84 extern "C" void LLVMRustPrintPassTimings() {
85 raw_fd_ostream OS(2, false); // stderr.
86 TimerGroup::printAll(OS);
89 extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M,
91 return wrap(unwrap(M)->getNamedValue(Name));
94 extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
96 LLVMTypeRef FunctionTy) {
98 unwrap(M)->getOrInsertFunction(Name, unwrap<FunctionType>(FunctionTy)));
101 extern "C" LLVMValueRef
102 LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
103 return wrap(unwrap(M)->getOrInsertGlobal(Name, unwrap(Ty)));
106 extern "C" LLVMTypeRef LLVMRustMetadataTypeInContext(LLVMContextRef C) {
107 return wrap(Type::getMetadataTy(*unwrap(C)));
110 static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
113 return Attribute::AlwaysInline;
115 return Attribute::ByVal;
117 return Attribute::Cold;
119 return Attribute::InlineHint;
121 return Attribute::MinSize;
123 return Attribute::Naked;
125 return Attribute::NoAlias;
127 return Attribute::NoCapture;
129 return Attribute::NoInline;
131 return Attribute::NonNull;
133 return Attribute::NoRedZone;
135 return Attribute::NoReturn;
137 return Attribute::NoUnwind;
138 case OptimizeForSize:
139 return Attribute::OptimizeForSize;
141 return Attribute::ReadOnly;
143 return Attribute::SExt;
145 return Attribute::StructRet;
147 return Attribute::UWTable;
149 return Attribute::ZExt;
151 return Attribute::InReg;
153 return Attribute::SanitizeThread;
154 case SanitizeAddress:
155 return Attribute::SanitizeAddress;
157 return Attribute::SanitizeMemory;
159 llvm_unreachable("bad AttributeKind");
162 extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned Index,
163 LLVMRustAttribute RustAttr) {
164 CallSite Call = CallSite(unwrap<Instruction>(Instr));
165 Attribute Attr = Attribute::get(Call->getContext(), fromRust(RustAttr));
167 Call.setAttributes(Call.getAttributes().addAttributes(
168 Call->getContext(), Index,
169 AttributeSet::get(Call->getContext(), Index, B)));
172 extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
175 CallSite Call = CallSite(unwrap<Instruction>(Instr));
177 B.addDereferenceableAttr(Bytes);
178 Call.setAttributes(Call.getAttributes().addAttributes(
179 Call->getContext(), Index,
180 AttributeSet::get(Call->getContext(), Index, B)));
183 extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn, unsigned Index,
184 LLVMRustAttribute RustAttr) {
185 Function *A = unwrap<Function>(Fn);
186 Attribute Attr = Attribute::get(A->getContext(), fromRust(RustAttr));
188 A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
191 extern "C" void LLVMRustAddDereferenceableAttr(LLVMValueRef Fn, unsigned Index,
193 Function *A = unwrap<Function>(Fn);
195 B.addDereferenceableAttr(Bytes);
196 A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
199 extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
203 Function *F = unwrap<Function>(Fn);
205 B.addAttribute(Name, Value);
206 F->addAttributes(Index, AttributeSet::get(F->getContext(), Index, B));
209 extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
211 LLVMRustAttribute RustAttr) {
212 Function *F = unwrap<Function>(Fn);
213 const AttributeSet PAL = F->getAttributes();
214 Attribute Attr = Attribute::get(F->getContext(), fromRust(RustAttr));
216 const AttributeSet PALNew = PAL.removeAttributes(
217 F->getContext(), Index, AttributeSet::get(F->getContext(), Index, B));
218 F->setAttributes(PALNew);
221 // enable fpmath flag UnsafeAlgebra
222 extern "C" void LLVMRustSetHasUnsafeAlgebra(LLVMValueRef V) {
223 if (auto I = dyn_cast<Instruction>(unwrap<Value>(V))) {
224 I->setHasUnsafeAlgebra(true);
228 extern "C" LLVMValueRef
229 LLVMRustBuildAtomicLoad(LLVMBuilderRef B, LLVMValueRef Source, const char *Name,
230 LLVMAtomicOrdering Order, unsigned Alignment) {
231 LoadInst *LI = new LoadInst(unwrap(Source), 0);
232 LI->setAtomic(fromRust(Order));
233 LI->setAlignment(Alignment);
234 return wrap(unwrap(B)->Insert(LI, Name));
237 extern "C" LLVMValueRef LLVMRustBuildAtomicStore(LLVMBuilderRef B,
240 LLVMAtomicOrdering Order,
241 unsigned Alignment) {
242 StoreInst *SI = new StoreInst(unwrap(V), unwrap(Target));
243 SI->setAtomic(fromRust(Order));
244 SI->setAlignment(Alignment);
245 return wrap(unwrap(B)->Insert(SI));
248 extern "C" LLVMValueRef
249 LLVMRustBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Target,
250 LLVMValueRef Old, LLVMValueRef Source,
251 LLVMAtomicOrdering Order,
252 LLVMAtomicOrdering FailureOrder, LLVMBool Weak) {
253 AtomicCmpXchgInst *ACXI = unwrap(B)->CreateAtomicCmpXchg(
254 unwrap(Target), unwrap(Old), unwrap(Source), fromRust(Order),
255 fromRust(FailureOrder));
260 enum class LLVMRustSynchronizationScope {
266 static SynchronizationScope fromRust(LLVMRustSynchronizationScope Scope) {
268 case LLVMRustSynchronizationScope::SingleThread:
270 case LLVMRustSynchronizationScope::CrossThread:
273 llvm_unreachable("bad SynchronizationScope.");
277 extern "C" LLVMValueRef
278 LLVMRustBuildAtomicFence(LLVMBuilderRef B, LLVMAtomicOrdering Order,
279 LLVMRustSynchronizationScope Scope) {
280 return wrap(unwrap(B)->CreateFence(fromRust(Order), fromRust(Scope)));
283 extern "C" void LLVMRustSetDebug(int Enabled) {
289 enum class LLVMRustAsmDialect {
295 static InlineAsm::AsmDialect fromRust(LLVMRustAsmDialect Dialect) {
297 case LLVMRustAsmDialect::Att:
298 return InlineAsm::AD_ATT;
299 case LLVMRustAsmDialect::Intel:
300 return InlineAsm::AD_Intel;
302 llvm_unreachable("bad AsmDialect.");
306 extern "C" LLVMValueRef LLVMRustInlineAsm(LLVMTypeRef Ty, char *AsmString,
308 LLVMBool HasSideEffects,
309 LLVMBool IsAlignStack,
310 LLVMRustAsmDialect Dialect) {
311 return wrap(InlineAsm::get(unwrap<FunctionType>(Ty), AsmString, Constraints,
312 HasSideEffects, IsAlignStack, fromRust(Dialect)));
315 typedef DIBuilder *LLVMRustDIBuilderRef;
317 typedef struct LLVMOpaqueMetadata *LLVMRustMetadataRef;
320 DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMRustMetadataRef)
322 inline Metadata **unwrap(LLVMRustMetadataRef *Vals) {
323 return reinterpret_cast<Metadata **>(Vals);
327 template <typename DIT> DIT *unwrapDIPtr(LLVMRustMetadataRef Ref) {
328 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
331 #define DIDescriptor DIScope
332 #define DIArray DINodeArray
333 #define unwrapDI unwrapDIPtr
335 // These values **must** match debuginfo::DIFlags! They also *happen*
336 // to match LLVM, but that isn't required as we do giant sets of
337 // matching below. The value shouldn't be directly passed to LLVM.
338 enum class LLVMRustDIFlags : uint32_t {
343 FlagFwdDecl = (1 << 2),
344 FlagAppleBlock = (1 << 3),
345 FlagBlockByrefStruct = (1 << 4),
346 FlagVirtual = (1 << 5),
347 FlagArtificial = (1 << 6),
348 FlagExplicit = (1 << 7),
349 FlagPrototyped = (1 << 8),
350 FlagObjcClassComplete = (1 << 9),
351 FlagObjectPointer = (1 << 10),
352 FlagVector = (1 << 11),
353 FlagStaticMember = (1 << 12),
354 FlagLValueReference = (1 << 13),
355 FlagRValueReference = (1 << 14),
356 FlagMainSubprogram = (1 << 21),
357 // Do not add values that are not supported by the minimum LLVM
358 // version we support!
361 inline LLVMRustDIFlags operator&(LLVMRustDIFlags A, LLVMRustDIFlags B) {
362 return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(A) &
363 static_cast<uint32_t>(B));
366 inline LLVMRustDIFlags operator|(LLVMRustDIFlags A, LLVMRustDIFlags B) {
367 return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(A) |
368 static_cast<uint32_t>(B));
371 inline LLVMRustDIFlags &operator|=(LLVMRustDIFlags &A, LLVMRustDIFlags B) {
375 inline bool isSet(LLVMRustDIFlags F) { return F != LLVMRustDIFlags::FlagZero; }
377 inline LLVMRustDIFlags visibility(LLVMRustDIFlags F) {
378 return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(F) & 0x3);
381 #if LLVM_VERSION_GE(4, 0)
382 static DINode::DIFlags fromRust(LLVMRustDIFlags Flags) {
383 DINode::DIFlags Result = DINode::DIFlags::FlagZero;
385 static unsigned fromRust(LLVMRustDIFlags Flags) {
389 switch (visibility(Flags)) {
390 case LLVMRustDIFlags::FlagPrivate:
391 Result |= DINode::DIFlags::FlagPrivate;
393 case LLVMRustDIFlags::FlagProtected:
394 Result |= DINode::DIFlags::FlagProtected;
396 case LLVMRustDIFlags::FlagPublic:
397 Result |= DINode::DIFlags::FlagPublic;
400 // The rest are handled below
404 if (isSet(Flags & LLVMRustDIFlags::FlagFwdDecl)) {
405 Result |= DINode::DIFlags::FlagFwdDecl;
407 if (isSet(Flags & LLVMRustDIFlags::FlagAppleBlock)) {
408 Result |= DINode::DIFlags::FlagAppleBlock;
410 if (isSet(Flags & LLVMRustDIFlags::FlagBlockByrefStruct)) {
411 Result |= DINode::DIFlags::FlagBlockByrefStruct;
413 if (isSet(Flags & LLVMRustDIFlags::FlagVirtual)) {
414 Result |= DINode::DIFlags::FlagVirtual;
416 if (isSet(Flags & LLVMRustDIFlags::FlagArtificial)) {
417 Result |= DINode::DIFlags::FlagArtificial;
419 if (isSet(Flags & LLVMRustDIFlags::FlagExplicit)) {
420 Result |= DINode::DIFlags::FlagExplicit;
422 if (isSet(Flags & LLVMRustDIFlags::FlagPrototyped)) {
423 Result |= DINode::DIFlags::FlagPrototyped;
425 if (isSet(Flags & LLVMRustDIFlags::FlagObjcClassComplete)) {
426 Result |= DINode::DIFlags::FlagObjcClassComplete;
428 if (isSet(Flags & LLVMRustDIFlags::FlagObjectPointer)) {
429 Result |= DINode::DIFlags::FlagObjectPointer;
431 if (isSet(Flags & LLVMRustDIFlags::FlagVector)) {
432 Result |= DINode::DIFlags::FlagVector;
434 if (isSet(Flags & LLVMRustDIFlags::FlagStaticMember)) {
435 Result |= DINode::DIFlags::FlagStaticMember;
437 if (isSet(Flags & LLVMRustDIFlags::FlagLValueReference)) {
438 Result |= DINode::DIFlags::FlagLValueReference;
440 if (isSet(Flags & LLVMRustDIFlags::FlagRValueReference)) {
441 Result |= DINode::DIFlags::FlagRValueReference;
443 #if LLVM_RUSTLLVM || LLVM_VERSION_GE(4, 0)
444 if (isSet(Flags & LLVMRustDIFlags::FlagMainSubprogram)) {
445 Result |= DINode::DIFlags::FlagMainSubprogram;
452 extern "C" uint32_t LLVMRustDebugMetadataVersion() {
453 return DEBUG_METADATA_VERSION;
456 extern "C" uint32_t LLVMRustVersionMinor() { return LLVM_VERSION_MINOR; }
458 extern "C" uint32_t LLVMRustVersionMajor() { return LLVM_VERSION_MAJOR; }
460 extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M, const char *Name,
462 unwrap(M)->addModuleFlag(Module::Warning, Name, Value);
465 extern "C" LLVMRustDIBuilderRef LLVMRustDIBuilderCreate(LLVMModuleRef M) {
466 return new DIBuilder(*unwrap(M));
469 extern "C" void LLVMRustDIBuilderDispose(LLVMRustDIBuilderRef Builder) {
473 extern "C" void LLVMRustDIBuilderFinalize(LLVMRustDIBuilderRef Builder) {
477 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateCompileUnit(
478 LLVMRustDIBuilderRef Builder, unsigned Lang, LLVMRustMetadataRef FileRef,
479 const char *Producer, bool isOptimized, const char *Flags,
480 unsigned RuntimeVer, const char *SplitName) {
481 auto *File = unwrapDI<DIFile>(FileRef);
483 #if LLVM_VERSION_GE(4, 0)
484 return wrap(Builder->createCompileUnit(Lang, File, Producer, isOptimized,
485 Flags, RuntimeVer, SplitName));
487 return wrap(Builder->createCompileUnit(Lang, File->getFilename(),
488 File->getDirectory(), Producer, isOptimized,
489 Flags, RuntimeVer, SplitName));
493 extern "C" LLVMRustMetadataRef
494 LLVMRustDIBuilderCreateFile(LLVMRustDIBuilderRef Builder, const char *Filename,
495 const char *Directory) {
496 return wrap(Builder->createFile(Filename, Directory));
499 extern "C" LLVMRustMetadataRef
500 LLVMRustDIBuilderCreateSubroutineType(LLVMRustDIBuilderRef Builder,
501 LLVMRustMetadataRef File,
502 LLVMRustMetadataRef ParameterTypes) {
503 return wrap(Builder->createSubroutineType(
504 #if LLVM_VERSION_EQ(3, 7)
505 unwrapDI<DIFile>(File),
507 DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
510 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateFunction(
511 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
512 const char *LinkageName, LLVMRustMetadataRef File, unsigned LineNo,
513 LLVMRustMetadataRef Ty, bool IsLocalToUnit, bool IsDefinition,
514 unsigned ScopeLine, LLVMRustDIFlags Flags, bool IsOptimized,
515 LLVMValueRef Fn, LLVMRustMetadataRef TParam, LLVMRustMetadataRef Decl) {
516 #if LLVM_VERSION_GE(3, 8)
517 DITemplateParameterArray TParams =
518 DITemplateParameterArray(unwrap<MDTuple>(TParam));
519 DISubprogram *Sub = Builder->createFunction(
520 unwrapDI<DIScope>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
521 LineNo, unwrapDI<DISubroutineType>(Ty), IsLocalToUnit, IsDefinition,
522 ScopeLine, fromRust(Flags), IsOptimized, TParams,
523 unwrapDIPtr<DISubprogram>(Decl));
524 unwrap<Function>(Fn)->setSubprogram(Sub);
527 return wrap(Builder->createFunction(
528 unwrapDI<DIScope>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
529 LineNo, unwrapDI<DISubroutineType>(Ty), IsLocalToUnit, IsDefinition,
530 ScopeLine, fromRust(Flags), IsOptimized, unwrap<Function>(Fn),
531 unwrapDIPtr<MDNode>(TParam), unwrapDIPtr<MDNode>(Decl)));
535 extern "C" LLVMRustMetadataRef
536 LLVMRustDIBuilderCreateBasicType(LLVMRustDIBuilderRef Builder, const char *Name,
537 uint64_t SizeInBits, uint32_t AlignInBits,
539 return wrap(Builder->createBasicType(Name, SizeInBits,
540 #if LLVM_VERSION_LE(3, 9)
546 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreatePointerType(
547 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef PointeeTy,
548 uint64_t SizeInBits, uint32_t AlignInBits, const char *Name) {
549 return wrap(Builder->createPointerType(unwrapDI<DIType>(PointeeTy),
550 SizeInBits, AlignInBits, Name));
553 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateStructType(
554 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
555 LLVMRustMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
556 uint32_t AlignInBits, LLVMRustDIFlags Flags,
557 LLVMRustMetadataRef DerivedFrom, LLVMRustMetadataRef Elements,
558 unsigned RunTimeLang, LLVMRustMetadataRef VTableHolder,
559 const char *UniqueId) {
560 return wrap(Builder->createStructType(
561 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
562 SizeInBits, AlignInBits, fromRust(Flags), unwrapDI<DIType>(DerivedFrom),
563 DINodeArray(unwrapDI<MDTuple>(Elements)), RunTimeLang,
564 unwrapDI<DIType>(VTableHolder), UniqueId));
567 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateMemberType(
568 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
569 LLVMRustMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
570 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMRustDIFlags Flags,
571 LLVMRustMetadataRef Ty) {
572 return wrap(Builder->createMemberType(unwrapDI<DIDescriptor>(Scope), Name,
573 unwrapDI<DIFile>(File), LineNo,
574 SizeInBits, AlignInBits, OffsetInBits,
575 fromRust(Flags), unwrapDI<DIType>(Ty)));
578 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateLexicalBlock(
579 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope,
580 LLVMRustMetadataRef File, unsigned Line, unsigned Col) {
581 return wrap(Builder->createLexicalBlock(unwrapDI<DIDescriptor>(Scope),
582 unwrapDI<DIFile>(File), Line, Col));
585 extern "C" LLVMRustMetadataRef
586 LLVMRustDIBuilderCreateLexicalBlockFile(LLVMRustDIBuilderRef Builder,
587 LLVMRustMetadataRef Scope,
588 LLVMRustMetadataRef File) {
589 return wrap(Builder->createLexicalBlockFile(unwrapDI<DIDescriptor>(Scope),
590 unwrapDI<DIFile>(File)));
593 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateStaticVariable(
594 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Context, const char *Name,
595 const char *LinkageName, LLVMRustMetadataRef File, unsigned LineNo,
596 LLVMRustMetadataRef Ty, bool IsLocalToUnit, LLVMValueRef V,
597 LLVMRustMetadataRef Decl = nullptr, uint32_t AlignInBits = 0) {
598 llvm::GlobalVariable *InitVal = cast<llvm::GlobalVariable>(unwrap(V));
600 #if LLVM_VERSION_GE(4, 0)
601 llvm::DIExpression *InitExpr = nullptr;
602 if (llvm::ConstantInt *IntVal = llvm::dyn_cast<llvm::ConstantInt>(InitVal)) {
603 InitExpr = Builder->createConstantValueExpression(
604 IntVal->getValue().getSExtValue());
605 } else if (llvm::ConstantFP *FPVal =
606 llvm::dyn_cast<llvm::ConstantFP>(InitVal)) {
607 InitExpr = Builder->createConstantValueExpression(
608 FPVal->getValueAPF().bitcastToAPInt().getZExtValue());
611 llvm::DIGlobalVariableExpression *VarExpr = Builder->createGlobalVariableExpression(
612 unwrapDI<DIDescriptor>(Context), Name, LinkageName,
613 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), IsLocalToUnit,
614 InitExpr, unwrapDIPtr<MDNode>(Decl), AlignInBits);
616 InitVal->setMetadata("dbg", VarExpr);
618 return wrap(VarExpr);
620 return wrap(Builder->createGlobalVariable(
621 unwrapDI<DIDescriptor>(Context), Name, LinkageName,
622 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), IsLocalToUnit,
623 InitVal, unwrapDIPtr<MDNode>(Decl)));
627 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateVariable(
628 LLVMRustDIBuilderRef Builder, unsigned Tag, LLVMRustMetadataRef Scope,
629 const char *Name, LLVMRustMetadataRef File, unsigned LineNo,
630 LLVMRustMetadataRef Ty, bool AlwaysPreserve, LLVMRustDIFlags Flags,
631 unsigned ArgNo, uint32_t AlignInBits) {
632 #if LLVM_VERSION_GE(3, 8)
633 if (Tag == 0x100) { // DW_TAG_auto_variable
634 return wrap(Builder->createAutoVariable(
635 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNo,
636 unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags)
637 #if LLVM_VERSION_GE(4, 0)
643 return wrap(Builder->createParameterVariable(
644 unwrapDI<DIDescriptor>(Scope), Name, ArgNo, unwrapDI<DIFile>(File),
645 LineNo, unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags)));
648 return wrap(Builder->createLocalVariable(
649 Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNo,
650 unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags), ArgNo));
654 extern "C" LLVMRustMetadataRef
655 LLVMRustDIBuilderCreateArrayType(LLVMRustDIBuilderRef Builder, uint64_t Size,
656 uint32_t AlignInBits, LLVMRustMetadataRef Ty,
657 LLVMRustMetadataRef Subscripts) {
659 Builder->createArrayType(Size, AlignInBits, unwrapDI<DIType>(Ty),
660 DINodeArray(unwrapDI<MDTuple>(Subscripts))));
663 extern "C" LLVMRustMetadataRef
664 LLVMRustDIBuilderCreateVectorType(LLVMRustDIBuilderRef Builder, uint64_t Size,
665 uint32_t AlignInBits, LLVMRustMetadataRef Ty,
666 LLVMRustMetadataRef Subscripts) {
668 Builder->createVectorType(Size, AlignInBits, unwrapDI<DIType>(Ty),
669 DINodeArray(unwrapDI<MDTuple>(Subscripts))));
672 extern "C" LLVMRustMetadataRef
673 LLVMRustDIBuilderGetOrCreateSubrange(LLVMRustDIBuilderRef Builder, int64_t Lo,
675 return wrap(Builder->getOrCreateSubrange(Lo, Count));
678 extern "C" LLVMRustMetadataRef
679 LLVMRustDIBuilderGetOrCreateArray(LLVMRustDIBuilderRef Builder,
680 LLVMRustMetadataRef *Ptr, unsigned Count) {
681 Metadata **DataValue = unwrap(Ptr);
683 Builder->getOrCreateArray(ArrayRef<Metadata *>(DataValue, Count)).get());
686 extern "C" LLVMValueRef LLVMRustDIBuilderInsertDeclareAtEnd(
687 LLVMRustDIBuilderRef Builder, LLVMValueRef V, LLVMRustMetadataRef VarInfo,
688 int64_t *AddrOps, unsigned AddrOpsCount, LLVMValueRef DL,
689 LLVMBasicBlockRef InsertAtEnd) {
690 return wrap(Builder->insertDeclare(
691 unwrap(V), unwrap<DILocalVariable>(VarInfo),
692 Builder->createExpression(llvm::ArrayRef<int64_t>(AddrOps, AddrOpsCount)),
693 DebugLoc(cast<MDNode>(unwrap<MetadataAsValue>(DL)->getMetadata())),
694 unwrap(InsertAtEnd)));
697 extern "C" LLVMRustMetadataRef
698 LLVMRustDIBuilderCreateEnumerator(LLVMRustDIBuilderRef Builder,
699 const char *Name, uint64_t Val) {
700 return wrap(Builder->createEnumerator(Name, Val));
703 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateEnumerationType(
704 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
705 LLVMRustMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
706 uint32_t AlignInBits, LLVMRustMetadataRef Elements,
707 LLVMRustMetadataRef ClassTy) {
708 return wrap(Builder->createEnumerationType(
709 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
710 SizeInBits, AlignInBits, DINodeArray(unwrapDI<MDTuple>(Elements)),
711 unwrapDI<DIType>(ClassTy)));
714 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateUnionType(
715 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
716 LLVMRustMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
717 uint32_t AlignInBits, LLVMRustDIFlags Flags, LLVMRustMetadataRef Elements,
718 unsigned RunTimeLang, const char *UniqueId) {
719 return wrap(Builder->createUnionType(
720 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
721 SizeInBits, AlignInBits, fromRust(Flags),
722 DINodeArray(unwrapDI<MDTuple>(Elements)), RunTimeLang, UniqueId));
725 extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateTemplateTypeParameter(
726 LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
727 LLVMRustMetadataRef Ty, LLVMRustMetadataRef File, unsigned LineNo,
729 return wrap(Builder->createTemplateTypeParameter(
730 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIType>(Ty)));
733 extern "C" LLVMRustMetadataRef
734 LLVMRustDIBuilderCreateNameSpace(LLVMRustDIBuilderRef Builder,
735 LLVMRustMetadataRef Scope, const char *Name,
736 LLVMRustMetadataRef File, unsigned LineNo) {
737 return wrap(Builder->createNameSpace(
738 unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNo
739 #if LLVM_VERSION_GE(4, 0)
741 false // ExportSymbols (only relevant for C++ anonymous namespaces)
747 LLVMRustDICompositeTypeSetTypeArray(LLVMRustDIBuilderRef Builder,
748 LLVMRustMetadataRef CompositeTy,
749 LLVMRustMetadataRef TyArray) {
750 DICompositeType *Tmp = unwrapDI<DICompositeType>(CompositeTy);
751 Builder->replaceArrays(Tmp, DINodeArray(unwrap<MDTuple>(TyArray)));
754 extern "C" LLVMValueRef
755 LLVMRustDIBuilderCreateDebugLocation(LLVMContextRef ContextRef, unsigned Line,
756 unsigned Column, LLVMRustMetadataRef Scope,
757 LLVMRustMetadataRef InlinedAt) {
758 LLVMContext &Context = *unwrap(ContextRef);
760 DebugLoc debug_loc = DebugLoc::get(Line, Column, unwrapDIPtr<MDNode>(Scope),
761 unwrapDIPtr<MDNode>(InlinedAt));
763 return wrap(MetadataAsValue::get(Context, debug_loc.getAsMDNode()));
766 extern "C" int64_t LLVMRustDIBuilderCreateOpDeref() {
767 return dwarf::DW_OP_deref;
770 extern "C" int64_t LLVMRustDIBuilderCreateOpPlus() { return dwarf::DW_OP_plus; }
772 extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
773 RawRustStringOstream OS(Str);
774 unwrap<llvm::Type>(Ty)->print(OS);
777 extern "C" void LLVMRustWriteValueToString(LLVMValueRef V,
779 RawRustStringOstream OS(Str);
781 unwrap<llvm::Value>(V)->getType()->print(OS);
783 unwrap<llvm::Value>(V)->print(OS);
787 extern "C" bool LLVMRustLinkInExternalBitcode(LLVMModuleRef DstRef, char *BC,
789 Module *Dst = unwrap(DstRef);
791 std::unique_ptr<MemoryBuffer> Buf =
792 MemoryBuffer::getMemBufferCopy(StringRef(BC, Len));
794 #if LLVM_VERSION_GE(4, 0)
795 Expected<std::unique_ptr<Module>> SrcOrError =
796 llvm::getLazyBitcodeModule(Buf->getMemBufferRef(), Dst->getContext());
798 LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str());
802 auto Src = std::move(*SrcOrError);
804 ErrorOr<std::unique_ptr<Module>> Src =
805 llvm::getLazyBitcodeModule(std::move(Buf), Dst->getContext());
807 LLVMRustSetLastError(Src.getError().message().c_str());
814 raw_string_ostream Stream(Err);
815 DiagnosticPrinterRawOStream DP(Stream);
816 #if LLVM_VERSION_GE(4, 0)
817 if (Linker::linkModules(*Dst, std::move(Src))) {
818 #elif LLVM_VERSION_GE(3, 8)
819 if (Linker::linkModules(*Dst, std::move(Src.get()))) {
821 if (Linker::LinkModules(Dst, Src->get(),
822 [&](const DiagnosticInfo &DI) { DI.print(DP); })) {
824 LLVMRustSetLastError(Err.c_str());
830 // Note that the two following functions look quite similar to the
831 // LLVMGetSectionName function. Sadly, it appears that this function only
832 // returns a char* pointer, which isn't guaranteed to be null-terminated. The
833 // function provided by LLVM doesn't return the length, so we've created our own
834 // function which returns the length as well as the data pointer.
836 // For an example of this not returning a null terminated string, see
837 // lib/Object/COFFObjectFile.cpp in the getSectionName function. One of the
838 // branches explicitly creates a StringRef without a null terminator, and then
841 inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
842 return reinterpret_cast<section_iterator *>(SI);
845 extern "C" size_t LLVMRustGetSectionName(LLVMSectionIteratorRef SI,
848 if (std::error_code EC = (*unwrap(SI))->getName(Ret))
849 report_fatal_error(EC.message());
854 // LLVMArrayType function does not support 64-bit ElementCount
855 extern "C" LLVMTypeRef LLVMRustArrayType(LLVMTypeRef ElementTy,
856 uint64_t ElementCount) {
857 return wrap(ArrayType::get(unwrap(ElementTy), ElementCount));
860 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
861 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DebugLoc, LLVMDebugLocRef)
863 extern "C" void LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef Str) {
864 RawRustStringOstream OS(Str);
865 unwrap(T)->print(OS);
868 extern "C" void LLVMRustUnpackOptimizationDiagnostic(
869 LLVMDiagnosticInfoRef DI, RustStringRef PassNameOut,
870 LLVMValueRef *FunctionOut, LLVMDebugLocRef *DebugLocOut,
871 RustStringRef MessageOut) {
872 // Undefined to call this not on an optimization diagnostic!
873 llvm::DiagnosticInfoOptimizationBase *Opt =
874 static_cast<llvm::DiagnosticInfoOptimizationBase *>(unwrap(DI));
876 RawRustStringOstream PassNameOS(PassNameOut);
877 PassNameOS << Opt->getPassName();
878 *FunctionOut = wrap(&Opt->getFunction());
879 *DebugLocOut = wrap(&Opt->getDebugLoc());
880 RawRustStringOstream MessageOS(MessageOut);
881 MessageOS << Opt->getMsg();
885 LLVMRustUnpackInlineAsmDiagnostic(LLVMDiagnosticInfoRef DI, unsigned *CookieOut,
886 LLVMTwineRef *MessageOut,
887 LLVMValueRef *InstructionOut) {
888 // Undefined to call this not on an inline assembly diagnostic!
889 llvm::DiagnosticInfoInlineAsm *IA =
890 static_cast<llvm::DiagnosticInfoInlineAsm *>(unwrap(DI));
892 *CookieOut = IA->getLocCookie();
893 *MessageOut = wrap(&IA->getMsgStr());
894 *InstructionOut = wrap(IA->getInstruction());
897 extern "C" void LLVMRustWriteDiagnosticInfoToString(LLVMDiagnosticInfoRef DI,
899 RawRustStringOstream OS(Str);
900 DiagnosticPrinterRawOStream DP(OS);
901 unwrap(DI)->print(DP);
904 enum class LLVMRustDiagnosticKind {
908 DebugMetadataVersion,
911 OptimizationRemarkMissed,
912 OptimizationRemarkAnalysis,
913 OptimizationRemarkAnalysisFPCommute,
914 OptimizationRemarkAnalysisAliasing,
915 OptimizationRemarkOther,
919 static LLVMRustDiagnosticKind toRust(DiagnosticKind Kind) {
922 return LLVMRustDiagnosticKind::InlineAsm;
924 return LLVMRustDiagnosticKind::StackSize;
925 case DK_DebugMetadataVersion:
926 return LLVMRustDiagnosticKind::DebugMetadataVersion;
927 case DK_SampleProfile:
928 return LLVMRustDiagnosticKind::SampleProfile;
929 case DK_OptimizationRemark:
930 return LLVMRustDiagnosticKind::OptimizationRemark;
931 case DK_OptimizationRemarkMissed:
932 return LLVMRustDiagnosticKind::OptimizationRemarkMissed;
933 case DK_OptimizationRemarkAnalysis:
934 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysis;
935 #if LLVM_VERSION_GE(3, 8)
936 case DK_OptimizationRemarkAnalysisFPCommute:
937 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysisFPCommute;
938 case DK_OptimizationRemarkAnalysisAliasing:
939 return LLVMRustDiagnosticKind::OptimizationRemarkAnalysisAliasing;
942 #if LLVM_VERSION_GE(3, 9)
943 return (Kind >= DK_FirstRemark && Kind <= DK_LastRemark)
944 ? LLVMRustDiagnosticKind::OptimizationRemarkOther
945 : LLVMRustDiagnosticKind::Other;
947 return LLVMRustDiagnosticKind::Other;
952 extern "C" LLVMRustDiagnosticKind
953 LLVMRustGetDiagInfoKind(LLVMDiagnosticInfoRef DI) {
954 return toRust((DiagnosticKind)unwrap(DI)->getKind());
956 // This is kept distinct from LLVMGetTypeKind, because when
957 // a new type kind is added, the Rust-side enum must be
958 // updated or UB will result.
959 extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {
960 switch (unwrap(Ty)->getTypeID()) {
962 return LLVMVoidTypeKind;
964 return LLVMHalfTypeKind;
965 case Type::FloatTyID:
966 return LLVMFloatTypeKind;
967 case Type::DoubleTyID:
968 return LLVMDoubleTypeKind;
969 case Type::X86_FP80TyID:
970 return LLVMX86_FP80TypeKind;
971 case Type::FP128TyID:
972 return LLVMFP128TypeKind;
973 case Type::PPC_FP128TyID:
974 return LLVMPPC_FP128TypeKind;
975 case Type::LabelTyID:
976 return LLVMLabelTypeKind;
977 case Type::MetadataTyID:
978 return LLVMMetadataTypeKind;
979 case Type::IntegerTyID:
980 return LLVMIntegerTypeKind;
981 case Type::FunctionTyID:
982 return LLVMFunctionTypeKind;
983 case Type::StructTyID:
984 return LLVMStructTypeKind;
985 case Type::ArrayTyID:
986 return LLVMArrayTypeKind;
987 case Type::PointerTyID:
988 return LLVMPointerTypeKind;
989 case Type::VectorTyID:
990 return LLVMVectorTypeKind;
991 case Type::X86_MMXTyID:
992 return LLVMX86_MMXTypeKind;
993 #if LLVM_VERSION_GE(3, 8)
994 case Type::TokenTyID:
995 return LLVMTokenTypeKind;
998 llvm_unreachable("Unhandled TypeID.");
1001 extern "C" void LLVMRustWriteDebugLocToString(LLVMContextRef C,
1003 RustStringRef Str) {
1004 RawRustStringOstream OS(Str);
1005 unwrap(DL)->print(OS);
1008 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SMDiagnostic, LLVMSMDiagnosticRef)
1010 extern "C" void LLVMRustSetInlineAsmDiagnosticHandler(
1011 LLVMContextRef C, LLVMContext::InlineAsmDiagHandlerTy H, void *CX) {
1012 unwrap(C)->setInlineAsmDiagnosticHandler(H, CX);
1015 extern "C" void LLVMRustWriteSMDiagnosticToString(LLVMSMDiagnosticRef D,
1016 RustStringRef Str) {
1017 RawRustStringOstream OS(Str);
1018 unwrap(D)->print("", OS);
1021 extern "C" LLVMValueRef
1022 LLVMRustBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1023 LLVMValueRef PersFn, unsigned NumClauses,
1024 const char *Name, LLVMValueRef F) {
1025 return LLVMBuildLandingPad(B, Ty, PersFn, NumClauses, Name);
1028 extern "C" LLVMValueRef LLVMRustBuildCleanupPad(LLVMBuilderRef B,
1029 LLVMValueRef ParentPad,
1031 LLVMValueRef *LLArgs,
1033 #if LLVM_VERSION_GE(3, 8)
1034 Value **Args = unwrap(LLArgs);
1035 if (ParentPad == nullptr) {
1036 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
1037 ParentPad = wrap(Constant::getNullValue(Ty));
1039 return wrap(unwrap(B)->CreateCleanupPad(
1040 unwrap(ParentPad), ArrayRef<Value *>(Args, ArgCount), Name));
1046 extern "C" LLVMValueRef LLVMRustBuildCleanupRet(LLVMBuilderRef B,
1047 LLVMValueRef CleanupPad,
1048 LLVMBasicBlockRef UnwindBB) {
1049 #if LLVM_VERSION_GE(3, 8)
1050 CleanupPadInst *Inst = cast<CleanupPadInst>(unwrap(CleanupPad));
1051 return wrap(unwrap(B)->CreateCleanupRet(Inst, unwrap(UnwindBB)));
1057 extern "C" LLVMValueRef
1058 LLVMRustBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
1059 unsigned ArgCount, LLVMValueRef *LLArgs, const char *Name) {
1060 #if LLVM_VERSION_GE(3, 8)
1061 Value **Args = unwrap(LLArgs);
1062 return wrap(unwrap(B)->CreateCatchPad(
1063 unwrap(ParentPad), ArrayRef<Value *>(Args, ArgCount), Name));
1069 extern "C" LLVMValueRef LLVMRustBuildCatchRet(LLVMBuilderRef B,
1071 LLVMBasicBlockRef BB) {
1072 #if LLVM_VERSION_GE(3, 8)
1073 return wrap(unwrap(B)->CreateCatchRet(cast<CatchPadInst>(unwrap(Pad)),
1080 extern "C" LLVMValueRef LLVMRustBuildCatchSwitch(LLVMBuilderRef B,
1081 LLVMValueRef ParentPad,
1082 LLVMBasicBlockRef BB,
1083 unsigned NumHandlers,
1085 #if LLVM_VERSION_GE(3, 8)
1086 if (ParentPad == nullptr) {
1087 Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
1088 ParentPad = wrap(Constant::getNullValue(Ty));
1090 return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(BB),
1091 NumHandlers, Name));
1097 extern "C" void LLVMRustAddHandler(LLVMValueRef CatchSwitchRef,
1098 LLVMBasicBlockRef Handler) {
1099 #if LLVM_VERSION_GE(3, 8)
1100 Value *CatchSwitch = unwrap(CatchSwitchRef);
1101 cast<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Handler));
1105 #if LLVM_VERSION_GE(3, 8)
1106 extern "C" OperandBundleDef *LLVMRustBuildOperandBundleDef(const char *Name,
1107 LLVMValueRef *Inputs,
1108 unsigned NumInputs) {
1109 return new OperandBundleDef(Name, makeArrayRef(unwrap(Inputs), NumInputs));
1112 extern "C" void LLVMRustFreeOperandBundleDef(OperandBundleDef *Bundle) {
1116 extern "C" LLVMValueRef LLVMRustBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1117 LLVMValueRef *Args, unsigned NumArgs,
1118 OperandBundleDef *Bundle,
1120 unsigned Len = Bundle ? 1 : 0;
1121 ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, Len);
1122 return wrap(unwrap(B)->CreateCall(
1123 unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs), Bundles, Name));
1126 extern "C" LLVMValueRef
1127 LLVMRustBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
1128 unsigned NumArgs, LLVMBasicBlockRef Then,
1129 LLVMBasicBlockRef Catch, OperandBundleDef *Bundle,
1131 unsigned Len = Bundle ? 1 : 0;
1132 ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, Len);
1133 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1134 makeArrayRef(unwrap(Args), NumArgs),
1138 extern "C" void *LLVMRustBuildOperandBundleDef(const char *Name,
1139 LLVMValueRef *Inputs,
1140 unsigned NumInputs) {
1144 extern "C" void LLVMRustFreeOperandBundleDef(void *Bundle) {}
1146 extern "C" LLVMValueRef LLVMRustBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1147 LLVMValueRef *Args, unsigned NumArgs,
1148 void *Bundle, const char *Name) {
1149 return LLVMBuildCall(B, Fn, Args, NumArgs, Name);
1152 extern "C" LLVMValueRef
1153 LLVMRustBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
1154 unsigned NumArgs, LLVMBasicBlockRef Then,
1155 LLVMBasicBlockRef Catch, void *Bundle, const char *Name) {
1156 return LLVMBuildInvoke(B, Fn, Args, NumArgs, Then, Catch, Name);
1160 extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
1161 LLVMBasicBlockRef BB) {
1162 auto Point = unwrap(BB)->getFirstInsertionPt();
1163 unwrap(B)->SetInsertPoint(unwrap(BB), Point);
1166 extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V,
1168 Triple TargetTriple(unwrap(M)->getTargetTriple());
1169 GlobalObject *GV = unwrap<GlobalObject>(V);
1170 if (!TargetTriple.isOSBinFormatMachO()) {
1171 GV->setComdat(unwrap(M)->getOrInsertComdat(Name));
1175 extern "C" void LLVMRustUnsetComdat(LLVMValueRef V) {
1176 GlobalObject *GV = unwrap<GlobalObject>(V);
1177 GV->setComdat(nullptr);
1180 enum class LLVMRustLinkage {
1181 ExternalLinkage = 0,
1182 AvailableExternallyLinkage = 1,
1183 LinkOnceAnyLinkage = 2,
1184 LinkOnceODRLinkage = 3,
1187 AppendingLinkage = 6,
1188 InternalLinkage = 7,
1190 ExternalWeakLinkage = 9,
1194 static LLVMRustLinkage toRust(LLVMLinkage Linkage) {
1196 case LLVMExternalLinkage:
1197 return LLVMRustLinkage::ExternalLinkage;
1198 case LLVMAvailableExternallyLinkage:
1199 return LLVMRustLinkage::AvailableExternallyLinkage;
1200 case LLVMLinkOnceAnyLinkage:
1201 return LLVMRustLinkage::LinkOnceAnyLinkage;
1202 case LLVMLinkOnceODRLinkage:
1203 return LLVMRustLinkage::LinkOnceODRLinkage;
1204 case LLVMWeakAnyLinkage:
1205 return LLVMRustLinkage::WeakAnyLinkage;
1206 case LLVMWeakODRLinkage:
1207 return LLVMRustLinkage::WeakODRLinkage;
1208 case LLVMAppendingLinkage:
1209 return LLVMRustLinkage::AppendingLinkage;
1210 case LLVMInternalLinkage:
1211 return LLVMRustLinkage::InternalLinkage;
1212 case LLVMPrivateLinkage:
1213 return LLVMRustLinkage::PrivateLinkage;
1214 case LLVMExternalWeakLinkage:
1215 return LLVMRustLinkage::ExternalWeakLinkage;
1216 case LLVMCommonLinkage:
1217 return LLVMRustLinkage::CommonLinkage;
1219 llvm_unreachable("Invalid LLVMRustLinkage value!");
1223 static LLVMLinkage fromRust(LLVMRustLinkage Linkage) {
1225 case LLVMRustLinkage::ExternalLinkage:
1226 return LLVMExternalLinkage;
1227 case LLVMRustLinkage::AvailableExternallyLinkage:
1228 return LLVMAvailableExternallyLinkage;
1229 case LLVMRustLinkage::LinkOnceAnyLinkage:
1230 return LLVMLinkOnceAnyLinkage;
1231 case LLVMRustLinkage::LinkOnceODRLinkage:
1232 return LLVMLinkOnceODRLinkage;
1233 case LLVMRustLinkage::WeakAnyLinkage:
1234 return LLVMWeakAnyLinkage;
1235 case LLVMRustLinkage::WeakODRLinkage:
1236 return LLVMWeakODRLinkage;
1237 case LLVMRustLinkage::AppendingLinkage:
1238 return LLVMAppendingLinkage;
1239 case LLVMRustLinkage::InternalLinkage:
1240 return LLVMInternalLinkage;
1241 case LLVMRustLinkage::PrivateLinkage:
1242 return LLVMPrivateLinkage;
1243 case LLVMRustLinkage::ExternalWeakLinkage:
1244 return LLVMExternalWeakLinkage;
1245 case LLVMRustLinkage::CommonLinkage:
1246 return LLVMCommonLinkage;
1248 llvm_unreachable("Invalid LLVMRustLinkage value!");
1251 extern "C" LLVMRustLinkage LLVMRustGetLinkage(LLVMValueRef V) {
1252 return toRust(LLVMGetLinkage(V));
1255 extern "C" void LLVMRustSetLinkage(LLVMValueRef V,
1256 LLVMRustLinkage RustLinkage) {
1257 LLVMSetLinkage(V, fromRust(RustLinkage));
1260 // Returns true if both high and low were successfully set. Fails in case constant wasn’t any of
1261 // the common sizes (1, 8, 16, 32, 64, 128 bits)
1262 extern "C" bool LLVMRustConstInt128Get(LLVMValueRef CV, bool sext, uint64_t *high, uint64_t *low)
1264 auto C = unwrap<llvm::ConstantInt>(CV);
1265 if (C->getBitWidth() > 128) { return false; }
1268 AP = C->getValue().sextOrSelf(128);
1270 AP = C->getValue().zextOrSelf(128);
1272 *low = AP.getLoBits(64).getZExtValue();
1273 *high = AP.getHiBits(64).getZExtValue();
1277 extern "C" LLVMContextRef LLVMRustGetValueContext(LLVMValueRef V) {
1278 return wrap(&unwrap(V)->getContext());
1281 enum class LLVMRustVisibility {
1287 static LLVMRustVisibility toRust(LLVMVisibility Vis) {
1289 case LLVMDefaultVisibility:
1290 return LLVMRustVisibility::Default;
1291 case LLVMHiddenVisibility:
1292 return LLVMRustVisibility::Hidden;
1293 case LLVMProtectedVisibility:
1294 return LLVMRustVisibility::Protected;
1296 llvm_unreachable("Invalid LLVMRustVisibility value!");
1299 static LLVMVisibility fromRust(LLVMRustVisibility Vis) {
1301 case LLVMRustVisibility::Default:
1302 return LLVMDefaultVisibility;
1303 case LLVMRustVisibility::Hidden:
1304 return LLVMHiddenVisibility;
1305 case LLVMRustVisibility::Protected:
1306 return LLVMProtectedVisibility;
1308 llvm_unreachable("Invalid LLVMRustVisibility value!");
1311 extern "C" LLVMRustVisibility LLVMRustGetVisibility(LLVMValueRef V) {
1312 return toRust(LLVMGetVisibility(V));
1315 // Oh hey, a binding that makes sense for once? (because LLVM’s own do not)
1316 extern "C" LLVMValueRef LLVMRustBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1317 LLVMTypeRef DestTy, bool isSigned) {
1318 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), isSigned, ""));
1321 extern "C" void LLVMRustSetVisibility(LLVMValueRef V,
1322 LLVMRustVisibility RustVisibility) {
1323 LLVMSetVisibility(V, fromRust(RustVisibility));