Fix comment typo.
[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 <ul>
12   <li><a href="#abstract">Abstract</a></li>
13   <li><a href="#requirements">Keeping LLVM Portable</a>
14   <ol>
15     <li><a href="#headers">Don't Include System Headers</a></li>
16     <li><a href="#expose">Don't Expose System Headers</a></li>
17     <li><a href="#c_headers">Allow Standard C Header Files</a></li>
18     <li><a href="#cpp_headers">Allow Standard C++ Header Files</a></li>
19     <li><a href="#highlev">High-Level Interface</a></li>
20     <li><a href="#nofunc">No Exposed Functions</a></li>
21     <li><a href="#nodata">No Exposed Data</a></li>
22     <li><a href="#nodupl">No Duplicate Implementations</a></li>
23     <li><a href="#nounused">No Unused Functionality</a></li>
24     <li><a href="#virtuals">No Virtual Methods</a></li>
25     <li><a href="#softerrors">Minimize Soft Errors</a></li>
26     <li><a href="#throw_spec">No throw() Specifications</a></li>
27     <li><a href="#organization">Code Organization</a></li>
28     <li><a href="#semantics">Consistent Semantics</a></li>
29     <li><a href="#bug">Tracking Bugzilla Bug: 351</a></li>
30   </ol></li>
31 </ul>
32
33 <div class="doc_author">
34   <p>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a></p>
35 </div>
36
37
38 <!-- *********************************************************************** -->
39 <div class="doc_section"><a name="abstract">Abstract</a></div>
40 <div class="doc_text">
41   <p>This document provides some details on LLVM's System Library, located in
42   the source at <tt>lib/System</tt> and <tt>include/llvm/System</tt>. The
43   library's purpose is to shield LLVM from the differences between operating
44   systems for the few services LLVM needs from the operating system. Much of
45   LLVM is written using portability features of standard C++. However, in a few
46   areas, system dependent facilities are needed and the System Library is the
47   wrapper around those system calls.</p>
48   <p>By centralizing LLVM's use of operating system interfaces, we make it 
49   possible for the LLVM tool chain and runtime libraries to be more easily 
50   ported to new platforms since (theoretically) only <tt>lib/System</tt> needs 
51   to be ported.  This library also unclutters the rest of LLVM from #ifdef use 
52   and special cases for specific operating systems. Such uses are replaced 
53   with simple calls to the interfaces provided in <tt>include/llvm/System</tt>.
54   </p> 
55   <p>Note that the System Library is not intended to be a complete operating 
56   system wrapper (such as the Adaptive Communications Environment (ACE) or 
57   Apache Portable Runtime (APR)), but only provides the functionality necessary
58   to support LLVM.
59   <p>The System Library was written by Reid Spencer who formulated the
60   design based on similar work originating from the eXtensible Programming 
61   System (XPS). Several people helped with the effort; especially,
62   Jeff Cohen and Henrik Bach on the Win32 port.</p>
63 </div>
64
65 <!-- *********************************************************************** -->
66 <div class="doc_section">
67   <a name="requirements">Keeping LLVM Portable</a>
68 </div>
69 <div class="doc_text">
70   <p>In order to keep LLVM portable, LLVM developers should adhere to a set of
71   portability rules associated with the System Library. Adherence to these rules
72   should help the System Library achieve its goal of shielding LLVM from the
73   variations in operating system interfaces and doing so efficiently.  The 
74   following sections define the rules needed to fulfill this objective.</p>
75 </div>
76
77 <!-- ======================================================================= -->
78 <div class="doc_subsection"><a name="headers">Don't Include System Headers</a>
79 </div>
80 <div class="doc_text">
81   <p>Except in <tt>lib/System</tt>, no LLVM source code should directly
82   <tt>#include</tt> a system header. Care has been taken to remove all such
83   <tt>#includes</tt> from LLVM while <tt>lib/System</tt> was being
84   developed.  Specifically this means that header files like "unistd.h", 
85   "windows.h", "stdio.h", and "string.h" are forbidden to be included by LLVM 
86   source code outside the implementation of <tt>lib/System</tt>.</p>
87   <p>To obtain system-dependent functionality, existing interfaces to the system
88   found in <tt>include/llvm/System</tt> should be used. If an appropriate 
89   interface is not available, it should be added to <tt>include/llvm/System</tt>
90   and implemented in <tt>lib/System</tt> for all supported platforms.</p>
91 </div>
92
93 <!-- ======================================================================= -->
94 <div class="doc_subsection"><a name="expose">Don't Expose System Headers</a>
95 </div>
96 <div class="doc_text">
97   <p>The System Library must shield LLVM from <em>all</em> system headers. To 
98   obtain system level functionality, LLVM source must 
99   <tt>#include "llvm/System/Thing.h"</tt> and nothing else. This means that 
100   <tt>Thing.h</tt> cannot expose any system header files. This protects LLVM 
101   from accidentally using system specific functionality and only allows it
102   via the <tt>lib/System</tt> interface.</p>
103 </div>
104
105 <!-- ======================================================================= -->
106 <div class="doc_subsection"><a name="c_headers">Use Standard C Headers</a></div>
107 <div class="doc_text">
108   <p>The <em>standard</em> C headers (the ones beginning with "c") are allowed
109   to be exposed through the <tt>lib/System</tt> interface. These headers and 
110   the things they declare are considered to be platform agnostic. LLVM source 
111   files may include them directly or obtain their inclusion through 
112   <tt>lib/System</tt> interfaces.</p>
113 </div>
114
115 <!-- ======================================================================= -->
116 <div class="doc_subsection"><a name="cpp_headers">Use Standard C++ Headers</a>
117 </div>
118 <div class="doc_text">
119   <p>The <em>standard</em> C++ headers from the standard C++ library and
120   standard template library may be exposed through the <tt>lib/System</tt>
121   interface. These headers and the things they declare are considered to be
122   platform agnostic. LLVM source files may include them or obtain their
123   inclusion through lib/System interfaces.</p>
124 </div>
125
126 <!-- ======================================================================= -->
127 <div class="doc_subsection"><a name="highlev">High Level Interface</a></div>
128 <div class="doc_text">
129   <p>The entry points specified in the interface of lib/System must be aimed at 
130   completing some reasonably high level task needed by LLVM. We do not want to
131   simply wrap each operating system call. It would be preferable to wrap several
132   operating system calls that are always used in conjunction with one another by
133   LLVM.</p>
134   <p>For example, consider what is needed to execute a program, wait for it to
135   complete, and return its result code. On Unix, this involves the following
136   operating system calls: <tt>getenv, fork, execve,</tt> and <tt>wait</tt>. The
137   correct thing for lib/System to provide is a function, say
138   <tt>ExecuteProgramAndWait</tt>, that implements the functionality completely.
139   what we don't want is wrappers for the operating system calls involved.</p>
140   <p>There must <em>not</em> be a one-to-one relationship between operating
141   system calls and the System library's interface. Any such interface function
142   will be suspicious.</p>
143 </div>
144
145 <!-- ======================================================================= -->
146 <div class="doc_subsection"><a name="nounused">No Unused Functionality</a></div>
147 <div class="doc_text">
148   <p>There must be no functionality specified in the interface of lib/System 
149   that isn't actually used by LLVM. We're not writing a general purpose
150   operating system wrapper here, just enough to satisfy LLVM's needs. And, LLVM
151   doesn't need much. This design goal aims to keep the lib/System interface
152   small and understandable which should foster its actual use and adoption.</p>
153 </div>
154
155 <!-- ======================================================================= -->
156 <div class="doc_subsection"><a name="nodupl">No Duplicate Implementations</a>
157 </div>
158 <div class="doc_text">
159   <p>The implementation of a function for a given platform must be written
160   exactly once. This implies that it must be possible to apply a function's 
161   implementation to multiple operating systems if those operating systems can
162   share the same implementation. This rule applies to the set of operating
163   systems supported for a given class of operating system (e.g. Unix, Win32).
164   </p>
165 </div>
166
167 <!-- ======================================================================= -->
168 <div class="doc_subsection"><a name="virtuals">No Virtual Methods</a></div>
169 <div class="doc_text">
170   <p>The System Library interfaces can be called quite frequently by LLVM. In
171   order to make those calls as efficient as possible, we discourage the use of
172   virtual methods. There is no need to use inheritance for implementation
173   differences, it just adds complexity. The <tt>#include</tt> mechanism works
174   just fine.</p>
175 </div>
176
177 <!-- ======================================================================= -->
178 <div class="doc_subsection"><a name="nofunc">No Exposed Functions</a></div>
179 <div class="doc_text">
180   <p>Any functions defined by system libraries (i.e. not defined by lib/System) 
181   must not be exposed through the lib/System interface, even if the header file 
182   for that function is not exposed. This prevents inadvertent use of system
183   specific functionality.</p>
184   <p>For example, the <tt>stat</tt> system call is notorious for having
185   variations in the data it provides. <tt>lib/System</tt> must not declare 
186   <tt>stat</tt> nor allow it to be declared. Instead it should provide its own 
187   interface to discovering information about files and directories. Those 
188   interfaces may be implemented in terms of <tt>stat</tt> but that is strictly 
189   an implementation detail. The interface provided by the System Library must
190   be implemented on all platforms (even those without <tt>stat</tt>).</p>
191 </div>
192
193 <!-- ======================================================================= -->
194 <div class="doc_subsection"><a name="nodata">No Exposed Data</a></div>
195 <div class="doc_text">
196   <p>Any data defined by system libraries (i.e. not defined by lib/System) must
197   not be exposed through the lib/System interface, even if the header file for
198   that function is not exposed. As with functions, this prevents inadvertent use
199   of data that might not exist on all platforms.</p>
200 </div>
201
202 <!-- ======================================================================= -->
203 <div class="doc_subsection"><a name="softerrors">Minimize Soft Errors</a></div>
204 <div class="doc_text">
205   <p>Operating system interfaces will generally provide error results for every
206   little thing that could go wrong. In almost all cases, you can divide these
207   error results into two groups: normal/good/soft and abnormal/bad/hard. That
208   is, some of the errors are simply information like "file not found", 
209   "insufficient privileges", etc. while other errors are much harder like
210   "out of space", "bad disk sector", or "system call interrupted". We'll call 
211   the first group "<i>soft</i>" errors and the second group "<i>hard</i>" 
212   errors.<p>
213   <p>lib/System must always attempt to minimize soft errors.
214   This is a design requirement because the
215   minimization of soft errors can affect the granularity and the nature of the
216   interface. In general, if you find that you're wanting to throw soft errors,
217   you must review the granularity of the interface because it is likely you're
218   trying to implement something that is too low level. The rule of thumb is to
219   provide interface functions that <em>can't</em> fail, except when faced with 
220   hard errors.</p>
221   <p>For a trivial example, suppose we wanted to add an "OpenFileForWriting" 
222   function. For many operating systems, if the file doesn't exist, attempting 
223   to open the file will produce an error.  However, lib/System should not
224   simply throw that error if it occurs because its a soft error. The problem
225   is that the interface function, OpenFileForWriting is too low level. It should
226   be OpenOrCreateFileForWriting. In the case of the soft "doesn't exist" error, 
227   this function would just create it and then open it for writing.</p>
228   <p>This design principle needs to be maintained in lib/System because it
229   avoids the propagation of soft error handling throughout the rest of LLVM.
230   Hard errors will generally just cause a termination for an LLVM tool so don't
231   be bashful about throwing them.</p>
232   <p>Rules of thumb:</p>
233   <ol>
234     <li>Don't throw soft errors, only hard errors.</li>
235     <li>If you're tempted to throw a soft error, re-think the interface.</li>
236     <li>Handle internally the most common normal/good/soft error conditions
237     so the rest of LLVM doesn't have to.</li>
238   </ol>
239 </div>
240
241 <!-- ======================================================================= -->
242 <div class="doc_subsection"><a name="throw_spec">No throw Specifications</a>
243 </div>
244 <div class="doc_text">
245   <p>None of the lib/System interface functions may be declared with C++ 
246   <tt>throw()</tt> specifications on them. This requirement makes sure that the
247   compiler does not insert additional exception handling code into the interface
248   functions. This is a performance consideration: lib/System functions are at
249   the bottom of many call chains and as such can be frequently called. We
250   need them to be as efficient as possible.  However, no routines in the
251   system library should actually throw exceptions.</p>
252 </div>
253
254 <!-- ======================================================================= -->
255 <div class="doc_subsection"><a name="organization">Code Organization</a></div>
256 <div class="doc_text">
257   <p>Implementations of the System Library interface are separated by their
258   general class of operating system. Currently only Unix and Win32 classes are
259   defined but more could be added for other operating system classifications.
260   To distinguish which implementation to compile, the code in lib/System uses
261   the LLVM_ON_UNIX and LLVM_ON_WIN32 #defines provided via configure through the
262   llvm/Config/config.h file. Each source file in lib/System, after implementing
263   the generic (operating system independent) functionality needs to include the
264   correct implementation using a set of <tt>#if defined(LLVM_ON_XYZ)</tt> 
265   directives. For example, if we had lib/System/File.cpp, we'd expect to see in
266   that file:</p>
267   <pre><tt>
268   #if defined(LLVM_ON_UNIX)
269   #include "Unix/File.cpp"
270   #endif
271   #if defined(LLVM_ON_WIN32)
272   #include "Win32/File.cpp"
273   #endif
274   </tt></pre>
275   <p>The implementation in lib/System/Unix/File.cpp should handle all Unix
276   variants. The implementation in lib/System/Win32/File.cpp should handle all
277   Win32 variants.  What this does is quickly differentiate the basic class of 
278   operating system that will provide the implementation. The specific details
279   for a given platform must still be determined through the use of
280   <tt>#ifdef</tt>.</p>
281 </div>
282
283 <!-- ======================================================================= -->
284 <div class="doc_subsection"><a name="semantics">Consistent Semantics</a></div>
285 <div class="doc_text">
286   <p>The implementation of a lib/System interface can vary drastically between
287   platforms. That's okay as long as the end result of the interface function 
288   is the same. For example, a function to create a directory is pretty straight
289   forward on all operating system. System V IPC on the other hand isn't even
290   supported on all platforms. Instead of "supporting" System V IPC, lib/System
291   should provide an interface to the basic concept of inter-process 
292   communications. The implementations might use System V IPC if that was 
293   available or named pipes, or whatever gets the job done effectively for a 
294   given operating system.  In all cases, the interface and the implementation 
295   must be semantically consistent. </p>
296 </div>
297
298 <!-- ======================================================================= -->
299 <div class="doc_subsection"><a name="bug">Bug 351</a></div>
300 <div class="doc_text">
301   <p>See <a href="http://llvm.org/PR351">bug 351</a>
302   for further details on the progress of this work</p>
303 </div>
304
305 <!-- *********************************************************************** -->
306
307 <hr>
308 <address>
309   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
310   src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
311   <a href="http://validator.w3.org/check/referer"><img
312   src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
313
314   <a href="mailto:rspencer@x10sys.com">Reid Spencer</a><br>
315   <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
316   Last modified: $Date$
317 </address>
318 </body>
319 </html>