• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- IRBindings.cpp - Additional bindings for ir ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines additional C bindings for the ir component.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "IRBindings.h"
15 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/DebugLoc.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
21 
22 using namespace llvm;
23 
LLVMAddFunctionAttr2(LLVMValueRef Fn,uint64_t PA)24 void LLVMAddFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
25   Function *Func = unwrap<Function>(Fn);
26   const AttributeSet PAL = Func->getAttributes();
27   AttrBuilder B(PA);
28   const AttributeSet PALnew =
29     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
30                       AttributeSet::get(Func->getContext(),
31                                         AttributeSet::FunctionIndex, B));
32   Func->setAttributes(PALnew);
33 }
34 
LLVMGetFunctionAttr2(LLVMValueRef Fn)35 uint64_t LLVMGetFunctionAttr2(LLVMValueRef Fn) {
36   Function *Func = unwrap<Function>(Fn);
37   const AttributeSet PAL = Func->getAttributes();
38   return PAL.Raw(AttributeSet::FunctionIndex);
39 }
40 
LLVMRemoveFunctionAttr2(LLVMValueRef Fn,uint64_t PA)41 void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
42   Function *Func = unwrap<Function>(Fn);
43   const AttributeSet PAL = Func->getAttributes();
44   AttrBuilder B(PA);
45   const AttributeSet PALnew =
46     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
47                          AttributeSet::get(Func->getContext(),
48                                            AttributeSet::FunctionIndex, B));
49   Func->setAttributes(PALnew);
50 }
51 
LLVMConstantAsMetadata(LLVMValueRef C)52 LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
53   return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
54 }
55 
LLVMMDString2(LLVMContextRef C,const char * Str,unsigned SLen)56 LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
57   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
58 }
59 
LLVMMDNode2(LLVMContextRef C,LLVMMetadataRef * MDs,unsigned Count)60 LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
61                             unsigned Count) {
62   return wrap(
63       MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
64 }
65 
LLVMTemporaryMDNode(LLVMContextRef C,LLVMMetadataRef * MDs,unsigned Count)66 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs,
67                                     unsigned Count) {
68   return wrap(MDTuple::getTemporary(*unwrap(C),
69                                     ArrayRef<Metadata *>(unwrap(MDs), Count))
70                   .release());
71 }
72 
LLVMAddNamedMetadataOperand2(LLVMModuleRef M,const char * name,LLVMMetadataRef Val)73 void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
74                                   LLVMMetadataRef Val) {
75   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
76   if (!N)
77     return;
78   if (!Val)
79     return;
80   N->addOperand(unwrap<MDNode>(Val));
81 }
82 
LLVMSetMetadata2(LLVMValueRef Inst,unsigned KindID,LLVMMetadataRef MD)83 void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
84   MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
85   unwrap<Instruction>(Inst)->setMetadata(KindID, N);
86 }
87 
LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD,LLVMMetadataRef New)88 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New) {
89   auto *Node = unwrap<MDNode>(MD);
90   Node->replaceAllUsesWith(unwrap<Metadata>(New));
91   MDNode::deleteTemporary(Node);
92 }
93 
LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref,unsigned Line,unsigned Col,LLVMMetadataRef Scope,LLVMMetadataRef InlinedAt)94 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
95                                   unsigned Col, LLVMMetadataRef Scope,
96                                   LLVMMetadataRef InlinedAt) {
97   unwrap(Bref)->SetCurrentDebugLocation(
98       DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
99                     InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
100 }
101 
LLVMSetSubprogram(LLVMValueRef Func,LLVMMetadataRef SP)102 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
103   unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
104 }
105