Skip to content

Commit 5dc914c

Browse files
committed
Implement -extI and -dllimport=externalOnly
1 parent 67852db commit 5dc914c

File tree

10 files changed

+49
-19
lines changed

10 files changed

+49
-19
lines changed

dmd/globals.d

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ enum LinkonceTemplates : byte
119119
enum DLLImport : byte
120120
{
121121
none,
122-
defaultLibsOnly, // only symbols from druntime/Phobos
122+
externalOnly, // only symbols from -extI modules
123+
defaultLibsOnly, // only symbols from druntime/Phobos and -extI modules
123124
all
124125
}
125126
} // IN_LLVM

dmd/globals.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ enum class LinkonceTemplates : char
127127
enum class DLLImport : char
128128
{
129129
none,
130-
defaultLibsOnly, // only symbols from druntime/Phobos
130+
externalOnly, // only symbols from -extI modules
131+
defaultLibsOnly, // only symbols from druntime/Phobos and -extI modules
131132
all
132133
};
133134
#endif

driver/cl_helpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void ImportPathsAdapter::push_back(const char *cstr) {
7272
error(Loc(), "Expected argument to '-%s'", name);
7373
}
7474

75-
arrp->push(ImportPathInfo(mem.xstrdup(cstr)));
75+
arrp->push(ImportPathInfo(mem.xstrdup(cstr), isExternal));
7676
}
7777

7878
} // namespace opts

