Fix typos.
[oota-llvm.git] / docs / FAQ.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2                       "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
5   <title>LLVM: Frequently Asked Questions</title>
6   <style type="text/css">
7     @import url("llvm.css");
8     .question { font-weight: bold }
9     .answer   { margin-left: 2em  }
10   </style>
11 </head>
12 <body>
13
14 <div class="doc_title">
15   LLVM: Frequently Asked Questions
16 </div>
17
18 <ol>
19   <li><a href="#license">License</a>
20   <ol>
21   <li>Why are the LLVM source code and the front-end distributed under different
22   licenses?</li>
23   <li>Does the University of Illinois Open Source License really qualify as an
24   "open source" license?</li>
25   <li>Can I modify LLVM source code and redistribute the modified source?</li>
26   <li>Can I modify LLVM source code and redistribute binaries or other tools
27   based on it, without redistributing the source?</li>
28   </ol></li>
29
30   <li><a href="#source">Source code</a>
31   <ol>
32   <li>In what language is LLVM written?</li>
33   <li>How portable is the LLVM source code?</li>
34   </ol></li>
35
36   <li><a href="#build">Build Problems</a>
37   <ol>
38   <li>When I run configure, it finds the wrong C compiler.</li>
39   <li>The <tt>configure</tt> script finds the right C compiler, but it uses the
40   LLVM linker from a previous build.  What do I do?</li>
41   <li>When creating a dynamic library, I get a strange GLIBC error.</li>
42   <li>I've updated my source tree from CVS, and now my build is trying to use a
43   file/directory that doesn't exist.</li>
44   <li>I've modified a Makefile in my source tree, but my build tree keeps using
45   the old version.  What do I do?</li>
46   <li>I've upgraded to a new version of LLVM, and I get strange build
47   errors.</li>
48   <li>I've built LLVM and am testing it, but the tests freeze.</li>
49   <li>Why do test results differ when I perform different types of builds?</li>
50   <li>Compiling LLVM with GCC 3.3.2 fails, what should I do?</li>
51   <li>When I use the test suite, all of the C Backend tests fail.  What is
52       wrong?</li>
53   <li>After CVS update, rebuilding gives the error "No rule to make
54   target".</li>
55   </ol></li>
56
57   <li><a href="#felangs">Source Languages</a>
58   <ol>
59     <li><a href="#langs">What source languages are supported?</a></li>
60     <li><a href="#langhlsupp">What support is there for higher level source
61       language constructs for building a compiler?</a></li>
62     <li><a href="GetElementPtr.html">I don't understand the GetElementPtr
63       instruction. Help!</a></li>
64   </ol>
65
66   <li><a href="#cfe">Using the GCC Front End</a>
67   <ol>
68     <li>
69     When I compile software that uses a configure script, the configure script
70     thinks my system has all of the header files and libraries it is testing
71     for.  How do I get configure to work correctly?
72     </li>
73
74     <li>
75     When I compile code using the LLVM GCC front end, it complains that it
76     cannot find libcrtend.a.
77     </li>
78
79     <li>
80     How can I disable all optimizations when compiling code using the LLVM GCC front end?
81     </li>
82
83     <li><a href="#translatec++">Can I use LLVM to convert C++ code to C code?</a></li>
84
85   </ol>
86   </li>
87
88   <li><a href="#cfe_code">Questions about code generated by the GCC front-end</a>
89   <ol>
90      <li><a href="#__main">What is this <tt>__main()</tt> call that gets inserted into
91          <tt>main()</tt>?</a></li>
92      <li><a href="#iosinit">What is this <tt>llvm.global_ctors</tt> and
93           <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I
94           #include &lt;iostream&gt;?</a></li>
95      <li><a href="#codedce">Where did all of my code go??</a></li>
96      <li><a href="#undef">What is this "<tt>undef</tt>" thing that shows up in my code?</a></li>
97   </ol>
98   </li>
99 </ol>
100
101 <div class="doc_author">
102   <p>Written by <a href="http://llvm.org">The LLVM Team</a></p>
103 </div>
104
105
106 <!-- *********************************************************************** -->
107 <div class="doc_section">
108   <a name="license">License</a>
109 </div>
110 <!-- *********************************************************************** -->
111
112 <div class="question">
113 <p>Why are the LLVM source code and the front-end distributed under different
114 licenses?</p>
115 </div>
116         
117 <div class="answer">
118 <p>The C/C++ front-ends are based on GCC and must be distributed under the GPL.
119 Our aim is to distribute LLVM source code under a <em>much less restrictive</em>
120 license, in particular one that does not compel users who distribute tools based
121 on modifying the source to redistribute the modified source code as well.</p>
122 </div>
123
124 <div class="question">
125 <p>Does the University of Illinois Open Source License really qualify as an
126 "open source" license?</p>
127 </div>
128
129 <div class="answer">
130 <p>Yes, the license is <a
131 href="http://www.opensource.org/licenses/UoI-NCSA.php">certified</a> by the Open
132 Source Initiative (OSI).</p>
133 </div>
134
135 <div class="question">
136 <p>Can I modify LLVM source code and redistribute the modified source?</p>
137 </div>
138
139 <div class="answer">
140 <p>Yes.  The modified source distribution must retain the copyright notice and
141 follow the three bulletted conditions listed in the <a
142 href="http://llvm.org/releases/1.3/LICENSE.TXT">LLVM license</a>.</p>
143 </div>
144
145 <div class="question">
146 <p>Can I modify LLVM source code and redistribute binaries or other tools based
147 on it, without redistributing the source?</p>
148 </div>
149
150 <div class="answer">
151 <p>Yes, this is why we distribute LLVM under a less restrictive license than
152 GPL, as explained in the first question above.</p>
153 </div>
154
155 <!-- *********************************************************************** -->
156 <div class="doc_section">
157   <a name="source">Source Code</a>
158 </div>
159 <!-- *********************************************************************** -->
160
161 <div class="question">
162 <p>In what language is LLVM written?</p>
163 </div>
164
165 <div class="answer">
166 <p>All of the LLVM tools and libraries are written in C++ with extensive use of
167 the STL.</p>
168 </div>
169
170 <div class="question">
171 <p>How portable is the LLVM source code?</p>
172 </div>
173
174 <div class="answer">
175 <p>The LLVM source code should be portable to most modern UNIX-like operating
176 systems.  Most of the code is written in standard C++ with operating system
177 services abstracted to a support library.  The tools required to build and test
178 LLVM have been ported to a plethora of platforms.</p>
179
180 <p>Some porting problems may exist in the following areas:</p>
181
182 <ul>
183
184   <li>The GCC front end code is not as portable as the LLVM suite, so it may not
185   compile as well on unsupported platforms.</li>
186
187   <li>The LLVM build system relies heavily on UNIX shell tools, like the Bourne
188   Shell and sed.  Porting to systems without these tools (MacOS 9, Plan 9) will
189   require more effort.</li>
190
191 </ul>
192
193 </div>
194
195 <!-- *********************************************************************** -->
196 <div class="doc_section">
197   <a name="build">Build Problems</a>
198 </div>
199 <!-- *********************************************************************** -->
200
201 <div class="question">
202 <p>When I run configure, it finds the wrong C compiler.</p>
203 </div>
204
205 <div class="answer">
206
207 <p>The <tt>configure</tt> script attempts to locate first <tt>gcc</tt> and then
208 <tt>cc</tt>, unless it finds compiler paths set in <tt>CC</tt> and <tt>CXX</tt>
209 for the C and C++ compiler, respectively.</p>
210
211 <p>If <tt>configure</tt> finds the wrong compiler, either adjust your
212 <tt>PATH</tt> environment variable or set <tt>CC</tt> and <tt>CXX</tt>
213 explicitly.</p>
214
215 </div>
216
217 <div class="question">
218 <p>The <tt>configure</tt> script finds the right C compiler, but it uses the
219 LLVM linker from a previous build.  What do I do?</p>
220 </div>
221
222 <div class="answer">
223 <p>The <tt>configure</tt> script uses the <tt>PATH</tt> to find executables, so
224 if it's grabbing the wrong linker/assembler/etc, there are two ways to fix
225 it:</p>
226
227 <ol>
228                 
229   <li><p>Adjust your <tt>PATH</tt> environment variable so that the correct
230   program appears first in the <tt>PATH</tt>.  This may work, but may not be
231   convenient when you want them <i>first</i> in your path for other
232   work.</p></li>
233
234   <li><p>Run <tt>configure</tt> with an alternative <tt>PATH</tt> that is
235   correct. In a Borne compatible shell, the syntax would be:</p>
236                 
237       <p><tt>PATH=[the path without the bad program] ./configure ...</tt></p>
238
239       <p>This is still somewhat inconvenient, but it allows <tt>configure</tt>
240       to do its work without having to adjust your <tt>PATH</tt>
241       permanently.</p></li>
242         
243 </ol>
244
245 </div>
246
247 <div class="question">
248 <p>When creating a dynamic library, I get a strange GLIBC error.</p>
249 </div>
250
251 <div class="answer">
252 <p>Under some operating systems (i.e. Linux), libtool does not work correctly if
253 GCC was compiled with the --disable-shared option.  To work around this, install
254 your own version of GCC that has shared libraries enabled by default.</p>
255 </div>
256
257 <div class="question">
258 <p>I've updated my source tree from CVS, and now my build is trying to use a
259 file/directory that doesn't exist.</p>
260 </div>
261
262 <div class="answer">
263 <p>You need to re-run configure in your object directory.  When new Makefiles
264 are added to the source tree, they have to be copied over to the object tree in
265 order to be used by the build.</p>
266 </div>
267
268 <div class="question">
269 <p>I've modified a Makefile in my source tree, but my build tree keeps using the
270 old version.  What do I do?</p>
271 </div>
272
273 <div class="answer">
274
275 <p>If the Makefile already exists in your object tree, you
276 can just run the following command in the top level directory of your object
277 tree:</p>
278
279 <p><tt>./config.status &lt;relative path to Makefile&gt;</tt><p>
280
281 <p>If the Makefile is new, you will have to modify the configure script to copy
282 it over.</p>
283
284 </div>
285
286 <div class="question">
287 <p>I've upgraded to a new version of LLVM, and I get strange build errors.</p>
288 </div>
289
290 <div class="answer">
291
292 <p>Sometimes, changes to the LLVM source code alters how the build system works.
293 Changes in libtool, autoconf, or header file dependencies are especially prone
294 to this sort of problem.</p>
295
296 <p>The best thing to try is to remove the old files and re-build.  In most
297 cases, this takes care of the problem.  To do this, just type <tt>make
298 clean</tt> and then <tt>make</tt> in the directory that fails to build.</p>
299
300 </div>
301
302 <div class="question">
303 <p>I've built LLVM and am testing it, but the tests freeze.</p>
304 </div>
305
306 <div class="answer">
307
308 <p>This is most likely occurring because you built a profile or release
309 (optimized) build of LLVM and have not specified the same information on the
310 <tt>gmake</tt> command line.</p>
311
312 <p>For example, if you built LLVM with the command:</p>
313
314 <p><tt>gmake ENABLE_PROFILING=1</tt>
315
316 <p>...then you must run the tests with the following commands:</p>
317
318 <p><tt>cd llvm/test<br>gmake  ENABLE_PROFILING=1</tt></p>
319
320 </div>
321
322 <div class="question">
323 <p>Why do test results differ when I perform different types of builds?</p>
324 </div>
325
326 <div class="answer">
327
328 <p>The LLVM test suite is dependent upon several features of the LLVM tools and
329 libraries.</p>
330
331 <p>First, the debugging assertions in code are not enabled in optimized or
332 profiling builds.  Hence, tests that used to fail may pass.</p>
333         
334 <p>Second, some tests may rely upon debugging options or behavior that is only
335 available in the debug build.  These tests will fail in an optimized or profile
336 build.</p>
337
338 </div>
339
340 <div class="question">
341 <p>Compiling LLVM with GCC 3.3.2 fails, what should I do?</p>
342 </div>
343
344 <div class="answer">
345 <p>This is <a href="http://gcc.gnu.org/PR?13392">a bug in GCC</a>, and 
346    affects projects other than LLVM.  Try upgrading or downgrading your GCC.</p>
347 </div>
348
349 <div class="question">
350 <p>After CVS update, rebuilding gives the error "No rule to make target".</p>
351 </div>
352
353 <div class="answer">
354 <p>If the error is of the form:</p>
355
356 <div class="doc_code">
357 <tt>
358 gmake[2]: *** No rule to make target `/path/to/somefile', needed by
359 `/path/to/another/file.d'.<br>
360 Stop.
361 </tt>
362 </div>
363
364 <p>This may occur anytime files are moved within the CVS repository or removed
365 entirely.  In this case, the best solution is to erase all <tt>.d</tt> files,
366 which list dependencies for source files, and rebuild:</p>
367
368 <div class="doc_code">
369 <pre>
370 % cd $LLVM_OBJ_DIR
371 % rm -f `find . -name \*\.d` 
372 % gmake 
373 </pre>
374 </div>
375
376 <p>In other cases, it may be necessary to run <tt>make clean</tt> before
377 rebuilding.</p>
378 </div>
379
380 <!-- *********************************************************************** -->
381 <div class="doc_section"><a name="felangs">Source Languages</a></div>
382
383 <div class="question"><p>
384   <a name="langs">What source languages are supported?</a></p>
385 </div>
386 <div class="answer">
387   <p>LLVM currently has full support for C and C++ source languages. These are
388   available through a special version of GCC that LLVM calls the 
389   <a href="#cfe">C Front End</a></p>
390   <p>There is an incomplete version of a Java front end available in the
391   <tt>llvm-java</tt> CVS repository. There is no documentation on this yet so
392   you'll need to download the code, compile it, and try it.</p>
393   <p>In the <tt>examples/BFtoLLVM</tt> directory is a translator for the 
394   BrainF*** language (2002 Language Specification).</p>
395   <p>In the <tt>projects/Stacker</tt> directory is a compiler and runtime
396   library for the Stacker language, a "toy" language loosely based on Forth.</p>
397   <p>The PyPy developers are working on integrating LLVM into the PyPy backend
398   so that PyPy language can translate to LLVM.</p>
399 </div>
400 <div class="question"><a name="langhlsupp">
401   <p>What support is there for a higher level source language constructs for 
402   building a compiler?</a></p>
403 </div>
404 <div class="answer">
405   <p>Currently, there isn't much. LLVM supports an intermediate representation
406   which is useful for code representation but will not support the high level
407   (abstract syntax tree) representation needed by most compilers. There are no
408   facilities for lexical nor semantic analysis. There is, however, a <i>mostly
409     implemented</i> configuration-driven 
410   <a href="CompilerDriver.html">compiler driver</a> which simplifies the task
411   of running optimizations, linking, and executable generation.</p>
412 </div>
413
414 <div class="question"><a name="langhlsupp">
415   <p>I don't understand the GetElementPtr
416       instruction. Help!</a></p>
417 </div>
418 <div class="answer">
419   <p>See <a href="GetElementPtr.html">The Often Misunderstood GEP
420    Instruction</a>.</li>
421 </div>
422
423 <!-- *********************************************************************** -->
424 <div class="doc_section">
425   <a name="cfe">Using the GCC Front End</a>
426 </div>
427
428 <div class="question">
429 <p>
430 When I compile software that uses a configure script, the configure script
431 thinks my system has all of the header files and libraries it is testing for.
432 How do I get configure to work correctly?
433 </p>
434 </div>
435
436 <div class="answer">
437 <p>
438 The configure script is getting things wrong because the LLVM linker allows
439 symbols to be undefined at link time (so that they can be resolved during JIT
440 or translation to the C back end).  That is why configure thinks your system
441 "has everything."
442 </p>
443 <p>
444 To work around this, perform the following steps:
445 </p>
446 <ol>
447   <li>Make sure the CC and CXX environment variables contains the full path to 
448   the LLVM GCC front end.</li>
449
450   <li>Make sure that the regular C compiler is first in your PATH. </li>
451
452   <li>Add the string "-Wl,-native" to your CFLAGS environment variable.</li>
453 </ol>
454
455 <p>
456 This will allow the <tt>llvm-ld</tt> linker to create a native code executable 
457 instead of shell script that runs the JIT.  Creating native code requires 
458 standard linkage, which in turn will allow the configure script to find out if 
459 code is not linking on your system because the feature isn't available on your 
460 system.</p>
461 </div>
462
463 <div class="question">
464 <p>
465 When I compile code using the LLVM GCC front end, it complains that it cannot
466 find libcrtend.a.
467 </p>
468 </div>
469
470 <div class="answer">
471 <p>
472 The only way this can happen is if you haven't installed the runtime library. To
473 correct this, do:</p>
474 <pre>
475   % cd llvm/runtime
476   % make clean ; make install-bytecode
477 </pre>
478 </div>
479
480 <div class="question">
481 <p>
482 How can I disable all optimizations when compiling code using the LLVM GCC front end?
483 </p>
484 </div>
485
486 <div class="answer">
487 <p>
488 Passing "-Wa,-disable-opt -Wl,-disable-opt" will disable *all* cleanup and
489 optimizations done at the llvm level, leaving you with the truly horrible
490 code that you desire.
491 </p>
492 </div>
493
494
495 <div class="question">
496 <p>
497 <a name="translatec++">Can I use LLVM to convert C++ code to C code?</a>
498 </p>
499 </div>
500
501 <div class="answer">
502 <p>Yes, you can use LLVM to convert code from any language LLVM supports to C.
503 Note that the generated C code will be very low level (all loops are lowered
504 to gotos, etc) and not very pretty (comments are stripped, original source
505 formatting is totally lost, variables are renamed, expressions are regrouped), 
506 so this may not be what you're looking for.  However, this is a good way to add
507 C++ support for a processor that does not otherwise have a C++ compiler.
508 </p>
509
510 <p>Use commands like this:</p>
511
512 <ol>
513 <li><p>Compile your program as normal with llvm-g++:</p></li>
514
515 <div class="doc_code">$ llvm-g++ x.cpp -o program</div>
516
517 <p>or:</p>
518
519 <div class="doc_code">
520  llvm-g++ a.cpp -c<br>
521  llvm-g++ b.cpp -c<br>
522  llvm-g++ a.o b.o -o program
523 </div>
524
525 <p>With llvm-gcc3, this will generate program and program.bc.  The .bc file is 
526 the LLVM version of the program all linked together.</p>
527
528 <li><p>Convert the LLVM code to C code, using the LLC tool with the C
529 backend:</p></li>
530
531 <div class="doc_code">$ llc -march=c program.bc -o program.c</div>
532
533 <li><p>Finally, compile the c file:</p></li>
534
535 <div class="doc_code">$ cc x.c</div>
536
537 </ol>
538
539 <p>Note that, by default, the C backend does not support exception handling.
540 If you want/need it for a certain program, you can enable it by passing
541 "-enable-correct-eh-support" to the llc program.  The resultant code will
542 use setjmp/longjmp to implement exception support that is correct but
543 relatively slow.
544 </p>
545
546 <p>Also note: this specific sequence of commands won't work if you use a 
547 function defined in the C++ runtime library (or any other C++ library).  To 
548 access an external C++ library, you must manually 
549 compile libstdc++ to LLVM bytecode, statically link it into your program, then
550 use the commands above to convert the whole result into C code.  Alternatively,
551 you can compile the libraries and your application into two different chunks
552 of C code and link them.</p>
553
554 </div>
555
556
557
558 <!-- *********************************************************************** -->
559 <div class="doc_section">
560   <a name="cfe_code">Questions about code generated by the GCC front-end</a>
561 </div>
562
563 <div class="question"><p>
564 <a name="__main"></a>
565 What is this <tt>__main()</tt> call that gets inserted into <tt>main()</tt>?
566 </p></div>
567
568 <div class="answer">
569 <p>
570 The <tt>__main</tt> call is inserted by the C/C++ compiler in order to guarantee
571 that static constructors and destructors are called when the program starts up
572 and shuts down.  In C, you can create static constructors and destructors by
573 using GCC extensions, and in C++ you can do so by creating a global variable
574 whose class has a ctor or dtor.
575 </p>
576
577 <p>
578 The actual implementation of <tt>__main</tt> lives in the
579 <tt>llvm/runtime/GCCLibraries/crtend/</tt> directory in the source-base, and is
580 linked in automatically when you link the program.
581 </p>
582 </div>
583
584 <!--=========================================================================-->
585
586 <div class="question">
587 <a name="iosinit"></a>
588 <p> What is this <tt>llvm.global_ctors</tt> and
589 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I #include
590 &lt;iostream&gt;?</p>
591 </div>
592
593 <div class="answer">
594
595 <p>If you #include the &lt;iostream&gt; header into a C++ translation unit, the
596 file will probably use the <tt>std::cin</tt>/<tt>std::cout</tt>/... global
597 objects.  However, C++ does not guarantee an order of initialization between
598 static objects in different translation units, so if a static ctor/dtor in your
599 .cpp file used <tt>std::cout</tt>, for example, the object would not necessarily
600 be automatically initialized before your use.</p>
601
602 <p>To make <tt>std::cout</tt> and friends work correctly in these scenarios, the
603 STL that we use declares a static object that gets created in every translation
604 unit that includes <tt>&lt;iostream&gt;</tt>.  This object has a static
605 constructor and destructor that initializes and destroys the global iostream
606 objects before they could possibly be used in the file.  The code that you see
607 in the .ll file corresponds to the constructor and destructor registration code.
608 </p>
609
610 <p>If you would like to make it easier to <b>understand</b> the LLVM code
611 generated by the compiler in the demo page, consider using <tt>printf()</tt>
612 instead of <tt>iostream</tt>s to print values.</p>
613
614 </div>
615
616 <!--=========================================================================-->
617
618 <div class="question"><p>
619 <a name="codedce"></a>
620 Where did all of my code go??
621 </p></div>
622
623 <div class="answer">
624 <p>
625 If you are using the LLVM demo page, you may often wonder what happened to all
626 of the code that you typed in.  Remember that the demo script is running the
627 code through the LLVM optimizers, so if your code doesn't actually do anything
628 useful, it might all be deleted.
629 </p>
630
631 <p>
632 To prevent this, make sure that the code is actually needed.  For example, if
633 you are computing some expression, return the value from the function instead of
634 leaving it in a local variable.  If you really want to constrain the optimizer,
635 you can read from and assign to <tt>volatile</tt> global variables.
636 </p>
637 </div>
638
639 <!--=========================================================================-->
640
641 <div class="question"><p>
642 <a name="undef"></a>
643 <p>What is this "<tt>undef</tt>" thing that shows up in my code?
644 </p></div>
645
646 <div class="answer">
647 <p>
648 <a href="LangRef.html#undef"><tt>undef</tt></a> is the LLVM way of representing
649 a value that is not defined.  You can get these if you do not initialize a 
650 variable before you use it.  For example, the C function:</p>
651
652 <div class="doc_code">
653   <tt>int X() { int i; return i; }</tt>
654 </div>
655
656 <p>Is compiled to "<tt>ret int undef</tt>" because "i" never has a value 
657 specified for it.
658 </p>
659 </div>
660
661 <!-- *********************************************************************** -->
662
663 <hr>
664 <address>
665   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
666   src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
667   <a href="http://validator.w3.org/check/referer"><img
668   src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
669
670   <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
671   Last modified: $Date$
672 </address>
673
674 </body>
675 </html>