LangImpl08.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. :orphan:
  2. ========================================
  3. Kaleidoscope: Compiling to Object Code
  4. ========================================
  5. .. contents::
  6. :local:
  7. Chapter 8 Introduction
  8. ======================
  9. Welcome to Chapter 8 of the "`Implementing a language with LLVM
  10. <index.html>`_" tutorial. This chapter describes how to compile our
  11. language down to object files.
  12. Choosing a target
  13. =================
  14. LLVM has native support for cross-compilation. You can compile to the
  15. architecture of your current machine, or just as easily compile for
  16. other architectures. In this tutorial, we'll target the current
  17. machine.
  18. To specify the architecture that you want to target, we use a string
  19. called a "target triple". This takes the form
  20. ``<arch><sub>-<vendor>-<sys>-<abi>`` (see the `cross compilation docs
  21. <http://clang.llvm.org/docs/CrossCompilation.html#target-triple>`_).
  22. As an example, we can see what clang thinks is our current target
  23. triple:
  24. ::
  25. $ clang --version | grep Target
  26. Target: x86_64-unknown-linux-gnu
  27. Running this command may show something different on your machine as
  28. you might be using a different architecture or operating system to me.
  29. Fortunately, we don't need to hard-code a target triple to target the
  30. current machine. LLVM provides ``sys::getDefaultTargetTriple``, which
  31. returns the target triple of the current machine.
  32. .. code-block:: c++
  33. auto TargetTriple = sys::getDefaultTargetTriple();
  34. LLVM doesn't require us to link in all the target
  35. functionality. For example, if we're just using the JIT, we don't need
  36. the assembly printers. Similarly, if we're only targeting certain
  37. architectures, we can only link in the functionality for those
  38. architectures.
  39. For this example, we'll initialize all the targets for emitting object
  40. code.
  41. .. code-block:: c++
  42. InitializeAllTargetInfos();
  43. InitializeAllTargets();
  44. InitializeAllTargetMCs();
  45. InitializeAllAsmParsers();
  46. InitializeAllAsmPrinters();
  47. We can now use our target triple to get a ``Target``:
  48. .. code-block:: c++
  49. std::string Error;
  50. auto Target = TargetRegistry::lookupTarget(TargetTriple, Error);
  51. // Print an error and exit if we couldn't find the requested target.
  52. // This generally occurs if we've forgotten to initialise the
  53. // TargetRegistry or we have a bogus target triple.
  54. if (!Target) {
  55. errs() << Error;
  56. return 1;
  57. }
  58. Target Machine
  59. ==============
  60. We will also need a ``TargetMachine``. This class provides a complete
  61. machine description of the machine we're targeting. If we want to
  62. target a specific feature (such as SSE) or a specific CPU (such as
  63. Intel's Sandylake), we do so now.
  64. To see which features and CPUs that LLVM knows about, we can use
  65. ``llc``. For example, let's look at x86:
  66. ::
  67. $ llvm-as < /dev/null | llc -march=x86 -mattr=help
  68. Available CPUs for this target:
  69. amdfam10 - Select the amdfam10 processor.
  70. athlon - Select the athlon processor.
  71. athlon-4 - Select the athlon-4 processor.
  72. ...
  73. Available features for this target:
  74. 16bit-mode - 16-bit mode (i8086).
  75. 32bit-mode - 32-bit mode (80386).
  76. 3dnow - Enable 3DNow! instructions.
  77. 3dnowa - Enable 3DNow! Athlon instructions.
  78. ...
  79. For our example, we'll use the generic CPU without any additional
  80. features, options or relocation model.
  81. .. code-block:: c++
  82. auto CPU = "generic";
  83. auto Features = "";
  84. TargetOptions opt;
  85. auto RM = Optional<Reloc::Model>();
  86. auto TargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);
  87. Configuring the Module
  88. ======================
  89. We're now ready to configure our module, to specify the target and
  90. data layout. This isn't strictly necessary, but the `frontend
  91. performance guide <../Frontend/PerformanceTips.html>`_ recommends
  92. this. Optimizations benefit from knowing about the target and data
  93. layout.
  94. .. code-block:: c++
  95. TheModule->setDataLayout(TargetMachine->createDataLayout());
  96. TheModule->setTargetTriple(TargetTriple);
  97. Emit Object Code
  98. ================
  99. We're ready to emit object code! Let's define where we want to write
  100. our file to:
  101. .. code-block:: c++
  102. auto Filename = "output.o";
  103. std::error_code EC;
  104. raw_fd_ostream dest(Filename, EC, sys::fs::OF_None);
  105. if (EC) {
  106. errs() << "Could not open file: " << EC.message();
  107. return 1;
  108. }
  109. Finally, we define a pass that emits object code, then we run that
  110. pass:
  111. .. code-block:: c++
  112. legacy::PassManager pass;
  113. auto FileType = TargetMachine::CGFT_ObjectFile;
  114. if (TargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType)) {
  115. errs() << "TargetMachine can't emit a file of this type";
  116. return 1;
  117. }
  118. pass.run(*TheModule);
  119. dest.flush();
  120. Putting It All Together
  121. =======================
  122. Does it work? Let's give it a try. We need to compile our code, but
  123. note that the arguments to ``llvm-config`` are different to the previous chapters.
  124. ::
  125. $ clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs all` -o toy
  126. Let's run it, and define a simple ``average`` function. Press Ctrl-D
  127. when you're done.
  128. ::
  129. $ ./toy
  130. ready> def average(x y) (x + y) * 0.5;
  131. ^D
  132. Wrote output.o
  133. We have an object file! To test it, let's write a simple program and
  134. link it with our output. Here's the source code:
  135. .. code-block:: c++
  136. #include <iostream>
  137. extern "C" {
  138. double average(double, double);
  139. }
  140. int main() {
  141. std::cout << "average of 3.0 and 4.0: " << average(3.0, 4.0) << std::endl;
  142. }
  143. We link our program to output.o and check the result is what we
  144. expected:
  145. ::
  146. $ clang++ main.cpp output.o -o main
  147. $ ./main
  148. average of 3.0 and 4.0: 3.5
  149. Full Code Listing
  150. =================
  151. .. literalinclude:: ../../../examples/Kaleidoscope/Chapter8/toy.cpp
  152. :language: c++
  153. `Next: Adding Debug Information <LangImpl09.html>`_