DValue *DtoAARemove(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 #if DMDV2 llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_aaDelX"); #else llvm::Function* func = LLVM_D_GetRuntimeFunction(gIR->module, "_aaDel"); #endif LLFunctionType* funcTy = func->getFunctionType(); if (Logger::enabled()) Logger::cout() << "_aaDel = " << *func << '\n'; // aa param LLValue* aaval = aa->getRVal(); if (Logger::enabled()) { Logger::cout() << "aaval: " << *aaval << '\n'; Logger::cout() << "totype: " << *funcTy->getParamType(0) << '\n'; } aaval = DtoBitCast(aaval, funcTy->getParamType(0)); // keyti param #if DMDV2 LLValue* keyti = to_keyti(aa); #else LLValue* keyti = to_keyti(key); #endif keyti = DtoBitCast(keyti, funcTy->getParamType(1)); // pkey param LLValue* pkey = makeLValue(loc, key); pkey = DtoBitCast(pkey, funcTy->getParamType(2)); // build arg vector LLSmallVector<LLValue*, 3> args; args.push_back(aaval); args.push_back(keyti); args.push_back(pkey); // call runtime LLCallSite call = gIR->CreateCallOrInvoke(func, args); #if DMDV2 return new DImValue(Type::tbool, call.getInstruction()); #else return NULL; #endif }
static void set_param_attrs(TypeFunction* f, llvm::Function* func, FuncDeclaration* fdecl) { LLSmallVector<llvm::AttributeWithIndex, 9> attrs; llvm::AttributeWithIndex PAWI; int idx = 0; // handle implicit args #define ADD_PA(X) \ if (f->fty.X) { \ if (f->fty.X->attrs) { \ PAWI.Index = idx; \ PAWI.Attrs = f->fty.X->attrs; \ attrs.push_back(PAWI); \ } \ idx++; \ } ADD_PA(ret) ADD_PA(arg_sret) ADD_PA(arg_this) ADD_PA(arg_nest) ADD_PA(arg_arguments) ADD_PA(arg_argptr) #undef ADD_PA // set attrs on the rest of the arguments size_t n = Parameter::dim(f->parameters); LLSmallVector<unsigned,8> attrptr(n, 0); for (size_t k = 0; k < n; ++k) { Parameter* fnarg = Parameter::getNth(f->parameters, k); assert(fnarg); attrptr[k] = f->fty.args[k]->attrs; } // reverse params? if (f->fty.reverseParams) { std::reverse(attrptr.begin(), attrptr.end()); } // build rest of attrs list for (int i = 0; i < n; i++) { if (attrptr[i]) { PAWI.Index = idx+i; PAWI.Attrs = attrptr[i]; attrs.push_back(PAWI); } } llvm::AttrListPtr attrlist = llvm::AttrListPtr::get(attrs.begin(), attrs.end()); func->setAttributes(attrlist); }
void DtoFinalizeClass(LLValue* inst) { // get runtime function llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_callfinalizer"); // build args LLSmallVector<LLValue*,1> arg; arg.push_back(DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp")); // call gIR->CreateCallOrInvoke(fn, arg, ""); }
LLStructType* DtoInterfaceInfoType() { if (gIR->interfaceInfoType) return gIR->interfaceInfoType; // build interface info type LLSmallVector<LLType*, 3> types; // ClassInfo classinfo ClassDeclaration* cd2 = ClassDeclaration::classinfo; DtoResolveClass(cd2); types.push_back(DtoType(cd2->type)); // void*[] vtbl LLSmallVector<LLType*, 2> vtbltypes; vtbltypes.push_back(DtoSize_t()); LLType* byteptrptrty = getPtrToType(getPtrToType(LLType::getInt8Ty(gIR->context()))); vtbltypes.push_back(byteptrptrty); types.push_back(LLStructType::get(gIR->context(), vtbltypes)); // int offset types.push_back(LLType::getInt32Ty(gIR->context())); // create type gIR->interfaceInfoType = LLStructType::get(gIR->context(), types); return gIR->interfaceInfoType; }
void IRLandingPad::constructLandingPad(llvm::BasicBlock* inBB) { // save and rewrite scope IRScope savedscope = gIR->scope(); gIR->scope() = IRScope(inBB,savedscope.end); // eh_ptr = llvm.eh.exception(); llvm::Function* eh_exception_fn = GET_INTRINSIC_DECL(eh_exception); LLValue* eh_ptr = gIR->ir->CreateCall(eh_exception_fn); // build selector arguments LLSmallVector<LLValue*, 6> selectorargs; // put in classinfos in the right order bool hasFinally = false; bool hasCatch = false; std::deque<IRLandingPadInfo>::iterator it = infos.begin(), end = infos.end(); for(; it != end; ++it) { if(it->finallyBody) hasFinally = true; else { hasCatch = true; assert(it->catchType); assert(it->catchType->ir.irStruct); selectorargs.insert(selectorargs.begin(), it->catchType->ir.irStruct->getClassInfoSymbol()); } } // if there's a finally, the eh table has to have a 0 action if(hasFinally) selectorargs.push_back(DtoConstUint(0)); // personality fn llvm::Function* personality_fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_eh_personality"); LLValue* personality_fn_arg = gIR->ir->CreateBitCast(personality_fn, getPtrToType(LLType::getInt8Ty(gIR->context()))); selectorargs.insert(selectorargs.begin(), personality_fn_arg); // eh storage target selectorargs.insert(selectorargs.begin(), eh_ptr); // if there is a catch and some catch allocated storage, store exception object if(hasCatch && catch_var) { const LLType* objectTy = DtoType(ClassDeclaration::object->type); gIR->ir->CreateStore(gIR->ir->CreateBitCast(eh_ptr, objectTy), catch_var); } // eh_sel = llvm.eh.selector(eh_ptr, cast(byte*)&_d_eh_personality, <selectorargs>); llvm::Function* eh_selector_fn = GET_INTRINSIC_DECL(eh_selector); LLValue* eh_sel = gIR->ir->CreateCall(eh_selector_fn, selectorargs.begin(), selectorargs.end()); // emit finallys and 'if' chain to catch the exception llvm::Function* eh_typeid_for_fn = GET_INTRINSIC_DECL(eh_typeid_for); std::deque<IRLandingPadInfo> infos = this->infos; std::stack<size_t> nInfos = this->nInfos; std::deque<IRLandingPadInfo>::reverse_iterator rit, rend = infos.rend(); for(rit = infos.rbegin(); rit != rend; ++rit) { // if it's a finally, emit its code if(rit->finallyBody) { size_t n = this->nInfos.top(); this->infos.resize(n); this->nInfos.pop(); rit->finallyBody->toIR(gIR); } // otherwise it's a catch and we'll add a if-statement else { llvm::BasicBlock *next = llvm::BasicBlock::Create(gIR->context(), "eh.next", gIR->topfunc(), gIR->scopeend()); LLValue *classInfo = DtoBitCast(rit->catchType->ir.irStruct->getClassInfoSymbol(), getPtrToType(DtoType(Type::tint8))); LLValue *eh_id = gIR->ir->CreateCall(eh_typeid_for_fn, classInfo); gIR->ir->CreateCondBr(gIR->ir->CreateICmpEQ(eh_sel, eh_id), rit->target, next); gIR->scope() = IRScope(next, gIR->scopeend()); } } // restore landing pad infos this->infos = infos; this->nInfos = nInfos; // no catch matched and all finallys executed - resume unwind llvm::Function* unwind_resume_fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_eh_resume_unwind"); gIR->ir->CreateCall(unwind_resume_fn, eh_ptr); gIR->ir->CreateUnreachable(); gIR->scope() = savedscope; }
DValue *DtoInlineAsmExpr(Loc &loc, FuncDeclaration *fd, Expressions *arguments, LLValue *sretPointer) { IF_LOG Logger::println("DtoInlineAsmExpr @ %s", loc.toChars()); LOG_SCOPE; assert(fd->toParent()->isTemplateInstance() && "invalid inline __asm expr"); assert(arguments->dim >= 2 && "invalid __asm call"); // get code param Expression *e = (*arguments)[0]; IF_LOG Logger::println("code exp: %s", e->toChars()); StringExp *se = static_cast<StringExp *>(e); if (e->op != TOKstring || se->sz != 1) { e->error("`__asm` code argument is not a `char[]` string literal"); fatal(); } std::string code(se->toPtr(), se->numberOfCodeUnits()); // get constraints param e = (*arguments)[1]; IF_LOG Logger::println("constraint exp: %s", e->toChars()); se = static_cast<StringExp *>(e); if (e->op != TOKstring || se->sz != 1) { e->error("`__asm` constraints argument is not a `char[]` string literal"); fatal(); } std::string constraints(se->toPtr(), se->numberOfCodeUnits()); // build runtime arguments size_t n = arguments->dim; LLSmallVector<llvm::Value *, 8> args; args.reserve(n - 2); std::vector<LLType *> argtypes; argtypes.reserve(n - 2); for (size_t i = 2; i < n; i++) { args.push_back(DtoRVal((*arguments)[i])); argtypes.push_back(args.back()->getType()); } // build asm function type Type *type = fd->type->nextOf(); LLType *ret_type = DtoType(type->toBasetype()); llvm::FunctionType *FT = llvm::FunctionType::get(ret_type, argtypes, false); // make sure the constraints are valid if (!llvm::InlineAsm::Verify(FT, constraints)) { e->error("`__asm` constraint argument is invalid"); fatal(); } // build asm call bool sideeffect = true; llvm::InlineAsm *ia = llvm::InlineAsm::get(FT, code, constraints, sideeffect); llvm::Value *rv = gIR->ir->CreateCall(ia, args, ""); if (sretPointer) { DtoStore(rv, DtoBitCast(sretPointer, getPtrToType(ret_type))); return new DLValue(type, sretPointer); } // work around missing tuple support for users of the return value if (type->ty == Tstruct) { // make a copy llvm::Value *mem = DtoAlloca(type, ".__asm_tuple_ret"); DtoStore(rv, DtoBitCast(mem, getPtrToType(ret_type))); return new DLValue(type, mem); } // return call as im value return new DImValue(type, rv); }
LLConstant * IrStruct::getClassInfoInterfaces() { IF_LOG Logger::println("Building ClassInfo.interfaces"); LOG_SCOPE; ClassDeclaration* cd = aggrdecl->isClassDeclaration(); assert(cd); size_t n = interfacesWithVtbls.size(); assert(stripModifiers(type)->irtype->isClass()->getNumInterfaceVtbls() == n && "inconsistent number of interface vtables in this class"); VarDeclarationIter interfaces_idx(ClassDeclaration::classinfo->fields, 3); if (n == 0) return getNullValue(DtoType(interfaces_idx->type)); // Build array of: // // struct Interface // { // ClassInfo classinfo; // void*[] vtbl; // ptrdiff_t offset; // } LLSmallVector<LLConstant*, 6> constants; constants.reserve(cd->vtblInterfaces->dim); LLType* classinfo_type = DtoType(ClassDeclaration::classinfo->type); LLType* voidptrptr_type = DtoType( Type::tvoid->pointerTo()->pointerTo()); VarDeclarationIter idx(ClassDeclaration::classinfo->fields, 3); LLStructType* interface_type = isaStruct(DtoType(idx->type->nextOf())); assert(interface_type); for (size_t i = 0; i < n; ++i) { BaseClass* it = interfacesWithVtbls[i]; IF_LOG Logger::println("Adding interface %s", it->base->toPrettyChars()); IrStruct* irinter = it->base->ir.irStruct; assert(irinter && "interface has null IrStruct"); IrTypeClass* itc = stripModifiers(irinter->type)->irtype->isClass(); assert(itc && "null interface IrTypeClass"); // classinfo LLConstant* ci = irinter->getClassInfoSymbol(); ci = DtoBitCast(ci, classinfo_type); // vtbl LLConstant* vtb; // interface get a null if (cd->isInterfaceDeclaration()) { vtb = DtoConstSlice(DtoConstSize_t(0), getNullValue(voidptrptr_type)); } else { ClassGlobalMap::iterator itv = interfaceVtblMap.find(it->base); assert(itv != interfaceVtblMap.end() && "interface vtbl not found"); vtb = itv->second; vtb = DtoBitCast(vtb, voidptrptr_type); vtb = DtoConstSlice(DtoConstSize_t(itc->getVtblSize()), vtb); } // offset LLConstant* off = DtoConstSize_t(it->offset); // create Interface struct LLConstant* inits[3] = { ci, vtb, off }; LLConstant* entry = LLConstantStruct::get(interface_type, llvm::makeArrayRef(inits, 3)); constants.push_back(entry); } // create Interface[N] LLArrayType* array_type = llvm::ArrayType::get(interface_type, n); // create and apply initializer LLConstant* arr = LLConstantArray::get(array_type, constants); classInterfacesArray->setInitializer(arr); // return null, only baseclass provide interfaces if (cd->vtblInterfaces->dim == 0) { return getNullValue(DtoType(interfaces_idx->type)); } // only the interface explicitly implemented by this class // (not super classes) should show in ClassInfo LLConstant* idxs[2] = { DtoConstSize_t(0), DtoConstSize_t(n - cd->vtblInterfaces->dim) }; LLConstant* ptr = llvm::ConstantExpr::getGetElementPtr( classInterfacesArray, idxs, true); // return as a slice return DtoConstSlice( DtoConstSize_t(cd->vtblInterfaces->dim), ptr ); }
void AggrTypeBuilder::addAggregate( AggregateDeclaration *ad, const AggrTypeBuilder::VarInitMap *explicitInits, AggrTypeBuilder::Aliases aliases) { const size_t n = ad->fields.dim; if (n == 0) return; // prioritize overlapping fields LLSmallVector<FieldPriority, 16> priorities; priorities.reserve(n); for (auto f : ad->fields) { priorities.push_back(prioritize(f, explicitInits)); IF_LOG Logger::println("Field priority for %s: %d", f->toChars(), priorities.back()); } // mirror the ad->fields array but only fill in contributors LLSmallVector<VarDeclaration *, 16> data(n, nullptr); // list of pairs: alias => actual field (same offset, same LL type) LLSmallVector<std::pair<VarDeclaration *, VarDeclaration *>, 16> aliasPairs; // one pass per priority in descending order const auto minMaxPriority = std::minmax_element(priorities.begin(), priorities.end()); for (int p = *minMaxPriority.second; p >= *minMaxPriority.first; p--) { // iterate over fields of that priority, in declaration order for (size_t index = 0; index < n; ++index) { if (priorities[index] != p) continue; VarDeclaration *field = ad->fields[index]; const size_t f_begin = field->offset; const size_t f_end = f_begin + field->type->size(); // skip empty fields if (f_begin == f_end) continue; // check for overlapping existing fields bool overlaps = false; if (field->overlapped) { for (const auto vd : data) { if (!vd) continue; const size_t v_begin = vd->offset; const size_t v_end = v_begin + vd->type->size(); if (v_begin < f_end && v_end > f_begin) { if (aliases == Aliases::AddToVarGEPIndices && v_begin == f_begin && DtoMemType(vd->type) == DtoMemType(field->type)) { aliasPairs.push_back(std::make_pair(field, vd)); } overlaps = true; break; } } } if (!overlaps) data[index] = field; } } // Now we can build a list of LLVM types for the actual LL fields. // Make sure to zero out any padding and set the GEP indices for the directly // indexable variables. // first we sort the list by offset std::sort(data.begin(), data.end(), var_offset_sort_cb); for (const auto vd : data) { if (!vd) continue; assert(vd->offset >= m_offset && "Variable overlaps previous field."); // Add an explicit field for any padding so we can zero it, as per TDPL // §7.1.1. if (m_offset < vd->offset) { m_fieldIndex += add_zeros(m_defaultTypes, m_offset, vd->offset); m_offset = vd->offset; } // add default type m_defaultTypes.push_back(DtoMemType(vd->type)); // advance offset to right past this field m_offset += getMemberSize(vd->type); // set the field index m_varGEPIndices[vd] = m_fieldIndex; // let any aliases reuse this field/GEP index for (const auto &pair : aliasPairs) { if (pair.second == vd) m_varGEPIndices[pair.first] = m_fieldIndex; } ++m_fieldIndex; } }
DValue * DtoInlineAsmExpr(Loc loc, FuncDeclaration * fd, Expressions * arguments) { Logger::println("DtoInlineAsmExpr @ %s", loc.toChars()); LOG_SCOPE; TemplateInstance* ti = fd->toParent()->isTemplateInstance(); assert(ti && "invalid inline __asm expr"); assert(arguments->dim >= 2 && "invalid __asm call"); // get code param Expression* e = static_cast<Expression*>(arguments->data[0]); Logger::println("code exp: %s", e->toChars()); StringExp* se = static_cast<StringExp*>(e); if (e->op != TOKstring || se->sz != 1) { e->error("__asm code argument is not a char[] string literal"); fatal(); } std::string code(static_cast<char*>(se->string), se->len); // get constraints param e = static_cast<Expression*>(arguments->data[1]); Logger::println("constraint exp: %s", e->toChars()); se = static_cast<StringExp*>(e); if (e->op != TOKstring || se->sz != 1) { e->error("__asm constraints argument is not a char[] string literal"); fatal(); } std::string constraints(static_cast<char*>(se->string), se->len); // build runtime arguments size_t n = arguments->dim; LLSmallVector<llvm::Value*, 8> args; args.reserve(n-2); std::vector<LLType*> argtypes; argtypes.reserve(n-2); for (size_t i = 2; i < n; i++) { e = static_cast<Expression*>(arguments->data[i]); args.push_back(e->toElem(gIR)->getRVal()); argtypes.push_back(args.back()->getType()); } // build asm function type Type* type = fd->type->nextOf()->toBasetype(); LLType* ret_type = DtoType(type); llvm::FunctionType* FT = llvm::FunctionType::get(ret_type, argtypes, false); // build asm call bool sideeffect = true; llvm::InlineAsm* ia = llvm::InlineAsm::get(FT, code, constraints, sideeffect); llvm::Value* rv = gIR->ir->CreateCall(ia, args, ""); // work around missing tuple support for users of the return value if (type->ty == Tstruct) { // make a copy llvm::Value* mem = DtoAlloca(type, ".__asm_tuple_ret"); TypeStruct* ts = static_cast<TypeStruct*>(type); size_t n = ts->sym->fields.dim; for (size_t i = 0; i < n; i++) { llvm::Value* v = gIR->ir->CreateExtractValue(rv, i, ""); llvm::Value* gep = DtoGEPi(mem, 0, i); DtoStore(v, gep); } return new DVarValue(fd->type->nextOf(), mem); } // return call as im value return new DImValue(fd->type->nextOf(), rv); }