Document the rational for the #include hierarchy.
[oota-llvm.git] / docs / SystemLibrary.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2                       "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5   <title>System Library</title>
6   <link rel="stylesheet" href="llvm.css" type="text/css">
7 </head>
8 <body>
9
10 <div class="doc_title">System Library</div>
11
12 <div class="doc_warning">
13   <p>Warning: This document is a work in progress.</p>
14 </div>
15
16 <ul>
17   <li><a href="#abstract">Abstract</a></li>
18   <li><a href="#requirements">System Library Requirements</a>
19   <ol>
20     <li><a href="#headers">Hide System Header Files</a></li>
21     <li><a href="#nofunc">No Exposed Functions</a></li>
22     <li><a href="#nodata">No Exposed Data</a></li>
23     <li><a href="#xcptns">No Exceptions</a></li>
24     <li><a href="#errors">Standard Error Codes</a></li>
25     <li><a href="#overhead">Minimize Overhead</a></li>
26   </ol></li>
27   <li><a href="#design">System Library Design</a>
28   <ol>
29     <li><a href="#opaque">Use Opaque Classes</a></li>
30     <li><a href="#common">Common Implementations</a></li>
31     <li><a href="#multi_imps">Multiple Implementations</a></li>
32     <li><a href="#lowlevel">Use Low Level Interfaces</a></li>
33     <li><a href="#memalloc">No Memory Allocation</a></li>
34     <li><a href="#virtuals">No Virtual Methods</a></li>
35   </ol></li>
36   <li><a href="#detail">System Library Details</a>
37   <ol>
38     <li><a href="#bug">Tracking Bugzilla Bug: 351</a></li>
39     <li><a href="#refimpl">Reference Implementatation</a></li>
40   </ol></li>
41 </ul>
42
43 <div class="doc_author">
44   <p>Written by <a href="rspencer@x10sys.com">Reid Spencer</a></p>
45 </div>
46
47
48 <!-- *********************************************************************** -->
49 <div class="doc_section"><a name="abstract">Abstract</a></div>
50 <div class="doc_text">
51   <p>This document describes the requirements, design, and implementation 
52   details of LLVM's System Library. The library is composed of the header files
53   in <tt>llvm/include/llvm/System</tt> and the source files in 
54   <tt>llvm/lib/System</tt>. The goal of this library is to completely shield 
55   LLVM from the variations in operating system interfaces. By centralizing 
56   LLVM's use of operating system interfaces, we make it possible for the LLVM
57   tool chain and runtime libraries to be more easily ported to new platforms.
58   The library also unclutters the rest of LLVM from #ifdef use and special
59   cases for specific operating systems.</p>
60   <p>The System Library was donated to LLVM by Reid Spencer who formulated the
61   original design as part of the eXtensible Programming System (XPS) which is
62   based, in part, on LLVM.</p>
63 </div>
64
65 <!-- *********************************************************************** -->
66 <div class="doc_section">
67   <a name="requirements">System Library Requirements</a>
68 </div>
69 <div class="doc_text">
70   <p>The System library's requirements are aimed at shielding LLVM from the
71   variations in operating system interfaces. The following sections define the
72   requirements needed to fulfill this objective.</p>
73 </div>
74
75 <!-- ======================================================================= -->
76 <div class="doc_subsection"><a name="headers">Hide System Header Files</a></div>
77 <div class="doc_text">
78   <p>To be written.</p>
79 </div>
80
81 <!-- ======================================================================= -->
82 <div class="doc_subsection"><a name="nofunc">No Exposed Functions</a></div>
83 <div class="doc_text">
84   <p>To be written.</p>
85 </div>
86
87 <!-- ======================================================================= -->
88 <div class="doc_subsection"><a name="nodata">No Exposed Data</a></div>
89 <div class="doc_text">
90   <p>To be written.</p>
91 </div>
92
93 <!-- ======================================================================= -->
94 <div class="doc_subsection"><a name="xcptns">No Exceptions</a></div>
95 <div class="doc_text">
96   <p>To be written.</p>
97 </div>
98
99 <!-- ======================================================================= -->
100 <div class="doc_subsection"><a name="errors">Standard Error Codes</a></div>
101 <div class="doc_text">
102   <p>To be written.</p>
103 </div>
104
105 <!-- ======================================================================= -->
106 <div class="doc_subsection"><a name="overhead">Minimize Overhead</a></div>
107 <div class="doc_text">
108   <p>To be written.</p>
109 </div>
110
111 <!-- *********************************************************************** -->
112 <div class="doc_section"><a name="design">System Library Design</a></div>
113 <div class="doc_text">
114   <p>In order to fulfill the requirements of the system library, strict design
115   objectives must be maintained in the library as it evolves.  The goal here 
116   is to provide interfaces to operating system concepts (files, memory maps, 
117   sockets, signals, locking, etc) efficiently and in such a way that the 
118   remainder of LLVM is completely operating system agnostic.</p>
119 </div>
120
121 <!-- ======================================================================= -->
122 <div class="doc_subsection"><a name="opaque">Use Opaque Classes</a></div>
123 <div class="doc_text">
124   <p>no public data</p>
125   <p>onlyprimitive typed private/protected data</p>
126   <p>data size is "right" for platform, not max of all platforms</p>
127   <p>each class corresponds to O/S concept</p>
128 </div>
129
130 <!-- ======================================================================= -->
131 <div class="doc_subsection"><a name="common">Common Implementations</a></div>
132 <div class="doc_text">
133   <p>To be written.</p>
134 </div>
135
136 <!-- ======================================================================= -->
137 <div class="doc_subsection">
138   <a name="multi_imps">Multiple Implementations</a>
139 </div>
140 <div class="doc_text">
141   <p>To be written.</p>
142 </div>
143
144 <!-- ======================================================================= -->
145 <div class="doc_subsection">
146   <a name="low_level">Use Low Level Interfaces</a>
147 </div>
148 <div class="doc_text">
149   <p>To be written.</p>
150 </div>
151
152 <!-- ======================================================================= -->
153 <div class="doc_subsection"><a name="memalloc">No Memory Allocation</a></div>
154 <div class="doc_text">
155   <p>To be written.</p>
156 </div>
157
158 <!-- ======================================================================= -->
159 <div class="doc_subsection"><a name="virtuals">No Virtual Methods</a></div>
160 <div class="doc_text">
161   <p>To be written.</p>
162 </div>
163
164 <!-- *********************************************************************** -->
165 <div class="doc_section"><a name="detail">System Library Details</a></div>
166 <div class="doc_text">
167   <p>To be written.</p>
168 </div>
169
170 <!-- ======================================================================= -->
171 <div class="doc_subsection"><a name="bug">Bug 351</a></div>
172 <div class="doc_text">
173   <p>See <a href="http://llvm.cs.uiuc.edu/PR351">bug 351</a>
174   for further details on the progress of this work</p>
175 </div>
176
177 <!-- ======================================================================= -->
178 <div class="doc_subsection"><a name="bug">Rationale For #include Hierarchy</a>
179 </div>
180 <div class="doc_text">
181   <p>In order to provide different implementations of the lib/System interface
182   for different platforms, it is necessary for the library to "sense" which
183   operating system is being compiled for and conditionally compile only the
184   applicabe parts of the library. While several operating system wrapper
185   libraries (e.g. APR, ACE) choose to use #ifdef preprocessor statements in
186   combination with autoconf variable (HAVE_* family), lib/System chooses an
187   alternate strategy. <p>
188   <p>To put it succinctly, the lib/System strategy has traded "#ifdef hell" for 
189   "#include hell". That is, a given implementation file defines one or more
190   functions for a particular operating system variant. The functions defined in
191   that file have no #ifdef's to disambiguate the platform since the file is only
192   compiled on one kind of platform. While this leads to the same function being
193   imlemented differently in different files, it is our contention that this
194   leads to better maintenance and easier portability.</p>
195   <p>For example, consider a function having different implementations on a
196   variety of platforms. Many wrapper libraries choose to deal with the different
197   implementations by using #ifdef, like this:</p>
198   <pre><tt>
199       void SomeFunction(void) {
200       #if defined __LINUX
201         // .. Linux implementation
202       #elif defined __WIN32
203         // .. Win32 implementation
204       #elif defined __SunOS
205         // .. SunOS implementation
206       #else
207       #warning "Don't know how to implement SomeFunction on this platform"
208       #endif
209       }
210   </tt></pre>
211   <p>The problem with this is that its very messy to read, especially as the
212   number of operating systems and their variants grow. The above example is
213   actually tame compared to what can happen when the implementation depends on
214   specific flavors and versions of the operating system. In that case you end up
215   with multiple levels of nested #if statements. This is what we mean by "#ifdef
216   hell".</p>
217   <p>To avoid the situation above, we've choosen to locate all functions for a
218   given implementation file for a specific operating system into one place. This
219   has the following advantages:<p>
220   <ul>
221     <li>No "#ifdef hell"</li>
222     <li>When porting, the strategy is quite straight forward: copy the
223     implementation file from a similar operating system to a new directory and
224     re-implement them.<li>
225     <li>Correctness is helped during porting because the new operating system's
226     implementation is wholly contained in a separate directory. There's no
227     chance to make an error in the #if statements and affect some other
228     operating system's implementation.</li>
229   </ul>
230   <p>So, given that we have decided to use #include instead of #if to provide
231   platform specific implementations, there are actually three ways we can go
232   about doing this. None of them are perfect, but we believe we've chosen the
233   lesser of the three evils. Given that there is a variable named $OS which
234   names the platform for which we must build, here's a summary of the three 
235   approaches we could use to determine the correct directory:</p>
236   <ol>
237     <li>Provide the compiler with a -I$(OS) on the command line. This could be
238     provided in only the lib/System makefile.</li>
239     <li>Use autoconf to transform #include statements in the implementation
240     files by using substitutions of @OS@. For example, if we had a file,
241     File.cpp.in, that contained "#include &lt;@OS@/File.cpp&gt;" this would get
242     transformed to "#include &lt;actual/File.cpp&gt;" where "actual" is the
243     actual name of the operating system</li>
244     <li>Create a link from $OBJ_DIR/platform to $SRC_DIR/$OS. This allows us to
245     use a generic directory name to get the correct platform, as in #include
246     &lt;platform/File.cpp&gt;</li>
247   </ol>
248   <p>Let's look at the pitfalls of each approach.</p>
249   <p>In approach #1, we end up with some confusion as to what gets included.
250   Suppose we have lib/System/File.cpp that includes just File.cpp to get the
251   platform specific part of the implementation. In this case, the include
252   directive with the &lt;&gt; syntax will include the right file but the include
253   directive with the "" syntax will recursively include the same file,
254   lib/System/File.cpp. In the case of #include &lt;File.cpp&gt;, the -I options
255   to the compiler are searched first so it works. But in the #include "File.cpp"
256   case, the current directory is searched first. Furthermore, in both cases,
257   neither include directive documents which File.cpp is getting included.</p>
258   <p>In approach #2, we have the problem of needing to reconfigure repeatedly.
259   Developer's generally hate that and we don't want lib/System to be a thorn in
260   everyone's side because it will constantly need updating as operating systems
261   change and as new operating systems are added. The problem occurs when a new
262   implementation file is added to the library. First of all, you have to add a
263   file with the .in suffix, then you have to add that file name to the list of
264   configurable files in the autoconf/configure.ac file, then you have to run
265   AutoRegen.sh to rebuild the configure script, then you have to run the
266   configure script. This is deemed to be a pretty large hassle.</p>
267   <p>In approach #3, we have the problem that not all platforms support links.
268   Fortunately the autoconf macro used to create the link can compensate for
269   this. If a link can't be made, the configure script will copy the correct
270   directory from $BUILD_SRC_DIR to $BUILD_OBJ_DIR under the new name. The only
271   problem with this is that if a copy is made, the copy doesn't get updated if
272   the programmer adds or modifies files in the $BUILD_SRC_DIR. A reconfigure or
273   manual copying is needed to get things to compile.<p>
274   <p>The approach we have taken in lib/System is #3. Here's why:<p>
275   <ul>
276     <li>Approach #1 is rejected because it doesn't document what's actually
277     getting included and the potential for mistakes with alternate include
278     directive forms is high.</li>
279     <li>Approach #2 are both viable and only really impact development when new
280     files are added to the library.</li>
281     <li>However, approach #2 impacts every new file on every platform all the
282     time. With approach #3, only those platforms not supporting links will be
283     affected. The number of platforms not supporting links is very small and
284     they are generally archaic.</li>
285     <li>Given the above, approach #3 seems to have the least impact.</li>
286   </ul>
287 </div>
288
289 <!-- ======================================================================= -->
290 <div class="doc_subsection">
291   <a name="refimpl">Reference Implementation</a>
292 </div>
293 <div class="doc_text">
294   <p>The <tt>linux</tt> implementation of the system library will always be the
295   reference implementation. This means that (a) the concepts defined by the
296   linux must be identically replicated in the other implementations and (b) the
297   linux implementation must always be complete (provide implementations for all
298   concepts).</p>
299 </div>
300
301 <!-- *********************************************************************** -->
302
303 <hr>
304 <address>
305   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
306   src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
307   <a href="http://validator.w3.org/check/referer"><img
308   src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
309
310   <a href="mailto:rspencer@x10sys.com">Reid Spencer</a><br>
311   <a href="http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure</a><br>
312   Last modified: $Date$
313 </address>
314 </body>
315 </html>