blob: 49711d581b9410915551b9180e5362b4790c89f5 [file] [log] [blame]
Sean Silvad7fb3962012-12-05 00:26:32 +00001========================================
2Kaleidoscope: Code generation to LLVM IR
3========================================
4
5.. contents::
6 :local:
7
Sean Silvad7fb3962012-12-05 00:26:32 +00008Chapter 3 Introduction
9======================
10
11Welcome to Chapter 3 of the "`Implementing a language with
12LLVM <index.html>`_" tutorial. This chapter shows you how to transform
13the `Abstract Syntax Tree <LangImpl2.html>`_, built in Chapter 2, into
14LLVM IR. This will teach you a little bit about how LLVM does things, as
15well as demonstrate how easy it is to use. It's much more work to build
16a lexer and parser than it is to generate LLVM IR code. :)
17
Lang Hames2d789c32015-08-26 03:07:41 +000018**Please note**: the code in this chapter and later require LLVM 3.7 or
19later. LLVM 3.6 and before will not work with it. Also note that you
Sean Silvad7fb3962012-12-05 00:26:32 +000020need to use a version of this tutorial that matches your LLVM release:
21If you are using an official LLVM release, use the version of the
22documentation included with your release or on the `llvm.org releases
23page <http://llvm.org/releases/>`_.
24
25Code Generation Setup
26=====================
27
28In order to generate LLVM IR, we want some simple setup to get started.
29First we define virtual code generation (codegen) methods in each AST
30class:
31
32.. code-block:: c++
33
34 /// ExprAST - Base class for all expression nodes.
35 class ExprAST {
36 public:
37 virtual ~ExprAST() {}
Lang Hames2d789c32015-08-26 03:07:41 +000038 virtual Value *codegen() = 0;
Sean Silvad7fb3962012-12-05 00:26:32 +000039 };
40
41 /// NumberExprAST - Expression class for numeric literals like "1.0".
42 class NumberExprAST : public ExprAST {
43 double Val;
Lang Hames59b0da82015-08-19 18:15:58 +000044
Sean Silvad7fb3962012-12-05 00:26:32 +000045 public:
Lang Hames09bf4c12015-08-18 18:11:06 +000046 NumberExprAST(double Val) : Val(Val) {}
Lang Hames2d789c32015-08-26 03:07:41 +000047 virtual Value *codegen();
Sean Silvad7fb3962012-12-05 00:26:32 +000048 };
49 ...
50
Lang Hames2d789c32015-08-26 03:07:41 +000051The codegen() method says to emit IR for that AST node along with all
Sean Silvad7fb3962012-12-05 00:26:32 +000052the things it depends on, and they all return an LLVM Value object.
53"Value" is the class used to represent a "`Static Single Assignment
54(SSA) <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_
55register" or "SSA value" in LLVM. The most distinct aspect of SSA values
56is that their value is computed as the related instruction executes, and
57it does not get a new value until (and if) the instruction re-executes.
58In other words, there is no way to "change" an SSA value. For more
59information, please read up on `Static Single
60Assignment <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_
61- the concepts are really quite natural once you grok them.
62
63Note that instead of adding virtual methods to the ExprAST class
64hierarchy, it could also make sense to use a `visitor
65pattern <http://en.wikipedia.org/wiki/Visitor_pattern>`_ or some other
66way to model this. Again, this tutorial won't dwell on good software
67engineering practices: for our purposes, adding a virtual method is
68simplest.
69
70The second thing we want is an "Error" method like we used for the
71parser, which will be used to report errors found during code generation
72(for example, use of an undeclared parameter):
73
74.. code-block:: c++
75
Lang Hames2d789c32015-08-26 03:07:41 +000076 static std::unique_ptr<Module> *TheModule;
77 static IRBuilder<> Builder(getGlobalContext());
78 static std::map<std::string, Value*> NamedValues;
79
Lang Hames59b0da82015-08-19 18:15:58 +000080 Value *ErrorV(const char *Str) {
81 Error(Str);
82 return nullptr;
83 }
Sean Silvad7fb3962012-12-05 00:26:32 +000084
Sean Silvad7fb3962012-12-05 00:26:32 +000085The static variables will be used during code generation. ``TheModule``
Lang Hames2d789c32015-08-26 03:07:41 +000086is an LLVM construct that contains functions and global variables. In many
87ways, it is the top-level structure that the LLVM IR uses to contain code.
88It will own the memory for all of the IR that we generate, which is why
89the codegen() method returns a raw Value\*, rather than a unique_ptr<Value>.
Sean Silvad7fb3962012-12-05 00:26:32 +000090
91The ``Builder`` object is a helper object that makes it easy to generate
92LLVM instructions. Instances of the
Sean Silva78da1a52015-03-17 21:02:37 +000093`IRBuilder <http://llvm.org/doxygen/IRBuilder_8h-source.html>`_
Sean Silvad7fb3962012-12-05 00:26:32 +000094class template keep track of the current place to insert instructions
95and has methods to create new instructions.
96
97The ``NamedValues`` map keeps track of which values are defined in the
98current scope and what their LLVM representation is. (In other words, it
99is a symbol table for the code). In this form of Kaleidoscope, the only
100things that can be referenced are function parameters. As such, function
101parameters will be in this map when generating code for their function
102body.
103
104With these basics in place, we can start talking about how to generate
105code for each expression. Note that this assumes that the ``Builder``
106has been set up to generate code *into* something. For now, we'll assume
107that this has already been done, and we'll just use it to emit code.
108
109Expression Code Generation
110==========================
111
112Generating LLVM code for expression nodes is very straightforward: less
113than 45 lines of commented code for all four of our expression nodes.
114First we'll do numeric literals:
115
116.. code-block:: c++
117
Lang Hames2d789c32015-08-26 03:07:41 +0000118 Value *NumberExprAST::codegen() {
Sean Silvad7fb3962012-12-05 00:26:32 +0000119 return ConstantFP::get(getGlobalContext(), APFloat(Val));
120 }
121
122In the LLVM IR, numeric constants are represented with the
123``ConstantFP`` class, which holds the numeric value in an ``APFloat``
124internally (``APFloat`` has the capability of holding floating point
125constants of Arbitrary Precision). This code basically just creates
126and returns a ``ConstantFP``. Note that in the LLVM IR that constants
127are all uniqued together and shared. For this reason, the API uses the
128"foo::get(...)" idiom instead of "new foo(..)" or "foo::Create(..)".
129
130.. code-block:: c++
131
Lang Hames2d789c32015-08-26 03:07:41 +0000132 Value *VariableExprAST::codegen() {
Sean Silvad7fb3962012-12-05 00:26:32 +0000133 // Look this variable up in the function.
134 Value *V = NamedValues[Name];
Lang Hames596aec92015-08-19 18:32:58 +0000135 if (!V)
136 ErrorV("Unknown variable name");
137 return V;
Sean Silvad7fb3962012-12-05 00:26:32 +0000138 }
139
140References to variables are also quite simple using LLVM. In the simple
141version of Kaleidoscope, we assume that the variable has already been
142emitted somewhere and its value is available. In practice, the only
143values that can be in the ``NamedValues`` map are function arguments.
144This code simply checks to see that the specified name is in the map (if
145not, an unknown variable is being referenced) and returns the value for
146it. In future chapters, we'll add support for `loop induction
147variables <LangImpl5.html#for>`_ in the symbol table, and for `local
148variables <LangImpl7.html#localvars>`_.
149
150.. code-block:: c++
151
Lang Hames2d789c32015-08-26 03:07:41 +0000152 Value *BinaryExprAST::codegen() {
153 Value *L = LHS->codegen();
154 Value *R = RHS->codegen();
Lang Hames59b0da82015-08-19 18:15:58 +0000155 if (!L || !R)
156 return nullptr;
Sean Silvad7fb3962012-12-05 00:26:32 +0000157
158 switch (Op) {
Lang Hames59b0da82015-08-19 18:15:58 +0000159 case '+':
160 return Builder.CreateFAdd(L, R, "addtmp");
161 case '-':
162 return Builder.CreateFSub(L, R, "subtmp");
163 case '*':
164 return Builder.CreateFMul(L, R, "multmp");
Sean Silvad7fb3962012-12-05 00:26:32 +0000165 case '<':
166 L = Builder.CreateFCmpULT(L, R, "cmptmp");
167 // Convert bool 0/1 to double 0.0 or 1.0
168 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
169 "booltmp");
Lang Hames59b0da82015-08-19 18:15:58 +0000170 default:
171 return ErrorV("invalid binary operator");
Sean Silvad7fb3962012-12-05 00:26:32 +0000172 }
173 }
174
175Binary operators start to get more interesting. The basic idea here is
176that we recursively emit code for the left-hand side of the expression,
177then the right-hand side, then we compute the result of the binary
178expression. In this code, we do a simple switch on the opcode to create
179the right LLVM instruction.
180
181In the example above, the LLVM builder class is starting to show its
182value. IRBuilder knows where to insert the newly created instruction,
183all you have to do is specify what instruction to create (e.g. with
184``CreateFAdd``), which operands to use (``L`` and ``R`` here) and
185optionally provide a name for the generated instruction.
186
187One nice thing about LLVM is that the name is just a hint. For instance,
188if the code above emits multiple "addtmp" variables, LLVM will
189automatically provide each one with an increasing, unique numeric
190suffix. Local value names for instructions are purely optional, but it
191makes it much easier to read the IR dumps.
192
193`LLVM instructions <../LangRef.html#instref>`_ are constrained by strict
194rules: for example, the Left and Right operators of an `add
195instruction <../LangRef.html#i_add>`_ must have the same type, and the
196result type of the add must match the operand types. Because all values
197in Kaleidoscope are doubles, this makes for very simple code for add,
198sub and mul.
199
200On the other hand, LLVM specifies that the `fcmp
201instruction <../LangRef.html#i_fcmp>`_ always returns an 'i1' value (a
202one bit integer). The problem with this is that Kaleidoscope wants the
203value to be a 0.0 or 1.0 value. In order to get these semantics, we
204combine the fcmp instruction with a `uitofp
205instruction <../LangRef.html#i_uitofp>`_. This instruction converts its
206input integer into a floating point value by treating the input as an
207unsigned value. In contrast, if we used the `sitofp
208instruction <../LangRef.html#i_sitofp>`_, the Kaleidoscope '<' operator
209would return 0.0 and -1.0, depending on the input value.
210
211.. code-block:: c++
212
Lang Hames2d789c32015-08-26 03:07:41 +0000213 Value *CallExprAST::codegen() {
Sean Silvad7fb3962012-12-05 00:26:32 +0000214 // Look up the name in the global module table.
215 Function *CalleeF = TheModule->getFunction(Callee);
Lang Hames59b0da82015-08-19 18:15:58 +0000216 if (!CalleeF)
Sean Silvad7fb3962012-12-05 00:26:32 +0000217 return ErrorV("Unknown function referenced");
218
219 // If argument mismatch error.
220 if (CalleeF->arg_size() != Args.size())
221 return ErrorV("Incorrect # arguments passed");
222
Lang Hames59b0da82015-08-19 18:15:58 +0000223 std::vector<Value *> ArgsV;
Sean Silvad7fb3962012-12-05 00:26:32 +0000224 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Lang Hames2d789c32015-08-26 03:07:41 +0000225 ArgsV.push_back(Args[i]->codegen());
Lang Hames59b0da82015-08-19 18:15:58 +0000226 if (!ArgsV.back())
227 return nullptr;
Sean Silvad7fb3962012-12-05 00:26:32 +0000228 }
229
230 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
231 }
232
Lang Hames2d789c32015-08-26 03:07:41 +0000233Code generation for function calls is quite straightforward with LLVM. The code
234above initially does a function name lookup in the LLVM Module's symbol table.
235Recall that the LLVM Module is the container that holds the functions we are
236JIT'ing. By giving each function the same name as what the user specifies, we
237can use the LLVM symbol table to resolve function names for us.
Sean Silvad7fb3962012-12-05 00:26:32 +0000238
239Once we have the function to call, we recursively codegen each argument
240that is to be passed in, and create an LLVM `call
241instruction <../LangRef.html#i_call>`_. Note that LLVM uses the native C
242calling conventions by default, allowing these calls to also call into
243standard library functions like "sin" and "cos", with no additional
244effort.
245
246This wraps up our handling of the four basic expressions that we have so
247far in Kaleidoscope. Feel free to go in and add some more. For example,
248by browsing the `LLVM language reference <../LangRef.html>`_ you'll find
249several other interesting instructions that are really easy to plug into
250our basic framework.
251
252Function Code Generation
253========================
254
255Code generation for prototypes and functions must handle a number of
256details, which make their code less beautiful than expression code
257generation, but allows us to illustrate some important points. First,
258lets talk about code generation for prototypes: they are used both for
259function bodies and external function declarations. The code starts
260with:
261
262.. code-block:: c++
263
Lang Hames2d789c32015-08-26 03:07:41 +0000264 Function *PrototypeAST::codegen() {
Sean Silvad7fb3962012-12-05 00:26:32 +0000265 // Make the function type: double(double,double) etc.
266 std::vector<Type*> Doubles(Args.size(),
267 Type::getDoubleTy(getGlobalContext()));
Lang Hames59b0da82015-08-19 18:15:58 +0000268 FunctionType *FT =
269 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
Sean Silvad7fb3962012-12-05 00:26:32 +0000270
Lang Hames59b0da82015-08-19 18:15:58 +0000271 Function *F =
272 Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
Sean Silvad7fb3962012-12-05 00:26:32 +0000273
274This code packs a lot of power into a few lines. Note first that this
275function returns a "Function\*" instead of a "Value\*". Because a
276"prototype" really talks about the external interface for a function
277(not the value computed by an expression), it makes sense for it to
278return the LLVM Function it corresponds to when codegen'd.
279
280The call to ``FunctionType::get`` creates the ``FunctionType`` that
281should be used for a given Prototype. Since all function arguments in
282Kaleidoscope are of type double, the first line creates a vector of "N"
283LLVM double types. It then uses the ``Functiontype::get`` method to
284create a function type that takes "N" doubles as arguments, returns one
285double as a result, and that is not vararg (the false parameter
286indicates this). Note that Types in LLVM are uniqued just like Constants
287are, so you don't "new" a type, you "get" it.
288
Lang Hames2d789c32015-08-26 03:07:41 +0000289The final line above actually creates the IR Function corresponding to
290the Prototype. This indicates the type, linkage and name to use, as
Sean Silvad7fb3962012-12-05 00:26:32 +0000291well as which module to insert into. "`external
292linkage <../LangRef.html#linkage>`_" means that the function may be
293defined outside the current module and/or that it is callable by
294functions outside the module. The Name passed in is the name the user
295specified: since "``TheModule``" is specified, this name is registered
Lang Hames2d789c32015-08-26 03:07:41 +0000296in "``TheModule``"s symbol table.
Sean Silvad7fb3962012-12-05 00:26:32 +0000297
298.. code-block:: c++
299
Lang Hames2d789c32015-08-26 03:07:41 +0000300 // Set names for all arguments.
301 unsigned Idx = 0;
302 for (auto &Arg : F->args())
303 Arg.setName(Args[Idx++]);
Sean Silvad7fb3962012-12-05 00:26:32 +0000304
Lang Hames2d789c32015-08-26 03:07:41 +0000305 return F;
Sean Silvad7fb3962012-12-05 00:26:32 +0000306
Lang Hames2d789c32015-08-26 03:07:41 +0000307Finally, we set the name of each of the function's arguments according to the
308names given in the Prototype. This step isn't strictly necessary, but keeping
309the names consistent makes the IR more readable, and allows subsequent code to
310refer directly to the arguments for their names, rather than having to look up
311them up in the Prototype AST.
Sean Silvad7fb3962012-12-05 00:26:32 +0000312
Lang Hames2d789c32015-08-26 03:07:41 +0000313At this point we have a function prototype with no body. This is how LLVM IR
314represents function declarations. For extern statements in Kaleidoscope, this
315is as far as we need to go. For function definitions however, we need to
316codegen and attach a function body.
Sean Silvad7fb3962012-12-05 00:26:32 +0000317
318.. code-block:: c++
319
Lang Hames2d789c32015-08-26 03:07:41 +0000320 Function *FunctionAST::codegen() {
321 // First, check for an existing function from a previous 'extern' declaration.
322 Function *TheFunction = TheModule->getFunction(Proto->getName());
Sean Silvad7fb3962012-12-05 00:26:32 +0000323
Lang Hames2d789c32015-08-26 03:07:41 +0000324 if (!TheFunction)
325 TheFunction = Proto->codegen();
Sean Silvad7fb3962012-12-05 00:26:32 +0000326
Lang Hames2d789c32015-08-26 03:07:41 +0000327 if (!TheFunction)
328 return nullptr;
329
330 if (!TheFunction->empty())
331 return (Function*)ErrorV("Function cannot be redefined.");
332
333
334For function definitions, we start by searching TheModule's symbol table for an
335existing version of this function, in case one has already been created using an
336'extern' statement. If Module::getFunction returns null then no previous version
337exists, so we'll codegen one from the Prototype. In either case, we want to
338assert that the function is empty (i.e. has no body yet) before we start.
Sean Silvad7fb3962012-12-05 00:26:32 +0000339
340.. code-block:: c++
341
Lang Hames2d789c32015-08-26 03:07:41 +0000342 // Create a new basic block to start insertion into.
343 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
344 Builder.SetInsertPoint(BB);
Sean Silvad7fb3962012-12-05 00:26:32 +0000345
Lang Hames2d789c32015-08-26 03:07:41 +0000346 // Record the function arguments in the NamedValues map.
347 NamedValues.clear();
348 for (auto &Arg : TheFunction->args())
349 NamedValues[Arg.getName()] = &Arg;
Sean Silvad7fb3962012-12-05 00:26:32 +0000350
351Now we get to the point where the ``Builder`` is set up. The first line
352creates a new `basic block <http://en.wikipedia.org/wiki/Basic_block>`_
353(named "entry"), which is inserted into ``TheFunction``. The second line
354then tells the builder that new instructions should be inserted into the
355end of the new basic block. Basic blocks in LLVM are an important part
356of functions that define the `Control Flow
357Graph <http://en.wikipedia.org/wiki/Control_flow_graph>`_. Since we
358don't have any control flow, our functions will only contain one block
359at this point. We'll fix this in `Chapter 5 <LangImpl5.html>`_ :).
360
Lang Hames2d789c32015-08-26 03:07:41 +0000361Next we add the function arguments to the NamedValues map (after first clearing
362it out) so that they're accessible to ``VariableExprAST`` nodes.
363
Sean Silvad7fb3962012-12-05 00:26:32 +0000364.. code-block:: c++
365
Lang Hames2d789c32015-08-26 03:07:41 +0000366 if (Value *RetVal = Body->codegen()) {
Sean Silvad7fb3962012-12-05 00:26:32 +0000367 // Finish off the function.
368 Builder.CreateRet(RetVal);
369
370 // Validate the generated code, checking for consistency.
371 verifyFunction(*TheFunction);
372
373 return TheFunction;
374 }
375
Lang Hames2d789c32015-08-26 03:07:41 +0000376Once the insertion point has been set up and the NamedValues map populated,
377we call the ``codegen()`` method for the root expression of the function. If no
378error happens, this emits code to compute the expression into the entry block
379and returns the value that was computed. Assuming no error, we then create an
380LLVM `ret instruction <../LangRef.html#i_ret>`_, which completes the function.
Sean Silvad7fb3962012-12-05 00:26:32 +0000381Once the function is built, we call ``verifyFunction``, which is
382provided by LLVM. This function does a variety of consistency checks on
383the generated code, to determine if our compiler is doing everything
384right. Using this is important: it can catch a lot of bugs. Once the
385function is finished and validated, we return it.
386
387.. code-block:: c++
388
389 // Error reading body, remove function.
390 TheFunction->eraseFromParent();
Lang Hames59b0da82015-08-19 18:15:58 +0000391 return nullptr;
Sean Silvad7fb3962012-12-05 00:26:32 +0000392 }
393
394The only piece left here is handling of the error case. For simplicity,
395we handle this by merely deleting the function we produced with the
396``eraseFromParent`` method. This allows the user to redefine a function
397that they incorrectly typed in before: if we didn't delete it, it would
398live in the symbol table, with a body, preventing future redefinition.
399
Lang Hames2d789c32015-08-26 03:07:41 +0000400This code does have a bug, though: If the ``FunctionAST::codegen()`` method
401finds an existing IR Function, it does not validate its signature against the
402definition's own prototype. This means that an earlier 'extern' declaration will
403take precedence over the function definition's signature, which can cause
404codegen to fail, for instance if the function arguments are named differently.
405There are a number of ways to fix this bug, see what you can come up with! Here
406is a testcase:
Sean Silvad7fb3962012-12-05 00:26:32 +0000407
408::
409
Lang Hames2d789c32015-08-26 03:07:41 +0000410 extern foo(a); # ok, defines foo.
411 def foo(b) b; # Error: Unknown variable name. (decl using 'a' takes precedence).
Sean Silvad7fb3962012-12-05 00:26:32 +0000412
413Driver Changes and Closing Thoughts
414===================================
415
416For now, code generation to LLVM doesn't really get us much, except that
417we can look at the pretty IR calls. The sample code inserts calls to
Lang Hames2d789c32015-08-26 03:07:41 +0000418codegen into the "``HandleDefinition``", "``HandleExtern``" etc
Sean Silvad7fb3962012-12-05 00:26:32 +0000419functions, and then dumps out the LLVM IR. This gives a nice way to look
420at the LLVM IR for simple functions. For example:
421
422::
423
424 ready> 4+5;
425 Read top-level expression:
426 define double @0() {
427 entry:
428 ret double 9.000000e+00
429 }
430
431Note how the parser turns the top-level expression into anonymous
432functions for us. This will be handy when we add `JIT
433support <LangImpl4.html#jit>`_ in the next chapter. Also note that the
434code is very literally transcribed, no optimizations are being performed
435except simple constant folding done by IRBuilder. We will `add
436optimizations <LangImpl4.html#trivialconstfold>`_ explicitly in the next
437chapter.
438
439::
440
441 ready> def foo(a b) a*a + 2*a*b + b*b;
442 Read function definition:
443 define double @foo(double %a, double %b) {
444 entry:
445 %multmp = fmul double %a, %a
446 %multmp1 = fmul double 2.000000e+00, %a
447 %multmp2 = fmul double %multmp1, %b
448 %addtmp = fadd double %multmp, %multmp2
449 %multmp3 = fmul double %b, %b
450 %addtmp4 = fadd double %addtmp, %multmp3
451 ret double %addtmp4
452 }
453
454This shows some simple arithmetic. Notice the striking similarity to the
455LLVM builder calls that we use to create the instructions.
456
457::
458
459 ready> def bar(a) foo(a, 4.0) + bar(31337);
460 Read function definition:
461 define double @bar(double %a) {
462 entry:
463 %calltmp = call double @foo(double %a, double 4.000000e+00)
464 %calltmp1 = call double @bar(double 3.133700e+04)
465 %addtmp = fadd double %calltmp, %calltmp1
466 ret double %addtmp
467 }
468
469This shows some function calls. Note that this function will take a long
470time to execute if you call it. In the future we'll add conditional
471control flow to actually make recursion useful :).
472
473::
474
475 ready> extern cos(x);
476 Read extern:
477 declare double @cos(double)
478
479 ready> cos(1.234);
480 Read top-level expression:
481 define double @1() {
482 entry:
483 %calltmp = call double @cos(double 1.234000e+00)
484 ret double %calltmp
485 }
486
487This shows an extern for the libm "cos" function, and a call to it.
488
489.. TODO:: Abandon Pygments' horrible `llvm` lexer. It just totally gives up
490 on highlighting this due to the first line.
491
492::
493
494 ready> ^D
495 ; ModuleID = 'my cool jit'
496
497 define double @0() {
498 entry:
499 %addtmp = fadd double 4.000000e+00, 5.000000e+00
500 ret double %addtmp
501 }
502
503 define double @foo(double %a, double %b) {
504 entry:
505 %multmp = fmul double %a, %a
506 %multmp1 = fmul double 2.000000e+00, %a
507 %multmp2 = fmul double %multmp1, %b
508 %addtmp = fadd double %multmp, %multmp2
509 %multmp3 = fmul double %b, %b
510 %addtmp4 = fadd double %addtmp, %multmp3
511 ret double %addtmp4
512 }
513
514 define double @bar(double %a) {
515 entry:
516 %calltmp = call double @foo(double %a, double 4.000000e+00)
517 %calltmp1 = call double @bar(double 3.133700e+04)
518 %addtmp = fadd double %calltmp, %calltmp1
519 ret double %addtmp
520 }
521
522 declare double @cos(double)
523
524 define double @1() {
525 entry:
526 %calltmp = call double @cos(double 1.234000e+00)
527 ret double %calltmp
528 }
529
530When you quit the current demo, it dumps out the IR for the entire
531module generated. Here you can see the big picture with all the
532functions referencing each other.
533
534This wraps up the third chapter of the Kaleidoscope tutorial. Up next,
535we'll describe how to `add JIT codegen and optimizer
536support <LangImpl4.html>`_ to this so we can actually start running
537code!
538
539Full Code Listing
540=================
541
542Here is the complete code listing for our running example, enhanced with
543the LLVM code generator. Because this uses the LLVM libraries, we need
544to link them in. To do this, we use the
545`llvm-config <http://llvm.org/cmds/llvm-config.html>`_ tool to inform
546our makefile/command line about which options to use:
547
548.. code-block:: bash
549
550 # Compile
Lang Hamesaa0f6732014-11-06 00:31:04 +0000551 clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy
Sean Silvad7fb3962012-12-05 00:26:32 +0000552 # Run
553 ./toy
554
555Here is the code:
556
Logan Chien855b17d2013-06-08 09:03:03 +0000557.. literalinclude:: ../../examples/Kaleidoscope/Chapter3/toy.cpp
558 :language: c++
Sean Silvad7fb3962012-12-05 00:26:32 +0000559
560`Next: Adding JIT and Optimizer Support <LangImpl4.html>`_
561