Write most of the isa, cast, dyn_cast section. It's not done yet though.
[oota-llvm.git] / docs / GettingStarted.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3   <head>
4     <title>Getting Started with LLVM System</title>
5   </head>
6
7   <body bgcolor=white>
8     <center><h1>Getting Started with the LLVM System<br><font size=3>By: <a
9     href="mailto:gshi1@uiuc.edu">Guochun Shi</a>,
10     <a href="mailto:sabre@nondot.org">Chris Lattner</a> and
11     <a href="http://www.cs.uiuc.edu/~vadve">Vikram Adve</a>
12     </font></h1></center>
13
14     <!--=====================================================================-->
15     <h2><a name="Contents">Contents</a></h2>
16     <!--=====================================================================-->
17
18     <ul>
19       <li><a href="#overview">Overview</a>
20       <li><a href="#starting">Getting started with LLVM</a>
21         <ol>
22           <li><a href="#quickstart">Getting started quickly (a summary)</a>
23           <li><a href="#checkout">Checkout LLVM from CVS</a>
24           <li><a href="#terminology">Terminology and Notation</tt></a>
25           <li><a href="#objfiles">The location for object files</tt></a>
26           <li><a href="#config">Local Configuration Options</tt></a>
27           <li><a href="#environment">Setting up your environment</a>
28           <li><a href="#compile">Compiling the source code</a>
29         </ol>
30       <li><a href="#layout">Program layout</a>
31         <ol>
32           <li><a href="#cvsdir">CVS directories</a>
33           <li><a href="#dd"><tt>Depend</tt>, <tt>Debug</tt>, &amp;
34                <tt>Release</tt> directories</a></li>
35           <li><a href="#include"><tt>llvm/include</tt></a>
36           <li><a href="#lib"><tt>llvm/lib</tt></a>
37           <li><a href="#test"><tt>llvm/test</tt></a>
38           <li><a href="#tools"><tt>llvm/tools</tt></a>  
39         </ol>
40       <li><a href="#tutorial">An example using the LLVM tool chain</a>
41       <li><a href="#links">Links</a>
42     </ul>
43
44
45     <!--=====================================================================-->
46     <center>
47     <h2><a name="overview"><b>Overview</b></a></h2>
48     </center>
49     <!--=====================================================================-->
50
51     <p>The <a href"starting">next section</a> of this guide is meant to get
52     you up and running with LLVM, and to give you some basic information about
53     the LLVM environment.  The <a href"#quickstart">first subsection</a> gives
54     a short summary for those who are already familiar with the system and
55     want to get started as quickly as possible.
56
57     <p>The later sections of this guide describe the <a
58     href"#layout">general layout</a> of the the llvm source tree, a <a
59     href="#tutorial">simple example</a> using the LLVM tool chain, and <a
60     href="#links">links</a> to find more information about LLVM or to get
61     help via e-mail.
62
63     <!--=====================================================================-->
64     <center>
65     <h2><a name="starting"><b>Getting Started</b></a></h2>
66     </center>
67     <!--=====================================================================-->
68
69
70     <!--=====================================================================-->
71     <h3><a name="quickstart"><b>Getting Started Quickly (A Summary)</b></a></h3>
72     <!--=====================================================================-->
73
74     Here's the short story for getting up and running quickly with LLVM:
75     <ul>
76     <li>Find the path to the CVS repository containing LLVM (we'll call this <i>CVSROOTDIR</i>).
77     <li><tt>cd <i>where-you-want-llvm-to-live</i></tt>
78     <li><tt>cvs -d <i>CVSROOTDIR</i> checkout llvm</tt>
79     <li>Edit <tt>llvm/Makefile.config</tt> to set local paths if necessary.
80     <li><tt>cd llvm</tt>
81     <li><tt>gmake -k |& tee gnumake.out
82             &nbsp;&nbsp;&nbsp;# this is csh or tcsh syntax</tt>
83     </ul>
84
85     <p>See <a href="#environment">Setting up your environment</a> on tips to
86     simplify working with the llvm front-end and compiled tools.  See the
87     other sub-sections below for other useful details in working with LLVM,
88     or go straight to <a href="#layout">Program Layout</a> to learn about the
89     layout of the source code tree.
90
91     <!------------------------------------------------------------------------->
92     <h3><a name="terminology">Terminology and Notation</a></h3>
93     <!------------------------------------------------------------------------->
94
95     <p>Through this manual, the following names are used to denote paths
96     specific to the local system and working environment.  <i>These are not
97     environment variables you need to set, but just strings used in the rest
98     of this document below.</i>.  In any of the examples below, simply replace
99     each of these names with the appropriate pathname on your local system.
100     All these paths are absolute:</p>
101     <ul>
102     </ul>
103
104     <!------------------------------------------------------------------------->
105     <h3><a name="checkout">Checkout LLVM from CVS</a></h3>
106     <!------------------------------------------------------------------------->
107
108     <p>Before checking out the source code, you will need to know the path to
109     CVS repository containing LLVM source code (we'll call this
110     <i>CVSROOTDIR</i> below).  Ask the person responsible for your local LLVM
111     installation to give you this path.
112
113     <p>To get a fresh copy of the entire source code, all you
114     need to do is check it out from CVS as follows:
115     <ul>
116     <li><tt>cd <i>where-you-want-llvm-to-live</i></tt>
117     <li><tt>cvs -d <i>CVSROOTDIR</i> checkout llvm</tt></p>
118     </ul>
119
120     <p>This will create an '<tt>llvm</tt>' directory in the current
121     directory and fully populate it with the LLVM source code, Makefiles,
122     test directories, and local copies of documentation files.</p>
123
124     <!------------------------------------------------------------------------->
125     <h3><a name="config">Local Configuration Options</a></h3>
126     <!------------------------------------------------------------------------->
127
128     <p>The file <tt>llvm/Makefile.config</tt>
129     defines the following path variables,
130     which are specific to a particular installation of LLVM.
131     These should need to be modified only once after checking out a copy
132     of LLVM (if the default values do not already match your system):
133
134     <ul>
135     <p><li><i>LLVM_OBJ_DIR</i> = Path to the llvm directory where
136                                  object files should be placed.
137                                  (See the Section on <a href=#objfiles>
138                                  The location for LLVM object files</a>
139                                  for more information.)
140     <p><li><i>LLVMGCCDIR</i>   = Path to the location of the LLVM front-end
141                                  binaries and associated libraries.
142     <p><li><i>BURG</i>         = Path to the burg program used for instruction
143                                  selection. 
144     <p><li><i>PURIFY</i>       = Path to the purify program.
145     </ul>
146
147     <!------------------------------------------------------------------------->
148     <h3><a name="objfiles">The location for LLVM object files</a></h3>
149     <!------------------------------------------------------------------------->
150
151     <p>The LLVM make system sends most output files generated during the build
152     into the directory defined by the variable LLVM_OBJ_DIR in
153     <tt>llvm/Makefile.config</tt>.
154     This can be either just your normal </tt>llvm</tt> source tree or some
155     other directory writable by you.  You may wish to put object files on a
156     different filesystem either to keep them from being backed up or to speed
157     up local builds.
158
159     <p>If you do not wish to use a different location for object files,
160     just set this variable to ".".
161
162     <!------------------------------------------------------------------------->
163     <h3><a name="environment">Setting up your environment</a></h3>
164     <!------------------------------------------------------------------------->
165
166     <i>NOTE: This step is optional but will set up your environment so you
167     can use the compiled LLVM tools with as little hassle as possible.</i>)
168
169     <p>Add the following lines to your <tt>.cshrc</tt> (or the corresponding
170     lines to your <tt>.profile</tt> if you use a bourne shell derivative).
171
172     <pre>
173        # Make the C front end easy to use...
174        alias llvmgcc <i>LLVMGCCDIR</i><tt>/bin/gcc</tt>
175
176        # Make the LLVM tools easy to use...
177        setenv PATH <i>LLVM_OBJ_DIR</i>/tools/Debug:${PATH}
178     </pre>
179     The <tt>llvmgcc</tt> alias is useful because the C compiler is not
180     included in the CVS tree you just checked out.
181     
182     <p>The other LLVM <a href="#tools">LLVM tools</a> are part of the LLVM
183     source base, and built when compiling LLVM.  They will be built into the
184     <tt><i>LLVM_OBJ_DIR</i>/tools/Debug</tt> directory.</p>
185
186     <!------------------------------------------------------------------------->
187     <h3><a name="compile">Compiling the source code</a></h3>
188     <!------------------------------------------------------------------------->
189
190     <p>Every directory in the LLVM source tree includes a Makefile to build it,
191     and any subdirectories that it contains.  These makefiles require that you
192     use <tt>gmake</tt>, instead of <tt>make</tt> to build them, but can
193     otherwise be used freely.  To build the entire LLVM system, just enter the
194     top level <tt>llvm</tt> directory and type <tt>gmake</tt>.  A few minutes
195     later you will hopefully have a freshly compiled toolchain waiting for you
196     in <tt>llvm/tools/Debug</tt>.  If you want to look at the libraries that
197     were compiled, look in <tt>llvm/lib/Debug</tt>.</p>
198
199     If you get an error talking about a <tt>/shared</tt> directory, follow the
200     instructions in the section about <a href="#environment">Setting Up Your
201     Environment.</a>
202
203
204
205     <!--=====================================================================-->
206     <center>
207     <h2><a name="layout"><b>Program Layout</b></a></h2>
208     </center>
209     <!--=====================================================================-->
210
211     <p>One useful source of infomation about the LLVM sourcebase is the LLVM <a
212     href="http://www.doxygen.org">doxygen</a> documentation, available at <tt><a
213     href="http://llvm.cs.uiuc.edu/doxygen/">http://llvm.cs.uiuc.edu/doxygen/</a></tt>. The
214     following is a brief introduction to code layout:</p>
215
216
217     <!------------------------------------------------------------------------->
218     <h3><a name="cvsdir"><tt>CVS</tt> directories</a></h3>
219     <!------------------------------------------------------------------------->
220
221     Every directory checked out of CVS will contain a <tt>CVS</tt> directory,
222     for the most part these can just be ignored.
223
224
225     <!------------------------------------------------------------------------->
226     <h3><a name="ddr"><tt>Depend</tt>, <tt>Debug</tt>, &amp; <tt>Release</tt>
227     directories</a></h3>
228     <!------------------------------------------------------------------------->
229
230     If you are building with the "<tt>BUILD_ROOT=.</tt>" option enabled in the
231     <tt>Makefile.common</tt> file, most source directories will contain two
232     directories, <tt>Depend</tt> and <tt>Debug</tt>. The <tt>Depend</tt>
233     directory contains automatically generated dependance files which are used
234     during compilation to make sure that source files get rebuilt if a header
235     file they use is modified. The <tt>Debug</tt> directory holds the object
236     files, library files and executables that are used for building a debug
237     enabled build.  The <tt>Release</tt> directory is created to hold the same
238     files when the <tt>ENABLE_OPTIMIZED=1</tt> flag is passed to <tt>gmake</tt>,
239     causing an optimized built to be performed.<p>
240
241
242     <!------------------------------------------------------------------------->
243     <h3><a name="include"><tt>llvm/include</tt></a></h3>
244     <!------------------------------------------------------------------------->
245
246     This directory contains public header files exported from the LLVM
247     library. The two main subdirectories of this directory are:<p>
248
249     <ol>
250        <li><tt>llvm/include/llvm</tt> - This directory contains all of the LLVM
251        specific header files.  This directory also has subdirectories for
252        different portions of llvm: <tt>Analysis</tt>, <tt>CodeGen</tt>,
253        <tt>Reoptimizer</tt>, <tt>Target</tt>, <tt>Transforms</tt>, etc...
254
255        <li><tt>llvm/include/Support</tt> - This directory contains generic
256        support libraries that are independant of LLVM, but are used by LLVM.
257        For example, some C++ STL utilities and a Command Line option processing
258        library.
259     </ol>
260
261     <!------------------------------------------------------------------------->
262     <h3><a name="lib"><tt>llvm/lib</tt></a></h3>
263     <!------------------------------------------------------------------------->
264
265     This directory contains most source files of LLVM system. In LLVM almost all
266     code exists in libraries, making it very easy to share code among the
267     different <a href="#tools">tools</a>.<p>
268
269      <dl compact>
270       <dt><tt>llvm/lib/VMCore/</tt><dd> This directory holds the core LLVM
271       source files that implement core classes like Instruction and BasicBlock.
272
273       <dt><tt>llvm/lib/AsmParser/</tt><dd> This directory holds the source code
274       for the LLVM assembly language parser library.
275
276       <dt><tt>llvm/lib/ByteCode/</tt><dd> This directory holds code for reading
277       and write LLVM bytecode.
278
279       <dt><tt>llvm/lib/CWrite/</tt><dd> This directory implements the LLVM to C
280       converter.
281
282       <dt><tt>llvm/lib/Analysis/</tt><dd> This directory contains a variety of
283       different program analyses, such as Dominator Information, Call Graphs,
284       Induction Variables, Interval Identification, Natural Loop Identification,
285       etc...
286
287       <dt><tt>llvm/lib/Transforms/</tt><dd> This directory contains the source
288       code for the LLVM to LLVM program transformations, such as Aggressive Dead
289       Code Elimination, Sparse Conditional Constant Propogation, Inlining, Loop
290       Invarient Code Motion, Dead Global Elimination, Pool Allocation, and many
291       others...
292
293       <dt><tt>llvm/lib/Target/</tt><dd> This directory contains files that
294       describe various target architectures for code generation.  For example,
295       the llvm/lib/Target/Sparc directory holds the Sparc machine
296       description.<br>
297               
298       <dt><tt>llvm/lib/CodeGen/</tt><dd> This directory contains the major parts
299       of the code generator: Instruction Selector, Instruction Scheduling, and
300       Register Allocation.
301
302       <dt><tt>llvm/lib/Reoptimizer/</tt><dd> This directory holds code related
303       to the runtime reoptimizer framework that is currently under development.
304               
305       <dt><tt>llvm/lib/Support/</tt><dd> This directory contains the source code
306       that corresponds to the header files located in
307       <tt>llvm/include/Support/</tt>.
308     </dl>
309
310     <!------------------------------------------------------------------------->
311     <h3><a name="test"><tt>llvm/test</tt></a></h3>
312     <!------------------------------------------------------------------------->
313
314     <p>This directory contains regression tests and source code that is used to
315     test the LLVM infrastructure...</p>
316
317     <!------------------------------------------------------------------------->
318     <h3><a name="tools"><tt>llvm/tools</tt></a></h3>
319     <!------------------------------------------------------------------------->
320
321     <p>The <b>tools</b> directory contains the executables built out of the
322     libraries above, which form the main part of the user interface.  You can
323     always get help for a tool by typing <tt>tool_name --help</tt>.  The
324     following is a brief introduction to the most important tools.</p>
325
326     <dl compact>
327       <dt><tt><b>as</b></tt><dd>The assembler transforms the human readable
328       llvm assembly to llvm bytecode.<p>
329
330       <dt><tt><b>dis</b></tt><dd>The disassembler transforms the llvm bytecode
331       to human readable llvm assembly.  Additionally it can convert LLVM
332       bytecode to C, which is enabled with the <tt>-c</tt> option.<p>
333
334       <dt><tt><b>lli</b></tt><dd> <tt>lli</tt> is the LLVM interpreter, which
335       can directly execute LLVM bytecode (although very slowly...). In addition
336       to a simple intepreter, <tt>lli</tt> is also has debugger and tracing
337       modes (entered by specifying <tt>-debug</tt> or <tt>-trace</tt> on the
338       command line, respectively).<p>
339
340       <dt><tt><b>llc</b></tt><dd> <tt>llc</tt> is the LLVM backend compiler,
341       which translates LLVM bytecode to a SPARC assembly file.<p>
342
343       <dt><tt><b>llvmgcc</b></tt><dd> <tt>llvmgcc</tt> is a GCC based C frontend
344       that has been retargeted to emit LLVM code as the machine code output.  It
345       works just like any other GCC compiler, taking the typical <tt>-c, -S, -E,
346       -o</tt> options that are typically used.  The source code for the
347       <tt>llvmgcc</tt> tool is currently not included in the LLVM cvs tree
348       because it is quite large and not very interesting.<p>
349
350       <ol>
351         <dt><tt><b>gccas</b></tt><dd> This tool is invoked by the
352         <tt>llvmgcc</tt> frontend as the "assembler" part of the compiler.  This
353         tool actually assembles LLVM assembly to LLVM bytecode,
354         performs a variety of optimizations,
355         and outputs LLVM bytecode.  Thus when you invoke <tt>llvmgcc -c x.c -o
356         x.o</tt>, you are causing <tt>gccas</tt> to be run, which writes the
357         <tt>x.o</tt> file (which is an LLVM bytecode file that can be
358         disassembled or manipulated just like any other bytecode file).  The
359         command line interface to <tt>gccas</tt> is designed to be as close as
360         possible to the <b>system</b> <tt>as</tt> utility so that the gcc
361         frontend itself did not have to be modified to interface to a "wierd"
362         assembler.<p>
363
364         <dt><tt><b>gccld</b></tt><dd> <tt>gccld</tt> links together several llvm
365         bytecode files into one bytecode file and does some optimization.  It is
366         the linker invoked by the gcc frontend when multiple .o files need to be
367         linked together.  Like <tt>gccas</tt> the command line interface of
368         <tt>gccld</tt> is designed to match the system linker, to aid
369         interfacing with the GCC frontend.<p>
370       </ol>
371
372       <dt><tt><b>opt</b></tt><dd> <tt>opt</tt> reads llvm bytecode, applies a
373       series of LLVM to LLVM transformations (which are specified on the command
374       line), and then outputs the resultant bytecode.  The '<tt>opt --help</tt>'
375       command is a good way to get a list of the program transformations
376       available in LLVM.<p>
377
378  
379       <dt><tt><b>analyze</b></tt><dd> <tt>analyze</tt> is used to run a specific
380       analysis on an input LLVM bytecode file and print out the results.  It is
381       primarily useful for debugging analyses, or familiarizing yourself with
382       what an analysis does.<p>
383
384     </dl>
385           
386     <!--=====================================================================-->
387     <h2><a name="tutorial">An example using the LLVM tool chain</h2>
388     <!--=====================================================================-->
389
390     <ol>
391     <li>First, create a simple C file, name it 'hello.c':
392        <pre>
393    #include &lt;stdio.h&gt;
394    int main() {
395      printf("hello world\n");
396      return 0;
397    }
398        </pre>
399
400     <li>Next, compile the C file into a LLVM bytecode file:<p>
401
402       <tt>% llvmgcc hello.c -o hello</tt><p>
403
404       This will create two result files: <tt>hello</tt> and
405       <tt>hello.bc</tt>. The <tt>hello.bc</tt> is the LLVM bytecode that
406       corresponds the the compiled program and the library facilities that it
407       required.  <tt>hello</tt> is a simple shell script that runs the bytecode
408       file with <tt>lli</tt>, making the result directly executable.<p>
409
410     <li>Run the program. To make sure the program ran, execute one of the
411     following commands:<p>
412       
413       <tt>% ./hello</tt><p>
414  
415       or<p>
416
417       <tt>% lli hello.bc</tt><p>
418
419     <li>Use the <tt>dis</tt> utility to take a look at the LLVM assembly
420     code:<p>
421
422       <tt>% dis < hello.bc | less</tt><p>
423
424     <li>Compile the program to native Sparc assembly using the code
425     generator:<p>
426
427       <tt>% llc hello.bc -o hello.s</tt><p>
428
429     <li>Assemble the native sparc assemble file into a program:<p>
430
431       <tt>% /opt/SUNWspro/bin/cc -xarch=v9 hello.s -o hello.sparc</tt><p>
432
433     <li>Execute the native sparc program:<p>
434
435       <tt>% ./hello.sparc</tt><p>
436
437     </ol>
438
439
440     <!--=====================================================================-->
441     <h2><a name="links">Links</a></h2>
442     <!--=====================================================================-->
443
444     <p>This document is just an <b>introduction</b> to how to use LLVM to do
445     some simple things... there are many more interesting and complicated things
446     that you can do that aren't documented here (but we'll gladly accept a patch
447     if you want to write something up!).  For more information about LLVM, check
448     out:</p>
449
450     <ul>
451     <li><a href="http://llvm.cs.uiuc.edu/">LLVM homepage</a></li>
452     <li><a href="http://tank.cs.uiuc.edu/doxygen/">LLVM doxygen tree</a></li>
453     </ul>
454
455     <hr>
456
457     If you have any questions or run into any snags (or you have any
458     additions...), please send an email to
459     <a href="mailto:hldnbrnd@uiuc.edu">Nicholas Hildenbrandt</a> or
460     <a href="mailto:sabre@nondot.org">Chris Lattner</a>.</p>
461
462             <!-- Created: Mon Jul  1 02:29:02 CDT 2002 -->
463             <!-- hhmts start -->
464 Last modified: Tue Aug 13 16:09:25 CDT 2002
465 <!-- hhmts end -->
466   </body>
467 </html>