More cosmetic tweaks for llvmc docs.
[oota-llvm.git] / docs / CompilerDriverTutorial.html
1 <?xml version="1.0" encoding="utf-8" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
7 <title>Tutorial - Using LLVMC</title>
8 <link rel="stylesheet" href="llvm-rst.css" type="text/css" />
9 </head>
10 <body>
11 <div class="document" id="tutorial-using-llvmc">
12 <h1 class="title">Tutorial - Using LLVMC</h1>
13 <div class="contents topic">
14 <p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
15 <ul class="simple">
16 <li><a class="reference" href="#introduction" id="id1" name="id1">Introduction</a></li>
17 <li><a class="reference" href="#compiling-with-llvmc" id="id2" name="id2">Compiling with LLVMC</a></li>
18 <li><a class="reference" href="#using-llvmc-to-generate-toolchain-drivers" id="id3" name="id3">Using LLVMC to generate toolchain drivers</a></li>
19 </ul>
20 </div>
21 <div class="doc_author">
22 <p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
23 </div><div class="section">
24 <h1><a class="toc-backref" href="#id1" id="introduction" name="introduction">Introduction</a></h1>
25 <p>LLVMC is a generic compiler driver, which plays the same role for LLVM
26 as the <tt class="docutils literal"><span class="pre">gcc</span></tt> program does for GCC - the difference being that LLVMC
27 is designed to be more adaptable and easier to customize. Most of
28 LLVMC functionality is implemented via plugins, which can be loaded
29 dynamically or compiled in. This tutorial describes the basic usage
30 and configuration of LLVMC.</p>
31 </div>
32 <div class="section">
33 <h1><a class="toc-backref" href="#id2" id="compiling-with-llvmc" name="compiling-with-llvmc">Compiling with LLVMC</a></h1>
34 <p>In general, LLVMC tries to be command-line compatible with <tt class="docutils literal"><span class="pre">gcc</span></tt> as
35 much as possible, so most of the familiar options work:</p>
36 <pre class="literal-block">
37 $ llvmc -O3 -Wall hello.cpp
38 $ ./a.out
39 hello
40 </pre>
41 <p>This will invoke <tt class="docutils literal"><span class="pre">llvm-g++</span></tt> under the hood (you can see which
42 commands are executed by using the <tt class="docutils literal"><span class="pre">-v</span></tt> option). For further help on
43 command-line LLVMC usage, refer to the <tt class="docutils literal"><span class="pre">llvmc</span> <span class="pre">--help</span></tt> output.</p>
44 </div>
45 <div class="section">
46 <h1><a class="toc-backref" href="#id3" id="using-llvmc-to-generate-toolchain-drivers" name="using-llvmc-to-generate-toolchain-drivers">Using LLVMC to generate toolchain drivers</a></h1>
47 <p>LLVMC plugins are written mostly using <a class="reference" href="http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html">TableGen</a>, so you need to
48 be familiar with it to get anything done.</p>
49 <p>Start by compiling <tt class="docutils literal"><span class="pre">plugins/Simple/Simple.td</span></tt>, which is a primitive
50 wrapper for <tt class="docutils literal"><span class="pre">gcc</span></tt>:</p>
51 <pre class="literal-block">
52 $ cd $LLVM_DIR/tools/llvmc
53 $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple
54 $ cat &gt; hello.c
55 [...]
56 $ mygcc hello.c
57 $ ./hello.out
58 Hello
59 </pre>
60 <p>Here we link our plugin with the LLVMC core statically to form an
61 executable file called <tt class="docutils literal"><span class="pre">mygcc</span></tt>. It is also possible to build our
62 plugin as a standalone dynamic library; this is described in the
63 reference manual.</p>
64 <p>Contents of the file <tt class="docutils literal"><span class="pre">Simple.td</span></tt> look like this:</p>
65 <pre class="literal-block">
66 // Include common definitions
67 include &quot;llvm/CompilerDriver/Common.td&quot;
68
69 // Tool descriptions
70 def gcc : Tool&lt;
71 [(in_language &quot;c&quot;),
72  (out_language &quot;executable&quot;),
73  (output_suffix &quot;out&quot;),
74  (cmd_line &quot;gcc $INFILE -o $OUTFILE&quot;),
75  (sink)
76 ]&gt;;
77
78 // Language map
79 def LanguageMap : LanguageMap&lt;[LangToSuffixes&lt;&quot;c&quot;, [&quot;c&quot;]&gt;]&gt;;
80
81 // Compilation graph
82 def CompilationGraph : CompilationGraph&lt;[Edge&lt;&quot;root&quot;, &quot;gcc&quot;&gt;]&gt;;
83 </pre>
84 <p>As you can see, this file consists of three parts: tool descriptions,
85 language map, and the compilation graph definition.</p>
86 <p>At the heart of LLVMC is the idea of a compilation graph: vertices in
87 this graph are tools, and edges represent a transformation path
88 between two tools (for example, assembly source produced by the
89 compiler can be transformed into executable code by an assembler). The
90 compilation graph is basically a list of edges; a special node named
91 <tt class="docutils literal"><span class="pre">root</span></tt> is used to mark graph entry points.</p>
92 <p>Tool descriptions are represented as property lists: most properties
93 in the example above should be self-explanatory; the <tt class="docutils literal"><span class="pre">sink</span></tt> property
94 means that all options lacking an explicit description should be
95 forwarded to this tool.</p>
96 <p>The <tt class="docutils literal"><span class="pre">LanguageMap</span></tt> associates a language name with a list of suffixes
97 and is used for deciding which toolchain corresponds to a given input
98 file.</p>
99 <p>To learn more about LLVMC customization, refer to the reference
100 manual and plugin source code in the <tt class="docutils literal"><span class="pre">plugins</span></tt> directory.</p>
101 <hr />
102 <address>
103 <a href="http://jigsaw.w3.org/css-validator/check/referer">
104 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
105    alt="Valid CSS" /></a>
106 <a href="http://validator.w3.org/check?uri=referer">
107 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
108    alt="Valid XHTML 1.0 Transitional"/></a>
109
110 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
111 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
112
113 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
114 </address></div>
115 </div>
116 </body>
117 </html>