blob: a27bb209290f3b70c79f694d1bf1bce8720e2fba [file] [log] [blame]
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +00001======================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +00002Tutorial - Using LLVMC
3======================
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +00004
5.. contents::
6
7.. raw:: html
8
9 <div class="doc_author">
10 <p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
11 </div>
12
13Introduction
14============
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000015
16LLVMC is a generic compiler driver, which plays the same role for LLVM
17as the ``gcc`` program does for GCC - the difference being that LLVMC
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000018is designed to be more adaptable and easier to customize. Most of
19LLVMC functionality is implemented via plugins, which can be loaded
20dynamically or compiled in. This tutorial describes the basic usage
21and configuration of LLVMC.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000022
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000023
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000024Compiling with LLVMC
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000025====================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000026
27In general, LLVMC tries to be command-line compatible with ``gcc`` as
28much as possible, so most of the familiar options work::
29
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000030 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000031 $ ./a.out
32 hello
33
Mikhail Glushenkov99f10642008-11-25 21:34:29 +000034This will invoke ``llvm-g++`` under the hood (you can see which
35commands are executed by using the ``-v`` option). For further help on
36command-line LLVMC usage, refer to the ``llvmc --help`` output.
37
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000038
39Using LLVMC to generate toolchain drivers
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000040=========================================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000041
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +000042LLVMC plugins are written mostly using TableGen_, so you need to
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000043be familiar with it to get anything done.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000044
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +000045.. _TableGen: http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
46
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000047Start by compiling ``plugins/Simple/Simple.td``, which is a primitive
48wrapper for ``gcc``::
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000049
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000050 $ cd $LLVM_DIR/tools/llvmc
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000051 $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple
52 $ cat > hello.c
53 [...]
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000054 $ mygcc hello.c
55 $ ./hello.out
56 Hello
57
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000058Here we link our plugin with the LLVMC core statically to form an
59executable file called ``mygcc``. It is also possible to build our
60plugin as a standalone dynamic library; this is described in the
61reference manual.
62
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000063Contents of the file ``Simple.td`` look like this::
64
65 // Include common definitions
Mikhail Glushenkov99f10642008-11-25 21:34:29 +000066 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000067
68 // Tool descriptions
69 def gcc : Tool<
70 [(in_language "c"),
71 (out_language "executable"),
72 (output_suffix "out"),
73 (cmd_line "gcc $INFILE -o $OUTFILE"),
74 (sink)
75 ]>;
76
77 // Language map
78 def LanguageMap : LanguageMap<[LangToSuffixes<"c", ["c"]>]>;
79
80 // Compilation graph
Mikhail Glushenkovfa990682008-11-17 17:29:18 +000081 def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>;
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000082
83As you can see, this file consists of three parts: tool descriptions,
84language map, and the compilation graph definition.
85
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000086At the heart of LLVMC is the idea of a compilation graph: vertices in
87this graph are tools, and edges represent a transformation path
Mikhail Glushenkovc178ead2008-09-22 20:45:17 +000088between two tools (for example, assembly source produced by the
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000089compiler can be transformed into executable code by an assembler). The
90compilation graph is basically a list of edges; a special node named
91``root`` is used to mark graph entry points.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000092
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000093Tool descriptions are represented as property lists: most properties
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000094in the example above should be self-explanatory; the ``sink`` property
95means that all options lacking an explicit description should be
96forwarded to this tool.
97
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000098The ``LanguageMap`` associates a language name with a list of suffixes
99and is used for deciding which toolchain corresponds to a given input
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000100file.
101
102To learn more about LLVMC customization, refer to the reference
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000103manual and plugin source code in the ``plugins`` directory.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000104
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000105.. raw:: html
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +0000106
107 <hr />
108 <address>
109 <a href="http://jigsaw.w3.org/css-validator/check/referer">
110 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
111 alt="Valid CSS" /></a>
112 <a href="http://validator.w3.org/check?uri=referer">
113 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
114 alt="Valid XHTML 1.0 Transitional"/></a>
115
116 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
117 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
118
119 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
120 </address>