blob: 6f0647784245b4e5ad47ed4716774432f93256a8 [file] [log] [blame]
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +00001======================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +00002Tutorial - Using LLVMC
3======================
Mikhail Glushenkovb7677be2008-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 Glushenkov6d1e9282008-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 Glushenkov1ce87222008-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 Glushenkovbd51c232008-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 Glushenkov1ce87222008-05-30 06:14:42 +000026
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000027
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000028Compiling with LLVMC
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000029====================
Mikhail Glushenkov1ce87222008-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 Glushenkovc7e56fe2008-11-25 21:38:12 +000034 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000035 $ ./a.out
36 hello
37
Mikhail Glushenkov99f10642008-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 Glushenkov1ce87222008-05-30 06:14:42 +000042
43Using LLVMC to generate toolchain drivers
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000044=========================================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000045
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +000046LLVMC plugins are written mostly using TableGen_, so you need to
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000047be familiar with it to get anything done.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000048
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +000049.. _TableGen: http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
50
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000051Start by compiling ``plugins/Simple/Simple.td``, which is a primitive
52wrapper for ``gcc``::
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000053
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000054 $ cd $LLVM_DIR/tools/llvmc
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000055 $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple
56 $ cat > hello.c
57 [...]
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000058 $ mygcc hello.c
59 $ ./hello.out
60 Hello
61
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000062Here we link our plugin with the LLVMC core statically to form an
63executable file called ``mygcc``. It is also possible to build our
64plugin as a standalone dynamic library; this is described in the
65reference manual.
66
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000067Contents of the file ``Simple.td`` look like this::
68
69 // Include common definitions
Mikhail Glushenkov99f10642008-11-25 21:34:29 +000070 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkov1ce87222008-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"),
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
Mikhail Glushenkovfa990682008-11-17 17:29:18 +000085 def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>;
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000086
87As you can see, this file consists of three parts: tool descriptions,
88language map, and the compilation graph definition.
89
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000090At the heart of LLVMC is the idea of a compilation graph: vertices in
91this graph are tools, and edges represent a transformation path
Mikhail Glushenkovc178ead2008-09-22 20:45:17 +000092between two tools (for example, assembly source produced by the
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000093compiler can be transformed into executable code by an assembler). The
94compilation graph is basically a list of edges; a special node named
95``root`` is used to mark graph entry points.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000096
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000097Tool descriptions are represented as property lists: most properties
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000098in the example above should be self-explanatory; the ``sink`` property
99means that all options lacking an explicit description should be
100forwarded to this tool.
101
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000102The ``LanguageMap`` associates a language name with a list of suffixes
103and is used for deciding which toolchain corresponds to a given input
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000104file.
105
106To learn more about LLVMC customization, refer to the reference
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000107manual and plugin source code in the ``plugins`` directory.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000108
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000109.. raw:: html
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +0000110
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>