driver/cl_helpers.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,13 @@ class StringsAdapter {
183183
class ImportPathsAdapter {
184184
const char *name;
185185
Array<ImportPathInfo> *arrp;
186+
bool isExternal;
186187

187188
public:
188-
ImportPathsAdapter(const char *name_, Array<ImportPathInfo> &arr) {
189+
ImportPathsAdapter(const char *name_, Array<ImportPathInfo> &arr, bool isExternal_) {
189190
name = name_;
190191
arrp = &arr;
192+
isExternal = isExternal_;
191193
assert(name);
192194
}
193195

driver/cl_options.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ cl::opt<DLLImport, true> dllimport(
7777
cl::values(
7878
clEnumValN(DLLImport::none, "none",
7979
"None (default with -link-defaultlib-shared=false)"),
80+
clEnumValN(
81+
DLLImport::externalOnly, "externalOnly",
82+
"Only symbols from modules imported from an -extI import root"),
8083
clEnumValN(DLLImport::defaultLibsOnly, "defaultLibsOnly",
81-
"Only druntime/Phobos symbols (default with "
82-
"-link-defaultlib-shared and -fvisibility=hidden)."),
84+
"Only druntime/Phobos symbols and symbols from modules "
85+
"imported from an -extI import root (default with "
86+
"-link-defaultlib-shared and -fvisibility=hidden)"),
8387
clEnumValN(DLLImport::all, "all",
8488
"All (default with -link-defaultlib-shared and "
8589
"-fvisibility=public)")));

driver/ldmd.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Where:\n\
174174
-deps print module dependencies (imports/file/version/debug/lib)\n\
175175
-deps=<filename> write module dependencies to filename (only imports)\n\
176176
-dllimport=<value>\n\
177-
Windows only: select symbols to dllimport (none/defaultLibsOnly/all)\n\
177+
Windows only: select symbols to dllimport (none/defaultLibsOnly/externalOnly/all)\n\
178178
-extern-std=<standard>\n\
179179
set C++ name mangling compatibility with <standard>\n"
180180
#if 0
@@ -210,6 +210,7 @@ Where:\n\
210210
-HCf=<filename> write C++ 'header' file to filename instead of stdout\n\
211211
--help print help and exit\n\
212212
-I=<directory> look for imports also in directory\n\
213+
-extI=<directory> look for imports that are out of the currently compiling binary, used to set the module as DllImport\n\
213214
-i[=<pattern>] include imported modules in the compilation\n\
214215
-identifiers=<table>\n\
215216
specify the non-ASCII tables for D identifiers\n\
@@ -689,6 +690,7 @@ void translateArgs(const llvm::SmallVectorImpl<const char *> &ldmdArgs,
689690
}
690691
/* -unittest
691692
* -I
693+
* -extI
692694
* -J
693695
*/
694696
else if (startsWith(p + 1, "debug") && p[6] != 'l') {

driver/main.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,22 @@ void generateJson(Modules *modules);
9090
using namespace dmd;
9191
using namespace opts;
9292

93-
static ImportPathsAdapter impPathsStore("I", global.params.imppath);
93+
static ImportPathsAdapter impPathsStore("I", global.params.imppath,
94+
/*isExternal=*/false);
9495
static cl::list<std::string, ImportPathsAdapter>
9596
importPaths("I", cl::desc("Look for imports also in <directory>"),
9697
cl::value_desc("directory"), cl::location(impPathsStore),
9798
cl::Prefix);
9899

100+
static ImportPathsAdapter extImpPathsStore("extI", global.params.imppath,
101+
/*isExternal=*/true);
102+
static cl::list<std::string, ImportPathsAdapter> extImportPaths(
103+
"extI",
104+
cl::desc("Look also in <directory> for imports that are external to the "
105+
"currently compiling binary. This affects the -dllimport behavior "
106+
"for data symbols from these binary-external modules."),
107+
cl::value_desc("directory"), cl::location(extImpPathsStore));
108+
99109
// Note: this option is parsed manually in C main().
100110
static cl::opt<bool> enableGC(
101111
"lowmem", cl::ZeroOrMore,

gen/llvmhelpers.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,25 +1676,35 @@ static bool isDefaultLibSymbol(Dsymbol *sym) {
16761676
(md->packages.length > 1 && md->packages.ptr[1] == Id::io)));
16771677
}
16781678

1679+
static bool isExplicitlyOutOfBinary(Dsymbol *sym) {
1680+
if (auto mod = sym->getModule())
1681+
return mod->isExplicitlyOutOfBinary;
1682+
return false;
1683+
}
1684+
16791685
bool defineOnDeclare(Dsymbol *sym, bool isFunction) {
16801686
// -linkonce-templates: all instantiated symbols
16811687
if (global.params.linkonceTemplates != LinkonceTemplates::no)
16821688
return sym->isInstantiated();
16831689

1684-
// -dllimport=defaultLibsOnly: all data symbols instantiated from
1685-
// druntime/Phobos templates
1690+
// -dllimport=externalOnly|defaultLibsOnly: all data symbols instantiated from
1691+
// binary-external modules, e.g., druntime/Phobos templates
16861692
// see https://github.com/ldc-developers/ldc/issues/3931
1687-
return !isFunction && global.params.dllimport == DLLImport::defaultLibsOnly &&
1688-
sym->isInstantiated() && isDefaultLibSymbol(sym);
1693+
const auto di = global.params.dllimport;
1694+
return !isFunction && sym->isInstantiated() &&
1695+
((di == DLLImport::externalOnly && isExplicitlyOutOfBinary(sym)) ||
1696+
(di == DLLImport::defaultLibsOnly &&
1697+
(isExplicitlyOutOfBinary(sym) || isDefaultLibSymbol(sym))));
16891698
}
16901699

16911700
bool dllimportDataSymbol(Dsymbol *sym) {
16921701
if (!global.params.targetTriple->isOSWindows())
16931702
return false;
16941703

1695-
if (sym->isExport() || global.params.dllimport == DLLImport::all ||
1696-
(global.params.dllimport == DLLImport::defaultLibsOnly &&
1697-
isDefaultLibSymbol(sym))) {
1704+
const auto di = global.params.dllimport;
1705+
if (sym->isExport() || di == DLLImport::all ||
1706+
(di >= DLLImport::externalOnly && isExplicitlyOutOfBinary(sym)) ||
1707+
(di == DLLImport::defaultLibsOnly && isDefaultLibSymbol(sym))) {
16981708
// Okay, this symbol is a candidate. Use dllimport unless we have a
16991709
// guaranteed-codegen'd definition in a root module.
17001710
if (auto mod = sym->isModule())

gen/modules.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ LLFunction *build_module_reference_and_ctor(const char *moduleMangle,
150150
LLConstant *mref = gIR->module.getNamedGlobal(mrefIRMangle);
151151
LLType *ptrTy = getOpaquePtrType();
152152
if (!mref) {
153-
mref =
154-
declareGlobal(Loc(), gIR->module, ptrTy, mrefIRMangle, false,
155-
false, global.params.dllimport != DLLImport::none);
153+
mref = declareGlobal(Loc(), gIR->module, ptrTy, mrefIRMangle, false, false,
154+
false);
156155
}
157156

158157
// make the function insert this moduleinfo as the beginning of the

gen/typinf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,8 @@ void buildTypeInfo(TypeInfoDeclaration *decl) {
450450
// immutable on the D side, and e.g. synchronized() can be used on the
451451
// implicit monitor.
452452
const bool isConstant = false;
453-
bool useDLLImport = isBuiltin && global.params.dllimport != DLLImport::none;
453+
const bool useDLLImport =
454+
isBuiltin && global.params.dllimport >= DLLImport::defaultLibsOnly;
454455
gvar = declareGlobal(decl->loc, gIR->module, type, irMangle, isConstant,
455456
false, useDLLImport);
456457
}

0 commit comments

Comments
 (0)