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