CompilerDriverTutorial.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
  7. <title>Tutorial - Using LLVMC</title>
  8. <link rel="stylesheet" href="llvm.css" type="text/css" />
  9. </head>
  10. <body>
  11. <div class="document" id="tutorial-using-llvmc">
  12. <h1 class="title">Tutorial - Using LLVMC</h1>
  13. <!-- This file was automatically generated by rst2html.
  14. Please do not edit directly!
  15. The ReST source lives in the directory 'tools/llvmc/doc'. -->
  16. <div class="contents topic" id="contents">
  17. <p class="topic-title first">Contents</p>
  18. <ul class="simple">
  19. <li><a class="reference internal" href="#introduction" id="id1">Introduction</a></li>
  20. <li><a class="reference internal" href="#compiling-with-llvmc" id="id2">Compiling with LLVMC</a></li>
  21. <li><a class="reference internal" href="#using-llvmc-to-generate-toolchain-drivers" id="id3">Using LLVMC to generate toolchain drivers</a></li>
  22. </ul>
  23. </div>
  24. <div class="doc_author">
  25. <p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
  26. </div><div class="section" id="introduction">
  27. <h1><a class="toc-backref" href="#id1">Introduction</a></h1>
  28. <p>LLVMC is a generic compiler driver, which plays the same role for LLVM
  29. as the <tt class="docutils literal"><span class="pre">gcc</span></tt> program does for GCC - the difference being that LLVMC
  30. is designed to be more adaptable and easier to customize. Most of
  31. LLVMC functionality is implemented via plugins, which can be loaded
  32. dynamically or compiled in. This tutorial describes the basic usage
  33. and configuration of LLVMC.</p>
  34. </div>
  35. <div class="section" id="compiling-with-llvmc">
  36. <h1><a class="toc-backref" href="#id2">Compiling with LLVMC</a></h1>
  37. <p>In general, LLVMC tries to be command-line compatible with <tt class="docutils literal"><span class="pre">gcc</span></tt> as
  38. much as possible, so most of the familiar options work:</p>
  39. <pre class="literal-block">
  40. $ llvmc -O3 -Wall hello.cpp
  41. $ ./a.out
  42. hello
  43. </pre>
  44. <p>This will invoke <tt class="docutils literal"><span class="pre">llvm-g++</span></tt> under the hood (you can see which
  45. commands are executed by using the <tt class="docutils literal"><span class="pre">-v</span></tt> option). For further help on
  46. command-line LLVMC usage, refer to the <tt class="docutils literal"><span class="pre">llvmc</span> <span class="pre">--help</span></tt> output.</p>
  47. </div>
  48. <div class="section" id="using-llvmc-to-generate-toolchain-drivers">
  49. <h1><a class="toc-backref" href="#id3">Using LLVMC to generate toolchain drivers</a></h1>
  50. <p>LLVMC plugins are written mostly using <a class="reference external" href="http://llvm.org/docs/TableGenFundamentals.html">TableGen</a>, so you need to
  51. be familiar with it to get anything done.</p>
  52. <p>Start by compiling <tt class="docutils literal"><span class="pre">example/Simple</span></tt>, which is a primitive wrapper for
  53. <tt class="docutils literal"><span class="pre">gcc</span></tt>:</p>
  54. <pre class="literal-block">
  55. $ cd $LLVM_DIR/tools/llvmc
  56. $ cp -r example/Simple plugins/Simple
  57. # NB: A less verbose way to compile standalone LLVMC-based drivers is
  58. # described in the reference manual.
  59. $ make LLVMC_BASED_DRIVER_NAME=mygcc LLVMC_BUILTIN_PLUGINS=Simple
  60. $ cat &gt; hello.c
  61. [...]
  62. $ mygcc hello.c
  63. $ ./hello.out
  64. Hello
  65. </pre>
  66. <p>Here we link our plugin with the LLVMC core statically to form an executable
  67. file called <tt class="docutils literal"><span class="pre">mygcc</span></tt>. It is also possible to build our plugin as a dynamic
  68. library to be loaded by the <tt class="docutils literal"><span class="pre">llvmc</span></tt> executable (or any other LLVMC-based
  69. standalone driver); this is described in the reference manual.</p>
  70. <p>Contents of the file <tt class="docutils literal"><span class="pre">Simple.td</span></tt> look like this:</p>
  71. <pre class="literal-block">
  72. // Include common definitions
  73. include &quot;llvm/CompilerDriver/Common.td&quot;
  74. // Tool descriptions
  75. def gcc : Tool&lt;
  76. [(in_language &quot;c&quot;),
  77. (out_language &quot;executable&quot;),
  78. (output_suffix &quot;out&quot;),
  79. (cmd_line &quot;gcc $INFILE -o $OUTFILE&quot;),
  80. (sink)
  81. ]&gt;;
  82. // Language map
  83. def LanguageMap : LanguageMap&lt;[LangToSuffixes&lt;&quot;c&quot;, [&quot;c&quot;]&gt;]&gt;;
  84. // Compilation graph
  85. def CompilationGraph : CompilationGraph&lt;[Edge&lt;&quot;root&quot;, &quot;gcc&quot;&gt;]&gt;;
  86. </pre>
  87. <p>As you can see, this file consists of three parts: tool descriptions,
  88. language map, and the compilation graph definition.</p>
  89. <p>At the heart of LLVMC is the idea of a compilation graph: vertices in
  90. this graph are tools, and edges represent a transformation path
  91. between two tools (for example, assembly source produced by the
  92. compiler can be transformed into executable code by an assembler). The
  93. compilation graph is basically a list of edges; a special node named
  94. <tt class="docutils literal"><span class="pre">root</span></tt> is used to mark graph entry points.</p>
  95. <p>Tool descriptions are represented as property lists: most properties
  96. in the example above should be self-explanatory; the <tt class="docutils literal"><span class="pre">sink</span></tt> property
  97. means that all options lacking an explicit description should be
  98. forwarded to this tool.</p>
  99. <p>The <tt class="docutils literal"><span class="pre">LanguageMap</span></tt> associates a language name with a list of suffixes
  100. and is used for deciding which toolchain corresponds to a given input
  101. file.</p>
  102. <p>To learn more about LLVMC customization, refer to the reference
  103. manual and plugin source code in the <tt class="docutils literal"><span class="pre">plugins</span></tt> directory.</p>
  104. <hr />
  105. <address>
  106. <a href="http://jigsaw.w3.org/css-validator/check/referer">
  107. <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
  108. alt="Valid CSS" /></a>
  109. <a href="http://validator.w3.org/check?uri=referer">
  110. <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
  111. alt="Valid XHTML 1.0 Transitional"/></a>
  112. <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
  113. <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
  114. Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
  115. </address></div>
  116. </div>
  117. </body>
  118. </html>