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>
16 <div class="doc_author">
17 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
20 <!-- *********************************************************************** -->
21 <div class="doc_section"><a name="intro">Part 6 Introduction</a></div>
22 <!-- *********************************************************************** -->
24 <div class="doc_text">
26 <p>Welcome to Part 6 of the "<a href="index.html">Implementing a language with
27 LLVM</a>" tutorial. At this point in our tutorial, we now have a fully
28 functional language that is fairly minimal, but also useful. One big problem
29 with it though is that it doesn't have many useful operators (like division,
30 logical negation, or even any comparisons other than less-than.</p>
32 <p>This chapter of the tutorial takes a wild digression into adding user-defined
33 operators to the simple and beautiful Kaleidoscope language, giving us a
34 simple and ugly language in some ways, but also a powerful one at the same time.
35 One of the great things about creating your own language is that you get to
36 decide what is good or bad. In this tutorial we'll assume that it is okay and
37 use this as a way to show some interesting parsing techniques.</p>
39 <p>At the end of this tutorial, we'll <a href="#example">run through a nice
40 little example</a> that shows an example application that you can build with
41 Kaleidoscope and the feature set it now has.</p>
45 <!-- *********************************************************************** -->
46 <div class="doc_section"><a name="idea">User-defined Operators: the Idea</a></div>
47 <!-- *********************************************************************** -->
49 <div class="doc_text">
52 The "operator overloading" that we will add to Kaleidoscope is more general than
53 languages like C++. In C++, you are only allowed to redefine existing
54 operators: you can't programatically change the grammar, introduce new
55 operators, change precedence levels, etc. In this chapter, we will add this
56 capability to Kaleidoscope, which will allow us to round out the set of
57 operators that are supported, culminating in a more interesting example app.</p>
59 <p>The point of going into user-defined operators in a tutorial like this is to
60 show the power and flexibility of using a hand-written parser. The parser we
61 are using so far is using recursive descent for most parts of the grammar, and
62 operator precedence parsing for the expressions. See <a
63 href="LangImpl2.html">Chapter 2</a> for details. Without using operator
64 precedence parsing, it would be very difficult to allow the programmer to
65 introduce new operators into the grammar: the grammar is dynamically extensible
68 <p>The two specific features we'll add are programmable unary operators (right
69 now, Kaleidoscope has no unary operators at all) as well as binary operators.
70 An example of this is:</p>
72 <div class="doc_code">
81 # Define > with the same precedence as <.
82 def binary> 10 (LHS RHS)
83 !(LHS < RHS); # alternatively, could just use "RHS < LHS"
85 # Binary "logical or", (note that it does not "short circuit")
86 def binary| 5 (LHS RHS)
94 # Define = with slightly lower precedence than relationals.
95 def binary= 9 (LHS RHS)
96 !(LHS < RHS | LHS > RHS);
100 <p>Many languages aspire to being able to implement their standard runtime
101 library in the language itself. In Kaleidoscope, we can implement significant
102 parts of the language in the library!</p>
104 <p>We will break down implementation of these features into two parts:
105 implementing support for user-defined binary operators and adding unary
110 <!-- *********************************************************************** -->
111 <div class="doc_section"><a name="binary">User-defined Binary Operators</a></div>
112 <!-- *********************************************************************** -->
114 <div class="doc_text">
116 <p>Adding support for user-defined binary operators is pretty simple with our
117 current framework. We'll first add support for the unary/binary keywords:</p>
119 <div class="doc_code">
124 tok_binary = -11, tok_unary = -12</b>
127 static int gettok() {
129 if (IdentifierStr == "for") return tok_for;
130 if (IdentifierStr == "in") return tok_in;
131 <b>if (IdentifierStr == "binary") return tok_binary;
132 if (IdentifierStr == "unary") return tok_unary;</b>
133 return tok_identifier;
137 <p>This just adds lexer support for the unary and binary keywords, like we
138 did in <a href="LangImpl5.html#iflexer">previous chapters</a>. One nice thing
139 about our current AST is that we represent binary operators fully generally
140 with their ASCII code as the opcode. For our extended operators, we'll use the
141 same representation, so we don't need any new AST or parser support.</p>
143 <p>On the other hand, we have to be able to represent the definitions of these
144 new operators, in the "def binary| 5" part of the function definition. In the
145 grammar so far, the "name" for the function definition is parsed as the
146 "prototype" production and into the <tt>PrototypeAST</tt> AST node. To
147 represent our new user-defined operators as prototypes, we have to extend
148 the <tt>PrototypeAST</tt> AST node like this:</p>
150 <div class="doc_code">
152 /// PrototypeAST - This class represents the "prototype" for a function,
153 /// which captures its argument names as well as if it is an operator.
156 std::vector<std::string> Args;
158 unsigned Precedence; // Precedence if a binary op.</b>
160 PrototypeAST(const std::string &name, const std::vector<std::string> &args,
161 <b>bool isoperator = false, unsigned prec = 0</b>)
162 : Name(name), Args(args), <b>isOperator(isoperator), Precedence(prec)</b> {}
164 <b>bool isUnaryOp() const { return isOperator && Args.size() == 1; }
165 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
167 char getOperatorName() const {
168 assert(isUnaryOp() || isBinaryOp());
169 return Name[Name.size()-1];
172 unsigned getBinaryPrecedence() const { return Precedence; }</b>
179 <p>Basically, in addition to knowing a name for the prototype, we now keep track
180 of whether it was an operator, and if it was, what precedence level the operator
181 is at. The precedence is only used for binary operators (as you'll see below,
182 it just doesn't apply for unary operators). Now that we have a way to represent
183 the prototype for a user-defined operator, we need to parse it:</p>
185 <div class="doc_code">
188 /// ::= id '(' id* ')'
189 <b>/// ::= binary LETTER number? (id, id)</b>
190 static PrototypeAST *ParsePrototype() {
193 <b>int Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
194 unsigned BinaryPrecedence = 30;</b>
198 return ErrorP("Expected function name in prototype");
200 FnName = IdentifierStr;
206 if (!isascii(CurTok))
207 return ErrorP("Expected binary operator");
209 FnName += (char)CurTok;
213 // Read the precedence if present.
214 if (CurTok == tok_number) {
215 if (NumVal < 1 || NumVal > 100)
216 return ErrorP("Invalid precedecnce: must be 1..100");
217 BinaryPrecedence = (unsigned)NumVal;
224 return ErrorP("Expected '(' in prototype");
226 std::vector<std::string> ArgNames;
227 while (getNextToken() == tok_identifier)
228 ArgNames.push_back(IdentifierStr);
230 return ErrorP("Expected ')' in prototype");
233 getNextToken(); // eat ')'.
235 <b>// Verify right number of names for operator.
236 if (Kind && ArgNames.size() != Kind)
237 return ErrorP("Invalid number of operands for operator");
239 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);</b>
244 <p>This is all fairly straight-forward parsing code, and we have already seen
245 a lot of similar code in the past. One interesting piece of this is the part
246 that sets up <tt>FnName</tt> for binary operators. This builds names like
247 "binary@" for a newly defined "@" operator. This takes advantage of the fact
248 that symbol names in the LLVM symbol table are allowed to have any character in
249 them, inluding embedded nul characters.</p>
251 <p>The next interesting piece is codegen support for these binary operators.
252 Given our current structure, this is a simple addition of a default case for our
253 existing binary operator node:</p>
255 <div class="doc_code">
257 Value *BinaryExprAST::Codegen() {
258 Value *L = LHS->Codegen();
259 Value *R = RHS->Codegen();
260 if (L == 0 || R == 0) return 0;
263 case '+': return Builder.CreateAdd(L, R, "addtmp");
264 case '-': return Builder.CreateSub(L, R, "subtmp");
265 case '*': return Builder.CreateMul(L, R, "multmp");
267 L = Builder.CreateFCmpULT(L, R, "multmp");
268 // Convert bool 0/1 to double 0.0 or 1.0
269 return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
270 <b>default: break;</b>
273 <b>// If it wasn't a builtin binary operator, it must be a user defined one. Emit
275 Function *F = TheModule->getFunction(std::string("binary")+Op);
276 assert(F && "binary operator not found!");
278 Value *Ops[] = { L, R };
279 return Builder.CreateCall(F, Ops, Ops+2, "binop");</b>
285 <p>As you can see above, the new code is actually really simple. It just does
286 a lookup for the appropriate operator in the symbol table and generates a
287 function call to it. Since user-defined operators are just built as normal
288 functions (because the "prototype" boils down into a function with the right
289 name) everything falls into place.</p>
291 <p>The final missing piece is a bit of top level magic, here:</p>
293 <div class="doc_code">
295 Function *FunctionAST::Codegen() {
298 Function *TheFunction = Proto->Codegen();
299 if (TheFunction == 0)
302 <b>// If this is an operator, install it.
303 if (Proto->isBinaryOp())
304 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();</b>
306 // Create a new basic block to start insertion into.
307 BasicBlock *BB = new BasicBlock("entry", TheFunction);
308 Builder.SetInsertPoint(BB);
310 if (Value *RetVal = Body->Codegen()) {
315 <p>Basically, before codegening a function, if it is a user-defined operator, we
316 register it in the precedence table. This allows the binary operator parsing
317 logic we already have to handle it. Since it is a fully-general operator
318 precedence parser, this is all we need to do to "extend the grammar".</p>
320 <p>With that, we have useful user-defined binary operators. This builds a lot
321 on the previous framework we built for other operators. Adding unary operators
322 is a bit more challenging, because we don't have any framework for it yet, lets
323 see what it takes.</p>
327 <!-- *********************************************************************** -->
328 <div class="doc_section"><a name="unary">User-defined Unary Operators</a></div>
329 <!-- *********************************************************************** -->
331 <div class="doc_text">
333 <p>Since we don't currently support unary operators in the Kaleidoscope
334 langugage, we'll need to add everything for them. Above, we added simple
335 support for the 'unary' keyword to the lexer. In addition to that, we need an
338 <div class="doc_code">
340 /// UnaryExprAST - Expression class for a unary operator.
341 class UnaryExprAST : public ExprAST {
345 UnaryExprAST(char opcode, ExprAST *operand)
346 : Opcode(opcode), Operand(operand) {}
347 virtual Value *Codegen();
352 <p>This AST node is very simple and obvious by now. It directly mirrors the
353 binary operator AST node, except that it only has one child. With this, we
354 need to add the parsing logic. Parsing a unary operator is pretty simple: we'll
355 add a new function to do it:</p>
357 <div class="doc_code">
362 static ExprAST *ParseUnary() {
363 // If the current token is not an operator, it must be a primary expr.
364 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
365 return ParsePrimary();
367 // If this is a unary operator, read it.
370 if (ExprAST *Operand = ParseUnary())
371 return new UnaryExprAST(Opc, Operand);
377 <p>The grammar we add is pretty straight-forward here. If we see a unary
378 operator when parsing a primary operator, we eat the operator as a prefix and
379 parse the remaining piece as another unary operator. This allows us to handle
380 multiple unary operators (e.g. "!!x"). Note that unary operators can't have
381 ambiguous parses like binary operators can, so there is no need for precedence
384 <p>The problem with the above is that we need to call ParseUnary from somewhere.
385 To do this, we change previous callers of ParsePrimary to call ParseUnary
388 <div class="doc_code">
392 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
394 <b>// Parse the unary expression after the binary operator.
395 ExprAST *RHS = ParseUnary();
396 if (!RHS) return 0;</b>
400 /// ::= unary binoprhs
402 static ExprAST *ParseExpression() {
403 <b>ExprAST *LHS = ParseUnary();</b>
406 return ParseBinOpRHS(0, LHS);
411 <p>With these two simple changes, we now parse unary operators and build the
412 AST for them. Next up, we need to add parser support for prototypes, to parse
413 the unary operator prototype. We extend the binary operator code above
416 <div class="doc_code">
419 /// ::= id '(' id* ')'
420 /// ::= binary LETTER number? (id, id)
421 <b>/// ::= unary LETTER (id)</b>
422 static PrototypeAST *ParsePrototype() {
425 int Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
426 unsigned BinaryPrecedence = 30;
430 return ErrorP("Expected function name in prototype");
432 FnName = IdentifierStr;
438 if (!isascii(CurTok))
439 return ErrorP("Expected unary operator");
441 FnName += (char)CurTok;
450 <p>As with binary operators, we name unary operators with a name that includes
451 the operator character. This assists us at code generation time. Speaking of,
452 the final piece we need to add is codegen support for unary operators. It looks
455 <div class="doc_code">
457 Value *UnaryExprAST::Codegen() {
458 Value *OperandV = Operand->Codegen();
459 if (OperandV == 0) return 0;
461 Function *F = TheModule->getFunction(std::string("unary")+Opcode);
463 return ErrorV("Unknown unary operator");
465 return Builder.CreateCall(F, OperandV, "unop");
470 <p>This code is similar to, but simpler than, the code for binary operators. It
471 is simpler primarily because it doesn't need to handle any predefined operators.
476 <!-- *********************************************************************** -->
477 <div class="doc_section"><a name="example">Kicking the Tires</a></div>
478 <!-- *********************************************************************** -->
480 <div class="doc_text">
482 <p>It is somewhat hard to believe, but with a few simple extensions we've
483 covered in the last chapters, we have grown a real-ish language. With this, we
484 can do a lot of interesting things, including I/O, math, and a bunch of other
485 things. For example, we can now add a nice sequencing operator (printd is
486 defined to print out the specified value and a newline):</p>
488 <div class="doc_code">
490 ready> <b>extern printd(x);</b>
491 Read extern: declare double @printd(double)
492 ready> <b>def binary : 1 (x y) 0; # Low-precedence operator that ignores operands.</b>
494 ready> <b>printd(123) : printd(456) : printd(789);</b>
498 Evaluated to 0.000000
502 <p>We can also define a bunch of other "primitive" operations, such as:</p>
504 <div class="doc_code">
517 # Define > with the same precedence as >. We could also easily define
519 def binary> 10 (LHS RHS)
522 # Binary logical or, which does not short circuit.
523 def binary| 5 (LHS RHS)
531 # Binary logical and, which does not short circuit.
532 def binary& 6 (LHS RHS)
538 # Define = with slightly lower precedence than relationals.
539 def binary = 9 (LHS RHS)
540 !(LHS < RHS | LHS > RHS);
546 <p>Given the previous if/then/else support, we can also define interesting
547 functions for I/O. For example, the following prints out a character whose
548 "density" reflects the value passed in: the lower the value, the denser the
551 <div class="doc_code">
555 extern putchard(char)
559 else if d > 4 then
561 else if d > 2 then
564 putchard(42); # '*'</b>
566 ready> <b>printdensity(1): printdensity(2): printdensity(3) :
567 printdensity(4): printdensity(5): printdensity(9): putchard(10);</b>
569 Evaluated to 0.000000
573 <p>Based on these simple primitive operations, we can start to define more
574 interesting things. For example, here's a little function that solves for the
575 number of iterations it takes for a function in the complex plane to
578 <div class="doc_code">
580 # determine whether the specific location diverges.
581 # Solve for z = z^2 + c in the complex plane.
582 def mandleconverger(real imag iters creal cimag)
583 if iters > 255 | (real*real + imag*imag > 4) then
586 mandleconverger(real*real - imag*imag + creal,
588 iters+1, creal, cimag);
590 # return the number of iterations required for the iteration to escape
591 def mandleconverge(real imag)
592 mandleconverger(real, imag, 0, real, imag);
596 <p>This "z = z<sup>2</sup> + c" function is a beautiful little creature that is the basis
597 for computation of the <a
598 href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot Set</a>. Our
599 <tt>mandelconverge</tt> function returns the number of iterations that it takes
600 for a complex orbit to escape, saturating to 255. This is not a very useful
601 function by itself, but if you plot its value over a two-dimensional plane,
602 you can see the mandelbrot set. Given that we are limited to using putchard
603 here, our amazing graphical output is limited, but we can whip together
604 something using the density plotter above:</p>
606 <div class="doc_code">
608 # compute and plot the mandlebrot set with the specified 2 dimentional range
610 def mandelhelp(xmin xmax xstep ymin ymax ystep)
611 for y = ymin, y < ymax, ystep in (
612 (for x = xmin, x < xmax, xstep in
613 printdensity(mandleconverge(x,y)))
617 # mandel - This is a convenient helper function for ploting the mandelbrot set
618 # from the specified position with the specified magnification.
619 def mandel(realstart imagstart realmag imagmag)
620 mandelhelp(realstart, realstart+realmag*78, realmag,
621 imagstart, imagstart+imagmag*40, imagmag);
625 <p>Given this, we can try plotting out the mandlebrot set! Lets try it out:</p>
627 <div class="doc_code">
629 ready> <b>mandel(-2.3, -1.3, 0.05, 0.07);</b>
630 *******************************+++++++++++*************************************
631 *************************+++++++++++++++++++++++*******************************
632 **********************+++++++++++++++++++++++++++++****************************
633 *******************+++++++++++++++++++++.. ...++++++++*************************
634 *****************++++++++++++++++++++++.... ...+++++++++***********************
635 ***************+++++++++++++++++++++++..... ...+++++++++*********************
636 **************+++++++++++++++++++++++.... ....+++++++++********************
637 *************++++++++++++++++++++++...... .....++++++++*******************
638 ************+++++++++++++++++++++....... .......+++++++******************
639 ***********+++++++++++++++++++.... ... .+++++++*****************
640 **********+++++++++++++++++....... .+++++++****************
641 *********++++++++++++++........... ...+++++++***************
642 ********++++++++++++............ ...++++++++**************
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 Evaluated to 0.000000
672 ready> <b>mandel(-2, -1, 0.02, 0.04);</b>
673 **************************+++++++++++++++++++++++++++++++++++++++++++++++++++++
674 ***********************++++++++++++++++++++++++++++++++++++++++++++++++++++++++
675 *********************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
676 *******************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++...
677 *****************+++++++++++++++++++++++++++++++++++++++++++++++++++++++++.....
678 ***************++++++++++++++++++++++++++++++++++++++++++++++++++++++++........
679 **************++++++++++++++++++++++++++++++++++++++++++++++++++++++...........
680 ************+++++++++++++++++++++++++++++++++++++++++++++++++++++..............
681 ***********++++++++++++++++++++++++++++++++++++++++++++++++++........ .
682 **********++++++++++++++++++++++++++++++++++++++++++++++.............
683 ********+++++++++++++++++++++++++++++++++++++++++++..................
684 *******+++++++++++++++++++++++++++++++++++++++.......................
685 ******+++++++++++++++++++++++++++++++++++...........................
686 *****++++++++++++++++++++++++++++++++............................
687 *****++++++++++++++++++++++++++++...............................
688 ****++++++++++++++++++++++++++...... .........................
689 ***++++++++++++++++++++++++......... ...... ...........
690 ***++++++++++++++++++++++............
691 **+++++++++++++++++++++..............
692 **+++++++++++++++++++................
693 *++++++++++++++++++.................
694 *++++++++++++++++............ ...
695 *++++++++++++++..............
696 *+++....++++................
697 *.......... ...........
699 *.......... ...........
700 *+++....++++................
701 *++++++++++++++..............
702 *++++++++++++++++............ ...
703 *++++++++++++++++++.................
704 **+++++++++++++++++++................
705 **+++++++++++++++++++++..............
706 ***++++++++++++++++++++++............
707 ***++++++++++++++++++++++++......... ...... ...........
708 ****++++++++++++++++++++++++++...... .........................
709 *****++++++++++++++++++++++++++++...............................
710 *****++++++++++++++++++++++++++++++++............................
711 ******+++++++++++++++++++++++++++++++++++...........................
712 *******+++++++++++++++++++++++++++++++++++++++.......................
713 ********+++++++++++++++++++++++++++++++++++++++++++..................
714 Evaluated to 0.000000
715 ready> <b>mandel(-0.9, -1.4, 0.02, 0.03);</b>
716 *******************************************************************************
717 *******************************************************************************
718 *******************************************************************************
719 **********+++++++++++++++++++++************************************************
720 *+++++++++++++++++++++++++++++++++++++++***************************************
721 +++++++++++++++++++++++++++++++++++++++++++++**********************************
722 ++++++++++++++++++++++++++++++++++++++++++++++++++*****************************
723 ++++++++++++++++++++++++++++++++++++++++++++++++++++++*************************
724 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++**********************
725 +++++++++++++++++++++++++++++++++.........++++++++++++++++++*******************
726 +++++++++++++++++++++++++++++++.... ......+++++++++++++++++++****************
727 +++++++++++++++++++++++++++++....... ........+++++++++++++++++++**************
728 ++++++++++++++++++++++++++++........ ........++++++++++++++++++++************
729 +++++++++++++++++++++++++++......... .. ...+++++++++++++++++++++**********
730 ++++++++++++++++++++++++++........... ....++++++++++++++++++++++********
731 ++++++++++++++++++++++++............. .......++++++++++++++++++++++******
732 +++++++++++++++++++++++............. ........+++++++++++++++++++++++****
733 ++++++++++++++++++++++........... ..........++++++++++++++++++++++***
734 ++++++++++++++++++++........... .........++++++++++++++++++++++*
735 ++++++++++++++++++............ ...........++++++++++++++++++++
736 ++++++++++++++++............... .............++++++++++++++++++
737 ++++++++++++++................. ...............++++++++++++++++
738 ++++++++++++.................. .................++++++++++++++
739 +++++++++.................. .................+++++++++++++
740 ++++++........ . ......... ..++++++++++++
741 ++............ ...... ....++++++++++
742 .............. ...++++++++++
743 .............. ....+++++++++
744 .............. .....++++++++
745 ............. ......++++++++
746 ........... .......++++++++
747 ......... ........+++++++
748 ......... ........+++++++
749 ......... ....+++++++
757 Evaluated to 0.000000
762 <p>At this point, you may be starting to realize that Kaleidoscope is a real
763 and powerful language. It may not be self-similar :), but it can be used to
764 plot things that are!</p>
766 <p>With this, we conclude the "adding user-defined operators" chapter of the
767 tutorial. We successfully extended our language with the ability to extend the
768 language in the library, and showed how this can be used to build a simple but
769 interesting end user application in Kaleidoscope. At this point, Kaleidoscope
770 can build a variety of applications that are functional and can call functions
771 with side-effects, but it can't actually define and mutate a variable itself.
774 <p>Strikingly, lack of this feature is an important limitation for some
775 languages, and it is not at all obvious how to <a href="LangImpl7.html">add
776 support for mutable variables</a> without having to add an "SSA construction"
777 phase to your front-end. In the next chapter, we will describe how you can
778 add this without building SSA in your front-end.</p>
783 <!-- *********************************************************************** -->
784 <div class="doc_section"><a name="code">Full Code Listing</a></div>
785 <!-- *********************************************************************** -->
787 <div class="doc_text">
790 Here is the complete code listing for our running example, enhanced with the
791 if/then/else and for expressions.. To build this example, use:
794 <div class="doc_code">
797 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
803 <p>Here is the code:</p>
805 <div class="doc_code">
807 #include "llvm/DerivedTypes.h"
808 #include "llvm/ExecutionEngine/ExecutionEngine.h"
809 #include "llvm/Module.h"
810 #include "llvm/ModuleProvider.h"
811 #include "llvm/PassManager.h"
812 #include "llvm/Analysis/Verifier.h"
813 #include "llvm/Target/TargetData.h"
814 #include "llvm/Transforms/Scalar.h"
815 #include "llvm/Support/LLVMBuilder.h"
816 #include <cstdio>
817 #include <string>
819 #include <vector>
820 using namespace llvm;
822 //===----------------------------------------------------------------------===//
824 //===----------------------------------------------------------------------===//
826 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
827 // of these for known things.
832 tok_def = -2, tok_extern = -3,
835 tok_identifier = -4, tok_number = -5,
838 tok_if = -6, tok_then = -7, tok_else = -8,
839 tok_for = -9, tok_in = -10,
842 tok_binary = -11, tok_unary = -12
845 static std::string IdentifierStr; // Filled in if tok_identifier
846 static double NumVal; // Filled in if tok_number
848 /// gettok - Return the next token from standard input.
849 static int gettok() {
850 static int LastChar = ' ';
852 // Skip any whitespace.
853 while (isspace(LastChar))
854 LastChar = getchar();
856 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
857 IdentifierStr = LastChar;
858 while (isalnum((LastChar = getchar())))
859 IdentifierStr += LastChar;
861 if (IdentifierStr == "def") return tok_def;
862 if (IdentifierStr == "extern") return tok_extern;
863 if (IdentifierStr == "if") return tok_if;
864 if (IdentifierStr == "then") return tok_then;
865 if (IdentifierStr == "else") return tok_else;
866 if (IdentifierStr == "for") return tok_for;
867 if (IdentifierStr == "in") return tok_in;
868 if (IdentifierStr == "binary") return tok_binary;
869 if (IdentifierStr == "unary") return tok_unary;
870 return tok_identifier;
873 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
877 LastChar = getchar();
878 } while (isdigit(LastChar) || LastChar == '.');
880 NumVal = strtod(NumStr.c_str(), 0);
884 if (LastChar == '#') {
885 // Comment until end of line.
886 do LastChar = getchar();
887 while (LastChar != EOF && LastChar != '\n' & LastChar != '\r');
893 // Check for end of file. Don't eat the EOF.
897 // Otherwise, just return the character as its ascii value.
898 int ThisChar = LastChar;
899 LastChar = getchar();
903 //===----------------------------------------------------------------------===//
904 // Abstract Syntax Tree (aka Parse Tree)
905 //===----------------------------------------------------------------------===//
907 /// ExprAST - Base class for all expression nodes.
910 virtual ~ExprAST() {}
911 virtual Value *Codegen() = 0;
914 /// NumberExprAST - Expression class for numeric literals like "1.0".
915 class NumberExprAST : public ExprAST {
918 NumberExprAST(double val) : Val(val) {}
919 virtual Value *Codegen();
922 /// VariableExprAST - Expression class for referencing a variable, like "a".
923 class VariableExprAST : public ExprAST {
926 VariableExprAST(const std::string &name) : Name(name) {}
927 virtual Value *Codegen();
930 /// UnaryExprAST - Expression class for a unary operator.
931 class UnaryExprAST : public ExprAST {
935 UnaryExprAST(char opcode, ExprAST *operand)
936 : Opcode(opcode), Operand(operand) {}
937 virtual Value *Codegen();
940 /// BinaryExprAST - Expression class for a binary operator.
941 class BinaryExprAST : public ExprAST {
945 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
946 : Op(op), LHS(lhs), RHS(rhs) {}
947 virtual Value *Codegen();
950 /// CallExprAST - Expression class for function calls.
951 class CallExprAST : public ExprAST {
953 std::vector<ExprAST*> Args;
955 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
956 : Callee(callee), Args(args) {}
957 virtual Value *Codegen();
960 /// IfExprAST - Expression class for if/then/else.
961 class IfExprAST : public ExprAST {
962 ExprAST *Cond, *Then, *Else;
964 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
965 : Cond(cond), Then(then), Else(_else) {}
966 virtual Value *Codegen();
969 /// ForExprAST - Expression class for for/in.
970 class ForExprAST : public ExprAST {
972 ExprAST *Start, *End, *Step, *Body;
974 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
975 ExprAST *step, ExprAST *body)
976 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
977 virtual Value *Codegen();
980 /// PrototypeAST - This class represents the "prototype" for a function,
981 /// which captures its argument names as well as if it is an operator.
984 std::vector<std::string> Args;
986 unsigned Precedence; // Precedence if a binary op.
988 PrototypeAST(const std::string &name, const std::vector<std::string> &args,
989 bool isoperator = false, unsigned prec = 0)
990 : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
992 bool isUnaryOp() const { return isOperator && Args.size() == 1; }
993 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
995 char getOperatorName() const {
996 assert(isUnaryOp() || isBinaryOp());
997 return Name[Name.size()-1];
1000 unsigned getBinaryPrecedence() const { return Precedence; }
1002 Function *Codegen();
1005 /// FunctionAST - This class represents a function definition itself.
1007 PrototypeAST *Proto;
1010 FunctionAST(PrototypeAST *proto, ExprAST *body)
1011 : Proto(proto), Body(body) {}
1013 Function *Codegen();
1016 //===----------------------------------------------------------------------===//
1018 //===----------------------------------------------------------------------===//
1020 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
1021 /// token the parser it looking at. getNextToken reads another token from the
1022 /// lexer and updates CurTok with its results.
1024 static int getNextToken() {
1025 return CurTok = gettok();
1028 /// BinopPrecedence - This holds the precedence for each binary operator that is
1030 static std::map<char, int> BinopPrecedence;
1032 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
1033 static int GetTokPrecedence() {
1034 if (!isascii(CurTok))
1037 // Make sure it's a declared binop.
1038 int TokPrec = BinopPrecedence[CurTok];
1039 if (TokPrec <= 0) return -1;
1043 /// Error* - These are little helper functions for error handling.
1044 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
1045 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
1046 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
1048 static ExprAST *ParseExpression();
1052 /// ::= identifer '(' expression* ')'
1053 static ExprAST *ParseIdentifierExpr() {
1054 std::string IdName = IdentifierStr;
1056 getNextToken(); // eat identifer.
1058 if (CurTok != '(') // Simple variable ref.
1059 return new VariableExprAST(IdName);
1062 getNextToken(); // eat (
1063 std::vector<ExprAST*> Args;
1064 if (CurTok != ')') {
1066 ExprAST *Arg = ParseExpression();
1068 Args.push_back(Arg);
1070 if (CurTok == ')') break;
1073 return Error("Expected ')'");
1081 return new CallExprAST(IdName, Args);
1084 /// numberexpr ::= number
1085 static ExprAST *ParseNumberExpr() {
1086 ExprAST *Result = new NumberExprAST(NumVal);
1087 getNextToken(); // consume the number
1091 /// parenexpr ::= '(' expression ')'
1092 static ExprAST *ParseParenExpr() {
1093 getNextToken(); // eat (.
1094 ExprAST *V = ParseExpression();
1098 return Error("expected ')'");
1099 getNextToken(); // eat ).
1103 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
1104 static ExprAST *ParseIfExpr() {
1105 getNextToken(); // eat the if.
1108 ExprAST *Cond = ParseExpression();
1109 if (!Cond) return 0;
1111 if (CurTok != tok_then)
1112 return Error("expected then");
1113 getNextToken(); // eat the then
1115 ExprAST *Then = ParseExpression();
1116 if (Then == 0) return 0;
1118 if (CurTok != tok_else)
1119 return Error("expected else");
1123 ExprAST *Else = ParseExpression();
1124 if (!Else) return 0;
1126 return new IfExprAST(Cond, Then, Else);
1129 /// forexpr ::= 'for' identifer '=' expr ',' expr (',' expr)? 'in' expression
1130 static ExprAST *ParseForExpr() {
1131 getNextToken(); // eat the for.
1133 if (CurTok != tok_identifier)
1134 return Error("expected identifier after for");
1136 std::string IdName = IdentifierStr;
1137 getNextToken(); // eat identifer.
1140 return Error("expected '=' after for");
1141 getNextToken(); // eat '='.
1144 ExprAST *Start = ParseExpression();
1145 if (Start == 0) return 0;
1147 return Error("expected ',' after for start value");
1150 ExprAST *End = ParseExpression();
1151 if (End == 0) return 0;
1153 // The step value is optional.
1155 if (CurTok == ',') {
1157 Step = ParseExpression();
1158 if (Step == 0) return 0;
1161 if (CurTok != tok_in)
1162 return Error("expected 'in' after for");
1163 getNextToken(); // eat 'in'.
1165 ExprAST *Body = ParseExpression();
1166 if (Body == 0) return 0;
1168 return new ForExprAST(IdName, Start, End, Step, Body);
1173 /// ::= identifierexpr
1178 static ExprAST *ParsePrimary() {
1180 default: return Error("unknown token when expecting an expression");
1181 case tok_identifier: return ParseIdentifierExpr();
1182 case tok_number: return ParseNumberExpr();
1183 case '(': return ParseParenExpr();
1184 case tok_if: return ParseIfExpr();
1185 case tok_for: return ParseForExpr();
1192 static ExprAST *ParseUnary() {
1193 // If the current token is not an operator, it must be a primary expr.
1194 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
1195 return ParsePrimary();
1197 // If this is a unary operator, read it.
1200 if (ExprAST *Operand = ParseUnary())
1201 return new UnaryExprAST(Opc, Operand);
1206 /// ::= ('+' unary)*
1207 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
1208 // If this is a binop, find its precedence.
1210 int TokPrec = GetTokPrecedence();
1212 // If this is a binop that binds at least as tightly as the current binop,
1213 // consume it, otherwise we are done.
1214 if (TokPrec < ExprPrec)
1217 // Okay, we know this is a binop.
1219 getNextToken(); // eat binop
1221 // Parse the unary expression after the binary operator.
1222 ExprAST *RHS = ParseUnary();
1225 // If BinOp binds less tightly with RHS than the operator after RHS, let
1226 // the pending operator take RHS as its LHS.
1227 int NextPrec = GetTokPrecedence();
1228 if (TokPrec < NextPrec) {
1229 RHS = ParseBinOpRHS(TokPrec+1, RHS);
1230 if (RHS == 0) return 0;
1234 LHS = new BinaryExprAST(BinOp, LHS, RHS);
1239 /// ::= unary binoprhs
1241 static ExprAST *ParseExpression() {
1242 ExprAST *LHS = ParseUnary();
1245 return ParseBinOpRHS(0, LHS);
1249 /// ::= id '(' id* ')'
1250 /// ::= binary LETTER number? (id, id)
1251 /// ::= unary LETTER (id)
1252 static PrototypeAST *ParsePrototype() {
1255 int Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
1256 unsigned BinaryPrecedence = 30;
1260 return ErrorP("Expected function name in prototype");
1261 case tok_identifier:
1262 FnName = IdentifierStr;
1268 if (!isascii(CurTok))
1269 return ErrorP("Expected unary operator");
1271 FnName += (char)CurTok;
1277 if (!isascii(CurTok))
1278 return ErrorP("Expected binary operator");
1280 FnName += (char)CurTok;
1284 // Read the precedence if present.
1285 if (CurTok == tok_number) {
1286 if (NumVal < 1 || NumVal > 100)
1287 return ErrorP("Invalid precedecnce: must be 1..100");
1288 BinaryPrecedence = (unsigned)NumVal;
1295 return ErrorP("Expected '(' in prototype");
1297 std::vector<std::string> ArgNames;
1298 while (getNextToken() == tok_identifier)
1299 ArgNames.push_back(IdentifierStr);
1301 return ErrorP("Expected ')' in prototype");
1304 getNextToken(); // eat ')'.
1306 // Verify right number of names for operator.
1307 if (Kind && ArgNames.size() != Kind)
1308 return ErrorP("Invalid number of operands for operator");
1310 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
1313 /// definition ::= 'def' prototype expression
1314 static FunctionAST *ParseDefinition() {
1315 getNextToken(); // eat def.
1316 PrototypeAST *Proto = ParsePrototype();
1317 if (Proto == 0) return 0;
1319 if (ExprAST *E = ParseExpression())
1320 return new FunctionAST(Proto, E);
1324 /// toplevelexpr ::= expression
1325 static FunctionAST *ParseTopLevelExpr() {
1326 if (ExprAST *E = ParseExpression()) {
1327 // Make an anonymous proto.
1328 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
1329 return new FunctionAST(Proto, E);
1334 /// external ::= 'extern' prototype
1335 static PrototypeAST *ParseExtern() {
1336 getNextToken(); // eat extern.
1337 return ParsePrototype();
1340 //===----------------------------------------------------------------------===//
1342 //===----------------------------------------------------------------------===//
1344 static Module *TheModule;
1345 static LLVMFoldingBuilder Builder;
1346 static std::map<std::string, Value*> NamedValues;
1347 static FunctionPassManager *TheFPM;
1349 Value *ErrorV(const char *Str) { Error(Str); return 0; }
1351 Value *NumberExprAST::Codegen() {
1352 return ConstantFP::get(Type::DoubleTy, APFloat(Val));
1355 Value *VariableExprAST::Codegen() {
1356 // Look this variable up in the function.
1357 Value *V = NamedValues[Name];
1358 return V ? V : ErrorV("Unknown variable name");
1361 Value *UnaryExprAST::Codegen() {
1362 Value *OperandV = Operand->Codegen();
1363 if (OperandV == 0) return 0;
1365 Function *F = TheModule->getFunction(std::string("unary")+Opcode);
1367 return ErrorV("Unknown unary operator");
1369 return Builder.CreateCall(F, OperandV, "unop");
1373 Value *BinaryExprAST::Codegen() {
1374 Value *L = LHS->Codegen();
1375 Value *R = RHS->Codegen();
1376 if (L == 0 || R == 0) return 0;
1379 case '+': return Builder.CreateAdd(L, R, "addtmp");
1380 case '-': return Builder.CreateSub(L, R, "subtmp");
1381 case '*': return Builder.CreateMul(L, R, "multmp");
1383 L = Builder.CreateFCmpULT(L, R, "multmp");
1384 // Convert bool 0/1 to double 0.0 or 1.0
1385 return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
1389 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
1391 Function *F = TheModule->getFunction(std::string("binary")+Op);
1392 assert(F && "binary operator not found!");
1394 Value *Ops[] = { L, R };
1395 return Builder.CreateCall(F, Ops, Ops+2, "binop");
1398 Value *CallExprAST::Codegen() {
1399 // Look up the name in the global module table.
1400 Function *CalleeF = TheModule->getFunction(Callee);
1402 return ErrorV("Unknown function referenced");
1404 // If argument mismatch error.
1405 if (CalleeF->arg_size() != Args.size())
1406 return ErrorV("Incorrect # arguments passed");
1408 std::vector<Value*> ArgsV;
1409 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1410 ArgsV.push_back(Args[i]->Codegen());
1411 if (ArgsV.back() == 0) return 0;
1414 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
1417 Value *IfExprAST::Codegen() {
1418 Value *CondV = Cond->Codegen();
1419 if (CondV == 0) return 0;
1421 // Convert condition to a bool by comparing equal to 0.0.
1422 CondV = Builder.CreateFCmpONE(CondV,
1423 ConstantFP::get(Type::DoubleTy, APFloat(0.0)),
1426 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1428 // Create blocks for the then and else cases. Insert the 'then' block at the
1429 // end of the function.
1430 BasicBlock *ThenBB = new BasicBlock("then", TheFunction);
1431 BasicBlock *ElseBB = new BasicBlock("else");
1432 BasicBlock *MergeBB = new BasicBlock("ifcont");
1434 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1437 Builder.SetInsertPoint(ThenBB);
1439 Value *ThenV = Then->Codegen();
1440 if (ThenV == 0) return 0;
1442 Builder.CreateBr(MergeBB);
1443 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1444 ThenBB = Builder.GetInsertBlock();
1447 TheFunction->getBasicBlockList().push_back(ElseBB);
1448 Builder.SetInsertPoint(ElseBB);
1450 Value *ElseV = Else->Codegen();
1451 if (ElseV == 0) return 0;
1453 Builder.CreateBr(MergeBB);
1454 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1455 ElseBB = Builder.GetInsertBlock();
1457 // Emit merge block.
1458 TheFunction->getBasicBlockList().push_back(MergeBB);
1459 Builder.SetInsertPoint(MergeBB);
1460 PHINode *PN = Builder.CreatePHI(Type::DoubleTy, "iftmp");
1462 PN->addIncoming(ThenV, ThenBB);
1463 PN->addIncoming(ElseV, ElseBB);
1467 Value *ForExprAST::Codegen() {
1470 // start = startexpr
1473 // variable = phi [start, loopheader], [nextvariable, loopend]
1479 // nextvariable = variable + step
1480 // endcond = endexpr
1481 // br endcond, loop, endloop
1484 // Emit the start code first, without 'variable' in scope.
1485 Value *StartVal = Start->Codegen();
1486 if (StartVal == 0) return 0;
1488 // Make the new basic block for the loop header, inserting after current
1490 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1491 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
1492 BasicBlock *LoopBB = new BasicBlock("loop", TheFunction);
1494 // Insert an explicit fall through from the current block to the LoopBB.
1495 Builder.CreateBr(LoopBB);
1497 // Start insertion in LoopBB.
1498 Builder.SetInsertPoint(LoopBB);
1500 // Start the PHI node with an entry for Start.
1501 PHINode *Variable = Builder.CreatePHI(Type::DoubleTy, VarName.c_str());
1502 Variable->addIncoming(StartVal, PreheaderBB);
1504 // Within the loop, the variable is defined equal to the PHI node. If it
1505 // shadows an existing variable, we have to restore it, so save it now.
1506 Value *OldVal = NamedValues[VarName];
1507 NamedValues[VarName] = Variable;
1509 // Emit the body of the loop. This, like any other expr, can change the
1510 // current BB. Note that we ignore the value computed by the body, but don't
1512 if (Body->Codegen() == 0)
1515 // Emit the step value.
1518 StepVal = Step->Codegen();
1519 if (StepVal == 0) return 0;
1521 // If not specified, use 1.0.
1522 StepVal = ConstantFP::get(Type::DoubleTy, APFloat(1.0));
1525 Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
1527 // Compute the end condition.
1528 Value *EndCond = End->Codegen();
1529 if (EndCond == 0) return EndCond;
1531 // Convert condition to a bool by comparing equal to 0.0.
1532 EndCond = Builder.CreateFCmpONE(EndCond,
1533 ConstantFP::get(Type::DoubleTy, APFloat(0.0)),
1536 // Create the "after loop" block and insert it.
1537 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
1538 BasicBlock *AfterBB = new BasicBlock("afterloop", TheFunction);
1540 // Insert the conditional branch into the end of LoopEndBB.
1541 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1543 // Any new code will be inserted in AfterBB.
1544 Builder.SetInsertPoint(AfterBB);
1546 // Add a new entry to the PHI node for the backedge.
1547 Variable->addIncoming(NextVar, LoopEndBB);
1549 // Restore the unshadowed variable.
1551 NamedValues[VarName] = OldVal;
1553 NamedValues.erase(VarName);
1556 // for expr always returns 0.0.
1557 return Constant::getNullValue(Type::DoubleTy);
1560 Function *PrototypeAST::Codegen() {
1561 // Make the function type: double(double,double) etc.
1562 std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
1563 FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
1565 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
1567 // If F conflicted, there was already something named 'Name'. If it has a
1568 // body, don't allow redefinition or reextern.
1569 if (F->getName() != Name) {
1570 // Delete the one we just made and get the existing one.
1571 F->eraseFromParent();
1572 F = TheModule->getFunction(Name);
1574 // If F already has a body, reject this.
1575 if (!F->empty()) {
1576 ErrorF("redefinition of function");
1580 // If F took a different number of args, reject.
1581 if (F->arg_size() != Args.size()) {
1582 ErrorF("redefinition of function with different # args");
1587 // Set names for all arguments.
1589 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1591 AI->setName(Args[Idx]);
1593 // Add arguments to variable symbol table.
1594 NamedValues[Args[Idx]] = AI;
1600 Function *FunctionAST::Codegen() {
1601 NamedValues.clear();
1603 Function *TheFunction = Proto->Codegen();
1604 if (TheFunction == 0)
1607 // If this is an operator, install it.
1608 if (Proto->isBinaryOp())
1609 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
1611 // Create a new basic block to start insertion into.
1612 BasicBlock *BB = new BasicBlock("entry", TheFunction);
1613 Builder.SetInsertPoint(BB);
1615 if (Value *RetVal = Body->Codegen()) {
1616 // Finish off the function.
1617 Builder.CreateRet(RetVal);
1619 // Validate the generated code, checking for consistency.
1620 verifyFunction(*TheFunction);
1622 // Optimize the function.
1623 TheFPM->run(*TheFunction);
1628 // Error reading body, remove function.
1629 TheFunction->eraseFromParent();
1631 if (Proto->isBinaryOp())
1632 BinopPrecedence.erase(Proto->getOperatorName());
1636 //===----------------------------------------------------------------------===//
1637 // Top-Level parsing and JIT Driver
1638 //===----------------------------------------------------------------------===//
1640 static ExecutionEngine *TheExecutionEngine;
1642 static void HandleDefinition() {
1643 if (FunctionAST *F = ParseDefinition()) {
1644 if (Function *LF = F->Codegen()) {
1645 fprintf(stderr, "Read function definition:");
1649 // Skip token for error recovery.
1654 static void HandleExtern() {
1655 if (PrototypeAST *P = ParseExtern()) {
1656 if (Function *F = P->Codegen()) {
1657 fprintf(stderr, "Read extern: ");
1661 // Skip token for error recovery.
1666 static void HandleTopLevelExpression() {
1667 // Evaluate a top level expression into an anonymous function.
1668 if (FunctionAST *F = ParseTopLevelExpr()) {
1669 if (Function *LF = F->Codegen()) {
1670 // JIT the function, returning a function pointer.
1671 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
1673 // Cast it to the right type (takes no arguments, returns a double) so we
1674 // can call it as a native function.
1675 double (*FP)() = (double (*)())FPtr;
1676 fprintf(stderr, "Evaluated to %f\n", FP());
1679 // Skip token for error recovery.
1684 /// top ::= definition | external | expression | ';'
1685 static void MainLoop() {
1687 fprintf(stderr, "ready> ");
1689 case tok_eof: return;
1690 case ';': getNextToken(); break; // ignore top level semicolons.
1691 case tok_def: HandleDefinition(); break;
1692 case tok_extern: HandleExtern(); break;
1693 default: HandleTopLevelExpression(); break;
1700 //===----------------------------------------------------------------------===//
1701 // "Library" functions that can be "extern'd" from user code.
1702 //===----------------------------------------------------------------------===//
1704 /// putchard - putchar that takes a double and returns 0.
1706 double putchard(double X) {
1711 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1713 double printd(double X) {
1718 //===----------------------------------------------------------------------===//
1719 // Main driver code.
1720 //===----------------------------------------------------------------------===//
1723 // Install standard binary operators.
1724 // 1 is lowest precedence.
1725 BinopPrecedence['<'] = 10;
1726 BinopPrecedence['+'] = 20;
1727 BinopPrecedence['-'] = 20;
1728 BinopPrecedence['*'] = 40; // highest.
1730 // Prime the first token.
1731 fprintf(stderr, "ready> ");
1734 // Make the module, which holds all the code.
1735 TheModule = new Module("my cool jit");
1738 TheExecutionEngine = ExecutionEngine::create(TheModule);
1741 ExistingModuleProvider OurModuleProvider(TheModule);
1742 FunctionPassManager OurFPM(&OurModuleProvider);
1744 // Set up the optimizer pipeline. Start with registering info about how the
1745 // target lays out data structures.
1746 OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
1747 // Do simple "peephole" optimizations and bit-twiddling optzns.
1748 OurFPM.add(createInstructionCombiningPass());
1749 // Reassociate expressions.
1750 OurFPM.add(createReassociatePass());
1751 // Eliminate Common SubExpressions.
1752 OurFPM.add(createGVNPass());
1753 // Simplify the control flow graph (deleting unreachable blocks, etc).
1754 OurFPM.add(createCFGSimplificationPass());
1755 // Set the global so the code gen can use this.
1756 TheFPM = &OurFPM;
1758 // Run the main "interpreter loop" now.
1762 } // Free module provider and pass manager.
1765 // Print out all of the generated code.
1774 <!-- *********************************************************************** -->
1777 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1778 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1779 <a href="http://validator.w3.org/check/referer"><img
1780 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1782 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1783 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
1784 Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $