@@ -69,14 +69,13 @@ namespace llvm {
6969 node (const entry &V) : Next(nullptr ), Val(V) {}
7070 };
7171
72- // / Add a node to the Registry: this is the interface between the plugin and
73- // / the executable.
74- // /
75- // / This function is exported by the executable and called by the plugin to
76- // / add a node to the executable's registry. Therefore it's not defined here
77- // / to avoid it being instantiated in the plugin and is instead defined in
78- // / the executable (see LLVM_INSTANTIATE_REGISTRY below).
79- static void add_node (node *N);
72+ static void add_node (node *N) {
73+ if (Tail)
74+ Tail->Next = N;
75+ else
76+ Head = N;
77+ Tail = N;
78+ }
8079
8180 // / Iterators for registry entries.
8281 // /
@@ -121,23 +120,61 @@ namespace llvm {
121120 add_node (&Node);
122121 }
123122 };
123+
124+ // / A dynamic import facility. This is used on Windows to
125+ // / import the entries added in the plugin.
126+ static void import (sys::DynamicLibrary &DL, const char *RegistryName) {
127+ typedef void *(*GetRegistry)();
128+ std::string Name (" LLVMGetRegistry_" );
129+ Name.append (RegistryName);
130+ GetRegistry Getter =
131+ (GetRegistry)(intptr_t )DL.getAddressOfSymbol (Name.c_str ());
132+ if (Getter) {
133+ // Call the getter function in order to get the full copy of the
134+ // registry defined in the plugin DLL, and copy them over to the
135+ // current Registry.
136+ typedef std::pair<const node *, const node *> Info;
137+ Info *I = static_cast <Info *>(Getter ());
138+ iterator begin (I->first );
139+ iterator end (I->second );
140+ for (++end; begin != end; ++begin) {
141+ // This Node object needs to remain alive for the
142+ // duration of the program.
143+ add_node (new node (*begin));
144+ }
145+ }
146+ }
147+
148+ // / Retrieve the data to be passed across DLL boundaries when
149+ // / importing registries from another DLL on Windows.
150+ static void *exportRegistry () {
151+ static std::pair<const node *, const node *> Info (Head, Tail);
152+ return &Info;
153+ }
124154 };
155+
156+
157+ // Since these are defined in a header file, plugins must be sure to export
158+ // these symbols.
159+ template <typename T>
160+ typename Registry<T>::node *Registry<T>::Head;
161+
162+ template <typename T>
163+ typename Registry<T>::node *Registry<T>::Tail;
125164} // end namespace llvm
126165
127- // / Instantiate a registry class.
128- // /
129- // / This instantiates add_node and the Head and Tail pointers.
130- #define LLVM_INSTANTIATE_REGISTRY (REGISTRY_CLASS ) \
131- namespace llvm { \
132- template <> void REGISTRY_CLASS::add_node (REGISTRY_CLASS::node *N) { \
133- if (Tail) \
134- Tail->Next = N; \
135- else \
136- Head = N; \
137- Tail = N; \
138- } \
139- template <> typename REGISTRY_CLASS::node *REGISTRY_CLASS::Head = nullptr ; \
140- template <> typename REGISTRY_CLASS::node *REGISTRY_CLASS::Tail = nullptr ; \
166+ #ifdef LLVM_ON_WIN32
167+ #define LLVM_EXPORT_REGISTRY (REGISTRY_CLASS ) \
168+ extern " C" { \
169+ __declspec (dllexport) void *__cdecl LLVMGetRegistry_##REGISTRY_CLASS() { \
170+ return REGISTRY_CLASS::exportRegistry (); \
171+ } \
141172 }
173+ #define LLVM_IMPORT_REGISTRY (REGISTRY_CLASS, DL ) \
174+ REGISTRY_CLASS::import (DL, #REGISTRY_CLASS)
175+ #else
176+ #define LLVM_EXPORT_REGISTRY (REGISTRY_CLASS )
177+ #define LLVM_IMPORT_REGISTRY (REGISTRY_CLASS, DL )
178+ #endif
142179
143180#endif // LLVM_SUPPORT_REGISTRY_H
0 commit comments