blob: 614737e9561bf6754d6c7d2ec724d9b823a90e3a [file] [log] [blame]
Reid Spencer4f6015a2004-07-17 10:04:49 +00001<!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
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000010<h1>System Library</h1>
Reid Spencer4f6015a2004-07-17 10:04:49 +000011<ul>
12 <li><a href="#abstract">Abstract</a></li>
Reid Spencer0c004852005-01-05 18:17:10 +000013 <li><a href="#requirements">Keeping LLVM Portable</a>
Reid Spencer4f6015a2004-07-17 10:04:49 +000014 <ol>
Reid Spencer0c004852005-01-05 18:17:10 +000015 <li><a href="#headers">Don't Include System Headers</a></li>
16 <li><a href="#expose">Don't Expose System Headers</a></li>
Reid Spencer7acb8662004-08-27 02:08:04 +000017 <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>
Reid Spencer0c004852005-01-05 18:17:10 +000019 <li><a href="#highlev">High-Level Interface</a></li>
Reid Spencer4f6015a2004-07-17 10:04:49 +000020 <li><a href="#nofunc">No Exposed Functions</a></li>
21 <li><a href="#nodata">No Exposed Data</a></li>
Reid Spencer0c004852005-01-05 18:17:10 +000022 <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>
Reid Spencer7acb8662004-08-27 02:08:04 +000026 <li><a href="#throw_spec">No throw() Specifications</a></li>
Reid Spencer0c004852005-01-05 18:17:10 +000027 <li><a href="#organization">Code Organization</a></li>
28 <li><a href="#semantics">Consistent Semantics</a></li>
Reid Spencer4f6015a2004-07-17 10:04:49 +000029 <li><a href="#bug">Tracking Bugzilla Bug: 351</a></li>
Reid Spencer4f6015a2004-07-17 10:04:49 +000030 </ol></li>
31</ul>
32
33<div class="doc_author">
Duncan Sands8036ca42007-03-30 12:22:09 +000034 <p>Written by <a href="mailto:rspencer@x10sys.com">Reid Spencer</a></p>
Reid Spencer4f6015a2004-07-17 10:04:49 +000035</div>
36
37
38<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000039<h2><a name="abstract">Abstract</a></h2>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000040<div>
Reid Spencer0c004852005-01-05 18:17:10 +000041 <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.
Reid Spencer7acb8662004-08-27 02:08:04 +000059 <p>The System Library was written by Reid Spencer who formulated the
Reid Spencer0c004852005-01-05 18:17:10 +000060 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>
Reid Spencer4f6015a2004-07-17 10:04:49 +000063</div>
64
65<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000066<h2>
Reid Spencer0c004852005-01-05 18:17:10 +000067 <a name="requirements">Keeping LLVM Portable</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000068</h2>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000069<div>
Reid Spencer0c004852005-01-05 18:17:10 +000070 <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>
Reid Spencer4f6015a2004-07-17 10:04:49 +000075
76<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000077<h3><a name="headers">Don't Include System Headers</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000078<div>
Reid Spencer0c004852005-01-05 18:17:10 +000079 <p>Except in <tt>lib/System</tt>, no LLVM source code should directly
80 <tt>#include</tt> a system header. Care has been taken to remove all such
81 <tt>#includes</tt> from LLVM while <tt>lib/System</tt> was being
82 developed. Specifically this means that header files like "unistd.h",
83 "windows.h", "stdio.h", and "string.h" are forbidden to be included by LLVM
84 source code outside the implementation of <tt>lib/System</tt>.</p>
85 <p>To obtain system-dependent functionality, existing interfaces to the system
86 found in <tt>include/llvm/System</tt> should be used. If an appropriate
87 interface is not available, it should be added to <tt>include/llvm/System</tt>
88 and implemented in <tt>lib/System</tt> for all supported platforms.</p>
89</div>
90
91<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000092<h3><a name="expose">Don't Expose System Headers</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000093<div>
Reid Spencer0c004852005-01-05 18:17:10 +000094 <p>The System Library must shield LLVM from <em>all</em> system headers. To
95 obtain system level functionality, LLVM source must
96 <tt>#include "llvm/System/Thing.h"</tt> and nothing else. This means that
97 <tt>Thing.h</tt> cannot expose any system header files. This protects LLVM
98 from accidentally using system specific functionality and only allows it
99 via the <tt>lib/System</tt> interface.</p>
100</div>
101
102<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000103<h3><a name="c_headers">Use Standard C Headers</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000104<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000105 <p>The <em>standard</em> C headers (the ones beginning with "c") are allowed
106 to be exposed through the <tt>lib/System</tt> interface. These headers and
107 the things they declare are considered to be platform agnostic. LLVM source
108 files may include them directly or obtain their inclusion through
109 <tt>lib/System</tt> interfaces.</p>
110</div>
111
112<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000113<h3><a name="cpp_headers">Use Standard C++ Headers</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000114<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000115 <p>The <em>standard</em> C++ headers from the standard C++ library and
116 standard template library may be exposed through the <tt>lib/System</tt>
117 interface. These headers and the things they declare are considered to be
118 platform agnostic. LLVM source files may include them or obtain their
119 inclusion through lib/System interfaces.</p>
120</div>
121
122<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000123<h3><a name="highlev">High Level Interface</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000124<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000125 <p>The entry points specified in the interface of lib/System must be aimed at
126 completing some reasonably high level task needed by LLVM. We do not want to
127 simply wrap each operating system call. It would be preferable to wrap several
128 operating system calls that are always used in conjunction with one another by
129 LLVM.</p>
130 <p>For example, consider what is needed to execute a program, wait for it to
131 complete, and return its result code. On Unix, this involves the following
132 operating system calls: <tt>getenv, fork, execve,</tt> and <tt>wait</tt>. The
133 correct thing for lib/System to provide is a function, say
134 <tt>ExecuteProgramAndWait</tt>, that implements the functionality completely.
135 what we don't want is wrappers for the operating system calls involved.</p>
136 <p>There must <em>not</em> be a one-to-one relationship between operating
137 system calls and the System library's interface. Any such interface function
138 will be suspicious.</p>
139</div>
140
141<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000142<h3><a name="nounused">No Unused Functionality</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000143<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000144 <p>There must be no functionality specified in the interface of lib/System
145 that isn't actually used by LLVM. We're not writing a general purpose
146 operating system wrapper here, just enough to satisfy LLVM's needs. And, LLVM
147 doesn't need much. This design goal aims to keep the lib/System interface
148 small and understandable which should foster its actual use and adoption.</p>
149</div>
150
151<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000152<h3><a name="nodupl">No Duplicate Implementations</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000153<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000154 <p>The implementation of a function for a given platform must be written
155 exactly once. This implies that it must be possible to apply a function's
156 implementation to multiple operating systems if those operating systems can
157 share the same implementation. This rule applies to the set of operating
158 systems supported for a given class of operating system (e.g. Unix, Win32).
Reid Spencer7acb8662004-08-27 02:08:04 +0000159 </p>
160</div>
161
162<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000163<h3><a name="virtuals">No Virtual Methods</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000164<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000165 <p>The System Library interfaces can be called quite frequently by LLVM. In
166 order to make those calls as efficient as possible, we discourage the use of
167 virtual methods. There is no need to use inheritance for implementation
168 differences, it just adds complexity. The <tt>#include</tt> mechanism works
169 just fine.</p>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000170</div>
171
172<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000173<h3><a name="nofunc">No Exposed Functions</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000174<div>
Reid Spencer7acb8662004-08-27 02:08:04 +0000175 <p>Any functions defined by system libraries (i.e. not defined by lib/System)
176 must not be exposed through the lib/System interface, even if the header file
177 for that function is not exposed. This prevents inadvertent use of system
178 specific functionality.</p>
179 <p>For example, the <tt>stat</tt> system call is notorious for having
Reid Spencer0c004852005-01-05 18:17:10 +0000180 variations in the data it provides. <tt>lib/System</tt> must not declare
181 <tt>stat</tt> nor allow it to be declared. Instead it should provide its own
182 interface to discovering information about files and directories. Those
183 interfaces may be implemented in terms of <tt>stat</tt> but that is strictly
184 an implementation detail. The interface provided by the System Library must
185 be implemented on all platforms (even those without <tt>stat</tt>).</p>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000186</div>
187
188<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000189<h3><a name="nodata">No Exposed Data</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000190<div>
Reid Spencer7acb8662004-08-27 02:08:04 +0000191 <p>Any data defined by system libraries (i.e. not defined by lib/System) must
192 not be exposed through the lib/System interface, even if the header file for
193 that function is not exposed. As with functions, this prevents inadvertent use
194 of data that might not exist on all platforms.</p>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000195</div>
196
197<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000198<h3><a name="softerrors">Minimize Soft Errors</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000199<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000200 <p>Operating system interfaces will generally provide error results for every
201 little thing that could go wrong. In almost all cases, you can divide these
202 error results into two groups: normal/good/soft and abnormal/bad/hard. That
203 is, some of the errors are simply information like "file not found",
204 "insufficient privileges", etc. while other errors are much harder like
205 "out of space", "bad disk sector", or "system call interrupted". We'll call
206 the first group "<i>soft</i>" errors and the second group "<i>hard</i>"
207 errors.<p>
Chris Lattner3ddd7172009-07-17 21:11:24 +0000208 <p>lib/System must always attempt to minimize soft errors.
209 This is a design requirement because the
Reid Spencer0c004852005-01-05 18:17:10 +0000210 minimization of soft errors can affect the granularity and the nature of the
211 interface. In general, if you find that you're wanting to throw soft errors,
212 you must review the granularity of the interface because it is likely you're
213 trying to implement something that is too low level. The rule of thumb is to
214 provide interface functions that <em>can't</em> fail, except when faced with
215 hard errors.</p>
216 <p>For a trivial example, suppose we wanted to add an "OpenFileForWriting"
217 function. For many operating systems, if the file doesn't exist, attempting
218 to open the file will produce an error. However, lib/System should not
219 simply throw that error if it occurs because its a soft error. The problem
220 is that the interface function, OpenFileForWriting is too low level. It should
221 be OpenOrCreateFileForWriting. In the case of the soft "doesn't exist" error,
222 this function would just create it and then open it for writing.</p>
223 <p>This design principle needs to be maintained in lib/System because it
224 avoids the propagation of soft error handling throughout the rest of LLVM.
225 Hard errors will generally just cause a termination for an LLVM tool so don't
226 be bashful about throwing them.</p>
227 <p>Rules of thumb:</p>
228 <ol>
229 <li>Don't throw soft errors, only hard errors.</li>
230 <li>If you're tempted to throw a soft error, re-think the interface.</li>
231 <li>Handle internally the most common normal/good/soft error conditions
232 so the rest of LLVM doesn't have to.</li>
233 </ol>
234</div>
235
236<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000237<h3><a name="throw_spec">No throw Specifications</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000238<div>
Reid Spencer7acb8662004-08-27 02:08:04 +0000239 <p>None of the lib/System interface functions may be declared with C++
240 <tt>throw()</tt> specifications on them. This requirement makes sure that the
John Criswell98d06362004-11-21 14:58:12 +0000241 compiler does not insert additional exception handling code into the interface
Reid Spencer7acb8662004-08-27 02:08:04 +0000242 functions. This is a performance consideration: lib/System functions are at
Reid Spencer0c004852005-01-05 18:17:10 +0000243 the bottom of many call chains and as such can be frequently called. We
Chris Lattner3ddd7172009-07-17 21:11:24 +0000244 need them to be as efficient as possible. However, no routines in the
245 system library should actually throw exceptions.</p>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000246</div>
247
248<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000249<h3><a name="organization">Code Organization</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000250<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000251 <p>Implementations of the System Library interface are separated by their
252 general class of operating system. Currently only Unix and Win32 classes are
253 defined but more could be added for other operating system classifications.
254 To distinguish which implementation to compile, the code in lib/System uses
255 the LLVM_ON_UNIX and LLVM_ON_WIN32 #defines provided via configure through the
256 llvm/Config/config.h file. Each source file in lib/System, after implementing
257 the generic (operating system independent) functionality needs to include the
258 correct implementation using a set of <tt>#if defined(LLVM_ON_XYZ)</tt>
259 directives. For example, if we had lib/System/File.cpp, we'd expect to see in
260 that file:</p>
261 <pre><tt>
262 #if defined(LLVM_ON_UNIX)
263 #include "Unix/File.cpp"
264 #endif
265 #if defined(LLVM_ON_WIN32)
266 #include "Win32/File.cpp"
267 #endif
268 </tt></pre>
269 <p>The implementation in lib/System/Unix/File.cpp should handle all Unix
270 variants. The implementation in lib/System/Win32/File.cpp should handle all
271 Win32 variants. What this does is quickly differentiate the basic class of
272 operating system that will provide the implementation. The specific details
273 for a given platform must still be determined through the use of
274 <tt>#ifdef</tt>.</p>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000275</div>
276
277<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000278<h3><a name="semantics">Consistent Semantics</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000279<div>
Reid Spencer0c004852005-01-05 18:17:10 +0000280 <p>The implementation of a lib/System interface can vary drastically between
281 platforms. That's okay as long as the end result of the interface function
282 is the same. For example, a function to create a directory is pretty straight
283 forward on all operating system. System V IPC on the other hand isn't even
284 supported on all platforms. Instead of "supporting" System V IPC, lib/System
285 should provide an interface to the basic concept of inter-process
286 communications. The implementations might use System V IPC if that was
287 available or named pipes, or whatever gets the job done effectively for a
288 given operating system. In all cases, the interface and the implementation
289 must be semantically consistent. </p>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000290</div>
291
292<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000293<h3><a name="bug">Bug 351</a></h3>
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000294<div>
Reid Spencer05fe4b02006-03-14 05:39:39 +0000295 <p>See <a href="http://llvm.org/PR351">bug 351</a>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000296 for further details on the progress of this work</p>
297</div>
298
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000299</div>
300
Reid Spencer4f6015a2004-07-17 10:04:49 +0000301<!-- *********************************************************************** -->
302
303<hr>
304<address>
305 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000306 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000307 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000308 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000309
310 <a href="mailto:rspencer@x10sys.com">Reid Spencer</a><br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000311 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Reid Spencer4f6015a2004-07-17 10:04:49 +0000312 Last modified: $Date$
313</address>
314</body>
315</html>