blob: 8484c57f9dc9461cc59ef6e45c862130228e1f4c [file] [log] [blame]
Sean Silvaee47edf2012-12-05 00:26:32 +00001==============================================
2Kaleidoscope: Adding JIT and Optimizer Support
3==============================================
4
5.. contents::
6 :local:
7
8Written by `Chris Lattner <mailto:sabre@nondot.org>`_
9
10Chapter 4 Introduction
11======================
12
13Welcome to Chapter 4 of the "`Implementing a language with
14LLVM <index.html>`_" tutorial. Chapters 1-3 described the implementation
15of a simple language and added support for generating LLVM IR. This
16chapter describes two new techniques: adding optimizer support to your
17language, and adding JIT compiler support. These additions will
18demonstrate how to get nice, efficient code for the Kaleidoscope
19language.
20
21Trivial Constant Folding
22========================
23
24Our demonstration for Chapter 3 is elegant and easy to extend.
25Unfortunately, it does not produce wonderful code. The IRBuilder,
26however, does give us obvious optimizations when compiling simple code:
27
28::
29
30 ready> def test(x) 1+2+x;
31 Read function definition:
32 define double @test(double %x) {
33 entry:
34 %addtmp = fadd double 3.000000e+00, %x
35 ret double %addtmp
36 }
37
38This code is not a literal transcription of the AST built by parsing the
39input. That would be:
40
41::
42
43 ready> def test(x) 1+2+x;
44 Read function definition:
45 define double @test(double %x) {
46 entry:
47 %addtmp = fadd double 2.000000e+00, 1.000000e+00
48 %addtmp1 = fadd double %addtmp, %x
49 ret double %addtmp1
50 }
51
52Constant folding, as seen above, in particular, is a very common and
53very important optimization: so much so that many language implementors
54implement constant folding support in their AST representation.
55
56With LLVM, you don't need this support in the AST. Since all calls to
57build LLVM IR go through the LLVM IR builder, the builder itself checked
58to see if there was a constant folding opportunity when you call it. If
59so, it just does the constant fold and return the constant instead of
60creating an instruction.
61
62Well, that was easy :). In practice, we recommend always using
63``IRBuilder`` when generating code like this. It has no "syntactic
64overhead" for its use (you don't have to uglify your compiler with
65constant checks everywhere) and it can dramatically reduce the amount of
66LLVM IR that is generated in some cases (particular for languages with a
67macro preprocessor or that use a lot of constants).
68
69On the other hand, the ``IRBuilder`` is limited by the fact that it does
70all of its analysis inline with the code as it is built. If you take a
71slightly more complex example:
72
73::
74
75 ready> def test(x) (1+2+x)*(x+(1+2));
76 ready> Read function definition:
77 define double @test(double %x) {
78 entry:
79 %addtmp = fadd double 3.000000e+00, %x
80 %addtmp1 = fadd double %x, 3.000000e+00
81 %multmp = fmul double %addtmp, %addtmp1
82 ret double %multmp
83 }
84
85In this case, the LHS and RHS of the multiplication are the same value.
86We'd really like to see this generate "``tmp = x+3; result = tmp*tmp;``"
87instead of computing "``x+3``" twice.
88
89Unfortunately, no amount of local analysis will be able to detect and
90correct this. This requires two transformations: reassociation of
91expressions (to make the add's lexically identical) and Common
92Subexpression Elimination (CSE) to delete the redundant add instruction.
93Fortunately, LLVM provides a broad range of optimizations that you can
94use, in the form of "passes".
95
96LLVM Optimization Passes
97========================
98
99LLVM provides many optimization passes, which do many different sorts of
100things and have different tradeoffs. Unlike other systems, LLVM doesn't
101hold to the mistaken notion that one set of optimizations is right for
102all languages and for all situations. LLVM allows a compiler implementor
103to make complete decisions about what optimizations to use, in which
104order, and in what situation.
105
106As a concrete example, LLVM supports both "whole module" passes, which
107look across as large of body of code as they can (often a whole file,
108but if run at link time, this can be a substantial portion of the whole
109program). It also supports and includes "per-function" passes which just
110operate on a single function at a time, without looking at other
111functions. For more information on passes and how they are run, see the
112`How to Write a Pass <../WritingAnLLVMPass.html>`_ document and the
113`List of LLVM Passes <../Passes.html>`_.
114
115For Kaleidoscope, we are currently generating functions on the fly, one
116at a time, as the user types them in. We aren't shooting for the
117ultimate optimization experience in this setting, but we also want to
118catch the easy and quick stuff where possible. As such, we will choose
119to run a few per-function optimizations as the user types the function
120in. If we wanted to make a "static Kaleidoscope compiler", we would use
121exactly the code we have now, except that we would defer running the
122optimizer until the entire file has been parsed.
123
124In order to get per-function optimizations going, we need to set up a
125`FunctionPassManager <../WritingAnLLVMPass.html#passmanager>`_ to hold
126and organize the LLVM optimizations that we want to run. Once we have
127that, we can add a set of optimizations to run. The code looks like
128this:
129
130.. code-block:: c++
131
132 FunctionPassManager OurFPM(TheModule);
133
134 // Set up the optimizer pipeline. Start with registering info about how the
135 // target lays out data structures.
136 OurFPM.add(new DataLayout(*TheExecutionEngine->getDataLayout()));
137 // Provide basic AliasAnalysis support for GVN.
138 OurFPM.add(createBasicAliasAnalysisPass());
139 // Do simple "peephole" optimizations and bit-twiddling optzns.
140 OurFPM.add(createInstructionCombiningPass());
141 // Reassociate expressions.
142 OurFPM.add(createReassociatePass());
143 // Eliminate Common SubExpressions.
144 OurFPM.add(createGVNPass());
145 // Simplify the control flow graph (deleting unreachable blocks, etc).
146 OurFPM.add(createCFGSimplificationPass());
147
148 OurFPM.doInitialization();
149
150 // Set the global so the code gen can use this.
151 TheFPM = &OurFPM;
152
153 // Run the main "interpreter loop" now.
154 MainLoop();
155
156This code defines a ``FunctionPassManager``, "``OurFPM``". It requires a
157pointer to the ``Module`` to construct itself. Once it is set up, we use
158a series of "add" calls to add a bunch of LLVM passes. The first pass is
159basically boilerplate, it adds a pass so that later optimizations know
160how the data structures in the program are laid out. The
161"``TheExecutionEngine``" variable is related to the JIT, which we will
162get to in the next section.
163
164In this case, we choose to add 4 optimization passes. The passes we
165chose here are a pretty standard set of "cleanup" optimizations that are
166useful for a wide variety of code. I won't delve into what they do but,
167believe me, they are a good starting place :).
168
169Once the PassManager is set up, we need to make use of it. We do this by
170running it after our newly created function is constructed (in
171``FunctionAST::Codegen``), but before it is returned to the client:
172
173.. code-block:: c++
174
175 if (Value *RetVal = Body->Codegen()) {
176 // Finish off the function.
177 Builder.CreateRet(RetVal);
178
179 // Validate the generated code, checking for consistency.
180 verifyFunction(*TheFunction);
181
182 // Optimize the function.
183 TheFPM->run(*TheFunction);
184
185 return TheFunction;
186 }
187
188As you can see, this is pretty straightforward. The
189``FunctionPassManager`` optimizes and updates the LLVM Function\* in
190place, improving (hopefully) its body. With this in place, we can try
191our test above again:
192
193::
194
195 ready> def test(x) (1+2+x)*(x+(1+2));
196 ready> Read function definition:
197 define double @test(double %x) {
198 entry:
199 %addtmp = fadd double %x, 3.000000e+00
200 %multmp = fmul double %addtmp, %addtmp
201 ret double %multmp
202 }
203
204As expected, we now get our nicely optimized code, saving a floating
205point add instruction from every execution of this function.
206
207LLVM provides a wide variety of optimizations that can be used in
208certain circumstances. Some `documentation about the various
209passes <../Passes.html>`_ is available, but it isn't very complete.
210Another good source of ideas can come from looking at the passes that
211``Clang`` runs to get started. The "``opt``" tool allows you to
212experiment with passes from the command line, so you can see if they do
213anything.
214
215Now that we have reasonable code coming out of our front-end, lets talk
216about executing it!
217
218Adding a JIT Compiler
219=====================
220
221Code that is available in LLVM IR can have a wide variety of tools
222applied to it. For example, you can run optimizations on it (as we did
223above), you can dump it out in textual or binary forms, you can compile
224the code to an assembly file (.s) for some target, or you can JIT
225compile it. The nice thing about the LLVM IR representation is that it
226is the "common currency" between many different parts of the compiler.
227
228In this section, we'll add JIT compiler support to our interpreter. The
229basic idea that we want for Kaleidoscope is to have the user enter
230function bodies as they do now, but immediately evaluate the top-level
231expressions they type in. For example, if they type in "1 + 2;", we
232should evaluate and print out 3. If they define a function, they should
233be able to call it from the command line.
234
235In order to do this, we first declare and initialize the JIT. This is
236done by adding a global variable and a call in ``main``:
237
238.. code-block:: c++
239
240 static ExecutionEngine *TheExecutionEngine;
241 ...
242 int main() {
243 ..
244 // Create the JIT. This takes ownership of the module.
245 TheExecutionEngine = EngineBuilder(TheModule).create();
246 ..
247 }
248
249This creates an abstract "Execution Engine" which can be either a JIT
250compiler or the LLVM interpreter. LLVM will automatically pick a JIT
251compiler for you if one is available for your platform, otherwise it
252will fall back to the interpreter.
253
254Once the ``ExecutionEngine`` is created, the JIT is ready to be used.
255There are a variety of APIs that are useful, but the simplest one is the
256"``getPointerToFunction(F)``" method. This method JIT compiles the
257specified LLVM Function and returns a function pointer to the generated
258machine code. In our case, this means that we can change the code that
259parses a top-level expression to look like this:
260
261.. code-block:: c++
262
263 static void HandleTopLevelExpression() {
264 // Evaluate a top-level expression into an anonymous function.
265 if (FunctionAST *F = ParseTopLevelExpr()) {
266 if (Function *LF = F->Codegen()) {
267 LF->dump(); // Dump the function for exposition purposes.
268
269 // JIT the function, returning a function pointer.
270 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
271
272 // Cast it to the right type (takes no arguments, returns a double) so we
273 // can call it as a native function.
274 double (*FP)() = (double (*)())(intptr_t)FPtr;
275 fprintf(stderr, "Evaluated to %f\n", FP());
276 }
277
278Recall that we compile top-level expressions into a self-contained LLVM
279function that takes no arguments and returns the computed double.
280Because the LLVM JIT compiler matches the native platform ABI, this
281means that you can just cast the result pointer to a function pointer of
282that type and call it directly. This means, there is no difference
283between JIT compiled code and native machine code that is statically
284linked into your application.
285
286With just these two changes, lets see how Kaleidoscope works now!
287
288::
289
290 ready> 4+5;
291 Read top-level expression:
292 define double @0() {
293 entry:
294 ret double 9.000000e+00
295 }
296
297 Evaluated to 9.000000
298
299Well this looks like it is basically working. The dump of the function
300shows the "no argument function that always returns double" that we
301synthesize for each top-level expression that is typed in. This
302demonstrates very basic functionality, but can we do more?
303
304::
305
306 ready> def testfunc(x y) x + y*2;
307 Read function definition:
308 define double @testfunc(double %x, double %y) {
309 entry:
310 %multmp = fmul double %y, 2.000000e+00
311 %addtmp = fadd double %multmp, %x
312 ret double %addtmp
313 }
314
315 ready> testfunc(4, 10);
316 Read top-level expression:
317 define double @1() {
318 entry:
319 %calltmp = call double @testfunc(double 4.000000e+00, double 1.000000e+01)
320 ret double %calltmp
321 }
322
323 Evaluated to 24.000000
324
325This illustrates that we can now call user code, but there is something
326a bit subtle going on here. Note that we only invoke the JIT on the
327anonymous functions that *call testfunc*, but we never invoked it on
328*testfunc* itself. What actually happened here is that the JIT scanned
329for all non-JIT'd functions transitively called from the anonymous
330function and compiled all of them before returning from
331``getPointerToFunction()``.
332
333The JIT provides a number of other more advanced interfaces for things
334like freeing allocated machine code, rejit'ing functions to update them,
335etc. However, even with this simple code, we get some surprisingly
336powerful capabilities - check this out (I removed the dump of the
337anonymous functions, you should get the idea by now :) :
338
339::
340
341 ready> extern sin(x);
342 Read extern:
343 declare double @sin(double)
344
345 ready> extern cos(x);
346 Read extern:
347 declare double @cos(double)
348
349 ready> sin(1.0);
350 Read top-level expression:
351 define double @2() {
352 entry:
353 ret double 0x3FEAED548F090CEE
354 }
355
356 Evaluated to 0.841471
357
358 ready> def foo(x) sin(x)*sin(x) + cos(x)*cos(x);
359 Read function definition:
360 define double @foo(double %x) {
361 entry:
362 %calltmp = call double @sin(double %x)
363 %multmp = fmul double %calltmp, %calltmp
364 %calltmp2 = call double @cos(double %x)
365 %multmp4 = fmul double %calltmp2, %calltmp2
366 %addtmp = fadd double %multmp, %multmp4
367 ret double %addtmp
368 }
369
370 ready> foo(4.0);
371 Read top-level expression:
372 define double @3() {
373 entry:
374 %calltmp = call double @foo(double 4.000000e+00)
375 ret double %calltmp
376 }
377
378 Evaluated to 1.000000
379
380Whoa, how does the JIT know about sin and cos? The answer is
381surprisingly simple: in this example, the JIT started execution of a
382function and got to a function call. It realized that the function was
383not yet JIT compiled and invoked the standard set of routines to resolve
384the function. In this case, there is no body defined for the function,
385so the JIT ended up calling "``dlsym("sin")``" on the Kaleidoscope
386process itself. Since "``sin``" is defined within the JIT's address
387space, it simply patches up calls in the module to call the libm version
388of ``sin`` directly.
389
390The LLVM JIT provides a number of interfaces (look in the
391``ExecutionEngine.h`` file) for controlling how unknown functions get
392resolved. It allows you to establish explicit mappings between IR
393objects and addresses (useful for LLVM global variables that you want to
394map to static tables, for example), allows you to dynamically decide on
395the fly based on the function name, and even allows you to have the JIT
396compile functions lazily the first time they're called.
397
398One interesting application of this is that we can now extend the
399language by writing arbitrary C++ code to implement operations. For
400example, if we add:
401
402.. code-block:: c++
403
404 /// putchard - putchar that takes a double and returns 0.
405 extern "C"
406 double putchard(double X) {
407 putchar((char)X);
408 return 0;
409 }
410
411Now we can produce simple output to the console by using things like:
412"``extern putchard(x); putchard(120);``", which prints a lowercase 'x'
413on the console (120 is the ASCII code for 'x'). Similar code could be
414used to implement file I/O, console input, and many other capabilities
415in Kaleidoscope.
416
417This completes the JIT and optimizer chapter of the Kaleidoscope
418tutorial. At this point, we can compile a non-Turing-complete
419programming language, optimize and JIT compile it in a user-driven way.
420Next up we'll look into `extending the language with control flow
421constructs <LangImpl5.html>`_, tackling some interesting LLVM IR issues
422along the way.
423
424Full Code Listing
425=================
426
427Here is the complete code listing for our running example, enhanced with
428the LLVM JIT and optimizer. To build this example, use:
429
430.. code-block:: bash
431
432 # Compile
433 clang++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
434 # Run
435 ./toy
436
437If you are compiling this on Linux, make sure to add the "-rdynamic"
438option as well. This makes sure that the external functions are resolved
439properly at runtime.
440
441Here is the code:
442
443.. code-block:: c++
444
445 #include "llvm/DerivedTypes.h"
446 #include "llvm/ExecutionEngine/ExecutionEngine.h"
447 #include "llvm/ExecutionEngine/JIT.h"
448 #include "llvm/IRBuilder.h"
449 #include "llvm/LLVMContext.h"
450 #include "llvm/Module.h"
451 #include "llvm/PassManager.h"
452 #include "llvm/Analysis/Verifier.h"
453 #include "llvm/Analysis/Passes.h"
454 #include "llvm/DataLayout.h"
455 #include "llvm/Transforms/Scalar.h"
456 #include "llvm/Support/TargetSelect.h"
457 #include <cstdio>
458 #include <string>
459 #include <map>
460 #include <vector>
461 using namespace llvm;
462
463 //===----------------------------------------------------------------------===//
464 // Lexer
465 //===----------------------------------------------------------------------===//
466
467 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
468 // of these for known things.
469 enum Token {
470 tok_eof = -1,
471
472 // commands
473 tok_def = -2, tok_extern = -3,
474
475 // primary
476 tok_identifier = -4, tok_number = -5
477 };
478
479 static std::string IdentifierStr; // Filled in if tok_identifier
480 static double NumVal; // Filled in if tok_number
481
482 /// gettok - Return the next token from standard input.
483 static int gettok() {
484 static int LastChar = ' ';
485
486 // Skip any whitespace.
487 while (isspace(LastChar))
488 LastChar = getchar();
489
490 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
491 IdentifierStr = LastChar;
492 while (isalnum((LastChar = getchar())))
493 IdentifierStr += LastChar;
494
495 if (IdentifierStr == "def") return tok_def;
496 if (IdentifierStr == "extern") return tok_extern;
497 return tok_identifier;
498 }
499
500 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
501 std::string NumStr;
502 do {
503 NumStr += LastChar;
504 LastChar = getchar();
505 } while (isdigit(LastChar) || LastChar == '.');
506
507 NumVal = strtod(NumStr.c_str(), 0);
508 return tok_number;
509 }
510
511 if (LastChar == '#') {
512 // Comment until end of line.
513 do LastChar = getchar();
514 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
515
516 if (LastChar != EOF)
517 return gettok();
518 }
519
520 // Check for end of file. Don't eat the EOF.
521 if (LastChar == EOF)
522 return tok_eof;
523
524 // Otherwise, just return the character as its ascii value.
525 int ThisChar = LastChar;
526 LastChar = getchar();
527 return ThisChar;
528 }
529
530 //===----------------------------------------------------------------------===//
531 // Abstract Syntax Tree (aka Parse Tree)
532 //===----------------------------------------------------------------------===//
533
534 /// ExprAST - Base class for all expression nodes.
535 class ExprAST {
536 public:
537 virtual ~ExprAST() {}
538 virtual Value *Codegen() = 0;
539 };
540
541 /// NumberExprAST - Expression class for numeric literals like "1.0".
542 class NumberExprAST : public ExprAST {
543 double Val;
544 public:
545 NumberExprAST(double val) : Val(val) {}
546 virtual Value *Codegen();
547 };
548
549 /// VariableExprAST - Expression class for referencing a variable, like "a".
550 class VariableExprAST : public ExprAST {
551 std::string Name;
552 public:
553 VariableExprAST(const std::string &name) : Name(name) {}
554 virtual Value *Codegen();
555 };
556
557 /// BinaryExprAST - Expression class for a binary operator.
558 class BinaryExprAST : public ExprAST {
559 char Op;
560 ExprAST *LHS, *RHS;
561 public:
562 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
563 : Op(op), LHS(lhs), RHS(rhs) {}
564 virtual Value *Codegen();
565 };
566
567 /// CallExprAST - Expression class for function calls.
568 class CallExprAST : public ExprAST {
569 std::string Callee;
570 std::vector<ExprAST*> Args;
571 public:
572 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
573 : Callee(callee), Args(args) {}
574 virtual Value *Codegen();
575 };
576
577 /// PrototypeAST - This class represents the "prototype" for a function,
578 /// which captures its name, and its argument names (thus implicitly the number
579 /// of arguments the function takes).
580 class PrototypeAST {
581 std::string Name;
582 std::vector<std::string> Args;
583 public:
584 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
585 : Name(name), Args(args) {}
586
587 Function *Codegen();
588 };
589
590 /// FunctionAST - This class represents a function definition itself.
591 class FunctionAST {
592 PrototypeAST *Proto;
593 ExprAST *Body;
594 public:
595 FunctionAST(PrototypeAST *proto, ExprAST *body)
596 : Proto(proto), Body(body) {}
597
598 Function *Codegen();
599 };
600
601 //===----------------------------------------------------------------------===//
602 // Parser
603 //===----------------------------------------------------------------------===//
604
605 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
606 /// token the parser is looking at. getNextToken reads another token from the
607 /// lexer and updates CurTok with its results.
608 static int CurTok;
609 static int getNextToken() {
610 return CurTok = gettok();
611 }
612
613 /// BinopPrecedence - This holds the precedence for each binary operator that is
614 /// defined.
615 static std::map<char, int> BinopPrecedence;
616
617 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
618 static int GetTokPrecedence() {
619 if (!isascii(CurTok))
620 return -1;
621
622 // Make sure it's a declared binop.
623 int TokPrec = BinopPrecedence[CurTok];
624 if (TokPrec <= 0) return -1;
625 return TokPrec;
626 }
627
628 /// Error* - These are little helper functions for error handling.
629 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
630 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
631 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
632
633 static ExprAST *ParseExpression();
634
635 /// identifierexpr
636 /// ::= identifier
637 /// ::= identifier '(' expression* ')'
638 static ExprAST *ParseIdentifierExpr() {
639 std::string IdName = IdentifierStr;
640
641 getNextToken(); // eat identifier.
642
643 if (CurTok != '(') // Simple variable ref.
644 return new VariableExprAST(IdName);
645
646 // Call.
647 getNextToken(); // eat (
648 std::vector<ExprAST*> Args;
649 if (CurTok != ')') {
650 while (1) {
651 ExprAST *Arg = ParseExpression();
652 if (!Arg) return 0;
653 Args.push_back(Arg);
654
655 if (CurTok == ')') break;
656
657 if (CurTok != ',')
658 return Error("Expected ')' or ',' in argument list");
659 getNextToken();
660 }
661 }
662
663 // Eat the ')'.
664 getNextToken();
665
666 return new CallExprAST(IdName, Args);
667 }
668
669 /// numberexpr ::= number
670 static ExprAST *ParseNumberExpr() {
671 ExprAST *Result = new NumberExprAST(NumVal);
672 getNextToken(); // consume the number
673 return Result;
674 }
675
676 /// parenexpr ::= '(' expression ')'
677 static ExprAST *ParseParenExpr() {
678 getNextToken(); // eat (.
679 ExprAST *V = ParseExpression();
680 if (!V) return 0;
681
682 if (CurTok != ')')
683 return Error("expected ')'");
684 getNextToken(); // eat ).
685 return V;
686 }
687
688 /// primary
689 /// ::= identifierexpr
690 /// ::= numberexpr
691 /// ::= parenexpr
692 static ExprAST *ParsePrimary() {
693 switch (CurTok) {
694 default: return Error("unknown token when expecting an expression");
695 case tok_identifier: return ParseIdentifierExpr();
696 case tok_number: return ParseNumberExpr();
697 case '(': return ParseParenExpr();
698 }
699 }
700
701 /// binoprhs
702 /// ::= ('+' primary)*
703 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
704 // If this is a binop, find its precedence.
705 while (1) {
706 int TokPrec = GetTokPrecedence();
707
708 // If this is a binop that binds at least as tightly as the current binop,
709 // consume it, otherwise we are done.
710 if (TokPrec < ExprPrec)
711 return LHS;
712
713 // Okay, we know this is a binop.
714 int BinOp = CurTok;
715 getNextToken(); // eat binop
716
717 // Parse the primary expression after the binary operator.
718 ExprAST *RHS = ParsePrimary();
719 if (!RHS) return 0;
720
721 // If BinOp binds less tightly with RHS than the operator after RHS, let
722 // the pending operator take RHS as its LHS.
723 int NextPrec = GetTokPrecedence();
724 if (TokPrec < NextPrec) {
725 RHS = ParseBinOpRHS(TokPrec+1, RHS);
726 if (RHS == 0) return 0;
727 }
728
729 // Merge LHS/RHS.
730 LHS = new BinaryExprAST(BinOp, LHS, RHS);
731 }
732 }
733
734 /// expression
735 /// ::= primary binoprhs
736 ///
737 static ExprAST *ParseExpression() {
738 ExprAST *LHS = ParsePrimary();
739 if (!LHS) return 0;
740
741 return ParseBinOpRHS(0, LHS);
742 }
743
744 /// prototype
745 /// ::= id '(' id* ')'
746 static PrototypeAST *ParsePrototype() {
747 if (CurTok != tok_identifier)
748 return ErrorP("Expected function name in prototype");
749
750 std::string FnName = IdentifierStr;
751 getNextToken();
752
753 if (CurTok != '(')
754 return ErrorP("Expected '(' in prototype");
755
756 std::vector<std::string> ArgNames;
757 while (getNextToken() == tok_identifier)
758 ArgNames.push_back(IdentifierStr);
759 if (CurTok != ')')
760 return ErrorP("Expected ')' in prototype");
761
762 // success.
763 getNextToken(); // eat ')'.
764
765 return new PrototypeAST(FnName, ArgNames);
766 }
767
768 /// definition ::= 'def' prototype expression
769 static FunctionAST *ParseDefinition() {
770 getNextToken(); // eat def.
771 PrototypeAST *Proto = ParsePrototype();
772 if (Proto == 0) return 0;
773
774 if (ExprAST *E = ParseExpression())
775 return new FunctionAST(Proto, E);
776 return 0;
777 }
778
779 /// toplevelexpr ::= expression
780 static FunctionAST *ParseTopLevelExpr() {
781 if (ExprAST *E = ParseExpression()) {
782 // Make an anonymous proto.
783 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
784 return new FunctionAST(Proto, E);
785 }
786 return 0;
787 }
788
789 /// external ::= 'extern' prototype
790 static PrototypeAST *ParseExtern() {
791 getNextToken(); // eat extern.
792 return ParsePrototype();
793 }
794
795 //===----------------------------------------------------------------------===//
796 // Code Generation
797 //===----------------------------------------------------------------------===//
798
799 static Module *TheModule;
800 static IRBuilder<> Builder(getGlobalContext());
801 static std::map<std::string, Value*> NamedValues;
802 static FunctionPassManager *TheFPM;
803
804 Value *ErrorV(const char *Str) { Error(Str); return 0; }
805
806 Value *NumberExprAST::Codegen() {
807 return ConstantFP::get(getGlobalContext(), APFloat(Val));
808 }
809
810 Value *VariableExprAST::Codegen() {
811 // Look this variable up in the function.
812 Value *V = NamedValues[Name];
813 return V ? V : ErrorV("Unknown variable name");
814 }
815
816 Value *BinaryExprAST::Codegen() {
817 Value *L = LHS->Codegen();
818 Value *R = RHS->Codegen();
819 if (L == 0 || R == 0) return 0;
820
821 switch (Op) {
822 case '+': return Builder.CreateFAdd(L, R, "addtmp");
823 case '-': return Builder.CreateFSub(L, R, "subtmp");
824 case '*': return Builder.CreateFMul(L, R, "multmp");
825 case '<':
826 L = Builder.CreateFCmpULT(L, R, "cmptmp");
827 // Convert bool 0/1 to double 0.0 or 1.0
828 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
829 "booltmp");
830 default: return ErrorV("invalid binary operator");
831 }
832 }
833
834 Value *CallExprAST::Codegen() {
835 // Look up the name in the global module table.
836 Function *CalleeF = TheModule->getFunction(Callee);
837 if (CalleeF == 0)
838 return ErrorV("Unknown function referenced");
839
840 // If argument mismatch error.
841 if (CalleeF->arg_size() != Args.size())
842 return ErrorV("Incorrect # arguments passed");
843
844 std::vector<Value*> ArgsV;
845 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
846 ArgsV.push_back(Args[i]->Codegen());
847 if (ArgsV.back() == 0) return 0;
848 }
849
850 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
851 }
852
853 Function *PrototypeAST::Codegen() {
854 // Make the function type: double(double,double) etc.
855 std::vector<Type*> Doubles(Args.size(),
856 Type::getDoubleTy(getGlobalContext()));
857 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
858 Doubles, false);
859
860 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
861
862 // If F conflicted, there was already something named 'Name'. If it has a
863 // body, don't allow redefinition or reextern.
864 if (F->getName() != Name) {
865 // Delete the one we just made and get the existing one.
866 F->eraseFromParent();
867 F = TheModule->getFunction(Name);
868
869 // If F already has a body, reject this.
870 if (!F->empty()) {
871 ErrorF("redefinition of function");
872 return 0;
873 }
874
875 // If F took a different number of args, reject.
876 if (F->arg_size() != Args.size()) {
877 ErrorF("redefinition of function with different # args");
878 return 0;
879 }
880 }
881
882 // Set names for all arguments.
883 unsigned Idx = 0;
884 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
885 ++AI, ++Idx) {
886 AI->setName(Args[Idx]);
887
888 // Add arguments to variable symbol table.
889 NamedValues[Args[Idx]] = AI;
890 }
891
892 return F;
893 }
894
895 Function *FunctionAST::Codegen() {
896 NamedValues.clear();
897
898 Function *TheFunction = Proto->Codegen();
899 if (TheFunction == 0)
900 return 0;
901
902 // Create a new basic block to start insertion into.
903 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
904 Builder.SetInsertPoint(BB);
905
906 if (Value *RetVal = Body->Codegen()) {
907 // Finish off the function.
908 Builder.CreateRet(RetVal);
909
910 // Validate the generated code, checking for consistency.
911 verifyFunction(*TheFunction);
912
913 // Optimize the function.
914 TheFPM->run(*TheFunction);
915
916 return TheFunction;
917 }
918
919 // Error reading body, remove function.
920 TheFunction->eraseFromParent();
921 return 0;
922 }
923
924 //===----------------------------------------------------------------------===//
925 // Top-Level parsing and JIT Driver
926 //===----------------------------------------------------------------------===//
927
928 static ExecutionEngine *TheExecutionEngine;
929
930 static void HandleDefinition() {
931 if (FunctionAST *F = ParseDefinition()) {
932 if (Function *LF = F->Codegen()) {
933 fprintf(stderr, "Read function definition:");
934 LF->dump();
935 }
936 } else {
937 // Skip token for error recovery.
938 getNextToken();
939 }
940 }
941
942 static void HandleExtern() {
943 if (PrototypeAST *P = ParseExtern()) {
944 if (Function *F = P->Codegen()) {
945 fprintf(stderr, "Read extern: ");
946 F->dump();
947 }
948 } else {
949 // Skip token for error recovery.
950 getNextToken();
951 }
952 }
953
954 static void HandleTopLevelExpression() {
955 // Evaluate a top-level expression into an anonymous function.
956 if (FunctionAST *F = ParseTopLevelExpr()) {
957 if (Function *LF = F->Codegen()) {
958 fprintf(stderr, "Read top-level expression:");
959 LF->dump();
960
961 // JIT the function, returning a function pointer.
962 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
963
964 // Cast it to the right type (takes no arguments, returns a double) so we
965 // can call it as a native function.
966 double (*FP)() = (double (*)())(intptr_t)FPtr;
967 fprintf(stderr, "Evaluated to %f\n", FP());
968 }
969 } else {
970 // Skip token for error recovery.
971 getNextToken();
972 }
973 }
974
975 /// top ::= definition | external | expression | ';'
976 static void MainLoop() {
977 while (1) {
978 fprintf(stderr, "ready> ");
979 switch (CurTok) {
980 case tok_eof: return;
981 case ';': getNextToken(); break; // ignore top-level semicolons.
982 case tok_def: HandleDefinition(); break;
983 case tok_extern: HandleExtern(); break;
984 default: HandleTopLevelExpression(); break;
985 }
986 }
987 }
988
989 //===----------------------------------------------------------------------===//
990 // "Library" functions that can be "extern'd" from user code.
991 //===----------------------------------------------------------------------===//
992
993 /// putchard - putchar that takes a double and returns 0.
994 extern "C"
995 double putchard(double X) {
996 putchar((char)X);
997 return 0;
998 }
999
1000 //===----------------------------------------------------------------------===//
1001 // Main driver code.
1002 //===----------------------------------------------------------------------===//
1003
1004 int main() {
1005 InitializeNativeTarget();
1006 LLVMContext &Context = getGlobalContext();
1007
1008 // Install standard binary operators.
1009 // 1 is lowest precedence.
1010 BinopPrecedence['<'] = 10;
1011 BinopPrecedence['+'] = 20;
1012 BinopPrecedence['-'] = 20;
1013 BinopPrecedence['*'] = 40; // highest.
1014
1015 // Prime the first token.
1016 fprintf(stderr, "ready> ");
1017 getNextToken();
1018
1019 // Make the module, which holds all the code.
1020 TheModule = new Module("my cool jit", Context);
1021
1022 // Create the JIT. This takes ownership of the module.
1023 std::string ErrStr;
1024 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
1025 if (!TheExecutionEngine) {
1026 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
1027 exit(1);
1028 }
1029
1030 FunctionPassManager OurFPM(TheModule);
1031
1032 // Set up the optimizer pipeline. Start with registering info about how the
1033 // target lays out data structures.
1034 OurFPM.add(new DataLayout(*TheExecutionEngine->getDataLayout()));
1035 // Provide basic AliasAnalysis support for GVN.
1036 OurFPM.add(createBasicAliasAnalysisPass());
1037 // Do simple "peephole" optimizations and bit-twiddling optzns.
1038 OurFPM.add(createInstructionCombiningPass());
1039 // Reassociate expressions.
1040 OurFPM.add(createReassociatePass());
1041 // Eliminate Common SubExpressions.
1042 OurFPM.add(createGVNPass());
1043 // Simplify the control flow graph (deleting unreachable blocks, etc).
1044 OurFPM.add(createCFGSimplificationPass());
1045
1046 OurFPM.doInitialization();
1047
1048 // Set the global so the code gen can use this.
1049 TheFPM = &OurFPM;
1050
1051 // Run the main "interpreter loop" now.
1052 MainLoop();
1053
1054 TheFPM = 0;
1055
1056 // Print out all of the generated code.
1057 TheModule->dump();
1058
1059 return 0;
1060 }
1061
1062`Next: Extending the language: control flow <LangImpl5.html>`_
1063