[llvm-commits] CVS: llvm/docs/SystemLibrary.html

Reid Spencer reid at x10sys.com
Thu Aug 26 19:08:14 PDT 2004

Changes in directory llvm/docs:

SystemLibrary.html updated: 1.5 -> 1.6
Log message:

Add more content.

This isn't in reviewable shape yet, unless you're curious.

Diffs of the changes:  (+221 -28)

Index: llvm/docs/SystemLibrary.html
diff -u llvm/docs/SystemLibrary.html:1.5 llvm/docs/SystemLibrary.html:1.6
--- llvm/docs/SystemLibrary.html:1.5	Thu Aug 26 13:52:52 2004
+++ llvm/docs/SystemLibrary.html	Thu Aug 26 21:08:04 2004
@@ -18,14 +18,18 @@
   <li><a href="#requirements">System Library Requirements</a>
     <li><a href="#headers">Hide System Header Files</a></li>
+    <li><a href="#c_headers">Allow Standard C Header Files</a></li>
+    <li><a href="#cpp_headers">Allow Standard C++ Header Files</a></li>
     <li><a href="#nofunc">No Exposed Functions</a></li>
     <li><a href="#nodata">No Exposed Data</a></li>
-    <li><a href="#xcptns">No Exceptions</a></li>
-    <li><a href="#errors">Standard Error Codes</a></li>
-    <li><a href="#overhead">Minimize Overhead</a></li>
+    <li><a href="#throw">Throw Only std::string</a></li>
+    <li><a href="#throw_spec">No throw() Specifications</a></li>
+    <li><a href="#nodupl">No Duplicate Impementations</a></li>
   <li><a href="#design">System Library Design</a>
+    <li><a href="#nounused">No Unused Functionality</a></li>
+    <li><a href="#highlev">High-Level Interface</a></li>
     <li><a href="#opaque">Use Opaque Classes</a></li>
     <li><a href="#common">Common Implementations</a></li>
     <li><a href="#multi_imps">Multiple Implementations</a></li>
@@ -54,12 +58,17 @@
   <tt>llvm/lib/System</tt>. The goal of this library is to completely shield 
   LLVM from the variations in operating system interfaces. By centralizing 
   LLVM's use of operating system interfaces, we make it possible for the LLVM
-  tool chain and runtime libraries to be more easily ported to new platforms.
-  The library also unclutters the rest of LLVM from #ifdef use and special
-  cases for specific operating systems.</p>
-  <p>The System Library was donated to LLVM by Reid Spencer who formulated the
-  original design as part of the eXtensible Programming System (XPS) which is
-  based, in part, on LLVM.</p>
+  tool chain and runtime libraries to be more easily ported to new platforms
+  since (theoretically) only <tt>llvm/lib/System</tt> needs to be ported.  This
+  library also unclutters the rest of LLVM from #ifdef use and special
+  cases for specific operating systems. Such uses are replaced with simple calls
+  to the interfaces provided in <tt>llvm/include/llvm/System</tt>.</p> Note that
+  lib/System is not intended to be a complete operating system wrapper (such as
+  the Adaptive Communications Environment (ACE) or Apache Portable Runtime
+  (APR)), but only to provide the functionality necessary to support LLVM.
+  <p>The System Library was written by Reid Spencer who formulated the
+  design based on similar original work as part of the eXtensible Programming 
+  System (XPS).</p>
 <!-- *********************************************************************** -->
@@ -69,43 +78,113 @@
 <div class="doc_text">
   <p>The System library's requirements are aimed at shielding LLVM from the
   variations in operating system interfaces. The following sections define the
-  requirements needed to fulfill this objective.</p>
+  requirements needed to fulfill this objective. Of necessity, these requirements 
+  must be strictly followed in order to ensure the library's goal is reached.</p>
 <!-- ======================================================================= -->
 <div class="doc_subsection"><a name="headers">Hide System Header Files</a></div>
 <div class="doc_text">
