1*9880d681SAndroid Build Coastguard Worker======================================== 2*9880d681SAndroid Build Coastguard Worker Kaleidoscope: Compiling to Object Code 3*9880d681SAndroid Build Coastguard Worker======================================== 4*9880d681SAndroid Build Coastguard Worker 5*9880d681SAndroid Build Coastguard Worker.. contents:: 6*9880d681SAndroid Build Coastguard Worker :local: 7*9880d681SAndroid Build Coastguard Worker 8*9880d681SAndroid Build Coastguard WorkerChapter 8 Introduction 9*9880d681SAndroid Build Coastguard Worker====================== 10*9880d681SAndroid Build Coastguard Worker 11*9880d681SAndroid Build Coastguard WorkerWelcome to Chapter 8 of the "`Implementing a language with LLVM 12*9880d681SAndroid Build Coastguard Worker<index.html>`_" tutorial. This chapter describes how to compile our 13*9880d681SAndroid Build Coastguard Workerlanguage down to object files. 14*9880d681SAndroid Build Coastguard Worker 15*9880d681SAndroid Build Coastguard WorkerChoosing a target 16*9880d681SAndroid Build Coastguard Worker================= 17*9880d681SAndroid Build Coastguard Worker 18*9880d681SAndroid Build Coastguard WorkerLLVM has native support for cross-compilation. You can compile to the 19*9880d681SAndroid Build Coastguard Workerarchitecture of your current machine, or just as easily compile for 20*9880d681SAndroid Build Coastguard Workerother architectures. In this tutorial, we'll target the current 21*9880d681SAndroid Build Coastguard Workermachine. 22*9880d681SAndroid Build Coastguard Worker 23*9880d681SAndroid Build Coastguard WorkerTo specify the architecture that you want to target, we use a string 24*9880d681SAndroid Build Coastguard Workercalled a "target triple". This takes the form 25*9880d681SAndroid Build Coastguard Worker``<arch><sub>-<vendor>-<sys>-<abi>`` (see the `cross compilation docs 26*9880d681SAndroid Build Coastguard Worker<http://clang.llvm.org/docs/CrossCompilation.html#target-triple>`_). 27*9880d681SAndroid Build Coastguard Worker 28*9880d681SAndroid Build Coastguard WorkerAs an example, we can see what clang thinks is our current target 29*9880d681SAndroid Build Coastguard Workertriple: 30*9880d681SAndroid Build Coastguard Worker 31*9880d681SAndroid Build Coastguard Worker:: 32*9880d681SAndroid Build Coastguard Worker 33*9880d681SAndroid Build Coastguard Worker $ clang --version | grep Target 34*9880d681SAndroid Build Coastguard Worker Target: x86_64-unknown-linux-gnu 35*9880d681SAndroid Build Coastguard Worker 36*9880d681SAndroid Build Coastguard WorkerRunning this command may show something different on your machine as 37*9880d681SAndroid Build Coastguard Workeryou might be using a different architecture or operating system to me. 38*9880d681SAndroid Build Coastguard Worker 39*9880d681SAndroid Build Coastguard WorkerFortunately, we don't need to hard-code a target triple to target the 40*9880d681SAndroid Build Coastguard Workercurrent machine. LLVM provides ``sys::getDefaultTargetTriple``, which 41*9880d681SAndroid Build Coastguard Workerreturns the target triple of the current machine. 42*9880d681SAndroid Build Coastguard Worker 43*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 44*9880d681SAndroid Build Coastguard Worker 45*9880d681SAndroid Build Coastguard Worker auto TargetTriple = sys::getDefaultTargetTriple(); 46*9880d681SAndroid Build Coastguard Worker 47*9880d681SAndroid Build Coastguard WorkerLLVM doesn't require us to to link in all the target 48*9880d681SAndroid Build Coastguard Workerfunctionality. For example, if we're just using the JIT, we don't need 49*9880d681SAndroid Build Coastguard Workerthe assembly printers. Similarly, if we're only targeting certain 50*9880d681SAndroid Build Coastguard Workerarchitectures, we can only link in the functionality for those 51*9880d681SAndroid Build Coastguard Workerarchitectures. 52*9880d681SAndroid Build Coastguard Worker 53*9880d681SAndroid Build Coastguard WorkerFor this example, we'll initialize all the targets for emitting object 54*9880d681SAndroid Build Coastguard Workercode. 55*9880d681SAndroid Build Coastguard Worker 56*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 57*9880d681SAndroid Build Coastguard Worker 58*9880d681SAndroid Build Coastguard Worker InitializeAllTargetInfos(); 59*9880d681SAndroid Build Coastguard Worker InitializeAllTargets(); 60*9880d681SAndroid Build Coastguard Worker InitializeAllTargetMCs(); 61*9880d681SAndroid Build Coastguard Worker InitializeAllAsmParsers(); 62*9880d681SAndroid Build Coastguard Worker InitializeAllAsmPrinters(); 63*9880d681SAndroid Build Coastguard Worker 64*9880d681SAndroid Build Coastguard WorkerWe can now use our target triple to get a ``Target``: 65*9880d681SAndroid Build Coastguard Worker 66*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 67*9880d681SAndroid Build Coastguard Worker 68*9880d681SAndroid Build Coastguard Worker std::string Error; 69*9880d681SAndroid Build Coastguard Worker auto Target = TargetRegistry::lookupTarget(TargetTriple, Error); 70*9880d681SAndroid Build Coastguard Worker 71*9880d681SAndroid Build Coastguard Worker // Print an error and exit if we couldn't find the requested target. 72*9880d681SAndroid Build Coastguard Worker // This generally occurs if we've forgotten to initialise the 73*9880d681SAndroid Build Coastguard Worker // TargetRegistry or we have a bogus target triple. 74*9880d681SAndroid Build Coastguard Worker if (!Target) { 75*9880d681SAndroid Build Coastguard Worker errs() << Error; 76*9880d681SAndroid Build Coastguard Worker return 1; 77*9880d681SAndroid Build Coastguard Worker } 78*9880d681SAndroid Build Coastguard Worker 79*9880d681SAndroid Build Coastguard WorkerTarget Machine 80*9880d681SAndroid Build Coastguard Worker============== 81*9880d681SAndroid Build Coastguard Worker 82*9880d681SAndroid Build Coastguard WorkerWe will also need a ``TargetMachine``. This class provides a complete 83*9880d681SAndroid Build Coastguard Workermachine description of the machine we're targeting. If we want to 84*9880d681SAndroid Build Coastguard Workertarget a specific feature (such as SSE) or a specific CPU (such as 85*9880d681SAndroid Build Coastguard WorkerIntel's Sandylake), we do so now. 86*9880d681SAndroid Build Coastguard Worker 87*9880d681SAndroid Build Coastguard WorkerTo see which features and CPUs that LLVM knows about, we can use 88*9880d681SAndroid Build Coastguard Worker``llc``. For example, let's look at x86: 89*9880d681SAndroid Build Coastguard Worker 90*9880d681SAndroid Build Coastguard Worker:: 91*9880d681SAndroid Build Coastguard Worker 92*9880d681SAndroid Build Coastguard Worker $ llvm-as < /dev/null | llc -march=x86 -mattr=help 93*9880d681SAndroid Build Coastguard Worker Available CPUs for this target: 94*9880d681SAndroid Build Coastguard Worker 95*9880d681SAndroid Build Coastguard Worker amdfam10 - Select the amdfam10 processor. 96*9880d681SAndroid Build Coastguard Worker athlon - Select the athlon processor. 97*9880d681SAndroid Build Coastguard Worker athlon-4 - Select the athlon-4 processor. 98*9880d681SAndroid Build Coastguard Worker ... 99*9880d681SAndroid Build Coastguard Worker 100*9880d681SAndroid Build Coastguard Worker Available features for this target: 101*9880d681SAndroid Build Coastguard Worker 102*9880d681SAndroid Build Coastguard Worker 16bit-mode - 16-bit mode (i8086). 103*9880d681SAndroid Build Coastguard Worker 32bit-mode - 32-bit mode (80386). 104*9880d681SAndroid Build Coastguard Worker 3dnow - Enable 3DNow! instructions. 105*9880d681SAndroid Build Coastguard Worker 3dnowa - Enable 3DNow! Athlon instructions. 106*9880d681SAndroid Build Coastguard Worker ... 107*9880d681SAndroid Build Coastguard Worker 108*9880d681SAndroid Build Coastguard WorkerFor our example, we'll use the generic CPU without any additional 109*9880d681SAndroid Build Coastguard Workerfeatures, options or relocation model. 110*9880d681SAndroid Build Coastguard Worker 111*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 112*9880d681SAndroid Build Coastguard Worker 113*9880d681SAndroid Build Coastguard Worker auto CPU = "generic"; 114*9880d681SAndroid Build Coastguard Worker auto Features = ""; 115*9880d681SAndroid Build Coastguard Worker 116*9880d681SAndroid Build Coastguard Worker TargetOptions opt; 117*9880d681SAndroid Build Coastguard Worker auto RM = Optional<Reloc::Model>(); 118*9880d681SAndroid Build Coastguard Worker auto TargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM); 119*9880d681SAndroid Build Coastguard Worker 120*9880d681SAndroid Build Coastguard Worker 121*9880d681SAndroid Build Coastguard WorkerConfiguring the Module 122*9880d681SAndroid Build Coastguard Worker====================== 123*9880d681SAndroid Build Coastguard Worker 124*9880d681SAndroid Build Coastguard WorkerWe're now ready to configure our module, to specify the target and 125*9880d681SAndroid Build Coastguard Workerdata layout. This isn't strictly necessary, but the `frontend 126*9880d681SAndroid Build Coastguard Workerperformance guide <../Frontend/PerformanceTips.html>`_ recommends 127*9880d681SAndroid Build Coastguard Workerthis. Optimizations benefit from knowing about the target and data 128*9880d681SAndroid Build Coastguard Workerlayout. 129*9880d681SAndroid Build Coastguard Worker 130*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 131*9880d681SAndroid Build Coastguard Worker 132*9880d681SAndroid Build Coastguard Worker TheModule->setDataLayout(TargetMachine->createDataLayout()); 133*9880d681SAndroid Build Coastguard Worker TheModule->setTargetTriple(TargetTriple); 134*9880d681SAndroid Build Coastguard Worker 135*9880d681SAndroid Build Coastguard WorkerEmit Object Code 136*9880d681SAndroid Build Coastguard Worker================ 137*9880d681SAndroid Build Coastguard Worker 138*9880d681SAndroid Build Coastguard WorkerWe're ready to emit object code! Let's define where we want to write 139*9880d681SAndroid Build Coastguard Workerour file to: 140*9880d681SAndroid Build Coastguard Worker 141*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 142*9880d681SAndroid Build Coastguard Worker 143*9880d681SAndroid Build Coastguard Worker auto Filename = "output.o"; 144*9880d681SAndroid Build Coastguard Worker std::error_code EC; 145*9880d681SAndroid Build Coastguard Worker raw_fd_ostream dest(Filename, EC, sys::fs::F_None); 146*9880d681SAndroid Build Coastguard Worker 147*9880d681SAndroid Build Coastguard Worker if (EC) { 148*9880d681SAndroid Build Coastguard Worker errs() << "Could not open file: " << EC.message(); 149*9880d681SAndroid Build Coastguard Worker return 1; 150*9880d681SAndroid Build Coastguard Worker } 151*9880d681SAndroid Build Coastguard Worker 152*9880d681SAndroid Build Coastguard WorkerFinally, we define a pass that emits object code, then we run that 153*9880d681SAndroid Build Coastguard Workerpass: 154*9880d681SAndroid Build Coastguard Worker 155*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 156*9880d681SAndroid Build Coastguard Worker 157*9880d681SAndroid Build Coastguard Worker legacy::PassManager pass; 158*9880d681SAndroid Build Coastguard Worker auto FileType = TargetMachine::CGFT_ObjectFile; 159*9880d681SAndroid Build Coastguard Worker 160*9880d681SAndroid Build Coastguard Worker if (TargetMachine->addPassesToEmitFile(pass, dest, FileType)) { 161*9880d681SAndroid Build Coastguard Worker errs() << "TargetMachine can't emit a file of this type"; 162*9880d681SAndroid Build Coastguard Worker return 1; 163*9880d681SAndroid Build Coastguard Worker } 164*9880d681SAndroid Build Coastguard Worker 165*9880d681SAndroid Build Coastguard Worker pass.run(*TheModule); 166*9880d681SAndroid Build Coastguard Worker dest.flush(); 167*9880d681SAndroid Build Coastguard Worker 168*9880d681SAndroid Build Coastguard WorkerPutting It All Together 169*9880d681SAndroid Build Coastguard Worker======================= 170*9880d681SAndroid Build Coastguard Worker 171*9880d681SAndroid Build Coastguard WorkerDoes it work? Let's give it a try. We need to compile our code, but 172*9880d681SAndroid Build Coastguard Workernote that the arguments to ``llvm-config`` are different to the previous chapters. 173*9880d681SAndroid Build Coastguard Worker 174*9880d681SAndroid Build Coastguard Worker:: 175*9880d681SAndroid Build Coastguard Worker 176*9880d681SAndroid Build Coastguard Worker $ clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs all` -o toy 177*9880d681SAndroid Build Coastguard Worker 178*9880d681SAndroid Build Coastguard WorkerLet's run it, and define a simple ``average`` function. Press Ctrl-D 179*9880d681SAndroid Build Coastguard Workerwhen you're done. 180*9880d681SAndroid Build Coastguard Worker 181*9880d681SAndroid Build Coastguard Worker:: 182*9880d681SAndroid Build Coastguard Worker 183*9880d681SAndroid Build Coastguard Worker $ ./toy 184*9880d681SAndroid Build Coastguard Worker ready> def average(x y) (x + y) * 0.5; 185*9880d681SAndroid Build Coastguard Worker ^D 186*9880d681SAndroid Build Coastguard Worker Wrote output.o 187*9880d681SAndroid Build Coastguard Worker 188*9880d681SAndroid Build Coastguard WorkerWe have an object file! To test it, let's write a simple program and 189*9880d681SAndroid Build Coastguard Workerlink it with our output. Here's the source code: 190*9880d681SAndroid Build Coastguard Worker 191*9880d681SAndroid Build Coastguard Worker.. code-block:: c++ 192*9880d681SAndroid Build Coastguard Worker 193*9880d681SAndroid Build Coastguard Worker #include <iostream> 194*9880d681SAndroid Build Coastguard Worker 195*9880d681SAndroid Build Coastguard Worker extern "C" { 196*9880d681SAndroid Build Coastguard Worker double average(double, double); 197*9880d681SAndroid Build Coastguard Worker } 198*9880d681SAndroid Build Coastguard Worker 199*9880d681SAndroid Build Coastguard Worker int main() { 200*9880d681SAndroid Build Coastguard Worker std::cout << "average of 3.0 and 4.0: " << average(3.0, 4.0) << std::endl; 201*9880d681SAndroid Build Coastguard Worker } 202*9880d681SAndroid Build Coastguard Worker 203*9880d681SAndroid Build Coastguard WorkerWe link our program to output.o and check the result is what we 204*9880d681SAndroid Build Coastguard Workerexpected: 205*9880d681SAndroid Build Coastguard Worker 206*9880d681SAndroid Build Coastguard Worker:: 207*9880d681SAndroid Build Coastguard Worker 208*9880d681SAndroid Build Coastguard Worker $ clang++ main.cpp output.o -o main 209*9880d681SAndroid Build Coastguard Worker $ ./main 210*9880d681SAndroid Build Coastguard Worker average of 3.0 and 4.0: 3.5 211*9880d681SAndroid Build Coastguard Worker 212*9880d681SAndroid Build Coastguard WorkerFull Code Listing 213*9880d681SAndroid Build Coastguard Worker================= 214*9880d681SAndroid Build Coastguard Worker 215*9880d681SAndroid Build Coastguard Worker.. literalinclude:: ../../examples/Kaleidoscope/Chapter8/toy.cpp 216*9880d681SAndroid Build Coastguard Worker :language: c++ 217*9880d681SAndroid Build Coastguard Worker 218*9880d681SAndroid Build Coastguard Worker`Next: Adding Debug Information <LangImpl09.html>`_ 219