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