1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
6 <title>Kaleidoscope: Extending the Language: User-defined Operators</title>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8 <meta name="author" content="Chris Lattner">
9 <link rel="stylesheet" href="../llvm.css" type="text/css">
14 <div class="doc_title">Kaleidoscope: Extending the Language: User-defined Operators</div>
19 <li><a href="#intro">Chapter 6 Introduction</a></li>
20 <li><a href="#idea">User-defined Operators: the Idea</a></li>
21 <li><a href="#binary">User-defined Binary Operators</a></li>
22 <li><a href="#unary">User-defined Unary Operators</a></li>
23 <li><a href="#example">Kicking the Tires</a></li>
24 <li><a href="#code">Full Code Listing</a></li>
29 <div class="doc_author">
30 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
33 <!-- *********************************************************************** -->
34 <div class="doc_section"><a name="intro">Chapter 6 Introduction</a></div>
35 <!-- *********************************************************************** -->
37 <div class="doc_text">
39 <p>Welcome to Chapter 6 of the "<a href="index.html">Implementing a language
40 with LLVM</a>" tutorial. At this point in our tutorial, we now have a fully
41 functional language that is fairly minimal, but also useful. One big problem
42 with it though is that it doesn't have many useful operators (like division,
43 logical negation, or even any comparisons other than less-than.</p>
45 <p>This chapter of the tutorial takes a wild digression into adding user-defined
46 operators to the simple and beautiful Kaleidoscope language, giving us a
47 simple and ugly language in some ways, but also a powerful one at the same time.
48 One of the great things about creating your own language is that you get to
49 decide what is good or bad. In this tutorial we'll assume that it is okay and
50 use this as a way to show some interesting parsing techniques.</p>
52 <p>At the end of this tutorial, we'll <a href="#example">run through a nice
53 little example</a> that shows an example application that you can build with
54 Kaleidoscope and the feature set it now has.</p>
58 <!-- *********************************************************************** -->
59 <div class="doc_section"><a name="idea">User-defined Operators: the Idea</a></div>
60 <!-- *********************************************************************** -->
62 <div class="doc_text">
65 The "operator overloading" that we will add to Kaleidoscope is more general than
66 languages like C++. In C++, you are only allowed to redefine existing
67 operators: you can't programatically change the grammar, introduce new
68 operators, change precedence levels, etc. In this chapter, we will add this
69 capability to Kaleidoscope, which will allow us to round out the set of
70 operators that are supported, culminating in a more interesting example app.</p>
72 <p>The point of going into user-defined operators in a tutorial like this is to
73 show the power and flexibility of using a hand-written parser. The parser we
74 are using so far is using recursive descent for most parts of the grammar, and
75 operator precedence parsing for the expressions. See <a
76 href="LangImpl2.html">Chapter 2</a> for details. Without using operator
77 precedence parsing, it would be very difficult to allow the programmer to
78 introduce new operators into the grammar: the grammar is dynamically extensible
81 <p>The two specific features we'll add are programmable unary operators (right
82 now, Kaleidoscope has no unary operators at all) as well as binary operators.
83 An example of this is:</p>
85 <div class="doc_code">
94 # Define > with the same precedence as <.
95 def binary> 10 (LHS RHS)
96 !(LHS < RHS); # alternatively, could just use "RHS < LHS"
98 # Binary "logical or", (note that it does not "short circuit")
99 def binary| 5 (LHS RHS)
107 # Define = with slightly lower precedence than relationals.
108 def binary= 9 (LHS RHS)
109 !(LHS < RHS | LHS > RHS);
113 <p>Many languages aspire to being able to implement their standard runtime
114 library in the language itself. In Kaleidoscope, we can implement significant
115 parts of the language in the library!</p>
117 <p>We will break down implementation of these features into two parts:
118 implementing support for user-defined binary operators and adding unary
123 <!-- *********************************************************************** -->
124 <div class="doc_section"><a name="binary">User-defined Binary Operators</a></div>
125 <!-- *********************************************************************** -->
127 <div class="doc_text">
129 <p>Adding support for user-defined binary operators is pretty simple with our
130 current framework. We'll first add support for the unary/binary keywords:</p>
132 <div class="doc_code">
137 tok_binary = -11, tok_unary = -12</b>
140 static int gettok() {
142 if (IdentifierStr == "for") return tok_for;
143 if (IdentifierStr == "in") return tok_in;
144 <b>if (IdentifierStr == "binary") return tok_binary;
145 if (IdentifierStr == "unary") return tok_unary;</b>
146 return tok_identifier;
150 <p>This just adds lexer support for the unary and binary keywords, like we
151 did in <a href="LangImpl5.html#iflexer">previous chapters</a>. One nice thing
152 about our current AST is that we represent binary operators fully generally
153 with their ASCII code as the opcode. For our extended operators, we'll use the
154 same representation, so we don't need any new AST or parser support.</p>
156 <p>On the other hand, we have to be able to represent the definitions of these
157 new operators, in the "def binary| 5" part of the function definition. In the
158 grammar so far, the "name" for the function definition is parsed as the
159 "prototype" production and into the <tt>PrototypeAST</tt> AST node. To
160 represent our new user-defined operators as prototypes, we have to extend
161 the <tt>PrototypeAST</tt> AST node like this:</p>
163 <div class="doc_code">
165 /// PrototypeAST - This class represents the "prototype" for a function,
166 /// which captures its argument names as well as if it is an operator.
169 std::vector<std::string> Args;
171 unsigned Precedence; // Precedence if a binary op.</b>
173 PrototypeAST(const std::string &name, const std::vector<std::string> &args,
174 <b>bool isoperator = false, unsigned prec = 0</b>)
175 : Name(name), Args(args), <b>isOperator(isoperator), Precedence(prec)</b> {}
177 <b>bool isUnaryOp() const { return isOperator && Args.size() == 1; }
178 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
180 char getOperatorName() const {
181 assert(isUnaryOp() || isBinaryOp());
182 return Name[Name.size()-1];
185 unsigned getBinaryPrecedence() const { return Precedence; }</b>
192 <p>Basically, in addition to knowing a name for the prototype, we now keep track
193 of whether it was an operator, and if it was, what precedence level the operator
194 is at. The precedence is only used for binary operators (as you'll see below,
195 it just doesn't apply for unary operators). Now that we have a way to represent
196 the prototype for a user-defined operator, we need to parse it:</p>
198 <div class="doc_code">
201 /// ::= id '(' id* ')'
202 <b>/// ::= binary LETTER number? (id, id)</b>
203 static PrototypeAST *ParsePrototype() {
206 <b>int Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
207 unsigned BinaryPrecedence = 30;</b>
211 return ErrorP("Expected function name in prototype");
213 FnName = IdentifierStr;
219 if (!isascii(CurTok))
220 return ErrorP("Expected binary operator");
222 FnName += (char)CurTok;
226 // Read the precedence if present.
227 if (CurTok == tok_number) {
228 if (NumVal < 1 || NumVal > 100)
229 return ErrorP("Invalid precedecnce: must be 1..100");
230 BinaryPrecedence = (unsigned)NumVal;
237 return ErrorP("Expected '(' in prototype");
239 std::vector<std::string> ArgNames;
240 while (getNextToken() == tok_identifier)
241 ArgNames.push_back(IdentifierStr);
243 return ErrorP("Expected ')' in prototype");
246 getNextToken(); // eat ')'.
248 <b>// Verify right number of names for operator.
249 if (Kind && ArgNames.size() != Kind)
250 return ErrorP("Invalid number of operands for operator");
252 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);</b>
257 <p>This is all fairly straight-forward parsing code, and we have already seen
258 a lot of similar code in the past. One interesting piece of this is the part
259 that sets up <tt>FnName</tt> for binary operators. This builds names like
260 "binary@" for a newly defined "@" operator. This takes advantage of the fact
261 that symbol names in the LLVM symbol table are allowed to have any character in
262 them, inluding embedded nul characters.</p>
264 <p>The next interesting piece is codegen support for these binary operators.
265 Given our current structure, this is a simple addition of a default case for our
266 existing binary operator node:</p>
268 <div class="doc_code">
270 Value *BinaryExprAST::Codegen() {
271 Value *L = LHS->Codegen();
272 Value *R = RHS->Codegen();
273 if (L == 0 || R == 0) return 0;
276 case '+': return Builder.CreateAdd(L, R, "addtmp");
277 case '-': return Builder.CreateSub(L, R, "subtmp");
278 case '*': return Builder.CreateMul(L, R, "multmp");
280 L = Builder.CreateFCmpULT(L, R, "multmp");
281 // Convert bool 0/1 to double 0.0 or 1.0
282 return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
283 <b>default: break;</b>
286 <b>// If it wasn't a builtin binary operator, it must be a user defined one. Emit
288 Function *F = TheModule->getFunction(std::string("binary")+Op);
289 assert(F && "binary operator not found!");
291 Value *Ops[] = { L, R };
292 return Builder.CreateCall(F, Ops, Ops+2, "binop");</b>
298 <p>As you can see above, the new code is actually really simple. It just does
299 a lookup for the appropriate operator in the symbol table and generates a
300 function call to it. Since user-defined operators are just built as normal
301 functions (because the "prototype" boils down into a function with the right
302 name) everything falls into place.</p>
304 <p>The final missing piece is a bit of top level magic, here:</p>
306 <div class="doc_code">
308 Function *FunctionAST::Codegen() {
311 Function *TheFunction = Proto->Codegen();
312 if (TheFunction == 0)
315 <b>// If this is an operator, install it.
316 if (Proto->isBinaryOp())
317 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();</b>
319 // Create a new basic block to start insertion into.
320 BasicBlock *BB = new BasicBlock("entry", TheFunction);
321 Builder.SetInsertPoint(BB);
323 if (Value *RetVal = Body->Codegen()) {
328 <p>Basically, before codegening a function, if it is a user-defined operator, we
329 register it in the precedence table. This allows the binary operator parsing
330 logic we already have to handle it. Since it is a fully-general operator
331 precedence parser, this is all we need to do to "extend the grammar".</p>
333 <p>With that, we have useful user-defined binary operators. This builds a lot
334 on the previous framework we built for other operators. Adding unary operators
335 is a bit more challenging, because we don't have any framework for it yet, lets
336 see what it takes.</p>
340 <!-- *********************************************************************** -->
341 <div class="doc_section"><a name="unary">User-defined Unary Operators</a></div>
342 <!-- *********************************************************************** -->
344 <div class="doc_text">
346 <p>Since we don't currently support unary operators in the Kaleidoscope
347 langugage, we'll need to add everything for them. Above, we added simple
348 support for the 'unary' keyword to the lexer. In addition to that, we need an
351 <div class="doc_code">
353 /// UnaryExprAST - Expression class for a unary operator.
354 class UnaryExprAST : public ExprAST {
358 UnaryExprAST(char opcode, ExprAST *operand)
359 : Opcode(opcode), Operand(operand) {}
360 virtual Value *Codegen();
365 <p>This AST node is very simple and obvious by now. It directly mirrors the
366 binary operator AST node, except that it only has one child. With this, we
367 need to add the parsing logic. Parsing a unary operator is pretty simple: we'll
368 add a new function to do it:</p>
370 <div class="doc_code">
375 static ExprAST *ParseUnary() {
376 // If the current token is not an operator, it must be a primary expr.
377 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
378 return ParsePrimary();
380 // If this is a unary operator, read it.
383 if (ExprAST *Operand = ParseUnary())
384 return new UnaryExprAST(Opc, Operand);
390 <p>The grammar we add is pretty straight-forward here. If we see a unary
391 operator when parsing a primary operator, we eat the operator as a prefix and
392 parse the remaining piece as another unary operator. This allows us to handle
393 multiple unary operators (e.g. "!!x"). Note that unary operators can't have
394 ambiguous parses like binary operators can, so there is no need for precedence
397 <p>The problem with the above is that we need to call ParseUnary from somewhere.
398 To do this, we change previous callers of ParsePrimary to call ParseUnary
401 <div class="doc_code">
405 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
407 <b>// Parse the unary expression after the binary operator.
408 ExprAST *RHS = ParseUnary();
409 if (!RHS) return 0;</b>
413 /// ::= unary binoprhs
415 static ExprAST *ParseExpression() {
416 <b>ExprAST *LHS = ParseUnary();</b>
419 return ParseBinOpRHS(0, LHS);
424 <p>With these two simple changes, we now parse unary operators and build the
425 AST for them. Next up, we need to add parser support for prototypes, to parse
426 the unary operator prototype. We extend the binary operator code above
429 <div class="doc_code">
432 /// ::= id '(' id* ')'
433 /// ::= binary LETTER number? (id, id)
434 <b>/// ::= unary LETTER (id)</b>
435 static PrototypeAST *ParsePrototype() {
438 int Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
439 unsigned BinaryPrecedence = 30;
443 return ErrorP("Expected function name in prototype");
445 FnName = IdentifierStr;
451 if (!isascii(CurTok))
452 return ErrorP("Expected unary operator");
454 FnName += (char)CurTok;
463 <p>As with binary operators, we name unary operators with a name that includes
464 the operator character. This assists us at code generation time. Speaking of,
465 the final piece we need to add is codegen support for unary operators. It looks
468 <div class="doc_code">
470 Value *UnaryExprAST::Codegen() {
471 Value *OperandV = Operand->Codegen();
472 if (OperandV == 0) return 0;
474 Function *F = TheModule->getFunction(std::string("unary")+Opcode);
476 return ErrorV("Unknown unary operator");
478 return Builder.CreateCall(F, OperandV, "unop");
483 <p>This code is similar to, but simpler than, the code for binary operators. It
484 is simpler primarily because it doesn't need to handle any predefined operators.
489 <!-- *********************************************************************** -->
490 <div class="doc_section"><a name="example">Kicking the Tires</a></div>
491 <!-- *********************************************************************** -->
493 <div class="doc_text">
495 <p>It is somewhat hard to believe, but with a few simple extensions we've
496 covered in the last chapters, we have grown a real-ish language. With this, we
497 can do a lot of interesting things, including I/O, math, and a bunch of other
498 things. For example, we can now add a nice sequencing operator (printd is
499 defined to print out the specified value and a newline):</p>
501 <div class="doc_code">
503 ready> <b>extern printd(x);</b>
504 Read extern: declare double @printd(double)
505 ready> <b>def binary : 1 (x y) 0; # Low-precedence operator that ignores operands.</b>
507 ready> <b>printd(123) : printd(456) : printd(789);</b>
511 Evaluated to 0.000000
515 <p>We can also define a bunch of other "primitive" operations, such as:</p>
517 <div class="doc_code">
530 # Define > with the same precedence as >. We could also easily define
532 def binary> 10 (LHS RHS)
535 # Binary logical or, which does not short circuit.
536 def binary| 5 (LHS RHS)
544 # Binary logical and, which does not short circuit.
545 def binary& 6 (LHS RHS)
551 # Define = with slightly lower precedence than relationals.
552 def binary = 9 (LHS RHS)
553 !(LHS < RHS | LHS > RHS);
559 <p>Given the previous if/then/else support, we can also define interesting
560 functions for I/O. For example, the following prints out a character whose
561 "density" reflects the value passed in: the lower the value, the denser the
564 <div class="doc_code">
568 extern putchard(char)
572 else if d > 4 then
574 else if d > 2 then
577 putchard(42); # '*'</b>
579 ready> <b>printdensity(1): printdensity(2): printdensity(3) :
580 printdensity(4): printdensity(5): printdensity(9): putchard(10);</b>
582 Evaluated to 0.000000
586 <p>Based on these simple primitive operations, we can start to define more
587 interesting things. For example, here's a little function that solves for the
588 number of iterations it takes for a function in the complex plane to
591 <div class="doc_code">
593 # determine whether the specific location diverges.
594 # Solve for z = z^2 + c in the complex plane.
595 def mandleconverger(real imag iters creal cimag)
596 if iters > 255 | (real*real + imag*imag > 4) then
599 mandleconverger(real*real - imag*imag + creal,
601 iters+1, creal, cimag);
603 # return the number of iterations required for the iteration to escape
604 def mandleconverge(real imag)
605 mandleconverger(real, imag, 0, real, imag);
609 <p>This "z = z<sup>2</sup> + c" function is a beautiful little creature that is the basis
610 for computation of the <a
611 href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot Set</a>. Our
612 <tt>mandelconverge</tt> function returns the number of iterations that it takes
613 for a complex orbit to escape, saturating to 255. This is not a very useful
614 function by itself, but if you plot its value over a two-dimensional plane,
615 you can see the mandelbrot set. Given that we are limited to using putchard
616 here, our amazing graphical output is limited, but we can whip together
617 something using the density plotter above:</p>
619 <div class="doc_code">
621 # compute and plot the mandlebrot set with the specified 2 dimentional range
623 def mandelhelp(xmin xmax xstep ymin ymax ystep)
624 for y = ymin, y < ymax, ystep in (
625 (for x = xmin, x < xmax, xstep in
626 printdensity(mandleconverge(x,y)))
630 # mandel - This is a convenient helper function for ploting the mandelbrot set
631 # from the specified position with the specified magnification.
632 def mandel(realstart imagstart realmag imagmag)
633 mandelhelp(realstart, realstart+realmag*78, realmag,
634 imagstart, imagstart+imagmag*40, imagmag);
638 <p>Given this, we can try plotting out the mandlebrot set! Lets try it out:</p>
640 <div class="doc_code">
642 ready> <b>mandel(-2.3, -1.3, 0.05, 0.07);</b>
643 *******************************+++++++++++*************************************
644 *************************+++++++++++++++++++++++*******************************
645 **********************+++++++++++++++++++++++++++++****************************
646 *******************+++++++++++++++++++++.. ...++++++++*************************
647 *****************++++++++++++++++++++++.... ...+++++++++***********************
648 ***************+++++++++++++++++++++++..... ...+++++++++*********************
649 **************+++++++++++++++++++++++.... ....+++++++++********************
650 *************++++++++++++++++++++++...... .....++++++++*******************
651 ************+++++++++++++++++++++....... .......+++++++******************
652 ***********+++++++++++++++++++.... ... .+++++++*****************
653 **********+++++++++++++++++....... .+++++++****************
654 *********++++++++++++++........... ...+++++++***************
655 ********++++++++++++............ ...++++++++**************
656 ********++++++++++... .......... .++++++++**************
657 *******+++++++++..... .+++++++++*************
658 *******++++++++...... ..+++++++++*************
659 *******++++++....... ..+++++++++*************
660 *******+++++...... ..+++++++++*************
661 *******.... .... ...+++++++++*************
662 *******.... . ...+++++++++*************
663 *******+++++...... ...+++++++++*************
664 *******++++++....... ..+++++++++*************
665 *******++++++++...... .+++++++++*************
666 *******+++++++++..... ..+++++++++*************
667 ********++++++++++... .......... .++++++++**************
668 ********++++++++++++............ ...++++++++**************
669 *********++++++++++++++.......... ...+++++++***************
670 **********++++++++++++++++........ .+++++++****************
671 **********++++++++++++++++++++.... ... ..+++++++****************
672 ***********++++++++++++++++++++++....... .......++++++++*****************
673 ************+++++++++++++++++++++++...... ......++++++++******************
674 **************+++++++++++++++++++++++.... ....++++++++********************
675 ***************+++++++++++++++++++++++..... ...+++++++++*********************
676 *****************++++++++++++++++++++++.... ...++++++++***********************
677 *******************+++++++++++++++++++++......++++++++*************************
678 *********************++++++++++++++++++++++.++++++++***************************
679 *************************+++++++++++++++++++++++*******************************
680 ******************************+++++++++++++************************************
681 *******************************************************************************
682 *******************************************************************************
683 *******************************************************************************
684 Evaluated to 0.000000
685 ready> <b>mandel(-2, -1, 0.02, 0.04);</b>
686 **************************+++++++++++++++++++++++++++++++++++++++++++++++++++++
687 ***********************++++++++++++++++++++++++++++++++++++++++++++++++++++++++
688 *********************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
689 *******************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++...
690 *****************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....
691 ***************++++++++++++++++++++++++++++++++++++++++++++++++++++++++........
692 **************++++++++++++++++++++++++++++++++++++++++++++++++++++++...........
693 ************+++++++++++++++++++++++++++++++++++++++++++++++++++++..............
694 ***********++++++++++++++++++++++++++++++++++++++++++++++++++........ .
695 **********++++++++++++++++++++++++++++++++++++++++++++++.............
696 ********+++++++++++++++++++++++++++++++++++++++++++..................
697 *******+++++++++++++++++++++++++++++++++++++++.......................
698 ******+++++++++++++++++++++++++++++++++++...........................
699 *****++++++++++++++++++++++++++++++++............................
700 *****++++++++++++++++++++++++++++...............................
701 ****++++++++++++++++++++++++++...... .........................
702 ***++++++++++++++++++++++++......... ...... ...........
703 ***++++++++++++++++++++++............
704 **+++++++++++++++++++++..............
705 **+++++++++++++++++++................
706 *++++++++++++++++++.................
707 *++++++++++++++++............ ...
708 *++++++++++++++..............
709 *+++....++++................
710 *.......... ...........
712 *.......... ...........
713 *+++....++++................
714 *++++++++++++++..............
715 *++++++++++++++++............ ...
716 *++++++++++++++++++.................
717 **+++++++++++++++++++................
718 **+++++++++++++++++++++..............
719 ***++++++++++++++++++++++............
720 ***++++++++++++++++++++++++......... ...... ...........
721 ****++++++++++++++++++++++++++...... .........................
722 *****++++++++++++++++++++++++++++...............................
723 *****++++++++++++++++++++++++++++++++............................
724 ******+++++++++++++++++++++++++++++++++++...........................
725 *******+++++++++++++++++++++++++++++++++++++++.......................
726 ********+++++++++++++++++++++++++++++++++++++++++++..................
727 Evaluated to 0.000000
728 ready> <b>mandel(-0.9, -1.4, 0.02, 0.03);</b>
729 *******************************************************************************
730 *******************************************************************************
731 *******************************************************************************
732 **********+++++++++++++++++++++************************************************
733 *+++++++++++++++++++++++++++++++++++++++***************************************
734 +++++++++++++++++++++++++++++++++++++++++++++**********************************
735 ++++++++++++++++++++++++++++++++++++++++++++++++++*****************************
736 ++++++++++++++++++++++++++++++++++++++++++++++++++++++*************************
737 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++**********************
738 +++++++++++++++++++++++++++++++++.........++++++++++++++++++*******************
739 +++++++++++++++++++++++++++++++.... ......+++++++++++++++++++****************
740 +++++++++++++++++++++++++++++....... ........+++++++++++++++++++**************
741 ++++++++++++++++++++++++++++........ ........++++++++++++++++++++************
742 +++++++++++++++++++++++++++......... .. ...+++++++++++++++++++++**********
743 ++++++++++++++++++++++++++........... ....++++++++++++++++++++++********
744 ++++++++++++++++++++++++............. .......++++++++++++++++++++++******
745 +++++++++++++++++++++++............. ........+++++++++++++++++++++++****
746 ++++++++++++++++++++++........... ..........++++++++++++++++++++++***
747 ++++++++++++++++++++........... .........++++++++++++++++++++++*
748 ++++++++++++++++++............ ...........++++++++++++++++++++
749 ++++++++++++++++............... .............++++++++++++++++++
750 ++++++++++++++................. ...............++++++++++++++++
751 ++++++++++++.................. .................++++++++++++++
752 +++++++++.................. .................+++++++++++++
753 ++++++........ . ......... ..++++++++++++
754 ++............ ...... ....++++++++++
755 .............. ...++++++++++
756 .............. ....+++++++++
757 .............. .....++++++++
758 ............. ......++++++++
759 ........... .......++++++++
760 ......... ........+++++++
761 ......... ........+++++++
762 ......... ....+++++++
770 Evaluated to 0.000000
775 <p>At this point, you may be starting to realize that Kaleidoscope is a real
776 and powerful language. It may not be self-similar :), but it can be used to
777 plot things that are!</p>
779 <p>With this, we conclude the "adding user-defined operators" chapter of the
780 tutorial. We successfully extended our language with the ability to extend the
781 language in the library, and showed how this can be used to build a simple but
782 interesting end user application in Kaleidoscope. At this point, Kaleidoscope
783 can build a variety of applications that are functional and can call functions
784 with side-effects, but it can't actually define and mutate a variable itself.
787 <p>Strikingly, lack of this feature is an important limitation for some
788 languages, and it is not at all obvious how to <a href="LangImpl7.html">add
789 support for mutable variables</a> without having to add an "SSA construction"
790 phase to your front-end. In the next chapter, we will describe how you can
791 add this without building SSA in your front-end.</p>
796 <!-- *********************************************************************** -->
797 <div class="doc_section"><a name="code">Full Code Listing</a></div>
798 <!-- *********************************************************************** -->
800 <div class="doc_text">
803 Here is the complete code listing for our running example, enhanced with the
804 if/then/else and for expressions.. To build this example, use:
807 <div class="doc_code">
810 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
816 <p>Here is the code:</p>
818 <div class="doc_code">
820 #include "llvm/DerivedTypes.h"
821 #include "llvm/ExecutionEngine/ExecutionEngine.h"
822 #include "llvm/Module.h"
823 #include "llvm/ModuleProvider.h"
824 #include "llvm/PassManager.h"
825 #include "llvm/Analysis/Verifier.h"
826 #include "llvm/Target/TargetData.h"
827 #include "llvm/Transforms/Scalar.h"
828 #include "llvm/Support/LLVMBuilder.h"
829 #include <cstdio>
830 #include <string>
832 #include <vector>
833 using namespace llvm;
835 //===----------------------------------------------------------------------===//
837 //===----------------------------------------------------------------------===//
839 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
840 // of these for known things.
845 tok_def = -2, tok_extern = -3,
848 tok_identifier = -4, tok_number = -5,
851 tok_if = -6, tok_then = -7, tok_else = -8,
852 tok_for = -9, tok_in = -10,
855 tok_binary = -11, tok_unary = -12
858 static std::string IdentifierStr; // Filled in if tok_identifier
859 static double NumVal; // Filled in if tok_number
861 /// gettok - Return the next token from standard input.
862 static int gettok() {
863 static int LastChar = ' ';
865 // Skip any whitespace.
866 while (isspace(LastChar))
867 LastChar = getchar();
869 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
870 IdentifierStr = LastChar;
871 while (isalnum((LastChar = getchar())))
872 IdentifierStr += LastChar;
874 if (IdentifierStr == "def") return tok_def;
875 if (IdentifierStr == "extern") return tok_extern;
876 if (IdentifierStr == "if") return tok_if;
877 if (IdentifierStr == "then") return tok_then;
878 if (IdentifierStr == "else") return tok_else;
879 if (IdentifierStr == "for") return tok_for;
880 if (IdentifierStr == "in") return tok_in;
881 if (IdentifierStr == "binary") return tok_binary;
882 if (IdentifierStr == "unary") return tok_unary;
883 return tok_identifier;
886 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
890 LastChar = getchar();
891 } while (isdigit(LastChar) || LastChar == '.');
893 NumVal = strtod(NumStr.c_str(), 0);
897 if (LastChar == '#') {
898 // Comment until end of line.
899 do LastChar = getchar();
900 while (LastChar != EOF && LastChar != '\n' & LastChar != '\r');
906 // Check for end of file. Don't eat the EOF.
910 // Otherwise, just return the character as its ascii value.
911 int ThisChar = LastChar;
912 LastChar = getchar();
916 //===----------------------------------------------------------------------===//
917 // Abstract Syntax Tree (aka Parse Tree)
918 //===----------------------------------------------------------------------===//
920 /// ExprAST - Base class for all expression nodes.
923 virtual ~ExprAST() {}
924 virtual Value *Codegen() = 0;
927 /// NumberExprAST - Expression class for numeric literals like "1.0".
928 class NumberExprAST : public ExprAST {
931 NumberExprAST(double val) : Val(val) {}
932 virtual Value *Codegen();
935 /// VariableExprAST - Expression class for referencing a variable, like "a".
936 class VariableExprAST : public ExprAST {
939 VariableExprAST(const std::string &name) : Name(name) {}
940 virtual Value *Codegen();
943 /// UnaryExprAST - Expression class for a unary operator.
944 class UnaryExprAST : public ExprAST {
948 UnaryExprAST(char opcode, ExprAST *operand)
949 : Opcode(opcode), Operand(operand) {}
950 virtual Value *Codegen();
953 /// BinaryExprAST - Expression class for a binary operator.
954 class BinaryExprAST : public ExprAST {
958 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
959 : Op(op), LHS(lhs), RHS(rhs) {}
960 virtual Value *Codegen();
963 /// CallExprAST - Expression class for function calls.
964 class CallExprAST : public ExprAST {
966 std::vector<ExprAST*> Args;
968 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
969 : Callee(callee), Args(args) {}
970 virtual Value *Codegen();
973 /// IfExprAST - Expression class for if/then/else.
974 class IfExprAST : public ExprAST {
975 ExprAST *Cond, *Then, *Else;
977 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
978 : Cond(cond), Then(then), Else(_else) {}
979 virtual Value *Codegen();
982 /// ForExprAST - Expression class for for/in.
983 class ForExprAST : public ExprAST {
985 ExprAST *Start, *End, *Step, *Body;
987 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
988 ExprAST *step, ExprAST *body)
989 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
990 virtual Value *Codegen();
993 /// PrototypeAST - This class represents the "prototype" for a function,
994 /// which captures its argument names as well as if it is an operator.
997 std::vector<std::string> Args;
999 unsigned Precedence; // Precedence if a binary op.
1001 PrototypeAST(const std::string &name, const std::vector<std::string> &args,
1002 bool isoperator = false, unsigned prec = 0)
1003 : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
1005 bool isUnaryOp() const { return isOperator && Args.size() == 1; }
1006 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
1008 char getOperatorName() const {
1009 assert(isUnaryOp() || isBinaryOp());
1010 return Name[Name.size()-1];
1013 unsigned getBinaryPrecedence() const { return Precedence; }
1015 Function *Codegen();
1018 /// FunctionAST - This class represents a function definition itself.
1020 PrototypeAST *Proto;
1023 FunctionAST(PrototypeAST *proto, ExprAST *body)
1024 : Proto(proto), Body(body) {}
1026 Function *Codegen();
1029 //===----------------------------------------------------------------------===//
1031 //===----------------------------------------------------------------------===//
1033 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
1034 /// token the parser it looking at. getNextToken reads another token from the
1035 /// lexer and updates CurTok with its results.
1037 static int getNextToken() {
1038 return CurTok = gettok();
1041 /// BinopPrecedence - This holds the precedence for each binary operator that is
1043 static std::map<char, int> BinopPrecedence;
1045 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
1046 static int GetTokPrecedence() {
1047 if (!isascii(CurTok))
1050 // Make sure it's a declared binop.
1051 int TokPrec = BinopPrecedence[CurTok];
1052 if (TokPrec <= 0) return -1;
1056 /// Error* - These are little helper functions for error handling.
1057 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
1058 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
1059 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
1061 static ExprAST *ParseExpression();
1065 /// ::= identifier '(' expression* ')'
1066 static ExprAST *ParseIdentifierExpr() {
1067 std::string IdName = IdentifierStr;
1069 getNextToken(); // eat identifier.
1071 if (CurTok != '(') // Simple variable ref.
1072 return new VariableExprAST(IdName);
1075 getNextToken(); // eat (
1076 std::vector<ExprAST*> Args;
1077 if (CurTok != ')') {
1079 ExprAST *Arg = ParseExpression();
1081 Args.push_back(Arg);
1083 if (CurTok == ')') break;
1086 return Error("Expected ')'");
1094 return new CallExprAST(IdName, Args);
1097 /// numberexpr ::= number
1098 static ExprAST *ParseNumberExpr() {
1099 ExprAST *Result = new NumberExprAST(NumVal);
1100 getNextToken(); // consume the number
1104 /// parenexpr ::= '(' expression ')'
1105 static ExprAST *ParseParenExpr() {
1106 getNextToken(); // eat (.
1107 ExprAST *V = ParseExpression();
1111 return Error("expected ')'");
1112 getNextToken(); // eat ).
1116 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
1117 static ExprAST *ParseIfExpr() {
1118 getNextToken(); // eat the if.
1121 ExprAST *Cond = ParseExpression();
1122 if (!Cond) return 0;
1124 if (CurTok != tok_then)
1125 return Error("expected then");
1126 getNextToken(); // eat the then
1128 ExprAST *Then = ParseExpression();
1129 if (Then == 0) return 0;
1131 if (CurTok != tok_else)
1132 return Error("expected else");
1136 ExprAST *Else = ParseExpression();
1137 if (!Else) return 0;
1139 return new IfExprAST(Cond, Then, Else);
1142 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
1143 static ExprAST *ParseForExpr() {
1144 getNextToken(); // eat the for.
1146 if (CurTok != tok_identifier)
1147 return Error("expected identifier after for");
1149 std::string IdName = IdentifierStr;
1150 getNextToken(); // eat identifier.
1153 return Error("expected '=' after for");
1154 getNextToken(); // eat '='.
1157 ExprAST *Start = ParseExpression();
1158 if (Start == 0) return 0;
1160 return Error("expected ',' after for start value");
1163 ExprAST *End = ParseExpression();
1164 if (End == 0) return 0;
1166 // The step value is optional.
1168 if (CurTok == ',') {
1170 Step = ParseExpression();
1171 if (Step == 0) return 0;
1174 if (CurTok != tok_in)
1175 return Error("expected 'in' after for");
1176 getNextToken(); // eat 'in'.
1178 ExprAST *Body = ParseExpression();
1179 if (Body == 0) return 0;
1181 return new ForExprAST(IdName, Start, End, Step, Body);
1186 /// ::= identifierexpr
1191 static ExprAST *ParsePrimary() {
1193 default: return Error("unknown token when expecting an expression");
1194 case tok_identifier: return ParseIdentifierExpr();
1195 case tok_number: return ParseNumberExpr();
1196 case '(': return ParseParenExpr();
1197 case tok_if: return ParseIfExpr();
1198 case tok_for: return ParseForExpr();
1205 static ExprAST *ParseUnary() {
1206 // If the current token is not an operator, it must be a primary expr.
1207 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
1208 return ParsePrimary();
1210 // If this is a unary operator, read it.
1213 if (ExprAST *Operand = ParseUnary())
1214 return new UnaryExprAST(Opc, Operand);
1219 /// ::= ('+' unary)*
1220 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
1221 // If this is a binop, find its precedence.
1223 int TokPrec = GetTokPrecedence();
1225 // If this is a binop that binds at least as tightly as the current binop,
1226 // consume it, otherwise we are done.
1227 if (TokPrec < ExprPrec)
1230 // Okay, we know this is a binop.
1232 getNextToken(); // eat binop
1234 // Parse the unary expression after the binary operator.
1235 ExprAST *RHS = ParseUnary();
1238 // If BinOp binds less tightly with RHS than the operator after RHS, let
1239 // the pending operator take RHS as its LHS.
1240 int NextPrec = GetTokPrecedence();
1241 if (TokPrec < NextPrec) {
1242 RHS = ParseBinOpRHS(TokPrec+1, RHS);
1243 if (RHS == 0) return 0;
1247 LHS = new BinaryExprAST(BinOp, LHS, RHS);
1252 /// ::= unary binoprhs
1254 static ExprAST *ParseExpression() {
1255 ExprAST *LHS = ParseUnary();
1258 return ParseBinOpRHS(0, LHS);
1262 /// ::= id '(' id* ')'
1263 /// ::= binary LETTER number? (id, id)
1264 /// ::= unary LETTER (id)
1265 static PrototypeAST *ParsePrototype() {
1268 int Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
1269 unsigned BinaryPrecedence = 30;
1273 return ErrorP("Expected function name in prototype");
1274 case tok_identifier:
1275 FnName = IdentifierStr;
1281 if (!isascii(CurTok))
1282 return ErrorP("Expected unary operator");
1284 FnName += (char)CurTok;
1290 if (!isascii(CurTok))
1291 return ErrorP("Expected binary operator");
1293 FnName += (char)CurTok;
1297 // Read the precedence if present.
1298 if (CurTok == tok_number) {
1299 if (NumVal < 1 || NumVal > 100)
1300 return ErrorP("Invalid precedecnce: must be 1..100");
1301 BinaryPrecedence = (unsigned)NumVal;
1308 return ErrorP("Expected '(' in prototype");
1310 std::vector<std::string> ArgNames;
1311 while (getNextToken() == tok_identifier)
1312 ArgNames.push_back(IdentifierStr);
1314 return ErrorP("Expected ')' in prototype");
1317 getNextToken(); // eat ')'.
1319 // Verify right number of names for operator.
1320 if (Kind && ArgNames.size() != Kind)
1321 return ErrorP("Invalid number of operands for operator");
1323 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
1326 /// definition ::= 'def' prototype expression
1327 static FunctionAST *ParseDefinition() {
1328 getNextToken(); // eat def.
1329 PrototypeAST *Proto = ParsePrototype();
1330 if (Proto == 0) return 0;
1332 if (ExprAST *E = ParseExpression())
1333 return new FunctionAST(Proto, E);
1337 /// toplevelexpr ::= expression
1338 static FunctionAST *ParseTopLevelExpr() {
1339 if (ExprAST *E = ParseExpression()) {
1340 // Make an anonymous proto.
1341 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
1342 return new FunctionAST(Proto, E);
1347 /// external ::= 'extern' prototype
1348 static PrototypeAST *ParseExtern() {
1349 getNextToken(); // eat extern.
1350 return ParsePrototype();
1353 //===----------------------------------------------------------------------===//
1355 //===----------------------------------------------------------------------===//
1357 static Module *TheModule;
1358 static LLVMFoldingBuilder Builder;
1359 static std::map<std::string, Value*> NamedValues;
1360 static FunctionPassManager *TheFPM;
1362 Value *ErrorV(const char *Str) { Error(Str); return 0; }
1364 Value *NumberExprAST::Codegen() {
1365 return ConstantFP::get(Type::DoubleTy, APFloat(Val));
1368 Value *VariableExprAST::Codegen() {
1369 // Look this variable up in the function.
1370 Value *V = NamedValues[Name];
1371 return V ? V : ErrorV("Unknown variable name");
1374 Value *UnaryExprAST::Codegen() {
1375 Value *OperandV = Operand->Codegen();
1376 if (OperandV == 0) return 0;
1378 Function *F = TheModule->getFunction(std::string("unary")+Opcode);
1380 return ErrorV("Unknown unary operator");
1382 return Builder.CreateCall(F, OperandV, "unop");
1386 Value *BinaryExprAST::Codegen() {
1387 Value *L = LHS->Codegen();
1388 Value *R = RHS->Codegen();
1389 if (L == 0 || R == 0) return 0;
1392 case '+': return Builder.CreateAdd(L, R, "addtmp");
1393 case '-': return Builder.CreateSub(L, R, "subtmp");
1394 case '*': return Builder.CreateMul(L, R, "multmp");
1396 L = Builder.CreateFCmpULT(L, R, "multmp");
1397 // Convert bool 0/1 to double 0.0 or 1.0
1398 return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
1402 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
1404 Function *F = TheModule->getFunction(std::string("binary")+Op);
1405 assert(F && "binary operator not found!");
1407 Value *Ops[] = { L, R };
1408 return Builder.CreateCall(F, Ops, Ops+2, "binop");
1411 Value *CallExprAST::Codegen() {
1412 // Look up the name in the global module table.
1413 Function *CalleeF = TheModule->getFunction(Callee);
1415 return ErrorV("Unknown function referenced");
1417 // If argument mismatch error.
1418 if (CalleeF->arg_size() != Args.size())
1419 return ErrorV("Incorrect # arguments passed");
1421 std::vector<Value*> ArgsV;
1422 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1423 ArgsV.push_back(Args[i]->Codegen());
1424 if (ArgsV.back() == 0) return 0;
1427 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
1430 Value *IfExprAST::Codegen() {
1431 Value *CondV = Cond->Codegen();
1432 if (CondV == 0) return 0;
1434 // Convert condition to a bool by comparing equal to 0.0.
1435 CondV = Builder.CreateFCmpONE(CondV,
1436 ConstantFP::get(Type::DoubleTy, APFloat(0.0)),
1439 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1441 // Create blocks for the then and else cases. Insert the 'then' block at the
1442 // end of the function.
1443 BasicBlock *ThenBB = new BasicBlock("then", TheFunction);
1444 BasicBlock *ElseBB = new BasicBlock("else");
1445 BasicBlock *MergeBB = new BasicBlock("ifcont");
1447 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1450 Builder.SetInsertPoint(ThenBB);
1452 Value *ThenV = Then->Codegen();
1453 if (ThenV == 0) return 0;
1455 Builder.CreateBr(MergeBB);
1456 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1457 ThenBB = Builder.GetInsertBlock();
1460 TheFunction->getBasicBlockList().push_back(ElseBB);
1461 Builder.SetInsertPoint(ElseBB);
1463 Value *ElseV = Else->Codegen();
1464 if (ElseV == 0) return 0;
1466 Builder.CreateBr(MergeBB);
1467 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1468 ElseBB = Builder.GetInsertBlock();
1470 // Emit merge block.
1471 TheFunction->getBasicBlockList().push_back(MergeBB);
1472 Builder.SetInsertPoint(MergeBB);
1473 PHINode *PN = Builder.CreatePHI(Type::DoubleTy, "iftmp");
1475 PN->addIncoming(ThenV, ThenBB);
1476 PN->addIncoming(ElseV, ElseBB);
1480 Value *ForExprAST::Codegen() {
1483 // start = startexpr
1486 // variable = phi [start, loopheader], [nextvariable, loopend]
1492 // nextvariable = variable + step
1493 // endcond = endexpr
1494 // br endcond, loop, endloop
1497 // Emit the start code first, without 'variable' in scope.
1498 Value *StartVal = Start->Codegen();
1499 if (StartVal == 0) return 0;
1501 // Make the new basic block for the loop header, inserting after current
1503 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1504 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
1505 BasicBlock *LoopBB = new BasicBlock("loop", TheFunction);
1507 // Insert an explicit fall through from the current block to the LoopBB.
1508 Builder.CreateBr(LoopBB);
1510 // Start insertion in LoopBB.
1511 Builder.SetInsertPoint(LoopBB);
1513 // Start the PHI node with an entry for Start.
1514 PHINode *Variable = Builder.CreatePHI(Type::DoubleTy, VarName.c_str());
1515 Variable->addIncoming(StartVal, PreheaderBB);
1517 // Within the loop, the variable is defined equal to the PHI node. If it
1518 // shadows an existing variable, we have to restore it, so save it now.
1519 Value *OldVal = NamedValues[VarName];
1520 NamedValues[VarName] = Variable;
1522 // Emit the body of the loop. This, like any other expr, can change the
1523 // current BB. Note that we ignore the value computed by the body, but don't
1525 if (Body->Codegen() == 0)
1528 // Emit the step value.
1531 StepVal = Step->Codegen();
1532 if (StepVal == 0) return 0;
1534 // If not specified, use 1.0.
1535 StepVal = ConstantFP::get(Type::DoubleTy, APFloat(1.0));
1538 Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
1540 // Compute the end condition.
1541 Value *EndCond = End->Codegen();
1542 if (EndCond == 0) return EndCond;
1544 // Convert condition to a bool by comparing equal to 0.0.
1545 EndCond = Builder.CreateFCmpONE(EndCond,
1546 ConstantFP::get(Type::DoubleTy, APFloat(0.0)),
1549 // Create the "after loop" block and insert it.
1550 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
1551 BasicBlock *AfterBB = new BasicBlock("afterloop", TheFunction);
1553 // Insert the conditional branch into the end of LoopEndBB.
1554 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1556 // Any new code will be inserted in AfterBB.
1557 Builder.SetInsertPoint(AfterBB);
1559 // Add a new entry to the PHI node for the backedge.
1560 Variable->addIncoming(NextVar, LoopEndBB);
1562 // Restore the unshadowed variable.
1564 NamedValues[VarName] = OldVal;
1566 NamedValues.erase(VarName);
1569 // for expr always returns 0.0.
1570 return Constant::getNullValue(Type::DoubleTy);
1573 Function *PrototypeAST::Codegen() {
1574 // Make the function type: double(double,double) etc.
1575 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
1576 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
1578 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
1580 // If F conflicted, there was already something named 'Name'. If it has a
1581 // body, don't allow redefinition or reextern.
1582 if (F->getName() != Name) {
1583 // Delete the one we just made and get the existing one.
1584 F->eraseFromParent();
1585 F = TheModule->getFunction(Name);
1587 // If F already has a body, reject this.
1588 if (!F->empty()) {
1589 ErrorF("redefinition of function");
1593 // If F took a different number of args, reject.
1594 if (F->arg_size() != Args.size()) {
1595 ErrorF("redefinition of function with different # args");
1600 // Set names for all arguments.
1602 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1604 AI->setName(Args[Idx]);
1606 // Add arguments to variable symbol table.
1607 NamedValues[Args[Idx]] = AI;
1613 Function *FunctionAST::Codegen() {
1614 NamedValues.clear();
1616 Function *TheFunction = Proto->Codegen();
1617 if (TheFunction == 0)
1620 // If this is an operator, install it.
1621 if (Proto->isBinaryOp())
1622 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
1624 // Create a new basic block to start insertion into.
1625 BasicBlock *BB = new BasicBlock("entry", TheFunction);
1626 Builder.SetInsertPoint(BB);
1628 if (Value *RetVal = Body->Codegen()) {
1629 // Finish off the function.
1630 Builder.CreateRet(RetVal);
1632 // Validate the generated code, checking for consistency.
1633 verifyFunction(*TheFunction);
1635 // Optimize the function.
1636 TheFPM->run(*TheFunction);
1641 // Error reading body, remove function.
1642 TheFunction->eraseFromParent();
1644 if (Proto->isBinaryOp())
1645 BinopPrecedence.erase(Proto->getOperatorName());
1649 //===----------------------------------------------------------------------===//
1650 // Top-Level parsing and JIT Driver
1651 //===----------------------------------------------------------------------===//
1653 static ExecutionEngine *TheExecutionEngine;
1655 static void HandleDefinition() {
1656 if (FunctionAST *F = ParseDefinition()) {
1657 if (Function *LF = F->Codegen()) {
1658 fprintf(stderr, "Read function definition:");
1662 // Skip token for error recovery.
1667 static void HandleExtern() {
1668 if (PrototypeAST *P = ParseExtern()) {
1669 if (Function *F = P->Codegen()) {
1670 fprintf(stderr, "Read extern: ");
1674 // Skip token for error recovery.
1679 static void HandleTopLevelExpression() {
1680 // Evaluate a top level expression into an anonymous function.
1681 if (FunctionAST *F = ParseTopLevelExpr()) {
1682 if (Function *LF = F->Codegen()) {
1683 // JIT the function, returning a function pointer.
1684 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
1686 // Cast it to the right type (takes no arguments, returns a double) so we
1687 // can call it as a native function.
1688 double (*FP)() = (double (*)())FPtr;
1689 fprintf(stderr, "Evaluated to %f\n", FP());
1692 // Skip token for error recovery.
1697 /// top ::= definition | external | expression | ';'
1698 static void MainLoop() {
1700 fprintf(stderr, "ready> ");
1702 case tok_eof: return;
1703 case ';': getNextToken(); break; // ignore top level semicolons.
1704 case tok_def: HandleDefinition(); break;
1705 case tok_extern: HandleExtern(); break;
1706 default: HandleTopLevelExpression(); break;
1713 //===----------------------------------------------------------------------===//
1714 // "Library" functions that can be "extern'd" from user code.
1715 //===----------------------------------------------------------------------===//
1717 /// putchard - putchar that takes a double and returns 0.
1719 double putchard(double X) {
1724 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1726 double printd(double X) {
1731 //===----------------------------------------------------------------------===//
1732 // Main driver code.
1733 //===----------------------------------------------------------------------===//
1736 // Install standard binary operators.
1737 // 1 is lowest precedence.
1738 BinopPrecedence['<'] = 10;
1739 BinopPrecedence['+'] = 20;
1740 BinopPrecedence['-'] = 20;
1741 BinopPrecedence['*'] = 40; // highest.
1743 // Prime the first token.
1744 fprintf(stderr, "ready> ");
1747 // Make the module, which holds all the code.
1748 TheModule = new Module("my cool jit");
1751 TheExecutionEngine = ExecutionEngine::create(TheModule);
1754 ExistingModuleProvider OurModuleProvider(TheModule);
1755 FunctionPassManager OurFPM(&OurModuleProvider);
1757 // Set up the optimizer pipeline. Start with registering info about how the
1758 // target lays out data structures.
1759 OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
1760 // Do simple "peephole" optimizations and bit-twiddling optzns.
1761 OurFPM.add(createInstructionCombiningPass());
1762 // Reassociate expressions.
1763 OurFPM.add(createReassociatePass());
1764 // Eliminate Common SubExpressions.
1765 OurFPM.add(createGVNPass());
1766 // Simplify the control flow graph (deleting unreachable blocks, etc).
1767 OurFPM.add(createCFGSimplificationPass());
1768 // Set the global so the code gen can use this.
1769 TheFPM = &OurFPM;
1771 // Run the main "interpreter loop" now.
1775 } // Free module provider and pass manager.
1778 // Print out all of the generated code.
1787 <!-- *********************************************************************** -->
1790 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1791 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1792 <a href="http://validator.w3.org/check/referer"><img
1793 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1795 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1796 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
1797 Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $