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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
//===-- moduleinfo.cpp ----------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/moduleinfo.h"
#include "dmd/errors.h"
#include "dmd/mangle.h"
#include "dmd/module.h"
#include "gen/abi.h"
#include "gen/classes.h"
#include "gen/irstate.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/mangling.h"
#include "gen/rttibuilder.h"
#include "gen/runtime.h"
#include "ir/irfunction.h"
#include "ir/irmodule.h"
#include "ir/irtype.h"
// These must match the values in druntime/src/object_.d
#define MIstandalone 0x4
#define MItlsctor 0x8
#define MItlsdtor 0x10
#define MIctor 0x20
#define MIdtor 0x40
#define MIxgetMembers 0x80
#define MIictor 0x100
#define MIunitTest 0x200
#define MIimportedModules 0x400
#define MIlocalClasses 0x800
#define MInew 0x80000000 // it's the "new" layout
namespace {
/// Creates a function in the current llvm::Module that dispatches to the given
/// functions one after each other and then increments the gate variables, if
/// any.
llvm::Function *buildForwarderFunction(
const std::string &name, const std::list<FuncDeclaration *> &funcs,
const std::list<VarDeclaration *> &gates = std::list<VarDeclaration *>()) {
// If there is no gates, we might get away without creating a function at all.
if (gates.empty()) {
if (funcs.empty()) {
return nullptr;
}
if (funcs.size() == 1) {
return DtoCallee(funcs.front());
}
}
// Create an internal-linkage void() function.
const auto fnTy =
LLFunctionType::get(LLType::getVoidTy(gIR->context()), {}, false);
const auto irMangle = getIRMangledFuncName(name, LINK::d);
assert(gIR->module.getFunction(irMangle) == NULL);
llvm::Function *fn = llvm::Function::Create(
fnTy, llvm::GlobalValue::InternalLinkage, irMangle, &gIR->module);
fn->setCallingConv(gABI->callingConv(LINK::d));
// Emit the body, consisting of...
const auto bb = llvm::BasicBlock::Create(gIR->context(), "", fn);
IRBuilder<> builder(bb);
ldc::DISubprogram dis = gIR->DBuilder.EmitModuleCTor(fn, name.c_str());
if (global.params.symdebug) {
// Need _some_ debug info to avoid inliner bug, see GitHub issue #998.
builder.SetCurrentDebugLocation(
llvm::DILocation::get(gIR->context(), 0, 0, dis));
}
// ... calling the given functions, and...
for (auto func : funcs) {
const auto f = DtoCallee(func);
const auto call = builder.CreateCall(f, {});
call->setCallingConv(gABI->callingConv(func));
}
// ... incrementing the gate variables.
for (auto gate : gates) {
assert(getIrGlobal(gate));
const auto val = getIrGlobal(gate)->value;
const auto rval = builder.CreateLoad(getPointeeType(val), val, "vgate");
const auto res = builder.CreateAdd(rval, DtoConstUint(1), "vgate");
builder.CreateStore(res, val);
}
builder.CreateRetVoid();
return fn;
}
namespace {
std::string getMangledName(Module *m, const char *suffix) {
OutBuffer buf;
buf.writestring("_D");
mangleToBuffer(m, &buf);
if (suffix)
buf.writestring(suffix);
return buf.peekChars();
}
}
llvm::Function *buildModuleCtor(Module *m) {
std::string name = getMangledName(m, "6__ctorZ");
IrModule *irm = getIrModule(m);
return buildForwarderFunction(name, irm->ctors, irm->gates);
}
llvm::Function *buildModuleDtor(Module *m) {
std::string name = getMangledName(m, "6__dtorZ");
return buildForwarderFunction(name, getIrModule(m)->dtors);
}
llvm::Function *buildModuleUnittest(Module *m) {
std::string name = getMangledName(m, "10__unittestZ");
return buildForwarderFunction(name, getIrModule(m)->unitTests);
}
llvm::Function *buildModuleSharedCtor(Module *m) {
std::string name = getMangledName(m, "13__shared_ctorZ");
IrModule *irm = getIrModule(m);
return buildForwarderFunction(name, irm->sharedCtors, irm->sharedGates);
}
llvm::Function *buildModuleSharedDtor(Module *m) {
std::string name = getMangledName(m, "13__shared_dtorZ");
return buildForwarderFunction(name, getIrModule(m)->sharedDtors);
}
/// Builds the (constant) data content for the importedModules[] array.
llvm::Constant *buildImportedModules(Module *m, size_t &count) {
const auto moduleInfoPtrTy = DtoPtrToType(getModuleInfoType());
std::vector<LLConstant *> importInits;
for (auto mod : m->aimports) {
if (!mod->needModuleInfo() || mod == m) {
continue;
}
importInits.push_back(
DtoBitCast(getIrModule(mod)->moduleInfoSymbol(), moduleInfoPtrTy));
}
count = importInits.size();
if (importInits.empty())
return nullptr;
const auto type = llvm::ArrayType::get(moduleInfoPtrTy, importInits.size());
return LLConstantArray::get(type, importInits);
}
/// Builds the (constant) data content for the localClasses[] array.
llvm::Constant *buildLocalClasses(Module *m, size_t &count) {
const auto classinfoTy = DtoType(getClassInfoType());
ClassDeclarations aclasses;
for (auto s : *m->members) {
s->addLocalClass(&aclasses);
}
std::vector<LLConstant *> classInfoRefs;
for (auto cd : aclasses) {
DtoResolveClass(cd);
if (cd->isInterfaceDeclaration()) {
IF_LOG Logger::println("skipping interface '%s' in moduleinfo",
cd->toPrettyChars());
continue;
}
if (cd->sizeok != Sizeok::done) {
IF_LOG Logger::println(
"skipping opaque class declaration '%s' in moduleinfo",
cd->toPrettyChars());
continue;
}
IF_LOG Logger::println("class: %s", cd->toPrettyChars());
classInfoRefs.push_back(
DtoBitCast(getIrAggr(cd)->getClassInfoSymbol(), classinfoTy));
}
count = classInfoRefs.size();
if (classInfoRefs.empty())
return nullptr;
const auto type = llvm::ArrayType::get(classinfoTy, classInfoRefs.size());
return LLConstantArray::get(type, classInfoRefs);
}
}
llvm::GlobalVariable *genModuleInfo(Module *m) {
// check declaration in object.d
const auto moduleInfoType = getModuleInfoType();
const auto moduleInfoDecl = Module::moduleinfo;
// The "new-style" ModuleInfo records are variable-length, with the presence
// of the various fields indicated by a certain flag bit. The base struct
// should consist only of the _flags/_index fields (the latter of which is
// unused).
if (moduleInfoDecl->structsize != 4 + 4) {
m->error("Unexpected size of struct `object.ModuleInfo`; "
"druntime version does not match compiler (see -v)");
fatal();
}
// First, figure out which fields are present and set the flags accordingly.
unsigned flags = MInew;
const auto fctor = buildModuleCtor(m);
if (fctor) {
flags |= MItlsctor;
}
const auto fdtor = buildModuleDtor(m);
if (fdtor) {
flags |= MItlsdtor;
}
const auto fsharedctor = buildModuleSharedCtor(m);
if (fsharedctor) {
flags |= MIctor;
}
const auto fshareddtor = buildModuleSharedDtor(m);
if (fshareddtor) {
flags |= MIdtor;
}
#if 0
if (fgetmembers)
flags |= MIxgetMembers;
#endif
const auto fictor = getIrModule(m)->coverageCtor;
if (fictor)
flags |= MIictor;
const auto funittest = buildModuleUnittest(m);
if (funittest) {
flags |= MIunitTest;
}
size_t importedModulesCount;
const auto importedModules = buildImportedModules(m, importedModulesCount);
if (importedModules) {
flags |= MIimportedModules;
}
size_t localClassesCount;
const auto localClasses = buildLocalClasses(m, localClassesCount);
if (localClasses) {
flags |= MIlocalClasses;
}
if (!m->needmoduleinfo) {
flags |= MIstandalone;
}
// Now, start building the initialiser for the ModuleInfo instance.
RTTIBuilder b(moduleInfoType);
b.push_uint(flags);
b.push_uint(0); // index
if (fctor) {
b.push(fctor);
}
if (fdtor) {
b.push(fdtor);
}
if (fsharedctor) {
b.push(fsharedctor);
}
if (fshareddtor) {
b.push(fshareddtor);
}
#if 0
if (fgetmembers)
b.push(fgetmembers);
#endif
if (fictor) {
b.push(fictor);
}
if (funittest) {
b.push(funittest);
}
if (importedModules) {
b.push_size(importedModulesCount);
b.push(importedModules);
}
if (localClasses) {
b.push_size(localClassesCount);
b.push(localClasses);
}
// Put out module name as a 0-terminated string.
const char *name = m->toPrettyChars();
const size_t len = strlen(name) + 1;
const auto it = llvm::IntegerType::getInt8Ty(gIR->context());
const auto at = llvm::ArrayType::get(it, len);
b.push(toConstantArray(it, at, name, len, false));
// Create a global symbol with the above initialiser.
LLGlobalVariable *moduleInfoSym = getIrModule(m)->moduleInfoSymbol();
b.finalize(moduleInfoSym);
setLinkage({LLGlobalValue::ExternalLinkage, needsCOMDAT()}, moduleInfoSym);
if (global.params.dllexport) {
moduleInfoSym->setDLLStorageClass(LLGlobalValue::DLLExportStorageClass);
}
return moduleInfoSym;
}
|