-  <p>To be written.</p>
+  <p>The library must sheild LLVM from <em>all</em> system libraries. To obtain
+  system level functionality, LLVM must <tt>#include "llvm/System/Thing.h"</tt>
+  and nothing else. This means that <tt>Thing.h</tt> cannot expose any system
+  header files. This protects LLVM from accidentally using system specific
+  functionality except through the lib/System interface.  Specifically this 
+  means that header files like "unistd.h", "windows.h", "stdio.h", and 
+  "string.h" are verbotten outside the implementation of lib/System.
+  </p>
+<!-- ======================================================================= -->
+<div class="doc_subsection"><a name="c_headers">Allow Standard C Headers</a>
+<div class="doc_text">
+  <p>The <em>standard</em> C headers (the ones beginning with "c") are allowed
+  to be exposed through the lib/System interface. These headers and the things
+  they declare are considered to be platform agnostic. LLVM source files may
+  include them or obtain their inclusion through lib/System interfaces.</p>
+<!-- ======================================================================= -->
+<div class="doc_subsection"><a name="cpp_headers">Allow Standard C++ Headers</a>
+<div class="doc_text">
+  <p>The <em>standard</em> C++ headers from the standard C++ library and
+  standard template library are allowed to be exposed through the lib/System
+  interface. These headers and the things they declare are considered to be
+  platform agnostic. LLVM source files may include them or obtain their
+  inclusion through lib/System interfaces.</p>
 <!-- ======================================================================= -->
 <div class="doc_subsection"><a name="nofunc">No Exposed Functions</a></div>
 <div class="doc_text">
-  <p>To be written.</p>
+  <p>Any functions defined by system libraries (i.e. not defined by lib/System) 
+  must not be exposed through the lib/System interface, even if the header file 
+  for that function is not exposed. This prevents inadvertent use of system
+  specific functionality.</p>
+  <p>For example, the <tt>stat</tt> system call is notorious for having
+  variations in the data it provides. lib/System must not declare <tt>stat</tt>
+  nor allow it to be declared. Instead it should provide its own interface to
+  discovering information about files and directories. Those interfaces may be
+  implemented in terms of <tt>stat</tt> but that is strictly an implementation
+  detail.</p>
 <!-- ======================================================================= -->
 <div class="doc_subsection"><a name="nodata">No Exposed Data</a></div>
 <div class="doc_text">
-  <p>To be written.</p>
+  <p>Any data defined by system libraries (i.e. not defined by lib/System) must
+  not be exposed through the lib/System interface, even if the header file for
+  that function is not exposed. As with functions, this prevents inadvertent use
+  of data that might not exist on all platforms.</p>
 <!-- ======================================================================= -->
-<div class="doc_subsection"><a name="xcptns">No Exceptions</a></div>
+<div class="doc_subsection"><a name="throw">Throw Only std::string</a></div>
 <div class="doc_text">
-  <p>To be written.</p>
+  <p>If an error occurs that lib/System cannot handle, the only action taken by
+  lib/System is to throw an instance of std:string. The contents of the string
+  must explain both what happened and the context in which it happened. The
+  format of the string should be a (possibly empty) list of contexts each 
+  terminated with a : and a space, followed by the error message, optionally
+  followed by a reason, and optionally followed by a suggestion.</p>
+  <p>For example, failure to open a file named "foo" could result in a message
+  like:</p>
+  <ul><li>foo: Unable to open file because it doesn't exist."</li></ul>
+  <p>The "foo:" part is the context. The "Unable to open file" part is the error
+  message. The "because it doesn't exist." part is the reason. This message has
+  no suggestion. Where possible, the imlementation of lib/System should use
+  operating system specific facilities for converting the error code returned by
+  a system call into an error message. This will help to make the error message
+  more familiar to users of that type of operating system.</p>
+  <p>Note that this requirement precludes the throwing of any other exceptions.
+  For example, various C++ standard library functions can cause exceptions to be
+  thrown (e.g. out of memory situation). In all cases, if there is a possibility
+  that non-string exceptions could be thrown, the lib/System library must ensure
+  that the exceptions are translated to std::string form.</p>
 <!-- ======================================================================= -->
-<div class="doc_subsection"><a name="errors">Standard Error Codes</a></div>
+<div class="doc_subsection"><a name="throw_spec">No throw Specifications</a>
 <div class="doc_text">
