blob: 1b545319176350db8b3e17e9a5736cd2f38cb32b [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001======================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Tutorial - Using LLVMC
3======================
4
5LLVMC is a generic compiler driver, which plays the same role for LLVM
6as the ``gcc`` program does for GCC - the difference being that LLVMC
Mikhail Glushenkov83237482008-10-15 09:29:13 +00007is designed to be more adaptable and easier to customize. Most of
8LLVMC functionality is implemented via plugins, which can be loaded
9dynamically or compiled in. This tutorial describes the basic usage
10and configuration of LLVMC.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000011
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000012
13.. contents::
14
15
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000016Compiling with LLVMC
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000017====================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000018
19In general, LLVMC tries to be command-line compatible with ``gcc`` as
20much as possible, so most of the familiar options work::
21
22 $ llvmc2 -O3 -Wall hello.cpp
23 $ ./a.out
24 hello
25
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000026This will invoke ``llvm-g++`` under the hood (you can see which
27commands are executed by using the ``-v`` option). For further help on
28command-line LLVMC usage, refer to the ``llvmc --help`` output.
29
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000030
31Using LLVMC to generate toolchain drivers
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000032=========================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000033
Mikhail Glushenkov83237482008-10-15 09:29:13 +000034LLVMC plugins are written mostly using TableGen [1]_, so you need to
35be familiar with it to get anything done.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000036
Mikhail Glushenkov83237482008-10-15 09:29:13 +000037Start by compiling ``plugins/Simple/Simple.td``, which is a primitive
38wrapper for ``gcc``::
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000039
40 $ cd $LLVM_DIR/tools/llvmc2
Mikhail Glushenkov83237482008-10-15 09:29:13 +000041 $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple
42 $ cat > hello.c
43 [...]
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000044 $ mygcc hello.c
45 $ ./hello.out
46 Hello
47
Mikhail Glushenkov83237482008-10-15 09:29:13 +000048Here we link our plugin with the LLVMC core statically to form an
49executable file called ``mygcc``. It is also possible to build our
50plugin as a standalone dynamic library; this is described in the
51reference manual.
52
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000053Contents of the file ``Simple.td`` look like this::
54
55 // Include common definitions
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000056 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000057
58 // Tool descriptions
59 def gcc : Tool<
60 [(in_language "c"),
61 (out_language "executable"),
62 (output_suffix "out"),
63 (cmd_line "gcc $INFILE -o $OUTFILE"),
64 (sink)
65 ]>;
66
67 // Language map
68 def LanguageMap : LanguageMap<[LangToSuffixes<"c", ["c"]>]>;
69
70 // Compilation graph
Mikhail Glushenkov01088772008-11-17 17:29:18 +000071 def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>;
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000072
73As you can see, this file consists of three parts: tool descriptions,
74language map, and the compilation graph definition.
75
Mikhail Glushenkov83237482008-10-15 09:29:13 +000076At the heart of LLVMC is the idea of a compilation graph: vertices in
77this graph are tools, and edges represent a transformation path
Mikhail Glushenkov5ccf28f2008-09-22 20:45:17 +000078between two tools (for example, assembly source produced by the
Mikhail Glushenkov83237482008-10-15 09:29:13 +000079compiler can be transformed into executable code by an assembler). The
80compilation graph is basically a list of edges; a special node named
81``root`` is used to mark graph entry points.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000082
Mikhail Glushenkov83237482008-10-15 09:29:13 +000083Tool descriptions are represented as property lists: most properties
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000084in the example above should be self-explanatory; the ``sink`` property
85means that all options lacking an explicit description should be
86forwarded to this tool.
87
Mikhail Glushenkov83237482008-10-15 09:29:13 +000088The ``LanguageMap`` associates a language name with a list of suffixes
89and is used for deciding which toolchain corresponds to a given input
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000090file.
91
92To learn more about LLVMC customization, refer to the reference
Mikhail Glushenkov83237482008-10-15 09:29:13 +000093manual and plugin source code in the ``plugins`` directory.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000094
95References
96==========
97
98.. [1] TableGen Fundamentals
99 http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
100