llvm.org GIT mirror llvm / fee64ee
Documentation: convert SystemLibrary documentation to reST git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168289 91177308-0d34-0410-b5e6-96231b3b80d8 Dmitri Gribenko 6 years ago
3 changed file(s) with 258 addition(s) and 318 deletion(s). Raw diff Collapse all Expand all
+0
-316
docs/SystemLibrary.html less more
None
1 "http://www.w3.org/TR/html4/strict.dtd">
2
3
4
5 System Library
6
7
8
9
10

System Library

11
12
  • Abstract
  • 13
  • Keeping LLVM Portable
  • 14
    15
  • Don't Include System Headers
  • 16
  • Don't Expose System Headers
  • 17
  • Allow Standard C Header Files
  • 18
  • Allow Standard C++ Header Files
  • 19
  • High-Level Interface
  • 20
  • No Exposed Functions
  • 21
  • No Exposed Data
  • 22
  • No Duplicate Implementations
  • 23
  • No Unused Functionality
  • 24
  • No Virtual Methods
  • 25
  • Minimize Soft Errors
  • 26
  • No throw() Specifications
  • 27
  • Code Organization
  • 28
  • Consistent Semantics
  • 29
  • Tracking Bugzilla Bug: 351
  • 30
    31
    32
    33
    34

    Written by Reid Spencer

    35
    36
    37
    38
    39

    Abstract

    40
    41

    This document provides some details on LLVM's System Library, located in

    42 the source at lib/System and include/llvm/System. The
    43 library's purpose is to shield LLVM from the differences between operating
    44 systems for the few services LLVM needs from the operating system. Much of
    45 LLVM is written using portability features of standard C++. However, in a few
    46 areas, system dependent facilities are needed and the System Library is the
    47 wrapper around those system calls.

    48

    By centralizing LLVM's use of operating system interfaces, we make it

    49 possible for the LLVM tool chain and runtime libraries to be more easily
    50 ported to new platforms since (theoretically) only lib/System needs
    51 to be ported. This library also unclutters the rest of LLVM from #ifdef use
    52 and special cases for specific operating systems. Such uses are replaced
    53 with simple calls to the interfaces provided in include/llvm/System.
    54

    55

    Note that the System Library is not intended to be a complete operating

    56 system wrapper (such as the Adaptive Communications Environment (ACE) or
    57 Apache Portable Runtime (APR)), but only provides the functionality necessary
    58 to support LLVM.
    59

    The System Library was written by Reid Spencer who formulated the

    60 design based on similar work originating from the eXtensible Programming
    61 System (XPS). Several people helped with the effort; especially,
    62 Jeff Cohen and Henrik Bach on the Win32 port.

    63
    64
    65
    66

    67 Keeping LLVM Portable
    68
    69
    70

    In order to keep LLVM portable, LLVM developers should adhere to a set of

    71 portability rules associated with the System Library. Adherence to these rules
    72 should help the System Library achieve its goal of shielding LLVM from the
    73 variations in operating system interfaces and doing so efficiently. The
    74 following sections define the rules needed to fulfill this objective.

    75
    76
    77

    Don't Include System Headers

    78
    79

    Except in lib/System, no LLVM source code should directly

    80 #include a system header. Care has been taken to remove all such
    81 #includes from LLVM while lib/System was being
    82 developed. Specifically this means that header files like "unistd.h",
    83 "windows.h", "stdio.h", and "string.h" are forbidden to be included by LLVM
    84 source code outside the implementation of lib/System.

    85

    To obtain system-dependent functionality, existing interfaces to the system

    86 found in include/llvm/System should be used. If an appropriate
    87 interface is not available, it should be added to include/llvm/System
    88 and implemented in lib/System for all supported platforms.

    89
    90
    91
    92

    Don't Expose System Headers

    93
    94

    The System Library must shield LLVM from all system headers. To

    95 obtain system level functionality, LLVM source must
    96 #include "llvm/System/Thing.h" and nothing else. This means that
    97 Thing.h cannot expose any system header files. This protects LLVM
    98 from accidentally using system specific functionality and only allows it
    99 via the lib/System interface.

    100
    101
    102
    103

    Use Standard C Headers

    104
    105

    The standard C headers (the ones beginning with "c") are allowed

    106 to be exposed through the lib/System interface. These headers and
    107 the things they declare are considered to be platform agnostic. LLVM source
    108 files may include them directly or obtain their inclusion through
    109 lib/System interfaces.

    110
    111
    112
    113

    Use Standard C++ Headers

    114
    115

    The standard C++ headers from the standard C++ library and

    116 standard template library may be exposed through the lib/System
    117 interface. These headers and the things they declare are considered to be
    118 platform agnostic. LLVM source files may include them or obtain their
    119 inclusion through lib/System interfaces.

    120
    121
    122
    123

    High Level Interface

    124
    125

    The entry points specified in the interface of lib/System must be aimed at

    126 completing some reasonably high level task needed by LLVM. We do not want to
    127 simply wrap each operating system call. It would be preferable to wrap several
    128 operating system calls that are always used in conjunction with one another by
    129 LLVM.

    130

    For example, consider what is needed to execute a program, wait for it to

    131 complete, and return its result code. On Unix, this involves the following
    132 operating system calls: getenv, fork, execve, and wait. The
    133 correct thing for lib/System to provide is a function, say
    134 ExecuteProgramAndWait, that implements the functionality completely.
    135 what we don't want is wrappers for the operating system calls involved.

    136

    There must not be a one-to-one relationship between operating

    137 system calls and the System library's interface. Any such interface function
    138 will be suspicious.

    139
    140
    141
    142

    No Unused Functionality

    143
    144

    There must be no functionality specified in the interface of lib/System

    145 that isn't actually used by LLVM. We're not writing a general purpose
    146 operating system wrapper here, just enough to satisfy LLVM's needs. And, LLVM
    147 doesn't need much. This design goal aims to keep the lib/System interface
    148 small and understandable which should foster its actual use and adoption.

    149
    150
    151
    152

    No Duplicate Implementations

    153
    154

    The implementation of a function for a given platform must be written

    155 exactly once. This implies that it must be possible to apply a function's
    156 implementation to multiple operating systems if those operating systems can
    157 share the same implementation. This rule applies to the set of operating
    158 systems supported for a given class of operating system (e.g. Unix, Win32).
    159

    160
    161
    162
    163

    No Virtual Methods

    164
    165

    The System Library interfaces can be called quite frequently by LLVM. In

    166 order to make those calls as efficient as possible, we discourage the use of
    167 virtual methods. There is no need to use inheritance for implementation
    168 differences, it just adds complexity. The #include mechanism works
    169 just fine.

    170
    171
    172
    173

    No Exposed Functions

    174
    175

    Any functions defined by system libraries (i.e. not defined by lib/System)

    176 must not be exposed through the lib/System interface, even if the header file
    177 for that function is not exposed. This prevents inadvertent use of system
    178 specific functionality.

    179

    For example, the stat system call is notorious for having

    180 variations in the data it provides. lib/System must not declare
    181 stat nor allow it to be declared. Instead it should provide its own
    182 interface to discovering information about files and directories. Those
    183 interfaces may be implemented in terms of stat but that is strictly
    184 an implementation detail. The interface provided by the System Library must
    185 be implemented on all platforms (even those without stat).

    186
    187
    188
    189

    No Exposed Data

    190
    191

    Any data defined by system libraries (i.e. not defined by lib/System) must

    192 not be exposed through the lib/System interface, even if the header file for
    193 that function is not exposed. As with functions, this prevents inadvertent use
    194 of data that might not exist on all platforms.

    195
    196
    197
    198

    Minimize Soft Errors

    199
    200

    Operating system interfaces will generally provide error results for every

    201 little thing that could go wrong. In almost all cases, you can divide these
    202 error results into two groups: normal/good/soft and abnormal/bad/hard. That
    203 is, some of the errors are simply information like "file not found",
    204 "insufficient privileges", etc. while other errors are much harder like
    205 "out of space", "bad disk sector", or "system call interrupted". We'll call
    206 the first group "soft" errors and the second group "hard"
    207 errors.

    208

    lib/System must always attempt to minimize soft errors.

    209 This is a design requirement because the
    210 minimization of soft errors can affect the granularity and the nature of the
    211 interface. In general, if you find that you're wanting to throw soft errors,
    212 you must review the granularity of the interface because it is likely you're
    213 trying to implement something that is too low level. The rule of thumb is to
    214 provide interface functions that can't fail, except when faced with
    215 hard errors.

    216

    For a trivial example, suppose we wanted to add an "OpenFileForWriting"

    217 function. For many operating systems, if the file doesn't exist, attempting
    218 to open the file will produce an error. However, lib/System should not
    219 simply throw that error if it occurs because its a soft error. The problem
    220 is that the interface function, OpenFileForWriting is too low level. It should
    221 be OpenOrCreateFileForWriting. In the case of the soft "doesn't exist" error,
    222 this function would just create it and then open it for writing.

    223

    This design principle needs to be maintained in lib/System because it

    224 avoids the propagation of soft error handling throughout the rest of LLVM.
    225 Hard errors will generally just cause a termination for an LLVM tool so don't
    226 be bashful about throwing them.

    227

    Rules of thumb:

    228
    229
  • Don't throw soft errors, only hard errors.
  • 230
  • If you're tempted to throw a soft error, re-think the interface.
  • 231
  • Handle internally the most common normal/good/soft error conditions
  • 232 so the rest of LLVM doesn't have to.
    233
    234
    235
    236
    237

    No throw Specifications

    238
    239

    None of the lib/System interface functions may be declared with C++

    240 throw() specifications on them. This requirement makes sure that the
    241 compiler does not insert additional exception handling code into the interface
    242 functions. This is a performance consideration: lib/System functions are at
    243 the bottom of many call chains and as such can be frequently called. We
    244 need them to be as efficient as possible. However, no routines in the
    245 system library should actually throw exceptions.

    246
    247
    248
    249

    Code Organization

    250
    251

    Implementations of the System Library interface are separated by their

    252 general class of operating system. Currently only Unix and Win32 classes are
    253 defined but more could be added for other operating system classifications.
    254 To distinguish which implementation to compile, the code in lib/System uses
    255 the LLVM_ON_UNIX and LLVM_ON_WIN32 #defines provided via configure through the
    256 llvm/Config/config.h file. Each source file in lib/System, after implementing
    257 the generic (operating system independent) functionality needs to include the
    258 correct implementation using a set of #if defined(LLVM_ON_XYZ)
    259 directives. For example, if we had lib/System/File.cpp, we'd expect to see in
    260 that file:

    261
    
    
                      
                    
    262 #if defined(LLVM_ON_UNIX)
    263 #include "Unix/File.cpp"
    264 #endif
    265 #if defined(LLVM_ON_WIN32)
    266 #include "Win32/File.cpp"
    267 #endif
    268
    269

    The implementation in lib/System/Unix/File.cpp should handle all Unix

    270 variants. The implementation in lib/System/Win32/File.cpp should handle all
    271 Win32 variants. What this does is quickly differentiate the basic class of
    272 operating system that will provide the implementation. The specific details
    273 for a given platform must still be determined through the use of
    274 #ifdef.

    275
    276
    277
    278

    Consistent Semantics

    279
    280

    The implementation of a lib/System interface can vary drastically between

    281 platforms. That's okay as long as the end result of the interface function
    282 is the same. For example, a function to create a directory is pretty straight
    283 forward on all operating system. System V IPC on the other hand isn't even
    284 supported on all platforms. Instead of "supporting" System V IPC, lib/System
    285 should provide an interface to the basic concept of inter-process
    286 communications. The implementations might use System V IPC if that was
    287 available or named pipes, or whatever gets the job done effectively for a
    288 given operating system. In all cases, the interface and the implementation
    289 must be semantically consistent.

    290
    291
    292
    293

    Bug 351

    294
    295

    See bug 351

    296 for further details on the progress of this work

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