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