1 // Macros for correct module ordering.
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.
12 // But for now, I take a half-automatic approach to the correct module
13 // ordering problem: PROVIDE/REQUIRE, as in Common Lisp.
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.)
22 // These macros work only with g++, and only in optimizing mode. But who
23 // wants to use CLN with other C++ compilers anyway...
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.
57 // OK, stop reading here, because it's getting obscene.
59 #if defined(__GNUC__) && defined(__OPTIMIZE__) && !(defined(__hppa__) && (__GNUC__ == 2) && (__GNUC_MINOR__ < 8)) && !defined(NO_PROVIDE_REQUIRE)
61 #define ASM_UNDERSCORE_PREFIX "_"
63 #define ASM_UNDERSCORE_PREFIX ""
65 // Globalize a label defined in the same translation unit.
66 // See macro ASM_GLOBALIZE_LABEL in the egcs sources.
67 #if defined(__i386__) || defined(__m68k__) || defined(__mips__) || defined(__mips64__) || defined(__alpha__) || defined(__rs6000__)
68 // Some m68k systems use "xdef" or "global" or ".global"...
69 #define CL_GLOBALIZE_LABEL(label) __asm__("\t.globl " label);
71 #if defined(__sparc__) || defined(__sparc64__) || defined(__arm__)
72 // Some arm systems use "EXPORT" or ".globl"...
73 #define CL_GLOBALIZE_LABEL(label) __asm__("\t.global " label);
76 #define CL_GLOBALIZE_LABEL(label) __asm__("\t.EXPORT " label ",ENTRY,PRIV_LEV=3");
79 #define CL_GLOBALIZE_LABEL(label) __asm__("\tglobal " label);
81 #if defined(__convex__)
82 #define CL_GLOBALIZE_LABEL(label) __asm__(".globl " label);
84 #ifndef CL_GLOBALIZE_LABEL
85 #define CL_GLOBALIZE_LABEL(label)
87 #if defined(__rs6000__) || defined(_WIN32)
88 #define CL_GLOBALIZE_JUMP_LABEL(label) CL_GLOBALIZE_LABEL(#label)
90 #define CL_GLOBALIZE_JUMP_LABEL(label)
92 #ifdef CL_NEED_GLOBALIZE_CTORDTOR
93 #define CL_GLOBALIZE_CTORDTOR_LABEL(label) CL_GLOBALIZE_LABEL(label)
95 #define CL_GLOBALIZE_CTORDTOR_LABEL(label)
97 // Output a label inside a function.
98 // See macro ASM_OUTPUT_LABEL in the egcs sources.
100 #define CL_OUTPUT_LABEL(label) ASM_VOLATILE ("\n" label)
102 #define CL_OUTPUT_LABEL(label) ASM_VOLATILE ("\n" label ":")
104 // ASM_VOLATILE(string) is for asms without arguments only!!
105 #if ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 91)) || (__GNUC__ >= 3)
106 // avoid warning caused by the volatile keyword
107 #define ASM_VOLATILE __asm__
109 // need volatile to avoid reordering
110 #define ASM_VOLATILE __asm__ __volatile__
112 // CL_JUMP_TO(addr) jumps to an address, like goto *(void*)(addr),
113 // except that the latter inhibits inlining of the function containing it
114 // in gcc-2.95. For new CPUs, look for "jump" and "indirect_jump" in gcc's
115 // machine description.
116 #if defined(__i386__)
117 #define CL_JUMP_TO(addr) ASM_VOLATILE("jmp %*%0" : : "rm" ((void*)(addr)))
119 #if defined(__m68k__)
120 #define CL_JUMP_TO(addr) ASM_VOLATILE("jmp %0@" : : "a" ((void*)(addr)))
122 #if defined(__mips__)
123 #define CL_JUMP_TO(addr) ASM_VOLATILE("%*j %0" : : "d" ((void*)(addr)))
125 #if defined(__sparc__) || defined(__sparc64__)
126 #define CL_JUMP_TO(addr) ASM_VOLATILE("jmp %0\n\tnop" : : "r" ((void*)(addr)))
128 #if defined(__alpha__)
129 #define CL_JUMP_TO(addr) ASM_VOLATILE("jmp $31,(%0),0" : : "r" ((void*)(addr)))
131 #if defined(__hppa__)
132 //#define CL_JUMP_TO(addr) ASM_VOLATILE("bv,n 0(%0)" : : "r" ((void*)(addr)))
133 #define CL_JUMP_TO(addr) ASM_VOLATILE("b " #addr "\n\tnop")
136 #define CL_JUMP_TO(addr) ASM_VOLATILE("mov pc,%0" : : "r" ((void*)(addr)))
138 #if defined(__rs6000__) || defined(__powerpc__) || defined(__ppc__)
139 //#define CL_JUMP_TO(addr) ASM_VOLATILE("mtctr %0\n\tbctr" : : "r" ((void*)(addr)))
140 #define CL_JUMP_TO(addr) ASM_VOLATILE("b " #addr)
142 #if defined(__m88k__)
143 #define CL_JUMP_TO(addr) ASM_VOLATILE("jmp %0" : : "r" ((void*)(addr)))
145 #if defined(__convex__)
146 #define CL_JUMP_TO(addr) ASM_VOLATILE("jmp (%0)" : : "r" ((void*)(addr)))
148 #define CL_PROVIDE(module) \
149 extern "C" void cl_module__##module##__firstglobalfun () {} \
150 extern "C" void cl_module__##module##__ctorend (void); \
151 extern "C" void cl_module__##module##__dtorend (void); \
152 CL_GLOBALIZE_JUMP_LABEL(cl_module__##module##__ctorend) \
153 CL_GLOBALIZE_JUMP_LABEL(cl_module__##module##__dtorend) \
154 CL_GLOBALIZE_CTORDTOR_LABEL( \
155 ASM_UNDERSCORE_PREFIX CL_GLOBAL_CONSTRUCTOR_PREFIX \
156 "cl_module__" #module "__firstglobalfun") \
157 CL_GLOBALIZE_CTORDTOR_LABEL( \
158 ASM_UNDERSCORE_PREFIX CL_GLOBAL_DESTRUCTOR_PREFIX \
159 "cl_module__" #module "__firstglobalfun") \
160 static int cl_module__##module##__counter; \
161 struct cl_module__##module##__controller { \
162 inline cl_module__##module##__controller () \
163 { if (cl_module__##module##__counter++) \
164 { CL_JUMP_TO(cl_module__##module##__ctorend); } \
166 inline ~cl_module__##module##__controller () \
167 { CL_OUTPUT_LABEL (ASM_UNDERSCORE_PREFIX "cl_module__" #module "__dtorend"); } \
169 static cl_module__##module##__controller cl_module__##module##__ctordummy;
170 #define CL_PROVIDE_END(module) \
171 struct cl_module__##module##__destroyer { \
172 inline cl_module__##module##__destroyer () \
173 { CL_OUTPUT_LABEL (ASM_UNDERSCORE_PREFIX "cl_module__" #module "__ctorend"); } \
174 inline ~cl_module__##module##__destroyer () \
175 { if (--cl_module__##module##__counter) \
176 { CL_JUMP_TO(cl_module__##module##__dtorend); } \
179 static cl_module__##module##__destroyer cl_module__##module##__dtordummy;
180 #define CL_REQUIRE(module) \
181 extern "C" void cl_module__##module##__ctor (void) \
182 __asm__ (ASM_UNDERSCORE_PREFIX CL_GLOBAL_CONSTRUCTOR_PREFIX \
183 "cl_module__" #module "__firstglobalfun"); \
184 extern "C" void cl_module__##module##__dtor (void) \
185 __asm__ (ASM_UNDERSCORE_PREFIX CL_GLOBAL_DESTRUCTOR_PREFIX \
186 "cl_module__" #module "__firstglobalfun"); \
187 struct _CL_REQUIRE_CLASSNAME(module,__LINE__) { \
188 inline _CL_REQUIRE_CLASSNAME(module,__LINE__) () \
189 { cl_module__##module##__ctor (); } \
190 inline ~_CL_REQUIRE_CLASSNAME(module,__LINE__) () \
191 { cl_module__##module##__dtor (); } \
193 static _CL_REQUIRE_CLASSNAME(module,__LINE__) \
194 _CL_REQUIRE_CLASSNAME(module##_requirer,__LINE__);
195 #define _CL_REQUIRE_CLASSNAME(module,line) __CL_REQUIRE_CLASSNAME(module,line)
196 #define __CL_REQUIRE_CLASSNAME(module,line) cl_module__##module##__##line
198 #define CL_PROVIDE(module)
199 #define CL_PROVIDE_END(module)
200 #define CL_REQUIRE(module)
203 #endif /* _CL_MODULES_H */