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 | ====================== |
Mikhail Glushenkov | 6d1e928 | 2008-12-13 02:28:58 +0000 | [diff] [blame^] | 4 | |
| 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 | |
| 13 | Introduction |
| 14 | ============ |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 15 | |
| 16 | LLVMC is a generic compiler driver, which plays the same role for LLVM |
| 17 | as the ``gcc`` program does for GCC - the difference being that LLVMC |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 18 | is designed to be more adaptable and easier to customize. Most of |
| 19 | LLVMC functionality is implemented via plugins, which can be loaded |
| 20 | dynamically or compiled in. This tutorial describes the basic usage |
| 21 | and configuration of LLVMC. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 22 | |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 23 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 24 | Compiling with LLVMC |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 25 | ==================== |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 26 | |
| 27 | In general, LLVMC tries to be command-line compatible with ``gcc`` as |
| 28 | much as possible, so most of the familiar options work:: |
| 29 | |
Mikhail Glushenkov | c7e56fe | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 30 | $ llvmc -O3 -Wall hello.cpp |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 31 | $ ./a.out |
| 32 | hello |
| 33 | |
Mikhail Glushenkov | 99f1064 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 34 | This will invoke ``llvm-g++`` under the hood (you can see which |
| 35 | commands are executed by using the ``-v`` option). For further help on |
| 36 | command-line LLVMC usage, refer to the ``llvmc --help`` output. |
| 37 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 38 | |
| 39 | Using LLVMC to generate toolchain drivers |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 40 | ========================================= |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 41 | |
Mikhail Glushenkov | 6d1e928 | 2008-12-13 02:28:58 +0000 | [diff] [blame^] | 42 | LLVMC plugins are written mostly using TableGen_, so you need to |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 43 | be familiar with it to get anything done. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 44 | |
Mikhail Glushenkov | 6d1e928 | 2008-12-13 02:28:58 +0000 | [diff] [blame^] | 45 | .. _TableGen: http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html |
| 46 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 47 | Start by compiling ``plugins/Simple/Simple.td``, which is a primitive |
| 48 | wrapper for ``gcc``:: |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 49 | |
Mikhail Glushenkov | c7e56fe | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 50 | $ cd $LLVM_DIR/tools/llvmc |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 51 | $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple |
| 52 | $ cat > hello.c |
| 53 | [...] |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 54 | $ mygcc hello.c |
| 55 | $ ./hello.out |
| 56 | Hello |
| 57 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 58 | Here we link our plugin with the LLVMC core statically to form an |
| 59 | executable file called ``mygcc``. It is also possible to build our |
| 60 | plugin as a standalone dynamic library; this is described in the |
| 61 | reference manual. |
| 62 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 63 | Contents of the file ``Simple.td`` look like this:: |
| 64 | |
| 65 | // Include common definitions |
Mikhail Glushenkov | 99f1064 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 66 | include "llvm/CompilerDriver/Common.td" |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 67 | |
| 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 Glushenkov | fa99068 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 81 | def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>; |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 82 | |
| 83 | As you can see, this file consists of three parts: tool descriptions, |
| 84 | language map, and the compilation graph definition. |
| 85 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 86 | At the heart of LLVMC is the idea of a compilation graph: vertices in |
| 87 | this graph are tools, and edges represent a transformation path |
Mikhail Glushenkov | c178ead | 2008-09-22 20:45:17 +0000 | [diff] [blame] | 88 | between two tools (for example, assembly source produced by the |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 89 | compiler can be transformed into executable code by an assembler). The |
| 90 | compilation graph is basically a list of edges; a special node named |
| 91 | ``root`` is used to mark graph entry points. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 92 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 93 | Tool descriptions are represented as property lists: most properties |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 94 | in the example above should be self-explanatory; the ``sink`` property |
| 95 | means that all options lacking an explicit description should be |
| 96 | forwarded to this tool. |
| 97 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 98 | The ``LanguageMap`` associates a language name with a list of suffixes |
| 99 | and is used for deciding which toolchain corresponds to a given input |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 100 | file. |
| 101 | |
| 102 | To learn more about LLVMC customization, refer to the reference |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 103 | manual and plugin source code in the ``plugins`` directory. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 104 | |
Mikhail Glushenkov | ac251f2 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 105 | .. raw:: html |
Mikhail Glushenkov | 6d1e928 | 2008-12-13 02:28:58 +0000 | [diff] [blame^] | 106 | |
| 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> |