Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 1 | ====================== |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 2 | Tutorial - Using LLVMC |
| 3 | ====================== |
| 4 | |
| 5 | LLVMC is a generic compiler driver, which plays the same role for LLVM |
| 6 | as the ``gcc`` program does for GCC - the difference being that LLVMC |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 7 | is designed to be more adaptable and easier to customize. Most of |
| 8 | LLVMC functionality is implemented via plugins, which can be loaded |
| 9 | dynamically or compiled in. This tutorial describes the basic usage |
| 10 | and configuration of LLVMC. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 11 | |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 12 | |
| 13 | .. contents:: |
| 14 | |
| 15 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 16 | Compiling with LLVMC |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 17 | ==================== |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 18 | |
| 19 | In general, LLVMC tries to be command-line compatible with ``gcc`` as |
| 20 | much as possible, so most of the familiar options work:: |
| 21 | |
| 22 | $ llvmc2 -O3 -Wall hello.cpp |
| 23 | $ ./a.out |
| 24 | hello |
| 25 | |
| 26 | For further help on command-line LLVMC usage, refer to the ``llvmc |
| 27 | --help`` output. |
| 28 | |
| 29 | Using LLVMC to generate toolchain drivers |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 30 | ========================================= |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 31 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 32 | LLVMC plugins are written mostly using TableGen [1]_, so you need to |
| 33 | be familiar with it to get anything done. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 34 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 35 | Start by compiling ``plugins/Simple/Simple.td``, which is a primitive |
| 36 | wrapper for ``gcc``:: |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 37 | |
| 38 | $ cd $LLVM_DIR/tools/llvmc2 |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 39 | $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple |
| 40 | $ cat > hello.c |
| 41 | [...] |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 42 | $ mygcc hello.c |
| 43 | $ ./hello.out |
| 44 | Hello |
| 45 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 46 | Here we link our plugin with the LLVMC core statically to form an |
| 47 | executable file called ``mygcc``. It is also possible to build our |
| 48 | plugin as a standalone dynamic library; this is described in the |
| 49 | reference manual. |
| 50 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 51 | Contents 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 | |
| 71 | As you can see, this file consists of three parts: tool descriptions, |
| 72 | language map, and the compilation graph definition. |
| 73 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 74 | At the heart of LLVMC is the idea of a compilation graph: vertices in |
| 75 | this graph are tools, and edges represent a transformation path |
Mikhail Glushenkov | c178ead | 2008-09-22 20:45:17 +0000 | [diff] [blame] | 76 | between two tools (for example, assembly source produced by the |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 77 | compiler can be transformed into executable code by an assembler). The |
| 78 | compilation graph is basically a list of edges; a special node named |
| 79 | ``root`` is used to mark graph entry points. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 80 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 81 | Tool descriptions are represented as property lists: most properties |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 82 | in the example above should be self-explanatory; the ``sink`` property |
| 83 | means that all options lacking an explicit description should be |
| 84 | forwarded to this tool. |
| 85 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 86 | The ``LanguageMap`` associates a language name with a list of suffixes |
| 87 | and is used for deciding which toolchain corresponds to a given input |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 88 | file. |
| 89 | |
| 90 | To learn more about LLVMC customization, refer to the reference |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame^] | 91 | manual and plugin source code in the ``plugins`` directory. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 92 | |
| 93 | References |
| 94 | ========== |
| 95 | |
| 96 | .. [1] TableGen Fundamentals |
| 97 | http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html |
| 98 | |