blob: fc4c12408c40a598b8e965c3197d2a7ef6ef222a [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001======================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Tutorial - Using LLVMC
3======================
Mikhail Glushenkov23f522a2008-12-13 17:51:47 +00004..
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'.
Mikhail Glushenkovd5652032008-12-13 02:28:58 +00008
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
17Introduction
18============
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000019
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000020LLVMC is a generic compiler driver, which plays the same role for LLVM as the
21``gcc`` program does for GCC - the difference being that LLVMC is designed to be
22more adaptable and easier to customize. Most of LLVMC functionality is
23implemented via high-level TableGen code, from which a corresponding C++ source
24file is automatically generated. This tutorial describes the basic usage and
25configuration of LLVMC.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000026
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000027
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000028Using the ``llvmc`` program
29===========================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000030
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000031In general, ``llvmc`` tries to be command-line compatible with ``gcc`` as much
32as possible, so most of the familiar options work::
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000033
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000034 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000035 $ ./a.out
36 hello
37
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000038This will invoke ``llvm-g++`` under the hood (you can see which commands are
39executed by using the ``-v`` option). For further help on command-line LLVMC
40usage, refer to the ``llvmc --help`` output.
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000041
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000042
43Using LLVMC to generate toolchain drivers
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000044=========================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000045
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000046LLVMC-based drivers are written mostly using TableGen_, so you need to be
47familiar with it to get anything done.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000048
Mikhail Glushenkov4aecec12009-06-17 02:56:08 +000049.. _TableGen: http://llvm.org/docs/TableGenFundamentals.html
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000050
Mikhail Glushenkov530f3992009-06-16 00:13:52 +000051Start by compiling ``example/Simple``, which is a primitive wrapper for
52``gcc``::
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000053
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000054 $ cd $LLVM_OBJ_DIR/tools/examples/Simple
55 $ make
Mikhail Glushenkov83237482008-10-15 09:29:13 +000056 $ cat > hello.c
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000057 #include <stdio.h>
58 int main() { printf("Hello\n"); }
59 $ $LLVM_BIN_DIR/Simple -v hello.c
60 gcc hello.c -o hello.out
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000061 $ ./hello.out
62 Hello
63
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000064We have thus produced a simple driver called, appropriately, ``Simple``, from
65the input TableGen file ``Simple.td``. The ``llvmc`` program itself is generated
66using a similar process (see ``llvmc/src``). Contents of the file ``Simple.td``
67look like this::
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000068
69 // Include common definitions
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000070 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000071
72 // Tool descriptions
73 def gcc : Tool<
74 [(in_language "c"),
75 (out_language "executable"),
76 (output_suffix "out"),
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000077 (command "gcc"),
78 (sink),
79
80 // -o is what is used by default, out_file_option here is included for
81 // instructive purposes.
82 (out_file_option "-o")
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000083 ]>;
84
85 // Language map
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000086 def LanguageMap : LanguageMap<[(lang_to_suffixes "c", "c")]>;
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000087
88 // Compilation graph
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000089 def CompilationGraph : CompilationGraph<[(edge "root", "gcc")]>;
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000090
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000091As you can see, this file consists of three parts: tool descriptions, language
92map, and the compilation graph definition.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000093
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +000094At the heart of LLVMC is the idea of a compilation graph: vertices in this graph
95are tools, and edges represent a transformation path between two tools (for
96example, assembly source produced by the compiler can be transformed into
97executable code by an assembler). The compilation graph is basically a list of
98edges; a special node named ``root`` is used to mark graph entry points.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000099
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000100Tool descriptions are represented as property lists: most properties in the
101example above should be self-explanatory; the ``sink`` property means that all
102options lacking an explicit description should be forwarded to this tool.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000103
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000104The ``LanguageMap`` associates a language name with a list of suffixes and is
105used for deciding which toolchain corresponds to a given input file.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000106
Mikhail Glushenkov834b93b2011-04-24 14:17:32 +0000107To learn more about writing your own drivers with LLVMC, refer to the reference
108manual and examples in the ``examples`` directory. Of a particular interest is
109the ``Skeleton`` example, which can serve as a template for your LLVMC-based
110drivers.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000111
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000112.. raw:: html
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000113
114 <hr />
115 <address>
116 <a href="http://jigsaw.w3.org/css-validator/check/referer">
117 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
118 alt="Valid CSS" /></a>
119 <a href="http://validator.w3.org/check?uri=referer">
120 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
121 alt="Valid XHTML 1.0 Transitional"/></a>
122
123 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
124 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
125
126 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
127 </address>