blob: 3be059891320a23700f34519d7d9c899b6641dda [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
Bill Wendlingc0e81142009-04-07 18:40:56 +00005 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006 <title>LLVM: Frequently Asked Questions</title>
7 <style type="text/css">
8 @import url("llvm.css");
9 .question { font-weight: bold }
10 .answer { margin-left: 2em }
11 </style>
12</head>
13<body>
14
15<div class="doc_title">
16 LLVM: Frequently Asked Questions
17</div>
18
19<ol>
20 <li><a href="#license">License</a>
21 <ol>
Bill Wendlingc0e81142009-04-07 18:40:56 +000022 <li>Why are the LLVM source code and the front-end distributed under
23 different licenses?</li>
24
25 <li>Does the University of Illinois Open Source License really qualify as an
26 "open source" license?</li>
27
28 <li>Can I modify LLVM source code and redistribute the modified source?</li>
29
30 <li>Can I modify LLVM source code and redistribute binaries or other tools
31 based on it, without redistributing the source?</li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 </ol></li>
33
34 <li><a href="#source">Source code</a>
35 <ol>
Bill Wendlingc0e81142009-04-07 18:40:56 +000036 <li>In what language is LLVM written?</li>
37
38 <li>How portable is the LLVM source code?</li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 </ol></li>
40
41 <li><a href="#build">Build Problems</a>
42 <ol>
Bill Wendlingc0e81142009-04-07 18:40:56 +000043 <li>When I run configure, it finds the wrong C compiler.</li>
44
45 <li>The <tt>configure</tt> script finds the right C compiler, but it uses
46 the LLVM linker from a previous build. What do I do?</li>
47
48 <li>When creating a dynamic library, I get a strange GLIBC error.</li>
49
50 <li>I've updated my source tree from Subversion, and now my build is trying
51 to use a file/directory that doesn't exist.</li>
52
53 <li>I've modified a Makefile in my source tree, but my build tree keeps
54 using the old version. What do I do?</li>
55
56 <li>I've upgraded to a new version of LLVM, and I get strange build
57 errors.</li>
58
59 <li>I've built LLVM and am testing it, but the tests freeze.</li>
60
61 <li>Why do test results differ when I perform different types of
62 builds?</li>
63
64 <li>Compiling LLVM with GCC 3.3.2 fails, what should I do?</li>
65
66 <li>Compiling LLVM with GCC succeeds, but the resulting tools do not work,
67 what can be wrong?</li>
68
69 <li>When I use the test suite, all of the C Backend tests fail. What is
70 wrong?</li>
71
72 <li>After Subversion update, rebuilding gives the error "No rule to make
73 target".</li>
74
75 <li><a href="#llvmc">The <tt>llvmc</tt> program gives me errors/doesn't
76 work.</a></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 </ol></li>
78
79 <li><a href="#felangs">Source Languages</a>
80 <ol>
81 <li><a href="#langs">What source languages are supported?</a></li>
Bill Wendlingc0e81142009-04-07 18:40:56 +000082
Gordon Henriksendb0558b2008-02-22 21:55:51 +000083 <li><a href="#langirgen">I'd like to write a self-hosting LLVM compiler. How
Bill Wendlingc0e81142009-04-07 18:40:56 +000084 should I interface with the LLVM middle-end optimizers and back-end code
85 generators?</a></li>
86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 <li><a href="#langhlsupp">What support is there for higher level source
Bill Wendlingc0e81142009-04-07 18:40:56 +000088 language constructs for building a compiler?</a></li>
89
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 <li><a href="GetElementPtr.html">I don't understand the GetElementPtr
91 instruction. Help!</a></li>
92 </ol>
93
94 <li><a href="#cfe">Using the GCC Front End</a>
95 <ol>
Bill Wendlingc0e81142009-04-07 18:40:56 +000096 <li>When I compile software that uses a configure script, the configure
97 script thinks my system has all of the header files and libraries it is
98 testing for. How do I get configure to work correctly?</li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
Bill Wendlingc0e81142009-04-07 18:40:56 +0000100 <li>When I compile code using the LLVM GCC front end, it complains that it
101 cannot find libcrtend.a?</li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
Bill Wendlingc0e81142009-04-07 18:40:56 +0000103 <li>How can I disable all optimizations when compiling code using the LLVM
104 GCC front end?</li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
Bill Wendlingc0e81142009-04-07 18:40:56 +0000106 <li><a href="#translatecxx">Can I use LLVM to convert C++ code to C
107 code?</a></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
Bill Wendlingc0e81142009-04-07 18:40:56 +0000109 <li><a href="#platformindependent">Can I compile C or C++ code to
110 platform-independent LLVM bitcode?</a></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 </ol>
112 </li>
113
114 <li><a href="#cfe_code">Questions about code generated by the GCC front-end</a>
115 <ol>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 <li><a href="#iosinit">What is this <tt>llvm.global_ctors</tt> and
117 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I
118 #include &lt;iostream&gt;?</a></li>
Bill Wendlingc0e81142009-04-07 18:40:56 +0000119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 <li><a href="#codedce">Where did all of my code go??</a></li>
Bill Wendlingc0e81142009-04-07 18:40:56 +0000121
122 <li><a href="#undef">What is this "<tt>undef</tt>" thing that shows up in
123 my code?</a></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 </ol>
125 </li>
126</ol>
127
128<div class="doc_author">
129 <p>Written by <a href="http://llvm.org">The LLVM Team</a></p>
130</div>
131
132
133<!-- *********************************************************************** -->
134<div class="doc_section">
135 <a name="license">License</a>
136</div>
137<!-- *********************************************************************** -->
138
139<div class="question">
140<p>Why are the LLVM source code and the front-end distributed under different
Bill Wendlingc0e81142009-04-07 18:40:56 +0000141 licenses?</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142</div>
143
144<div class="answer">
145<p>The C/C++ front-ends are based on GCC and must be distributed under the GPL.
Bill Wendlingc0e81142009-04-07 18:40:56 +0000146 Our aim is to distribute LLVM source code under a <em>much less
147 restrictive</em> license, in particular one that does not compel users who
148 distribute tools based on modifying the source to redistribute the modified
149 source code as well.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150</div>
151
152<div class="question">
153<p>Does the University of Illinois Open Source License really qualify as an
Bill Wendlingc0e81142009-04-07 18:40:56 +0000154 "open source" license?</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155</div>
156
157<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000158<p>Yes, the license
159 is <a href="http://www.opensource.org/licenses/UoI-NCSA.php">certified</a> by
160 the Open Source Initiative (OSI).</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161</div>
162
163<div class="question">
164<p>Can I modify LLVM source code and redistribute the modified source?</p>
165</div>
166
167<div class="answer">
168<p>Yes. The modified source distribution must retain the copyright notice and
Bill Wendlingc0e81142009-04-07 18:40:56 +0000169 follow the three bulletted conditions listed in
170 the <a href="http://llvm.org/svn/llvm-project/llvm/trunk/LICENSE.TXT">LLVM
171 license</a>.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172</div>
173
174<div class="question">
175<p>Can I modify LLVM source code and redistribute binaries or other tools based
Bill Wendlingc0e81142009-04-07 18:40:56 +0000176 on it, without redistributing the source?</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177</div>
178
179<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000180<p>Yes. This is why we distribute LLVM under a less restrictive license than
181 GPL, as explained in the first question above.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182</div>
183
184<!-- *********************************************************************** -->
185<div class="doc_section">
186 <a name="source">Source Code</a>
187</div>
188<!-- *********************************************************************** -->
189
190<div class="question">
191<p>In what language is LLVM written?</p>
192</div>
193
194<div class="answer">
195<p>All of the LLVM tools and libraries are written in C++ with extensive use of
Bill Wendlingc0e81142009-04-07 18:40:56 +0000196 the STL.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197</div>
198
199<div class="question">
200<p>How portable is the LLVM source code?</p>
201</div>
202
203<div class="answer">
204<p>The LLVM source code should be portable to most modern UNIX-like operating
205systems. Most of the code is written in standard C++ with operating system
206services abstracted to a support library. The tools required to build and test
207LLVM have been ported to a plethora of platforms.</p>
208
209<p>Some porting problems may exist in the following areas:</p>
210
211<ul>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 <li>The GCC front end code is not as portable as the LLVM suite, so it may not
Bill Wendling4e05864c2007-09-22 09:54:47 +0000213 compile as well on unsupported platforms.</li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214
215 <li>The LLVM build system relies heavily on UNIX shell tools, like the Bourne
Bill Wendling4e05864c2007-09-22 09:54:47 +0000216 Shell and sed. Porting to systems without these tools (MacOS 9, Plan 9)
217 will require more effort.</li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218</ul>
219
220</div>
221
222<!-- *********************************************************************** -->
223<div class="doc_section">
224 <a name="build">Build Problems</a>
225</div>
226<!-- *********************************************************************** -->
227
228<div class="question">
229<p>When I run configure, it finds the wrong C compiler.</p>
230</div>
231
232<div class="answer">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233<p>The <tt>configure</tt> script attempts to locate first <tt>gcc</tt> and then
Bill Wendlingc0e81142009-04-07 18:40:56 +0000234 <tt>cc</tt>, unless it finds compiler paths set in <tt>CC</tt>
235 and <tt>CXX</tt> for the C and C++ compiler, respectively.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236
237<p>If <tt>configure</tt> finds the wrong compiler, either adjust your
Bill Wendlingc0e81142009-04-07 18:40:56 +0000238 <tt>PATH</tt> environment variable or set <tt>CC</tt> and <tt>CXX</tt>
239 explicitly.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240
241</div>
242
243<div class="question">
244<p>The <tt>configure</tt> script finds the right C compiler, but it uses the
Bill Wendlingc0e81142009-04-07 18:40:56 +0000245 LLVM linker from a previous build. What do I do?</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246</div>
247
248<div class="answer">
249<p>The <tt>configure</tt> script uses the <tt>PATH</tt> to find executables, so
Bill Wendlingc0e81142009-04-07 18:40:56 +0000250 if it's grabbing the wrong linker/assembler/etc, there are two ways to fix
251 it:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252
253<ol>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 <li><p>Adjust your <tt>PATH</tt> environment variable so that the correct
Bill Wendling4e05864c2007-09-22 09:54:47 +0000255 program appears first in the <tt>PATH</tt>. This may work, but may not be
256 convenient when you want them <i>first</i> in your path for other
257 work.</p></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258
259 <li><p>Run <tt>configure</tt> with an alternative <tt>PATH</tt> that is
Bill Wendling4e05864c2007-09-22 09:54:47 +0000260 correct. In a Borne compatible shell, the syntax would be:</p>
261
Misha Brukman4fbcf682008-12-17 18:11:40 +0000262<pre class="doc_code">
Bill Wendling4e05864c2007-09-22 09:54:47 +0000263% PATH=[the path without the bad program] ./configure ...
264</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265
266 <p>This is still somewhat inconvenient, but it allows <tt>configure</tt>
Bill Wendling4e05864c2007-09-22 09:54:47 +0000267 to do its work without having to adjust your <tt>PATH</tt>
268 permanently.</p></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269</ol>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270</div>
271
272<div class="question">
273<p>When creating a dynamic library, I get a strange GLIBC error.</p>
274</div>
275
276<div class="answer">
277<p>Under some operating systems (i.e. Linux), libtool does not work correctly if
Bill Wendlingc0e81142009-04-07 18:40:56 +0000278 GCC was compiled with the --disable-shared option. To work around this,
279 install your own version of GCC that has shared libraries enabled by
280 default.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281</div>
282
283<div class="question">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000284<p>I've updated my source tree from Subversion, and now my build is trying to
285 use a file/directory that doesn't exist.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286</div>
287
288<div class="answer">
289<p>You need to re-run configure in your object directory. When new Makefiles
Bill Wendlingc0e81142009-04-07 18:40:56 +0000290 are added to the source tree, they have to be copied over to the object tree
291 in order to be used by the build.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292</div>
293
294<div class="question">
295<p>I've modified a Makefile in my source tree, but my build tree keeps using the
Bill Wendlingc0e81142009-04-07 18:40:56 +0000296 old version. What do I do?</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297</div>
298
299<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000300<p>If the Makefile already exists in your object tree, you can just run the
301 following command in the top level directory of your object tree:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302
Misha Brukman4fbcf682008-12-17 18:11:40 +0000303<pre class="doc_code">
304% ./config.status &lt;relative path to Makefile&gt;
305</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
307<p>If the Makefile is new, you will have to modify the configure script to copy
Bill Wendlingc0e81142009-04-07 18:40:56 +0000308 it over.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309</div>
310
311<div class="question">
312<p>I've upgraded to a new version of LLVM, and I get strange build errors.</p>
313</div>
314
315<div class="answer">
316
317<p>Sometimes, changes to the LLVM source code alters how the build system works.
Bill Wendlingc0e81142009-04-07 18:40:56 +0000318 Changes in libtool, autoconf, or header file dependencies are especially
319 prone to this sort of problem.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320
321<p>The best thing to try is to remove the old files and re-build. In most
Bill Wendlingc0e81142009-04-07 18:40:56 +0000322 cases, this takes care of the problem. To do this, just type <tt>make
323 clean</tt> and then <tt>make</tt> in the directory that fails to build.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324</div>
325
326<div class="question">
327<p>I've built LLVM and am testing it, but the tests freeze.</p>
328</div>
329
330<div class="answer">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331<p>This is most likely occurring because you built a profile or release
Bill Wendlingc0e81142009-04-07 18:40:56 +0000332 (optimized) build of LLVM and have not specified the same information on the
333 <tt>gmake</tt> command line.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334
335<p>For example, if you built LLVM with the command:</p>
336
Misha Brukman4fbcf682008-12-17 18:11:40 +0000337<pre class="doc_code">
338% gmake ENABLE_PROFILING=1
339</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340
341<p>...then you must run the tests with the following commands:</p>
342
Misha Brukman4fbcf682008-12-17 18:11:40 +0000343<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344% cd llvm/test
345% gmake ENABLE_PROFILING=1
346</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347</div>
348
349<div class="question">
350<p>Why do test results differ when I perform different types of builds?</p>
351</div>
352
353<div class="answer">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354<p>The LLVM test suite is dependent upon several features of the LLVM tools and
Bill Wendlingc0e81142009-04-07 18:40:56 +0000355 libraries.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356
357<p>First, the debugging assertions in code are not enabled in optimized or
Bill Wendlingc0e81142009-04-07 18:40:56 +0000358 profiling builds. Hence, tests that used to fail may pass.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359
360<p>Second, some tests may rely upon debugging options or behavior that is only
Bill Wendlingc0e81142009-04-07 18:40:56 +0000361 available in the debug build. These tests will fail in an optimized or
362 profile build.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363</div>
364
365<div class="question">
366<p>Compiling LLVM with GCC 3.3.2 fails, what should I do?</p>
367</div>
368
369<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000370<p>This is <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13392">a bug in
371 GCC</a>, and affects projects other than LLVM. Try upgrading or downgrading
372 your GCC.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373</div>
374
375<div class="question">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000376<p>Compiling LLVM with GCC succeeds, but the resulting tools do not work, what
377 can be wrong?</p>
Gabor Greif5b6141c2009-03-02 19:08:05 +0000378</div>
379
380<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000381<p>Several versions of GCC have shown a weakness in miscompiling the LLVM
382 codebase. Please consult your compiler version (<tt>gcc --version</tt>) to
383 find out whether it is <a href="GettingStarted.html#brokengcc">broken</a>.
384 If so, your only option is to upgrade GCC to a known good version.</p>
Gabor Greif5b6141c2009-03-02 19:08:05 +0000385</div>
386
387<div class="question">
Bill Wendling4e05864c2007-09-22 09:54:47 +0000388<p>After Subversion update, rebuilding gives the error "No rule to make
Bill Wendlingc0e81142009-04-07 18:40:56 +0000389 target".</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390</div>
391
392<div class="answer">
393<p>If the error is of the form:</p>
394
Misha Brukman4fbcf682008-12-17 18:11:40 +0000395<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396gmake[2]: *** No rule to make target `/path/to/somefile', needed by
397`/path/to/another/file.d'.<br>
398Stop.
399</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400
Bill Wendlingc0e81142009-04-07 18:40:56 +0000401<p>This may occur anytime files are moved within the Subversion repository or
402 removed entirely. In this case, the best solution is to erase all
403 <tt>.d</tt> files, which list dependencies for source files, and rebuild:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404
Misha Brukman4fbcf682008-12-17 18:11:40 +0000405<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406% cd $LLVM_OBJ_DIR
407% rm -f `find . -name \*\.d`
408% gmake
409</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410
411<p>In other cases, it may be necessary to run <tt>make clean</tt> before
Bill Wendlingc0e81142009-04-07 18:40:56 +0000412 rebuilding.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413</div>
414
Bill Wendling4e05864c2007-09-22 09:54:47 +0000415<div class="question"><p><a name="llvmc">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000416<p>The <tt>llvmc</tt> program gives me errors/doesn't work.</a></p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417</div>
418
419<div class="answer">
Bill Wendling4e05864c2007-09-22 09:54:47 +0000420<p><tt>llvmc</tt> is experimental and isn't really supported. We suggest
Bill Wendlingc0e81142009-04-07 18:40:56 +0000421 using <tt>llvm-gcc</tt> instead.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422</div>
423
424<!-- *********************************************************************** -->
425<div class="doc_section"><a name="felangs">Source Languages</a></div>
426
Bill Wendlingc0e81142009-04-07 18:40:56 +0000427<div class="question">
428<p><a name="langs">What source languages are supported?</a></p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429</div>
Gordon Henriksen2c2e1482008-02-22 20:58:29 +0000430
Gordon Henriksen2c2e1482008-02-22 20:58:29 +0000431<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000432<p>LLVM currently has full support for C and C++ source languages. These are
433 available through a special version of GCC that LLVM calls the
434 <a href="#cfe">C Front End</a></p>
435
436<p>There is an incomplete version of a Java front end available in the
437 <tt>java</tt> module. There is no documentation on this yet so you'll need to
438 download the code, compile it, and try it.</p>
439
440<p>The PyPy developers are working on integrating LLVM into the PyPy backend so
441 that PyPy language can translate to LLVM.</p>
Gordon Henriksen2c2e1482008-02-22 20:58:29 +0000442</div>
443
Bill Wendlingc0e81142009-04-07 18:40:56 +0000444<div class="question">
445<p><a name="langirgen">I'd like to write a self-hosting LLVM compiler. How
446 should I interface with the LLVM middle-end optimizers and back-end code
447 generators?</a></p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448</div>
449
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000451<p>Your compiler front-end will communicate with LLVM by creating a module in
452 the LLVM intermediate representation (IR) format. Assuming you want to write
453 your language's compiler in the language itself (rather than C++), there are
454 3 major ways to tackle generating LLVM IR from a front-end:</p>
455
456<ul>
457 <li><strong>Call into the LLVM libraries code using your language's FFI
458 (foreign function interface).</strong>
459
460 <ul>
461 <li><em>for:</em> best tracks changes to the LLVM IR, .ll syntax, and .bc
462 format</li>
463
464 <li><em>for:</em> enables running LLVM optimization passes without a
465 emit/parse overhead</li>
466
467 <li><em>for:</em> adapts well to a JIT context</li>
468
469 <li><em>against:</em> lots of ugly glue code to write</li>
470 </ul></li>
471
472 <li> <strong>Emit LLVM assembly from your compiler's native language.</strong>
473 <ul>
474 <li><em>for:</em> very straightforward to get started</li>
475
476 <li><em>against:</em> the .ll parser is slower than the bitcode reader
477 when interfacing to the middle end</li>
478
479 <li><em>against:</em> you'll have to re-engineer the LLVM IR object model
480 and asm writer in your language</li>
481
482 <li><em>against:</em> it may be harder to track changes to the IR</li>
483 </ul></li>
484
485 <li><strong>Emit LLVM bitcode from your compiler's native language.</strong>
486
487 <ul>
488 <li><em>for:</em> can use the more-efficient bitcode reader when
489 interfacing to the middle end</li>
490
491 <li><em>against:</em> you'll have to re-engineer the LLVM IR object
492 model and bitcode writer in your language</li>
493
494 <li><em>against:</em> it may be harder to track changes to the IR</li>
495 </ul></li>
496</ul>
497
498<p>If you go with the first option, the C bindings in include/llvm-c should help
499 a lot, since most languages have strong support for interfacing with C. The
500 most common hurdle with calling C from managed code is interfacing with the
501 garbage collector. The C interface was designed to require very little memory
502 management, and so is straightforward in this regard.</p>
503</div>
504
505<div class="question">
506<p><a name="langhlsupp">What support is there for a higher level source language
507 constructs for building a compiler?</a></p>
508</div>
509
510<div class="answer">
511<p>Currently, there isn't much. LLVM supports an intermediate representation
512 which is useful for code representation but will not support the high level
513 (abstract syntax tree) representation needed by most compilers. There are no
514 facilities for lexical nor semantic analysis. There is, however, a <i>mostly
515 implemented</i> configuration-driven
516 <a href="CompilerDriver.html">compiler driver</a> which simplifies the task
517 of running optimizations, linking, and executable generation.</p>
518</div>
519
520<div class="question">
521<p><a name="getelementptr">I don't understand the GetElementPtr
522 instruction. Help!</a></p>
523</div>
524
525<div class="answer">
526<p>See <a href="GetElementPtr.html">The Often Misunderstood GEP
Bill Wendling4e05864c2007-09-22 09:54:47 +0000527 Instruction</a>.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528</div>
529
530<!-- *********************************************************************** -->
531<div class="doc_section">
532 <a name="cfe">Using the GCC Front End</a>
533</div>
534
535<div class="question">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000536<p>When I compile software that uses a configure script, the configure script
537 thinks my system has all of the header files and libraries it is testing for.
538 How do I get configure to work correctly?</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539</div>
540
541<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000542<p>The configure script is getting things wrong because the LLVM linker allows
543 symbols to be undefined at link time (so that they can be resolved during JIT
544 or translation to the C back end). That is why configure thinks your system
545 "has everything."</p>
546
547<p>To work around this, perform the following steps:</p>
548
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549<ol>
Bill Wendlingc0e81142009-04-07 18:40:56 +0000550 <li>Make sure the CC and CXX environment variables contains the full path to
551 the LLVM GCC front end.</li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552
553 <li>Make sure that the regular C compiler is first in your PATH. </li>
554
555 <li>Add the string "-Wl,-native" to your CFLAGS environment variable.</li>
556</ol>
557
Bill Wendlingc0e81142009-04-07 18:40:56 +0000558<p>This will allow the <tt>llvm-ld</tt> linker to create a native code
559 executable instead of shell script that runs the JIT. Creating native code
560 requires standard linkage, which in turn will allow the configure script to
561 find out if code is not linking on your system because the feature isn't
562 available on your system.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563</div>
564
565<div class="question">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000566<p>When I compile code using the LLVM GCC front end, it complains that it cannot
567 find libcrtend.a.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568</p>
569</div>
570
571<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000572<p>The only way this can happen is if you haven't installed the runtime
573 library. To correct this, do:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574
Misha Brukman4fbcf682008-12-17 18:11:40 +0000575<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576% cd llvm/runtime
577% make clean ; make install-bytecode
578</pre>
579</div>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580
581<div class="question">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000582<p>How can I disable all optimizations when compiling code using the LLVM GCC
583 front end?</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584</div>
585
586<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000587<p>Passing "-Wa,-disable-opt -Wl,-disable-opt" will disable *all* cleanup and
588 optimizations done at the llvm level, leaving you with the truly horrible
589 code that you desire.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590</div>
591
592
593<div class="question">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000594<p><a name="translatecxx">Can I use LLVM to convert C++ code to C code?</a></p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595</div>
596
597<div class="answer">
598<p>Yes, you can use LLVM to convert code from any language LLVM supports to C.
Bill Wendlingc0e81142009-04-07 18:40:56 +0000599 Note that the generated C code will be very low level (all loops are lowered
600 to gotos, etc) and not very pretty (comments are stripped, original source
601 formatting is totally lost, variables are renamed, expressions are
602 regrouped), so this may not be what you're looking for. Also, there are
603 several limitations noted below.<p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604
605<p>Use commands like this:</p>
606
607<ol>
Bill Wendling4e05864c2007-09-22 09:54:47 +0000608 <li><p>Compile your program as normal with llvm-g++:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609
Misha Brukman4fbcf682008-12-17 18:11:40 +0000610<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611% llvm-g++ x.cpp -o program
612</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613
Bill Wendlingc0e81142009-04-07 18:40:56 +0000614 <p>or:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615
Misha Brukman4fbcf682008-12-17 18:11:40 +0000616<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617% llvm-g++ a.cpp -c
618% llvm-g++ b.cpp -c
619% llvm-g++ a.o b.o -o program
620</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621
Bill Wendling4e05864c2007-09-22 09:54:47 +0000622 <p>With llvm-gcc3, this will generate program and program.bc. The .bc
623 file is the LLVM version of the program all linked together.</p></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624
Bill Wendling4e05864c2007-09-22 09:54:47 +0000625 <li><p>Convert the LLVM code to C code, using the LLC tool with the C
626 backend:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627
Misha Brukman4fbcf682008-12-17 18:11:40 +0000628<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629% llc -march=c program.bc -o program.c
Misha Brukman4fbcf682008-12-17 18:11:40 +0000630</pre></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631
Bill Wendlingc0e81142009-04-07 18:40:56 +0000632 <li><p>Finally, compile the C file:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633
Misha Brukman4fbcf682008-12-17 18:11:40 +0000634<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635% cc x.c
Misha Brukman4fbcf682008-12-17 18:11:40 +0000636</pre></li>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637
638</ol>
639
Bill Wendlingc0e81142009-04-07 18:40:56 +0000640<p>Using LLVM does not eliminate the need for C++ library support. If you use
641 the llvm-g++ front-end, the generated code will depend on g++'s C++ support
642 libraries in the same way that code generated from g++ would. If you use
643 another C++ front-end, the generated code will depend on whatever library
644 that front-end would normally require.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645
Bill Wendlingc0e81142009-04-07 18:40:56 +0000646<p>If you are working on a platform that does not provide any C++ libraries, you
647 may be able to manually compile libstdc++ to LLVM bitcode, statically link it
648 into your program, then use the commands above to convert the whole result
649 into C code. Alternatively, you might compile the libraries and your
650 application into two different chunks of C code and link them.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651
Bill Wendlingc0e81142009-04-07 18:40:56 +0000652<p>Note that, by default, the C back end does not support exception handling.
653 If you want/need it for a certain program, you can enable it by passing
654 "-enable-correct-eh-support" to the llc program. The resultant code will use
655 setjmp/longjmp to implement exception support that is relatively slow, and
656 not C++-ABI-conforming on most platforms, but otherwise correct.</p>
djga0237ec2009-01-25 16:04:50 +0000657
Bill Wendlingc0e81142009-04-07 18:40:56 +0000658<p>Also, there are a number of other limitations of the C backend that cause it
659 to produce code that does not fully conform to the C++ ABI on most
660 platforms. Some of the C++ programs in LLVM's test suite are known to fail
661 when compiled with the C back end because of ABI incompatiblities with
662 standard C++ libraries.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663</div>
664
Dan Gohmanb8e23ab2009-02-10 17:26:53 +0000665<div class="question">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000666<p><a name="platformindependent">Can I compile C or C++ code to
667 platform-independent LLVM bitcode?</a></p>
Dan Gohmanb8e23ab2009-02-10 17:26:53 +0000668</div>
669
670<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000671<p>No. C and C++ are inherently platform-dependent languages. The most obvious
672 example of this is the preprocessor. A very common way that C code is made
673 portable is by using the preprocessor to include platform-specific code. In
674 practice, information about other platforms is lost after preprocessing, so
675 the result is inherently dependent on the platform that the preprocessing was
676 targetting.</p>
Dan Gohmanb8e23ab2009-02-10 17:26:53 +0000677
Bill Wendlingc0e81142009-04-07 18:40:56 +0000678<p>Another example is <tt>sizeof</tt>. It's common for <tt>sizeof(long)</tt> to
679 vary between platforms. In most C front-ends, <tt>sizeof</tt> is expanded to
680 a constant immediately, thus hardwaring a platform-specific detail.</p>
Dan Gohmanb8e23ab2009-02-10 17:26:53 +0000681
Bill Wendlingc0e81142009-04-07 18:40:56 +0000682<p>Also, since many platforms define their ABIs in terms of C, and since LLVM is
683 lower-level than C, front-ends currently must emit platform-specific IR in
684 order to have the result conform to the platform ABI.</p>
Dan Gohmanb8e23ab2009-02-10 17:26:53 +0000685</div>
686
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687<!-- *********************************************************************** -->
688<div class="doc_section">
689 <a name="cfe_code">Questions about code generated by the GCC front-end</a>
690</div>
691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692<div class="question">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000693<p><a name="iosinit">What is this <tt>llvm.global_ctors</tt> and
694 <tt>_GLOBAL__I__tmp_webcompile...</tt> stuff that happens when I <tt>#include
695 &lt;iostream&gt;</tt>?</a></p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696</div>
697
698<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000699<p>If you <tt>#include</tt> the <tt>&lt;iostream&gt;</tt> header into a C++
700 translation unit, the file will probably use
701 the <tt>std::cin</tt>/<tt>std::cout</tt>/... global objects. However, C++
702 does not guarantee an order of initialization between static objects in
703 different translation units, so if a static ctor/dtor in your .cpp file
704 used <tt>std::cout</tt>, for example, the object would not necessarily be
705 automatically initialized before your use.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706
707<p>To make <tt>std::cout</tt> and friends work correctly in these scenarios, the
Bill Wendlingc0e81142009-04-07 18:40:56 +0000708 STL that we use declares a static object that gets created in every
709 translation unit that includes <tt>&lt;iostream&gt;</tt>. This object has a
710 static constructor and destructor that initializes and destroys the global
711 iostream objects before they could possibly be used in the file. The code
712 that you see in the .ll file corresponds to the constructor and destructor
713 registration code.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714</p>
715
716<p>If you would like to make it easier to <b>understand</b> the LLVM code
Bill Wendlingc0e81142009-04-07 18:40:56 +0000717 generated by the compiler in the demo page, consider using <tt>printf()</tt>
718 instead of <tt>iostream</tt>s to print values.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719</div>
720
721<!--=========================================================================-->
722
Bill Wendlingc0e81142009-04-07 18:40:56 +0000723<div class="question">
724<p><a name="codedce">Where did all of my code go??</a></p>
725</div>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726
727<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000728<p>If you are using the LLVM demo page, you may often wonder what happened to
729 all of the code that you typed in. Remember that the demo script is running
730 the code through the LLVM optimizers, so if your code doesn't actually do
731 anything useful, it might all be deleted.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732
Bill Wendlingc0e81142009-04-07 18:40:56 +0000733<p>To prevent this, make sure that the code is actually needed. For example, if
734 you are computing some expression, return the value from the function instead
735 of leaving it in a local variable. If you really want to constrain the
736 optimizer, you can read from and assign to <tt>volatile</tt> global
737 variables.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738</div>
739
740<!--=========================================================================-->
741
Bill Wendlingc0e81142009-04-07 18:40:56 +0000742<div class="question">
743<p><a name="undef">What is this "<tt>undef</tt>" thing that shows up in my
744 code?</p>
745</div>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746
747<div class="answer">
Bill Wendlingc0e81142009-04-07 18:40:56 +0000748<p><a href="LangRef.html#undef"><tt>undef</tt></a> is the LLVM way of
749 representing a value that is not defined. You can get these if you do not
750 initialize a variable before you use it. For example, the C function:</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751
Misha Brukman4fbcf682008-12-17 18:11:40 +0000752<pre class="doc_code">
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753int X() { int i; return i; }
754</pre>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755
Bill Wendlingc0e81142009-04-07 18:40:56 +0000756<p>Is compiled to "<tt>ret i32 undef</tt>" because "<tt>i</tt>" never has a
757 value specified for it.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758</div>
759
760<!-- *********************************************************************** -->
761
762<hr>
763<address>
764 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman947321d2008-12-11 17:34:48 +0000765 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman947321d2008-12-11 17:34:48 +0000767 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768
769 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
770 Last modified: $Date$
771</address>
772
773</body>
774</html>