llvm::Function *getRuntimeFunction(const Loc &loc, llvm::Module &target, const char *name) { checkForImplicitGCCall(loc, name); if (!M) { initRuntime(); } LLFunction *fn = target.getFunction(name); if (fn) { return fn; } fn = M->getFunction(name); if (!fn) { error(loc, "Runtime function '%s' was not found", name); fatal(); } LLFunctionType *fnty = fn->getFunctionType(); LLFunction *resfn = llvm::cast<llvm::Function>(target.getOrInsertFunction(name, fnty)); resfn->setAttributes(fn->getAttributes()); resfn->setCallingConv(fn->getCallingConv()); return resfn; }
llvm::BasicBlock * TryCatchFinallyScopes::emitLandingPadMSVC(CleanupCursor cleanupScope) { LLFunction *currentFunction = irs.func()->func; if (!currentFunction->hasPersonalityFn()) { const char *personality = "__CxxFrameHandler3"; LLFunction *personalityFn = getRuntimeFunction(Loc(), irs.module, personality); currentFunction->setPersonalityFn(personalityFn); } if (cleanupScope == 0) return runCleanupPad(cleanupScope, nullptr); llvm::BasicBlock *&pad = getLandingPadRef(cleanupScope); if (!pad) pad = emitLandingPadMSVC(cleanupScope - 1); return runCleanupPad(cleanupScope, pad); }
llvm::Function* LLVM_D_GetRuntimeFunction(llvm::Module* target, const char* name) { if (noruntime) { error("No implicit runtime calls allowed with -noruntime option enabled"); fatal(); } if (!M) { LLVM_D_InitRuntime(); } LLFunction* fn = target->getFunction(name); if (fn) return fn; fn = M->getFunction(name); assert(fn && "Runtime function not found."); LLFunctionType* fnty = fn->getFunctionType(); LLFunction* resfn = llvm::cast<llvm::Function>(target->getOrInsertFunction(name, fnty)); resfn->setAttributes(fn->getAttributes()); return resfn; }
llvm::GlobalVariable * IrAggr::getInterfaceVtbl(BaseClass * b, bool new_instance, size_t interfaces_index) { ClassGlobalMap::iterator it = interfaceVtblMap.find(b->base); if (it != interfaceVtblMap.end()) return it->second; IF_LOG Logger::println("Building vtbl for implementation of interface %s in class %s", b->base->toPrettyChars(), aggrdecl->toPrettyChars()); LOG_SCOPE; ClassDeclaration* cd = aggrdecl->isClassDeclaration(); assert(cd && "not a class aggregate"); FuncDeclarations vtbl_array; b->fillVtbl(cd, &vtbl_array, new_instance); std::vector<llvm::Constant*> constants; constants.reserve(vtbl_array.dim); if (!b->base->isCPPinterface()) { // skip interface info for CPP interfaces // start with the interface info VarDeclarationIter interfaces_idx(ClassDeclaration::classinfo->fields, 3); // index into the interfaces array llvm::Constant* idxs[2] = { DtoConstSize_t(0), DtoConstSize_t(interfaces_index) }; llvm::Constant* c = llvm::ConstantExpr::getGetElementPtr( getInterfaceArraySymbol(), idxs, true); constants.push_back(c); } // add virtual function pointers size_t n = vtbl_array.dim; for (size_t i = b->base->vtblOffset(); i < n; i++) { Dsymbol* dsym = static_cast<Dsymbol*>(vtbl_array.data[i]); if (dsym == NULL) { // FIXME // why is this null? // happens for mini/s.d constants.push_back(getNullValue(getVoidPtrType())); continue; } FuncDeclaration* fd = dsym->isFuncDeclaration(); assert(fd && "vtbl entry not a function"); assert((!fd->isAbstract() || fd->fbody) && "null symbol in interface implementation vtable"); fd->codegen(Type::sir); assert(fd->ir.irFunc && "invalid vtbl function"); LLFunction *fn = fd->ir.irFunc->func; // If the base is a cpp interface, 'this' parameter is a pointer to // the interface not the underlying object as expected. Instead of // the function, we place into the vtable a small wrapper, called thunk, // that casts 'this' to the object and then pass it to the real function. if (b->base->isCPPinterface()) { TypeFunction *f = (TypeFunction*)fd->type->toBasetype(); assert(f->fty.arg_this); // create the thunk function OutBuffer name; name.writestring("Th"); name.printf("%i", b->offset); name.writestring(fd->mangle()); LLFunction *thunk = LLFunction::Create(isaFunction(fn->getType()->getContainedType(0)), DtoLinkage(fd), name.toChars(), gIR->module); // create entry and end blocks llvm::BasicBlock* beginbb = llvm::BasicBlock::Create(gIR->context(), "entry", thunk); llvm::BasicBlock* endbb = llvm::BasicBlock::Create(gIR->context(), "endentry", thunk); gIR->scopes.push_back(IRScope(beginbb, endbb)); // copy the function parameters, so later we can pass them to the real function std::vector<LLValue*> args; llvm::Function::arg_iterator iarg = thunk->arg_begin(); for (; iarg != thunk->arg_end(); ++iarg) args.push_back(iarg); // cast 'this' to Object LLValue* &thisArg = args[(f->fty.arg_sret == 0) ? 0 : 1]; LLType* thisType = thisArg->getType(); thisArg = DtoBitCast(thisArg, getVoidPtrType()); thisArg = DtoGEP1(thisArg, DtoConstInt(-b->offset)); thisArg = DtoBitCast(thisArg, thisType); // call the real vtbl function. LLValue *retVal = gIR->ir->CreateCall(fn, args); // return from the thunk if (thunk->getReturnType() == LLType::getVoidTy(gIR->context())) llvm::ReturnInst::Create(gIR->context(), beginbb); else llvm::ReturnInst::Create(gIR->context(), retVal, beginbb); // clean up gIR->scopes.pop_back(); thunk->getBasicBlockList().pop_back(); fn = thunk; } constants.push_back(fn); } // build the vtbl constant llvm::Constant* vtbl_constant = LLConstantStruct::getAnon(gIR->context(), constants, false); // create the global variable to hold it llvm::GlobalValue::LinkageTypes _linkage = DtoExternalLinkage(aggrdecl); std::string mangle("_D"); mangle.append(cd->mangle()); mangle.append("11__interface"); mangle.append(b->base->mangle()); mangle.append("6__vtblZ"); llvm::GlobalVariable* GV = getOrCreateGlobal(cd->loc, *gIR->module, vtbl_constant->getType(), true, _linkage, vtbl_constant, mangle ); // insert into the vtbl map interfaceVtblMap.insert(std::make_pair(b->base, GV)); return GV; }
void DtoDeclareFunction(FuncDeclaration* fdecl) { DtoResolveFunction(fdecl); if (fdecl->ir.declared) return; fdecl->ir.declared = true; Logger::println("DtoDeclareFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars()); LOG_SCOPE; //printf("declare function: %s\n", fdecl->toPrettyChars()); // intrinsic sanity check if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) { error(fdecl->loc, "intrinsics cannot have function bodies"); fatal(); } // get TypeFunction* Type* t = fdecl->type->toBasetype(); TypeFunction* f = (TypeFunction*)t; bool declareOnly = !mustDefineSymbol(fdecl); if (fdecl->llvmInternal == LLVMva_start) declareOnly = true; if (!fdecl->ir.irFunc) { fdecl->ir.irFunc = new IrFunction(fdecl); } // mangled name const char* mangled_name; if (fdecl->llvmInternal == LLVMintrinsic) mangled_name = fdecl->intrinsicName.c_str(); else mangled_name = fdecl->mangle(); LLFunction* vafunc = 0; if (fdecl->isVaIntrinsic()) vafunc = DtoDeclareVaFunction(fdecl); // construct function LLFunctionType* functype = DtoFunctionType(fdecl); LLFunction* func = vafunc ? vafunc : gIR->module->getFunction(mangled_name); if (!func) { func = LLFunction::Create(functype, DtoLinkage(fdecl), mangled_name, gIR->module); } else if (func->getFunctionType() != functype) { error(fdecl->loc, "Function type does not match previously declared function with the same mangled name: %s", fdecl->mangle()); } if (Logger::enabled()) Logger::cout() << "func = " << *func << std::endl; // add func to IRFunc fdecl->ir.irFunc->func = func; // calling convention if (!vafunc && fdecl->llvmInternal != LLVMintrinsic) func->setCallingConv(DtoCallingConv(fdecl->loc, f->linkage)); else // fall back to C, it should be the right thing to do func->setCallingConv(llvm::CallingConv::C); // parameter attributes if (!fdecl->isIntrinsic()) { set_param_attrs(f, func, fdecl); if (global.params.disableRedZone) { func->addFnAttr(NoRedZone); } } // main if (fdecl->isMain()) { gIR->mainFunc = func; } #if DMDV2 // shared static ctor if (fdecl->isSharedStaticCtorDeclaration()) { if (mustDefineSymbol(fdecl)) { gIR->sharedCtors.push_back(fdecl); } } // shared static dtor else if (StaticDtorDeclaration *dtorDecl = fdecl->isSharedStaticDtorDeclaration()) { if (mustDefineSymbol(fdecl)) { gIR->sharedDtors.push_front(fdecl); if (dtorDecl->vgate) gIR->sharedGates.push_front(dtorDecl->vgate); } } else #endif // static ctor if (fdecl->isStaticCtorDeclaration()) { if (mustDefineSymbol(fdecl)) { gIR->ctors.push_back(fdecl); } } // static dtor else if (StaticDtorDeclaration *dtorDecl = fdecl->isStaticDtorDeclaration()) { if (mustDefineSymbol(fdecl)) { gIR->dtors.push_front(fdecl); #if DMDV2 if (dtorDecl->vgate) gIR->gates.push_front(dtorDecl->vgate); #endif } } // we never reference parameters of function prototypes std::string str; // if (!declareOnly) { // name parameters llvm::Function::arg_iterator iarg = func->arg_begin(); if (f->fty.arg_sret) { iarg->setName(".sret_arg"); fdecl->ir.irFunc->retArg = iarg; ++iarg; } if (f->fty.arg_this) { iarg->setName(".this_arg"); fdecl->ir.irFunc->thisArg = iarg; assert(fdecl->ir.irFunc->thisArg); ++iarg; } else if (f->fty.arg_nest) { iarg->setName(".nest_arg"); fdecl->ir.irFunc->nestArg = iarg; assert(fdecl->ir.irFunc->nestArg); ++iarg; } if (f->fty.arg_argptr) { iarg->setName("._arguments"); fdecl->ir.irFunc->_arguments = iarg; ++iarg; iarg->setName("._argptr"); fdecl->ir.irFunc->_argptr = iarg; ++iarg; } int k = 0; for (; iarg != func->arg_end(); ++iarg) { if (fdecl->parameters && fdecl->parameters->dim > k) { int paramIndex = f->fty.reverseParams ? fdecl->parameters->dim-k-1 : k; Dsymbol* argsym = (Dsymbol*)fdecl->parameters->data[paramIndex]; VarDeclaration* argvd = argsym->isVarDeclaration(); assert(argvd); assert(!argvd->ir.irLocal); argvd->ir.irParam = new IrParameter(argvd); argvd->ir.irParam->value = iarg; argvd->ir.irParam->arg = f->fty.args[paramIndex]; str = argvd->ident->toChars(); str.append("_arg"); iarg->setName(str); k++; } else { iarg->setName("unnamed"); } } } if (fdecl->isUnitTestDeclaration() && !declareOnly) gIR->unitTests.push_back(fdecl); if (!declareOnly) Type::sir->addFunctionBody(fdecl->ir.irFunc); else assert(func->getLinkage() != llvm::GlobalValue::InternalLinkage); }
void DtoDeclareFunction(FuncDeclaration* fdecl) { DtoResolveFunction(fdecl); if (fdecl->ir.isDeclared()) return; fdecl->ir.setDeclared(); IF_LOG Logger::println("DtoDeclareFunction(%s): %s", fdecl->toPrettyChars(), fdecl->loc.toChars()); LOG_SCOPE; if (fdecl->isUnitTestDeclaration() && !global.params.useUnitTests) { Logger::println("unit tests not enabled"); return; } //printf("declare function: %s\n", fdecl->toPrettyChars()); // intrinsic sanity check if (fdecl->llvmInternal == LLVMintrinsic && fdecl->fbody) { error(fdecl->loc, "intrinsics cannot have function bodies"); fatal(); } // get TypeFunction* Type* t = fdecl->type->toBasetype(); TypeFunction* f = static_cast<TypeFunction*>(t); // create IrFunction IrFunction *irFunc = getIrFunc(fdecl, true); LLFunction* vafunc = 0; if (DtoIsVaIntrinsic(fdecl)) vafunc = DtoDeclareVaFunction(fdecl); // calling convention LINK link = f->linkage; if (vafunc || fdecl->llvmInternal == LLVMintrinsic // DMD treats _Dmain as having C calling convention and this has been // hardcoded into druntime, even if the frontend type has D linkage. // See Bugzilla issue 9028. || fdecl->isMain() ) { link = LINKc; } // mangled name std::string mangledName(mangleExact(fdecl)); mangledName = gABI->mangleForLLVM(mangledName, link); // construct function LLFunctionType* functype = DtoFunctionType(fdecl); LLFunction* func = vafunc ? vafunc : gIR->module->getFunction(mangledName); if (!func) { if(fdecl->llvmInternal == LLVMinline_ir) { func = DtoInlineIRFunction(fdecl); } else { // All function declarations are "external" - any other linkage type // is set when actually defining the function. func = LLFunction::Create(functype, llvm::GlobalValue::ExternalLinkage, mangledName, gIR->module); } } else if (func->getFunctionType() != functype) { error(fdecl->loc, "Function type does not match previously declared function with the same mangled name: %s", mangleExact(fdecl)); fatal(); } func->setCallingConv(gABI->callingConv(link)); IF_LOG Logger::cout() << "func = " << *func << std::endl; // add func to IRFunc irFunc->func = func; // parameter attributes if (!DtoIsIntrinsic(fdecl)) { set_param_attrs(f, func, fdecl); if (global.params.disableRedZone) { func->addFnAttr(LDC_ATTRIBUTE(NoRedZone)); } } // main if (fdecl->isMain()) { // Detect multiple main functions, which is disallowed. DMD checks this // in the glue code, so we need to do it here as well. if (gIR->mainFunc) { error(fdecl->loc, "only one main function allowed"); } gIR->mainFunc = func; } if (fdecl->neverInline) { irFunc->setNeverInline(); } if (fdecl->llvmInternal == LLVMglobal_crt_ctor || fdecl->llvmInternal == LLVMglobal_crt_dtor) { AppendFunctionToLLVMGlobalCtorsDtors(func, fdecl->priority, fdecl->llvmInternal == LLVMglobal_crt_ctor); } IrFuncTy &irFty = irFunc->irFty; // if (!declareOnly) { // name parameters llvm::Function::arg_iterator iarg = func->arg_begin(); if (irFty.arg_sret) { iarg->setName(".sret_arg"); irFunc->retArg = iarg; ++iarg; } if (irFty.arg_this) { iarg->setName(".this_arg"); irFunc->thisArg = iarg; VarDeclaration* v = fdecl->vthis; if (v) { // We already build the this argument here if we will need it // later for codegen'ing the function, just as normal // parameters below, because it can be referred to in nested // context types. Will be given storage in DtoDefineFunction. assert(!isIrParameterCreated(v)); IrParameter *irParam = getIrParameter(v, true); irParam->value = iarg; irParam->arg = irFty.arg_this; irParam->isVthis = true; } ++iarg; } else if (irFty.arg_nest) { iarg->setName(".nest_arg"); irFunc->nestArg = iarg; assert(irFunc->nestArg); ++iarg; } if (irFty.arg_arguments) { iarg->setName("._arguments"); irFunc->_arguments = iarg; ++iarg; } // we never reference parameters of function prototypes unsigned int k = 0; for (; iarg != func->arg_end(); ++iarg) { if (fdecl->parameters && fdecl->parameters->dim > k) { int paramIndex = irFty.reverseParams ? fdecl->parameters->dim-k-1 : k; Dsymbol* argsym = static_cast<Dsymbol*>(fdecl->parameters->data[paramIndex]); VarDeclaration* argvd = argsym->isVarDeclaration(); assert(argvd); assert(!isIrLocalCreated(argvd)); std::string str(argvd->ident->toChars()); str.append("_arg"); iarg->setName(str); IrParameter *irParam = getIrParameter(argvd, true); irParam->value = iarg; irParam->arg = irFty.args[paramIndex]; k++; } else { iarg->setName("unnamed"); } } } }
LLFunction* DtoInlineIRFunction(FuncDeclaration* fdecl) { const char* mangled_name = mangleExact(fdecl); TemplateInstance* tinst = fdecl->parent->isTemplateInstance(); assert(tinst); Objects& objs = tinst->tdtypes; assert(objs.dim == 3); Expression* a0 = isExpression(objs[0]); assert(a0); StringExp* strexp = a0->toStringExp(); assert(strexp); assert(strexp->sz == 1); std::string code(static_cast<char*>(strexp->string), strexp->len); Type* ret = isType(objs[1]); assert(ret); Tuple* a2 = isTuple(objs[2]); assert(a2); Objects& arg_types = a2->objects; std::string str; llvm::raw_string_ostream stream(str); stream << "define " << *DtoType(ret) << " @" << mangled_name << "("; for(size_t i = 0; ;) { Type* ty = isType(arg_types[i]); //assert(ty); if(!ty) { error(tinst->loc, "All parameters of a template defined with pragma llvm_inline_ir, except for the first one, should be types"); fatal(); } stream << *DtoType(ty); i++; if(i >= arg_types.dim) break; stream << ", "; } if(ret->ty == Tvoid) code.append("\nret void"); stream << ")\n{\n" << code << "\n}"; llvm::SMDiagnostic err; #if LDC_LLVM_VER >= 306 std::unique_ptr<llvm::Module> m = llvm::parseAssemblyString( stream.str().c_str(), err, gIR->context()); #elif LDC_LLVM_VER >= 303 llvm::Module* m = llvm::ParseAssemblyString( stream.str().c_str(), NULL, err, gIR->context()); #else llvm::ParseAssemblyString( stream.str().c_str(), gIR->module, err, gIR->context()); #endif std::string errstr = err.getMessage(); if(errstr != "") error(tinst->loc, "can't parse inline LLVM IR:\n%s\n%s\n%s\nThe input string was: \n%s", #if LDC_LLVM_VER >= 303 err.getLineContents().str().c_str(), #else err.getLineContents().c_str(), #endif (std::string(err.getColumnNo(), ' ') + '^').c_str(), errstr.c_str(), stream.str().c_str()); #if LDC_LLVM_VER >= 306 llvm::Linker(gIR->module).linkInModule(m.get()); #else #if LDC_LLVM_VER >= 303 std::string errstr2 = ""; #if LDC_LLVM_VER >= 306 llvm::Linker(gIR->module).linkInModule(m.get(), &errstr2); #else llvm::Linker(gIR->module).linkInModule(m, &errstr2); #endif if(errstr2 != "") error(tinst->loc, "Error when linking in llvm inline ir: %s", errstr2.c_str()); #endif #endif LLFunction* fun = gIR->module->getFunction(mangled_name); fun->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); fun->addFnAttr(LDC_ATTRIBUTE(AlwaysInline)); return fun; }