1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
6 <title>Kaleidoscope: Implementing code generation to LLVM IR</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: Code generation to LLVM IR</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 3 Introduction</a></div>
22 <!-- *********************************************************************** -->
24 <div class="doc_text">
26 <p>Welcome to part 3 of the "<a href="index.html">Implementing a language with
27 LLVM</a>" tutorial. This chapter shows you how to transform the <a
28 href="LangImpl2.html">Abstract Syntax Tree built in Chapter 2</a> into LLVM IR.
29 This will teach you a little bit about how LLVM does things, as well as
30 demonstrate how easy it is to use. It's much more work to build a lexer and
31 parser than it is to generate LLVM IR code.
36 <!-- *********************************************************************** -->
37 <div class="doc_section"><a name="basics">Code Generation setup</a></div>
38 <!-- *********************************************************************** -->
40 <div class="doc_text">
43 In order to generate LLVM IR, we want some simple setup to get started. First,
44 we define virtual codegen methods in each AST class:</p>
46 <div class="doc_code">
48 /// ExprAST - Base class for all expression nodes.
52 virtual Value *Codegen() = 0;
55 /// NumberExprAST - Expression class for numeric literals like "1.0".
56 class NumberExprAST : public ExprAST {
59 explicit NumberExprAST(double val) : Val(val) {}
60 virtual Value *Codegen();
66 <p>The Codegen() method says to emit IR for that AST node and all things it
67 depends on, and they all return an LLVM Value object.
68 "Value" is the class used to represent a "<a
69 href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single
70 Assignment (SSA)</a> register" or "SSA value" in LLVM. The most distinct aspect
71 of SSA values is that their value is computed as the related instruction
72 executes, and it does not get a new value until (and if) the instruction
73 re-executes. In order words, there is no way to "change" an SSA value. For
74 more information, please read up on <a
75 href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single
76 Assignment</a> - the concepts are really quite natural once you grok them.</p>
79 second thing we want is an "Error" method like we used for parser, which will
80 be used to report errors found during code generation (for example, use of an
81 undeclared parameter):</p>
83 <div class="doc_code">
85 Value *ErrorV(const char *Str) { Error(Str); return 0; }
87 static Module *TheModule;
88 static LLVMBuilder Builder;
89 static std::map<std::string, Value*> NamedValues;
93 <p>The static variables will be used during code generation. <tt>TheModule</tt>
94 is the LLVM construct that contains all of the functions and global variables in
95 a chunk of code. In many ways, it is the top-level structure that the LLVM IR
96 uses to contain code.</p>
98 <p>The <tt>Builder</tt> object is a helper object that makes it easy to generate
99 LLVM instructions. The <tt>Builder</tt> keeps track of the current place to
100 insert instructions and has methods to create new instructions.</p>
102 <p>The <tt>NamedValues</tt> map keeps track of which values are defined in the
103 current scope and what their LLVM representation is. In this form of
104 Kaleidoscope, the only things that can be referenced are function parameters.
105 As such, function parameters will be in this map when generating code for their
109 With these basics in place, we can start talking about how to generate code for
110 each expression. Note that this assumes that the <tt>Builder</tt> has been set
111 up to generate code <em>into</em> something. For now, we'll assume that this
112 has already been done, and we'll just use it to emit code.
117 <!-- *********************************************************************** -->
118 <div class="doc_section"><a name="exprs">Expression Code Generation</a></div>
119 <!-- *********************************************************************** -->
121 <div class="doc_text">
123 <p>Generating LLVM code for expression nodes is very straight-forward: less
124 than 45 lines of commented code for all four of our expression nodes. First,
125 we'll do numeric literals:</p>
127 <div class="doc_code">
129 Value *NumberExprAST::Codegen() {
130 return ConstantFP::get(Type::DoubleTy, APFloat(Val));
135 <p>In the LLVM IR, numeric constants are represented with the
136 <tt>ConstantFP</tt> class, which holds the numeric value in an <tt>APFloat</tt>
137 internally (<tt>APFloat</tt> has the capability of holding floating point
138 constants of <em>A</em>rbitrary <em>P</em>recision). This code basically just
139 creates and returns a <tt>ConstantFP</tt>. Note that in the LLVM IR
140 that constants are all uniqued together and shared. For this reason, the API
141 uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::create(..).</p>
143 <div class="doc_code">
145 Value *VariableExprAST::Codegen() {
146 // Look this variable up in the function.
147 Value *V = NamedValues[Name];
148 return V ? V : ErrorV("Unknown variable name");
153 <p>References to variables is also quite simple here. In the simple version
154 of Kaleidoscope, we assume that the variable has already been emited somewhere
155 and its value is available. In practice, the only values that can be in the
156 <tt>NamedValues</tt> map are function arguments. This
157 code simply checks to see that the specified name is in the map (if not, an
158 unknown variable is being referenced) and returns the value for it.</p>
160 <div class="doc_code">
162 Value *BinaryExprAST::Codegen() {
163 Value *L = LHS->Codegen();
164 Value *R = RHS->Codegen();
165 if (L == 0 || R == 0) return 0;
168 case '+': return Builder.CreateAdd(L, R, "addtmp");
169 case '-': return Builder.CreateSub(L, R, "subtmp");
170 case '*': return Builder.CreateMul(L, R, "multmp");
172 L = Builder.CreateFCmpULT(L, R, "multmp");
173 // Convert bool 0/1 to double 0.0 or 1.0
174 return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
175 default: return ErrorV("invalid binary operator");
181 <p>Binary operators start to get more interesting. The basic idea here is that
182 we recursively emit code for the left-hand side of the expression, then the
183 right-hand side, then we compute the result of the binary expression. In this
184 code, we do a simple switch on the opcode to create the right LLVM instruction.
187 <p>In this example, the LLVM builder class is starting to show its value.
188 Because it knows where to insert the newly created instruction, you just have to
189 specificy what instruction to create (e.g. with <tt>CreateAdd</tt>), which
190 operands to use (<tt>L</tt> and <tt>R</tt> here) and optionally provide a name
191 for the generated instruction. One nice thing about LLVM is that the name is
192 just a hint: if there are multiple additions in a single function, the first
193 will be named "addtmp" and the second will be "autorenamed" by adding a suffix,
194 giving it a name like "addtmp42". Local value names for instructions are purely
195 optional, but it makes it much easier to read the IR dumps.</p>
197 <p><a href="../LangRef.html#instref">LLVM instructions</a> are constrained to
198 have very strict type properties: for example, the Left and Right operators of
199 an <a href="../LangRef.html#i_add">add instruction</a> have to have the same
200 type, and that the result of the add matches the operands. Because all values
201 in Kaleidoscope are doubles, this makes for very simple code for add, sub and
204 <p>On the other hand, LLVM specifies that the <a
205 href="../LangRef.html#i_fcmp">fcmp instruction</a> always returns an 'i1' value
206 (a one bit integer). However, Kaleidoscope wants the value to be a 0.0 or 1.0
207 value. In order to get these semantics, we combine the fcmp instruction with
208 a <a href="../LangRef.html#i_uitofp">uitofp instruction</a>. This instruction
209 converts its input integer into a floating point value by treating the input
210 as an unsigned value. In contrast, if we used the <a
211 href="../LangRef.html#i_sitofp">sitofp instruction</a>, the Kaleidoscope '<'
212 operator would return 0.0 and -1.0, depending on the input value.</p>
214 <div class="doc_code">
216 Value *CallExprAST::Codegen() {
217 // Look up the name in the global module table.
218 Function *CalleeF = TheModule->getFunction(Callee);
220 return ErrorV("Unknown function referenced");
222 // If argument mismatch error.
223 if (CalleeF->arg_size() != Args.size())
224 return ErrorV("Incorrect # arguments passed");
226 std::vector<Value*> ArgsV;
227 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
228 ArgsV.push_back(Args[i]->Codegen());
229 if (ArgsV.back() == 0) return 0;
232 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
237 <p>Code generation for function calls is quite straight-forward with LLVM. The
238 code above first looks the name of the function up in the LLVM Module's symbol
239 table. Recall that the LLVM Module is the container that holds all of the
240 functions we are JIT'ing. By giving each function the same name as what the
241 user specifies, we can use the LLVM symbol table to resolve function names for
244 <p>Once we have the function to call, we recursively codegen each argument that
245 is to be passed in, and create an LLVM <a href="../LangRef.html#i_call">call
246 instruction</a>. Note that LLVM uses the native C calling conventions by
247 default, allowing these calls to call into standard library functions like
248 "sin" and "cos" with no additional effort.</p>
250 <p>This wraps up our handling of the four basic expressions that we have so far
251 in Kaleidoscope. Feel free to go in and add some more. For example, by
252 browsing the <a href="../LangRef.html">LLVM language reference</a> you'll find
253 several other interesting instructions that are really easy to plug into our
258 <!-- *********************************************************************** -->
259 <div class="doc_section"><a name="code">Conclusions and the Full Code</a></div>
260 <!-- *********************************************************************** -->
262 <div class="doc_text">
264 <div class="doc_code">
267 // g++ -g toy.cpp `llvm-config --cppflags` `llvm-config --ldflags` \
268 // `llvm-config --libs core` -I ~/llvm/include/
270 // See example below.
272 #include "llvm/DerivedTypes.h"
273 #include "llvm/Module.h"
274 #include "llvm/Support/LLVMBuilder.h"
275 #include <cstdio>
276 #include <string>
278 #include <vector>
279 using namespace llvm;
281 //===----------------------------------------------------------------------===//
283 //===----------------------------------------------------------------------===//
285 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
286 // of these for known things.
291 tok_def = -2, tok_extern = -3,
294 tok_identifier = -4, tok_number = -5,
297 static std::string IdentifierStr; // Filled in if tok_identifier
298 static double NumVal; // Filled in if tok_number
300 /// gettok - Return the next token from standard input.
301 static int gettok() {
302 static int LastChar = ' ';
304 // Skip any whitespace.
305 while (isspace(LastChar))
306 LastChar = getchar();
308 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
309 IdentifierStr = LastChar;
310 while (isalnum((LastChar = getchar())))
311 IdentifierStr += LastChar;
313 if (IdentifierStr == "def") return tok_def;
314 if (IdentifierStr == "extern") return tok_extern;
315 return tok_identifier;
318 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
322 LastChar = getchar();
323 } while (isdigit(LastChar) || LastChar == '.');
325 NumVal = strtod(NumStr.c_str(), 0);
329 if (LastChar == '#') {
330 // Comment until end of line.
331 do LastChar = getchar();
332 while (LastChar != EOF && LastChar != '\n' & LastChar != '\r');
338 // Check for end of file. Don't eat the EOF.
342 // Otherwise, just return the character as its ascii value.
343 int ThisChar = LastChar;
344 LastChar = getchar();
348 //===----------------------------------------------------------------------===//
349 // Abstract Syntax Tree (aka Parse Tree)
350 //===----------------------------------------------------------------------===//
352 /// ExprAST - Base class for all expression nodes.
355 virtual ~ExprAST() {}
356 virtual Value *Codegen() = 0;
359 /// NumberExprAST - Expression class for numeric literals like "1.0".
360 class NumberExprAST : public ExprAST {
363 explicit NumberExprAST(double val) : Val(val) {}
364 virtual Value *Codegen();
367 /// VariableExprAST - Expression class for referencing a variable, like "a".
368 class VariableExprAST : public ExprAST {
371 explicit VariableExprAST(const std::string &name) : Name(name) {}
372 virtual Value *Codegen();
375 /// BinaryExprAST - Expression class for a binary operator.
376 class BinaryExprAST : public ExprAST {
380 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
381 : Op(op), LHS(lhs), RHS(rhs) {}
382 virtual Value *Codegen();
385 /// CallExprAST - Expression class for function calls.
386 class CallExprAST : public ExprAST {
388 std::vector<ExprAST*> Args;
390 CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
391 : Callee(callee), Args(args) {}
392 virtual Value *Codegen();
395 /// PrototypeAST - This class represents the "prototype" for a function,
396 /// which captures its argument names as well as if it is an operator.
399 std::vector<std::string> Args;
401 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
402 : Name(name), Args(args) {}
407 /// FunctionAST - This class represents a function definition itself.
412 FunctionAST(PrototypeAST *proto, ExprAST *body)
413 : Proto(proto), Body(body) {}
418 //===----------------------------------------------------------------------===//
420 //===----------------------------------------------------------------------===//
422 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
423 /// token the parser it looking at. getNextToken reads another token from the
424 /// lexer and updates CurTok with its results.
426 static int getNextToken() {
427 return CurTok = gettok();
430 /// BinopPrecedence - This holds the precedence for each binary operator that is
432 static std::map<char, int> BinopPrecedence;
434 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
435 static int GetTokPrecedence() {
436 if (!isascii(CurTok))
439 // Make sure it's a declared binop.
440 int TokPrec = BinopPrecedence[CurTok];
441 if (TokPrec <= 0) return -1;
445 /// Error* - These are little helper functions for error handling.
446 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
447 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
448 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
450 static ExprAST *ParseExpression();
454 /// ::= identifer '(' expression* ')'
455 static ExprAST *ParseIdentifierExpr() {
456 std::string IdName = IdentifierStr;
458 getNextToken(); // eat identifer.
460 if (CurTok != '(') // Simple variable ref.
461 return new VariableExprAST(IdName);
464 getNextToken(); // eat (
465 std::vector<ExprAST*> Args;
467 ExprAST *Arg = ParseExpression();
471 if (CurTok == ')') break;
474 return Error("Expected ')'");
481 return new CallExprAST(IdName, Args);
484 /// numberexpr ::= number
485 static ExprAST *ParseNumberExpr() {
486 ExprAST *Result = new NumberExprAST(NumVal);
487 getNextToken(); // consume the number
491 /// parenexpr ::= '(' expression ')'
492 static ExprAST *ParseParenExpr() {
493 getNextToken(); // eat (.
494 ExprAST *V = ParseExpression();
498 return Error("expected ')'");
499 getNextToken(); // eat ).
504 /// ::= identifierexpr
507 static ExprAST *ParsePrimary() {
509 default: return Error("unknown token when expecting an expression");
510 case tok_identifier: return ParseIdentifierExpr();
511 case tok_number: return ParseNumberExpr();
512 case '(': return ParseParenExpr();
517 /// ::= ('+' primary)*
518 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
519 // If this is a binop, find its precedence.
521 int TokPrec = GetTokPrecedence();
523 // If this is a binop that binds at least as tightly as the current binop,
524 // consume it, otherwise we are done.
525 if (TokPrec < ExprPrec)
528 // Okay, we know this is a binop.
530 getNextToken(); // eat binop
532 // Parse the primary expression after the binary operator.
533 ExprAST *RHS = ParsePrimary();
536 // If BinOp binds less tightly with RHS than the operator after RHS, let
537 // the pending operator take RHS as its LHS.
538 int NextPrec = GetTokPrecedence();
539 if (TokPrec < NextPrec) {
540 RHS = ParseBinOpRHS(TokPrec+1, RHS);
541 if (RHS == 0) return 0;
545 LHS = new BinaryExprAST(BinOp, LHS, RHS);
550 /// ::= primary binoprhs
552 static ExprAST *ParseExpression() {
553 ExprAST *LHS = ParsePrimary();
556 return ParseBinOpRHS(0, LHS);
560 /// ::= id '(' id* ')'
561 static PrototypeAST *ParsePrototype() {
562 if (CurTok != tok_identifier)
563 return ErrorP("Expected function name in prototype");
565 std::string FnName = IdentifierStr;
569 return ErrorP("Expected '(' in prototype");
571 std::vector<std::string> ArgNames;
572 while (getNextToken() == tok_identifier)
573 ArgNames.push_back(IdentifierStr);
575 return ErrorP("Expected ')' in prototype");
578 getNextToken(); // eat ')'.
580 return new PrototypeAST(FnName, ArgNames);
583 /// definition ::= 'def' prototype expression
584 static FunctionAST *ParseDefinition() {
585 getNextToken(); // eat def.
586 PrototypeAST *Proto = ParsePrototype();
587 if (Proto == 0) return 0;
589 if (ExprAST *E = ParseExpression())
590 return new FunctionAST(Proto, E);
594 /// toplevelexpr ::= expression
595 static FunctionAST *ParseTopLevelExpr() {
596 if (ExprAST *E = ParseExpression()) {
597 // Make an anonymous proto.
598 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
599 return new FunctionAST(Proto, E);
604 /// external ::= 'extern' prototype
605 static PrototypeAST *ParseExtern() {
606 getNextToken(); // eat extern.
607 return ParsePrototype();
610 //===----------------------------------------------------------------------===//
612 //===----------------------------------------------------------------------===//
614 static Module *TheModule;
615 static LLVMBuilder Builder;
616 static std::map<std::string, Value*> NamedValues;
618 Value *ErrorV(const char *Str) { Error(Str); return 0; }
620 Value *NumberExprAST::Codegen() {
621 return ConstantFP::get(Type::DoubleTy, APFloat(Val));
624 Value *VariableExprAST::Codegen() {
625 // Look this variable up in the function.
626 Value *V = NamedValues[Name];
627 return V ? V : ErrorV("Unknown variable name");
630 Value *BinaryExprAST::Codegen() {
631 Value *L = LHS->Codegen();
632 Value *R = RHS->Codegen();
633 if (L == 0 || R == 0) return 0;
636 case '+': return Builder.CreateAdd(L, R, "addtmp");
637 case '-': return Builder.CreateSub(L, R, "subtmp");
638 case '*': return Builder.CreateMul(L, R, "multmp");
640 L = Builder.CreateFCmpULT(L, R, "multmp");
641 // Convert bool 0/1 to double 0.0 or 1.0
642 return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
643 default: return ErrorV("invalid binary operator");
647 Value *CallExprAST::Codegen() {
648 // Look up the name in the global module table.
649 Function *CalleeF = TheModule->getFunction(Callee);
651 return ErrorV("Unknown function referenced");
653 // If argument mismatch error.
654 if (CalleeF->arg_size() != Args.size())
655 return ErrorV("Incorrect # arguments passed");
657 std::vector<Value*> ArgsV;
658 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
659 ArgsV.push_back(Args[i]->Codegen());
660 if (ArgsV.back() == 0) return 0;
663 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
666 Function *PrototypeAST::Codegen() {
667 // Make the function type: double(double,double) etc.
669 FunctionType::get(Type::DoubleTy, std::vector<const Type*>(Args.size(),
673 Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
675 // If F conflicted, there was already something named 'Name'. If it has a
676 // body, don't allow redefinition or reextern.
677 if (F->getName() != Name) {
678 // Delete the one we just made and get the existing one.
679 F->eraseFromParent();
680 F = TheModule->getFunction(Name);
682 // If F already has a body, reject this.
683 if (!F->empty()) {
684 ErrorF("redefinition of function");
688 // If F took a different number of args, reject.
689 if (F->arg_size() != Args.size()) {
690 ErrorF("redefinition of function with different # args");
695 // Set names for all arguments.
697 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
699 AI->setName(Args[Idx]);
701 // Add arguments to variable symbol table.
702 NamedValues[Args[Idx]] = AI;
708 Function *FunctionAST::Codegen() {
711 Function *TheFunction = Proto->Codegen();
712 if (TheFunction == 0)
715 // Create a new basic block to start insertion into.
716 Builder.SetInsertPoint(new BasicBlock("entry", TheFunction));
718 if (Value *RetVal = Body->Codegen()) {
719 // Finish off the function.
720 Builder.CreateRet(RetVal);
724 // Error reading body, remove function.
725 TheFunction->eraseFromParent();
729 //===----------------------------------------------------------------------===//
730 // Top-Level parsing and JIT Driver
731 //===----------------------------------------------------------------------===//
733 static void HandleDefinition() {
734 if (FunctionAST *F = ParseDefinition()) {
735 if (Function *LF = F->Codegen()) {
736 fprintf(stderr, "Read function definition:");
740 // Skip token for error recovery.
745 static void HandleExtern() {
746 if (PrototypeAST *P = ParseExtern()) {
747 if (Function *F = P->Codegen()) {
748 fprintf(stderr, "Read extern: ");
752 // Skip token for error recovery.
757 static void HandleTopLevelExpression() {
758 // Evaluate a top level expression into an anonymous function.
759 if (FunctionAST *F = ParseTopLevelExpr()) {
760 if (Function *LF = F->Codegen()) {
761 fprintf(stderr, "Read top-level expression:");
765 // Skip token for error recovery.
770 /// top ::= definition | external | expression | ';'
771 static void MainLoop() {
773 fprintf(stderr, "ready> ");
775 case tok_eof: return;
776 case ';': getNextToken(); break; // ignore top level semicolons.
777 case tok_def: HandleDefinition(); break;
778 case tok_extern: HandleExtern(); break;
779 default: HandleTopLevelExpression(); break;
786 //===----------------------------------------------------------------------===//
787 // "Library" functions that can be "extern'd" from user code.
788 //===----------------------------------------------------------------------===//
790 /// putchard - putchar that takes a double and returns 0.
792 double putchard(double X) {
797 //===----------------------------------------------------------------------===//
799 //===----------------------------------------------------------------------===//
802 TheModule = new Module("my cool jit");
804 // Install standard binary operators.
805 // 1 is lowest precedence.
806 BinopPrecedence['<'] = 10;
807 BinopPrecedence['+'] = 20;
808 BinopPrecedence['-'] = 20;
809 BinopPrecedence['*'] = 40; // highest.
811 // Prime the first token.
812 fprintf(stderr, "ready> ");
816 TheModule->dump();
835 <!-- *********************************************************************** -->
838 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
839 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
840 <a href="http://validator.w3.org/check/referer"><img
841 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" /></a>
843 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
844 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
845 Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $