llvm.org GIT mirror llvm / 31c6c5d
Add examples for Kaleidoscope chapters 2 through 6. Conflicts: examples/Makefile git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82574 91177308-0d34-0410-b5e6-96231b3b80d8 Erick Tryzelaar 10 years ago
18 changed file(s) with 3503 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
0 add_subdirectory(Chapter2)
1 add_subdirectory(Chapter3)
2 add_subdirectory(Chapter4)
3 add_subdirectory(Chapter5)
4 add_subdirectory(Chapter6)
5 add_subdirectory(Chapter7)
0 add_llvm_example(Kaleidoscope-Ch2
1 toy.cpp
2 )
0 ##===- examples/Kaleidoscope/Chapter2/Makefile -------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 LEVEL = ../../..
9 TOOLNAME = Kaleidoscope-Ch2
10 EXAMPLE_TOOL = 1
11
12 include $(LEVEL)/Makefile.common
0 #include
1 #include
2 #include
3 #include
4 #include
5
6 //===----------------------------------------------------------------------===//
7 // Lexer
8 //===----------------------------------------------------------------------===//
9
10 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
11 // of these for known things.
12 enum Token {
13 tok_eof = -1,
14
15 // commands
16 tok_def = -2, tok_extern = -3,
17
18 // primary
19 tok_identifier = -4, tok_number = -5
20 };
21
22 static std::string IdentifierStr; // Filled in if tok_identifier
23 static double NumVal; // Filled in if tok_number
24
25 /// gettok - Return the next token from standard input.
26 static int gettok() {
27 static int LastChar = ' ';
28
29 // Skip any whitespace.
30 while (isspace(LastChar))
31 LastChar = getchar();
32
33 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
34 IdentifierStr = LastChar;
35 while (isalnum((LastChar = getchar())))
36 IdentifierStr += LastChar;
37
38 if (IdentifierStr == "def") return tok_def;
39 if (IdentifierStr == "extern") return tok_extern;
40 return tok_identifier;
41 }
42
43 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
44 std::string NumStr;
45 do {
46 NumStr += LastChar;
47 LastChar = getchar();
48 } while (isdigit(LastChar) || LastChar == '.');
49
50 NumVal = strtod(NumStr.c_str(), 0);
51 return tok_number;
52 }
53
54 if (LastChar == '#') {
55 // Comment until end of line.
56 do LastChar = getchar();
57 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
58
59 if (LastChar != EOF)
60 return gettok();
61 }
62
63 // Check for end of file. Don't eat the EOF.
64 if (LastChar == EOF)
65 return tok_eof;
66
67 // Otherwise, just return the character as its ascii value.
68 int ThisChar = LastChar;
69 LastChar = getchar();
70 return ThisChar;
71 }
72
73 //===----------------------------------------------------------------------===//
74 // Abstract Syntax Tree (aka Parse Tree)
75 //===----------------------------------------------------------------------===//
76
77 /// ExprAST - Base class for all expression nodes.
78 class ExprAST {
79 public:
80 virtual ~ExprAST() {}
81 };
82
83 /// NumberExprAST - Expression class for numeric literals like "1.0".
84 class NumberExprAST : public ExprAST {
85 double Val;
86 public:
87 NumberExprAST(double val) : Val(val) {}
88 };
89
90 /// VariableExprAST - Expression class for referencing a variable, like "a".
91 class VariableExprAST : public ExprAST {
92 std::string Name;
93 public:
94 VariableExprAST(const std::string &name) : Name(name) {}
95 };
96
97 /// BinaryExprAST - Expression class for a binary operator.
98 class BinaryExprAST : public ExprAST {
99 char Op;
100 ExprAST *LHS, *RHS;
101 public:
102 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
103 : Op(op), LHS(lhs), RHS(rhs) {}
104 };
105
106 /// CallExprAST - Expression class for function calls.
107 class CallExprAST : public ExprAST {
108 std::string Callee;
109 std::vector Args;
110 public:
111 CallExprAST(const std::string &callee, std::vector &args)
112 : Callee(callee), Args(args) {}
113 };
114
115 /// PrototypeAST - This class represents the "prototype" for a function,
116 /// which captures its name, and its argument names (thus implicitly the number
117 /// of arguments the function takes).
118 class PrototypeAST {
119 std::string Name;
120 std::vector Args;
121 public:
122 PrototypeAST(const std::string &name, const std::vector &args)
123 : Name(name), Args(args) {}
124
125 };
126
127 /// FunctionAST - This class represents a function definition itself.
128 class FunctionAST {
129 PrototypeAST *Proto;
130 ExprAST *Body;
131 public:
132 FunctionAST(PrototypeAST *proto, ExprAST *body)
133 : Proto(proto), Body(body) {}
134
135 };
136
137 //===----------------------------------------------------------------------===//
138 // Parser
139 //===----------------------------------------------------------------------===//
140
141 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
142 /// token the parser is looking at. getNextToken reads another token from the
143 /// lexer and updates CurTok with its results.
144 static int CurTok;
145 static int getNextToken() {
146 return CurTok = gettok();
147 }
148
149 /// BinopPrecedence - This holds the precedence for each binary operator that is
150 /// defined.
151 static std::map BinopPrecedence;
152
153 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
154 static int GetTokPrecedence() {
155 if (!isascii(CurTok))
156 return -1;
157
158 // Make sure it's a declared binop.
159 int TokPrec = BinopPrecedence[CurTok];
160 if (TokPrec <= 0) return -1;
161 return TokPrec;
162 }
163
164 /// Error* - These are little helper functions for error handling.
165 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
166 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
167 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
168
169 static ExprAST *ParseExpression();
170
171 /// identifierexpr
172 /// ::= identifier
173 /// ::= identifier '(' expression* ')'
174 static ExprAST *ParseIdentifierExpr() {
175 std::string IdName = IdentifierStr;
176
177 getNextToken(); // eat identifier.
178
179 if (CurTok != '(') // Simple variable ref.
180 return new VariableExprAST(IdName);
181
182 // Call.
183 getNextToken(); // eat (
184 std::vector Args;
185 if (CurTok != ')') {
186 while (1) {
187 ExprAST *Arg = ParseExpression();
188 if (!Arg) return 0;
189 Args.push_back(Arg);
190
191 if (CurTok == ')') break;
192
193 if (CurTok != ',')
194 return Error("Expected ')' or ',' in argument list");
195 getNextToken();
196 }
197 }
198
199 // Eat the ')'.
200 getNextToken();
201
202 return new CallExprAST(IdName, Args);
203 }
204
205 /// numberexpr ::= number
206 static ExprAST *ParseNumberExpr() {
207 ExprAST *Result = new NumberExprAST(NumVal);
208 getNextToken(); // consume the number
209 return Result;
210 }
211
212 /// parenexpr ::= '(' expression ')'
213 static ExprAST *ParseParenExpr() {
214 getNextToken(); // eat (.
215 ExprAST *V = ParseExpression();
216 if (!V) return 0;
217
218 if (CurTok != ')')
219 return Error("expected ')'");
220 getNextToken(); // eat ).
221 return V;
222 }
223
224 /// primary
225 /// ::= identifierexpr
226 /// ::= numberexpr
227 /// ::= parenexpr
228 static ExprAST *ParsePrimary() {
229 switch (CurTok) {
230 default: return Error("unknown token when expecting an expression");
231 case tok_identifier: return ParseIdentifierExpr();
232 case tok_number: return ParseNumberExpr();
233 case '(': return ParseParenExpr();
234 }
235 }
236
237 /// binoprhs
238 /// ::= ('+' primary)*
239 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
240 // If this is a binop, find its precedence.
241 while (1) {
242 int TokPrec = GetTokPrecedence();
243
244 // If this is a binop that binds at least as tightly as the current binop,
245 // consume it, otherwise we are done.
246 if (TokPrec < ExprPrec)
247 return LHS;
248
249 // Okay, we know this is a binop.
250 int BinOp = CurTok;
251 getNextToken(); // eat binop
252
253 // Parse the primary expression after the binary operator.
254 ExprAST *RHS = ParsePrimary();
255 if (!RHS) return 0;
256
257 // If BinOp binds less tightly with RHS than the operator after RHS, let
258 // the pending operator take RHS as its LHS.
259 int NextPrec = GetTokPrecedence();
260 if (TokPrec < NextPrec) {
261 RHS = ParseBinOpRHS(TokPrec+1, RHS);
262 if (RHS == 0) return 0;
263 }
264
265 // Merge LHS/RHS.
266 LHS = new BinaryExprAST(BinOp, LHS, RHS);
267 }
268 }
269
270 /// expression
271 /// ::= primary binoprhs
272 ///
273 static ExprAST *ParseExpression() {
274 ExprAST *LHS = ParsePrimary();
275 if (!LHS) return 0;
276
277 return ParseBinOpRHS(0, LHS);
278 }
279
280 /// prototype
281 /// ::= id '(' id* ')'
282 static PrototypeAST *ParsePrototype() {
283 if (CurTok != tok_identifier)
284 return ErrorP("Expected function name in prototype");
285
286 std::string FnName = IdentifierStr;
287 getNextToken();
288
289 if (CurTok != '(')
290 return ErrorP("Expected '(' in prototype");
291
292 std::vector ArgNames;
293 while (getNextToken() == tok_identifier)
294 ArgNames.push_back(IdentifierStr);
295 if (CurTok != ')')
296 return ErrorP("Expected ')' in prototype");
297
298 // success.
299 getNextToken(); // eat ')'.
300
301 return new PrototypeAST(FnName, ArgNames);
302 }
303
304 /// definition ::= 'def' prototype expression
305 static FunctionAST *ParseDefinition() {
306 getNextToken(); // eat def.
307 PrototypeAST *Proto = ParsePrototype();
308 if (Proto == 0) return 0;
309
310 if (ExprAST *E = ParseExpression())
311 return new FunctionAST(Proto, E);
312 return 0;
313 }
314
315 /// toplevelexpr ::= expression
316 static FunctionAST *ParseTopLevelExpr() {
317 if (ExprAST *E = ParseExpression()) {
318 // Make an anonymous proto.
319 PrototypeAST *Proto = new PrototypeAST("", std::vector());
320 return new FunctionAST(Proto, E);
321 }
322 return 0;
323 }
324
325 /// external ::= 'extern' prototype
326 static PrototypeAST *ParseExtern() {
327 getNextToken(); // eat extern.
328 return ParsePrototype();
329 }
330
331 //===----------------------------------------------------------------------===//
332 // Top-Level parsing
333 //===----------------------------------------------------------------------===//
334
335 static void HandleDefinition() {
336 if (ParseDefinition()) {
337 fprintf(stderr, "Parsed a function definition.\n");
338 } else {
339 // Skip token for error recovery.
340 getNextToken();
341 }
342 }
343
344 static void HandleExtern() {
345 if (ParseExtern()) {
346 fprintf(stderr, "Parsed an extern\n");
347 } else {
348 // Skip token for error recovery.
349 getNextToken();
350 }
351 }
352
353 static void HandleTopLevelExpression() {
354 // Evaluate a top-level expression into an anonymous function.
355 if (ParseTopLevelExpr()) {
356 fprintf(stderr, "Parsed a top-level expr\n");
357 } else {
358 // Skip token for error recovery.
359 getNextToken();
360 }
361 }
362
363 /// top ::= definition | external | expression | ';'
364 static void MainLoop() {
365 while (1) {
366 fprintf(stderr, "ready> ");
367 switch (CurTok) {
368 case tok_eof: return;
369 case ';': getNextToken(); break; // ignore top-level semicolons.
370 case tok_def: HandleDefinition(); break;
371 case tok_extern: HandleExtern(); break;
372 default: HandleTopLevelExpression(); break;
373 }
374 }
375 }
376
377 //===----------------------------------------------------------------------===//
378 // Main driver code.
379 //===----------------------------------------------------------------------===//
380
381 int main() {
382 // Install standard binary operators.
383 // 1 is lowest precedence.
384 BinopPrecedence['<'] = 10;
385 BinopPrecedence['+'] = 20;
386 BinopPrecedence['-'] = 20;
387 BinopPrecedence['*'] = 40; // highest.
388
389 // Prime the first token.
390 fprintf(stderr, "ready> ");
391 getNextToken();
392
393 // Run the main "interpreter loop" now.
394 MainLoop();
395
396 return 0;
397 }
0 set(LLVM_LINK_COMPONENTS core)
1
2 add_llvm_example(Kaleidoscope-Ch3
3 toy.cpp
4 )
0 ##===- examples/Kaleidoscope/Chapter3/Makefile -------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 LEVEL = ../../..
9 TOOLNAME = Kaleidoscope-Ch3
10 EXAMPLE_TOOL = 1
11
12 LINK_COMPONENTS := core
13
14 include $(LEVEL)/Makefile.common
0 #include "llvm/DerivedTypes.h"
1 #include "llvm/LLVMContext.h"
2 #include "llvm/Module.h"
3 #include "llvm/Analysis/Verifier.h"
4 #include "llvm/Support/IRBuilder.h"
5 #include
6 #include
7 #include
8 #include
9 using namespace llvm;
10
11 //===----------------------------------------------------------------------===//
12 // Lexer
13 //===----------------------------------------------------------------------===//
14
15 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
16 // of these for known things.
17 enum Token {
18 tok_eof = -1,
19
20 // commands
21 tok_def = -2, tok_extern = -3,
22
23 // primary
24 tok_identifier = -4, tok_number = -5
25 };
26
27 static std::string IdentifierStr; // Filled in if tok_identifier
28 static double NumVal; // Filled in if tok_number
29
30 /// gettok - Return the next token from standard input.
31 static int gettok() {
32 static int LastChar = ' ';
33
34 // Skip any whitespace.
35 while (isspace(LastChar))
36 LastChar = getchar();
37
38 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
39 IdentifierStr = LastChar;
40 while (isalnum((LastChar = getchar())))
41 IdentifierStr += LastChar;
42
43 if (IdentifierStr == "def") return tok_def;
44 if (IdentifierStr == "extern") return tok_extern;
45 return tok_identifier;
46 }
47
48 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
49 std::string NumStr;
50 do {
51 NumStr += LastChar;
52 LastChar = getchar();
53 } while (isdigit(LastChar) || LastChar == '.');
54
55 NumVal = strtod(NumStr.c_str(), 0);
56 return tok_number;
57 }
58
59 if (LastChar == '#') {
60 // Comment until end of line.
61 do LastChar = getchar();
62 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
63
64 if (LastChar != EOF)
65 return gettok();
66 }
67
68 // Check for end of file. Don't eat the EOF.
69 if (LastChar == EOF)
70 return tok_eof;
71
72 // Otherwise, just return the character as its ascii value.
73 int ThisChar = LastChar;
74 LastChar = getchar();
75 return ThisChar;
76 }
77
78 //===----------------------------------------------------------------------===//
79 // Abstract Syntax Tree (aka Parse Tree)
80 //===----------------------------------------------------------------------===//
81
82 /// ExprAST - Base class for all expression nodes.
83 class ExprAST {
84 public:
85 virtual ~ExprAST() {}
86 virtual Value *Codegen() = 0;
87 };
88
89 /// NumberExprAST - Expression class for numeric literals like "1.0".
90 class NumberExprAST : public ExprAST {
91 double Val;
92 public:
93 NumberExprAST(double val) : Val(val) {}
94 virtual Value *Codegen();
95 };
96
97 /// VariableExprAST - Expression class for referencing a variable, like "a".
98 class VariableExprAST : public ExprAST {
99 std::string Name;
100 public:
101 VariableExprAST(const std::string &name) : Name(name) {}
102 virtual Value *Codegen();
103 };
104
105 /// BinaryExprAST - Expression class for a binary operator.
106 class BinaryExprAST : public ExprAST {
107 char Op;
108 ExprAST *LHS, *RHS;
109 public:
110 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
111 : Op(op), LHS(lhs), RHS(rhs) {}
112 virtual Value *Codegen();
113 };
114
115 /// CallExprAST - Expression class for function calls.
116 class CallExprAST : public ExprAST {
117 std::string Callee;
118 std::vector Args;
119 public:
120 CallExprAST(const std::string &callee, std::vector &args)
121 : Callee(callee), Args(args) {}
122 virtual Value *Codegen();
123 };
124
125 /// PrototypeAST - This class represents the "prototype" for a function,
126 /// which captures its name, and its argument names (thus implicitly the number
127 /// of arguments the function takes).
128 class PrototypeAST {
129 std::string Name;
130 std::vector Args;
131 public:
132 PrototypeAST(const std::string &name, const std::vector &args)
133 : Name(name), Args(args) {}
134
135 Function *Codegen();
136 };
137
138 /// FunctionAST - This class represents a function definition itself.
139 class FunctionAST {
140 PrototypeAST *Proto;
141 ExprAST *Body;
142 public:
143 FunctionAST(PrototypeAST *proto, ExprAST *body)
144 : Proto(proto), Body(body) {}
145
146 Function *Codegen();
147 };
148
149 //===----------------------------------------------------------------------===//
150 // Parser
151 //===----------------------------------------------------------------------===//
152
153 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
154 /// token the parser is looking at. getNextToken reads another token from the
155 /// lexer and updates CurTok with its results.
156 static int CurTok;
157 static int getNextToken() {
158 return CurTok = gettok();
159 }
160
161 /// BinopPrecedence - This holds the precedence for each binary operator that is
162 /// defined.
163 static std::map BinopPrecedence;
164
165 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
166 static int GetTokPrecedence() {
167 if (!isascii(CurTok))
168 return -1;
169
170 // Make sure it's a declared binop.
171 int TokPrec = BinopPrecedence[CurTok];
172 if (TokPrec <= 0) return -1;
173 return TokPrec;
174 }
175
176 /// Error* - These are little helper functions for error handling.
177 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
178 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
179 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
180
181 static ExprAST *ParseExpression();
182
183 /// identifierexpr
184 /// ::= identifier
185 /// ::= identifier '(' expression* ')'
186 static ExprAST *ParseIdentifierExpr() {
187 std::string IdName = IdentifierStr;
188
189 getNextToken(); // eat identifier.
190
191 if (CurTok != '(') // Simple variable ref.
192 return new VariableExprAST(IdName);
193
194 // Call.
195 getNextToken(); // eat (
196 std::vector Args;
197 if (CurTok != ')') {
198 while (1) {
199 ExprAST *Arg = ParseExpression();
200 if (!Arg) return 0;
201 Args.push_back(Arg);
202
203 if (CurTok == ')') break;
204
205 if (CurTok != ',')
206 return Error("Expected ')' or ',' in argument list");
207 getNextToken();
208 }
209 }
210
211 // Eat the ')'.
212 getNextToken();
213
214 return new CallExprAST(IdName, Args);
215 }
216
217 /// numberexpr ::= number
218 static ExprAST *ParseNumberExpr() {
219 ExprAST *Result = new NumberExprAST(NumVal);
220 getNextToken(); // consume the number
221 return Result;
222 }
223
224 /// parenexpr ::= '(' expression ')'
225 static ExprAST *ParseParenExpr() {
226 getNextToken(); // eat (.
227 ExprAST *V = ParseExpression();
228 if (!V) return 0;
229
230 if (CurTok != ')')
231 return Error("expected ')'");
232 getNextToken(); // eat ).
233 return V;
234 }
235
236 /// primary
237 /// ::= identifierexpr
238 /// ::= numberexpr
239 /// ::= parenexpr
240 static ExprAST *ParsePrimary() {
241 switch (CurTok) {
242 default: return Error("unknown token when expecting an expression");
243 case tok_identifier: return ParseIdentifierExpr();
244 case tok_number: return ParseNumberExpr();
245 case '(': return ParseParenExpr();
246 }
247 }
248
249 /// binoprhs
250 /// ::= ('+' primary)*
251 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
252 // If this is a binop, find its precedence.
253 while (1) {
254 int TokPrec = GetTokPrecedence();
255
256 // If this is a binop that binds at least as tightly as the current binop,
257 // consume it, otherwise we are done.
258 if (TokPrec < ExprPrec)
259 return LHS;
260
261 // Okay, we know this is a binop.
262 int BinOp = CurTok;
263 getNextToken(); // eat binop
264
265 // Parse the primary expression after the binary operator.
266 ExprAST *RHS = ParsePrimary();
267 if (!RHS) return 0;
268
269 // If BinOp binds less tightly with RHS than the operator after RHS, let
270 // the pending operator take RHS as its LHS.
271 int NextPrec = GetTokPrecedence();
272 if (TokPrec < NextPrec) {
273 RHS = ParseBinOpRHS(TokPrec+1, RHS);
274 if (RHS == 0) return 0;
275 }
276
277 // Merge LHS/RHS.
278 LHS = new BinaryExprAST(BinOp, LHS, RHS);
279 }
280 }
281
282 /// expression
283 /// ::= primary binoprhs
284 ///
285 static ExprAST *ParseExpression() {
286 ExprAST *LHS = ParsePrimary();
287 if (!LHS) return 0;
288
289 return ParseBinOpRHS(0, LHS);
290 }
291
292 /// prototype
293 /// ::= id '(' id* ')'
294 static PrototypeAST *ParsePrototype() {
295 if (CurTok != tok_identifier)
296 return ErrorP("Expected function name in prototype");
297
298 std::string FnName = IdentifierStr;
299 getNextToken();
300
301 if (CurTok != '(')
302 return ErrorP("Expected '(' in prototype");
303
304 std::vector ArgNames;
305 while (getNextToken() == tok_identifier)
306 ArgNames.push_back(IdentifierStr);
307 if (CurTok != ')')
308 return ErrorP("Expected ')' in prototype");
309
310 // success.
311 getNextToken(); // eat ')'.
312
313 return new PrototypeAST(FnName, ArgNames);
314 }
315
316 /// definition ::= 'def' prototype expression
317 static FunctionAST *ParseDefinition() {
318 getNextToken(); // eat def.
319 PrototypeAST *Proto = ParsePrototype();
320 if (Proto == 0) return 0;
321
322 if (ExprAST *E = ParseExpression())
323 return new FunctionAST(Proto, E);
324 return 0;
325 }
326
327 /// toplevelexpr ::= expression
328 static FunctionAST *ParseTopLevelExpr() {
329 if (ExprAST *E = ParseExpression()) {
330 // Make an anonymous proto.
331 PrototypeAST *Proto = new PrototypeAST("", std::vector());
332 return new FunctionAST(Proto, E);
333 }
334 return 0;
335 }
336
337 /// external ::= 'extern' prototype
338 static PrototypeAST *ParseExtern() {
339 getNextToken(); // eat extern.
340 return ParsePrototype();
341 }
342
343 //===----------------------------------------------------------------------===//
344 // Code Generation
345 //===----------------------------------------------------------------------===//
346
347 static Module *TheModule;
348 static IRBuilder<> Builder(getGlobalContext());
349 static std::map NamedValues;
350
351 Value *ErrorV(const char *Str) { Error(Str); return 0; }
352
353 Value *NumberExprAST::Codegen() {
354 return ConstantFP::get(getGlobalContext(), APFloat(Val));
355 }
356
357 Value *VariableExprAST::Codegen() {
358 // Look this variable up in the function.
359 Value *V = NamedValues[Name];
360 return V ? V : ErrorV("Unknown variable name");
361 }
362
363 Value *BinaryExprAST::Codegen() {
364 Value *L = LHS->Codegen();
365 Value *R = RHS->Codegen();
366 if (L == 0 || R == 0) return 0;
367
368 switch (Op) {
369 case '+': return Builder.CreateAdd(L, R, "addtmp");
370 case '-': return Builder.CreateSub(L, R, "subtmp");
371 case '*': return Builder.CreateMul(L, R, "multmp");
372 case '<':
373 L = Builder.CreateFCmpULT(L, R, "cmptmp");
374 // Convert bool 0/1 to double 0.0 or 1.0
375 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
376 "booltmp");
377 default: return ErrorV("invalid binary operator");
378 }
379 }
380
381 Value *CallExprAST::Codegen() {
382 // Look up the name in the global module table.
383 Function *CalleeF = TheModule->getFunction(Callee);
384 if (CalleeF == 0)
385 return ErrorV("Unknown function referenced");
386
387 // If argument mismatch error.
388 if (CalleeF->arg_size() != Args.size())
389 return ErrorV("Incorrect # arguments passed");
390
391 std::vector ArgsV;
392 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
393 ArgsV.push_back(Args[i]->Codegen());
394 if (ArgsV.back() == 0) return 0;
395 }
396
397 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
398 }
399
400 Function *PrototypeAST::Codegen() {
401 // Make the function type: double(double,double) etc.
402 std::vector Doubles(Args.size(),
403 Type::getDoubleTy(getGlobalContext()));
404 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
405 Doubles, false);
406
407 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
408
409 // If F conflicted, there was already something named 'Name'. If it has a
410 // body, don't allow redefinition or reextern.
411 if (F->getName() != Name) {
412 // Delete the one we just made and get the existing one.
413 F->eraseFromParent();
414 F = TheModule->getFunction(Name);
415
416 // If F already has a body, reject this.
417 if (!F->empty()) {
418 ErrorF("redefinition of function");
419 return 0;
420 }
421
422 // If F took a different number of args, reject.
423 if (F->arg_size() != Args.size()) {
424 ErrorF("redefinition of function with different # args");
425 return 0;
426 }
427 }
428
429 // Set names for all arguments.
430 unsigned Idx = 0;
431 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
432 ++AI, ++Idx) {
433 AI->setName(Args[Idx]);
434
435 // Add arguments to variable symbol table.
436 NamedValues[Args[Idx]] = AI;
437 }
438
439 return F;
440 }
441
442 Function *FunctionAST::Codegen() {
443 NamedValues.clear();
444
445 Function *TheFunction = Proto->Codegen();
446 if (TheFunction == 0)
447 return 0;
448
449 // Create a new basic block to start insertion into.
450 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
451 Builder.SetInsertPoint(BB);
452
453 if (Value *RetVal = Body->Codegen()) {
454 // Finish off the function.
455 Builder.CreateRet(RetVal);
456
457 // Validate the generated code, checking for consistency.
458 verifyFunction(*TheFunction);
459
460 return TheFunction;
461 }
462
463 // Error reading body, remove function.
464 TheFunction->eraseFromParent();
465 return 0;
466 }
467
468 //===----------------------------------------------------------------------===//
469 // Top-Level parsing and JIT Driver
470 //===----------------------------------------------------------------------===//
471
472 static void HandleDefinition() {
473 if (FunctionAST *F = ParseDefinition()) {
474 if (Function *LF = F->Codegen()) {
475 fprintf(stderr, "Read function definition:");
476 LF->dump();
477 }
478 } else {
479 // Skip token for error recovery.
480 getNextToken();
481 }
482 }
483
484 static void HandleExtern() {
485 if (PrototypeAST *P = ParseExtern()) {
486 if (Function *F = P->Codegen()) {
487 fprintf(stderr, "Read extern: ");
488 F->dump();
489 }
490 } else {
491 // Skip token for error recovery.
492 getNextToken();
493 }
494 }
495
496 static void HandleTopLevelExpression() {
497 // Evaluate a top-level expression into an anonymous function.
498 if (FunctionAST *F = ParseTopLevelExpr()) {
499 if (Function *LF = F->Codegen()) {
500 fprintf(stderr, "Read top-level expression:");
501 LF->dump();
502 }
503 } else {
504 // Skip token for error recovery.
505 getNextToken();
506 }
507 }
508
509 /// top ::= definition | external | expression | ';'
510 static void MainLoop() {
511 while (1) {
512 fprintf(stderr, "ready> ");
513 switch (CurTok) {
514 case tok_eof: return;
515 case ';': getNextToken(); break; // ignore top-level semicolons.
516 case tok_def: HandleDefinition(); break;
517 case tok_extern: HandleExtern(); break;
518 default: HandleTopLevelExpression(); break;
519 }
520 }
521 }
522
523 //===----------------------------------------------------------------------===//
524 // "Library" functions that can be "extern'd" from user code.
525 //===----------------------------------------------------------------------===//
526
527 /// putchard - putchar that takes a double and returns 0.
528 extern "C"
529 double putchard(double X) {
530 putchar((char)X);
531 return 0;
532 }
533
534 //===----------------------------------------------------------------------===//
535 // Main driver code.
536 //===----------------------------------------------------------------------===//
537
538 int main() {
539 LLVMContext &Context = getGlobalContext();
540
541 // Install standard binary operators.
542 // 1 is lowest precedence.
543 BinopPrecedence['<'] = 10;
544 BinopPrecedence['+'] = 20;
545 BinopPrecedence['-'] = 20;
546 BinopPrecedence['*'] = 40; // highest.
547
548 // Prime the first token.
549 fprintf(stderr, "ready> ");
550 getNextToken();
551
552 // Make the module, which holds all the code.
553 TheModule = new Module("my cool jit", Context);
554
555 // Run the main "interpreter loop" now.
556 MainLoop();
557
558 // Print out all of the generated code.
559 TheModule->dump();
560
561 return 0;
562 }
0 set(LLVM_LINK_COMPONENTS core jit interpreter native)
1
2 add_llvm_example(Kaleidoscope-Ch4
3 toy.cpp
4 )
0 ##===- examples/Kaleidoscope/Chapter4/Makefile -------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 LEVEL = ../../..
9 TOOLNAME = Kaleidoscope-Ch4
10 EXAMPLE_TOOL = 1
11
12 LINK_COMPONENTS := core jit interpreter native
13
14 include $(LEVEL)/Makefile.common
0 #include "llvm/DerivedTypes.h"
1 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2 #include "llvm/ExecutionEngine/Interpreter.h"
3 #include "llvm/ExecutionEngine/JIT.h"
4 #include "llvm/LLVMContext.h"
5 #include "llvm/Module.h"
6 #include "llvm/ModuleProvider.h"
7 #include "llvm/PassManager.h"
8 #include "llvm/Analysis/Verifier.h"
9 #include "llvm/Target/TargetData.h"
10 #include "llvm/Target/TargetSelect.h"
11 #include "llvm/Transforms/Scalar.h"
12 #include "llvm/Support/IRBuilder.h"
13 #include
14 #include
15 #include
16 #include
17 using namespace llvm;
18
19 //===----------------------------------------------------------------------===//
20 // Lexer
21 //===----------------------------------------------------------------------===//
22
23 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
24 // of these for known things.
25 enum Token {
26 tok_eof = -1,
27
28 // commands
29 tok_def = -2, tok_extern = -3,
30
31 // primary
32 tok_identifier = -4, tok_number = -5
33 };
34
35 static std::string IdentifierStr; // Filled in if tok_identifier
36 static double NumVal; // Filled in if tok_number
37
38 /// gettok - Return the next token from standard input.
39 static int gettok() {
40 static int LastChar = ' ';
41
42 // Skip any whitespace.
43 while (isspace(LastChar))
44 LastChar = getchar();
45
46 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
47 IdentifierStr = LastChar;
48 while (isalnum((LastChar = getchar())))
49 IdentifierStr += LastChar;
50
51 if (IdentifierStr == "def") return tok_def;
52 if (IdentifierStr == "extern") return tok_extern;
53 return tok_identifier;
54 }
55
56 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
57 std::string NumStr;
58 do {
59 NumStr += LastChar;
60 LastChar = getchar();
61 } while (isdigit(LastChar) || LastChar == '.');
62
63 NumVal = strtod(NumStr.c_str(), 0);
64 return tok_number;
65 }
66
67 if (LastChar == '#') {
68 // Comment until end of line.
69 do LastChar = getchar();
70 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
71
72 if (LastChar != EOF)
73 return gettok();
74 }
75
76 // Check for end of file. Don't eat the EOF.
77 if (LastChar == EOF)
78 return tok_eof;
79
80 // Otherwise, just return the character as its ascii value.
81 int ThisChar = LastChar;
82 LastChar = getchar();
83 return ThisChar;
84 }
85
86 //===----------------------------------------------------------------------===//
87 // Abstract Syntax Tree (aka Parse Tree)
88 //===----------------------------------------------------------------------===//
89
90 /// ExprAST - Base class for all expression nodes.
91 class ExprAST {
92 public:
93 virtual ~ExprAST() {}
94 virtual Value *Codegen() = 0;
95 };
96
97 /// NumberExprAST - Expression class for numeric literals like "1.0".
98 class NumberExprAST : public ExprAST {
99 double Val;
100 public:
101 NumberExprAST(double val) : Val(val) {}
102 virtual Value *Codegen();
103 };
104
105 /// VariableExprAST - Expression class for referencing a variable, like "a".
106 class VariableExprAST : public ExprAST {
107 std::string Name;
108 public:
109 VariableExprAST(const std::string &name) : Name(name) {}
110 virtual Value *Codegen();
111 };
112
113 /// BinaryExprAST - Expression class for a binary operator.
114 class BinaryExprAST : public ExprAST {
115 char Op;
116 ExprAST *LHS, *RHS;
117 public:
118 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
119 : Op(op), LHS(lhs), RHS(rhs) {}
120 virtual Value *Codegen();
121 };
122
123 /// CallExprAST - Expression class for function calls.
124 class CallExprAST : public ExprAST {
125 std::string Callee;
126 std::vector Args;
127 public:
128 CallExprAST(const std::string &callee, std::vector &args)
129 : Callee(callee), Args(args) {}
130 virtual Value *Codegen();
131 };
132
133 /// PrototypeAST - This class represents the "prototype" for a function,
134 /// which captures its name, and its argument names (thus implicitly the number
135 /// of arguments the function takes).
136 class PrototypeAST {
137 std::string Name;
138 std::vector Args;
139 public:
140 PrototypeAST(const std::string &name, const std::vector &args)
141 : Name(name), Args(args) {}
142
143 Function *Codegen();
144 };
145
146 /// FunctionAST - This class represents a function definition itself.
147 class FunctionAST {
148 PrototypeAST *Proto;
149 ExprAST *Body;
150 public:
151 FunctionAST(PrototypeAST *proto, ExprAST *body)
152 : Proto(proto), Body(body) {}
153
154 Function *Codegen();
155 };
156
157 //===----------------------------------------------------------------------===//
158 // Parser
159 //===----------------------------------------------------------------------===//
160
161 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
162 /// token the parser is looking at. getNextToken reads another token from the
163 /// lexer and updates CurTok with its results.
164 static int CurTok;
165 static int getNextToken() {
166 return CurTok = gettok();
167 }
168
169 /// BinopPrecedence - This holds the precedence for each binary operator that is
170 /// defined.
171 static std::map BinopPrecedence;
172
173 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
174 static int GetTokPrecedence() {
175 if (!isascii(CurTok))
176 return -1;
177
178 // Make sure it's a declared binop.
179 int TokPrec = BinopPrecedence[CurTok];
180 if (TokPrec <= 0) return -1;
181 return TokPrec;
182 }
183
184 /// Error* - These are little helper functions for error handling.
185 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
186 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
187 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
188
189 static ExprAST *ParseExpression();
190
191 /// identifierexpr
192 /// ::= identifier
193 /// ::= identifier '(' expression* ')'
194 static ExprAST *ParseIdentifierExpr() {
195 std::string IdName = IdentifierStr;
196
197 getNextToken(); // eat identifier.
198
199 if (CurTok != '(') // Simple variable ref.
200 return new VariableExprAST(IdName);
201
202 // Call.
203 getNextToken(); // eat (
204 std::vector Args;
205 if (CurTok != ')') {
206 while (1) {
207 ExprAST *Arg = ParseExpression();
208 if (!Arg) return 0;
209 Args.push_back(Arg);
210
211 if (CurTok == ')') break;
212
213 if (CurTok != ',')
214 return Error("Expected ')' or ',' in argument list");
215 getNextToken();
216 }
217 }
218
219 // Eat the ')'.
220 getNextToken();
221
222 return new CallExprAST(IdName, Args);
223 }
224
225 /// numberexpr ::= number
226 static ExprAST *ParseNumberExpr() {
227 ExprAST *Result = new NumberExprAST(NumVal);
228 getNextToken(); // consume the number
229 return Result;
230 }
231
232 /// parenexpr ::= '(' expression ')'
233 static ExprAST *ParseParenExpr() {
234 getNextToken(); // eat (.
235 ExprAST *V = ParseExpression();
236 if (!V) return 0;
237
238 if (CurTok != ')')
239 return Error("expected ')'");
240 getNextToken(); // eat ).
241 return V;
242 }
243
244 /// primary
245 /// ::= identifierexpr
246 /// ::= numberexpr
247 /// ::= parenexpr
248 static ExprAST *ParsePrimary() {
249 switch (CurTok) {
250 default: return Error("unknown token when expecting an expression");
251 case tok_identifier: return ParseIdentifierExpr();
252 case tok_number: return ParseNumberExpr();
253 case '(': return ParseParenExpr();
254 }
255 }
256
257 /// binoprhs
258 /// ::= ('+' primary)*
259 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
260 // If this is a binop, find its precedence.
261 while (1) {
262 int TokPrec = GetTokPrecedence();
263
264 // If this is a binop that binds at least as tightly as the current binop,
265 // consume it, otherwise we are done.
266 if (TokPrec < ExprPrec)
267 return LHS;
268
269 // Okay, we know this is a binop.
270 int BinOp = CurTok;
271 getNextToken(); // eat binop
272
273 // Parse the primary expression after the binary operator.
274 ExprAST *RHS = ParsePrimary();
275 if (!RHS) return 0;
276
277 // If BinOp binds less tightly with RHS than the operator after RHS, let
278 // the pending operator take RHS as its LHS.
279 int NextPrec = GetTokPrecedence();
280 if (TokPrec < NextPrec) {
281 RHS = ParseBinOpRHS(TokPrec+1, RHS);
282 if (RHS == 0) return 0;
283 }
284
285 // Merge LHS/RHS.
286 LHS = new BinaryExprAST(BinOp, LHS, RHS);
287 }
288 }
289
290 /// expression
291 /// ::= primary binoprhs
292 ///
293 static ExprAST *ParseExpression() {
294 ExprAST *LHS = ParsePrimary();
295 if (!LHS) return 0;
296
297 return ParseBinOpRHS(0, LHS);
298 }
299
300 /// prototype
301 /// ::= id '(' id* ')'
302 static PrototypeAST *ParsePrototype() {
303 if (CurTok != tok_identifier)
304 return ErrorP("Expected function name in prototype");
305
306 std::string FnName = IdentifierStr;
307 getNextToken();
308
309 if (CurTok != '(')
310 return ErrorP("Expected '(' in prototype");
311
312 std::vector ArgNames;
313 while (getNextToken() == tok_identifier)
314 ArgNames.push_back(IdentifierStr);
315 if (CurTok != ')')
316 return ErrorP("Expected ')' in prototype");
317
318 // success.
319 getNextToken(); // eat ')'.
320
321 return new PrototypeAST(FnName, ArgNames);
322 }
323
324 /// definition ::= 'def' prototype expression
325 static FunctionAST *ParseDefinition() {
326 getNextToken(); // eat def.
327 PrototypeAST *Proto = ParsePrototype();
328 if (Proto == 0) return 0;
329
330 if (ExprAST *E = ParseExpression())
331 return new FunctionAST(Proto, E);
332 return 0;
333 }
334
335 /// toplevelexpr ::= expression
336 static FunctionAST *ParseTopLevelExpr() {
337 if (ExprAST *E = ParseExpression()) {
338 // Make an anonymous proto.
339 PrototypeAST *Proto = new PrototypeAST("", std::vector());
340 return new FunctionAST(Proto, E);
341 }
342 return 0;
343 }
344
345 /// external ::= 'extern' prototype
346 static PrototypeAST *ParseExtern() {
347 getNextToken(); // eat extern.
348 return ParsePrototype();
349 }
350
351 //===----------------------------------------------------------------------===//
352 // Code Generation
353 //===----------------------------------------------------------------------===//
354
355 static Module *TheModule;
356 static IRBuilder<> Builder(getGlobalContext());
357 static std::map NamedValues;
358 static FunctionPassManager *TheFPM;
359
360 Value *ErrorV(const char *Str) { Error(Str); return 0; }
361
362 Value *NumberExprAST::Codegen() {
363 return ConstantFP::get(getGlobalContext(), APFloat(Val));
364 }
365
366 Value *VariableExprAST::Codegen() {
367 // Look this variable up in the function.
368 Value *V = NamedValues[Name];
369 return V ? V : ErrorV("Unknown variable name");
370 }
371
372 Value *BinaryExprAST::Codegen() {
373 Value *L = LHS->Codegen();
374 Value *R = RHS->Codegen();
375 if (L == 0 || R == 0) return 0;
376
377 switch (Op) {
378 case '+': return Builder.CreateAdd(L, R, "addtmp");
379 case '-': return Builder.CreateSub(L, R, "subtmp");
380 case '*': return Builder.CreateMul(L, R, "multmp");
381 case '<':
382 L = Builder.CreateFCmpULT(L, R, "cmptmp");
383 // Convert bool 0/1 to double 0.0 or 1.0
384 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
385 "booltmp");
386 default: return ErrorV("invalid binary operator");
387 }
388 }
389
390 Value *CallExprAST::Codegen() {
391 // Look up the name in the global module table.
392 Function *CalleeF = TheModule->getFunction(Callee);
393 if (CalleeF == 0)
394 return ErrorV("Unknown function referenced");
395
396 // If argument mismatch error.
397 if (CalleeF->arg_size() != Args.size())
398 return ErrorV("Incorrect # arguments passed");
399
400 std::vector ArgsV;
401 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
402 ArgsV.push_back(Args[i]->Codegen());
403 if (ArgsV.back() == 0) return 0;
404 }
405
406 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
407 }
408
409 Function *PrototypeAST::Codegen() {
410 // Make the function type: double(double,double) etc.
411 std::vector Doubles(Args.size(),
412 Type::getDoubleTy(getGlobalContext()));
413 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
414 Doubles, false);
415
416 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
417
418 // If F conflicted, there was already something named 'Name'. If it has a
419 // body, don't allow redefinition or reextern.
420 if (F->getName() != Name) {
421 // Delete the one we just made and get the existing one.
422 F->eraseFromParent();
423 F = TheModule->getFunction(Name);
424
425 // If F already has a body, reject this.
426 if (!F->empty()) {
427 ErrorF("redefinition of function");
428 return 0;
429 }
430
431 // If F took a different number of args, reject.
432 if (F->arg_size() != Args.size()) {
433 ErrorF("redefinition of function with different # args");
434 return 0;
435 }
436 }
437
438 // Set names for all arguments.
439 unsigned Idx = 0;
440 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
441 ++AI, ++Idx) {
442 AI->setName(Args[Idx]);
443
444 // Add arguments to variable symbol table.
445 NamedValues[Args[Idx]] = AI;
446 }
447
448 return F;
449 }
450
451 Function *FunctionAST::Codegen() {
452 NamedValues.clear();
453
454 Function *TheFunction = Proto->Codegen();
455 if (TheFunction == 0)
456 return 0;
457
458 // Create a new basic block to start insertion into.
459 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
460 Builder.SetInsertPoint(BB);
461
462 if (Value *RetVal = Body->Codegen()) {
463 // Finish off the function.
464 Builder.CreateRet(RetVal);
465
466 // Validate the generated code, checking for consistency.
467 verifyFunction(*TheFunction);
468
469 // Optimize the function.
470 TheFPM->run(*TheFunction);
471
472 return TheFunction;
473 }
474
475 // Error reading body, remove function.
476 TheFunction->eraseFromParent();
477 return 0;
478 }
479
480 //===----------------------------------------------------------------------===//
481 // Top-Level parsing and JIT Driver
482 //===----------------------------------------------------------------------===//
483
484 static ExecutionEngine *TheExecutionEngine;
485
486 static void HandleDefinition() {
487 if (FunctionAST *F = ParseDefinition()) {
488 if (Function *LF = F->Codegen()) {
489 fprintf(stderr, "Read function definition:");
490 LF->dump();
491 }
492 } else {
493 // Skip token for error recovery.
494 getNextToken();
495 }
496 }
497
498 static void HandleExtern() {
499 if (PrototypeAST *P = ParseExtern()) {
500 if (Function *F = P->Codegen()) {
501 fprintf(stderr, "Read extern: ");
502 F->dump();
503 }
504 } else {
505 // Skip token for error recovery.
506 getNextToken();
507 }
508 }
509
510 static void HandleTopLevelExpression() {
511 // Evaluate a top-level expression into an anonymous function.
512 if (FunctionAST *F = ParseTopLevelExpr()) {
513 if (Function *LF = F->Codegen()) {
514 // JIT the function, returning a function pointer.
515 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
516
517 // Cast it to the right type (takes no arguments, returns a double) so we
518 // can call it as a native function.
519 double (*FP)() = (double (*)())(intptr_t)FPtr;
520 fprintf(stderr, "Evaluated to %f\n", FP());
521 }
522 } else {
523 // Skip token for error recovery.
524 getNextToken();
525 }
526 }
527
528 /// top ::= definition | external | expression | ';'
529 static void MainLoop() {
530 while (1) {
531 fprintf(stderr, "ready> ");
532 switch (CurTok) {
533 case tok_eof: return;
534 case ';': getNextToken(); break; // ignore top-level semicolons.
535 case tok_def: HandleDefinition(); break;
536 case tok_extern: HandleExtern(); break;
537 default: HandleTopLevelExpression(); break;
538 }
539 }
540 }
541
542 //===----------------------------------------------------------------------===//
543 // "Library" functions that can be "extern'd" from user code.
544 //===----------------------------------------------------------------------===//
545
546 /// putchard - putchar that takes a double and returns 0.
547 extern "C"
548 double putchard(double X) {
549 putchar((char)X);
550 return 0;
551 }
552
553 //===----------------------------------------------------------------------===//
554 // Main driver code.
555 //===----------------------------------------------------------------------===//
556
557 int main() {
558 InitializeNativeTarget();
559 LLVMContext &Context = getGlobalContext();
560
561 // Install standard binary operators.
562 // 1 is lowest precedence.
563 BinopPrecedence['<'] = 10;
564 BinopPrecedence['+'] = 20;
565 BinopPrecedence['-'] = 20;
566 BinopPrecedence['*'] = 40; // highest.
567
568 // Prime the first token.
569 fprintf(stderr, "ready> ");
570 getNextToken();
571
572 // Make the module, which holds all the code.
573 TheModule = new Module("my cool jit", Context);
574
575 ExistingModuleProvider *OurModuleProvider =
576 new ExistingModuleProvider(TheModule);
577
578 // Create the JIT. This takes ownership of the module and module provider.
579 TheExecutionEngine = EngineBuilder(OurModuleProvider).create();
580
581 FunctionPassManager OurFPM(OurModuleProvider);
582
583 // Set up the optimizer pipeline. Start with registering info about how the
584 // target lays out data structures.
585 OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
586 // Do simple "peephole" optimizations and bit-twiddling optzns.
587 OurFPM.add(createInstructionCombiningPass());
588 // Reassociate expressions.
589 OurFPM.add(createReassociatePass());
590 // Eliminate Common SubExpressions.
591 OurFPM.add(createGVNPass());
592 // Simplify the control flow graph (deleting unreachable blocks, etc).
593 OurFPM.add(createCFGSimplificationPass());
594
595 OurFPM.doInitialization();
596
597 // Set the global so the code gen can use this.
598 TheFPM = &OurFPM;
599
600 // Run the main "interpreter loop" now.
601 MainLoop();
602
603 TheFPM = 0;
604
605 // Print out all of the generated code.
606 TheModule->dump();
607
608 return 0;
609 }
0 set(LLVM_LINK_COMPONENTS core jit interpreter native)
1
2 add_llvm_example(Kaleidoscope-Ch5
3 toy.cpp
4 )
0 ##===- examples/Kaleidoscope/Chapter5/Makefile -------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 LEVEL = ../../..
9 TOOLNAME = Kaleidoscope-Ch5
10 EXAMPLE_TOOL = 1
11
12 LINK_COMPONENTS := core jit interpreter native
13
14 include $(LEVEL)/Makefile.common
0 #include "llvm/DerivedTypes.h"
1 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2 #include "llvm/ExecutionEngine/Interpreter.h"
3 #include "llvm/ExecutionEngine/JIT.h"
4 #include "llvm/LLVMContext.h"
5 #include "llvm/Module.h"
6 #include "llvm/ModuleProvider.h"
7 #include "llvm/PassManager.h"
8 #include "llvm/Analysis/Verifier.h"
9 #include "llvm/Target/TargetData.h"
10 #include "llvm/Target/TargetSelect.h"
11 #include "llvm/Transforms/Scalar.h"
12 #include "llvm/Support/IRBuilder.h"
13 #include
14 #include
15 #include
16 #include
17 using namespace llvm;
18
19 //===----------------------------------------------------------------------===//
20 // Lexer
21 //===----------------------------------------------------------------------===//
22
23 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
24 // of these for known things.
25 enum Token {
26 tok_eof = -1,
27
28 // commands
29 tok_def = -2, tok_extern = -3,
30
31 // primary
32 tok_identifier = -4, tok_number = -5,
33
34 // control
35 tok_if = -6, tok_then = -7, tok_else = -8,
36 tok_for = -9, tok_in = -10
37 };
38
39 static std::string IdentifierStr; // Filled in if tok_identifier
40 static double NumVal; // Filled in if tok_number
41
42 /// gettok - Return the next token from standard input.
43 static int gettok() {
44 static int LastChar = ' ';
45
46 // Skip any whitespace.
47 while (isspace(LastChar))
48 LastChar = getchar();
49
50 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
51 IdentifierStr = LastChar;
52 while (isalnum((LastChar = getchar())))
53 IdentifierStr += LastChar;
54
55 if (IdentifierStr == "def") return tok_def;
56 if (IdentifierStr == "extern") return tok_extern;
57 if (IdentifierStr == "if") return tok_if;
58 if (IdentifierStr == "then") return tok_then;
59 if (IdentifierStr == "else") return tok_else;
60 if (IdentifierStr == "for") return tok_for;
61 if (IdentifierStr == "in") return tok_in;
62 return tok_identifier;
63 }
64
65 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
66 std::string NumStr;
67 do {
68 NumStr += LastChar;
69 LastChar = getchar();
70 } while (isdigit(LastChar) || LastChar == '.');
71
72 NumVal = strtod(NumStr.c_str(), 0);
73 return tok_number;
74 }
75
76 if (LastChar == '#') {
77 // Comment until end of line.
78 do LastChar = getchar();
79 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
80
81 if (LastChar != EOF)
82 return gettok();
83 }
84
85 // Check for end of file. Don't eat the EOF.
86 if (LastChar == EOF)
87 return tok_eof;
88
89 // Otherwise, just return the character as its ascii value.
90 int ThisChar = LastChar;
91 LastChar = getchar();
92 return ThisChar;
93 }
94
95 //===----------------------------------------------------------------------===//
96 // Abstract Syntax Tree (aka Parse Tree)
97 //===----------------------------------------------------------------------===//
98
99 /// ExprAST - Base class for all expression nodes.
100 class ExprAST {
101 public:
102 virtual ~ExprAST() {}
103 virtual Value *Codegen() = 0;
104 };
105
106 /// NumberExprAST - Expression class for numeric literals like "1.0".
107 class NumberExprAST : public ExprAST {
108 double Val;
109 public:
110 NumberExprAST(double val) : Val(val) {}
111 virtual Value *Codegen();
112 };
113
114 /// VariableExprAST - Expression class for referencing a variable, like "a".
115 class VariableExprAST : public ExprAST {
116 std::string Name;
117 public:
118 VariableExprAST(const std::string &name) : Name(name) {}
119 virtual Value *Codegen();
120 };
121
122 /// BinaryExprAST - Expression class for a binary operator.
123 class BinaryExprAST : public ExprAST {
124 char Op;
125 ExprAST *LHS, *RHS;
126 public:
127 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
128 : Op(op), LHS(lhs), RHS(rhs) {}
129 virtual Value *Codegen();
130 };
131
132 /// CallExprAST - Expression class for function calls.
133 class CallExprAST : public ExprAST {
134 std::string Callee;
135 std::vector Args;
136 public:
137 CallExprAST(const std::string &callee, std::vector &args)
138 : Callee(callee), Args(args) {}
139 virtual Value *Codegen();
140 };
141
142 /// IfExprAST - Expression class for if/then/else.
143 class IfExprAST : public ExprAST {
144 ExprAST *Cond, *Then, *Else;
145 public:
146 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
147 : Cond(cond), Then(then), Else(_else) {}
148 virtual Value *Codegen();
149 };
150
151 /// ForExprAST - Expression class for for/in.
152 class ForExprAST : public ExprAST {
153 std::string VarName;
154 ExprAST *Start, *End, *Step, *Body;
155 public:
156 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
157 ExprAST *step, ExprAST *body)
158 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
159 virtual Value *Codegen();
160 };
161
162 /// PrototypeAST - This class represents the "prototype" for a function,
163 /// which captures its name, and its argument names (thus implicitly the number
164 /// of arguments the function takes).
165 class PrototypeAST {
166 std::string Name;
167 std::vector Args;
168 public:
169 PrototypeAST(const std::string &name, const std::vector &args)
170 : Name(name), Args(args) {}
171
172 Function *Codegen();
173 };
174
175 /// FunctionAST - This class represents a function definition itself.
176 class FunctionAST {
177 PrototypeAST *Proto;
178 ExprAST *Body;
179 public:
180 FunctionAST(PrototypeAST *proto, ExprAST *body)
181 : Proto(proto), Body(body) {}
182
183 Function *Codegen();
184 };
185
186 //===----------------------------------------------------------------------===//
187 // Parser
188 //===----------------------------------------------------------------------===//
189
190 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
191 /// token the parser is looking at. getNextToken reads another token from the
192 /// lexer and updates CurTok with its results.
193 static int CurTok;
194 static int getNextToken() {
195 return CurTok = gettok();
196 }
197
198 /// BinopPrecedence - This holds the precedence for each binary operator that is
199 /// defined.
200 static std::map BinopPrecedence;
201
202 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
203 static int GetTokPrecedence() {
204 if (!isascii(CurTok))
205 return -1;
206
207 // Make sure it's a declared binop.
208 int TokPrec = BinopPrecedence[CurTok];
209 if (TokPrec <= 0) return -1;
210 return TokPrec;
211 }
212
213 /// Error* - These are little helper functions for error handling.
214 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
215 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
216 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
217
218 static ExprAST *ParseExpression();
219
220 /// identifierexpr
221 /// ::= identifier
222 /// ::= identifier '(' expression* ')'
223 static ExprAST *ParseIdentifierExpr() {
224 std::string IdName = IdentifierStr;
225
226 getNextToken(); // eat identifier.
227
228 if (CurTok != '(') // Simple variable ref.
229 return new VariableExprAST(IdName);
230
231 // Call.
232 getNextToken(); // eat (
233 std::vector Args;
234 if (CurTok != ')') {
235 while (1) {
236 ExprAST *Arg = ParseExpression();
237 if (!Arg) return 0;
238 Args.push_back(Arg);
239
240 if (CurTok == ')') break;
241
242 if (CurTok != ',')
243 return Error("Expected ')' or ',' in argument list");
244 getNextToken();
245 }
246 }
247
248 // Eat the ')'.
249 getNextToken();
250
251 return new CallExprAST(IdName, Args);
252 }
253
254 /// numberexpr ::= number
255 static ExprAST *ParseNumberExpr() {
256 ExprAST *Result = new NumberExprAST(NumVal);
257 getNextToken(); // consume the number
258 return Result;
259 }
260
261 /// parenexpr ::= '(' expression ')'
262 static ExprAST *ParseParenExpr() {
263 getNextToken(); // eat (.
264 ExprAST *V = ParseExpression();
265 if (!V) return 0;
266
267 if (CurTok != ')')
268 return Error("expected ')'");
269 getNextToken(); // eat ).
270 return V;
271 }
272
273 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
274 static ExprAST *ParseIfExpr() {
275 getNextToken(); // eat the if.
276
277 // condition.
278 ExprAST *Cond = ParseExpression();
279 if (!Cond) return 0;
280
281 if (CurTok != tok_then)
282 return Error("expected then");
283 getNextToken(); // eat the then
284
285 ExprAST *Then = ParseExpression();
286 if (Then == 0) return 0;
287
288 if (CurTok != tok_else)
289 return Error("expected else");
290
291 getNextToken();
292
293 ExprAST *Else = ParseExpression();
294 if (!Else) return 0;
295
296 return new IfExprAST(Cond, Then, Else);
297 }
298
299 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
300 static ExprAST *ParseForExpr() {
301 getNextToken(); // eat the for.
302
303 if (CurTok != tok_identifier)
304 return Error("expected identifier after for");
305
306 std::string IdName = IdentifierStr;
307 getNextToken(); // eat identifier.
308
309 if (CurTok != '=')
310 return Error("expected '=' after for");
311 getNextToken(); // eat '='.
312
313
314 ExprAST *Start = ParseExpression();
315 if (Start == 0) return 0;
316 if (CurTok != ',')
317 return Error("expected ',' after for start value");
318 getNextToken();
319
320 ExprAST *End = ParseExpression();
321 if (End == 0) return 0;
322
323 // The step value is optional.
324 ExprAST *Step = 0;
325 if (CurTok == ',') {
326 getNextToken();
327 Step = ParseExpression();
328 if (Step == 0) return 0;
329 }
330
331 if (CurTok != tok_in)
332 return Error("expected 'in' after for");
333 getNextToken(); // eat 'in'.
334
335 ExprAST *Body = ParseExpression();
336 if (Body == 0) return 0;
337
338 return new ForExprAST(IdName, Start, End, Step, Body);
339 }
340
341 /// primary
342 /// ::= identifierexpr
343 /// ::= numberexpr
344 /// ::= parenexpr
345 /// ::= ifexpr
346 /// ::= forexpr
347 static ExprAST *ParsePrimary() {
348 switch (CurTok) {
349 default: return Error("unknown token when expecting an expression");
350 case tok_identifier: return ParseIdentifierExpr();
351 case tok_number: return ParseNumberExpr();
352 case '(': return ParseParenExpr();
353 case tok_if: return ParseIfExpr();
354 case tok_for: return ParseForExpr();
355 }
356 }
357
358 /// binoprhs
359 /// ::= ('+' primary)*
360 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
361 // If this is a binop, find its precedence.
362 while (1) {
363 int TokPrec = GetTokPrecedence();
364
365 // If this is a binop that binds at least as tightly as the current binop,
366 // consume it, otherwise we are done.
367 if (TokPrec < ExprPrec)
368 return LHS;
369
370 // Okay, we know this is a binop.
371 int BinOp = CurTok;
372 getNextToken(); // eat binop
373
374 // Parse the primary expression after the binary operator.
375 ExprAST *RHS = ParsePrimary();
376 if (!RHS) return 0;
377
378 // If BinOp binds less tightly with RHS than the operator after RHS, let
379 // the pending operator take RHS as its LHS.
380 int NextPrec = GetTokPrecedence();
381 if (TokPrec < NextPrec) {
382 RHS = ParseBinOpRHS(TokPrec+1, RHS);
383 if (RHS == 0) return 0;
384 }
385
386 // Merge LHS/RHS.
387 LHS = new BinaryExprAST(BinOp, LHS, RHS);
388 }
389 }
390
391 /// expression
392 /// ::= primary binoprhs
393 ///
394 static ExprAST *ParseExpression() {
395 ExprAST *LHS = ParsePrimary();
396 if (!LHS) return 0;
397
398 return ParseBinOpRHS(0, LHS);
399 }
400
401 /// prototype
402 /// ::= id '(' id* ')'
403 static PrototypeAST *ParsePrototype() {
404 if (CurTok != tok_identifier)
405 return ErrorP("Expected function name in prototype");
406
407 std::string FnName = IdentifierStr;
408 getNextToken();
409
410 if (CurTok != '(')
411 return ErrorP("Expected '(' in prototype");
412
413 std::vector ArgNames;
414 while (getNextToken() == tok_identifier)
415 ArgNames.push_back(IdentifierStr);
416 if (CurTok != ')')
417 return ErrorP("Expected ')' in prototype");
418
419 // success.
420 getNextToken(); // eat ')'.
421
422 return new PrototypeAST(FnName, ArgNames);
423 }
424
425 /// definition ::= 'def' prototype expression
426 static FunctionAST *ParseDefinition() {
427 getNextToken(); // eat def.
428 PrototypeAST *Proto = ParsePrototype();
429 if (Proto == 0) return 0;
430
431 if (ExprAST *E = ParseExpression())
432 return new FunctionAST(Proto, E);
433 return 0;
434 }
435
436 /// toplevelexpr ::= expression
437 static FunctionAST *ParseTopLevelExpr() {
438 if (ExprAST *E = ParseExpression()) {
439 // Make an anonymous proto.
440 PrototypeAST *Proto = new PrototypeAST("", std::vector());
441 return new FunctionAST(Proto, E);
442 }
443 return 0;
444 }
445
446 /// external ::= 'extern' prototype
447 static PrototypeAST *ParseExtern() {
448 getNextToken(); // eat extern.
449 return ParsePrototype();
450 }
451
452 //===----------------------------------------------------------------------===//
453 // Code Generation
454 //===----------------------------------------------------------------------===//
455
456 static Module *TheModule;
457 static IRBuilder<> Builder(getGlobalContext());
458 static std::map NamedValues;
459 static FunctionPassManager *TheFPM;
460
461 Value *ErrorV(const char *Str) { Error(Str); return 0; }
462
463 Value *NumberExprAST::Codegen() {
464 return ConstantFP::get(getGlobalContext(), APFloat(Val));
465 }
466
467 Value *VariableExprAST::Codegen() {
468 // Look this variable up in the function.
469 Value *V = NamedValues[Name];
470 return V ? V : ErrorV("Unknown variable name");
471 }
472
473 Value *BinaryExprAST::Codegen() {
474 Value *L = LHS->Codegen();
475 Value *R = RHS->Codegen();
476 if (L == 0 || R == 0) return 0;
477
478 switch (Op) {
479 case '+': return Builder.CreateAdd(L, R, "addtmp");
480 case '-': return Builder.CreateSub(L, R, "subtmp");
481 case '*': return Builder.CreateMul(L, R, "multmp");
482 case '<':
483 L = Builder.CreateFCmpULT(L, R, "cmptmp");
484 // Convert bool 0/1 to double 0.0 or 1.0
485 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
486 "booltmp");
487 default: return ErrorV("invalid binary operator");
488 }
489 }
490
491 Value *CallExprAST::Codegen() {
492 // Look up the name in the global module table.
493 Function *CalleeF = TheModule->getFunction(Callee);
494 if (CalleeF == 0)
495 return ErrorV("Unknown function referenced");
496
497 // If argument mismatch error.
498 if (CalleeF->arg_size() != Args.size())
499 return ErrorV("Incorrect # arguments passed");
500
501 std::vector ArgsV;
502 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
503 ArgsV.push_back(Args[i]->Codegen());
504 if (ArgsV.back() == 0) return 0;
505 }
506
507 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
508 }
509
510 Value *IfExprAST::Codegen() {
511 Value *CondV = Cond->Codegen();
512 if (CondV == 0) return 0;
513
514 // Convert condition to a bool by comparing equal to 0.0.
515 CondV = Builder.CreateFCmpONE(CondV,
516 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
517 "ifcond");
518
519 Function *TheFunction = Builder.GetInsertBlock()->getParent();
520
521 // Create blocks for the then and else cases. Insert the 'then' block at the
522 // end of the function.
523 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
524 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
525 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
526
527 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
528
529 // Emit then value.
530 Builder.SetInsertPoint(ThenBB);
531
532 Value *ThenV = Then->Codegen();
533 if (ThenV == 0) return 0;
534
535 Builder.CreateBr(MergeBB);
536 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
537 ThenBB = Builder.GetInsertBlock();
538
539 // Emit else block.
540 TheFunction->getBasicBlockList().push_back(ElseBB);
541 Builder.SetInsertPoint(ElseBB);
542
543 Value *ElseV = Else->Codegen();
544 if (ElseV == 0) return 0;
545
546 Builder.CreateBr(MergeBB);
547 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
548 ElseBB = Builder.GetInsertBlock();
549
550 // Emit merge block.
551 TheFunction->getBasicBlockList().push_back(MergeBB);
552 Builder.SetInsertPoint(MergeBB);
553 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
554 "iftmp");
555
556 PN->addIncoming(ThenV, ThenBB);
557 PN->addIncoming(ElseV, ElseBB);
558 return PN;
559 }
560
561 Value *ForExprAST::Codegen() {
562 // Output this as:
563 // ...
564 // start = startexpr
565 // goto loop
566 // loop:
567 // variable = phi [start, loopheader], [nextvariable, loopend]
568 // ...
569 // bodyexpr
570 // ...
571 // loopend:
572 // step = stepexpr
573 // nextvariable = variable + step
574 // endcond = endexpr
575 // br endcond, loop, endloop
576 // outloop:
577
578 // Emit the start code first, without 'variable' in scope.
579 Value *StartVal = Start->Codegen();
580 if (StartVal == 0) return 0;
581
582 // Make the new basic block for the loop header, inserting after current
583 // block.
584 Function *TheFunction = Builder.GetInsertBlock()->getParent();
585 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
586 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
587
588 // Insert an explicit fall through from the current block to the LoopBB.
589 Builder.CreateBr(LoopBB);
590
591 // Start insertion in LoopBB.
592 Builder.SetInsertPoint(LoopBB);
593
594 // Start the PHI node with an entry for Start.
595 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), VarName.c_str());
596 Variable->addIncoming(StartVal, PreheaderBB);
597
598 // Within the loop, the variable is defined equal to the PHI node. If it
599 // shadows an existing variable, we have to restore it, so save it now.
600 Value *OldVal = NamedValues[VarName];
601 NamedValues[VarName] = Variable;
602
603 // Emit the body of the loop. This, like any other expr, can change the
604 // current BB. Note that we ignore the value computed by the body, but don't
605 // allow an error.
606 if (Body->Codegen() == 0)
607 return 0;
608
609 // Emit the step value.
610 Value *StepVal;
611 if (Step) {
612 StepVal = Step->Codegen();
613 if (StepVal == 0) return 0;
614 } else {
615 // If not specified, use 1.0.
616 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
617 }
618
619 Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
620
621 // Compute the end condition.
622 Value *EndCond = End->Codegen();
623 if (EndCond == 0) return EndCond;
624
625 // Convert condition to a bool by comparing equal to 0.0.
626 EndCond = Builder.CreateFCmpONE(EndCond,
627 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
628 "loopcond");
629
630 // Create the "after loop" block and insert it.
631 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
632 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
633
634 // Insert the conditional branch into the end of LoopEndBB.
635 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
636
637 // Any new code will be inserted in AfterBB.
638 Builder.SetInsertPoint(AfterBB);
639
640 // Add a new entry to the PHI node for the backedge.
641 Variable->addIncoming(NextVar, LoopEndBB);
642
643 // Restore the unshadowed variable.
644 if (OldVal)
645 NamedValues[VarName] = OldVal;
646 else
647 NamedValues.erase(VarName);
648
649
650 // for expr always returns 0.0.
651 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
652 }
653
654 Function *PrototypeAST::Codegen() {
655 // Make the function type: double(double,double) etc.
656 std::vector Doubles(Args.size(),
657 Type::getDoubleTy(getGlobalContext()));
658 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
659 Doubles, false);
660
661 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
662
663 // If F conflicted, there was already something named 'Name'. If it has a
664 // body, don't allow redefinition or reextern.
665 if (F->getName() != Name) {
666 // Delete the one we just made and get the existing one.
667 F->eraseFromParent();
668 F = TheModule->getFunction(Name);
669
670 // If F already has a body, reject this.
671 if (!F->empty()) {
672 ErrorF("redefinition of function");
673 return 0;
674 }
675
676 // If F took a different number of args, reject.
677 if (F->arg_size() != Args.size()) {
678 ErrorF("redefinition of function with different # args");
679 return 0;
680 }
681 }
682
683 // Set names for all arguments.
684 unsigned Idx = 0;
685 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
686 ++AI, ++Idx) {
687 AI->setName(Args[Idx]);
688
689 // Add arguments to variable symbol table.
690 NamedValues[Args[Idx]] = AI;
691 }
692
693 return F;
694 }
695
696 Function *FunctionAST::Codegen() {
697 NamedValues.clear();
698
699 Function *TheFunction = Proto->Codegen();
700 if (TheFunction == 0)
701 return 0;
702
703 // Create a new basic block to start insertion into.
704 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
705 Builder.SetInsertPoint(BB);
706
707 if (Value *RetVal = Body->Codegen()) {
708 // Finish off the function.
709 Builder.CreateRet(RetVal);
710
711 // Validate the generated code, checking for consistency.
712 verifyFunction(*TheFunction);
713
714 // Optimize the function.
715 TheFPM->run(*TheFunction);
716
717 return TheFunction;
718 }
719
720 // Error reading body, remove function.
721 TheFunction->eraseFromParent();
722 return 0;
723 }
724
725 //===----------------------------------------------------------------------===//
726 // Top-Level parsing and JIT Driver
727 //===----------------------------------------------------------------------===//
728
729 static ExecutionEngine *TheExecutionEngine;
730
731 static void HandleDefinition() {
732 if (FunctionAST *F = ParseDefinition()) {
733 if (Function *LF = F->Codegen()) {
734 fprintf(stderr, "Read function definition:");
735 LF->dump();
736 }
737 } else {
738 // Skip token for error recovery.
739 getNextToken();
740 }
741 }
742
743 static void HandleExtern() {
744 if (PrototypeAST *P = ParseExtern()) {
745 if (Function *F = P->Codegen()) {
746 fprintf(stderr, "Read extern: ");
747 F->dump();
748 }
749 } else {
750 // Skip token for error recovery.
751 getNextToken();
752 }
753 }
754
755 static void HandleTopLevelExpression() {
756 // Evaluate a top-level expression into an anonymous function.
757 if (FunctionAST *F = ParseTopLevelExpr()) {
758 if (Function *LF = F->Codegen()) {
759 // JIT the function, returning a function pointer.
760 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
761
762 // Cast it to the right type (takes no arguments, returns a double) so we
763 // can call it as a native function.
764 double (*FP)() = (double (*)())(intptr_t)FPtr;
765 fprintf(stderr, "Evaluated to %f\n", FP());
766 }
767 } else {
768 // Skip token for error recovery.
769 getNextToken();
770 }
771 }
772
773 /// top ::= definition | external | expression | ';'
774 static void MainLoop() {
775 while (1) {
776 fprintf(stderr, "ready> ");
777 switch (CurTok) {
778 case tok_eof: return;
779 case ';': getNextToken(); break; // ignore top-level semicolons.
780 case tok_def: HandleDefinition(); break;
781 case tok_extern: HandleExtern(); break;
782 default: HandleTopLevelExpression(); break;
783 }
784 }
785 }
786
787 //===----------------------------------------------------------------------===//
788 // "Library" functions that can be "extern'd" from user code.
789 //===----------------------------------------------------------------------===//
790
791 /// putchard - putchar that takes a double and returns 0.
792 extern "C"
793 double putchard(double X) {
794 putchar((char)X);
795 return 0;
796 }
797
798 //===----------------------------------------------------------------------===//
799 // Main driver code.
800 //===----------------------------------------------------------------------===//
801
802 int main() {
803 InitializeNativeTarget();
804 LLVMContext &Context = getGlobalContext();
805
806 // Install standard binary operators.
807 // 1 is lowest precedence.
808 BinopPrecedence['<'] = 10;
809 BinopPrecedence['+'] = 20;
810 BinopPrecedence['-'] = 20;
811 BinopPrecedence['*'] = 40; // highest.
812
813 // Prime the first token.
814 fprintf(stderr, "ready> ");
815 getNextToken();
816
817 // Make the module, which holds all the code.
818 TheModule = new Module("my cool jit", Context);
819
820 ExistingModuleProvider *OurModuleProvider =
821 new ExistingModuleProvider(TheModule);
822
823 // Create the JIT. This takes ownership of the module and module provider.
824 TheExecutionEngine = EngineBuilder(OurModuleProvider).create();
825
826 FunctionPassManager OurFPM(OurModuleProvider);
827
828 // Set up the optimizer pipeline. Start with registering info about how the
829 // target lays out data structures.
830 OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
831 // Do simple "peephole" optimizations and bit-twiddling optzns.
832 OurFPM.add(createInstructionCombiningPass());
833 // Reassociate expressions.
834 OurFPM.add(createReassociatePass());
835 // Eliminate Common SubExpressions.
836 OurFPM.add(createGVNPass());
837 // Simplify the control flow graph (deleting unreachable blocks, etc).
838 OurFPM.add(createCFGSimplificationPass());
839
840 OurFPM.doInitialization();
841
842 // Set the global so the code gen can use this.
843 TheFPM = &OurFPM;
844
845 // Run the main "interpreter loop" now.
846 MainLoop();
847
848 TheFPM = 0;
849
850 // Print out all of the generated code.
851 TheModule->dump();
852
853 return 0;
854 }
0 set(LLVM_LINK_COMPONENTS core jit interpreter native)
1
2 add_llvm_example(Kaleidoscope-Ch6
3 toy.cpp
4 )
0 ##===- examples/Kaleidoscope/Chapter6/Makefile -------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 LEVEL = ../../..
9 TOOLNAME = Kaleidoscope-Ch6
10 EXAMPLE_TOOL = 1
11
12 LINK_COMPONENTS := core jit interpreter native
13
14 include $(LEVEL)/Makefile.common
0 #include "llvm/DerivedTypes.h"
1 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2 #include "llvm/ExecutionEngine/Interpreter.h"
3 #include "llvm/ExecutionEngine/JIT.h"
4 #include "llvm/LLVMContext.h"
5 #include "llvm/Module.h"
6 #include "llvm/ModuleProvider.h"
7 #include "llvm/PassManager.h"
8 #include "llvm/Analysis/Verifier.h"
9 #include "llvm/Target/TargetData.h"
10 #include "llvm/Target/TargetSelect.h"
11 #include "llvm/Transforms/Scalar.h"
12 #include "llvm/Support/IRBuilder.h"
13 #include
14 #include
15 #include
16 #include
17 using namespace llvm;
18
19 //===----------------------------------------------------------------------===//
20 // Lexer
21 //===----------------------------------------------------------------------===//
22
23 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
24 // of these for known things.
25 enum Token {
26 tok_eof = -1,
27
28 // commands
29 tok_def = -2, tok_extern = -3,
30
31 // primary
32 tok_identifier = -4, tok_number = -5,
33
34 // control
35 tok_if = -6, tok_then = -7, tok_else = -8,
36 tok_for = -9, tok_in = -10,
37
38 // operators
39 tok_binary = -11, tok_unary = -12
40 };
41
42 static std::string IdentifierStr; // Filled in if tok_identifier
43 static double NumVal; // Filled in if tok_number
44
45 /// gettok - Return the next token from standard input.
46 static int gettok() {
47 static int LastChar = ' ';
48
49 // Skip any whitespace.
50 while (isspace(LastChar))
51 LastChar = getchar();
52
53 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
54 IdentifierStr = LastChar;
55 while (isalnum((LastChar = getchar())))
56 IdentifierStr += LastChar;
57
58 if (IdentifierStr == "def") return tok_def;
59 if (IdentifierStr == "extern") return tok_extern;
60 if (IdentifierStr == "if") return tok_if;
61 if (IdentifierStr == "then") return tok_then;
62 if (IdentifierStr == "else") return tok_else;
63 if (IdentifierStr == "for") return tok_for;
64 if (IdentifierStr == "in") return tok_in;
65 if (IdentifierStr == "binary") return tok_binary;
66 if (IdentifierStr == "unary") return tok_unary;
67 return tok_identifier;
68 }
69
70 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
71 std::string NumStr;
72 do {
73 NumStr += LastChar;
74 LastChar = getchar();
75 } while (isdigit(LastChar) || LastChar == '.');
76
77 NumVal = strtod(NumStr.c_str(), 0);
78 return tok_number;
79 }
80
81 if (LastChar == '#') {
82 // Comment until end of line.
83 do LastChar = getchar();
84 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
85
86 if (LastChar != EOF)
87 return gettok();
88 }
89
90 // Check for end of file. Don't eat the EOF.
91 if (LastChar == EOF)
92 return tok_eof;
93
94 // Otherwise, just return the character as its ascii value.
95 int ThisChar = LastChar;
96 LastChar = getchar();
97 return ThisChar;
98 }
99
100 //===----------------------------------------------------------------------===//
101 // Abstract Syntax Tree (aka Parse Tree)
102 //===----------------------------------------------------------------------===//
103
104 /// ExprAST - Base class for all expression nodes.
105 class ExprAST {
106 public:
107 virtual ~ExprAST() {}
108 virtual Value *Codegen() = 0;
109 };
110
111 /// NumberExprAST - Expression class for numeric literals like "1.0".
112 class NumberExprAST : public ExprAST {
113 double Val;
114 public:
115 NumberExprAST(double val) : Val(val) {}
116 virtual Value *Codegen();
117 };
118
119 /// VariableExprAST - Expression class for referencing a variable, like "a".
120 class VariableExprAST : public ExprAST {
121 std::string Name;
122 public:
123 VariableExprAST(const std::string &name) : Name(name) {}
124 virtual Value *Codegen();
125 };
126
127 /// UnaryExprAST - Expression class for a unary operator.
128 class UnaryExprAST : public ExprAST {
129 char Opcode;
130 ExprAST *Operand;
131 public:
132 UnaryExprAST(char opcode, ExprAST *operand)
133 : Opcode(opcode), Operand(operand) {}
134 virtual Value *Codegen();
135 };
136
137 /// BinaryExprAST - Expression class for a binary operator.
138 class BinaryExprAST : public ExprAST {
139 char Op;
140 ExprAST *LHS, *RHS;
141 public:
142 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
143 : Op(op), LHS(lhs), RHS(rhs) {}
144 virtual Value *Codegen();
145 };
146
147 /// CallExprAST - Expression class for function calls.
148 class CallExprAST : public ExprAST {
149 std::string Callee;
150 std::vector Args;
151 public:
152 CallExprAST(const std::string &callee, std::vector &args)
153 : Callee(callee), Args(args) {}
154 virtual Value *Codegen();
155 };
156
157 /// IfExprAST - Expression class for if/then/else.
158 class IfExprAST : public ExprAST {
159 ExprAST *Cond, *Then, *Else;
160 public:
161 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
162 : Cond(cond), Then(then), Else(_else) {}
163 virtual Value *Codegen();
164 };
165
166 /// ForExprAST - Expression class for for/in.
167 class ForExprAST : public ExprAST {
168 std::string VarName;
169 ExprAST *Start, *End, *Step, *Body;
170 public:
171 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
172 ExprAST *step, ExprAST *body)
173 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
174 virtual Value *Codegen();
175 };
176
177 /// PrototypeAST - This class represents the "prototype" for a function,
178 /// which captures its name, and its argument names (thus implicitly the number
179 /// of arguments the function takes), as well as if it is an operator.
180 class PrototypeAST {
181 std::string Name;
182 std::vector Args;
183 bool isOperator;
184 unsigned Precedence; // Precedence if a binary op.
185 public:
186 PrototypeAST(const std::string &name, const std::vector &args,
187 bool isoperator = false, unsigned prec = 0)
188 : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
189
190 bool isUnaryOp() const { return isOperator && Args.size() == 1; }
191 bool isBinaryOp() const { return isOperator && Args.size() == 2; }
192
193 char getOperatorName() const {
194 assert(isUnaryOp() || isBinaryOp());
195 return Name[Name.size()-1];
196 }
197
198 unsigned getBinaryPrecedence() const { return Precedence; }
199
200 Function *Codegen();
201 };
202
203 /// FunctionAST - This class represents a function definition itself.
204 class FunctionAST {
205 PrototypeAST *Proto;
206 ExprAST *Body;
207 public:
208 FunctionAST(PrototypeAST *proto, ExprAST *body)
209 : Proto(proto), Body(body) {}
210
211 Function *Codegen();
212 };
213
214 //===----------------------------------------------------------------------===//
215 // Parser
216 //===----------------------------------------------------------------------===//
217
218 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
219 /// token the parser is looking at. getNextToken reads another token from the
220 /// lexer and updates CurTok with its results.
221 static int CurTok;
222 static int getNextToken() {
223 return CurTok = gettok();
224 }
225
226 /// BinopPrecedence - This holds the precedence for each binary operator that is
227 /// defined.
228 static std::map BinopPrecedence;
229
230 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
231 static int GetTokPrecedence() {
232 if (!isascii(CurTok))
233 return -1;
234
235 // Make sure it's a declared binop.
236 int TokPrec = BinopPrecedence[CurTok];
237 if (TokPrec <= 0) return -1;
238 return TokPrec;
239 }
240
241 /// Error* - These are little helper functions for error handling.
242 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
243 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
244 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
245
246 static ExprAST *ParseExpression();
247
248 /// identifierexpr
249 /// ::= identifier
250 /// ::= identifier '(' expression* ')'
251 static ExprAST *ParseIdentifierExpr() {
252 std::string IdName = IdentifierStr;
253
254 getNextToken(); // eat identifier.
255
256 if (CurTok != '(') // Simple variable ref.
257 return new VariableExprAST(IdName);
258
259 // Call.
260 getNextToken(); // eat (
261 std::vector Args;
262 if (CurTok != ')') {
263 while (1) {
264 ExprAST *Arg = ParseExpression();
265 if (!Arg) return 0;
266 Args.push_back(Arg);
267
268 if (CurTok == ')') break;
269
270 if (CurTok != ',')
271 return Error("Expected ')' or ',' in argument list");
272 getNextToken();
273 }
274 }
275
276 // Eat the ')'.
277 getNextToken();
278
279 return new CallExprAST(IdName, Args);
280 }
281
282 /// numberexpr ::= number
283 static ExprAST *ParseNumberExpr() {
284 ExprAST *Result = new NumberExprAST(NumVal);
285 getNextToken(); // consume the number
286 return Result;
287 }
288
289 /// parenexpr ::= '(' expression ')'
290 static ExprAST *ParseParenExpr() {
291 getNextToken(); // eat (.
292 ExprAST *V = ParseExpression();
293 if (!V) return 0;
294
295 if (CurTok != ')')
296 return Error("expected ')'");
297 getNextToken(); // eat ).
298 return V;
299 }
300
301 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
302 static ExprAST *ParseIfExpr() {
303 getNextToken(); // eat the if.
304
305 // condition.
306 ExprAST *Cond = ParseExpression();
307 if (!Cond) return 0;
308
309 if (CurTok != tok_then)
310 return Error("expected then");
311 getNextToken(); // eat the then
312
313 ExprAST *Then = ParseExpression();
314 if (Then == 0) return 0;
315
316 if (CurTok != tok_else)
317 return Error("expected else");
318
319 getNextToken();
320
321 ExprAST *Else = ParseExpression();
322 if (!Else) return 0;
323
324 return new IfExprAST(Cond, Then, Else);
325 }
326
327 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
328 static ExprAST *ParseForExpr() {
329 getNextToken(); // eat the for.
330
331 if (CurTok != tok_identifier)
332 return Error("expected identifier after for");
333
334 std::string IdName = IdentifierStr;
335 getNextToken(); // eat identifier.
336
337 if (CurTok != '=')
338 return Error("expected '=' after for");
339 getNextToken(); // eat '='.
340
341
342 ExprAST *Start = ParseExpression();
343 if (Start == 0) return 0;
344 if (CurTok != ',')
345 return Error("expected ',' after for start value");
346 getNextToken();
347
348 ExprAST *End = ParseExpression();
349 if (End == 0) return 0;
350
351 // The step value is optional.
352 ExprAST *Step = 0;
353 if (CurTok == ',') {
354 getNextToken();
355 Step = ParseExpression();
356 if (Step == 0) return 0;
357 }
358
359 if (CurTok != tok_in)
360 return Error("expected 'in' after for");
361 getNextToken(); // eat 'in'.
362
363 ExprAST *Body = ParseExpression();
364 if (Body == 0) return 0;
365
366 return new ForExprAST(IdName, Start, End, Step, Body);
367 }
368
369 /// primary
370 /// ::= identifierexpr
371 /// ::= numberexpr
372 /// ::= parenexpr
373 /// ::= ifexpr
374 /// ::= forexpr
375 static ExprAST *ParsePrimary() {
376 switch (CurTok) {
377 default: return Error("unknown token when expecting an expression");
378 case tok_identifier: return ParseIdentifierExpr();
379 case tok_number: return ParseNumberExpr();
380 case '(': return ParseParenExpr();
381 case tok_if: return ParseIfExpr();
382 case tok_for: return ParseForExpr();
383 }
384 }
385
386 /// unary
387 /// ::= primary
388 /// ::= '!' unary
389 static ExprAST *ParseUnary() {
390 // If the current token is not an operator, it must be a primary expr.
391 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
392 return ParsePrimary();
393
394 // If this is a unary operator, read it.
395 int Opc = CurTok;
396 getNextToken();
397 if (ExprAST *Operand = ParseUnary())
398 return new UnaryExprAST(Opc, Operand);
399 return 0;
400 }
401
402 /// binoprhs
403 /// ::= ('+' unary)*
404 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
405 // If this is a binop, find its precedence.
406 while (1) {
407 int TokPrec = GetTokPrecedence();
408
409 // If this is a binop that binds at least as tightly as the current binop,
410 // consume it, otherwise we are done.
411 if (TokPrec < ExprPrec)
412 return LHS;
413
414 // Okay, we know this is a binop.
415 int BinOp = CurTok;
416 getNextToken(); // eat binop
417
418 // Parse the unary expression after the binary operator.
419 ExprAST *RHS = ParseUnary();
420 if (!RHS) return 0;
421
422 // If BinOp binds less tightly with RHS than the operator after RHS, let
423 // the pending operator take RHS as its LHS.
424 int NextPrec = GetTokPrecedence();
425 if (TokPrec < NextPrec) {
426 RHS = ParseBinOpRHS(TokPrec+1, RHS);
427 if (RHS == 0) return 0;
428 }
429
430 // Merge LHS/RHS.
431 LHS = new BinaryExprAST(BinOp, LHS, RHS);
432 }
433 }
434
435 /// expression
436 /// ::= unary binoprhs
437 ///
438 static ExprAST *ParseExpression() {
439 ExprAST *LHS = ParseUnary();
440 if (!LHS) return 0;
441
442 return ParseBinOpRHS(0, LHS);
443 }
444
445 /// prototype
446 /// ::= id '(' id* ')'
447 /// ::= binary LETTER number? (id, id)
448 /// ::= unary LETTER (id)
449 static PrototypeAST *ParsePrototype() {
450 std::string FnName;
451
452 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
453 unsigned BinaryPrecedence = 30;
454
455 switch (CurTok) {
456 default:
457 return ErrorP("Expected function name in prototype");
458 case tok_identifier:
459 FnName = IdentifierStr;
460 Kind = 0;
461 getNextToken();
462 break;
463 case tok_unary:
464 getNextToken();
465 if (!isascii(CurTok))
466 return ErrorP("Expected unary operator");
467 FnName = "unary";
468 FnName += (char)CurTok;
469 Kind = 1;
470 getNextToken();
471 break;
472 case tok_binary:
473 getNextToken();
474 if (!isascii(CurTok))
475 return ErrorP("Expected binary operator");
476 FnName = "binary";
477 FnName += (char)CurTok;
478 Kind = 2;
479 getNextToken();
480
481 // Read the precedence if present.
482 if (CurTok == tok_number) {
483 if (NumVal < 1 || NumVal > 100)
484 return ErrorP("Invalid precedecnce: must be 1..100");
485 BinaryPrecedence = (unsigned)NumVal;
486 getNextToken();
487 }
488 break;
489 }
490
491 if (CurTok != '(')
492 return ErrorP("Expected '(' in prototype");
493
494 std::vector ArgNames;
495 while (getNextToken() == tok_identifier)
496 ArgNames.push_back(IdentifierStr);
497 if (CurTok != ')')
498 return ErrorP("Expected ')' in prototype");
499
500 // success.
501 getNextToken(); // eat ')'.
502
503 // Verify right number of names for operator.
504 if (Kind && ArgNames.size() != Kind)
505 return ErrorP("Invalid number of operands for operator");
506
507 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
508 }
509
510 /// definition ::= 'def' prototype expression
511 static FunctionAST *ParseDefinition() {
512 getNextToken(); // eat def.
513 PrototypeAST *Proto = ParsePrototype();
514 if (Proto == 0) return 0;
515
516 if (ExprAST *E = ParseExpression())
517 return new FunctionAST(Proto, E);
518 return 0;
519 }
520
521 /// toplevelexpr ::= expression
522 static FunctionAST *ParseTopLevelExpr() {
523 if (ExprAST *E = ParseExpression()) {
524 // Make an anonymous proto.
525 PrototypeAST *Proto = new PrototypeAST("", std::vector());
526 return new FunctionAST(Proto, E);
527 }
528 return 0;
529 }
530
531 /// external ::= 'extern' prototype
532 static PrototypeAST *ParseExtern() {
533 getNextToken(); // eat extern.
534 return ParsePrototype();
535 }
536
537 //===----------------------------------------------------------------------===//
538 // Code Generation
539 //===----------------------------------------------------------------------===//
540
541 static Module *TheModule;
542 static IRBuilder<> Builder(getGlobalContext());
543 static std::map NamedValues;
544 static FunctionPassManager *TheFPM;
545
546 Value *ErrorV(const char *Str) { Error(Str); return 0; }
547
548 Value *NumberExprAST::Codegen() {
549 return ConstantFP::get(getGlobalContext(), APFloat(Val));
550 }
551
552 Value *VariableExprAST::Codegen() {
553 // Look this variable up in the function.
554 Value *V = NamedValues[Name];
555 return V ? V : ErrorV("Unknown variable name");
556 }
557
558 Value *UnaryExprAST::Codegen() {
559 Value *OperandV = Operand->Codegen();
560 if (OperandV == 0) return 0;
561
562 Function *F = TheModule->getFunction(std::string("unary")+Opcode);
563 if (F == 0)
564 return ErrorV("Unknown unary operator");
565
566 return Builder.CreateCall(F, OperandV, "unop");
567 }
568
569 Value *BinaryExprAST::Codegen() {
570 Value *L = LHS->Codegen();
571 Value *R = RHS->Codegen();
572 if (L == 0 || R == 0) return 0;
573
574 switch (Op) {
575 case '+': return Builder.CreateAdd(L, R, "addtmp");
576 case '-': return Builder.CreateSub(L, R, "subtmp");
577 case '*': return Builder.CreateMul(L, R, "multmp");
578 case '<':
579 L = Builder.CreateFCmpULT(L, R, "cmptmp");
580 // Convert bool 0/1 to double 0.0 or 1.0
581 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
582 "booltmp");
583 default: break;
584 }
585
586 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
587 // a call to it.
588 Function *F = TheModule->getFunction(std::string("binary")+Op);
589 assert(F && "binary operator not found!");
590
591 Value *Ops[] = { L, R };
592 return Builder.CreateCall(F, Ops, Ops+2, "binop");
593 }
594
595 Value *CallExprAST::Codegen() {
596 // Look up the name in the global module table.
597 Function *CalleeF = TheModule->getFunction(Callee);
598 if (CalleeF == 0)
599 return ErrorV("Unknown function referenced");
600
601 // If argument mismatch error.
602 if (CalleeF->arg_size() != Args.size())
603 return ErrorV("Incorrect # arguments passed");
604
605 std::vector ArgsV;
606 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
607 ArgsV.push_back(Args[i]->Codegen());
608 if (ArgsV.back() == 0) return 0;
609 }
610
611 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
612 }
613
614 Value *IfExprAST::Codegen() {
615 Value *CondV = Cond->Codegen();
616 if (CondV == 0) return 0;
617
618 // Convert condition to a bool by comparing equal to 0.0.
619 CondV = Builder.CreateFCmpONE(CondV,
620 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
621 "ifcond");
622
623 Function *TheFunction = Builder.GetInsertBlock()->getParent();
624
625 // Create blocks for the then and else cases. Insert the 'then' block at the
626 // end of the function.
627 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
628 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
629 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
630
631 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
632
633 // Emit then value.
634 Builder.SetInsertPoint(ThenBB);
635
636 Value *ThenV = Then->Codegen();
637 if (ThenV == 0) return 0;
638
639 Builder.CreateBr(MergeBB);
640 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
641 ThenBB = Builder.GetInsertBlock();
642
643 // Emit else block.
644 TheFunction->getBasicBlockList().push_back(ElseBB);
645 Builder.SetInsertPoint(ElseBB);
646
647 Value *ElseV = Else->Codegen();
648 if (ElseV == 0) return 0;
649
650 Builder.CreateBr(MergeBB);
651 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
652 ElseBB = Builder.GetInsertBlock();
653
654 // Emit merge block.
655 TheFunction->getBasicBlockList().push_back(MergeBB);
656 Builder.SetInsertPoint(MergeBB);
657 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
658 "iftmp");
659
660 PN->addIncoming(ThenV, ThenBB);
661 PN->addIncoming(ElseV, ElseBB);
662 return PN;
663 }
664
665 Value *ForExprAST::Codegen() {
666 // Output this as:
667 // ...
668 // start = startexpr
669 // goto loop
670 // loop:
671 // variable = phi [start, loopheader], [nextvariable, loopend]
672 // ...
673 // bodyexpr
674 // ...
675 // loopend:
676 // step = stepexpr
677 // nextvariable = variable + step
678 // endcond = endexpr
679 // br endcond, loop, endloop
680 // outloop:
681
682 // Emit the start code first, without 'variable' in scope.
683 Value *StartVal = Start->Codegen();
684 if (StartVal == 0) return 0;
685
686 // Make the new basic block for the loop header, inserting after current
687 // block.
688 Function *TheFunction = Builder.GetInsertBlock()->getParent();
689 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
690 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
691
692 // Insert an explicit fall through from the current block to the LoopBB.
693 Builder.CreateBr(LoopBB);
694
695 // Start insertion in LoopBB.
696 Builder.SetInsertPoint(LoopBB);
697
698 // Start the PHI node with an entry for Start.
699 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), VarName.c_str());
700 Variable->addIncoming(StartVal, PreheaderBB);
701
702 // Within the loop, the variable is defined equal to the PHI node. If it
703 // shadows an existing variable, we have to restore it, so save it now.
704 Value *OldVal = NamedValues[VarName];
705 NamedValues[VarName] = Variable;
706
707 // Emit the body of the loop. This, like any other expr, can change the
708 // current BB. Note that we ignore the value computed by the body, but don't
709 // allow an error.
710 if (Body->Codegen() == 0)
711 return 0;
712
713 // Emit the step value.
714 Value *StepVal;
715 if (Step) {
716 StepVal = Step->Codegen();
717 if (StepVal == 0) return 0;
718 } else {
719 // If not specified, use 1.0.
720 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
721 }
722
723 Value *NextVar = Builder.CreateAdd(Variable, StepVal, "nextvar");
724
725 // Compute the end condition.
726 Value *EndCond = End->Codegen();
727 if (EndCond == 0) return EndCond;
728
729 // Convert condition to a bool by comparing equal to 0.0.
730 EndCond = Builder.CreateFCmpONE(EndCond,
731 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
732 "loopcond");
733
734 // Create the "after loop" block and insert it.
735 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
736 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
737
738 // Insert the conditional branch into the end of LoopEndBB.
739 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
740
741 // Any new code will be inserted in AfterBB.
742 Builder.SetInsertPoint(AfterBB);
743
744 // Add a new entry to the PHI node for the backedge.
745 Variable->addIncoming(NextVar, LoopEndBB);
746
747 // Restore the unshadowed variable.
748 if (OldVal)
749 NamedValues[VarName] = OldVal;
750 else
751 NamedValues.erase(VarName);
752
753
754 // for expr always returns 0.0.
755 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
756 }
757
758 Function *PrototypeAST::Codegen() {
759 // Make the function type: double(double,double) etc.
760 std::vector Doubles(Args.size(),
761 Type::getDoubleTy(getGlobalContext()));
762 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
763 Doubles, false);
764
765 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
766
767 // If F conflicted, there was already something named 'Name'. If it has a
768 // body, don't allow redefinition or reextern.
769 if (F->getName() != Name) {
770 // Delete the one we just made and get the existing one.
771 F->eraseFromParent();
772 F = TheModule->getFunction(Name);
773
774 // If F already has a body, reject this.
775 if (!F->empty()) {
776 ErrorF("redefinition of function");
777 return 0;
778 }
779
780 // If F took a different number of args, reject.
781 if (F->arg_size() != Args.size()) {
782 ErrorF("redefinition of function with different # args");
783 return 0;
784 }
785 }
786
787 // Set names for all arguments.
788 unsigned Idx = 0;
789 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
790 ++AI, ++Idx) {
791 AI->setName(Args[Idx]);
792
793 // Add arguments to variable symbol table.
794 NamedValues[Args[Idx]] = AI;
795 }
796
797 return F;
798 }
799
800 Function *FunctionAST::Codegen() {
801 NamedValues.clear();
802
803 Function *TheFunction = Proto->Codegen();
804 if (TheFunction == 0)
805 return 0;
806
807 // If this is an operator, install it.
808 if (Proto->isBinaryOp())
809 BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
810
811 // Create a new basic block to start insertion into.
812 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
813 Builder.SetInsertPoint(BB);
814
815 if (Value *RetVal = Body->Codegen()) {
816 // Finish off the function.
817 Builder.CreateRet(RetVal);
818
819 // Validate the generated code, checking for consistency.
820 verifyFunction(*TheFunction);
821
822 // Optimize the function.
823 TheFPM->run(*TheFunction);
824
825 return TheFunction;
826 }
827
828 // Error reading body, remove function.
829 TheFunction->eraseFromParent();
830
831 if (Proto->isBinaryOp())
832 BinopPrecedence.erase(Proto->getOperatorName());
833 return 0;
834 }
835
836 //===----------------------------------------------------------------------===//
837 // Top-Level parsing and JIT Driver
838 //===----------------------------------------------------------------------===//
839
840 static ExecutionEngine *TheExecutionEngine;
841
842 static void HandleDefinition() {
843 if (FunctionAST *F = ParseDefinition()) {
844 if (Function *LF = F->Codegen()) {
845 fprintf(stderr, "Read function definition:");
846 LF->dump();
847 }
848 } else {
849 // Skip token for error recovery.
850 getNextToken();
851 }
852 }
853
854 static void HandleExtern() {
855 if (PrototypeAST *P = ParseExtern()) {
856 if (Function *F = P->Codegen()) {
857 fprintf(stderr, "Read extern: ");
858 F->dump();
859 }
860 } else {
861 // Skip token for error recovery.
862 getNextToken();
863 }
864 }
865
866 static void HandleTopLevelExpression() {
867 // Evaluate a top-level expression into an anonymous function.
868 if (FunctionAST *F = ParseTopLevelExpr()) {
869 if (Function *LF = F->Codegen()) {
870 // JIT the function, returning a function pointer.
871 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
872
873 // Cast it to the right type (takes no arguments, returns a double) so we
874 // can call it as a native function.
875 double (*FP)() = (double (*)())(intptr_t)FPtr;
876 fprintf(stderr, "Evaluated to %f\n", FP());
877 }
878 } else {
879 // Skip token for error recovery.
880 getNextToken();
881 }
882 }
883
884 /// top ::= definition | external | expression | ';'
885 static void MainLoop() {
886 while (1) {
887 fprintf(stderr, "ready> ");
888 switch (CurTok) {
889 case tok_eof: return;
890 case ';': getNextToken(); break; // ignore top-level semicolons.
891 case tok_def: HandleDefinition(); break;
892 case tok_extern: HandleExtern(); break;
893 default: HandleTopLevelExpression(); break;
894 }
895 }
896 }
897
898 //===----------------------------------------------------------------------===//
899 // "Library" functions that can be "extern'd" from user code.
900 //===----------------------------------------------------------------------===//
901
902 /// putchard - putchar that takes a double and returns 0.
903 extern "C"
904 double putchard(double X) {
905 putchar((char)X);
906 return 0;
907 }
908
909 /// printd - printf that takes a double prints it as "%f\n", returning 0.
910 extern "C"
911 double printd(double X) {
912 printf("%f\n", X);
913 return 0;
914 }
915
916 //===----------------------------------------------------------------------===//
917 // Main driver code.
918 //===----------------------------------------------------------------------===//
919
920 int main() {
921 InitializeNativeTarget();
922 LLVMContext &Context = getGlobalContext();
923
924 // Install standard binary operators.
925 // 1 is lowest precedence.
926 BinopPrecedence['<'] = 10;
927 BinopPrecedence['+'] = 20;
928 BinopPrecedence['-'] = 20;
929 BinopPrecedence['*'] = 40; // highest.
930
931 // Prime the first token.
932 fprintf(stderr, "ready> ");
933 getNextToken();
934
935 // Make the module, which holds all the code.
936 TheModule = new Module("my cool jit", Context);
937
938 ExistingModuleProvider *OurModuleProvider =
939 new ExistingModuleProvider(TheModule);
940
941 // Create the JIT. This takes ownership of the module and module provider.
942 TheExecutionEngine = EngineBuilder(OurModuleProvider).create();
943
944 FunctionPassManager OurFPM(OurModuleProvider);
945
946 // Set up the optimizer pipeline. Start with registering info about how the
947 // target lays out data structures.
948 OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
949 // Do simple "peephole" optimizations and bit-twiddling optzns.
950 OurFPM.add(createInstructionCombiningPass());
951 // Reassociate expressions.
952 OurFPM.add(createReassociatePass());
953 // Eliminate Common SubExpressions.
954 OurFPM.add(createGVNPass());
955 // Simplify the control flow graph (deleting unreachable blocks, etc).
956 OurFPM.add(createCFGSimplificationPass());
957
958 OurFPM.doInitialization();
959
960 // Set the global so the code gen can use this.
961 TheFPM = &OurFPM;
962
963 // Run the main "interpreter loop" now.
964 MainLoop();
965
966 TheFPM = 0;
967
968 // Print out all of the generated code.
969 TheModule->dump();
970
971 return 0;
972 }
None ##===- examples/Kaleidoscope-Ch7/Makefile ------------------*- Makefile -*-===##
0 ##===- examples/Kaleidoscope/Chapter7/Makefile -------------*- Makefile -*-===##
11 #
22 # The LLVM Compiler Infrastructure
33 #
55 # License. See LICENSE.TXT for details.
66 #
77 ##===----------------------------------------------------------------------===##
8 LEVEL = ../..
8 LEVEL = ../../..
99 TOOLNAME = Kaleidoscope-Ch7
1010 EXAMPLE_TOOL = 1
1111
99
1010 include $(LEVEL)/Makefile.config
1111
12 PARALLEL_DIRS:= Chapter7
12 PARALLEL_DIRS:= Chapter2 Chapter3 Chapter4 Chapter5 Chapter6 Chapter7
1313
1414 include $(LEVEL)/Makefile.common