SystemLibrary.rst 12 KB

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