blob: f7eb30e48abcfc0dc840169c709fc1435a543369 [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
20LLVMC is a generic compiler driver, which plays the same role for LLVM
21as the ``gcc`` program does for GCC - the difference being that LLVMC
Mikhail Glushenkov83237482008-10-15 09:29:13 +000022is designed to be more adaptable and easier to customize. Most of
23LLVMC functionality is implemented via plugins, which can be loaded
24dynamically or compiled in. This tutorial describes the basic usage
25and configuration of LLVMC.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000026
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000027
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000028Compiling with LLVMC
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000029====================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000030
31In general, LLVMC tries to be command-line compatible with ``gcc`` as
32much as possible, so most of the familiar options work::
33
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 Glushenkovebdeca72008-11-25 21:34:29 +000038This will invoke ``llvm-g++`` under the hood (you can see which
39commands are executed by using the ``-v`` option). For further help on
40command-line LLVMC usage, refer to the ``llvmc --help`` output.
41
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 Glushenkovd5652032008-12-13 02:28:58 +000046LLVMC plugins are written mostly using TableGen_, so you need to
Mikhail Glushenkov83237482008-10-15 09:29:13 +000047be familiar with it to get anything done.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000048
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000049.. _TableGen: http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
50
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 Glushenkov113ec352008-11-25 21:38:12 +000054 $ cd $LLVM_DIR/tools/llvmc
Mikhail Glushenkov530f3992009-06-16 00:13:52 +000055
56 # NB: A less verbose way to compile standalone LLVMC-based drivers is
57 # described in the reference manual.
58
59 $ make LLVMC_BASED_DRIVER_NAME=mygcc LLVMC_BUILTIN_PLUGINS=Simple
Mikhail Glushenkov83237482008-10-15 09:29:13 +000060 $ cat > hello.c
61 [...]
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000062 $ mygcc hello.c
63 $ ./hello.out
64 Hello
65
Mikhail Glushenkov530f3992009-06-16 00:13:52 +000066Here we link our plugin with the LLVMC core statically to form an executable
67file called ``mygcc``. It is also possible to build our plugin as a dynamic
68library to be loaded by the ``llvmc`` executable (or any other LLVMC-based
69standalone driver); this is described in the reference manual.
Mikhail Glushenkov83237482008-10-15 09:29:13 +000070
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000071Contents of the file ``Simple.td`` look like this::
72
73 // Include common definitions
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000074 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000075
76 // Tool descriptions
77 def gcc : Tool<
78 [(in_language "c"),
79 (out_language "executable"),
80 (output_suffix "out"),
81 (cmd_line "gcc $INFILE -o $OUTFILE"),
82 (sink)
83 ]>;
84
85 // Language map
86 def LanguageMap : LanguageMap<[LangToSuffixes<"c", ["c"]>]>;
87
88 // Compilation graph
Mikhail Glushenkov01088772008-11-17 17:29:18 +000089 def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>;
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000090
91As you can see, this file consists of three parts: tool descriptions,
92language map, and the compilation graph definition.
93
Mikhail Glushenkov83237482008-10-15 09:29:13 +000094At the heart of LLVMC is the idea of a compilation graph: vertices in
95this graph are tools, and edges represent a transformation path
Mikhail Glushenkov5ccf28f2008-09-22 20:45:17 +000096between two tools (for example, assembly source produced by the
Mikhail Glushenkov83237482008-10-15 09:29:13 +000097compiler can be transformed into executable code by an assembler). The
98compilation graph is basically a list of edges; a special node named
99``root`` is used to mark graph entry points.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000100
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000101Tool descriptions are represented as property lists: most properties
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000102in the example above should be self-explanatory; the ``sink`` property
103means that all options lacking an explicit description should be
104forwarded to this tool.
105
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000106The ``LanguageMap`` associates a language name with a list of suffixes
107and is used for deciding which toolchain corresponds to a given input
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000108file.
109
110To learn more about LLVMC customization, refer to the reference
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000111manual and plugin source code in the ``plugins`` directory.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000112
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000113.. raw:: html
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000114
115 <hr />
116 <address>
117 <a href="http://jigsaw.w3.org/css-validator/check/referer">
118 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
119 alt="Valid CSS" /></a>
120 <a href="http://validator.w3.org/check?uri=referer">
121 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
122 alt="Valid XHTML 1.0 Transitional"/></a>
123
124 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
125 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
126
127 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
128 </address>