blob: e37297242647d1208cb609ccff2259dbd8136864 [file] [log] [blame]
Misha Brukmana6538852003-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 Brukman7ce62cc2004-06-01 18:51:03 +00006 <style type="text/css">
Misha Brukmana6538852003-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 Criswellc310f622003-10-13 16:13:06 +000013
Misha Brukmana6538852003-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 Brukmana6538852003-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 Spencer669ed452007-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 Brukmana6538852003-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 Lattner306acee2003-12-22 04:06:12 +000050 <li>Compiling LLVM with GCC 3.3.2 fails, what should I do?</li>
Gabor Greif54820ce2009-03-02 19:08:05 +000051 <li>Compiling LLVM with GCC succeeds, but the resulting tools do not work, what can be wrong?</li>
John Criswelld1799612004-03-29 20:23:11 +000052 <li>When I use the test suite, all of the C Backend tests fail. What is
53 wrong?</li>
Reid Spencer669ed452007-07-09 08:04:31 +000054 <li>After Subversion update, rebuilding gives the error "No rule to make
Bill Wendling174d5782007-05-29 09:35:34 +000055 target".</li>
56 <li><a href="#llvmc">The <tt>llvmc</tt> program gives me errors/doesn't
Bill Wendlinge9a6c352007-09-22 09:54:47 +000057 work.</a></li>
Misha Brukmana6538852003-11-06 21:55:44 +000058 </ol></li>
John Criswell76c1e382003-11-18 16:08:49 +000059
Reid Spencer501bfee2006-04-26 14:52:19 +000060 <li><a href="#felangs">Source Languages</a>
61 <ol>
62 <li><a href="#langs">What source languages are supported?</a></li>
Gordon Henriksene5079052008-02-22 21:55:51 +000063 <li><a href="#langirgen">I'd like to write a self-hosting LLVM compiler. How
64 should I interface with the LLVM middle-end optimizers and back-end code
Dan Gohmanf0032762008-11-24 17:18:39 +000065 generators?</a></li>
Reid Spencer501bfee2006-04-26 14:52:19 +000066 <li><a href="#langhlsupp">What support is there for higher level source
67 language constructs for building a compiler?</a></li>
Reid Spencere00906f2006-08-10 20:15:58 +000068 <li><a href="GetElementPtr.html">I don't understand the GetElementPtr
69 instruction. Help!</a></li>
Reid Spencer501bfee2006-04-26 14:52:19 +000070 </ol>
71
Chris Lattnercc33d702003-11-19 05:53:12 +000072 <li><a href="#cfe">Using the GCC Front End</a>
John Criswell76c1e382003-11-18 16:08:49 +000073 <ol>
74 <li>
75 When I compile software that uses a configure script, the configure script
76 thinks my system has all of the header files and libraries it is testing
77 for. How do I get configure to work correctly?
78 </li>
79
80 <li>
81 When I compile code using the LLVM GCC front end, it complains that it
Chris Lattner7911ce22004-05-23 21:07:27 +000082 cannot find libcrtend.a.
John Criswell76c1e382003-11-18 16:08:49 +000083 </li>
Tanya Lattner14fc5c12005-04-25 20:36:56 +000084
85 <li>
86 How can I disable all optimizations when compiling code using the LLVM GCC front end?
87 </li>
88
Dan Gohmanf0032762008-11-24 17:18:39 +000089 <li><a href="#translatecxx">Can I use LLVM to convert C++ code to C code?</a></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +000090
Dan Gohmancfbcd592009-02-10 17:26:53 +000091 <li><a href="#platformindependent">Can I compile C or C++ code to platform-independent LLVM bitcode?</a></li>
92
John Criswell76c1e382003-11-18 16:08:49 +000093 </ol>
94 </li>
Chris Lattnercc33d702003-11-19 05:53:12 +000095
96 <li><a href="#cfe_code">Questions about code generated by the GCC front-end</a>
97 <ol>
Chris Lattner5a53c5d2005-02-25 20:30:21 +000098 <li><a href="#iosinit">What is this <tt>llvm.global_ctors</tt> and
Chris Lattnerc50bbc92004-03-29 19:14:35 +000099 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000100 #include &lt;iostream&gt;?</a></li>
101 <li><a href="#codedce">Where did all of my code go??</a></li>
102 <li><a href="#undef">What is this "<tt>undef</tt>" thing that shows up in my code?</a></li>
Chris Lattnercc33d702003-11-19 05:53:12 +0000103 </ol>
104 </li>
Misha Brukmana6538852003-11-06 21:55:44 +0000105</ol>
106
Chris Lattner7911ce22004-05-23 21:07:27 +0000107<div class="doc_author">
Reid Spencer05fe4b02006-03-14 05:39:39 +0000108 <p>Written by <a href="http://llvm.org">The LLVM Team</a></p>
Chris Lattner7911ce22004-05-23 21:07:27 +0000109</div>
110
111
Misha Brukmana6538852003-11-06 21:55:44 +0000112<!-- *********************************************************************** -->
113<div class="doc_section">
114 <a name="license">License</a>
115</div>
116<!-- *********************************************************************** -->
117
118<div class="question">
119<p>Why are the LLVM source code and the front-end distributed under different
120licenses?</p>
121</div>
122
123<div class="answer">
124<p>The C/C++ front-ends are based on GCC and must be distributed under the GPL.
125Our aim is to distribute LLVM source code under a <em>much less restrictive</em>
126license, in particular one that does not compel users who distribute tools based
127on modifying the source to redistribute the modified source code as well.</p>
128</div>
129
130<div class="question">
131<p>Does the University of Illinois Open Source License really qualify as an
132"open source" license?</p>
133</div>
134
135<div class="answer">
136<p>Yes, the license is <a
137href="http://www.opensource.org/licenses/UoI-NCSA.php">certified</a> by the Open
138Source Initiative (OSI).</p>
139</div>
140
141<div class="question">
142<p>Can I modify LLVM source code and redistribute the modified source?</p>
143</div>
144
145<div class="answer">
146<p>Yes. The modified source distribution must retain the copyright notice and
147follow the three bulletted conditions listed in the <a
Misha Brukmanb6106552008-12-17 18:06:53 +0000148href="http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT">LLVM license</a>.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000149</div>
150
151<div class="question">
152<p>Can I modify LLVM source code and redistribute binaries or other tools based
153on it, without redistributing the source?</p>
154</div>
155
156<div class="answer">
157<p>Yes, this is why we distribute LLVM under a less restrictive license than
158GPL, as explained in the first question above.</p>
159</div>
160
161<!-- *********************************************************************** -->
162<div class="doc_section">
163 <a name="source">Source Code</a>
164</div>
165<!-- *********************************************************************** -->
166
167<div class="question">
168<p>In what language is LLVM written?</p>
169</div>
170
171<div class="answer">
172<p>All of the LLVM tools and libraries are written in C++ with extensive use of
173the STL.</p>
174</div>
175
176<div class="question">
177<p>How portable is the LLVM source code?</p>
178</div>
179
180<div class="answer">
181<p>The LLVM source code should be portable to most modern UNIX-like operating
182systems. Most of the code is written in standard C++ with operating system
183services abstracted to a support library. The tools required to build and test
184LLVM have been ported to a plethora of platforms.</p>
185
186<p>Some porting problems may exist in the following areas:</p>
187
188<ul>
Misha Brukmana6538852003-11-06 21:55:44 +0000189 <li>The GCC front end code is not as portable as the LLVM suite, so it may not
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000190 compile as well on unsupported platforms.</li>
Misha Brukmana6538852003-11-06 21:55:44 +0000191
Misha Brukmana6538852003-11-06 21:55:44 +0000192 <li>The LLVM build system relies heavily on UNIX shell tools, like the Bourne
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000193 Shell and sed. Porting to systems without these tools (MacOS 9, Plan 9)
194 will require more effort.</li>
Misha Brukmana6538852003-11-06 21:55:44 +0000195</ul>
196
197</div>
198
199<!-- *********************************************************************** -->
200<div class="doc_section">
201 <a name="build">Build Problems</a>
202</div>
203<!-- *********************************************************************** -->
204
205<div class="question">
206<p>When I run configure, it finds the wrong C compiler.</p>
207</div>
208
209<div class="answer">
210
211<p>The <tt>configure</tt> script attempts to locate first <tt>gcc</tt> and then
212<tt>cc</tt>, unless it finds compiler paths set in <tt>CC</tt> and <tt>CXX</tt>
213for the C and C++ compiler, respectively.</p>
214
215<p>If <tt>configure</tt> finds the wrong compiler, either adjust your
216<tt>PATH</tt> environment variable or set <tt>CC</tt> and <tt>CXX</tt>
217explicitly.</p>
218
219</div>
220
221<div class="question">
Misha Brukmana6538852003-11-06 21:55:44 +0000222<p>The <tt>configure</tt> script finds the right C compiler, but it uses the
223LLVM linker from a previous build. What do I do?</p>
224</div>
225
226<div class="answer">
227<p>The <tt>configure</tt> script uses the <tt>PATH</tt> to find executables, so
228if it's grabbing the wrong linker/assembler/etc, there are two ways to fix
229it:</p>
230
231<ol>
Misha Brukmana6538852003-11-06 21:55:44 +0000232 <li><p>Adjust your <tt>PATH</tt> environment variable so that the correct
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000233 program appears first in the <tt>PATH</tt>. This may work, but may not be
234 convenient when you want them <i>first</i> in your path for other
235 work.</p></li>
Misha Brukmana6538852003-11-06 21:55:44 +0000236
237 <li><p>Run <tt>configure</tt> with an alternative <tt>PATH</tt> that is
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000238 correct. In a Borne compatible shell, the syntax would be:</p>
239
Misha Brukmana54d4b22008-12-17 18:11:40 +0000240<pre class="doc_code">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000241% PATH=[the path without the bad program] ./configure ...
242</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000243
244 <p>This is still somewhat inconvenient, but it allows <tt>configure</tt>
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000245 to do its work without having to adjust your <tt>PATH</tt>
246 permanently.</p></li>
Misha Brukmana6538852003-11-06 21:55:44 +0000247</ol>
248
249</div>
250
251<div class="question">
252<p>When creating a dynamic library, I get a strange GLIBC error.</p>
253</div>
254
255<div class="answer">
256<p>Under some operating systems (i.e. Linux), libtool does not work correctly if
257GCC was compiled with the --disable-shared option. To work around this, install
258your own version of GCC that has shared libraries enabled by default.</p>
259</div>
260
261<div class="question">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000262<p>I've updated my source tree from Subversion, and now my build is trying to
263use a file/directory that doesn't exist.</p>
Misha Brukmana6538852003-11-06 21:55:44 +0000264</div>
265
266<div class="answer">
267<p>You need to re-run configure in your object directory. When new Makefiles
268are added to the source tree, they have to be copied over to the object tree in
269order to be used by the build.</p>
270</div>
271
272<div class="question">
273<p>I've modified a Makefile in my source tree, but my build tree keeps using the
274old version. What do I do?</p>
275</div>
276
277<div class="answer">
Misha Brukmana6538852003-11-06 21:55:44 +0000278<p>If the Makefile already exists in your object tree, you
279can just run the following command in the top level directory of your object
280tree:</p>
281
Misha Brukmana54d4b22008-12-17 18:11:40 +0000282<pre class="doc_code">
283% ./config.status &lt;relative path to Makefile&gt;
284</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000285
286<p>If the Makefile is new, you will have to modify the configure script to copy
287it over.</p>
288
289</div>
290
291<div class="question">
292<p>I've upgraded to a new version of LLVM, and I get strange build errors.</p>
293</div>
294
295<div class="answer">
296
297<p>Sometimes, changes to the LLVM source code alters how the build system works.
298Changes in libtool, autoconf, or header file dependencies are especially prone
299to this sort of problem.</p>
300
301<p>The best thing to try is to remove the old files and re-build. In most
302cases, this takes care of the problem. To do this, just type <tt>make
303clean</tt> and then <tt>make</tt> in the directory that fails to build.</p>
304
305</div>
306
307<div class="question">
308<p>I've built LLVM and am testing it, but the tests freeze.</p>
309</div>
310
311<div class="answer">
312
313<p>This is most likely occurring because you built a profile or release
314(optimized) build of LLVM and have not specified the same information on the
315<tt>gmake</tt> command line.</p>
316
317<p>For example, if you built LLVM with the command:</p>
318
Misha Brukmana54d4b22008-12-17 18:11:40 +0000319<pre class="doc_code">
320% gmake ENABLE_PROFILING=1
321</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000322
323<p>...then you must run the tests with the following commands:</p>
324
Misha Brukmana54d4b22008-12-17 18:11:40 +0000325<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000326% cd llvm/test
327% gmake ENABLE_PROFILING=1
328</pre>
Misha Brukmana6538852003-11-06 21:55:44 +0000329
330</div>
331
332<div class="question">
333<p>Why do test results differ when I perform different types of builds?</p>
334</div>
335
336<div class="answer">
337
338<p>The LLVM test suite is dependent upon several features of the LLVM tools and
339libraries.</p>
340
341<p>First, the debugging assertions in code are not enabled in optimized or
342profiling builds. Hence, tests that used to fail may pass.</p>
343
344<p>Second, some tests may rely upon debugging options or behavior that is only
345available in the debug build. These tests will fail in an optimized or profile
346build.</p>
347
348</div>
349
Chris Lattner8a0b9242003-12-08 05:43:19 +0000350<div class="question">
Chris Lattner306acee2003-12-22 04:06:12 +0000351<p>Compiling LLVM with GCC 3.3.2 fails, what should I do?</p>
Chris Lattner8a0b9242003-12-08 05:43:19 +0000352</div>
353
354<div class="answer">
Gabor Greiff94ecc32009-03-02 19:11:53 +0000355<p>This is <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13392">a bug in GCC</a>, and
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000356affects projects other than LLVM. Try upgrading or downgrading your GCC.</p>
Chris Lattner8a0b9242003-12-08 05:43:19 +0000357</div>
358
John Criswelld1799612004-03-29 20:23:11 +0000359<div class="question">
Gabor Greif54820ce2009-03-02 19:08:05 +0000360<p>Compiling LLVM with GCC succeeds, but the resulting tools do not work, what can be wrong?</p>
361</div>
362
363<div class="answer">
364<p>Several versions of GCC have shown a weakness in miscompiling the LLVM codebase. Please
365consult your compiler version (<tt>gcc --version</tt>) to find out whether it is
366<a href="GettingStarted.html#brokengcc">broken</a>. If so, your only option is to upgrade
367GCC to a known good version.</p>
368</div>
369
370<div class="question">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000371<p>After Subversion update, rebuilding gives the error "No rule to make
372target".</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000373</div>
374
375<div class="answer">
376<p>If the error is of the form:</p>
377
Misha Brukmana54d4b22008-12-17 18:11:40 +0000378<pre class="doc_code">
Misha Brukman1739aec2004-09-09 16:36:47 +0000379gmake[2]: *** No rule to make target `/path/to/somefile', needed by
380`/path/to/another/file.d'.<br>
381Stop.
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000382</pre>
Misha Brukman1739aec2004-09-09 16:36:47 +0000383
Reid Spencer669ed452007-07-09 08:04:31 +0000384<p>This may occur anytime files are moved within the Subversion repository or
385removed entirely. In this case, the best solution is to erase all
386<tt>.d</tt> files, which list dependencies for source files, and rebuild:</p>
Misha Brukman1739aec2004-09-09 16:36:47 +0000387
Misha Brukmana54d4b22008-12-17 18:11:40 +0000388<pre class="doc_code">
Misha Brukman1739aec2004-09-09 16:36:47 +0000389% cd $LLVM_OBJ_DIR
390% rm -f `find . -name \*\.d`
391% gmake
392</pre>
Misha Brukman1739aec2004-09-09 16:36:47 +0000393
394<p>In other cases, it may be necessary to run <tt>make clean</tt> before
395rebuilding.</p>
396</div>
397
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000398<div class="question"><p><a name="llvmc">
399The <tt>llvmc</tt> program gives me errors/doesn't work.</a></p>
Bill Wendling174d5782007-05-29 09:35:34 +0000400</div>
401
402<div class="answer">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000403<p><tt>llvmc</tt> is experimental and isn't really supported. We suggest
404using <tt>llvm-gcc</tt> instead.</p>
Bill Wendling174d5782007-05-29 09:35:34 +0000405</div>
406
Misha Brukmana6538852003-11-06 21:55:44 +0000407<!-- *********************************************************************** -->
Reid Spencerc87f4972006-04-26 15:46:53 +0000408<div class="doc_section"><a name="felangs">Source Languages</a></div>
Reid Spencer501bfee2006-04-26 14:52:19 +0000409
Reid Spencerc87f4972006-04-26 15:46:53 +0000410<div class="question"><p>
411 <a name="langs">What source languages are supported?</a></p>
412</div>
413<div class="answer">
Reid Spencer501bfee2006-04-26 14:52:19 +0000414 <p>LLVM currently has full support for C and C++ source languages. These are
415 available through a special version of GCC that LLVM calls the
416 <a href="#cfe">C Front End</a></p>
417 <p>There is an incomplete version of a Java front end available in the
Reid Spencer669ed452007-07-09 08:04:31 +0000418 <tt>java</tt> module. There is no documentation on this yet so
Reid Spencer501bfee2006-04-26 14:52:19 +0000419 you'll need to download the code, compile it, and try it.</p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000420 <p>The PyPy developers are working on integrating LLVM into the PyPy backend
421 so that PyPy language can translate to LLVM.</p>
422</div>
Gordon Henriksen58366822008-02-22 20:58:29 +0000423
424<div class="question"><p><a name="langirgen">
Gordon Henriksene5079052008-02-22 21:55:51 +0000425 I'd like to write a self-hosting LLVM compiler. How should I interface with
426 the LLVM middle-end optimizers and back-end code generators?
Gordon Henriksen58366822008-02-22 20:58:29 +0000427</a></p></div>
428<div class="answer">
429 <p>Your compiler front-end will communicate with LLVM by creating a module in
Gordon Henriksene5079052008-02-22 21:55:51 +0000430 the LLVM intermediate representation (IR) format. Assuming you want to
431 write your language's compiler in the language itself (rather than C++),
432 there are 3 major ways to tackle generating LLVM IR from a front-end:</p>
Gordon Henriksen58366822008-02-22 20:58:29 +0000433 <ul>
434 <li>
435 <strong>Call into the LLVM libraries code using your language's FFI
436 (foreign function interface).</strong>
437 <ul>
438 <li><em>for:</em> best tracks changes to the LLVM IR, .ll syntax,
439 and .bc format</li>
440 <li><em>for:</em> enables running LLVM optimization passes without a
441 emit/parse overhead</li>
442 <li><em>for:</em> adapts well to a JIT context</li>
443 <li><em>against:</em> lots of ugly glue code to write</li>
444 </ul>
445 </li>
446 <li>
447 <strong>Emit LLVM assembly from your compiler's native language.</strong>
448 <ul>
449 <li><em>for:</em> very straightforward to get started</li>
450 <li><em>against:</em> the .ll parser is slower than the bitcode reader
451 when interfacing to the middle end</li>
452 <li><em>against:</em> you'll have to re-engineer the LLVM IR object
453 model and asm writer in your language</li>
454 <li><em>against:</em> it may be harder to track changes to the IR</li>
455 </ul>
456 </li>
457 <li>
458 <strong>Emit LLVM bitcode from your compiler's native language.</strong>
459 <ul>
460 <li><em>for:</em> can use the more-efficient bitcode reader when
461 interfacing to the middle end</li>
462 <li><em>against:</em> you'll have to re-engineer the LLVM IR object
463 model and bitcode writer in your language</li>
464 <li><em>against:</em> it may be harder to track changes to the IR</li>
465 </ul>
466 </li>
467 </ul>
468 <p>If you go with the first option, the C bindings in include/llvm-c should
469 help a lot, since most languages have strong support for interfacing with
470 C. The most common hurdle with calling C from managed code is interfacing
471 with the garbage collector. The C interface was designed to require very
472 little memory management, and so is straightforward in this regard.</p>
473</div>
474
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000475<div class="question"><p><a name="langhlsupp">
476 What support is there for a higher level source language constructs for
Reid Spencerc87f4972006-04-26 15:46:53 +0000477 building a compiler?</a></p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000478</div>
479<div class="answer">
480 <p>Currently, there isn't much. LLVM supports an intermediate representation
481 which is useful for code representation but will not support the high level
482 (abstract syntax tree) representation needed by most compilers. There are no
Jeff Cohen7b8229a2006-04-26 21:03:17 +0000483 facilities for lexical nor semantic analysis. There is, however, a <i>mostly
Reid Spencer501bfee2006-04-26 14:52:19 +0000484 implemented</i> configuration-driven
485 <a href="CompilerDriver.html">compiler driver</a> which simplifies the task
486 of running optimizations, linking, and executable generation.</p>
Reid Spencer501bfee2006-04-26 14:52:19 +0000487</div>
Chris Lattner33bef482006-08-15 00:43:35 +0000488
Dan Gohmanf0032762008-11-24 17:18:39 +0000489<div class="question"><p><a name="getelementptr">
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000490 I don't understand the GetElementPtr instruction. Help!</a></p>
Chris Lattner33bef482006-08-15 00:43:35 +0000491</div>
492<div class="answer">
493 <p>See <a href="GetElementPtr.html">The Often Misunderstood GEP
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000494 Instruction</a>.</p>
Chris Lattner33bef482006-08-15 00:43:35 +0000495</div>
496
Reid Spencer501bfee2006-04-26 14:52:19 +0000497<!-- *********************************************************************** -->
498<div class="doc_section">
Chris Lattnercc33d702003-11-19 05:53:12 +0000499 <a name="cfe">Using the GCC Front End</a>
John Criswell6ea30b02003-11-18 16:05:23 +0000500</div>
501
502<div class="question">
503<p>
504When I compile software that uses a configure script, the configure script
505thinks my system has all of the header files and libraries it is testing for.
506How do I get configure to work correctly?
507</p>
508</div>
509
510<div class="answer">
511<p>
512The configure script is getting things wrong because the LLVM linker allows
513symbols to be undefined at link time (so that they can be resolved during JIT
514or translation to the C back end). That is why configure thinks your system
515"has everything."
516</p>
517<p>
518To work around this, perform the following steps:
519</p>
John Criswell6ea30b02003-11-18 16:05:23 +0000520<ol>
Reid Spencer434262a2007-02-09 15:59:08 +0000521 <li>Make sure the CC and CXX environment variables contains the full path to
522 the LLVM GCC front end.</li>
John Criswell6ea30b02003-11-18 16:05:23 +0000523
Reid Spencer434262a2007-02-09 15:59:08 +0000524 <li>Make sure that the regular C compiler is first in your PATH. </li>
John Criswell6ea30b02003-11-18 16:05:23 +0000525
Reid Spencer434262a2007-02-09 15:59:08 +0000526 <li>Add the string "-Wl,-native" to your CFLAGS environment variable.</li>
John Criswell6ea30b02003-11-18 16:05:23 +0000527</ol>
528
529<p>
Reid Spencer434262a2007-02-09 15:59:08 +0000530This will allow the <tt>llvm-ld</tt> linker to create a native code executable
531instead of shell script that runs the JIT. Creating native code requires
532standard linkage, which in turn will allow the configure script to find out if
533code is not linking on your system because the feature isn't available on your
534system.</p>
John Criswell6ea30b02003-11-18 16:05:23 +0000535</div>
536
Chris Lattnercc33d702003-11-19 05:53:12 +0000537<div class="question">
John Criswell6ea30b02003-11-18 16:05:23 +0000538<p>
539When I compile code using the LLVM GCC front end, it complains that it cannot
John Criswellcd161192004-03-12 18:20:15 +0000540find libcrtend.a.
John Criswell6ea30b02003-11-18 16:05:23 +0000541</p>
542</div>
543
Chris Lattnercc33d702003-11-19 05:53:12 +0000544<div class="answer">
John Criswell6ea30b02003-11-18 16:05:23 +0000545<p>
Reid Spencerf96eb572004-12-15 00:14:01 +0000546The only way this can happen is if you haven't installed the runtime library. To
547correct this, do:</p>
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000548
Misha Brukmana54d4b22008-12-17 18:11:40 +0000549<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000550% cd llvm/runtime
551% make clean ; make install-bytecode
Reid Spencerf96eb572004-12-15 00:14:01 +0000552</pre>
John Criswell6ea30b02003-11-18 16:05:23 +0000553</div>
Chris Lattnercc33d702003-11-19 05:53:12 +0000554
Tanya Lattner14fc5c12005-04-25 20:36:56 +0000555<div class="question">
556<p>
557How can I disable all optimizations when compiling code using the LLVM GCC front end?
558</p>
559</div>
560
561<div class="answer">
562<p>
563Passing "-Wa,-disable-opt -Wl,-disable-opt" will disable *all* cleanup and
564optimizations done at the llvm level, leaving you with the truly horrible
565code that you desire.
566</p>
567</div>
Chris Lattnercc33d702003-11-19 05:53:12 +0000568
Chris Lattneraf7fd202006-07-19 18:19:59 +0000569
570<div class="question">
571<p>
Dan Gohmanf0032762008-11-24 17:18:39 +0000572<a name="translatecxx">Can I use LLVM to convert C++ code to C code?</a>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000573</p>
574</div>
575
576<div class="answer">
577<p>Yes, you can use LLVM to convert code from any language LLVM supports to C.
578Note that the generated C code will be very low level (all loops are lowered
579to gotos, etc) and not very pretty (comments are stripped, original source
580formatting is totally lost, variables are renamed, expressions are regrouped),
Dan Gohmand5b455f2009-01-25 16:04:50 +0000581so this may not be what you're looking for. Also, there are several
582limitations noted below.<p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000583
584<p>Use commands like this:</p>
585
586<ol>
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000587 <li><p>Compile your program as normal with llvm-g++:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000588
Misha Brukmana54d4b22008-12-17 18:11:40 +0000589<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000590% llvm-g++ x.cpp -o program
591</pre>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000592
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000593 <p>or:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000594
Misha Brukmana54d4b22008-12-17 18:11:40 +0000595<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000596% llvm-g++ a.cpp -c
597% llvm-g++ b.cpp -c
598% llvm-g++ a.o b.o -o program
599</pre>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000600
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000601 <p>With llvm-gcc3, this will generate program and program.bc. The .bc
602 file is the LLVM version of the program all linked together.</p></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000603
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000604 <li><p>Convert the LLVM code to C code, using the LLC tool with the C
605 backend:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000606
Misha Brukmana54d4b22008-12-17 18:11:40 +0000607<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000608% llc -march=c program.bc -o program.c
Misha Brukmana54d4b22008-12-17 18:11:40 +0000609</pre></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000610
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000611<li><p>Finally, compile the C file:</p>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000612
Misha Brukmana54d4b22008-12-17 18:11:40 +0000613<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000614% cc x.c
Misha Brukmana54d4b22008-12-17 18:11:40 +0000615</pre></li>
Chris Lattneraf7fd202006-07-19 18:19:59 +0000616
617</ol>
618
Dan Gohmand5b455f2009-01-25 16:04:50 +0000619<p>Using LLVM does not eliminate the need for C++ library support.
620If you use the llvm-g++ front-end, the generated code will depend on
621g++'s C++ support libraries in the same way that code generated from
622g++ would. If you use another C++ front-end, the generated code will
623depend on whatever library that front-end would normally require.</p>
Chris Lattnerb495fb02006-08-31 04:26:31 +0000624
Dan Gohmand5b455f2009-01-25 16:04:50 +0000625<p>If you are working on a platform that does not provide any C++
626libraries, you may be able to manually compile libstdc++ to LLVM
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000627bitcode, statically link it into your program, then use the commands above to
Dan Gohmand5b455f2009-01-25 16:04:50 +0000628convert the whole result into C code. Alternatively, you might compile the
Bill Wendlinge9a6c352007-09-22 09:54:47 +0000629libraries and your application into two different chunks of C code and link
630them.</p>
Chris Lattnerb495fb02006-08-31 04:26:31 +0000631
Dan Gohmand5b455f2009-01-25 16:04:50 +0000632<p>Note that, by default, the C back end does not support exception handling. If
633you want/need it for a certain program, you can enable it by passing
634"-enable-correct-eh-support" to the llc program. The resultant code will use
635setjmp/longjmp to implement exception support that is relatively slow, and
636not C++-ABI-conforming on most platforms, but otherwise correct.</p>
637
638<p>Also, there are a number of other limitations of the C backend that
639cause it to produce code that does not fully conform to the C++ ABI on
640most platforms. Some of the C++ programs in LLVM's test suite are known
641to fail when compiled with the C back end because of ABI incompatiblities
642with standard C++ libraries.</p>
643
Chris Lattneraf7fd202006-07-19 18:19:59 +0000644</div>
645
Dan Gohmancfbcd592009-02-10 17:26:53 +0000646<div class="question">
647<p>
648<a name="platformindependent">Can I compile C or C++ code to platform-independent LLVM bitcode?</a>
649</p>
650</div>
651
652<div class="answer">
653
654<p>No. C and C++ are inherently platform-dependent languages. The most
655obvious example of this is the preprocessor. A very common way that C code
656is made portable is by using the preprocessor to include platform-specific
657code. In practice, information about other platforms is lost after
658preprocessing, so the result is inherently dependent on the platform that
659the preprocessing was targetting.</p>
660
661<p>Another example is <tt>sizeof</tt>. It's common for <tt>sizeof(long)</tt>
662to vary between platforms. In most C front-ends, <tt>sizeof</tt> is expanded
663to a constant immediately, thus hardwaring a platform-specific detail.</p>
664
665<p>Also, since many platforms define their ABIs in terms of C, and since
666LLVM is lower-level than C, front-ends currently must emit platform-specific
667IR in order to have the result conform to the platform ABI.</p>
668
669</div>
670
Chris Lattnercc33d702003-11-19 05:53:12 +0000671<!-- *********************************************************************** -->
672<div class="doc_section">
673 <a name="cfe_code">Questions about code generated by the GCC front-end</a>
674</div>
675
Misha Brukman237dc2a2004-12-03 23:58:18 +0000676<div class="question">
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000677<a name="iosinit"></a>
Misha Brukman237dc2a2004-12-03 23:58:18 +0000678<p> What is this <tt>llvm.global_ctors</tt> and
679<tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I #include
680&lt;iostream&gt;?</p>
681</div>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000682
683<div class="answer">
Misha Brukman237dc2a2004-12-03 23:58:18 +0000684
685<p>If you #include the &lt;iostream&gt; header into a C++ translation unit, the
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000686file will probably use the <tt>std::cin</tt>/<tt>std::cout</tt>/... global
687objects. However, C++ does not guarantee an order of initialization between
688static objects in different translation units, so if a static ctor/dtor in your
689.cpp file used <tt>std::cout</tt>, for example, the object would not necessarily
Misha Brukman237dc2a2004-12-03 23:58:18 +0000690be automatically initialized before your use.</p>
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000691
Misha Brukman237dc2a2004-12-03 23:58:18 +0000692<p>To make <tt>std::cout</tt> and friends work correctly in these scenarios, the
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000693STL that we use declares a static object that gets created in every translation
Misha Brukman237dc2a2004-12-03 23:58:18 +0000694unit that includes <tt>&lt;iostream&gt;</tt>. This object has a static
695constructor and destructor that initializes and destroys the global iostream
696objects before they could possibly be used in the file. The code that you see
697in the .ll file corresponds to the constructor and destructor registration code.
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000698</p>
699
Misha Brukman237dc2a2004-12-03 23:58:18 +0000700<p>If you would like to make it easier to <b>understand</b> the LLVM code
701generated by the compiler in the demo page, consider using <tt>printf()</tt>
702instead of <tt>iostream</tt>s to print values.</p>
703
Chris Lattnerc50bbc92004-03-29 19:14:35 +0000704</div>
705
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000706<!--=========================================================================-->
707
708<div class="question"><p>
709<a name="codedce"></a>
710Where did all of my code go??
711</p></div>
712
713<div class="answer">
714<p>
715If you are using the LLVM demo page, you may often wonder what happened to all
716of the code that you typed in. Remember that the demo script is running the
717code through the LLVM optimizers, so if your code doesn't actually do anything
718useful, it might all be deleted.
719</p>
720
721<p>
722To prevent this, make sure that the code is actually needed. For example, if
723you are computing some expression, return the value from the function instead of
724leaving it in a local variable. If you really want to constrain the optimizer,
725you can read from and assign to <tt>volatile</tt> global variables.
726</p>
727</div>
728
729<!--=========================================================================-->
730
731<div class="question"><p>
732<a name="undef"></a>
733<p>What is this "<tt>undef</tt>" thing that shows up in my code?
734</p></div>
735
736<div class="answer">
737<p>
738<a href="LangRef.html#undef"><tt>undef</tt></a> is the LLVM way of representing
739a value that is not defined. You can get these if you do not initialize a
740variable before you use it. For example, the C function:</p>
741
Misha Brukmana54d4b22008-12-17 18:11:40 +0000742<pre class="doc_code">
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000743int X() { int i; return i; }
744</pre>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000745
Bill Wendlingd6a68eb2007-05-29 09:24:33 +0000746<p>Is compiled to "<tt>ret i32 undef</tt>" because "<tt>i</tt>" never has
747a value specified for it.</p>
Chris Lattner5a53c5d2005-02-25 20:30:21 +0000748</div>
749
John Criswell6ea30b02003-11-18 16:05:23 +0000750<!-- *********************************************************************** -->
John Criswellc310f622003-10-13 16:13:06 +0000751
752<hr>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000753<address>
754 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000755 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000756 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000757 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000758
Reid Spencer05fe4b02006-03-14 05:39:39 +0000759 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
Misha Brukmana6538852003-11-06 21:55:44 +0000760 Last modified: $Date$
Misha Brukman7ce62cc2004-06-01 18:51:03 +0000761</address>
John Criswellf08c5d82003-10-24 22:48:20 +0000762
John Criswellc310f622003-10-13 16:13:06 +0000763</body>
764</html>