1 ========================================
2 Kaleidoscope: Compiling to Object Code
3 ========================================
11 Welcome to Chapter 8 of the "`Implementing a language with LLVM
12 <index.html>`_" tutorial. This chapter describes how to compile our
13 language down to object files.
18 LLVM has native support for cross-compilation. You can compile to the
19 architecture of your current machine, or just as easily compile for
20 other architectures. In this tutorial, we'll target the current
23 To specify the architecture that you want to target, we use a string
24 called a "target triple". This takes the form
25 ``<arch><sub>-<vendor>-<sys>-<abi>`` (see the `cross compilation docs
26 <http://clang.llvm.org/docs/CrossCompilation.html#target-triple>`_).
28 As an example, we can see what clang thinks is our current target
33 $ clang --version | grep Target
34 Target: x86_64-unknown-linux-gnu
36 Running this command may show something different on your machine as
37 you might be using a different architecture or operating system to me.
39 Fortunately, we don't need to hard-code a target triple to target the
40 current machine. LLVM provides ``sys::getDefaultTargetTriple``, which
41 returns the target triple of the current machine.
45 auto TargetTriple = sys::getDefaultTargetTriple();
47 LLVM doesn't require us to link in all the target
48 functionality. For example, if we're just using the JIT, we don't need
49 the assembly printers. Similarly, if we're only targeting certain
50 architectures, we can only link in the functionality for those
53 For this example, we'll initialize all the targets for emitting object
58 InitializeAllTargetInfos();
59 InitializeAllTargets();
60 InitializeAllTargetMCs();
61 InitializeAllAsmParsers();
62 InitializeAllAsmPrinters();
64 We can now use our target triple to get a ``Target``:
69 auto Target = TargetRegistry::lookupTarget(TargetTriple, Error);
71 // Print an error and exit if we couldn't find the requested target.
72 // This generally occurs if we've forgotten to initialise the
73 // TargetRegistry or we have a bogus target triple.
82 We will also need a ``TargetMachine``. This class provides a complete
83 machine description of the machine we're targeting. If we want to
84 target a specific feature (such as SSE) or a specific CPU (such as
85 Intel's Sandylake), we do so now.
87 To see which features and CPUs that LLVM knows about, we can use
88 ``llc``. For example, let's look at x86:
92 $ llvm-as < /dev/null | llc -march=x86 -mattr=help
93 Available CPUs for this target:
95 amdfam10 - Select the amdfam10 processor.
96 athlon - Select the athlon processor.
97 athlon-4 - Select the athlon-4 processor.
100 Available features for this target:
102 16bit-mode - 16-bit mode (i8086).
103 32bit-mode - 32-bit mode (80386).
104 3dnow - Enable 3DNow! instructions.
105 3dnowa - Enable 3DNow! Athlon instructions.
108 For our example, we'll use the generic CPU without any additional
109 features, options or relocation model.
113 auto CPU = "generic";
117 auto RM = Optional<Reloc::Model>();
118 auto TargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);
121 Configuring the Module
122 ======================
124 We're now ready to configure our module, to specify the target and
125 data layout. This isn't strictly necessary, but the `frontend
126 performance guide <../Frontend/PerformanceTips.html>`_ recommends
127 this. Optimizations benefit from knowing about the target and data
132 TheModule->setDataLayout(TargetMachine->createDataLayout());
133 TheModule->setTargetTriple(TargetTriple);
138 We're ready to emit object code! Let's define where we want to write
143 auto Filename = "output.o";
145 raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
148 errs() << "Could not open file: " << EC.message();
152 Finally, we define a pass that emits object code, then we run that
157 legacy::PassManager pass;
158 auto FileType = TargetMachine::CGFT_ObjectFile;
160 if (TargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType)) {
161 errs() << "TargetMachine can't emit a file of this type";
165 pass.run(*TheModule);
168 Putting It All Together
169 =======================
171 Does it work? Let's give it a try. We need to compile our code, but
172 note that the arguments to ``llvm-config`` are different to the previous chapters.
176 $ clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs all` -o toy
178 Let's run it, and define a simple ``average`` function. Press Ctrl-D
184 ready> def average(x y) (x + y) * 0.5;
188 We have an object file! To test it, let's write a simple program and
189 link it with our output. Here's the source code:
196 double average(double, double);
200 std::cout << "average of 3.0 and 4.0: " << average(3.0, 4.0) << std::endl;
203 We link our program to output.o and check the result is what we
208 $ clang++ main.cpp output.o -o main
210 average of 3.0 and 4.0: 3.5
215 .. literalinclude:: ../../examples/Kaleidoscope/Chapter8/toy.cpp
218 `Next: Adding Debug Information <LangImpl09.html>`_