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