Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame^] | 1 | Tutorial - Using LLVMC |
| 2 | ====================== |
| 3 | |
| 4 | LLVMC is a generic compiler driver, which plays the same role for LLVM |
| 5 | as the ``gcc`` program does for GCC - the difference being that LLVMC |
| 6 | is designed to be more adaptable and easier to customize. This |
| 7 | tutorial describes the basic usage and configuration of LLVMC. |
| 8 | |
| 9 | Compiling with LLVMC |
| 10 | -------------------- |
| 11 | |
| 12 | In general, LLVMC tries to be command-line compatible with ``gcc`` as |
| 13 | much as possible, so most of the familiar options work:: |
| 14 | |
| 15 | $ llvmc2 -O3 -Wall hello.cpp |
| 16 | $ ./a.out |
| 17 | hello |
| 18 | |
| 19 | For further help on command-line LLVMC usage, refer to the ``llvmc |
| 20 | --help`` output. |
| 21 | |
| 22 | Using LLVMC to generate toolchain drivers |
| 23 | ----------------------------------------- |
| 24 | |
| 25 | At the time of writing LLVMC does not support on-the-fly reloading of |
| 26 | configuration, so it will be necessary to recompile its source |
| 27 | code. LLVMC uses TableGen [1]_ as its configuration language, so |
| 28 | you'll need to familiar with it. |
| 29 | |
| 30 | Start by compiling ``examples/Simple.td``, which is a simple wrapper |
| 31 | for ``gcc``:: |
| 32 | |
| 33 | $ cd $LLVM_DIR/tools/llvmc2 |
| 34 | $ make TOOLNAME=mygcc GRAPH=examples/Simple.td |
| 35 | $ edit hello.c |
| 36 | $ mygcc hello.c |
| 37 | $ ./hello.out |
| 38 | Hello |
| 39 | |
| 40 | Contents of the file ``Simple.td`` look like this:: |
| 41 | |
| 42 | // Include common definitions |
| 43 | include "Common.td" |
| 44 | |
| 45 | // Tool descriptions |
| 46 | def gcc : Tool< |
| 47 | [(in_language "c"), |
| 48 | (out_language "executable"), |
| 49 | (output_suffix "out"), |
| 50 | (cmd_line "gcc $INFILE -o $OUTFILE"), |
| 51 | (sink) |
| 52 | ]>; |
| 53 | |
| 54 | // Language map |
| 55 | def LanguageMap : LanguageMap<[LangToSuffixes<"c", ["c"]>]>; |
| 56 | |
| 57 | // Compilation graph |
| 58 | def CompilationGraph : CompilationGraph<[Edge<root, gcc>]>; |
| 59 | |
| 60 | As you can see, this file consists of three parts: tool descriptions, |
| 61 | language map, and the compilation graph definition. |
| 62 | |
| 63 | At the heart of LLVMC is the idea of a transformation graph: vertices |
| 64 | in this graph are tools, and edges signify that there is a |
| 65 | transformation path between two tools (for example, assembly source |
| 66 | produced by the compiler can be transformed into executable code by an |
| 67 | assembler). A special node named ``root`` is used to mark graph entry |
| 68 | points. |
| 69 | |
| 70 | Tool descriptions are basically lists of properties: most properties |
| 71 | in the example above should be self-explanatory; the ``sink`` property |
| 72 | means that all options lacking an explicit description should be |
| 73 | forwarded to this tool. |
| 74 | |
| 75 | ``LanguageMap`` associates a language name with a list of suffixes and |
| 76 | is used for deciding which toolchain corresponds to a given input |
| 77 | file. |
| 78 | |
| 79 | To learn more about LLVMC customization, refer to the reference |
| 80 | manual and sample configuration files in the ``examples`` directory. |
| 81 | |
| 82 | References |
| 83 | ========== |
| 84 | |
| 85 | .. [1] TableGen Fundamentals |
| 86 | http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html |
| 87 | |