blob: ffc1de903b8b2f4b833c1bd5b00d5ca9371c83d0 [file] [log] [blame]
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +00001======================
Mikhail Glushenkov1ce87222008-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 Glushenkovbd51c232008-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 Glushenkov1ce87222008-05-30 06:14:42 +000011
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000012
13.. contents::
14
15
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000016Compiling with LLVMC
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000017====================
Mikhail Glushenkov1ce87222008-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
26For further help on command-line LLVMC usage, refer to the ``llvmc
27--help`` output.
28
29Using LLVMC to generate toolchain drivers
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000030=========================================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000031
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000032LLVMC plugins are written mostly using TableGen [1]_, so you need to
33be familiar with it to get anything done.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000034
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000035Start by compiling ``plugins/Simple/Simple.td``, which is a primitive
36wrapper for ``gcc``::
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000037
38 $ cd $LLVM_DIR/tools/llvmc2
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000039 $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple
40 $ cat > hello.c
41 [...]
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000042 $ mygcc hello.c
43 $ ./hello.out
44 Hello
45
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000046Here we link our plugin with the LLVMC core statically to form an
47executable file called ``mygcc``. It is also possible to build our
48plugin as a standalone dynamic library; this is described in the
49reference manual.
50
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000051Contents of the file ``Simple.td`` look like this::
52
53 // Include common definitions
54 include "Common.td"
55
56 // Tool descriptions
57 def gcc : Tool<
58 [(in_language "c"),
59 (out_language "executable"),
60 (output_suffix "out"),
61 (cmd_line "gcc $INFILE -o $OUTFILE"),
62 (sink)
63 ]>;
64
65 // Language map
66 def LanguageMap : LanguageMap<[LangToSuffixes<"c", ["c"]>]>;
67
68 // Compilation graph
69 def CompilationGraph : CompilationGraph<[Edge<root, gcc>]>;
70
71As you can see, this file consists of three parts: tool descriptions,
72language map, and the compilation graph definition.
73
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000074At the heart of LLVMC is the idea of a compilation graph: vertices in
75this graph are tools, and edges represent a transformation path
Mikhail Glushenkovc178ead2008-09-22 20:45:17 +000076between two tools (for example, assembly source produced by the
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000077compiler can be transformed into executable code by an assembler). The
78compilation graph is basically a list of edges; a special node named
79``root`` is used to mark graph entry points.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000080
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000081Tool descriptions are represented as property lists: most properties
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000082in the example above should be self-explanatory; the ``sink`` property
83means that all options lacking an explicit description should be
84forwarded to this tool.
85
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000086The ``LanguageMap`` associates a language name with a list of suffixes
87and is used for deciding which toolchain corresponds to a given input
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000088file.
89
90To learn more about LLVMC customization, refer to the reference
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000091manual and plugin source code in the ``plugins`` directory.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000092
93References
94==========
95
96.. [1] TableGen Fundamentals
97 http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
98