247 lines · plain
1===============2Support Library3===============4 5Abstract6========7 8This document provides some details on LLVM's Support Library, located in the9source at ``lib/Support`` and ``include/llvm/Support``. The library's purpose10is to shield LLVM from the differences between operating systems for the few11services LLVM needs from the operating system. Much of LLVM is written using12portability features of standard C++. However, in a few areas, system dependent13facilities are needed and the Support Library is the wrapper around those14system calls.15 16By centralizing LLVM's use of operating system interfaces, we make it possible17for the LLVM tool chain and runtime libraries to be more easily ported to new18platforms since (theoretically) only ``lib/Support`` needs to be ported. This19library also unclutters the rest of LLVM from #ifdef use and special cases for20specific operating systems. Such uses are replaced with simple calls to the21interfaces provided in ``include/llvm/Support``.22 23Note that the Support Library is not intended to be a complete operating system24wrapper (such as the Adaptive Communications Environment (ACE) or Apache25Portable Runtime (APR)), but only provides the functionality necessary to26support LLVM.27 28The Support Library was originally referred to as the System Library, written29by Reid Spencer who formulated the design based on similar work originating30from the eXtensible Programming System (XPS). Several people helped with the31effort; especially, Jeff Cohen and Henrik Bach on the Win32 port.32 33Keeping LLVM Portable34=====================35 36In order to keep LLVM portable, LLVM developers should adhere to a set of37portability rules associated with the Support Library. Adherence to these rules38should help the Support Library achieve its goal of shielding LLVM from the39variations in operating system interfaces and doing so efficiently. The40following sections define the rules needed to fulfill this objective.41 42Don't Include System Headers43----------------------------44 45Except in ``lib/Support``, no LLVM source code should directly ``#include`` a46system header. Care has been taken to remove all such ``#includes`` from LLVM47while ``lib/Support`` was being developed. Specifically this means that header48files like "``unistd.h``", "``windows.h``", "``stdio.h``", and "``string.h``"49are forbidden to be included by LLVM source code outside the implementation of50``lib/Support``.51 52To obtain system-dependent functionality, existing interfaces to the system53found in ``include/llvm/Support`` should be used. If an appropriate interface is54not available, it should be added to ``include/llvm/Support`` and implemented in55``lib/Support`` for all supported platforms.56 57Don't Expose System Headers58---------------------------59 60The Support Library must shield LLVM from **all** system headers. To obtain61system level functionality, LLVM source must62``#include "llvm/Support/Thing.h"`` and nothing else. This means that63``Thing.h`` cannot expose any system header files. This protects LLVM from64accidentally using system specific functionality and only allows it via65the ``lib/Support`` interface.66 67Use Standard C Headers68----------------------69 70The **standard** C headers (the ones beginning with "c") are allowed to be71exposed through the ``lib/Support`` interface. These headers and the things they72declare are considered to be platform agnostic. LLVM source files may include73them directly or obtain their inclusion through ``lib/Support`` interfaces.74 75Use Standard C++ Headers76------------------------77 78The **standard** C++ headers from the standard C++ library and standard79template library may be exposed through the ``lib/Support`` interface. These80headers and the things they declare are considered to be platform agnostic.81LLVM source files may include them or obtain their inclusion through82``lib/Support`` interfaces.83 84High Level Interface85--------------------86 87The entry points specified in the interface of ``lib/Support`` must be aimed at88completing some reasonably high level task needed by LLVM. We do not want to89simply wrap each operating system call. It would be preferable to wrap several90operating system calls that are always used in conjunction with one another by91LLVM.92 93For example, consider what is needed to execute a program, wait for it to94complete, and return its result code. On Unix, this involves the following95operating system calls: ``getenv``, ``fork``, ``execve``, and ``wait``. The96correct thing for ``lib/Support`` to provide is a function, say97``ExecuteProgramAndWait``, that implements the functionality completely. what98we don't want is wrappers for the operating system calls involved.99 100There must **not** be a one-to-one relationship between operating system101calls and the Support library's interface. Any such interface function will be102suspicious.103 104No Unused Functionality105-----------------------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 operating109system wrapper here, just enough to satisfy LLVM's needs. And, LLVM doesn't110need much. This design goal aims to keep the ``lib/Support`` interface small and111understandable which should foster its actual use and adoption.112 113No Duplicate Implementations114----------------------------115 116The implementation of a function for a given platform must be written exactly117once. This implies that it must be possible to apply a function's118implementation to multiple operating systems if those operating systems can119share the same implementation. This rule applies to the set of operating120systems supported for a given class of operating system (e.g. Unix, Win32).121 122No Virtual Methods123------------------124 125The Support Library interfaces can be called quite frequently by LLVM. In order126to make those calls as efficient as possible, we discourage the use of virtual127methods. There is no need to use inheritance for implementation differences, it128just adds complexity. The ``#include`` mechanism works just fine.129 130No Exposed Functions131--------------------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 header135file for that function is not exposed. This prevents inadvertent use of system136specific functionality.137 138For example, the ``stat`` system call is notorious for having variations in the139data it provides. ``lib/Support`` must not declare ``stat`` nor allow it to be140declared. Instead it should provide its own interface to discovering141information about files and directories. Those interfaces may be implemented in142terms of ``stat`` but that is strictly an implementation detail. The interface143provided by the Support Library must be implemented on all platforms (even144those without ``stat``).145 146No Exposed Data147---------------148 149Any data defined by system libraries (i.e. not defined by ``lib/Support``) must150not be exposed through the ``lib/Support`` interface, even if the header file151for that function is not exposed. As with functions, this prevents inadvertent152use of data that might not exist on all platforms.153 154Minimize Soft Errors155--------------------156 157Operating system interfaces will generally provide error results for every158little thing that could go wrong. In almost all cases, you can divide these159error results into two groups: normal/good/soft and abnormal/bad/hard. That is,160some of the errors are simply information like "file not found", "insufficient161privileges", etc. while other errors are much harder like "out of space", "bad162disk 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 design166requirement because the minimization of soft errors can affect the granularity167and the nature of the interface. In general, if you find that you're wanting to168throw soft errors, you must review the granularity of the interface because it169is likely you're trying to implement something that is too low level. The rule170of thumb is to provide interface functions that **can't** fail, except when171faced 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 to175open the file will produce an error. However, ``lib/Support`` should not simply176throw that error if it occurs because its a soft error. The problem is that the177interface function, ``OpenFileForWriting`` is too low level. It should be178``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 it182avoids 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't184be 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 conditions193 so the rest of LLVM doesn't have to.194 195No throw Specifications196-----------------------197 198None of the ``lib/Support`` interface functions may be declared with C++199``throw()`` specifications on them. This requirement makes sure that the200compiler does not insert additional exception handling code into the interface201functions. This is a performance consideration: ``lib/Support`` functions are202at the bottom of many call chains and as such can be frequently called. We203need them to be as efficient as possible. However, no routines in the system204library should actually throw exceptions.205 206Code Organization207-----------------208 209Implementations of the Support Library interface are separated by their general210class of operating system. Currently only Unix and Win32 classes are defined211but more could be added for other operating system classifications. To212distinguish which implementation to compile, the code in ``lib/Support`` uses213the ``LLVM_ON_UNIX`` and ``_WIN32`` ``#defines``. Each source file in214``lib/Support``, after implementing the generic (operating system independent)215functionality needs to include the correct implementation using a set of216``#if defined(LLVM_ON_XYZ)`` directives. For example, if we had217``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 #endif224 #if defined(_WIN32)225 #include "Windows/Path.inc"226 #endif227 228The implementation in ``lib/Support/Unix/Path.inc`` should handle all Unix229variants. The implementation in ``lib/Support/Windows/Path.inc`` should handle230all Windows variants. What this does is quickly inc the basic class231of operating system that will provide the implementation. The specific details232for a given platform must still be determined through the use of ``#ifdef``.233 234Consistent Semantics235--------------------236 237The implementation of a ``lib/Support`` interface can vary drastically between238platforms. That's okay as long as the end result of the interface function is239the same. For example, a function to create a directory is pretty straight240forward on all operating system. System V IPC on the other hand isn't even241supported on all platforms. Instead of "supporting" System V IPC,242``lib/Support`` should provide an interface to the basic concept of243inter-process communications. The implementations might use System V IPC if244that was available or named pipes, or whatever gets the job done effectively245for a given operating system. In all cases, the interface and the246implementation must be semantically consistent.247