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