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