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
|
//===-- aa.cpp ------------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/aa.h"
#include "dmd/aggregate.h"
#include "dmd/declaration.h"
#include "dmd/module.h"
#include "dmd/mtype.h"
#include "gen/arrays.h"
#include "gen/dvalue.h"
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/runtime.h"
#include "gen/tollvm.h"
#include "ir/irfunction.h"
#include "ir/irmodule.h"
// returns the keytype typeinfo
static LLConstant *to_keyti(const Loc &loc, DValue *aa, LLType *targetType) {
// keyti param
assert(aa->type->toBasetype()->ty == TY::Taarray);
TypeAArray *aatype = static_cast<TypeAArray *>(aa->type->toBasetype());
LLConstant *ti = DtoTypeInfoOf(loc, aatype->index, /*base=*/false);
return DtoBitCast(ti, targetType);
}
////////////////////////////////////////////////////////////////////////////////
DLValue *DtoAAIndex(const Loc &loc, Type *type, DValue *aa, DValue *key,
bool lvalue) {
// D2:
// call:
// extern(C) void* _aaGetY(AA* aa, TypeInfo aati, size_t valuesize, void*
// pkey)
// or
// extern(C) void* _aaInX(AA aa*, TypeInfo keyti, void* pkey)
// first get the runtime function
llvm::Function *func =
getRuntimeFunction(loc, gIR->module, lvalue ? "_aaGetY" : "_aaInX");
LLFunctionType *funcTy = func->getFunctionType();
// aa param
LLValue *aaval = lvalue ? DtoLVal(aa) : DtoRVal(aa);
aaval = DtoBitCast(aaval, funcTy->getParamType(0));
// pkey param
LLValue *pkey = makeLValue(loc, key);
pkey = DtoBitCast(pkey, funcTy->getParamType(lvalue ? 3 : 2));
// call runtime
LLValue *ret;
if (lvalue) {
LLValue *rawAATI =
DtoTypeInfoOf(loc, aa->type->unSharedOf()->mutableOf(), /*base=*/false);
LLValue *castedAATI = DtoBitCast(rawAATI, funcTy->getParamType(1));
LLValue *valsize = DtoConstSize_t(getTypeAllocSize(DtoType(type)));
ret = gIR->CreateCallOrInvoke(func, aaval, castedAATI, valsize, pkey,
"aa.index");
} else {
LLValue *keyti = to_keyti(loc, aa, funcTy->getParamType(1));
ret = gIR->CreateCallOrInvoke(func, aaval, keyti, pkey, "aa.index");
}
// cast return value
LLType *targettype = DtoPtrToType(type);
if (ret->getType() != targettype) {
ret = DtoBitCast(ret, targettype);
}
// Only check bounds for rvalues ('aa[key]').
// Lvalue use ('aa[key] = value') auto-adds an element.
if (!lvalue && gIR->emitArrayBoundsChecks()) {
llvm::BasicBlock *okbb = gIR->insertBB("aaboundsok");
llvm::BasicBlock *failbb = gIR->insertBBAfter(okbb, "aaboundscheckfail");
LLValue *nullaa = LLConstant::getNullValue(ret->getType());
LLValue *cond = gIR->ir->CreateICmpNE(nullaa, ret, "aaboundscheck");
gIR->ir->CreateCondBr(cond, okbb, failbb);
// set up failbb to call the array bounds error runtime function
gIR->ir->SetInsertPoint(failbb);
emitRangeError(gIR, loc);
// if ok, proceed in okbb
gIR->ir->SetInsertPoint(okbb);
}
return new DLValue(type, ret);
}
////////////////////////////////////////////////////////////////////////////////
DValue *DtoAAIn(const Loc &loc, Type *type, DValue *aa, DValue *key) {
// D1:
// call:
// extern(C) void* _aaIn(AA aa*, TypeInfo keyti, void* pkey)
// D2:
// call:
// extern(C) void* _aaInX(AA aa*, TypeInfo keyti, void* pkey)
// first get the runtime function
llvm::Function *func = getRuntimeFunction(loc, gIR->module, "_aaInX");
LLFunctionType *funcTy = func->getFunctionType();
IF_LOG Logger::cout() << "_aaIn = " << *func << '\n';
// aa param
LLValue *aaval = DtoRVal(aa);
IF_LOG {
Logger::cout() << "aaval: " << *aaval << '\n';
Logger::cout() << "totype: " << *funcTy->getParamType(0) << '\n';
}
aaval = DtoBitCast(aaval, funcTy->getParamType(0));
// keyti param
LLValue *keyti = to_keyti(loc, aa, funcTy->getParamType(1));
// pkey param
LLValue *pkey = makeLValue(loc, key);
pkey = DtoBitCast(pkey, getVoidPtrType());
// call runtime
LLValue *ret = gIR->CreateCallOrInvoke(func, aaval, keyti, pkey, "aa.in");
// cast return value
LLType *targettype = DtoType(type);
if (ret->getType() != targettype) {
ret = DtoBitCast(ret, targettype);
}
return new DImValue(type, ret);
}
////////////////////////////////////////////////////////////////////////////////
DValue *DtoAARemove(const Loc &loc, DValue *aa, DValue *key) {
// D1:
// call:
// extern(C) void _aaDel(AA aa, TypeInfo keyti, void* pkey)
// D2:
// call:
// extern(C) bool _aaDelX(AA aa, TypeInfo keyti, void* pkey)
// first get the runtime function
llvm::Function *func = getRuntimeFunction(loc, gIR->module, "_aaDelX");
LLFunctionType *funcTy = func->getFunctionType();
IF_LOG Logger::cout() << "_aaDel = " << *func << '\n';
// aa param
LLValue *aaval = DtoRVal(aa);
IF_LOG {
Logger::cout() << "aaval: " << *aaval << '\n';
Logger::cout() << "totype: " << *funcTy->getParamType(0) << '\n';
}
aaval = DtoBitCast(aaval, funcTy->getParamType(0));
// keyti param
LLValue *keyti = to_keyti(loc, aa, funcTy->getParamType(1));
// pkey param
LLValue *pkey = makeLValue(loc, key);
pkey = DtoBitCast(pkey, funcTy->getParamType(2));
// call runtime
LLValue *res = gIR->CreateCallOrInvoke(func, aaval, keyti, pkey);
return new DImValue(Type::tbool, res);
}
////////////////////////////////////////////////////////////////////////////////
LLValue *DtoAAEquals(const Loc &loc, EXP op, DValue *l, DValue *r) {
Type *t = l->type->toBasetype();
assert(t == r->type->toBasetype() &&
"aa equality is only defined for aas of same type");
llvm::Function *func = getRuntimeFunction(loc, gIR->module, "_aaEqual");
LLFunctionType *funcTy = func->getFunctionType();
LLValue *aaval = DtoBitCast(DtoRVal(l), funcTy->getParamType(1));
LLValue *abval = DtoBitCast(DtoRVal(r), funcTy->getParamType(2));
LLValue *aaTypeInfo = DtoTypeInfoOf(loc, t);
LLValue *res =
gIR->CreateCallOrInvoke(func, aaTypeInfo, aaval, abval, "aaEqRes");
const auto predicate = eqTokToICmpPred(op, /* invert = */ true);
res = gIR->ir->CreateICmp(predicate, res, DtoConstInt(0));
return res;
}
|