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