/
llvmhelpers.h
239 lines (190 loc) · 8.81 KB
/
llvmhelpers.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//===-- gen/llvmhelpers.h - General LLVM codegen helpers --------*- C++ -*-===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// General codegen helper constructs.
//
// TODO: Merge with gen/tollvm.h, then refactor into sensible parts.
//
//===----------------------------------------------------------------------===//
#ifndef LDC_GEN_LLVMHELPERS_H
#define LDC_GEN_LLVMHELPERS_H
#include "mtype.h"
#include "statement.h"
#include "gen/dvalue.h"
#include "gen/llvm.h"
#include "ir/irfuncty.h"
// this is used for tracking try-finally scopes
struct EnclosingTryFinally
{
TryFinallyStatement* tf;
llvm::BasicBlock* landingPad;
void emitCode(IRState* p);
EnclosingTryFinally(TryFinallyStatement* _tf, llvm::BasicBlock* _pad)
: tf(_tf), landingPad(_pad) {}
};
// dynamic memory helpers
LLValue* DtoNew(Loc& loc, Type* newtype);
void DtoDeleteMemory(Loc& loc, LLValue* ptr);
void DtoDeleteClass(Loc& loc, LLValue* inst);
void DtoDeleteInterface(Loc& loc, LLValue* inst);
void DtoDeleteArray(Loc& loc, DValue* arr);
// emit an alloca
llvm::AllocaInst* DtoAlloca(Type* type, const char* name = "");
llvm::AllocaInst* DtoArrayAlloca(Type* type, unsigned arraysize, const char* name = "");
llvm::AllocaInst* DtoRawAlloca(LLType* lltype, size_t alignment, const char* name = "");
LLValue* DtoGcMalloc(Loc& loc, LLType* lltype, const char* name = "");
// assertion generator
void DtoAssert(Module* M, Loc& loc, DValue* msg);
// returns module file name
LLValue* DtoModuleFileName(Module* M, const Loc& loc);
/// emits goto to LabelStatement with the target identifier
/// the sourceFinally is only used for error checking
void DtoGoto(Loc &loc, LabelDsymbol *target, TryFinallyStatement *sourceFinally);
// Generates IR for enclosing handlers between the current state and
// the scope created by the 'target' statement.
void DtoEnclosingHandlers(Loc& loc, Statement* target);
/// Enters a critical section.
void DtoEnterCritical(Loc& loc, LLValue* g);
/// leaves a critical section.
void DtoLeaveCritical(Loc& loc, LLValue* g);
/// Enters a monitor lock.
void DtoEnterMonitor(Loc& loc, LLValue* v);
/// Leaves a monitor lock.
void DtoLeaveMonitor(Loc& loc, LLValue* v);
// basic operations
void DtoAssign(Loc& loc, DValue* lhs, DValue* rhs, int op = -1, bool canSkipPostblit = false);
DValue* DtoSymbolAddress(Loc& loc, Type* type, Declaration* decl);
llvm::Constant* DtoConstSymbolAddress(Loc& loc,Declaration* decl);
/// Create a null DValue.
DValue* DtoNullValue(Type* t, Loc loc = Loc());
// casts
DValue* DtoCastInt(Loc& loc, DValue* val, Type* to);
DValue* DtoCastPtr(Loc& loc, DValue* val, Type* to);
DValue* DtoCastFloat(Loc& loc, DValue* val, Type* to);
DValue* DtoCastDelegate(Loc& loc, DValue* val, Type* to);
DValue* DtoCast(Loc& loc, DValue* val, Type* to);
// return the same val as passed in, modified to the target type, if possible, otherwise returns a new DValue
DValue* DtoPaintType(Loc& loc, DValue* val, Type* to);
// is template instance check, returns module where instantiated
TemplateInstance* DtoIsTemplateInstance(Dsymbol* s);
/// Makes sure the declarations corresponding to the given D symbol have been
/// emitted to the currently processed LLVM module.
///
/// This means that dsym->ir can be expected to set to reasonable values.
///
/// This function does *not* emit any (function, variable) *definitions*; this
/// is done by Dsymbol::codegen.
void DtoResolveDsymbol(Dsymbol* dsym);
void DtoResolveVariable(VarDeclaration* var);
// declaration inside a declarationexp
void DtoVarDeclaration(VarDeclaration* var);
DValue* DtoDeclarationExp(Dsymbol* declaration);
LLValue* DtoRawVarDeclaration(VarDeclaration* var, LLValue* addr = 0);
// initializer helpers
LLConstant* DtoConstInitializer(Loc& loc, Type* type, Initializer* init);
LLConstant* DtoConstExpInit(Loc& loc, Type* targetType, Expression* exp);
// getting typeinfo of type, base=true casts to object.TypeInfo
LLConstant* DtoTypeInfoOf(Type* ty, bool base=true);
// binary operations
DValue* DtoBinAdd(DValue* lhs, DValue* rhs);
DValue* DtoBinSub(DValue* lhs, DValue* rhs);
// these binops need an explicit result type to handling
// to give 'ifloat op float' and 'float op ifloat' the correct type
DValue* DtoBinMul(Type* resulttype, DValue* lhs, DValue* rhs);
DValue* DtoBinDiv(Type* resulttype, DValue* lhs, DValue* rhs);
DValue* DtoBinRem(Type* resulttype, DValue* lhs, DValue* rhs);
LLValue* DtoBinNumericEquals(Loc& loc, DValue* lhs, DValue* rhs, TOK op);
LLValue* DtoBinFloatsEquals(Loc& loc, DValue* lhs, DValue* rhs, TOK op);
// target stuff
void findDefaultTarget();
/// Fixup an overloaded intrinsic name string.
void DtoOverloadedIntrinsicName(TemplateInstance* ti, TemplateDeclaration* td, std::string& name);
/// Returns true if there is any unaligned type inside the aggregate.
bool hasUnalignedFields(Type* t);
/// Returns a pointer to the given member field of an aggregate.
///
/// 'src' is a pointer to the start of the memory of an 'ad' instance.
LLValue* DtoIndexAggregate(LLValue* src, AggregateDeclaration* ad, VarDeclaration* vd);
/// Returns the index of a given member variable in the resulting LLVM type of
/// an aggregate.
///
/// This is only a valid operation if the field is known to be non-overlapping,
/// so that no byte-wise offset is needed.
unsigned getFieldGEPIndex(AggregateDeclaration* ad, VarDeclaration* vd);
///
DValue* DtoInlineAsmExpr(Loc& loc, FuncDeclaration* fd, Expressions* arguments);
/// Update an offset to make sure it follows both the D and LLVM alignments.
/// Returns the offset rounded up to the closest safely aligned offset.
size_t realignOffset(size_t offset, Type* type);
/// Returns the llvm::Value of the passed DValue, making sure that it is an
/// lvalue (has a memory address), so it can be passed to the D runtime
/// functions without problems.
LLValue* makeLValue(Loc& loc, DValue* value);
void callPostblit(Loc& loc, Expression *exp, LLValue *val);
/// Returns whether the given variable is a DMD-internal "ref variable".
///
/// D doesn't have reference variables (the ref keyword is only usable in
/// function signatures and foreach headers), but the DMD frontend internally
/// creates them in cases like lowering a ref foreach to a for loop or the
/// implicit __result variable for ref-return functions with out contracts.
bool isSpecialRefVar(VarDeclaration* vd);
/// Returns whether the type is unsigned in LLVM terms, which also includes
/// pointers.
bool isLLVMUnsigned(Type* t);
/// Converts a DMD comparison operation token into the corresponding LLVM icmp
/// predicate for the given operand signedness.
///
/// For some operations, the result can be a constant. In this case outConst is
/// set to it, otherwise outPred is set to the predicate to use.
void tokToIcmpPred(TOK op, bool isUnsigned, llvm::ICmpInst::Predicate* outPred,
llvm::Value** outConst);
////////////////////////////////////////////
// gen/tocall.cpp stuff below
////////////////////////////////////////////
///
IrFuncTy &DtoIrTypeFunction(DValue* fnval);
///
TypeFunction* DtoTypeFunction(DValue* fnval);
///
LLValue* DtoCallableValue(DValue* fn);
///
LLFunctionType* DtoExtractFunctionType(LLType* type);
///
DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions* arguments, LLValue* retvar = 0);
Type* stripModifiers(Type* type, bool transitive = false);
void printLabelName(std::ostream& target, const char* func_mangle, const char* label_name);
void AppendFunctionToLLVMGlobalCtorsDtors(llvm::Function* func, const uint32_t priority, const bool isCtor);
template <typename T>
LLConstant* toConstantArray(LLType* ct, LLArrayType* at, T* str, size_t len, bool nullterm = true)
{
std::vector<LLConstant*> vals;
vals.reserve(len+1);
for (size_t i = 0; i < len; ++i) {
vals.push_back(LLConstantInt::get(ct, str[i], false));
}
if (nullterm)
vals.push_back(LLConstantInt::get(ct, 0, false));
return LLConstantArray::get(at, vals);
}
/// Tries to create an LLVM global with the given properties. If a variable with
/// the same mangled name already exists, checks if the types match and returns
/// it instead.
///
/// Necessary to support multiple declarations with the same mangled name, as
/// can be the case due to pragma(mangle).
llvm::GlobalVariable* getOrCreateGlobal(Loc& loc, llvm::Module& module,
llvm::Type* type, bool isConstant, llvm::GlobalValue::LinkageTypes linkage,
llvm::Constant* init, llvm::StringRef name, bool isThreadLocal = false);
FuncDeclaration* getParentFunc(Dsymbol* sym, bool stopOnStatic);
void Declaration_codegen(Dsymbol *decl);
void Declaration_codegen(Dsymbol *decl, IRState *irs);
DValue *toElem(Expression *e);
DValue *toElemDtor(Expression *e);
LLConstant *toConstElem(Expression *e, IRState *p);
#endif