]> www.ginac.de Git - cln.git/blob - include/cln/modules.h
Force link-time references despite optimizations done by g++.
[cln.git] / include / cln / modules.h
1 // Macros for correct module ordering.
2
3 #ifndef _CL_MODULES_H
4 #define _CL_MODULES_H
5
6 // The order of initialization of different compilation units is not
7 // specified in C++. AIX 4 has a linker which apparently does order
8 // the modules according to dependencies, so that low-level modules
9 // will be initialized earlier than the high-level modules which depend
10 // on them. I have a patch for GNU ld that does the same thing.
11 //
12 // But for now, I take a half-automatic approach to the correct module
13 // ordering problem: PROVIDE/REQUIRE, as in Common Lisp.
14 //
15 // CL_PROVIDE(module) must be the first code-generating entity in a module.
16 // Inline function definitions can precede it, but global variable/function/
17 // class definitions may not precede it.
18 // Afterwards, any number of CL_REQUIRE(othermodule) is allowed.
19 // At the end of the module, there must be a corresponding
20 // CL_PROVIDE_END(module). (Sorry for this, it's really needed.)
21 //
22 // These macros work only with g++, and only in optimizing mode. But who
23 // wants to use CLN with other C++ compilers anyway...
24
25 // How to apply these macros:
26 // 1. Find out about variables which need to be initialized.
27 //    On Linux/ELF, you can use a command like
28 //    $ nm -o libcln.a | grep -v ' [UTtRrW] ' | sort +1
29 //    A symbol of type "D" or "d" lies in the preinitialized DATA section,
30 //    a symbol of type "B" or "b" lies in the uninitialized BSS section.
31 //    All of them have to be checked.
32 //  - Those which contain POD (= plain old data, i.e. scalar values or
33 //    class instances without nontrivial constructors) are already fully
34 //    initialized by the linker and can be discarded from these considerations.
35 //  - Those which are static variables inside a function (you recognize
36 //    them: g++ appends a dot and a number to their name) are initialized
37 //    the first time the function is entered. They can be discarded from
38 //    our considerations as well.
39 // 2. Find out which of these variables are publically exposed (to the user of
40 //    the library) through the library's include files, either directly or
41 //    through inline functions, or indirectly through normal function calls.
42 //    These variables can be referenced from any user module U, hence any
43 //    such module must CL_REQUIRE(M) the variable's definition module M.
44 //    Since there is no CL_REQUIRE_IF_NEEDED(M) macro (which is equivalent
45 //    to CL_REQUIRE(M) if the required module will be part of the executable
46 //    but does nothing if M is not used), we must preventively put the
47 //    CL_REQUIRE(M) into the header file. Hopefully M is either used anyway
48 //    or does not bring in too much code into the executable.
49 // 3. Variables which are not publicly exposed but used internally by the
50 //    library can be handled by adding a CL_REQUIRE in all the library's
51 //    modules which directly or indirectly use the variable.
52 // 4. Variables and functions which can be reasonably assumed to not be
53 //    accessed or executed during initialization need not be treated.
54 //    For example, I/O to external streams, exception handling facilities,
55 //    number theory stuff, etc.
56
57 // OK, stop reading here, because it's getting obscene.
58
59 #if defined(__GNUC__) && defined(__OPTIMIZE__) && !(defined(__hppa__) && (__GNUC__ == 2) && (__GNUC_MINOR__ < 8)) && !defined(NO_PROVIDE_REQUIRE)
60   #ifdef ASM_UNDERSCORE
61     #define ASM_UNDERSCORE_PREFIX "_"
62   #else
63     #define ASM_UNDERSCORE_PREFIX ""
64   #endif
65   // Globalize a label defined in the same translation unit.
66   // See macro ASM_GLOBALIZE_LABEL in the gcc sources.
67   #if defined(__i386__) || defined(__m68k__) || defined(__mips__) || defined(__mips64__) || defined(__alpha__) || defined(__rs6000__) || defined(__s390__)
68     // Some m68k systems use "xdef" or "global" or ".global"...
69     #define CL_GLOBALIZE_LABEL(label)  __asm__("\t.globl " label);
70   #endif
71   #if defined(__sparc__) || defined(__sparc64__) || defined(__arm__) || defined(__ia64__)
72     // Some arm systems use "EXPORT" or ".globl"...
73     #define CL_GLOBALIZE_LABEL(label)  __asm__("\t.global " label);
74   #endif
75   #if defined(__hppa__)
76     #define CL_GLOBALIZE_LABEL(label)  __asm__("\t.EXPORT " label ",ENTRY,PRIV_LEV=3");
77   #endif
78   #if defined(__m88k__)
79     #define CL_GLOBALIZE_LABEL(label)  __asm__("\tglobal " label);
80   #endif
81   #if defined(__convex__)
82     #define CL_GLOBALIZE_LABEL(label)  __asm__(".globl " label);
83   #endif
84   #ifndef CL_GLOBALIZE_LABEL
85     #define CL_GLOBALIZE_LABEL(label)
86   #endif
87   #if defined(__rs6000__) || defined(_WIN32)
88     #define CL_GLOBALIZE_JUMP_LABEL(label)  CL_GLOBALIZE_LABEL(#label)
89   #else
90     #define CL_GLOBALIZE_JUMP_LABEL(label)
91   #endif
92   #ifdef CL_NEED_GLOBALIZE_CTORDTOR
93     #define CL_GLOBALIZE_CTORDTOR_LABEL(label)  CL_GLOBALIZE_LABEL(label)
94   #else
95     #define CL_GLOBALIZE_CTORDTOR_LABEL(label)
96   #endif
97   // Output a label inside a function.
98   // See macro ASM_OUTPUT_LABEL in the gcc sources.
99   #if defined(__hppa__)
100     // Some hppa (Linux) systems want `label:', HPUX used to use just `label'.
101     // I tried to find out, but was unable to find the assembler on my HPUX-11
102     // boxen so decided to potentially ditch the support (no joke).  Please
103     // send an email if you can explain to me what's going on! (-rbk. 07/2001)
104     #define CL_OUTPUT_LABEL(label)  ASM_VOLATILE ("\n" label ":")
105   #else
106     #define CL_OUTPUT_LABEL(label)  ASM_VOLATILE ("\n" label ":")
107   #endif
108   // ASM_VOLATILE(string) is for asms without arguments only!!
109   #if ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 91)) || (__GNUC__ >= 3)
110     // avoid warning caused by the volatile keyword
111     #define ASM_VOLATILE  __asm__
112   #else
113     // need volatile to avoid reordering
114     #define ASM_VOLATILE  __asm__ __volatile__
115   #endif
116   // CL_JUMP_TO(addr) jumps to an address, like  goto *(void*)(addr),
117   // except that the latter inhibits inlining of the function containing it
118   // in gcc-2.95. For new CPUs, look for "jump" and "indirect_jump" in gcc's
119   // machine description.
120   #if defined(__i386__)
121     #define CL_JUMP_TO(addr)  ASM_VOLATILE("jmp %*%0" : : "rm" ((void*)(addr)))
122   #endif
123   #if defined(__m68k__)
124     #define CL_JUMP_TO(addr)  ASM_VOLATILE("jmp %0@" : : "a" ((void*)(addr)))
125   #endif
126   #if defined(__mips__)
127     #define CL_JUMP_TO(addr)  ASM_VOLATILE("%*j %0" : : "d" ((void*)(addr)))
128   #endif
129   #if defined(__sparc__) || defined(__sparc64__)
130     #define CL_JUMP_TO(addr)  ASM_VOLATILE("jmp %0\n\tnop" : : "r" ((void*)(addr)))
131   #endif
132   #if defined(__alpha__)
133     #define CL_JUMP_TO(addr)  ASM_VOLATILE("jmp $31,(%0),0" : : "r" ((void*)(addr)))
134   #endif
135   #if defined(__hppa__)
136     //#define CL_JUMP_TO(addr)  ASM_VOLATILE("bv,n 0(%0)" : : "r" ((void*)(addr)))
137     #define CL_JUMP_TO(addr)  ASM_VOLATILE("b " #addr "\n\tnop")
138   #endif
139   #if defined(__arm__)
140     #define CL_JUMP_TO(addr)  ASM_VOLATILE("mov pc,%0" : : "r" ((void*)(addr)))
141   #endif
142   #if defined(__rs6000__) || defined(__powerpc__) || defined(__ppc__)
143     //#define CL_JUMP_TO(addr)  ASM_VOLATILE("mtctr %0\n\tbctr" : : "r" ((void*)(addr)))
144     #define CL_JUMP_TO(addr)  ASM_VOLATILE("b " #addr)
145   #endif
146   #if defined(__m88k__)
147     #define CL_JUMP_TO(addr)  ASM_VOLATILE("jmp %0" : : "r" ((void*)(addr)))
148   #endif
149   #if defined(__convex__)
150     #define CL_JUMP_TO(addr)  ASM_VOLATILE("jmp (%0)" : : "r" ((void*)(addr)))
151   #endif
152   #if defined(__ia64__)
153     #define CL_JUMP_TO(addr)  ASM_VOLATILE("br " #addr)
154   #endif
155   #if defined(__s390__)
156     #define CL_JUMP_TO(addr)  ASM_VOLATILE("br %0" : : "a" ((void*)(addr)))
157   #endif
158   #ifdef CL_GLOBAL_DESTRUCTOR_PREFIX
159     #define CL_PROVIDE(module)  \
160       extern "C" void cl_module__##module##__firstglobalfun () {}       \
161       extern "C" void cl_module__##module##__ctorend (void);            \
162       extern "C" void cl_module__##module##__dtorend (void);            \
163       CL_GLOBALIZE_JUMP_LABEL(cl_module__##module##__ctorend)           \
164       CL_GLOBALIZE_JUMP_LABEL(cl_module__##module##__dtorend)           \
165       CL_GLOBALIZE_CTORDTOR_LABEL(                                      \
166                  ASM_UNDERSCORE_PREFIX CL_GLOBAL_CONSTRUCTOR_PREFIX     \
167                  "cl_module__" #module "__firstglobalfun")              \
168       CL_GLOBALIZE_CTORDTOR_LABEL(                                      \
169                  ASM_UNDERSCORE_PREFIX CL_GLOBAL_DESTRUCTOR_PREFIX      \
170                  "cl_module__" #module "__firstglobalfun")              \
171       static int cl_module__##module##__counter;                        \
172       struct cl_module__##module##__controller {                        \
173         inline cl_module__##module##__controller ()                     \
174           { if (cl_module__##module##__counter++)                       \
175               { CL_JUMP_TO(cl_module__##module##__ctorend); }           \
176           }                                                             \
177         inline ~cl_module__##module##__controller ()                    \
178           { CL_OUTPUT_LABEL (ASM_UNDERSCORE_PREFIX "cl_module__" #module "__dtorend"); } \
179       };                                                                        \
180       static cl_module__##module##__controller cl_module__##module##__ctordummy;
181     #define CL_PROVIDE_END(module)  \
182       struct cl_module__##module##__destroyer {                         \
183         inline cl_module__##module##__destroyer ()                      \
184           { CL_OUTPUT_LABEL (ASM_UNDERSCORE_PREFIX "cl_module__" #module "__ctorend"); } \
185         inline ~cl_module__##module##__destroyer ()                     \
186           { if (--cl_module__##module##__counter)                       \
187               { CL_JUMP_TO(cl_module__##module##__dtorend); }           \
188           }                                                             \
189       };                                                                \
190       static cl_module__##module##__destroyer cl_module__##module##__dtordummy;
191     #define CL_REQUIRE(module)  \
192       extern "C" void cl_module__##module##__ctor (void)                \
193         __asm__ (ASM_UNDERSCORE_PREFIX CL_GLOBAL_CONSTRUCTOR_PREFIX     \
194                  "cl_module__" #module "__firstglobalfun");             \
195       extern "C" void cl_module__##module##__dtor (void)                \
196         __asm__ (ASM_UNDERSCORE_PREFIX CL_GLOBAL_DESTRUCTOR_PREFIX      \
197                  "cl_module__" #module "__firstglobalfun");             \
198       struct _CL_REQUIRE_CLASSNAME(module,__LINE__) {                   \
199         inline _CL_REQUIRE_CLASSNAME(module,__LINE__) ()                \
200           { cl_module__##module##__ctor (); }                           \
201         inline ~_CL_REQUIRE_CLASSNAME(module,__LINE__) ()               \
202           { cl_module__##module##__dtor (); }                           \
203       };                                                                \
204       static _CL_REQUIRE_CLASSNAME(module,__LINE__)                     \
205         _CL_REQUIRE_CLASSNAME(module##_requirer,__LINE__);
206   #else
207     // gcc-3.0 -fuse-cxa-atexit doesn't have a single per-module destructor
208     // function anymore. Instead, for each object's static constructor it
209     // executes, it pushes the corresponding object's destructor onto a list.
210     // Thus we need to hack the constructors only.
211     #define CL_PROVIDE(module)  \
212       extern "C" void cl_module__##module##__firstglobalfun () {}       \
213       extern "C" void cl_module__##module##__ctorend (void);            \
214       CL_GLOBALIZE_JUMP_LABEL(cl_module__##module##__ctorend)           \
215       CL_GLOBALIZE_CTORDTOR_LABEL(                                      \
216                  ASM_UNDERSCORE_PREFIX CL_GLOBAL_CONSTRUCTOR_PREFIX     \
217                  "cl_module__" #module "__firstglobalfun")              \
218       static int cl_module__##module##__counter;                        \
219       struct cl_module__##module##__controller {                        \
220         inline cl_module__##module##__controller ()                     \
221           { if (cl_module__##module##__counter++)                       \
222               { CL_JUMP_TO(cl_module__##module##__ctorend); }           \
223           }                                                             \
224       };                                                                \
225       static cl_module__##module##__controller cl_module__##module##__ctordummy;
226     #define CL_PROVIDE_END(module)  \
227       struct cl_module__##module##__destroyer {                         \
228         inline cl_module__##module##__destroyer ()                      \
229           { CL_OUTPUT_LABEL (ASM_UNDERSCORE_PREFIX "cl_module__" #module "__ctorend"); } \
230       };                                                                \
231       static cl_module__##module##__destroyer cl_module__##module##__dtordummy;
232     #define CL_REQUIRE(module)  \
233       extern "C" void cl_module__##module##__ctor (void)                \
234         __asm__ (ASM_UNDERSCORE_PREFIX CL_GLOBAL_CONSTRUCTOR_PREFIX     \
235                  "cl_module__" #module "__firstglobalfun");             \
236       struct _CL_REQUIRE_CLASSNAME(module,__LINE__) {                   \
237         inline _CL_REQUIRE_CLASSNAME(module,__LINE__) ()                \
238           { cl_module__##module##__ctor (); }                           \
239       };                                                                \
240       static _CL_REQUIRE_CLASSNAME(module,__LINE__)                     \
241         _CL_REQUIRE_CLASSNAME(module##_requirer,__LINE__);
242   #endif
243   #define _CL_REQUIRE_CLASSNAME(module,line) __CL_REQUIRE_CLASSNAME(module,line)
244   #define __CL_REQUIRE_CLASSNAME(module,line) cl_module__##module##__##line
245 #else
246   #define CL_PROVIDE(module)
247   #define CL_PROVIDE_END(module)
248   #define CL_REQUIRE(module)
249 #endif
250
251 // Sometimes a link time dependency is needed, but without requirements
252 // on initialization order.
253 //
254 // CL_FORCE_LINK(dummy,external_variable)
255 // forces a link time reference to the external_variable.
256 #include <stdlib.h>
257 #if 0
258 // This definition does not work.  It gets optimized away by g++ 3.1.
259 #define CL_FORCE_LINK(dummy,external_variable) \
260   static const void* const dummy[] = { &dummy, &external_variable };
261 #else
262 #define CL_FORCE_LINK(dummy,external_variable) \
263   static const                                                          \
264   struct dummy {                                                        \
265     inline dummy () {                                                   \
266       if ((void*) &external_variable == (void*) this)                   \
267         abort();                                                        \
268     }                                                                   \
269   }                                                                     \
270   dummy##_instance;
271 #endif
272
273 #endif /* _CL_MODULES_H */