-  <p>To be written.</p>
+  <p>None of the lib/System interface functions may be declared with C++ 
+  <tt>throw()</tt> specifications on them. This requirement makes sure that the
+  compler does not insert addtional exception handling code into the interface
+  functions. This is a performance consideration: lib/System functions are at
+  the bottom of the many call chains and as such can be frequently called. We
+  need them to be as efficient as possible.</p>
 <!-- ======================================================================= -->
-<div class="doc_subsection"><a name="overhead">Minimize Overhead</a></div>
+<div class="doc_subsection"><a name="nodupl">No Duplicate Implementations</a>
 <div class="doc_text">
-  <p>To be written.</p>
+  <p>The implementation of a function for a given platform must be written
+  exactly once. This implies that it must be possible to apply a function's 
+  implementation to multiple operating systems if those operating systems can
+  share the same implementation.</p>
 <!-- *********************************************************************** -->
@@ -119,6 +198,128 @@
 <!-- ======================================================================= -->
+<div class="doc_subsection"><a name="nounused">No Unused Functionality</a></div>
+<div class="doc_text">
+  <p>There must be no functionality specified in the interface of lib/System 
+  that isn't actually used by LLVM. We're not writing a general purpose
+  operating system wrapper here, just enough to satisfy LLVM's needs. And, LLVM
+  doesn't need much. This design goal aims to keep the lib/System interface
+  small and understandable which should foster its actual use and adoption.</p>
+<!-- ======================================================================= -->
+<div class="doc_subsection"><a name="highlev">High Level Interface</a></div>
+<div class="doc_text">
+  <p>The entry points specified in the interface of lib/System must be aimed at 
+  completing some reasonably high level task needed by LLVM. We do not want to
+  simply wrap each operating system call. It would be preferable to wrap several
+  operating system calls that are always used in conjunction with one another by
+  LLVM.</p>
+  <p>For example, consider what is needed to execute a program, wait for it to
+  complete, and return its result code. On Unix, this involves the following
+  operating system calls: <tt>getenv, fork, execve,</tt> and <tt>wait</tt>. The
+  correct thing for lib/System to provide is a function, say
+  <tt>ExecuteProgramAndWait</tt>, that implements the functionality completely.
+  what we don't want is wrappers for the operating system calls involved.</p>
+  <p>There must <em>not</em> be a one-to-one relationship between operating
+  system calls and the System library's interface. Any such interface function
+  will be suspicious.</p>
+<!-- ======================================================================= -->
+<div class="doc_subsection"><a name="highlev">Minimize Soft Errors</a></div>
+<div class="doc_text">
+  <p>Operating system interfaces will generally provide errors results for every
+  little thing that could go wrong. In almost all cases, you can divide these
+  error results into two groups: normal/good/soft and abnormal/bad/hard. That
+  is, some of the errors are simply information like "file not found", 
+  "insufficient privileges", etc. while other errors are much harder like
+  "out of space", "bad disk sector", or "system call interrupted". Well call the
+  first group "soft" errors and the second group "hard" errors.<p>
+  <p>lib/System must always attempt to minimize soft errors and always just
+  throw a std::string on hard errors. This is a design requirement because the
+  minimization of soft errors can affect the granularity and the nature of the
+  interface. In general, if you find that you're wanting to throw soft errors,
+  you must review the granularity of the interface because it is likely you're
+  trying to implement something that is too low level. The rule of thumb is to
+  provide interface functions that "can't" fail, except when faced with hard
+  errors.</p>
+  <p>For a trivial example, suppose we wanted to add an "OpenFileForWriting" 
+  function. For many operating systems, if the file doesn't exist, attempting 
+  to open the file will produce an error.  However, lib/System should not
+  simply throw that error if it occurs because its a soft error. The problem
+  is that the interface function, OpenFileForWriting is too low level. It should
+  be OpenOrCreateFileForWriting. In the case of the soft "doesn't exist" error, 
+  this function would just create it and then open it for writing.</p>
+  <p>This design principle needs to be maintained in lib/System because it
+  avoids the propagation of soft error handling throughout the rest of LLVM.
+  Hard errors will generally just cause a termination for an LLVM tool so don't
+  be bashful about throwing them.</p>
+  <p>Rules of thumb:</p>
+  <ol>
+    <li>Don't throw soft errors, only hard errors.</li>
+    <li>If you're tempted to throw a soft error, re-think the interface.</li>
+    <li>Handle internally the most common normal/good/soft error conditions
+    so the rest of LLVM doesn't have to.</li>
+  </ol>
+10. The implementation of a lib/System interface can vary drastically between
+    platforms. That's okay as long as the end result of the interface function is
+    the same. For example, a function to create a directory is pretty straight
+    forward on all operating system. System V IPC on the other hand isn't even
+    supported on all platforms. Instead of "supporting" System V IPC, lib/System
+    should provide an interface to the basic concept of inter-process 
+    communications. The implementations might use System V IPC if that was
+    available or named pipes, or whatever gets the job done effectively for a
+    given operating system.
+11. Implementations are separated first by the general class of operating system
+    as provided by the configure script's $build variable. This variable is used
+    to create a link from $BUILD_OBJ_ROOT/lib/System/platform to a directory in
+    $BUILD_SRC_ROOT/lib/System directory with the same name as the $build
+    variable. This provides a retargetable include mechanism. By using the link's
+    name (platform) we can actually include the operating specific
+    implementation. For example, support $build is "Darwin" for MacOS X. If we
+    place:
+      #include "platform/File.cpp"
+    into a a file in lib/System, it will actually include
+    lib/System/Darwin/File.cpp. What this does is quickly differentiate the basic
+    class of operating system that will provide the implementation.
+12. Implementation files in lib/System need may only do two things: (1) define 
+    functions and data that is *TRULY* generic (completely platform agnostic) and
+    (2) #include the platform specific implementation with:
+       #include "platform/Impl.cpp"
+    where Impl is the name of the implementation files.
+13. Platform specific implementation files (platform/Impl.cpp) may only #include
+    other Impl.cpp files found in directories under lib/System. The order of
+    inclusion is very important (from most generic to most specific) so that we
+    don't inadvertently place an implementation in the wrong place. For example,
+    consider a fictitious implementation file named DoIt.cpp. Here's how the
+    #includes should work for a Linux platform
+    lib/System/DoIt.cpp
+      #include "platform/DoIt.cpp"        // platform specific impl. of Doit
+      DoIt
+    lib/System/Linux/DoIt.cpp             // impl that works on all Linux 
+      #include "../Unix/DoIt.cpp"         // generic Unix impl. of DoIt
+      #include "../Unix/SUS/DoIt.cpp      // SUS specific impl. of DoIt
+      #include "../Unix/SUS/v3/DoIt.cpp   // SUSv3 specific impl. of DoIt
+    Note that the #includes in lib/System/Linux/DoIt.cpp are all optional but
+    should be used where the implementation of some functionality can be shared
+    across some set of Unix variants. We don't want to duplicate code across
+    variants if their implementation could be shared.
+<!-- ======================================================================= -->
 <div class="doc_subsection"><a name="opaque">Use Opaque Classes</a></div>
 <div class="doc_text">
   <p>no public data</p>
@@ -142,14 +343,6 @@
 <!-- ======================================================================= -->
-<div class="doc_subsection">
-  <a name="low_level">Use Low Level Interfaces</a>
-<div class="doc_text">
-  <p>To be written.</p>
-<!-- ======================================================================= -->
 <div class="doc_subsection"><a name="memalloc">No Memory Allocation</a></div>
 <div class="doc_text">
   <p>To be written.</p>
@@ -309,7 +502,7 @@
   <a href="mailto:rspencer at x10sys.com">Reid Spencer</a><br>
   <a href="http://llvm.cs.uiuc.edu">LLVM Compiler Infrastructure</a><br>
-  Last modified: $Date: 2004/08/26 18:52:52 $
+  Last modified: $Date: 2004/08/27 02:08:04 $

More information about the llvm-commits mailing list