1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>System Library</title>
<link rel="stylesheet" href="llvm.css" type="text/css">
</head>
<body>
<div class="doc_title">System Library</div>
<div class="doc_warning">
<p>Warning: This document is a work in progress.</p>
</div>
<ul>
<li><a href="#abstract">Abstract</a></li>
<li><a href="#requirements">System Library Requirements</a>
<ol>
<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="#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>
</ol></li>
<li><a href="#design">System Library Design</a>
<ol>
<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>
<li><a href="#lowlevel">Use Low Level Interfaces</a></li>
<li><a href="#memalloc">No Memory Allocation</a></li>
<li><a href="#virtuals">No Virtual Methods</a></li>
</ol></li>
<li><a href="#detail">System Library Details</a>
<ol>
<li><a href="#bug">Tracking Bugzilla Bug: 351</a></li>
<li><a href="#refimpl">Reference Implementatation</a></li>
</ol></li>
</ul>
<div class="doc_author">
<p>Written by <a href="rspencer@x10sys.com">Reid Spencer</a></p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"><a name="abstract">Abstract</a></div>
<div class="doc_text">
<p>This document describes the requirements, design, and implementation
details of LLVM's System Library. The library is composed of the header files
in <tt>llvm/include/llvm/System</tt> and the source files in
<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
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>
</div>
<!-- *********************************************************************** -->
<div class="doc_section">
<a name="requirements">System Library Requirements</a>
</div>
<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. Of necessity, these requirements
must be strictly followed in order to ensure the library's goal is reached.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="headers">Hide System Header Files</a></div>
<div class="doc_text">
<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>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="c_headers">Allow Standard C Headers</a>
</div>
<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>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="cpp_headers">Allow Standard C++ Headers</a>
</div>
<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>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="nofunc">No Exposed Functions</a></div>
<div class="doc_text">
<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>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="nodata">No Exposed Data</a></div>
<div class="doc_text">
<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>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="throw">Throw Only std::string</a></div>
<div class="doc_text">
<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>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="throw_spec">No throw Specifications</a>
</div>
<div class="doc_text">
<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>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="nodupl">No Duplicate Implementations</a>
</div>
<div class="doc_text">
<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>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"><a name="design">System Library Design</a></div>
<div class="doc_text">
<p>In order to fulfill the requirements of the system library, strict design
objectives must be maintained in the library as it evolves. The goal here
is to provide interfaces to operating system concepts (files, memory maps,
sockets, signals, locking, etc) efficiently and in such a way that the
remainder of LLVM is completely operating system agnostic.</p>
</div>
<!-- ======================================================================= -->
<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>
<!-- ======================================================================= -->
<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>
<!-- ======================================================================= -->
<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 g
|