blob: d94c368d0c7b0f3532010f5a4680bb2e971dd504 [file] [log] [blame]
Misha Brukman6c91a332003-11-06 21:55:44 +00001<!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>
Misha Brukman5363e382004-06-01 18:51:03 +00006 <style type="text/css">
Misha Brukman6c91a332003-11-06 21:55:44 +00007 @import url("llvm.css");
8 .question { font-weight: bold }
9 .answer { margin-left: 2em }
10 </style>
11</head>
12<body>
John Criswell8af73202003-10-13 16:13:06 +000013
Misha Brukman6c91a332003-11-06 21:55:44 +000014<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>
Misha Brukman6c91a332003-11-06 21:55:44 +000039 <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>
Reid Spencerc7f87f22007-07-09 08:04:31 +000042 <li>I've updated my source tree from Subversion, and now my build is trying
43 to use a file/directory that doesn't exist.</li>
Misha Brukman6c91a332003-11-06 21:55:44 +000044 <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>
Chris Lattner5abdc6f2003-12-22 04:06:12 +000050 <li>Compiling LLVM with GCC 3.3.2 fails, what should I do?</li>
John Criswellfa2f3682004-03-29 20:23:11 +000051 <li>When I use the test suite, all of the C Backend tests fail. What is
52 wrong?</li>
Reid Spencerc7f87f22007-07-09 08:04:31 +000053 <li>After Subversion update, rebuilding gives the error "No rule to make
Bill Wendling0a303eb2007-05-29 09:35:34 +000054 target".</li>
55 <li><a href="#llvmc">The <tt>llvmc</tt> program gives me errors/doesn't
Bill Wendlingbeee6222007-09-22 09:54:47 +000056 work.</a></li>
Misha Brukman6c91a332003-11-06 21:55:44 +000057 </ol></li>
John Criswell956cf452003-11-18 16:08:49 +000058
Reid Spencer9aa244e2006-04-26 14:52:19 +000059 <li><a href="#felangs">Source Languages</a>
60 <ol>
61 <li><a href="#langs">What source languages are supported?</a></li>
Gordon Henriksen60c14042008-02-22 21:55:51 +000062 <li><a href="#langirgen">I'd like to write a self-hosting LLVM compiler. How
63 should I interface with the LLVM middle-end optimizers and back-end code
Dan Gohman8ef44982008-11-24 17:18:39 +000064 generators?</a></li>
Reid Spencer9aa244e2006-04-26 14:52:19 +000065 <li><a href="#langhlsupp">What support is there for higher level source
66 language constructs for building a compiler?</a></li>
Reid Spencer4bded9c2006-08-10 20:15:58 +000067 <li><a href="GetElementPtr.html">I don't understand the GetElementPtr
68 instruction. Help!</a></li>
Reid Spencer9aa244e2006-04-26 14:52:19 +000069 </ol>
70
Chris Lattner23353312003-11-19 05:53:12 +000071 <li><a href="#cfe">Using the GCC Front End</a>
John Criswell956cf452003-11-18 16:08:49 +000072 <ol>
73 <li>
74 When I compile software that uses a configure script, the configure script
75 thinks my system has all of the header files and libraries it is testing
76 for. How do I get configure to work correctly?
77 </li>
78
79 <li>
80 When I compile code using the LLVM GCC front end, it complains that it
Chris Lattner020e1fc2004-05-23 21:07:27 +000081 cannot find libcrtend.a.
John Criswell956cf452003-11-18 16:08:49 +000082 </li>
Tanya Lattneraf0ac272005-04-25 20:36:56 +000083
84 <li>
85 How can I disable all optimizations when compiling code using the LLVM GCC front end?
86 </li>
87
Dan Gohman8ef44982008-11-24 17:18:39 +000088 <li><a href="#translatecxx">Can I use LLVM to convert C++ code to C code?</a></li>
Chris Lattner7b1d3c02006-07-19 18:19:59 +000089
Dan Gohman1093e9a2009-02-10 17:26:53 +000090 <li><a href="#platformindependent">Can I compile C or C++ code to platform-independent LLVM bitcode?</a></li>
91
John Criswell956cf452003-11-18 16:08:49 +000092 </ol>
93 </li>
Chris Lattner23353312003-11-19 05:53:12 +000094
95 <li><a href="#cfe_code">Questions about code generated by the GCC front-end</a>
96 <ol>
Chris Lattner37d55472005-02-25 20:30:21 +000097 <li><a href="#iosinit">What is this <tt>llvm.global_ctors</tt> and
Chris Lattner87d6b402004-03-29 19:14:35 +000098 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I
Chris Lattner37d55472005-02-25 20:30:21 +000099 #include &lt;iostream&gt;?</a></li>
100 <li><a href="#codedce">Where did all of my code go??</a></li>
101 <li><a href="#undef">What is this "<tt>undef</tt>" thing that shows up in my code?</a></li>
Chris Lattner23353312003-11-19 05:53:12 +0000102 </ol>
103 </li>
Misha Brukman6c91a332003-11-06 21:55:44 +0000104</ol>
105
Chris Lattner020e1fc2004-05-23 21:07:27 +0000106<div class="doc_author">
Reid Spencerca058542006-03-14 05:39:39 +0000107 <p>Written by <a href="http://llvm.org">The LLVM Team</a></p>
Chris Lattner020e1fc2004-05-23 21:07:27 +0000108</div>
109
110
Misha Brukman6c91a332003-11-06 21:55:44 +0000111<!-- *********************************************************************** -->
112<div class="doc_section">
113 <a name="license">License</a>
114</div>
115<!-- *********************************************************************** -->
116
117<div class="question">
118<p>Why are the LLVM source code and the front-end distributed under different
119licenses?</p>
120</div>
121
122<div class="answer">
123<p>The C/C++ front-ends are based on GCC and must be distributed under the GPL.
124Our aim is to distribute LLVM source code under a <em>much less restrictive</em>
125license, in particular one that does not compel users who distribute tools based
126on modifying the source to redistribute the modified source code as well.</p>
127</div>
128
129<div class="question">
130<p>Does the University of Illinois Open Source License really qualify as an
131"open source" license?</p>
132</div>
133
134<div class="answer">
135<p>Yes, the license is <a
136href="http://www.opensource.org/licenses/UoI-NCSA.php">certified</a> by the Open
137Source Initiative (OSI).</p>
138</div>
139
140<div class="question">
141<p>Can I modify LLVM source code and redistribute the modified source?</p>
142</div>
143
144<div class="answer">
145<p>Yes. The modified source distribution must retain the copyright notice and
146follow the three bulletted conditions listed in the <a
Misha Brukman6c321b62008-12-17 18:06:53 +0000147href="http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT">LLVM license</a>.</p>
Misha Brukman6c91a332003-11-06 21:55:44 +0000148</div>
149
150<div class="question">
151<p>Can I modify LLVM source code and redistribute binaries or other tools based
152on it, without redistributing the source?</p>
153</div>
154
155<div class="answer">
156<p>Yes, this is why we distribute LLVM under a less restrictive license than
157GPL, as explained in the first question above.</p>
158</div>
159
160<!-- *********************************************************************** -->
161<div class="doc_section">
162 <a name="source">Source Code</a>
163</div>
164<!-- *********************************************************************** -->
165
166<div class="question">
167<p>In what language is LLVM written?</p>
168</div>
169
170<div class="answer">
171<p>All of the LLVM tools and libraries are written in C++ with extensive use of
172the STL.</p>
173</div>
174
175<div class="question">
176<p>How portable is the LLVM source code?</p>
177</div>
178
179<div class="answer">
180<p>The LLVM source code should be portable to most modern UNIX-like operating
181systems. Most of the code is written in standard C++ with operating system
182services abstracted to a support library. The tools required to build and test
183LLVM have been ported to a plethora of platforms.</p>
184
185<p>Some porting problems may exist in the following areas:</p>
186
187<ul>
Misha Brukman6c91a332003-11-06 21:55:44 +0000188 <li>The GCC front end code is not as portable as the LLVM suite, so it may not
Bill Wendlingbeee6222007-09-22 09:54:47 +0000189 compile as well on unsupported platforms.</li>
Misha Brukman6c91a332003-11-06 21:55:44 +0000190
Misha Brukman6c91a332003-11-06 21:55:44 +0000191 <li>The LLVM build system relies heavily on UNIX shell tools, like the Bourne
Bill Wendlingbeee6222007-09-22 09:54:47 +0000192 Shell and sed. Porting to systems without these tools (MacOS 9, Plan 9)
193 will require more effort.</li>
Misha Brukman6c91a332003-11-06 21:55:44 +0000194</ul>
195
196</div>
197
198<!-- *********************************************************************** -->
199<div class="doc_section">
200 <a name="build">Build Problems</a>
201</div>
202<!-- *********************************************************************** -->
203
204<div class="question">
205<p>When I run configure, it finds the wrong C compiler.</p>
206</div>
207
208<div class="answer">
209
210<p>The <tt>configure</tt> script attempts to locate first <tt>gcc</tt> and then
211<tt>cc</tt>, unless it finds compiler paths set in <tt>CC</tt> and <tt>CXX</tt>
212for the C and C++ compiler, respectively.</p>
213
214<p>If <tt>configure</tt> finds the wrong compiler, either adjust your
215<tt>PATH</tt> environment variable or set <tt>CC</tt> and <tt>CXX</tt>
216explicitly.</p>
217
218</div>
219
220<div class="question">
Misha Brukman6c91a332003-11-06 21:55:44 +0000221<p>The <tt>configure</tt> script finds the right C compiler, but it uses the
222LLVM linker from a previous build. What do I do?</p>
223</div>
224
225<div class="answer">
226<p>The <tt>configure</tt> script uses the <tt>PATH</tt> to find executables, so
227if it's grabbing the wrong linker/assembler/etc, there are two ways to fix
228it:</p>
229
230<ol>
Misha Brukman6c91a332003-11-06 21:55:44 +0000231 <li><p>Adjust your <tt>PATH</tt> environment variable so that the correct
Bill Wendlingbeee6222007-09-22 09:54:47 +0000232 program appears first in the <tt>PATH</tt>. This may work, but may not be
233 convenient when you want them <i>first</i> in your path for other
234 work.</p></li>
Misha Brukman6c91a332003-11-06 21:55:44 +0000235
236 <li><p>Run <tt>configure</tt> with an alternative <tt>PATH</tt> that is
Bill Wendlingbeee6222007-09-22 09:54:47 +0000237 correct. In a Borne compatible shell, the syntax would be:</p>
238
Misha Brukman29f390b2008-12-17 18:11:40 +0000239<pre class="doc_code">
Bill Wendlingbeee6222007-09-22 09:54:47 +0000240% PATH=[the path without the bad program] ./configure ...
241</pre>
Misha Brukman6c91a332003-11-06 21:55:44 +0000242
243 <p>This is still somewhat inconvenient, but it allows <tt>configure</tt>
Bill Wendlingbeee6222007-09-22 09:54:47 +0000244 to do its work without having to adjust your <tt>PATH</tt>
245 permanently.</p></li>
Misha Brukman6c91a332003-11-06 21:55:44 +0000246</ol>
247
248</div>
249
250<div class="question">
251<p>When creating a dynamic library, I get a strange GLIBC error.</p>
252</div>
253
254<div class="answer">
255<p>Under some operating systems (i.e. Linux), libtool does not work correctly if
256GCC was compiled with the --disable-shared option. To work around this, install
257your own version of GCC that has shared libraries enabled by default.</p>
258</div>
259
260<div class="question">
Bill Wendlingbeee6222007-09-22 09:54:47 +0000261<p>I've updated my source tree from Subversion, and now my build is trying to
262use a file/directory that doesn't exist.</p>
Misha Brukman6c91a332003-11-06 21:55:44 +0000263</div>
264
265<div class="answer">
266<p>You need to re-run configure in your object directory. When new Makefiles
267are added to the source tree, they have to be copied over to the object tree in
268order to be used by the build.</p>
269</div>
270
271<div class="question">
272<p>I've modified a Makefile in my source tree, but my build tree keeps using the
273old version. What do I do?</p>
274</div>
275
276<div class="answer">
Misha Brukman6c91a332003-11-06 21:55:44 +0000277<p>If the Makefile already exists in your object tree, you
278can just run the following command in the top level directory of your object
279tree:</p>
280
Misha Brukman29f390b2008-12-17 18:11:40 +0000281<pre class="doc_code">
282% ./config.status &lt;relative path to Makefile&gt;
283</pre>
Misha Brukman6c91a332003-11-06 21:55:44 +0000284
285<p>If the Makefile is new, you will have to modify the configure script to copy
286it over.</p>
287
288</div>
289
290<div class="question">
291<p>I've upgraded to a new version of LLVM, and I get strange build errors.</p>
292</div>
293
294<div class="answer">
295
296<p>Sometimes, changes to the LLVM source code alters how the build system works.
297Changes in libtool, autoconf, or header file dependencies are especially prone
298to this sort of problem.</p>
299
300<p>The best thing to try is to remove the old files and re-build. In most
301cases, this takes care of the problem. To do this, just type <tt>make
302clean</tt> and then <tt>make</tt> in the directory that fails to build.</p>
303
304</div>
305
306<div class="question">
307<p>I've built LLVM and am testing it, but the tests freeze.</p>
308</div>
309
310<div class="answer">
311
312<p>This is most likely occurring because you built a profile or release
313(optimized) build of LLVM and have not specified the same information on the
314<tt>gmake</tt> command line.</p>
315
316<p>For example, if you built LLVM with the command:</p>
317
Misha Brukman29f390b2008-12-17 18:11:40 +0000318<pre class="doc_code">
319% gmake ENABLE_PROFILING=1
320</pre>
Misha Brukman6c91a332003-11-06 21:55:44 +0000321
322<p>...then you must run the tests with the following commands:</p>
323
Misha Brukman29f390b2008-12-17 18:11:40 +0000324<pre class="doc_code">
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000325% cd llvm/test
326% gmake ENABLE_PROFILING=1
327</pre>
Misha Brukman6c91a332003-11-06 21:55:44 +0000328
329</div>
330
331<div class="question">
332<p>Why do test results differ when I perform different types of builds?</p>
333</div>
334
335<div class="answer">
336
337<p>The LLVM test suite is dependent upon several features of the LLVM tools and
338libraries.</p>
339
340<p>First, the debugging assertions in code are not enabled in optimized or
341profiling builds. Hence, tests that used to fail may pass.</p>
342
343<p>Second, some tests may rely upon debugging options or behavior that is only
344available in the debug build. These tests will fail in an optimized or profile
345build.</p>
346
347</div>
348
Chris Lattner408916b2003-12-08 05:43:19 +0000349<div class="question">
Chris Lattner5abdc6f2003-12-22 04:06:12 +0000350<p>Compiling LLVM with GCC 3.3.2 fails, what should I do?</p>
Chris Lattner408916b2003-12-08 05:43:19 +0000351</div>
352
353<div class="answer">
Chris Lattner5abdc6f2003-12-22 04:06:12 +0000354<p>This is <a href="http://gcc.gnu.org/PR?13392">a bug in GCC</a>, and
Bill Wendlingbeee6222007-09-22 09:54:47 +0000355affects projects other than LLVM. Try upgrading or downgrading your GCC.</p>
Chris Lattner408916b2003-12-08 05:43:19 +0000356</div>
357
John Criswellfa2f3682004-03-29 20:23:11 +0000358<div class="question">
Bill Wendlingbeee6222007-09-22 09:54:47 +0000359<p>After Subversion update, rebuilding gives the error "No rule to make
360target".</p>
Misha Brukman46572352004-09-09 16:36:47 +0000361</div>
362
363<div class="answer">
364<p>If the error is of the form:</p>
365
Misha Brukman29f390b2008-12-17 18:11:40 +0000366<pre class="doc_code">
Misha Brukman46572352004-09-09 16:36:47 +0000367gmake[2]: *** No rule to make target `/path/to/somefile', needed by
368`/path/to/another/file.d'.<br>
369Stop.
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000370</pre>
Misha Brukman46572352004-09-09 16:36:47 +0000371
Reid Spencerc7f87f22007-07-09 08:04:31 +0000372<p>This may occur anytime files are moved within the Subversion repository or
373removed entirely. In this case, the best solution is to erase all
374<tt>.d</tt> files, which list dependencies for source files, and rebuild:</p>
Misha Brukman46572352004-09-09 16:36:47 +0000375
Misha Brukman29f390b2008-12-17 18:11:40 +0000376<pre class="doc_code">
Misha Brukman46572352004-09-09 16:36:47 +0000377% cd $LLVM_OBJ_DIR
378% rm -f `find . -name \*\.d`
379% gmake
380</pre>
Misha Brukman46572352004-09-09 16:36:47 +0000381
382<p>In other cases, it may be necessary to run <tt>make clean</tt> before
383rebuilding.</p>
384</div>
385
Bill Wendlingbeee6222007-09-22 09:54:47 +0000386<div class="question"><p><a name="llvmc">
387The <tt>llvmc</tt> program gives me errors/doesn't work.</a></p>
Bill Wendling0a303eb2007-05-29 09:35:34 +0000388</div>
389
390<div class="answer">
Bill Wendlingbeee6222007-09-22 09:54:47 +0000391<p><tt>llvmc</tt> is experimental and isn't really supported. We suggest
392using <tt>llvm-gcc</tt> instead.</p>
Bill Wendling0a303eb2007-05-29 09:35:34 +0000393</div>
394
Misha Brukman6c91a332003-11-06 21:55:44 +0000395<!-- *********************************************************************** -->
Reid Spencer4d3171f2006-04-26 15:46:53 +0000396<div class="doc_section"><a name="felangs">Source Languages</a></div>
Reid Spencer9aa244e2006-04-26 14:52:19 +0000397
Reid Spencer4d3171f2006-04-26 15:46:53 +0000398<div class="question"><p>
399 <a name="langs">What source languages are supported?</a></p>
400</div>
401<div class="answer">
Reid Spencer9aa244e2006-04-26 14:52:19 +0000402 <p>LLVM currently has full support for C and C++ source languages. These are
403 available through a special version of GCC that LLVM calls the
404 <a href="#cfe">C Front End</a></p>
405 <p>There is an incomplete version of a Java front end available in the
Reid Spencerc7f87f22007-07-09 08:04:31 +0000406 <tt>java</tt> module. There is no documentation on this yet so
Reid Spencer9aa244e2006-04-26 14:52:19 +0000407 you'll need to download the code, compile it, and try it.</p>
Reid Spencer9aa244e2006-04-26 14:52:19 +0000408 <p>The PyPy developers are working on integrating LLVM into the PyPy backend
409 so that PyPy language can translate to LLVM.</p>
410</div>
Gordon Henriksen6ee6b7c2008-02-22 20:58:29 +0000411
412<div class="question"><p><a name="langirgen">
Gordon Henriksen60c14042008-02-22 21:55:51 +0000413 I'd like to write a self-hosting LLVM compiler. How should I interface with
414 the LLVM middle-end optimizers and back-end code generators?
Gordon Henriksen6ee6b7c2008-02-22 20:58:29 +0000415</a></p></div>
416<div class="answer">
417 <p>Your compiler front-end will communicate with LLVM by creating a module in
Gordon Henriksen60c14042008-02-22 21:55:51 +0000418 the LLVM intermediate representation (IR) format. Assuming you want to
419 write your language's compiler in the language itself (rather than C++),
420 there are 3 major ways to tackle generating LLVM IR from a front-end:</p>
Gordon Henriksen6ee6b7c2008-02-22 20:58:29 +0000421 <ul>
422 <li>
423 <strong>Call into the LLVM libraries code using your language's FFI
424 (foreign function interface).</strong>
425 <ul>
426 <li><em>for:</em> best tracks changes to the LLVM IR, .ll syntax,
427 and .bc format</li>
428 <li><em>for:</em> enables running LLVM optimization passes without a
429 emit/parse overhead</li>
430 <li><em>for:</em> adapts well to a JIT context</li>
431 <li><em>against:</em> lots of ugly glue code to write</li>
432 </ul>
433 </li>
434 <li>
435 <strong>Emit LLVM assembly from your compiler's native language.</strong>
436 <ul>
437 <li><em>for:</em> very straightforward to get started</li>
438 <li><em>against:</em> the .ll parser is slower than the bitcode reader
439 when interfacing to the middle end</li>
440 <li><em>against:</em> you'll have to re-engineer the LLVM IR object
441 model and asm writer in your language</li>
442 <li><em>against:</em> it may be harder to track changes to the IR</li>
443 </ul>
444 </li>
445 <li>
446 <strong>Emit LLVM bitcode from your compiler's native language.</strong>
447 <ul>
448 <li><em>for:</em> can use the more-efficient bitcode reader when
449 interfacing to the middle end</li>
450 <li><em>against:</em> you'll have to re-engineer the LLVM IR object
451 model and bitcode writer in your language</li>
452 <li><em>against:</em> it may be harder to track changes to the IR</li>
453 </ul>
454 </li>
455 </ul>
456 <p>If you go with the first option, the C bindings in include/llvm-c should
457 help a lot, since most languages have strong support for interfacing with
458 C. The most common hurdle with calling C from managed code is interfacing
459 with the garbage collector. The C interface was designed to require very
460 little memory management, and so is straightforward in this regard.</p>
461</div>
462
Bill Wendlingbeee6222007-09-22 09:54:47 +0000463<div class="question"><p><a name="langhlsupp">
464 What support is there for a higher level source language constructs for
Reid Spencer4d3171f2006-04-26 15:46:53 +0000465 building a compiler?</a></p>
Reid Spencer9aa244e2006-04-26 14:52:19 +0000466</div>
467<div class="answer">
468 <p>Currently, there isn't much. LLVM supports an intermediate representation
469 which is useful for code representation but will not support the high level
470 (abstract syntax tree) representation needed by most compilers. There are no
Jeff Cohen027fbc22006-04-26 21:03:17 +0000471 facilities for lexical nor semantic analysis. There is, however, a <i>mostly
Reid Spencer9aa244e2006-04-26 14:52:19 +0000472 implemented</i> configuration-driven
473 <a href="CompilerDriver.html">compiler driver</a> which simplifies the task
474 of running optimizations, linking, and executable generation.</p>
Reid Spencer9aa244e2006-04-26 14:52:19 +0000475</div>
Chris Lattner667c9012006-08-15 00:43:35 +0000476
Dan Gohman8ef44982008-11-24 17:18:39 +0000477<div class="question"><p><a name="getelementptr">
Bill Wendlingbeee6222007-09-22 09:54:47 +0000478 I don't understand the GetElementPtr instruction. Help!</a></p>
Chris Lattner667c9012006-08-15 00:43:35 +0000479</div>
480<div class="answer">
481 <p>See <a href="GetElementPtr.html">The Often Misunderstood GEP
Bill Wendlingbeee6222007-09-22 09:54:47 +0000482 Instruction</a>.</p>
Chris Lattner667c9012006-08-15 00:43:35 +0000483</div>
484
Reid Spencer9aa244e2006-04-26 14:52:19 +0000485<!-- *********************************************************************** -->
486<div class="doc_section">
Chris Lattner23353312003-11-19 05:53:12 +0000487 <a name="cfe">Using the GCC Front End</a>
John Criswell5e0f07e2003-11-18 16:05:23 +0000488</div>
489
490<div class="question">
491<p>
492When I compile software that uses a configure script, the configure script
493thinks my system has all of the header files and libraries it is testing for.
494How do I get configure to work correctly?
495</p>
496</div>
497
498<div class="answer">
499<p>
500The configure script is getting things wrong because the LLVM linker allows
501symbols to be undefined at link time (so that they can be resolved during JIT
502or translation to the C back end). That is why configure thinks your system
503"has everything."
504</p>
505<p>
506To work around this, perform the following steps:
507</p>
John Criswell5e0f07e2003-11-18 16:05:23 +0000508<ol>
Reid Spencera7974cc2007-02-09 15:59:08 +0000509 <li>Make sure the CC and CXX environment variables contains the full path to
510 the LLVM GCC front end.</li>
John Criswell5e0f07e2003-11-18 16:05:23 +0000511
Reid Spencera7974cc2007-02-09 15:59:08 +0000512 <li>Make sure that the regular C compiler is first in your PATH. </li>
John Criswell5e0f07e2003-11-18 16:05:23 +0000513
Reid Spencera7974cc2007-02-09 15:59:08 +0000514 <li>Add the string "-Wl,-native" to your CFLAGS environment variable.</li>
John Criswell5e0f07e2003-11-18 16:05:23 +0000515</ol>
516
517<p>
Reid Spencera7974cc2007-02-09 15:59:08 +0000518This will allow the <tt>llvm-ld</tt> linker to create a native code executable
519instead of shell script that runs the JIT. Creating native code requires
520standard linkage, which in turn will allow the configure script to find out if
521code is not linking on your system because the feature isn't available on your
522system.</p>
John Criswell5e0f07e2003-11-18 16:05:23 +0000523</div>
524
Chris Lattner23353312003-11-19 05:53:12 +0000525<div class="question">
John Criswell5e0f07e2003-11-18 16:05:23 +0000526<p>
527When I compile code using the LLVM GCC front end, it complains that it cannot
John Criswelld89dbd02004-03-12 18:20:15 +0000528find libcrtend.a.
John Criswell5e0f07e2003-11-18 16:05:23 +0000529</p>
530</div>
531
Chris Lattner23353312003-11-19 05:53:12 +0000532<div class="answer">
John Criswell5e0f07e2003-11-18 16:05:23 +0000533<p>
Reid Spencer360d6b72004-12-15 00:14:01 +0000534The only way this can happen is if you haven't installed the runtime library. To
535correct this, do:</p>
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000536
Misha Brukman29f390b2008-12-17 18:11:40 +0000537<pre class="doc_code">
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000538% cd llvm/runtime
539% make clean ; make install-bytecode
Reid Spencer360d6b72004-12-15 00:14:01 +0000540</pre>
John Criswell5e0f07e2003-11-18 16:05:23 +0000541</div>
Chris Lattner23353312003-11-19 05:53:12 +0000542
Tanya Lattneraf0ac272005-04-25 20:36:56 +0000543<div class="question">
544<p>
545How can I disable all optimizations when compiling code using the LLVM GCC front end?
546</p>
547</div>
548
549<div class="answer">
550<p>
551Passing "-Wa,-disable-opt -Wl,-disable-opt" will disable *all* cleanup and
552optimizations done at the llvm level, leaving you with the truly horrible
553code that you desire.
554</p>
555</div>
Chris Lattner23353312003-11-19 05:53:12 +0000556
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000557
558<div class="question">
559<p>
Dan Gohman8ef44982008-11-24 17:18:39 +0000560<a name="translatecxx">Can I use LLVM to convert C++ code to C code?</a>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000561</p>
562</div>
563
564<div class="answer">
565<p>Yes, you can use LLVM to convert code from any language LLVM supports to C.
566Note that the generated C code will be very low level (all loops are lowered
567to gotos, etc) and not very pretty (comments are stripped, original source
568formatting is totally lost, variables are renamed, expressions are regrouped),
Dan Gohmane3f22782009-01-25 16:04:50 +0000569so this may not be what you're looking for. Also, there are several
570limitations noted below.<p>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000571
572<p>Use commands like this:</p>
573
574<ol>
Bill Wendlingbeee6222007-09-22 09:54:47 +0000575 <li><p>Compile your program as normal with llvm-g++:</p>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000576
Misha Brukman29f390b2008-12-17 18:11:40 +0000577<pre class="doc_code">
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000578% llvm-g++ x.cpp -o program
579</pre>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000580
Bill Wendlingbeee6222007-09-22 09:54:47 +0000581 <p>or:</p>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000582
Misha Brukman29f390b2008-12-17 18:11:40 +0000583<pre class="doc_code">
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000584% llvm-g++ a.cpp -c
585% llvm-g++ b.cpp -c
586% llvm-g++ a.o b.o -o program
587</pre>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000588
Bill Wendlingbeee6222007-09-22 09:54:47 +0000589 <p>With llvm-gcc3, this will generate program and program.bc. The .bc
590 file is the LLVM version of the program all linked together.</p></li>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000591
Bill Wendlingbeee6222007-09-22 09:54:47 +0000592 <li><p>Convert the LLVM code to C code, using the LLC tool with the C
593 backend:</p>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000594
Misha Brukman29f390b2008-12-17 18:11:40 +0000595<pre class="doc_code">
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000596% llc -march=c program.bc -o program.c
Misha Brukman29f390b2008-12-17 18:11:40 +0000597</pre></li>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000598
Bill Wendlingbeee6222007-09-22 09:54:47 +0000599<li><p>Finally, compile the C file:</p>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000600
Misha Brukman29f390b2008-12-17 18:11:40 +0000601<pre class="doc_code">
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000602% cc x.c
Misha Brukman29f390b2008-12-17 18:11:40 +0000603</pre></li>
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000604
605</ol>
606
Dan Gohmane3f22782009-01-25 16:04:50 +0000607<p>Using LLVM does not eliminate the need for C++ library support.
608If you use the llvm-g++ front-end, the generated code will depend on
609g++'s C++ support libraries in the same way that code generated from
610g++ would. If you use another C++ front-end, the generated code will
611depend on whatever library that front-end would normally require.</p>
Chris Lattnerb0961902006-08-31 04:26:31 +0000612
Dan Gohmane3f22782009-01-25 16:04:50 +0000613<p>If you are working on a platform that does not provide any C++
614libraries, you may be able to manually compile libstdc++ to LLVM
Bill Wendlingbeee6222007-09-22 09:54:47 +0000615bitcode, statically link it into your program, then use the commands above to
Dan Gohmane3f22782009-01-25 16:04:50 +0000616convert the whole result into C code. Alternatively, you might compile the
Bill Wendlingbeee6222007-09-22 09:54:47 +0000617libraries and your application into two different chunks of C code and link
618them.</p>
Chris Lattnerb0961902006-08-31 04:26:31 +0000619
Dan Gohmane3f22782009-01-25 16:04:50 +0000620<p>Note that, by default, the C back end does not support exception handling. If
621you want/need it for a certain program, you can enable it by passing
622"-enable-correct-eh-support" to the llc program. The resultant code will use
623setjmp/longjmp to implement exception support that is relatively slow, and
624not C++-ABI-conforming on most platforms, but otherwise correct.</p>
625
626<p>Also, there are a number of other limitations of the C backend that
627cause it to produce code that does not fully conform to the C++ ABI on
628most platforms. Some of the C++ programs in LLVM's test suite are known
629to fail when compiled with the C back end because of ABI incompatiblities
630with standard C++ libraries.</p>
631
Chris Lattner7b1d3c02006-07-19 18:19:59 +0000632</div>
633
Dan Gohman1093e9a2009-02-10 17:26:53 +0000634<div class="question">
635<p>
636<a name="platformindependent">Can I compile C or C++ code to platform-independent LLVM bitcode?</a>
637</p>
638</div>
639
640<div class="answer">
641
642<p>No. C and C++ are inherently platform-dependent languages. The most
643obvious example of this is the preprocessor. A very common way that C code
644is made portable is by using the preprocessor to include platform-specific
645code. In practice, information about other platforms is lost after
646preprocessing, so the result is inherently dependent on the platform that
647the preprocessing was targetting.</p>
648
649<p>Another example is <tt>sizeof</tt>. It's common for <tt>sizeof(long)</tt>
650to vary between platforms. In most C front-ends, <tt>sizeof</tt> is expanded
651to a constant immediately, thus hardwaring a platform-specific detail.</p>
652
653<p>Also, since many platforms define their ABIs in terms of C, and since
654LLVM is lower-level than C, front-ends currently must emit platform-specific
655IR in order to have the result conform to the platform ABI.</p>
656
657</div>
658
Chris Lattner23353312003-11-19 05:53:12 +0000659<!-- *********************************************************************** -->
660<div class="doc_section">
661 <a name="cfe_code">Questions about code generated by the GCC front-end</a>
662</div>
663
Misha Brukman8d9a83a2004-12-03 23:58:18 +0000664<div class="question">
Chris Lattner37d55472005-02-25 20:30:21 +0000665<a name="iosinit"></a>
Misha Brukman8d9a83a2004-12-03 23:58:18 +0000666<p> What is this <tt>llvm.global_ctors</tt> and
667<tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I #include
668&lt;iostream&gt;?</p>
669</div>
Chris Lattner87d6b402004-03-29 19:14:35 +0000670
671<div class="answer">
Misha Brukman8d9a83a2004-12-03 23:58:18 +0000672
673<p>If you #include the &lt;iostream&gt; header into a C++ translation unit, the
Chris Lattner87d6b402004-03-29 19:14:35 +0000674file will probably use the <tt>std::cin</tt>/<tt>std::cout</tt>/... global
675objects. However, C++ does not guarantee an order of initialization between
676static objects in different translation units, so if a static ctor/dtor in your
677.cpp file used <tt>std::cout</tt>, for example, the object would not necessarily
Misha Brukman8d9a83a2004-12-03 23:58:18 +0000678be automatically initialized before your use.</p>
Chris Lattner87d6b402004-03-29 19:14:35 +0000679
Misha Brukman8d9a83a2004-12-03 23:58:18 +0000680<p>To make <tt>std::cout</tt> and friends work correctly in these scenarios, the
Chris Lattner87d6b402004-03-29 19:14:35 +0000681STL that we use declares a static object that gets created in every translation
Misha Brukman8d9a83a2004-12-03 23:58:18 +0000682unit that includes <tt>&lt;iostream&gt;</tt>. This object has a static
683constructor and destructor that initializes and destroys the global iostream
684objects before they could possibly be used in the file. The code that you see
685in the .ll file corresponds to the constructor and destructor registration code.
Chris Lattner87d6b402004-03-29 19:14:35 +0000686</p>
687
Misha Brukman8d9a83a2004-12-03 23:58:18 +0000688<p>If you would like to make it easier to <b>understand</b> the LLVM code
689generated by the compiler in the demo page, consider using <tt>printf()</tt>
690instead of <tt>iostream</tt>s to print values.</p>
691
Chris Lattner87d6b402004-03-29 19:14:35 +0000692</div>
693
Chris Lattner37d55472005-02-25 20:30:21 +0000694<!--=========================================================================-->
695
696<div class="question"><p>
697<a name="codedce"></a>
698Where did all of my code go??
699</p></div>
700
701<div class="answer">
702<p>
703If you are using the LLVM demo page, you may often wonder what happened to all
704of the code that you typed in. Remember that the demo script is running the
705code through the LLVM optimizers, so if your code doesn't actually do anything
706useful, it might all be deleted.
707</p>
708
709<p>
710To prevent this, make sure that the code is actually needed. For example, if
711you are computing some expression, return the value from the function instead of
712leaving it in a local variable. If you really want to constrain the optimizer,
713you can read from and assign to <tt>volatile</tt> global variables.
714</p>
715</div>
716
717<!--=========================================================================-->
718
719<div class="question"><p>
720<a name="undef"></a>
721<p>What is this "<tt>undef</tt>" thing that shows up in my code?
722</p></div>
723
724<div class="answer">
725<p>
726<a href="LangRef.html#undef"><tt>undef</tt></a> is the LLVM way of representing
727a value that is not defined. You can get these if you do not initialize a
728variable before you use it. For example, the C function:</p>
729
Misha Brukman29f390b2008-12-17 18:11:40 +0000730<pre class="doc_code">
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000731int X() { int i; return i; }
732</pre>
Chris Lattner37d55472005-02-25 20:30:21 +0000733
Bill Wendling2c22fcb2007-05-29 09:24:33 +0000734<p>Is compiled to "<tt>ret i32 undef</tt>" because "<tt>i</tt>" never has
735a value specified for it.</p>
Chris Lattner37d55472005-02-25 20:30:21 +0000736</div>
737
John Criswell5e0f07e2003-11-18 16:05:23 +0000738<!-- *********************************************************************** -->
John Criswell8af73202003-10-13 16:13:06 +0000739
740<hr>
Misha Brukman5363e382004-06-01 18:51:03 +0000741<address>
742 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman86242e12008-12-11 17:34:48 +0000743 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Misha Brukman5363e382004-06-01 18:51:03 +0000744 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman86242e12008-12-11 17:34:48 +0000745 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Misha Brukman5363e382004-06-01 18:51:03 +0000746
Reid Spencerca058542006-03-14 05:39:39 +0000747 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
Misha Brukman6c91a332003-11-06 21:55:44 +0000748 Last modified: $Date$
Misha Brukman5363e382004-06-01 18:51:03 +0000749</address>
John Criswellebe140b2003-10-24 22:48:20 +0000750
John Criswell8af73202003-10-13 16:13:06 +0000751</body>
752</html>