forked from tinygo-org/go-llvm
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathIRBindings.cpp
More file actions
159 lines (142 loc) · 5.62 KB
/
Copy pathIRBindings.cpp
File metadata and controls
159 lines (142 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//===- IRBindings.cpp - Additional bindings for ir ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines additional C bindings for the ir component.
//
//===----------------------------------------------------------------------===//
#include "IRBindings.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include <algorithm>
using namespace llvm;
LLVMValueRef LLVMGoConstFPFromBits(LLVMTypeRef Ty, const uint64_t *Words,
unsigned NumWords) {
if (!Ty || !unwrap(Ty)->isFloatingPointTy())
return nullptr;
auto *T = unwrap(Ty);
unsigned BitWidth = T->getScalarSizeInBits();
if (NumWords != (BitWidth + 63) / 64 || !Words)
return nullptr;
if (BitWidth % 64 && (Words[NumWords - 1] >> (BitWidth % 64)))
return nullptr;
#if LLVM_VERSION_MAJOR >= 22
return LLVMConstFPFromBits(Ty, Words);
#else
return wrap(ConstantFP::get(
T->getContext(), APFloat(T->getFltSemantics(),
APInt(BitWidth, ArrayRef<uint64_t>(Words, NumWords)))));
#endif
}
unsigned LLVMGoConstFPGetBits(LLVMValueRef Val, uint64_t *Words) {
if (!Val)
return 0;
auto *FP = dyn_cast<ConstantFP>(unwrap(Val));
if (!FP)
return 0;
APInt Bits = FP->getValueAPF().bitcastToAPInt();
unsigned NumWords = Bits.getNumWords();
if (Words)
std::copy_n(Bits.getRawData(), NumWords, Words);
return NumWords;
}
LLVMAttributeRef LLVMGoCreateConstantRangeAttribute(
LLVMContextRef C, unsigned KindID, unsigned NumBits,
const uint64_t *LowerWords, const uint64_t *UpperWords) {
#if LLVM_VERSION_MAJOR >= 19
return LLVMCreateConstantRangeAttribute(C, KindID, NumBits, LowerWords,
UpperWords);
#else
return nullptr;
#endif
}
LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
}
LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
}
LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
unsigned Count) {
return wrap(
MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
}
void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
LLVMMetadataRef Val) {
NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
if (!N)
return;
if (!Val)
return;
N->addOperand(unwrap<MDNode>(Val));
}
void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
unwrap<Instruction>(Inst)->setMetadata(KindID, N);
}
void LLVMGoSetCurrentDebugLocation(LLVMBuilderRef Bref, unsigned Line,
unsigned Col, LLVMMetadataRef Scope,
LLVMMetadataRef InlinedAt) {
if (!Scope)
unwrap(Bref)->SetCurrentDebugLocation(DebugLoc());
else
unwrap(Bref)->SetCurrentDebugLocation(DILocation::get(
unwrap<MDNode>(Scope)->getContext(), Line, Col, unwrap<MDNode>(Scope),
InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
}
LLVMDebugLocMetadata LLVMGoGetCurrentDebugLocation(LLVMBuilderRef Bref) {
const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
const auto* InlinedAt = Loc.getInlinedAt();
const LLVMDebugLocMetadata md{
Loc.getLine(),
Loc.getCol(),
wrap(Loc.getScope()),
InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),
};
return md;
}
LLVMValueRef LLVMGoGetInlineAsm(LLVMTypeRef Ty, char *AsmString,
size_t AsmStringSize, char *Constraints,
size_t ConstraintsSize, LLVMBool HasSideEffects,
LLVMBool IsAlignStack,
LLVMInlineAsmDialect Dialect, LLVMBool CanThrow)
{
return LLVMGetInlineAsm(Ty, AsmString,
AsmStringSize, Constraints,
ConstraintsSize, HasSideEffects,
IsAlignStack,
Dialect, CanThrow);
}
LLVMValueRef LLVMGoBuildIntrinsicCall(LLVMBuilderRef B, LLVMTypeRef RetTy,
unsigned ID, LLVMValueRef *Args,
unsigned Count, const char *Name) {
Intrinsic::ID IntrinsicID = static_cast<Intrinsic::ID>(ID);
ArrayRef<Value *> UnwrappedArgs(unwrap(Args), Count);
SmallVector<Type *, 8> ParamTys;
SmallVector<Type *, 8> OverloadTys;
SmallVector<Intrinsic::IITDescriptor, 8> Infos;
ParamTys.reserve(Count);
for (Value *Arg : UnwrappedArgs)
ParamTys.push_back(Arg->getType());
FunctionType *FTy = FunctionType::get(unwrap(RetTy), ParamTys, false);
Intrinsic::getIntrinsicInfoTableEntries(IntrinsicID, Infos);
ArrayRef<Intrinsic::IITDescriptor> InfoRef(Infos);
if (Intrinsic::matchIntrinsicSignature(FTy, InfoRef, OverloadTys) !=
Intrinsic::MatchIntrinsicTypes_Match)
return nullptr;
return wrap(unwrap(B)->CreateIntrinsic(IntrinsicID, OverloadTys,
UnwrappedArgs, nullptr, Name));
}