blob: 30d03ff8e4f6ac9a54e44c0775c21a8b0627e6ae [file] [log] [blame]
NAKAMURA Takumi85c9bac2015-03-02 01:04:34 +00001#include "llvm/ADT/STLExtras.h"
Chandler Carruth17e0bc32015-08-06 07:33:15 +00002#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +00003#include "llvm/Analysis/Passes.h"
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +00004#include "llvm/ExecutionEngine/ExecutionEngine.h"
Eric Christopher1b74b652014-12-08 18:00:38 +00005#include "llvm/ExecutionEngine/MCJIT.h"
6#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +00007#include "llvm/IR/DataLayout.h"
8#include "llvm/IR/DerivedTypes.h"
9#include "llvm/IR/IRBuilder.h"
10#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000011#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000012#include "llvm/IR/Module.h"
Chandler Carruth20d4e6b2014-01-13 09:58:03 +000013#include "llvm/IR/Verifier.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000014#include "llvm/Support/TargetSelect.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +000015#include "llvm/Transforms/Scalar.h"
Will Dietz981af002013-10-12 00:55:57 +000016#include <cctype>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000017#include <cstdio>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000018#include <map>
Chandler Carruth605e30e2012-12-04 10:16:57 +000019#include <string>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000020#include <vector>
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Lexer
25//===----------------------------------------------------------------------===//
26
27// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
28// of these for known things.
29enum Token {
30 tok_eof = -1,
31
32 // commands
Eric Christopherc0239362014-12-08 18:12:28 +000033 tok_def = -2,
34 tok_extern = -3,
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000035
36 // primary
Eric Christopherc0239362014-12-08 18:12:28 +000037 tok_identifier = -4,
38 tok_number = -5,
39
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000040 // control
Eric Christopherc0239362014-12-08 18:12:28 +000041 tok_if = -6,
42 tok_then = -7,
43 tok_else = -8,
44 tok_for = -9,
45 tok_in = -10
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000046};
47
Eric Christopherc0239362014-12-08 18:12:28 +000048static std::string IdentifierStr; // Filled in if tok_identifier
49static double NumVal; // Filled in if tok_number
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000050
51/// gettok - Return the next token from standard input.
52static int gettok() {
53 static int LastChar = ' ';
54
55 // Skip any whitespace.
56 while (isspace(LastChar))
57 LastChar = getchar();
58
59 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
60 IdentifierStr = LastChar;
61 while (isalnum((LastChar = getchar())))
62 IdentifierStr += LastChar;
63
Eric Christopherc0239362014-12-08 18:12:28 +000064 if (IdentifierStr == "def")
65 return tok_def;
66 if (IdentifierStr == "extern")
67 return tok_extern;
68 if (IdentifierStr == "if")
69 return tok_if;
70 if (IdentifierStr == "then")
71 return tok_then;
72 if (IdentifierStr == "else")
73 return tok_else;
74 if (IdentifierStr == "for")
75 return tok_for;
76 if (IdentifierStr == "in")
77 return tok_in;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000078 return tok_identifier;
79 }
80
Eric Christopherc0239362014-12-08 18:12:28 +000081 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000082 std::string NumStr;
83 do {
84 NumStr += LastChar;
85 LastChar = getchar();
86 } while (isdigit(LastChar) || LastChar == '.');
87
88 NumVal = strtod(NumStr.c_str(), 0);
89 return tok_number;
90 }
91
92 if (LastChar == '#') {
93 // Comment until end of line.
Eric Christopherc0239362014-12-08 18:12:28 +000094 do
95 LastChar = getchar();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000096 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
Eric Christopherc0239362014-12-08 18:12:28 +000097
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000098 if (LastChar != EOF)
99 return gettok();
100 }
Eric Christopherc0239362014-12-08 18:12:28 +0000101
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000102 // Check for end of file. Don't eat the EOF.
103 if (LastChar == EOF)
104 return tok_eof;
105
106 // Otherwise, just return the character as its ascii value.
107 int ThisChar = LastChar;
108 LastChar = getchar();
109 return ThisChar;
110}
111
112//===----------------------------------------------------------------------===//
113// Abstract Syntax Tree (aka Parse Tree)
114//===----------------------------------------------------------------------===//
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000115namespace {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000116/// ExprAST - Base class for all expression nodes.
117class ExprAST {
118public:
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000119 virtual ~ExprAST() {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000120 virtual Value *Codegen() = 0;
121};
122
123/// NumberExprAST - Expression class for numeric literals like "1.0".
124class NumberExprAST : public ExprAST {
125 double Val;
Eric Christopherc0239362014-12-08 18:12:28 +0000126
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000127public:
128 NumberExprAST(double val) : Val(val) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000129 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000130};
131
132/// VariableExprAST - Expression class for referencing a variable, like "a".
133class VariableExprAST : public ExprAST {
134 std::string Name;
Eric Christopherc0239362014-12-08 18:12:28 +0000135
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000136public:
137 VariableExprAST(const std::string &name) : Name(name) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000138 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000139};
140
141/// BinaryExprAST - Expression class for a binary operator.
142class BinaryExprAST : public ExprAST {
143 char Op;
144 ExprAST *LHS, *RHS;
Eric Christopherc0239362014-12-08 18:12:28 +0000145
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000146public:
Eric Christopherc0239362014-12-08 18:12:28 +0000147 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
148 : Op(op), LHS(lhs), RHS(rhs) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000149 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000150};
151
152/// CallExprAST - Expression class for function calls.
153class CallExprAST : public ExprAST {
154 std::string Callee;
Eric Christopherc0239362014-12-08 18:12:28 +0000155 std::vector<ExprAST *> Args;
156
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000157public:
Eric Christopherc0239362014-12-08 18:12:28 +0000158 CallExprAST(const std::string &callee, std::vector<ExprAST *> &args)
159 : Callee(callee), Args(args) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000160 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000161};
162
163/// IfExprAST - Expression class for if/then/else.
164class IfExprAST : public ExprAST {
165 ExprAST *Cond, *Then, *Else;
Eric Christopherc0239362014-12-08 18:12:28 +0000166
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000167public:
168 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
Eric Christopherc0239362014-12-08 18:12:28 +0000169 : Cond(cond), Then(then), Else(_else) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000170 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000171};
172
173/// ForExprAST - Expression class for for/in.
174class ForExprAST : public ExprAST {
175 std::string VarName;
176 ExprAST *Start, *End, *Step, *Body;
Eric Christopherc0239362014-12-08 18:12:28 +0000177
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000178public:
179 ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
180 ExprAST *step, ExprAST *body)
Eric Christopherc0239362014-12-08 18:12:28 +0000181 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000182 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000183};
184
185/// PrototypeAST - This class represents the "prototype" for a function,
186/// which captures its name, and its argument names (thus implicitly the number
187/// of arguments the function takes).
188class PrototypeAST {
189 std::string Name;
190 std::vector<std::string> Args;
Eric Christopherc0239362014-12-08 18:12:28 +0000191
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000192public:
193 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
Eric Christopherc0239362014-12-08 18:12:28 +0000194 : Name(name), Args(args) {}
195
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000196 Function *Codegen();
197};
198
199/// FunctionAST - This class represents a function definition itself.
200class FunctionAST {
201 PrototypeAST *Proto;
202 ExprAST *Body;
Eric Christopherc0239362014-12-08 18:12:28 +0000203
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000204public:
Eric Christopherc0239362014-12-08 18:12:28 +0000205 FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {}
206
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000207 Function *Codegen();
208};
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000209} // end anonymous namespace
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000210
211//===----------------------------------------------------------------------===//
212// Parser
213//===----------------------------------------------------------------------===//
214
215/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
216/// token the parser is looking at. getNextToken reads another token from the
217/// lexer and updates CurTok with its results.
218static int CurTok;
Eric Christopherc0239362014-12-08 18:12:28 +0000219static int getNextToken() { return CurTok = gettok(); }
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000220
221/// BinopPrecedence - This holds the precedence for each binary operator that is
222/// defined.
223static std::map<char, int> BinopPrecedence;
224
225/// GetTokPrecedence - Get the precedence of the pending binary operator token.
226static int GetTokPrecedence() {
227 if (!isascii(CurTok))
228 return -1;
Eric Christopherc0239362014-12-08 18:12:28 +0000229
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000230 // Make sure it's a declared binop.
231 int TokPrec = BinopPrecedence[CurTok];
Eric Christopherc0239362014-12-08 18:12:28 +0000232 if (TokPrec <= 0)
233 return -1;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000234 return TokPrec;
235}
236
237/// Error* - These are little helper functions for error handling.
Eric Christopherc0239362014-12-08 18:12:28 +0000238ExprAST *Error(const char *Str) {
239 fprintf(stderr, "Error: %s\n", Str);
240 return 0;
241}
242PrototypeAST *ErrorP(const char *Str) {
243 Error(Str);
244 return 0;
245}
246FunctionAST *ErrorF(const char *Str) {
247 Error(Str);
248 return 0;
249}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000250
251static ExprAST *ParseExpression();
252
253/// identifierexpr
254/// ::= identifier
255/// ::= identifier '(' expression* ')'
256static ExprAST *ParseIdentifierExpr() {
257 std::string IdName = IdentifierStr;
Eric Christopherc0239362014-12-08 18:12:28 +0000258
259 getNextToken(); // eat identifier.
260
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000261 if (CurTok != '(') // Simple variable ref.
262 return new VariableExprAST(IdName);
Eric Christopherc0239362014-12-08 18:12:28 +0000263
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000264 // Call.
Eric Christopherc0239362014-12-08 18:12:28 +0000265 getNextToken(); // eat (
266 std::vector<ExprAST *> Args;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000267 if (CurTok != ')') {
268 while (1) {
269 ExprAST *Arg = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000270 if (!Arg)
271 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000272 Args.push_back(Arg);
273
Eric Christopherc0239362014-12-08 18:12:28 +0000274 if (CurTok == ')')
275 break;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000276
277 if (CurTok != ',')
278 return Error("Expected ')' or ',' in argument list");
279 getNextToken();
280 }
281 }
282
283 // Eat the ')'.
284 getNextToken();
Eric Christopherc0239362014-12-08 18:12:28 +0000285
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000286 return new CallExprAST(IdName, Args);
287}
288
289/// numberexpr ::= number
290static ExprAST *ParseNumberExpr() {
291 ExprAST *Result = new NumberExprAST(NumVal);
292 getNextToken(); // consume the number
293 return Result;
294}
295
296/// parenexpr ::= '(' expression ')'
297static ExprAST *ParseParenExpr() {
Eric Christopherc0239362014-12-08 18:12:28 +0000298 getNextToken(); // eat (.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000299 ExprAST *V = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000300 if (!V)
301 return 0;
302
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000303 if (CurTok != ')')
304 return Error("expected ')'");
Eric Christopherc0239362014-12-08 18:12:28 +0000305 getNextToken(); // eat ).
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000306 return V;
307}
308
309/// ifexpr ::= 'if' expression 'then' expression 'else' expression
310static ExprAST *ParseIfExpr() {
Eric Christopherc0239362014-12-08 18:12:28 +0000311 getNextToken(); // eat the if.
312
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000313 // condition.
314 ExprAST *Cond = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000315 if (!Cond)
316 return 0;
317
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000318 if (CurTok != tok_then)
319 return Error("expected then");
Eric Christopherc0239362014-12-08 18:12:28 +0000320 getNextToken(); // eat the then
321
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000322 ExprAST *Then = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000323 if (Then == 0)
324 return 0;
325
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000326 if (CurTok != tok_else)
327 return Error("expected else");
Eric Christopherc0239362014-12-08 18:12:28 +0000328
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000329 getNextToken();
Eric Christopherc0239362014-12-08 18:12:28 +0000330
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000331 ExprAST *Else = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000332 if (!Else)
333 return 0;
334
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000335 return new IfExprAST(Cond, Then, Else);
336}
337
338/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
339static ExprAST *ParseForExpr() {
Eric Christopherc0239362014-12-08 18:12:28 +0000340 getNextToken(); // eat the for.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000341
342 if (CurTok != tok_identifier)
343 return Error("expected identifier after for");
Eric Christopherc0239362014-12-08 18:12:28 +0000344
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000345 std::string IdName = IdentifierStr;
Eric Christopherc0239362014-12-08 18:12:28 +0000346 getNextToken(); // eat identifier.
347
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000348 if (CurTok != '=')
349 return Error("expected '=' after for");
Eric Christopherc0239362014-12-08 18:12:28 +0000350 getNextToken(); // eat '='.
351
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000352 ExprAST *Start = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000353 if (Start == 0)
354 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000355 if (CurTok != ',')
356 return Error("expected ',' after for start value");
357 getNextToken();
Eric Christopherc0239362014-12-08 18:12:28 +0000358
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000359 ExprAST *End = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000360 if (End == 0)
361 return 0;
362
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000363 // The step value is optional.
364 ExprAST *Step = 0;
365 if (CurTok == ',') {
366 getNextToken();
367 Step = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000368 if (Step == 0)
369 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000370 }
Eric Christopherc0239362014-12-08 18:12:28 +0000371
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000372 if (CurTok != tok_in)
373 return Error("expected 'in' after for");
Eric Christopherc0239362014-12-08 18:12:28 +0000374 getNextToken(); // eat 'in'.
375
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000376 ExprAST *Body = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000377 if (Body == 0)
378 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000379
380 return new ForExprAST(IdName, Start, End, Step, Body);
381}
382
383/// primary
384/// ::= identifierexpr
385/// ::= numberexpr
386/// ::= parenexpr
387/// ::= ifexpr
388/// ::= forexpr
389static ExprAST *ParsePrimary() {
390 switch (CurTok) {
Eric Christopherc0239362014-12-08 18:12:28 +0000391 default:
392 return Error("unknown token when expecting an expression");
393 case tok_identifier:
394 return ParseIdentifierExpr();
395 case tok_number:
396 return ParseNumberExpr();
397 case '(':
398 return ParseParenExpr();
399 case tok_if:
400 return ParseIfExpr();
401 case tok_for:
402 return ParseForExpr();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000403 }
404}
405
406/// binoprhs
407/// ::= ('+' primary)*
408static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
409 // If this is a binop, find its precedence.
410 while (1) {
411 int TokPrec = GetTokPrecedence();
Eric Christopherc0239362014-12-08 18:12:28 +0000412
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000413 // If this is a binop that binds at least as tightly as the current binop,
414 // consume it, otherwise we are done.
415 if (TokPrec < ExprPrec)
416 return LHS;
Eric Christopherc0239362014-12-08 18:12:28 +0000417
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000418 // Okay, we know this is a binop.
419 int BinOp = CurTok;
Eric Christopherc0239362014-12-08 18:12:28 +0000420 getNextToken(); // eat binop
421
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000422 // Parse the primary expression after the binary operator.
423 ExprAST *RHS = ParsePrimary();
Eric Christopherc0239362014-12-08 18:12:28 +0000424 if (!RHS)
425 return 0;
426
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000427 // If BinOp binds less tightly with RHS than the operator after RHS, let
428 // the pending operator take RHS as its LHS.
429 int NextPrec = GetTokPrecedence();
430 if (TokPrec < NextPrec) {
Eric Christopherc0239362014-12-08 18:12:28 +0000431 RHS = ParseBinOpRHS(TokPrec + 1, RHS);
432 if (RHS == 0)
433 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000434 }
Eric Christopherc0239362014-12-08 18:12:28 +0000435
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000436 // Merge LHS/RHS.
437 LHS = new BinaryExprAST(BinOp, LHS, RHS);
438 }
439}
440
441/// expression
442/// ::= primary binoprhs
443///
444static ExprAST *ParseExpression() {
445 ExprAST *LHS = ParsePrimary();
Eric Christopherc0239362014-12-08 18:12:28 +0000446 if (!LHS)
447 return 0;
448
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000449 return ParseBinOpRHS(0, LHS);
450}
451
452/// prototype
453/// ::= id '(' id* ')'
454static PrototypeAST *ParsePrototype() {
455 if (CurTok != tok_identifier)
456 return ErrorP("Expected function name in prototype");
457
458 std::string FnName = IdentifierStr;
459 getNextToken();
Eric Christopherc0239362014-12-08 18:12:28 +0000460
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000461 if (CurTok != '(')
462 return ErrorP("Expected '(' in prototype");
Eric Christopherc0239362014-12-08 18:12:28 +0000463
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000464 std::vector<std::string> ArgNames;
465 while (getNextToken() == tok_identifier)
466 ArgNames.push_back(IdentifierStr);
467 if (CurTok != ')')
468 return ErrorP("Expected ')' in prototype");
Eric Christopherc0239362014-12-08 18:12:28 +0000469
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000470 // success.
Eric Christopherc0239362014-12-08 18:12:28 +0000471 getNextToken(); // eat ')'.
472
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000473 return new PrototypeAST(FnName, ArgNames);
474}
475
476/// definition ::= 'def' prototype expression
477static FunctionAST *ParseDefinition() {
Eric Christopherc0239362014-12-08 18:12:28 +0000478 getNextToken(); // eat def.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000479 PrototypeAST *Proto = ParsePrototype();
Eric Christopherc0239362014-12-08 18:12:28 +0000480 if (Proto == 0)
481 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000482
483 if (ExprAST *E = ParseExpression())
484 return new FunctionAST(Proto, E);
485 return 0;
486}
487
488/// toplevelexpr ::= expression
489static FunctionAST *ParseTopLevelExpr() {
490 if (ExprAST *E = ParseExpression()) {
491 // Make an anonymous proto.
492 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
493 return new FunctionAST(Proto, E);
494 }
495 return 0;
496}
497
498/// external ::= 'extern' prototype
499static PrototypeAST *ParseExtern() {
Eric Christopherc0239362014-12-08 18:12:28 +0000500 getNextToken(); // eat extern.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000501 return ParsePrototype();
502}
503
504//===----------------------------------------------------------------------===//
505// Code Generation
506//===----------------------------------------------------------------------===//
507
508static Module *TheModule;
509static IRBuilder<> Builder(getGlobalContext());
Eric Christopherc0239362014-12-08 18:12:28 +0000510static std::map<std::string, Value *> NamedValues;
Chandler Carruth7ecd9912015-02-13 10:21:05 +0000511static legacy::FunctionPassManager *TheFPM;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000512
Eric Christopherc0239362014-12-08 18:12:28 +0000513Value *ErrorV(const char *Str) {
514 Error(Str);
515 return 0;
516}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000517
518Value *NumberExprAST::Codegen() {
519 return ConstantFP::get(getGlobalContext(), APFloat(Val));
520}
521
522Value *VariableExprAST::Codegen() {
523 // Look this variable up in the function.
524 Value *V = NamedValues[Name];
525 return V ? V : ErrorV("Unknown variable name");
526}
527
528Value *BinaryExprAST::Codegen() {
529 Value *L = LHS->Codegen();
530 Value *R = RHS->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000531 if (L == 0 || R == 0)
532 return 0;
533
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000534 switch (Op) {
Eric Christopherc0239362014-12-08 18:12:28 +0000535 case '+':
536 return Builder.CreateFAdd(L, R, "addtmp");
537 case '-':
538 return Builder.CreateFSub(L, R, "subtmp");
539 case '*':
540 return Builder.CreateFMul(L, R, "multmp");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000541 case '<':
542 L = Builder.CreateFCmpULT(L, R, "cmptmp");
543 // Convert bool 0/1 to double 0.0 or 1.0
544 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
545 "booltmp");
Eric Christopherc0239362014-12-08 18:12:28 +0000546 default:
547 return ErrorV("invalid binary operator");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000548 }
549}
550
551Value *CallExprAST::Codegen() {
552 // Look up the name in the global module table.
553 Function *CalleeF = TheModule->getFunction(Callee);
554 if (CalleeF == 0)
555 return ErrorV("Unknown function referenced");
Eric Christopherc0239362014-12-08 18:12:28 +0000556
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000557 // If argument mismatch error.
558 if (CalleeF->arg_size() != Args.size())
559 return ErrorV("Incorrect # arguments passed");
560
Eric Christopherc0239362014-12-08 18:12:28 +0000561 std::vector<Value *> ArgsV;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000562 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
563 ArgsV.push_back(Args[i]->Codegen());
Eric Christopherc0239362014-12-08 18:12:28 +0000564 if (ArgsV.back() == 0)
565 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000566 }
Eric Christopherc0239362014-12-08 18:12:28 +0000567
Francois Pichetc5d10502011-07-15 10:59:52 +0000568 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000569}
570
571Value *IfExprAST::Codegen() {
572 Value *CondV = Cond->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000573 if (CondV == 0)
574 return 0;
575
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000576 // Convert condition to a bool by comparing equal to 0.0.
Eric Christopherc0239362014-12-08 18:12:28 +0000577 CondV = Builder.CreateFCmpONE(
578 CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
579
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000580 Function *TheFunction = Builder.GetInsertBlock()->getParent();
Eric Christopherc0239362014-12-08 18:12:28 +0000581
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000582 // Create blocks for the then and else cases. Insert the 'then' block at the
583 // end of the function.
Eric Christopherc0239362014-12-08 18:12:28 +0000584 BasicBlock *ThenBB =
585 BasicBlock::Create(getGlobalContext(), "then", TheFunction);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000586 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
587 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
Eric Christopherc0239362014-12-08 18:12:28 +0000588
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000589 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000590
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000591 // Emit then value.
592 Builder.SetInsertPoint(ThenBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000593
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000594 Value *ThenV = Then->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000595 if (ThenV == 0)
596 return 0;
597
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000598 Builder.CreateBr(MergeBB);
599 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
600 ThenBB = Builder.GetInsertBlock();
Eric Christopherc0239362014-12-08 18:12:28 +0000601
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000602 // Emit else block.
603 TheFunction->getBasicBlockList().push_back(ElseBB);
604 Builder.SetInsertPoint(ElseBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000605
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000606 Value *ElseV = Else->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000607 if (ElseV == 0)
608 return 0;
609
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000610 Builder.CreateBr(MergeBB);
611 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
612 ElseBB = Builder.GetInsertBlock();
Eric Christopherc0239362014-12-08 18:12:28 +0000613
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000614 // Emit merge block.
615 TheFunction->getBasicBlockList().push_back(MergeBB);
616 Builder.SetInsertPoint(MergeBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000617 PHINode *PN =
618 Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
619
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000620 PN->addIncoming(ThenV, ThenBB);
621 PN->addIncoming(ElseV, ElseBB);
622 return PN;
623}
624
625Value *ForExprAST::Codegen() {
626 // Output this as:
627 // ...
628 // start = startexpr
629 // goto loop
Eric Christopherc0239362014-12-08 18:12:28 +0000630 // loop:
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000631 // variable = phi [start, loopheader], [nextvariable, loopend]
632 // ...
633 // bodyexpr
634 // ...
635 // loopend:
636 // step = stepexpr
637 // nextvariable = variable + step
638 // endcond = endexpr
639 // br endcond, loop, endloop
640 // outloop:
Eric Christopherc0239362014-12-08 18:12:28 +0000641
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000642 // Emit the start code first, without 'variable' in scope.
643 Value *StartVal = Start->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000644 if (StartVal == 0)
645 return 0;
646
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000647 // Make the new basic block for the loop header, inserting after current
648 // block.
649 Function *TheFunction = Builder.GetInsertBlock()->getParent();
650 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
Eric Christopherc0239362014-12-08 18:12:28 +0000651 BasicBlock *LoopBB =
652 BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
653
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000654 // Insert an explicit fall through from the current block to the LoopBB.
655 Builder.CreateBr(LoopBB);
656
657 // Start insertion in LoopBB.
658 Builder.SetInsertPoint(LoopBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000659
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000660 // Start the PHI node with an entry for Start.
Eric Christopherc0239362014-12-08 18:12:28 +0000661 PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
662 2, VarName.c_str());
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000663 Variable->addIncoming(StartVal, PreheaderBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000664
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000665 // Within the loop, the variable is defined equal to the PHI node. If it
666 // shadows an existing variable, we have to restore it, so save it now.
667 Value *OldVal = NamedValues[VarName];
668 NamedValues[VarName] = Variable;
Eric Christopherc0239362014-12-08 18:12:28 +0000669
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000670 // Emit the body of the loop. This, like any other expr, can change the
671 // current BB. Note that we ignore the value computed by the body, but don't
672 // allow an error.
673 if (Body->Codegen() == 0)
674 return 0;
Eric Christopherc0239362014-12-08 18:12:28 +0000675
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000676 // Emit the step value.
677 Value *StepVal;
678 if (Step) {
679 StepVal = Step->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000680 if (StepVal == 0)
681 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000682 } else {
683 // If not specified, use 1.0.
684 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
685 }
Eric Christopherc0239362014-12-08 18:12:28 +0000686
Chris Lattner26d79502010-06-21 22:51:14 +0000687 Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000688
689 // Compute the end condition.
690 Value *EndCond = End->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000691 if (EndCond == 0)
692 return EndCond;
693
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000694 // Convert condition to a bool by comparing equal to 0.0.
Eric Christopherc0239362014-12-08 18:12:28 +0000695 EndCond = Builder.CreateFCmpONE(
696 EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
697
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000698 // Create the "after loop" block and insert it.
699 BasicBlock *LoopEndBB = Builder.GetInsertBlock();
Eric Christopherc0239362014-12-08 18:12:28 +0000700 BasicBlock *AfterBB =
701 BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
702
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000703 // Insert the conditional branch into the end of LoopEndBB.
704 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000705
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000706 // Any new code will be inserted in AfterBB.
707 Builder.SetInsertPoint(AfterBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000708
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000709 // Add a new entry to the PHI node for the backedge.
710 Variable->addIncoming(NextVar, LoopEndBB);
Eric Christopherc0239362014-12-08 18:12:28 +0000711
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000712 // Restore the unshadowed variable.
713 if (OldVal)
714 NamedValues[VarName] = OldVal;
715 else
716 NamedValues.erase(VarName);
717
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000718 // for expr always returns 0.0.
719 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
720}
721
722Function *PrototypeAST::Codegen() {
723 // Make the function type: double(double,double) etc.
Eric Christopherc0239362014-12-08 18:12:28 +0000724 std::vector<Type *> Doubles(Args.size(),
725 Type::getDoubleTy(getGlobalContext()));
726 FunctionType *FT =
727 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
728
729 Function *F =
730 Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
731
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000732 // If F conflicted, there was already something named 'Name'. If it has a
733 // body, don't allow redefinition or reextern.
734 if (F->getName() != Name) {
735 // Delete the one we just made and get the existing one.
736 F->eraseFromParent();
737 F = TheModule->getFunction(Name);
Eric Christopherc0239362014-12-08 18:12:28 +0000738
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000739 // If F already has a body, reject this.
740 if (!F->empty()) {
741 ErrorF("redefinition of function");
742 return 0;
743 }
Eric Christopherc0239362014-12-08 18:12:28 +0000744
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000745 // If F took a different number of args, reject.
746 if (F->arg_size() != Args.size()) {
747 ErrorF("redefinition of function with different # args");
748 return 0;
749 }
750 }
Eric Christopherc0239362014-12-08 18:12:28 +0000751
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000752 // Set names for all arguments.
753 unsigned Idx = 0;
754 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
755 ++AI, ++Idx) {
756 AI->setName(Args[Idx]);
Eric Christopherc0239362014-12-08 18:12:28 +0000757
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000758 // Add arguments to variable symbol table.
759 NamedValues[Args[Idx]] = AI;
760 }
Eric Christopherc0239362014-12-08 18:12:28 +0000761
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000762 return F;
763}
764
765Function *FunctionAST::Codegen() {
766 NamedValues.clear();
Eric Christopherc0239362014-12-08 18:12:28 +0000767
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000768 Function *TheFunction = Proto->Codegen();
769 if (TheFunction == 0)
770 return 0;
Eric Christopherc0239362014-12-08 18:12:28 +0000771
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000772 // Create a new basic block to start insertion into.
773 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
774 Builder.SetInsertPoint(BB);
Eric Christopherc0239362014-12-08 18:12:28 +0000775
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000776 if (Value *RetVal = Body->Codegen()) {
777 // Finish off the function.
778 Builder.CreateRet(RetVal);
779
780 // Validate the generated code, checking for consistency.
781 verifyFunction(*TheFunction);
782
783 // Optimize the function.
784 TheFPM->run(*TheFunction);
Eric Christopherc0239362014-12-08 18:12:28 +0000785
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000786 return TheFunction;
787 }
Eric Christopherc0239362014-12-08 18:12:28 +0000788
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000789 // Error reading body, remove function.
790 TheFunction->eraseFromParent();
791 return 0;
792}
793
794//===----------------------------------------------------------------------===//
795// Top-Level parsing and JIT Driver
796//===----------------------------------------------------------------------===//
797
798static ExecutionEngine *TheExecutionEngine;
799
800static void HandleDefinition() {
801 if (FunctionAST *F = ParseDefinition()) {
802 if (Function *LF = F->Codegen()) {
803 fprintf(stderr, "Read function definition:");
804 LF->dump();
805 }
806 } else {
807 // Skip token for error recovery.
808 getNextToken();
809 }
810}
811
812static void HandleExtern() {
813 if (PrototypeAST *P = ParseExtern()) {
814 if (Function *F = P->Codegen()) {
815 fprintf(stderr, "Read extern: ");
816 F->dump();
817 }
818 } else {
819 // Skip token for error recovery.
820 getNextToken();
821 }
822}
823
824static void HandleTopLevelExpression() {
825 // Evaluate a top-level expression into an anonymous function.
826 if (FunctionAST *F = ParseTopLevelExpr()) {
827 if (Function *LF = F->Codegen()) {
Eric Christopher1b74b652014-12-08 18:00:38 +0000828 TheExecutionEngine->finalizeObject();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000829 // JIT the function, returning a function pointer.
830 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
Eric Christopherc0239362014-12-08 18:12:28 +0000831
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000832 // Cast it to the right type (takes no arguments, returns a double) so we
833 // can call it as a native function.
834 double (*FP)() = (double (*)())(intptr_t)FPtr;
835 fprintf(stderr, "Evaluated to %f\n", FP());
836 }
837 } else {
838 // Skip token for error recovery.
839 getNextToken();
840 }
841}
842
843/// top ::= definition | external | expression | ';'
844static void MainLoop() {
845 while (1) {
846 fprintf(stderr, "ready> ");
847 switch (CurTok) {
Eric Christopherc0239362014-12-08 18:12:28 +0000848 case tok_eof:
849 return;
850 case ';':
851 getNextToken();
852 break; // ignore top-level semicolons.
853 case tok_def:
854 HandleDefinition();
855 break;
856 case tok_extern:
857 HandleExtern();
858 break;
859 default:
860 HandleTopLevelExpression();
861 break;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000862 }
863 }
864}
865
866//===----------------------------------------------------------------------===//
867// "Library" functions that can be "extern'd" from user code.
868//===----------------------------------------------------------------------===//
869
870/// putchard - putchar that takes a double and returns 0.
Eric Christopherc0239362014-12-08 18:12:28 +0000871extern "C" double putchard(double X) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000872 putchar((char)X);
873 return 0;
874}
875
876//===----------------------------------------------------------------------===//
877// Main driver code.
878//===----------------------------------------------------------------------===//
879
880int main() {
881 InitializeNativeTarget();
Eric Christopher1b74b652014-12-08 18:00:38 +0000882 InitializeNativeTargetAsmPrinter();
883 InitializeNativeTargetAsmParser();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000884 LLVMContext &Context = getGlobalContext();
885
886 // Install standard binary operators.
887 // 1 is lowest precedence.
888 BinopPrecedence['<'] = 10;
889 BinopPrecedence['+'] = 20;
890 BinopPrecedence['-'] = 20;
Eric Christopherc0239362014-12-08 18:12:28 +0000891 BinopPrecedence['*'] = 40; // highest.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000892
893 // Prime the first token.
894 fprintf(stderr, "ready> ");
895 getNextToken();
896
897 // Make the module, which holds all the code.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000898 std::unique_ptr<Module> Owner = make_unique<Module>("my cool jit", Context);
899 TheModule = Owner.get();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000900
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000901 // Create the JIT. This takes ownership of the module.
Jeffrey Yasskin8a303242010-02-11 19:15:20 +0000902 std::string ErrStr;
Eric Christopherc0239362014-12-08 18:12:28 +0000903 TheExecutionEngine =
904 EngineBuilder(std::move(Owner))
905 .setErrorStr(&ErrStr)
906 .setMCJITMemoryManager(llvm::make_unique<SectionMemoryManager>())
907 .create();
Jeffrey Yasskin8a303242010-02-11 19:15:20 +0000908 if (!TheExecutionEngine) {
909 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
910 exit(1);
911 }
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000912
Chandler Carruth7ecd9912015-02-13 10:21:05 +0000913 legacy::FunctionPassManager OurFPM(TheModule);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000914
915 // Set up the optimizer pipeline. Start with registering info about how the
916 // target lays out data structures.
Mehdi Aminicd253da2015-07-16 16:47:18 +0000917 TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
Dan Gohman56f3a4c2010-11-15 18:41:10 +0000918 // Provide basic AliasAnalysis support for GVN.
919 OurFPM.add(createBasicAliasAnalysisPass());
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000920 // Do simple "peephole" optimizations and bit-twiddling optzns.
921 OurFPM.add(createInstructionCombiningPass());
922 // Reassociate expressions.
923 OurFPM.add(createReassociatePass());
924 // Eliminate Common SubExpressions.
925 OurFPM.add(createGVNPass());
926 // Simplify the control flow graph (deleting unreachable blocks, etc).
927 OurFPM.add(createCFGSimplificationPass());
928
929 OurFPM.doInitialization();
930
931 // Set the global so the code gen can use this.
932 TheFPM = &OurFPM;
933
934 // Run the main "interpreter loop" now.
935 MainLoop();
936
937 TheFPM = 0;
938
939 // Print out all of the generated code.
940 TheModule->dump();
941
942 return 0;
943}