1d80f118eSChris Lattner======================================== 2d80f118eSChris LattnerKaleidoscope: Code generation to LLVM IR 3d80f118eSChris Lattner======================================== 4d80f118eSChris Lattner 5d80f118eSChris Lattner.. contents:: 6d80f118eSChris Lattner :local: 7d80f118eSChris Lattner 8d80f118eSChris LattnerChapter 3 Introduction 9d80f118eSChris Lattner====================== 10d80f118eSChris Lattner 11d80f118eSChris LattnerWelcome to Chapter 3 of the "`Implementing a language with 12d80f118eSChris LattnerLLVM <index.html>`_" tutorial. This chapter shows you how to transform 13d80f118eSChris Lattnerthe `Abstract Syntax Tree <LangImpl02.html>`_, built in Chapter 2, into 14d80f118eSChris LattnerLLVM IR. This will teach you a little bit about how LLVM does things, as 15d80f118eSChris Lattnerwell as demonstrate how easy it is to use. It's much more work to build 16d80f118eSChris Lattnera lexer and parser than it is to generate LLVM IR code. :) 17d80f118eSChris Lattner 18d80f118eSChris Lattner**Please note**: the code in this chapter and later require LLVM 3.7 or 19d80f118eSChris Lattnerlater. LLVM 3.6 and before will not work with it. Also note that you 20d80f118eSChris Lattnerneed to use a version of this tutorial that matches your LLVM release: 21d80f118eSChris LattnerIf you are using an official LLVM release, use the version of the 22d80f118eSChris Lattnerdocumentation included with your release or on the `llvm.org releases 2372fd1033SSylvestre Ledrupage <https://llvm.org/releases/>`_. 24d80f118eSChris Lattner 25d80f118eSChris LattnerCode Generation Setup 26d80f118eSChris Lattner===================== 27d80f118eSChris Lattner 28d80f118eSChris LattnerIn order to generate LLVM IR, we want some simple setup to get started. 29d80f118eSChris LattnerFirst we define virtual code generation (codegen) methods in each AST 30d80f118eSChris Lattnerclass: 31d80f118eSChris Lattner 32d80f118eSChris Lattner.. code-block:: c++ 33d80f118eSChris Lattner 34d80f118eSChris Lattner /// ExprAST - Base class for all expression nodes. 35d80f118eSChris Lattner class ExprAST { 36d80f118eSChris Lattner public: 37*153431ecSRoman Sokolkov virtual ~ExprAST() = default; 38d80f118eSChris Lattner virtual Value *codegen() = 0; 39d80f118eSChris Lattner }; 40d80f118eSChris Lattner 41d80f118eSChris Lattner /// NumberExprAST - Expression class for numeric literals like "1.0". 42d80f118eSChris Lattner class NumberExprAST : public ExprAST { 43d80f118eSChris Lattner double Val; 44d80f118eSChris Lattner 45d80f118eSChris Lattner public: 46d80f118eSChris Lattner NumberExprAST(double Val) : Val(Val) {} 47*153431ecSRoman Sokolkov Value *codegen() override; 48d80f118eSChris Lattner }; 49d80f118eSChris Lattner ... 50d80f118eSChris Lattner 51d80f118eSChris LattnerThe codegen() method says to emit IR for that AST node along with all 52d80f118eSChris Lattnerthe things it depends on, and they all return an LLVM Value object. 53d80f118eSChris Lattner"Value" is the class used to represent a "`Static Single Assignment 54d80f118eSChris Lattner(SSA) <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_ 55d80f118eSChris Lattnerregister" or "SSA value" in LLVM. The most distinct aspect of SSA values 56d80f118eSChris Lattneris that their value is computed as the related instruction executes, and 57d80f118eSChris Lattnerit does not get a new value until (and if) the instruction re-executes. 58d80f118eSChris LattnerIn other words, there is no way to "change" an SSA value. For more 59d80f118eSChris Lattnerinformation, please read up on `Static Single 60d80f118eSChris LattnerAssignment <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_ 61d80f118eSChris Lattner- the concepts are really quite natural once you grok them. 62d80f118eSChris Lattner 63d80f118eSChris LattnerNote that instead of adding virtual methods to the ExprAST class 64d80f118eSChris Lattnerhierarchy, it could also make sense to use a `visitor 65d80f118eSChris Lattnerpattern <http://en.wikipedia.org/wiki/Visitor_pattern>`_ or some other 66d80f118eSChris Lattnerway to model this. Again, this tutorial won't dwell on good software 67d80f118eSChris Lattnerengineering practices: for our purposes, adding a virtual method is 68d80f118eSChris Lattnersimplest. 69d80f118eSChris Lattner 7028da5759SChris MorinThe second thing we want is a "LogError" method like we used for the 71d80f118eSChris Lattnerparser, which will be used to report errors found during code generation 72d80f118eSChris Lattner(for example, use of an undeclared parameter): 73d80f118eSChris Lattner 74d80f118eSChris Lattner.. code-block:: c++ 75d80f118eSChris Lattner 76d80f118eSChris Lattner static LLVMContext TheContext; 77d80f118eSChris Lattner static IRBuilder<> Builder(TheContext); 78d80f118eSChris Lattner static std::unique_ptr<Module> TheModule; 79d80f118eSChris Lattner static std::map<std::string, Value *> NamedValues; 80d80f118eSChris Lattner 81d80f118eSChris Lattner Value *LogErrorV(const char *Str) { 82d80f118eSChris Lattner LogError(Str); 83d80f118eSChris Lattner return nullptr; 84d80f118eSChris Lattner } 85d80f118eSChris Lattner 86d80f118eSChris LattnerThe static variables will be used during code generation. ``TheContext`` 87d80f118eSChris Lattneris an opaque object that owns a lot of core LLVM data structures, such as 88d80f118eSChris Lattnerthe type and constant value tables. We don't need to understand it in 89d80f118eSChris Lattnerdetail, we just need a single instance to pass into APIs that require it. 90d80f118eSChris Lattner 91d80f118eSChris LattnerThe ``Builder`` object is a helper object that makes it easy to generate 92d80f118eSChris LattnerLLVM instructions. Instances of the 93e15996b5SHan Seoul-Oh`IRBuilder <https://llvm.org/doxygen/IRBuilder_8h_source.html>`_ 94d80f118eSChris Lattnerclass template keep track of the current place to insert instructions 95d80f118eSChris Lattnerand has methods to create new instructions. 96d80f118eSChris Lattner 97d80f118eSChris Lattner``TheModule`` is an LLVM construct that contains functions and global 98d80f118eSChris Lattnervariables. In many ways, it is the top-level structure that the LLVM IR 99d80f118eSChris Lattneruses to contain code. It will own the memory for all of the IR that we 100d80f118eSChris Lattnergenerate, which is why the codegen() method returns a raw Value\*, 101d80f118eSChris Lattnerrather than a unique_ptr<Value>. 102d80f118eSChris Lattner 103d80f118eSChris LattnerThe ``NamedValues`` map keeps track of which values are defined in the 104d80f118eSChris Lattnercurrent scope and what their LLVM representation is. (In other words, it 105d80f118eSChris Lattneris a symbol table for the code). In this form of Kaleidoscope, the only 106d80f118eSChris Lattnerthings that can be referenced are function parameters. As such, function 107d80f118eSChris Lattnerparameters will be in this map when generating code for their function 108d80f118eSChris Lattnerbody. 109d80f118eSChris Lattner 110d80f118eSChris LattnerWith these basics in place, we can start talking about how to generate 111d80f118eSChris Lattnercode for each expression. Note that this assumes that the ``Builder`` 112d80f118eSChris Lattnerhas been set up to generate code *into* something. For now, we'll assume 113d80f118eSChris Lattnerthat this has already been done, and we'll just use it to emit code. 114d80f118eSChris Lattner 115d80f118eSChris LattnerExpression Code Generation 116d80f118eSChris Lattner========================== 117d80f118eSChris Lattner 118d80f118eSChris LattnerGenerating LLVM code for expression nodes is very straightforward: less 119d80f118eSChris Lattnerthan 45 lines of commented code for all four of our expression nodes. 120d80f118eSChris LattnerFirst we'll do numeric literals: 121d80f118eSChris Lattner 122d80f118eSChris Lattner.. code-block:: c++ 123d80f118eSChris Lattner 124d80f118eSChris Lattner Value *NumberExprAST::codegen() { 125d80f118eSChris Lattner return ConstantFP::get(TheContext, APFloat(Val)); 126d80f118eSChris Lattner } 127d80f118eSChris Lattner 128d80f118eSChris LattnerIn the LLVM IR, numeric constants are represented with the 129d80f118eSChris Lattner``ConstantFP`` class, which holds the numeric value in an ``APFloat`` 130d80f118eSChris Lattnerinternally (``APFloat`` has the capability of holding floating point 131d80f118eSChris Lattnerconstants of Arbitrary Precision). This code basically just creates 132d80f118eSChris Lattnerand returns a ``ConstantFP``. Note that in the LLVM IR that constants 133d80f118eSChris Lattnerare all uniqued together and shared. For this reason, the API uses the 134d80f118eSChris Lattner"foo::get(...)" idiom instead of "new foo(..)" or "foo::Create(..)". 135d80f118eSChris Lattner 136d80f118eSChris Lattner.. code-block:: c++ 137d80f118eSChris Lattner 138d80f118eSChris Lattner Value *VariableExprAST::codegen() { 139d80f118eSChris Lattner // Look this variable up in the function. 140d80f118eSChris Lattner Value *V = NamedValues[Name]; 141d80f118eSChris Lattner if (!V) 142d80f118eSChris Lattner LogErrorV("Unknown variable name"); 143d80f118eSChris Lattner return V; 144d80f118eSChris Lattner } 145d80f118eSChris Lattner 146d80f118eSChris LattnerReferences to variables are also quite simple using LLVM. In the simple 147d80f118eSChris Lattnerversion of Kaleidoscope, we assume that the variable has already been 148d80f118eSChris Lattneremitted somewhere and its value is available. In practice, the only 149d80f118eSChris Lattnervalues that can be in the ``NamedValues`` map are function arguments. 150d80f118eSChris LattnerThis code simply checks to see that the specified name is in the map (if 151d80f118eSChris Lattnernot, an unknown variable is being referenced) and returns the value for 152d80f118eSChris Lattnerit. In future chapters, we'll add support for `loop induction 1535864cb38SBrian Gesiakvariables <LangImpl05.html#for-loop-expression>`_ in the symbol table, and for `local 1545864cb38SBrian Gesiakvariables <LangImpl07.html#user-defined-local-variables>`_. 155d80f118eSChris Lattner 156d80f118eSChris Lattner.. code-block:: c++ 157d80f118eSChris Lattner 158d80f118eSChris Lattner Value *BinaryExprAST::codegen() { 159d80f118eSChris Lattner Value *L = LHS->codegen(); 160d80f118eSChris Lattner Value *R = RHS->codegen(); 161d80f118eSChris Lattner if (!L || !R) 162d80f118eSChris Lattner return nullptr; 163d80f118eSChris Lattner 164d80f118eSChris Lattner switch (Op) { 165d80f118eSChris Lattner case '+': 166d80f118eSChris Lattner return Builder.CreateFAdd(L, R, "addtmp"); 167d80f118eSChris Lattner case '-': 168d80f118eSChris Lattner return Builder.CreateFSub(L, R, "subtmp"); 169d80f118eSChris Lattner case '*': 170d80f118eSChris Lattner return Builder.CreateFMul(L, R, "multmp"); 171d80f118eSChris Lattner case '<': 172d80f118eSChris Lattner L = Builder.CreateFCmpULT(L, R, "cmptmp"); 173d80f118eSChris Lattner // Convert bool 0/1 to double 0.0 or 1.0 174d80f118eSChris Lattner return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), 175d80f118eSChris Lattner "booltmp"); 176d80f118eSChris Lattner default: 177d80f118eSChris Lattner return LogErrorV("invalid binary operator"); 178d80f118eSChris Lattner } 179d80f118eSChris Lattner } 180d80f118eSChris Lattner 181d80f118eSChris LattnerBinary operators start to get more interesting. The basic idea here is 182d80f118eSChris Lattnerthat we recursively emit code for the left-hand side of the expression, 183d80f118eSChris Lattnerthen the right-hand side, then we compute the result of the binary 184d80f118eSChris Lattnerexpression. In this code, we do a simple switch on the opcode to create 185d80f118eSChris Lattnerthe right LLVM instruction. 186d80f118eSChris Lattner 187d80f118eSChris LattnerIn the example above, the LLVM builder class is starting to show its 188d80f118eSChris Lattnervalue. IRBuilder knows where to insert the newly created instruction, 189d80f118eSChris Lattnerall you have to do is specify what instruction to create (e.g. with 190d80f118eSChris Lattner``CreateFAdd``), which operands to use (``L`` and ``R`` here) and 191d80f118eSChris Lattneroptionally provide a name for the generated instruction. 192d80f118eSChris Lattner 193d80f118eSChris LattnerOne nice thing about LLVM is that the name is just a hint. For instance, 194d80f118eSChris Lattnerif the code above emits multiple "addtmp" variables, LLVM will 195d80f118eSChris Lattnerautomatically provide each one with an increasing, unique numeric 196d80f118eSChris Lattnersuffix. Local value names for instructions are purely optional, but it 197d80f118eSChris Lattnermakes it much easier to read the IR dumps. 198d80f118eSChris Lattner 1995e782e74Skristina`LLVM instructions <../../LangRef.html#instruction-reference>`_ are constrained by strict 200114a8903SBill Wendlingrules: for example, the Left and Right operands of an `add 2012916489cSkristinainstruction <../../LangRef.html#add-instruction>`_ must have the same type, and the 202d80f118eSChris Lattnerresult type of the add must match the operand types. Because all values 203d80f118eSChris Lattnerin Kaleidoscope are doubles, this makes for very simple code for add, 204d80f118eSChris Lattnersub and mul. 205d80f118eSChris Lattner 206d80f118eSChris LattnerOn the other hand, LLVM specifies that the `fcmp 2072916489cSkristinainstruction <../../LangRef.html#fcmp-instruction>`_ always returns an 'i1' value (a 208d80f118eSChris Lattnerone bit integer). The problem with this is that Kaleidoscope wants the 209d80f118eSChris Lattnervalue to be a 0.0 or 1.0 value. In order to get these semantics, we 210d80f118eSChris Lattnercombine the fcmp instruction with a `uitofp 2112916489cSkristinainstruction <../../LangRef.html#uitofp-to-instruction>`_. This instruction converts its 212d80f118eSChris Lattnerinput integer into a floating point value by treating the input as an 213d80f118eSChris Lattnerunsigned value. In contrast, if we used the `sitofp 2142916489cSkristinainstruction <../../LangRef.html#sitofp-to-instruction>`_, the Kaleidoscope '<' operator 215d80f118eSChris Lattnerwould return 0.0 and -1.0, depending on the input value. 216d80f118eSChris Lattner 217d80f118eSChris Lattner.. code-block:: c++ 218d80f118eSChris Lattner 219d80f118eSChris Lattner Value *CallExprAST::codegen() { 220d80f118eSChris Lattner // Look up the name in the global module table. 221d80f118eSChris Lattner Function *CalleeF = TheModule->getFunction(Callee); 222d80f118eSChris Lattner if (!CalleeF) 223d80f118eSChris Lattner return LogErrorV("Unknown function referenced"); 224d80f118eSChris Lattner 225d80f118eSChris Lattner // If argument mismatch error. 226d80f118eSChris Lattner if (CalleeF->arg_size() != Args.size()) 227d80f118eSChris Lattner return LogErrorV("Incorrect # arguments passed"); 228d80f118eSChris Lattner 229d80f118eSChris Lattner std::vector<Value *> ArgsV; 230d80f118eSChris Lattner for (unsigned i = 0, e = Args.size(); i != e; ++i) { 231d80f118eSChris Lattner ArgsV.push_back(Args[i]->codegen()); 232d80f118eSChris Lattner if (!ArgsV.back()) 233d80f118eSChris Lattner return nullptr; 234d80f118eSChris Lattner } 235d80f118eSChris Lattner 236d80f118eSChris Lattner return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); 237d80f118eSChris Lattner } 238d80f118eSChris Lattner 239d80f118eSChris LattnerCode generation for function calls is quite straightforward with LLVM. The code 240d80f118eSChris Lattnerabove initially does a function name lookup in the LLVM Module's symbol table. 241d80f118eSChris LattnerRecall that the LLVM Module is the container that holds the functions we are 242d80f118eSChris LattnerJIT'ing. By giving each function the same name as what the user specifies, we 243d80f118eSChris Lattnercan use the LLVM symbol table to resolve function names for us. 244d80f118eSChris Lattner 245d80f118eSChris LattnerOnce we have the function to call, we recursively codegen each argument 246d80f118eSChris Lattnerthat is to be passed in, and create an LLVM `call 2472916489cSkristinainstruction <../../LangRef.html#call-instruction>`_. Note that LLVM uses the native C 248d80f118eSChris Lattnercalling conventions by default, allowing these calls to also call into 249d80f118eSChris Lattnerstandard library functions like "sin" and "cos", with no additional 250d80f118eSChris Lattnereffort. 251d80f118eSChris Lattner 252d80f118eSChris LattnerThis wraps up our handling of the four basic expressions that we have so 253d80f118eSChris Lattnerfar in Kaleidoscope. Feel free to go in and add some more. For example, 2542916489cSkristinaby browsing the `LLVM language reference <../../LangRef.html>`_ you'll find 255d80f118eSChris Lattnerseveral other interesting instructions that are really easy to plug into 256d80f118eSChris Lattnerour basic framework. 257d80f118eSChris Lattner 258d80f118eSChris LattnerFunction Code Generation 259d80f118eSChris Lattner======================== 260d80f118eSChris Lattner 261d80f118eSChris LattnerCode generation for prototypes and functions must handle a number of 262d80f118eSChris Lattnerdetails, which make their code less beautiful than expression code 263d80f118eSChris Lattnergeneration, but allows us to illustrate some important points. First, 264d80f118eSChris Lattnerlet's talk about code generation for prototypes: they are used both for 265d80f118eSChris Lattnerfunction bodies and external function declarations. The code starts 266d80f118eSChris Lattnerwith: 267d80f118eSChris Lattner 268d80f118eSChris Lattner.. code-block:: c++ 269d80f118eSChris Lattner 270d80f118eSChris Lattner Function *PrototypeAST::codegen() { 271d80f118eSChris Lattner // Make the function type: double(double,double) etc. 272d80f118eSChris Lattner std::vector<Type*> Doubles(Args.size(), 273d80f118eSChris Lattner Type::getDoubleTy(TheContext)); 274d80f118eSChris Lattner FunctionType *FT = 275d80f118eSChris Lattner FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false); 276d80f118eSChris Lattner 277d80f118eSChris Lattner Function *F = 278d80f118eSChris Lattner Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get()); 279d80f118eSChris Lattner 280d80f118eSChris LattnerThis code packs a lot of power into a few lines. Note first that this 281d80f118eSChris Lattnerfunction returns a "Function\*" instead of a "Value\*". Because a 282d80f118eSChris Lattner"prototype" really talks about the external interface for a function 283d80f118eSChris Lattner(not the value computed by an expression), it makes sense for it to 284d80f118eSChris Lattnerreturn the LLVM Function it corresponds to when codegen'd. 285d80f118eSChris Lattner 286d80f118eSChris LattnerThe call to ``FunctionType::get`` creates the ``FunctionType`` that 287d80f118eSChris Lattnershould be used for a given Prototype. Since all function arguments in 288d80f118eSChris LattnerKaleidoscope are of type double, the first line creates a vector of "N" 289d80f118eSChris LattnerLLVM double types. It then uses the ``Functiontype::get`` method to 290d80f118eSChris Lattnercreate a function type that takes "N" doubles as arguments, returns one 291d80f118eSChris Lattnerdouble as a result, and that is not vararg (the false parameter 292d80f118eSChris Lattnerindicates this). Note that Types in LLVM are uniqued just like Constants 293d80f118eSChris Lattnerare, so you don't "new" a type, you "get" it. 294d80f118eSChris Lattner 295d80f118eSChris LattnerThe final line above actually creates the IR Function corresponding to 296d80f118eSChris Lattnerthe Prototype. This indicates the type, linkage and name to use, as 297d80f118eSChris Lattnerwell as which module to insert into. "`external 2982916489cSkristinalinkage <../../LangRef.html#linkage>`_" means that the function may be 299d80f118eSChris Lattnerdefined outside the current module and/or that it is callable by 300d80f118eSChris Lattnerfunctions outside the module. The Name passed in is the name the user 301d80f118eSChris Lattnerspecified: since "``TheModule``" is specified, this name is registered 302d80f118eSChris Lattnerin "``TheModule``"s symbol table. 303d80f118eSChris Lattner 304d80f118eSChris Lattner.. code-block:: c++ 305d80f118eSChris Lattner 306d80f118eSChris Lattner // Set names for all arguments. 307d80f118eSChris Lattner unsigned Idx = 0; 308d80f118eSChris Lattner for (auto &Arg : F->args()) 309d80f118eSChris Lattner Arg.setName(Args[Idx++]); 310d80f118eSChris Lattner 311d80f118eSChris Lattner return F; 312d80f118eSChris Lattner 313d80f118eSChris LattnerFinally, we set the name of each of the function's arguments according to the 314d80f118eSChris Lattnernames given in the Prototype. This step isn't strictly necessary, but keeping 315d80f118eSChris Lattnerthe names consistent makes the IR more readable, and allows subsequent code to 316d80f118eSChris Lattnerrefer directly to the arguments for their names, rather than having to look up 317d80f118eSChris Lattnerthem up in the Prototype AST. 318d80f118eSChris Lattner 319d80f118eSChris LattnerAt this point we have a function prototype with no body. This is how LLVM IR 320d80f118eSChris Lattnerrepresents function declarations. For extern statements in Kaleidoscope, this 321d80f118eSChris Lattneris as far as we need to go. For function definitions however, we need to 322d80f118eSChris Lattnercodegen and attach a function body. 323d80f118eSChris Lattner 324d80f118eSChris Lattner.. code-block:: c++ 325d80f118eSChris Lattner 326d80f118eSChris Lattner Function *FunctionAST::codegen() { 327d80f118eSChris Lattner // First, check for an existing function from a previous 'extern' declaration. 328d80f118eSChris Lattner Function *TheFunction = TheModule->getFunction(Proto->getName()); 329d80f118eSChris Lattner 330d80f118eSChris Lattner if (!TheFunction) 331d80f118eSChris Lattner TheFunction = Proto->codegen(); 332d80f118eSChris Lattner 333d80f118eSChris Lattner if (!TheFunction) 334d80f118eSChris Lattner return nullptr; 335d80f118eSChris Lattner 336d80f118eSChris Lattner if (!TheFunction->empty()) 337d80f118eSChris Lattner return (Function*)LogErrorV("Function cannot be redefined."); 338d80f118eSChris Lattner 339d80f118eSChris Lattner 340d80f118eSChris LattnerFor function definitions, we start by searching TheModule's symbol table for an 341d80f118eSChris Lattnerexisting version of this function, in case one has already been created using an 342d80f118eSChris Lattner'extern' statement. If Module::getFunction returns null then no previous version 343d80f118eSChris Lattnerexists, so we'll codegen one from the Prototype. In either case, we want to 344d80f118eSChris Lattnerassert that the function is empty (i.e. has no body yet) before we start. 345d80f118eSChris Lattner 346d80f118eSChris Lattner.. code-block:: c++ 347d80f118eSChris Lattner 348d80f118eSChris Lattner // Create a new basic block to start insertion into. 349d80f118eSChris Lattner BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction); 350d80f118eSChris Lattner Builder.SetInsertPoint(BB); 351d80f118eSChris Lattner 352d80f118eSChris Lattner // Record the function arguments in the NamedValues map. 353d80f118eSChris Lattner NamedValues.clear(); 354d80f118eSChris Lattner for (auto &Arg : TheFunction->args()) 355d80f118eSChris Lattner NamedValues[Arg.getName()] = &Arg; 356d80f118eSChris Lattner 357d80f118eSChris LattnerNow we get to the point where the ``Builder`` is set up. The first line 358d80f118eSChris Lattnercreates a new `basic block <http://en.wikipedia.org/wiki/Basic_block>`_ 359d80f118eSChris Lattner(named "entry"), which is inserted into ``TheFunction``. The second line 360d80f118eSChris Lattnerthen tells the builder that new instructions should be inserted into the 361d80f118eSChris Lattnerend of the new basic block. Basic blocks in LLVM are an important part 362d80f118eSChris Lattnerof functions that define the `Control Flow 363d80f118eSChris LattnerGraph <http://en.wikipedia.org/wiki/Control_flow_graph>`_. Since we 364d80f118eSChris Lattnerdon't have any control flow, our functions will only contain one block 365d80f118eSChris Lattnerat this point. We'll fix this in `Chapter 5 <LangImpl05.html>`_ :). 366d80f118eSChris Lattner 367d80f118eSChris LattnerNext we add the function arguments to the NamedValues map (after first clearing 368d80f118eSChris Lattnerit out) so that they're accessible to ``VariableExprAST`` nodes. 369d80f118eSChris Lattner 370d80f118eSChris Lattner.. code-block:: c++ 371d80f118eSChris Lattner 372d80f118eSChris Lattner if (Value *RetVal = Body->codegen()) { 373d80f118eSChris Lattner // Finish off the function. 374d80f118eSChris Lattner Builder.CreateRet(RetVal); 375d80f118eSChris Lattner 376d80f118eSChris Lattner // Validate the generated code, checking for consistency. 377d80f118eSChris Lattner verifyFunction(*TheFunction); 378d80f118eSChris Lattner 379d80f118eSChris Lattner return TheFunction; 380d80f118eSChris Lattner } 381d80f118eSChris Lattner 382d80f118eSChris LattnerOnce the insertion point has been set up and the NamedValues map populated, 383d80f118eSChris Lattnerwe call the ``codegen()`` method for the root expression of the function. If no 384d80f118eSChris Lattnererror happens, this emits code to compute the expression into the entry block 385d80f118eSChris Lattnerand returns the value that was computed. Assuming no error, we then create an 3862916489cSkristinaLLVM `ret instruction <../../LangRef.html#ret-instruction>`_, which completes the function. 387d80f118eSChris LattnerOnce the function is built, we call ``verifyFunction``, which is 388d80f118eSChris Lattnerprovided by LLVM. This function does a variety of consistency checks on 389d80f118eSChris Lattnerthe generated code, to determine if our compiler is doing everything 390d80f118eSChris Lattnerright. Using this is important: it can catch a lot of bugs. Once the 391d80f118eSChris Lattnerfunction is finished and validated, we return it. 392d80f118eSChris Lattner 393d80f118eSChris Lattner.. code-block:: c++ 394d80f118eSChris Lattner 395d80f118eSChris Lattner // Error reading body, remove function. 396d80f118eSChris Lattner TheFunction->eraseFromParent(); 397d80f118eSChris Lattner return nullptr; 398d80f118eSChris Lattner } 399d80f118eSChris Lattner 400d80f118eSChris LattnerThe only piece left here is handling of the error case. For simplicity, 401d80f118eSChris Lattnerwe handle this by merely deleting the function we produced with the 402d80f118eSChris Lattner``eraseFromParent`` method. This allows the user to redefine a function 403d80f118eSChris Lattnerthat they incorrectly typed in before: if we didn't delete it, it would 404d80f118eSChris Lattnerlive in the symbol table, with a body, preventing future redefinition. 405d80f118eSChris Lattner 406d80f118eSChris LattnerThis code does have a bug, though: If the ``FunctionAST::codegen()`` method 407d80f118eSChris Lattnerfinds an existing IR Function, it does not validate its signature against the 408d80f118eSChris Lattnerdefinition's own prototype. This means that an earlier 'extern' declaration will 409d80f118eSChris Lattnertake precedence over the function definition's signature, which can cause 410d80f118eSChris Lattnercodegen to fail, for instance if the function arguments are named differently. 411d80f118eSChris LattnerThere are a number of ways to fix this bug, see what you can come up with! Here 412d80f118eSChris Lattneris a testcase: 413d80f118eSChris Lattner 414d80f118eSChris Lattner:: 415d80f118eSChris Lattner 416d80f118eSChris Lattner extern foo(a); # ok, defines foo. 417d80f118eSChris Lattner def foo(b) b; # Error: Unknown variable name. (decl using 'a' takes precedence). 418d80f118eSChris Lattner 419d80f118eSChris LattnerDriver Changes and Closing Thoughts 420d80f118eSChris Lattner=================================== 421d80f118eSChris Lattner 422d80f118eSChris LattnerFor now, code generation to LLVM doesn't really get us much, except that 423d80f118eSChris Lattnerwe can look at the pretty IR calls. The sample code inserts calls to 424d80f118eSChris Lattnercodegen into the "``HandleDefinition``", "``HandleExtern``" etc 425d80f118eSChris Lattnerfunctions, and then dumps out the LLVM IR. This gives a nice way to look 426d80f118eSChris Lattnerat the LLVM IR for simple functions. For example: 427d80f118eSChris Lattner 428d80f118eSChris Lattner:: 429d80f118eSChris Lattner 430d80f118eSChris Lattner ready> 4+5; 431d80f118eSChris Lattner Read top-level expression: 432d80f118eSChris Lattner define double @0() { 433d80f118eSChris Lattner entry: 434d80f118eSChris Lattner ret double 9.000000e+00 435d80f118eSChris Lattner } 436d80f118eSChris Lattner 437d80f118eSChris LattnerNote how the parser turns the top-level expression into anonymous 438d80f118eSChris Lattnerfunctions for us. This will be handy when we add `JIT 4395864cb38SBrian Gesiaksupport <LangImpl04.html#adding-a-jit-compiler>`_ in the next chapter. Also note that the 440d80f118eSChris Lattnercode is very literally transcribed, no optimizations are being performed 441d80f118eSChris Lattnerexcept simple constant folding done by IRBuilder. We will `add 4425864cb38SBrian Gesiakoptimizations <LangImpl04.html#trivial-constant-folding>`_ explicitly in the next 443d80f118eSChris Lattnerchapter. 444d80f118eSChris Lattner 445d80f118eSChris Lattner:: 446d80f118eSChris Lattner 447d80f118eSChris Lattner ready> def foo(a b) a*a + 2*a*b + b*b; 448d80f118eSChris Lattner Read function definition: 449d80f118eSChris Lattner define double @foo(double %a, double %b) { 450d80f118eSChris Lattner entry: 451d80f118eSChris Lattner %multmp = fmul double %a, %a 452d80f118eSChris Lattner %multmp1 = fmul double 2.000000e+00, %a 453d80f118eSChris Lattner %multmp2 = fmul double %multmp1, %b 454d80f118eSChris Lattner %addtmp = fadd double %multmp, %multmp2 455d80f118eSChris Lattner %multmp3 = fmul double %b, %b 456d80f118eSChris Lattner %addtmp4 = fadd double %addtmp, %multmp3 457d80f118eSChris Lattner ret double %addtmp4 458d80f118eSChris Lattner } 459d80f118eSChris Lattner 460d80f118eSChris LattnerThis shows some simple arithmetic. Notice the striking similarity to the 461d80f118eSChris LattnerLLVM builder calls that we use to create the instructions. 462d80f118eSChris Lattner 463d80f118eSChris Lattner:: 464d80f118eSChris Lattner 465d80f118eSChris Lattner ready> def bar(a) foo(a, 4.0) + bar(31337); 466d80f118eSChris Lattner Read function definition: 467d80f118eSChris Lattner define double @bar(double %a) { 468d80f118eSChris Lattner entry: 469d80f118eSChris Lattner %calltmp = call double @foo(double %a, double 4.000000e+00) 470d80f118eSChris Lattner %calltmp1 = call double @bar(double 3.133700e+04) 471d80f118eSChris Lattner %addtmp = fadd double %calltmp, %calltmp1 472d80f118eSChris Lattner ret double %addtmp 473d80f118eSChris Lattner } 474d80f118eSChris Lattner 475d80f118eSChris LattnerThis shows some function calls. Note that this function will take a long 476d80f118eSChris Lattnertime to execute if you call it. In the future we'll add conditional 477d80f118eSChris Lattnercontrol flow to actually make recursion useful :). 478d80f118eSChris Lattner 479d80f118eSChris Lattner:: 480d80f118eSChris Lattner 481d80f118eSChris Lattner ready> extern cos(x); 482d80f118eSChris Lattner Read extern: 483d80f118eSChris Lattner declare double @cos(double) 484d80f118eSChris Lattner 485d80f118eSChris Lattner ready> cos(1.234); 486d80f118eSChris Lattner Read top-level expression: 487d80f118eSChris Lattner define double @1() { 488d80f118eSChris Lattner entry: 489d80f118eSChris Lattner %calltmp = call double @cos(double 1.234000e+00) 490d80f118eSChris Lattner ret double %calltmp 491d80f118eSChris Lattner } 492d80f118eSChris Lattner 493d80f118eSChris LattnerThis shows an extern for the libm "cos" function, and a call to it. 494d80f118eSChris Lattner 495d80f118eSChris Lattner.. TODO:: Abandon Pygments' horrible `llvm` lexer. It just totally gives up 496d80f118eSChris Lattner on highlighting this due to the first line. 497d80f118eSChris Lattner 498d80f118eSChris Lattner:: 499d80f118eSChris Lattner 500d80f118eSChris Lattner ready> ^D 501d80f118eSChris Lattner ; ModuleID = 'my cool jit' 502d80f118eSChris Lattner 503d80f118eSChris Lattner define double @0() { 504d80f118eSChris Lattner entry: 505d80f118eSChris Lattner %addtmp = fadd double 4.000000e+00, 5.000000e+00 506d80f118eSChris Lattner ret double %addtmp 507d80f118eSChris Lattner } 508d80f118eSChris Lattner 509d80f118eSChris Lattner define double @foo(double %a, double %b) { 510d80f118eSChris Lattner entry: 511d80f118eSChris Lattner %multmp = fmul double %a, %a 512d80f118eSChris Lattner %multmp1 = fmul double 2.000000e+00, %a 513d80f118eSChris Lattner %multmp2 = fmul double %multmp1, %b 514d80f118eSChris Lattner %addtmp = fadd double %multmp, %multmp2 515d80f118eSChris Lattner %multmp3 = fmul double %b, %b 516d80f118eSChris Lattner %addtmp4 = fadd double %addtmp, %multmp3 517d80f118eSChris Lattner ret double %addtmp4 518d80f118eSChris Lattner } 519d80f118eSChris Lattner 520d80f118eSChris Lattner define double @bar(double %a) { 521d80f118eSChris Lattner entry: 522d80f118eSChris Lattner %calltmp = call double @foo(double %a, double 4.000000e+00) 523d80f118eSChris Lattner %calltmp1 = call double @bar(double 3.133700e+04) 524d80f118eSChris Lattner %addtmp = fadd double %calltmp, %calltmp1 525d80f118eSChris Lattner ret double %addtmp 526d80f118eSChris Lattner } 527d80f118eSChris Lattner 528d80f118eSChris Lattner declare double @cos(double) 529d80f118eSChris Lattner 530d80f118eSChris Lattner define double @1() { 531d80f118eSChris Lattner entry: 532d80f118eSChris Lattner %calltmp = call double @cos(double 1.234000e+00) 533d80f118eSChris Lattner ret double %calltmp 534d80f118eSChris Lattner } 535d80f118eSChris Lattner 536d80f118eSChris LattnerWhen you quit the current demo (by sending an EOF via CTRL+D on Linux 537d80f118eSChris Lattneror CTRL+Z and ENTER on Windows), it dumps out the IR for the entire 538d80f118eSChris Lattnermodule generated. Here you can see the big picture with all the 539d80f118eSChris Lattnerfunctions referencing each other. 540d80f118eSChris Lattner 541d80f118eSChris LattnerThis wraps up the third chapter of the Kaleidoscope tutorial. Up next, 542d80f118eSChris Lattnerwe'll describe how to `add JIT codegen and optimizer 543d80f118eSChris Lattnersupport <LangImpl04.html>`_ to this so we can actually start running 544d80f118eSChris Lattnercode! 545d80f118eSChris Lattner 546d80f118eSChris LattnerFull Code Listing 547d80f118eSChris Lattner================= 548d80f118eSChris Lattner 549d80f118eSChris LattnerHere is the complete code listing for our running example, enhanced with 550d80f118eSChris Lattnerthe LLVM code generator. Because this uses the LLVM libraries, we need 551d80f118eSChris Lattnerto link them in. To do this, we use the 55272fd1033SSylvestre Ledru`llvm-config <https://llvm.org/cmds/llvm-config.html>`_ tool to inform 553d80f118eSChris Lattnerour makefile/command line about which options to use: 554d80f118eSChris Lattner 555d80f118eSChris Lattner.. code-block:: bash 556d80f118eSChris Lattner 557d80f118eSChris Lattner # Compile 558d80f118eSChris Lattner clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy 559d80f118eSChris Lattner # Run 560d80f118eSChris Lattner ./toy 561d80f118eSChris Lattner 562d80f118eSChris LattnerHere is the code: 563d80f118eSChris Lattner 564147e0ddaSHans Wennborg.. literalinclude:: ../../../examples/Kaleidoscope/Chapter3/toy.cpp 565d80f118eSChris Lattner :language: c++ 566d80f118eSChris Lattner 567d80f118eSChris Lattner`Next: Adding JIT and Optimizer Support <LangImpl04.html>`_ 568d80f118eSChris Lattner 569