xref: /openbsd-src/gnu/llvm/llvm/examples/Kaleidoscope/MCJIT/lazy/toy-jit.cpp (revision 09467b48e8bc8b4905716062da846024139afbf2)
1*09467b48Spatrick #define MINIMAL_STDERR_OUTPUT
2*09467b48Spatrick 
3*09467b48Spatrick #include "llvm/Analysis/Passes.h"
4*09467b48Spatrick #include "llvm/ExecutionEngine/ExecutionEngine.h"
5*09467b48Spatrick #include "llvm/IR/DataLayout.h"
6*09467b48Spatrick #include "llvm/IR/DerivedTypes.h"
7*09467b48Spatrick #include "llvm/IR/IRBuilder.h"
8*09467b48Spatrick #include "llvm/IR/LLVMContext.h"
9*09467b48Spatrick #include "llvm/IR/LegacyPassManager.h"
10*09467b48Spatrick #include "llvm/IR/Module.h"
11*09467b48Spatrick #include "llvm/IR/Verifier.h"
12*09467b48Spatrick #include "llvm/Support/TargetSelect.h"
13*09467b48Spatrick #include "llvm/Transforms/Scalar.h"
14*09467b48Spatrick #include <cctype>
15*09467b48Spatrick #include <cstdio>
16*09467b48Spatrick #include <map>
17*09467b48Spatrick #include <string>
18*09467b48Spatrick #include <vector>
19*09467b48Spatrick 
20*09467b48Spatrick using namespace llvm;
21*09467b48Spatrick 
22*09467b48Spatrick //===----------------------------------------------------------------------===//
23*09467b48Spatrick // Lexer
24*09467b48Spatrick //===----------------------------------------------------------------------===//
25*09467b48Spatrick 
26*09467b48Spatrick // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
27*09467b48Spatrick // of these for known things.
28*09467b48Spatrick enum Token {
29*09467b48Spatrick   tok_eof = -1,
30*09467b48Spatrick 
31*09467b48Spatrick   // commands
32*09467b48Spatrick   tok_def = -2, tok_extern = -3,
33*09467b48Spatrick 
34*09467b48Spatrick   // primary
35*09467b48Spatrick   tok_identifier = -4, tok_number = -5,
36*09467b48Spatrick 
37*09467b48Spatrick   // control
38*09467b48Spatrick   tok_if = -6, tok_then = -7, tok_else = -8,
39*09467b48Spatrick   tok_for = -9, tok_in = -10,
40*09467b48Spatrick 
41*09467b48Spatrick   // operators
42*09467b48Spatrick   tok_binary = -11, tok_unary = -12,
43*09467b48Spatrick 
44*09467b48Spatrick   // var definition
45*09467b48Spatrick   tok_var = -13
46*09467b48Spatrick };
47*09467b48Spatrick 
48*09467b48Spatrick static std::string IdentifierStr;  // Filled in if tok_identifier
49*09467b48Spatrick static double NumVal;              // Filled in if tok_number
50*09467b48Spatrick 
51*09467b48Spatrick /// gettok - Return the next token from standard input.
52*09467b48Spatrick static int gettok() {
53*09467b48Spatrick   static int LastChar = ' ';
54*09467b48Spatrick 
55*09467b48Spatrick   // Skip any whitespace.
56*09467b48Spatrick   while (isspace(LastChar))
57*09467b48Spatrick     LastChar = getchar();
58*09467b48Spatrick 
59*09467b48Spatrick   if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
60*09467b48Spatrick     IdentifierStr = LastChar;
61*09467b48Spatrick     while (isalnum((LastChar = getchar())))
62*09467b48Spatrick       IdentifierStr += LastChar;
63*09467b48Spatrick 
64*09467b48Spatrick     if (IdentifierStr == "def") return tok_def;
65*09467b48Spatrick     if (IdentifierStr == "extern") return tok_extern;
66*09467b48Spatrick     if (IdentifierStr == "if") return tok_if;
67*09467b48Spatrick     if (IdentifierStr == "then") return tok_then;
68*09467b48Spatrick     if (IdentifierStr == "else") return tok_else;
69*09467b48Spatrick     if (IdentifierStr == "for") return tok_for;
70*09467b48Spatrick     if (IdentifierStr == "in") return tok_in;
71*09467b48Spatrick     if (IdentifierStr == "binary") return tok_binary;
72*09467b48Spatrick     if (IdentifierStr == "unary") return tok_unary;
73*09467b48Spatrick     if (IdentifierStr == "var") return tok_var;
74*09467b48Spatrick     return tok_identifier;
75*09467b48Spatrick   }
76*09467b48Spatrick 
77*09467b48Spatrick   if (isdigit(LastChar) || LastChar == '.') {   // Number: [0-9.]+
78*09467b48Spatrick     std::string NumStr;
79*09467b48Spatrick     do {
80*09467b48Spatrick       NumStr += LastChar;
81*09467b48Spatrick       LastChar = getchar();
82*09467b48Spatrick     } while (isdigit(LastChar) || LastChar == '.');
83*09467b48Spatrick 
84*09467b48Spatrick     NumVal = strtod(NumStr.c_str(), 0);
85*09467b48Spatrick     return tok_number;
86*09467b48Spatrick   }
87*09467b48Spatrick 
88*09467b48Spatrick   if (LastChar == '#') {
89*09467b48Spatrick     // Comment until end of line.
90*09467b48Spatrick     do LastChar = getchar();
91*09467b48Spatrick     while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
92*09467b48Spatrick 
93*09467b48Spatrick     if (LastChar != EOF)
94*09467b48Spatrick       return gettok();
95*09467b48Spatrick   }
96*09467b48Spatrick 
97*09467b48Spatrick   // Check for end of file.  Don't eat the EOF.
98*09467b48Spatrick   if (LastChar == EOF)
99*09467b48Spatrick     return tok_eof;
100*09467b48Spatrick 
101*09467b48Spatrick   // Otherwise, just return the character as its ascii value.
102*09467b48Spatrick   int ThisChar = LastChar;
103*09467b48Spatrick   LastChar = getchar();
104*09467b48Spatrick   return ThisChar;
105*09467b48Spatrick }
106*09467b48Spatrick 
107*09467b48Spatrick //===----------------------------------------------------------------------===//
108*09467b48Spatrick // Abstract Syntax Tree (aka Parse Tree)
109*09467b48Spatrick //===----------------------------------------------------------------------===//
110*09467b48Spatrick 
111*09467b48Spatrick /// ExprAST - Base class for all expression nodes.
112*09467b48Spatrick class ExprAST {
113*09467b48Spatrick public:
114*09467b48Spatrick   virtual ~ExprAST() {}
115*09467b48Spatrick   virtual Value *Codegen() = 0;
116*09467b48Spatrick };
117*09467b48Spatrick 
118*09467b48Spatrick /// NumberExprAST - Expression class for numeric literals like "1.0".
119*09467b48Spatrick class NumberExprAST : public ExprAST {
120*09467b48Spatrick   double Val;
121*09467b48Spatrick public:
122*09467b48Spatrick   NumberExprAST(double val) : Val(val) {}
123*09467b48Spatrick   virtual Value *Codegen();
124*09467b48Spatrick };
125*09467b48Spatrick 
126*09467b48Spatrick /// VariableExprAST - Expression class for referencing a variable, like "a".
127*09467b48Spatrick class VariableExprAST : public ExprAST {
128*09467b48Spatrick   std::string Name;
129*09467b48Spatrick public:
130*09467b48Spatrick   VariableExprAST(const std::string &name) : Name(name) {}
131*09467b48Spatrick   const std::string &getName() const { return Name; }
132*09467b48Spatrick   virtual Value *Codegen();
133*09467b48Spatrick };
134*09467b48Spatrick 
135*09467b48Spatrick /// UnaryExprAST - Expression class for a unary operator.
136*09467b48Spatrick class UnaryExprAST : public ExprAST {
137*09467b48Spatrick   char Opcode;
138*09467b48Spatrick   ExprAST *Operand;
139*09467b48Spatrick public:
140*09467b48Spatrick   UnaryExprAST(char opcode, ExprAST *operand)
141*09467b48Spatrick     : Opcode(opcode), Operand(operand) {}
142*09467b48Spatrick   virtual Value *Codegen();
143*09467b48Spatrick };
144*09467b48Spatrick 
145*09467b48Spatrick /// BinaryExprAST - Expression class for a binary operator.
146*09467b48Spatrick class BinaryExprAST : public ExprAST {
147*09467b48Spatrick   char Op;
148*09467b48Spatrick   ExprAST *LHS, *RHS;
149*09467b48Spatrick public:
150*09467b48Spatrick   BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
151*09467b48Spatrick     : Op(op), LHS(lhs), RHS(rhs) {}
152*09467b48Spatrick   virtual Value *Codegen();
153*09467b48Spatrick };
154*09467b48Spatrick 
155*09467b48Spatrick /// CallExprAST - Expression class for function calls.
156*09467b48Spatrick class CallExprAST : public ExprAST {
157*09467b48Spatrick   std::string Callee;
158*09467b48Spatrick   std::vector<ExprAST*> Args;
159*09467b48Spatrick public:
160*09467b48Spatrick   CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
161*09467b48Spatrick     : Callee(callee), Args(args) {}
162*09467b48Spatrick   virtual Value *Codegen();
163*09467b48Spatrick };
164*09467b48Spatrick 
165*09467b48Spatrick /// IfExprAST - Expression class for if/then/else.
166*09467b48Spatrick class IfExprAST : public ExprAST {
167*09467b48Spatrick   ExprAST *Cond, *Then, *Else;
168*09467b48Spatrick public:
169*09467b48Spatrick   IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
170*09467b48Spatrick   : Cond(cond), Then(then), Else(_else) {}
171*09467b48Spatrick   virtual Value *Codegen();
172*09467b48Spatrick };
173*09467b48Spatrick 
174*09467b48Spatrick /// ForExprAST - Expression class for for/in.
175*09467b48Spatrick class ForExprAST : public ExprAST {
176*09467b48Spatrick   std::string VarName;
177*09467b48Spatrick   ExprAST *Start, *End, *Step, *Body;
178*09467b48Spatrick public:
179*09467b48Spatrick   ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
180*09467b48Spatrick              ExprAST *step, ExprAST *body)
181*09467b48Spatrick     : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
182*09467b48Spatrick   virtual Value *Codegen();
183*09467b48Spatrick };
184*09467b48Spatrick 
185*09467b48Spatrick /// VarExprAST - Expression class for var/in
186*09467b48Spatrick class VarExprAST : public ExprAST {
187*09467b48Spatrick   std::vector<std::pair<std::string, ExprAST*> > VarNames;
188*09467b48Spatrick   ExprAST *Body;
189*09467b48Spatrick public:
190*09467b48Spatrick   VarExprAST(const std::vector<std::pair<std::string, ExprAST*> > &varnames,
191*09467b48Spatrick              ExprAST *body)
192*09467b48Spatrick   : VarNames(varnames), Body(body) {}
193*09467b48Spatrick 
194*09467b48Spatrick   virtual Value *Codegen();
195*09467b48Spatrick };
196*09467b48Spatrick 
197*09467b48Spatrick /// PrototypeAST - This class represents the "prototype" for a function,
198*09467b48Spatrick /// which captures its argument names as well as if it is an operator.
199*09467b48Spatrick class PrototypeAST {
200*09467b48Spatrick   std::string Name;
201*09467b48Spatrick   std::vector<std::string> Args;
202*09467b48Spatrick   bool isOperator;
203*09467b48Spatrick   unsigned Precedence;  // Precedence if a binary op.
204*09467b48Spatrick public:
205*09467b48Spatrick   PrototypeAST(const std::string &name, const std::vector<std::string> &args,
206*09467b48Spatrick                bool isoperator = false, unsigned prec = 0)
207*09467b48Spatrick   : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
208*09467b48Spatrick 
209*09467b48Spatrick   bool isUnaryOp() const { return isOperator && Args.size() == 1; }
210*09467b48Spatrick   bool isBinaryOp() const { return isOperator && Args.size() == 2; }
211*09467b48Spatrick 
212*09467b48Spatrick   char getOperatorName() const {
213*09467b48Spatrick     assert(isUnaryOp() || isBinaryOp());
214*09467b48Spatrick     return Name[Name.size()-1];
215*09467b48Spatrick   }
216*09467b48Spatrick 
217*09467b48Spatrick   unsigned getBinaryPrecedence() const { return Precedence; }
218*09467b48Spatrick 
219*09467b48Spatrick   Function *Codegen();
220*09467b48Spatrick 
221*09467b48Spatrick   void CreateArgumentAllocas(Function *F);
222*09467b48Spatrick };
223*09467b48Spatrick 
224*09467b48Spatrick /// FunctionAST - This class represents a function definition itself.
225*09467b48Spatrick class FunctionAST {
226*09467b48Spatrick   PrototypeAST *Proto;
227*09467b48Spatrick   ExprAST *Body;
228*09467b48Spatrick public:
229*09467b48Spatrick   FunctionAST(PrototypeAST *proto, ExprAST *body)
230*09467b48Spatrick     : Proto(proto), Body(body) {}
231*09467b48Spatrick 
232*09467b48Spatrick   Function *Codegen();
233*09467b48Spatrick };
234*09467b48Spatrick 
235*09467b48Spatrick //===----------------------------------------------------------------------===//
236*09467b48Spatrick // Parser
237*09467b48Spatrick //===----------------------------------------------------------------------===//
238*09467b48Spatrick 
239*09467b48Spatrick /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current
240*09467b48Spatrick /// token the parser is looking at.  getNextToken reads another token from the
241*09467b48Spatrick /// lexer and updates CurTok with its results.
242*09467b48Spatrick static int CurTok;
243*09467b48Spatrick static int getNextToken() {
244*09467b48Spatrick   return CurTok = gettok();
245*09467b48Spatrick }
246*09467b48Spatrick 
247*09467b48Spatrick /// BinopPrecedence - This holds the precedence for each binary operator that is
248*09467b48Spatrick /// defined.
249*09467b48Spatrick static std::map<char, int> BinopPrecedence;
250*09467b48Spatrick 
251*09467b48Spatrick /// GetTokPrecedence - Get the precedence of the pending binary operator token.
252*09467b48Spatrick static int GetTokPrecedence() {
253*09467b48Spatrick   if (!isascii(CurTok))
254*09467b48Spatrick     return -1;
255*09467b48Spatrick 
256*09467b48Spatrick   // Make sure it's a declared binop.
257*09467b48Spatrick   int TokPrec = BinopPrecedence[CurTok];
258*09467b48Spatrick   if (TokPrec <= 0) return -1;
259*09467b48Spatrick   return TokPrec;
260*09467b48Spatrick }
261*09467b48Spatrick 
262*09467b48Spatrick /// Error* - These are little helper functions for error handling.
263*09467b48Spatrick ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
264*09467b48Spatrick PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
265*09467b48Spatrick FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
266*09467b48Spatrick 
267*09467b48Spatrick static ExprAST *ParseExpression();
268*09467b48Spatrick 
269*09467b48Spatrick /// identifierexpr
270*09467b48Spatrick ///   ::= identifier
271*09467b48Spatrick ///   ::= identifier '(' expression* ')'
272*09467b48Spatrick static ExprAST *ParseIdentifierExpr() {
273*09467b48Spatrick   std::string IdName = IdentifierStr;
274*09467b48Spatrick 
275*09467b48Spatrick   getNextToken();  // eat identifier.
276*09467b48Spatrick 
277*09467b48Spatrick   if (CurTok != '(') // Simple variable ref.
278*09467b48Spatrick     return new VariableExprAST(IdName);
279*09467b48Spatrick 
280*09467b48Spatrick   // Call.
281*09467b48Spatrick   getNextToken();  // eat (
282*09467b48Spatrick   std::vector<ExprAST*> Args;
283*09467b48Spatrick   if (CurTok != ')') {
284*09467b48Spatrick     while (1) {
285*09467b48Spatrick       ExprAST *Arg = ParseExpression();
286*09467b48Spatrick       if (!Arg) return 0;
287*09467b48Spatrick       Args.push_back(Arg);
288*09467b48Spatrick 
289*09467b48Spatrick       if (CurTok == ')') break;
290*09467b48Spatrick 
291*09467b48Spatrick       if (CurTok != ',')
292*09467b48Spatrick         return Error("Expected ')' or ',' in argument list");
293*09467b48Spatrick       getNextToken();
294*09467b48Spatrick     }
295*09467b48Spatrick   }
296*09467b48Spatrick 
297*09467b48Spatrick   // Eat the ')'.
298*09467b48Spatrick   getNextToken();
299*09467b48Spatrick 
300*09467b48Spatrick   return new CallExprAST(IdName, Args);
301*09467b48Spatrick }
302*09467b48Spatrick 
303*09467b48Spatrick /// numberexpr ::= number
304*09467b48Spatrick static ExprAST *ParseNumberExpr() {
305*09467b48Spatrick   ExprAST *Result = new NumberExprAST(NumVal);
306*09467b48Spatrick   getNextToken(); // consume the number
307*09467b48Spatrick   return Result;
308*09467b48Spatrick }
309*09467b48Spatrick 
310*09467b48Spatrick /// parenexpr ::= '(' expression ')'
311*09467b48Spatrick static ExprAST *ParseParenExpr() {
312*09467b48Spatrick   getNextToken();  // eat (.
313*09467b48Spatrick   ExprAST *V = ParseExpression();
314*09467b48Spatrick   if (!V) return 0;
315*09467b48Spatrick 
316*09467b48Spatrick   if (CurTok != ')')
317*09467b48Spatrick     return Error("expected ')'");
318*09467b48Spatrick   getNextToken();  // eat ).
319*09467b48Spatrick   return V;
320*09467b48Spatrick }
321*09467b48Spatrick 
322*09467b48Spatrick /// ifexpr ::= 'if' expression 'then' expression 'else' expression
323*09467b48Spatrick static ExprAST *ParseIfExpr() {
324*09467b48Spatrick   getNextToken();  // eat the if.
325*09467b48Spatrick 
326*09467b48Spatrick   // condition.
327*09467b48Spatrick   ExprAST *Cond = ParseExpression();
328*09467b48Spatrick   if (!Cond) return 0;
329*09467b48Spatrick 
330*09467b48Spatrick   if (CurTok != tok_then)
331*09467b48Spatrick     return Error("expected then");
332*09467b48Spatrick   getNextToken();  // eat the then
333*09467b48Spatrick 
334*09467b48Spatrick   ExprAST *Then = ParseExpression();
335*09467b48Spatrick   if (Then == 0) return 0;
336*09467b48Spatrick 
337*09467b48Spatrick   if (CurTok != tok_else)
338*09467b48Spatrick     return Error("expected else");
339*09467b48Spatrick 
340*09467b48Spatrick   getNextToken();
341*09467b48Spatrick 
342*09467b48Spatrick   ExprAST *Else = ParseExpression();
343*09467b48Spatrick   if (!Else) return 0;
344*09467b48Spatrick 
345*09467b48Spatrick   return new IfExprAST(Cond, Then, Else);
346*09467b48Spatrick }
347*09467b48Spatrick 
348*09467b48Spatrick /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
349*09467b48Spatrick static ExprAST *ParseForExpr() {
350*09467b48Spatrick   getNextToken();  // eat the for.
351*09467b48Spatrick 
352*09467b48Spatrick   if (CurTok != tok_identifier)
353*09467b48Spatrick     return Error("expected identifier after for");
354*09467b48Spatrick 
355*09467b48Spatrick   std::string IdName = IdentifierStr;
356*09467b48Spatrick   getNextToken();  // eat identifier.
357*09467b48Spatrick 
358*09467b48Spatrick   if (CurTok != '=')
359*09467b48Spatrick     return Error("expected '=' after for");
360*09467b48Spatrick   getNextToken();  // eat '='.
361*09467b48Spatrick 
362*09467b48Spatrick 
363*09467b48Spatrick   ExprAST *Start = ParseExpression();
364*09467b48Spatrick   if (Start == 0) return 0;
365*09467b48Spatrick   if (CurTok != ',')
366*09467b48Spatrick     return Error("expected ',' after for start value");
367*09467b48Spatrick   getNextToken();
368*09467b48Spatrick 
369*09467b48Spatrick   ExprAST *End = ParseExpression();
370*09467b48Spatrick   if (End == 0) return 0;
371*09467b48Spatrick 
372*09467b48Spatrick   // The step value is optional.
373*09467b48Spatrick   ExprAST *Step = 0;
374*09467b48Spatrick   if (CurTok == ',') {
375*09467b48Spatrick     getNextToken();
376*09467b48Spatrick     Step = ParseExpression();
377*09467b48Spatrick     if (Step == 0) return 0;
378*09467b48Spatrick   }
379*09467b48Spatrick 
380*09467b48Spatrick   if (CurTok != tok_in)
381*09467b48Spatrick     return Error("expected 'in' after for");
382*09467b48Spatrick   getNextToken();  // eat 'in'.
383*09467b48Spatrick 
384*09467b48Spatrick   ExprAST *Body = ParseExpression();
385*09467b48Spatrick   if (Body == 0) return 0;
386*09467b48Spatrick 
387*09467b48Spatrick   return new ForExprAST(IdName, Start, End, Step, Body);
388*09467b48Spatrick }
389*09467b48Spatrick 
390*09467b48Spatrick /// varexpr ::= 'var' identifier ('=' expression)?
391*09467b48Spatrick //                    (',' identifier ('=' expression)?)* 'in' expression
392*09467b48Spatrick static ExprAST *ParseVarExpr() {
393*09467b48Spatrick   getNextToken();  // eat the var.
394*09467b48Spatrick 
395*09467b48Spatrick   std::vector<std::pair<std::string, ExprAST*> > VarNames;
396*09467b48Spatrick 
397*09467b48Spatrick   // At least one variable name is required.
398*09467b48Spatrick   if (CurTok != tok_identifier)
399*09467b48Spatrick     return Error("expected identifier after var");
400*09467b48Spatrick 
401*09467b48Spatrick   while (1) {
402*09467b48Spatrick     std::string Name = IdentifierStr;
403*09467b48Spatrick     getNextToken();  // eat identifier.
404*09467b48Spatrick 
405*09467b48Spatrick     // Read the optional initializer.
406*09467b48Spatrick     ExprAST *Init = 0;
407*09467b48Spatrick     if (CurTok == '=') {
408*09467b48Spatrick       getNextToken(); // eat the '='.
409*09467b48Spatrick 
410*09467b48Spatrick       Init = ParseExpression();
411*09467b48Spatrick       if (Init == 0) return 0;
412*09467b48Spatrick     }
413*09467b48Spatrick 
414*09467b48Spatrick     VarNames.push_back(std::make_pair(Name, Init));
415*09467b48Spatrick 
416*09467b48Spatrick     // End of var list, exit loop.
417*09467b48Spatrick     if (CurTok != ',') break;
418*09467b48Spatrick     getNextToken(); // eat the ','.
419*09467b48Spatrick 
420*09467b48Spatrick     if (CurTok != tok_identifier)
421*09467b48Spatrick       return Error("expected identifier list after var");
422*09467b48Spatrick   }
423*09467b48Spatrick 
424*09467b48Spatrick   // At this point, we have to have 'in'.
425*09467b48Spatrick   if (CurTok != tok_in)
426*09467b48Spatrick     return Error("expected 'in' keyword after 'var'");
427*09467b48Spatrick   getNextToken();  // eat 'in'.
428*09467b48Spatrick 
429*09467b48Spatrick   ExprAST *Body = ParseExpression();
430*09467b48Spatrick   if (Body == 0) return 0;
431*09467b48Spatrick 
432*09467b48Spatrick   return new VarExprAST(VarNames, Body);
433*09467b48Spatrick }
434*09467b48Spatrick 
435*09467b48Spatrick /// primary
436*09467b48Spatrick ///   ::= identifierexpr
437*09467b48Spatrick ///   ::= numberexpr
438*09467b48Spatrick ///   ::= parenexpr
439*09467b48Spatrick ///   ::= ifexpr
440*09467b48Spatrick ///   ::= forexpr
441*09467b48Spatrick ///   ::= varexpr
442*09467b48Spatrick static ExprAST *ParsePrimary() {
443*09467b48Spatrick   switch (CurTok) {
444*09467b48Spatrick   default: return Error("unknown token when expecting an expression");
445*09467b48Spatrick   case tok_identifier: return ParseIdentifierExpr();
446*09467b48Spatrick   case tok_number:     return ParseNumberExpr();
447*09467b48Spatrick   case '(':            return ParseParenExpr();
448*09467b48Spatrick   case tok_if:         return ParseIfExpr();
449*09467b48Spatrick   case tok_for:        return ParseForExpr();
450*09467b48Spatrick   case tok_var:        return ParseVarExpr();
451*09467b48Spatrick   }
452*09467b48Spatrick }
453*09467b48Spatrick 
454*09467b48Spatrick /// unary
455*09467b48Spatrick ///   ::= primary
456*09467b48Spatrick ///   ::= '!' unary
457*09467b48Spatrick static ExprAST *ParseUnary() {
458*09467b48Spatrick   // If the current token is not an operator, it must be a primary expr.
459*09467b48Spatrick   if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
460*09467b48Spatrick     return ParsePrimary();
461*09467b48Spatrick 
462*09467b48Spatrick   // If this is a unary operator, read it.
463*09467b48Spatrick   int Opc = CurTok;
464*09467b48Spatrick   getNextToken();
465*09467b48Spatrick   if (ExprAST *Operand = ParseUnary())
466*09467b48Spatrick     return new UnaryExprAST(Opc, Operand);
467*09467b48Spatrick   return 0;
468*09467b48Spatrick }
469*09467b48Spatrick 
470*09467b48Spatrick /// binoprhs
471*09467b48Spatrick ///   ::= ('+' unary)*
472*09467b48Spatrick static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
473*09467b48Spatrick   // If this is a binop, find its precedence.
474*09467b48Spatrick   while (1) {
475*09467b48Spatrick     int TokPrec = GetTokPrecedence();
476*09467b48Spatrick 
477*09467b48Spatrick     // If this is a binop that binds at least as tightly as the current binop,
478*09467b48Spatrick     // consume it, otherwise we are done.
479*09467b48Spatrick     if (TokPrec < ExprPrec)
480*09467b48Spatrick       return LHS;
481*09467b48Spatrick 
482*09467b48Spatrick     // Okay, we know this is a binop.
483*09467b48Spatrick     int BinOp = CurTok;
484*09467b48Spatrick     getNextToken();  // eat binop
485*09467b48Spatrick 
486*09467b48Spatrick     // Parse the unary expression after the binary operator.
487*09467b48Spatrick     ExprAST *RHS = ParseUnary();
488*09467b48Spatrick     if (!RHS) return 0;
489*09467b48Spatrick 
490*09467b48Spatrick     // If BinOp binds less tightly with RHS than the operator after RHS, let
491*09467b48Spatrick     // the pending operator take RHS as its LHS.
492*09467b48Spatrick     int NextPrec = GetTokPrecedence();
493*09467b48Spatrick     if (TokPrec < NextPrec) {
494*09467b48Spatrick       RHS = ParseBinOpRHS(TokPrec+1, RHS);
495*09467b48Spatrick       if (RHS == 0) return 0;
496*09467b48Spatrick     }
497*09467b48Spatrick 
498*09467b48Spatrick     // Merge LHS/RHS.
499*09467b48Spatrick     LHS = new BinaryExprAST(BinOp, LHS, RHS);
500*09467b48Spatrick   }
501*09467b48Spatrick }
502*09467b48Spatrick 
503*09467b48Spatrick /// expression
504*09467b48Spatrick ///   ::= unary binoprhs
505*09467b48Spatrick ///
506*09467b48Spatrick static ExprAST *ParseExpression() {
507*09467b48Spatrick   ExprAST *LHS = ParseUnary();
508*09467b48Spatrick   if (!LHS) return 0;
509*09467b48Spatrick 
510*09467b48Spatrick   return ParseBinOpRHS(0, LHS);
511*09467b48Spatrick }
512*09467b48Spatrick 
513*09467b48Spatrick /// prototype
514*09467b48Spatrick ///   ::= id '(' id* ')'
515*09467b48Spatrick ///   ::= binary LETTER number? (id, id)
516*09467b48Spatrick ///   ::= unary LETTER (id)
517*09467b48Spatrick static PrototypeAST *ParsePrototype() {
518*09467b48Spatrick   std::string FnName;
519*09467b48Spatrick 
520*09467b48Spatrick   unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
521*09467b48Spatrick   unsigned BinaryPrecedence = 30;
522*09467b48Spatrick 
523*09467b48Spatrick   switch (CurTok) {
524*09467b48Spatrick   default:
525*09467b48Spatrick     return ErrorP("Expected function name in prototype");
526*09467b48Spatrick   case tok_identifier:
527*09467b48Spatrick     FnName = IdentifierStr;
528*09467b48Spatrick     Kind = 0;
529*09467b48Spatrick     getNextToken();
530*09467b48Spatrick     break;
531*09467b48Spatrick   case tok_unary:
532*09467b48Spatrick     getNextToken();
533*09467b48Spatrick     if (!isascii(CurTok))
534*09467b48Spatrick       return ErrorP("Expected unary operator");
535*09467b48Spatrick     FnName = "unary";
536*09467b48Spatrick     FnName += (char)CurTok;
537*09467b48Spatrick     Kind = 1;
538*09467b48Spatrick     getNextToken();
539*09467b48Spatrick     break;
540*09467b48Spatrick   case tok_binary:
541*09467b48Spatrick     getNextToken();
542*09467b48Spatrick     if (!isascii(CurTok))
543*09467b48Spatrick       return ErrorP("Expected binary operator");
544*09467b48Spatrick     FnName = "binary";
545*09467b48Spatrick     FnName += (char)CurTok;
546*09467b48Spatrick     Kind = 2;
547*09467b48Spatrick     getNextToken();
548*09467b48Spatrick 
549*09467b48Spatrick     // Read the precedence if present.
550*09467b48Spatrick     if (CurTok == tok_number) {
551*09467b48Spatrick       if (NumVal < 1 || NumVal > 100)
552*09467b48Spatrick         return ErrorP("Invalid precedecnce: must be 1..100");
553*09467b48Spatrick       BinaryPrecedence = (unsigned)NumVal;
554*09467b48Spatrick       getNextToken();
555*09467b48Spatrick     }
556*09467b48Spatrick     break;
557*09467b48Spatrick   }
558*09467b48Spatrick 
559*09467b48Spatrick   if (CurTok != '(')
560*09467b48Spatrick     return ErrorP("Expected '(' in prototype");
561*09467b48Spatrick 
562*09467b48Spatrick   std::vector<std::string> ArgNames;
563*09467b48Spatrick   while (getNextToken() == tok_identifier)
564*09467b48Spatrick     ArgNames.push_back(IdentifierStr);
565*09467b48Spatrick   if (CurTok != ')')
566*09467b48Spatrick     return ErrorP("Expected ')' in prototype");
567*09467b48Spatrick 
568*09467b48Spatrick   // success.
569*09467b48Spatrick   getNextToken();  // eat ')'.
570*09467b48Spatrick 
571*09467b48Spatrick   // Verify right number of names for operator.
572*09467b48Spatrick   if (Kind && ArgNames.size() != Kind)
573*09467b48Spatrick     return ErrorP("Invalid number of operands for operator");
574*09467b48Spatrick 
575*09467b48Spatrick   return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
576*09467b48Spatrick }
577*09467b48Spatrick 
578*09467b48Spatrick /// definition ::= 'def' prototype expression
579*09467b48Spatrick static FunctionAST *ParseDefinition() {
580*09467b48Spatrick   getNextToken();  // eat def.
581*09467b48Spatrick   PrototypeAST *Proto = ParsePrototype();
582*09467b48Spatrick   if (Proto == 0) return 0;
583*09467b48Spatrick 
584*09467b48Spatrick   if (ExprAST *E = ParseExpression())
585*09467b48Spatrick     return new FunctionAST(Proto, E);
586*09467b48Spatrick   return 0;
587*09467b48Spatrick }
588*09467b48Spatrick 
589*09467b48Spatrick /// toplevelexpr ::= expression
590*09467b48Spatrick static FunctionAST *ParseTopLevelExpr() {
591*09467b48Spatrick   if (ExprAST *E = ParseExpression()) {
592*09467b48Spatrick     // Make an anonymous proto.
593*09467b48Spatrick     PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
594*09467b48Spatrick     return new FunctionAST(Proto, E);
595*09467b48Spatrick   }
596*09467b48Spatrick   return 0;
597*09467b48Spatrick }
598*09467b48Spatrick 
599*09467b48Spatrick /// external ::= 'extern' prototype
600*09467b48Spatrick static PrototypeAST *ParseExtern() {
601*09467b48Spatrick   getNextToken();  // eat extern.
602*09467b48Spatrick   return ParsePrototype();
603*09467b48Spatrick }
604*09467b48Spatrick 
605*09467b48Spatrick //===----------------------------------------------------------------------===//
606*09467b48Spatrick // Code Generation
607*09467b48Spatrick //===----------------------------------------------------------------------===//
608*09467b48Spatrick 
609*09467b48Spatrick static Module *TheModule;
610*09467b48Spatrick static FunctionPassManager *TheFPM;
611*09467b48Spatrick static LLVMContext TheContext;
612*09467b48Spatrick static IRBuilder<> Builder(TheContext);
613*09467b48Spatrick static std::map<std::string, AllocaInst*> NamedValues;
614*09467b48Spatrick 
615*09467b48Spatrick Value *ErrorV(const char *Str) { Error(Str); return 0; }
616*09467b48Spatrick 
617*09467b48Spatrick /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
618*09467b48Spatrick /// the function.  This is used for mutable variables etc.
619*09467b48Spatrick static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
620*09467b48Spatrick                                           const std::string &VarName) {
621*09467b48Spatrick   IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
622*09467b48Spatrick                  TheFunction->getEntryBlock().begin());
623*09467b48Spatrick   return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str());
624*09467b48Spatrick }
625*09467b48Spatrick 
626*09467b48Spatrick Value *NumberExprAST::Codegen() {
627*09467b48Spatrick   return ConstantFP::get(TheContext, APFloat(Val));
628*09467b48Spatrick }
629*09467b48Spatrick 
630*09467b48Spatrick Value *VariableExprAST::Codegen() {
631*09467b48Spatrick   // Look this variable up in the function.
632*09467b48Spatrick   Value *V = NamedValues[Name];
633*09467b48Spatrick   if (V == 0) return ErrorV("Unknown variable name");
634*09467b48Spatrick 
635*09467b48Spatrick   // Load the value.
636*09467b48Spatrick   return Builder.CreateLoad(V, Name.c_str());
637*09467b48Spatrick }
638*09467b48Spatrick 
639*09467b48Spatrick Value *UnaryExprAST::Codegen() {
640*09467b48Spatrick   Value *OperandV = Operand->Codegen();
641*09467b48Spatrick   if (OperandV == 0) return 0;
642*09467b48Spatrick #ifdef USE_MCJIT
643*09467b48Spatrick   Function *F = TheHelper->getFunction(MakeLegalFunctionName(std::string("unary")+Opcode));
644*09467b48Spatrick #else
645*09467b48Spatrick   Function *F = TheModule->getFunction(std::string("unary")+Opcode);
646*09467b48Spatrick #endif
647*09467b48Spatrick   if (F == 0)
648*09467b48Spatrick     return ErrorV("Unknown unary operator");
649*09467b48Spatrick 
650*09467b48Spatrick   return Builder.CreateCall(F, OperandV, "unop");
651*09467b48Spatrick }
652*09467b48Spatrick 
653*09467b48Spatrick Value *BinaryExprAST::Codegen() {
654*09467b48Spatrick   // Special case '=' because we don't want to emit the LHS as an expression.
655*09467b48Spatrick   if (Op == '=') {
656*09467b48Spatrick     // Assignment requires the LHS to be an identifier.
657*09467b48Spatrick     VariableExprAST *LHSE = dynamic_cast<VariableExprAST*>(LHS);
658*09467b48Spatrick     if (!LHSE)
659*09467b48Spatrick       return ErrorV("destination of '=' must be a variable");
660*09467b48Spatrick     // Codegen the RHS.
661*09467b48Spatrick     Value *Val = RHS->Codegen();
662*09467b48Spatrick     if (Val == 0) return 0;
663*09467b48Spatrick 
664*09467b48Spatrick     // Look up the name.
665*09467b48Spatrick     Value *Variable = NamedValues[LHSE->getName()];
666*09467b48Spatrick     if (Variable == 0) return ErrorV("Unknown variable name");
667*09467b48Spatrick 
668*09467b48Spatrick     Builder.CreateStore(Val, Variable);
669*09467b48Spatrick     return Val;
670*09467b48Spatrick   }
671*09467b48Spatrick 
672*09467b48Spatrick   Value *L = LHS->Codegen();
673*09467b48Spatrick   Value *R = RHS->Codegen();
674*09467b48Spatrick   if (L == 0 || R == 0) return 0;
675*09467b48Spatrick 
676*09467b48Spatrick   switch (Op) {
677*09467b48Spatrick   case '+': return Builder.CreateFAdd(L, R, "addtmp");
678*09467b48Spatrick   case '-': return Builder.CreateFSub(L, R, "subtmp");
679*09467b48Spatrick   case '*': return Builder.CreateFMul(L, R, "multmp");
680*09467b48Spatrick   case '/': return Builder.CreateFDiv(L, R, "divtmp");
681*09467b48Spatrick   case '<':
682*09467b48Spatrick     L = Builder.CreateFCmpULT(L, R, "cmptmp");
683*09467b48Spatrick     // Convert bool 0/1 to double 0.0 or 1.0
684*09467b48Spatrick     return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
685*09467b48Spatrick   default: break;
686*09467b48Spatrick   }
687*09467b48Spatrick 
688*09467b48Spatrick   // If it wasn't a builtin binary operator, it must be a user defined one. Emit
689*09467b48Spatrick   // a call to it.
690*09467b48Spatrick   Function *F = TheModule->getFunction(std::string("binary")+Op);
691*09467b48Spatrick   assert(F && "binary operator not found!");
692*09467b48Spatrick 
693*09467b48Spatrick   Value *Ops[] = { L, R };
694*09467b48Spatrick   return Builder.CreateCall(F, Ops, "binop");
695*09467b48Spatrick }
696*09467b48Spatrick 
697*09467b48Spatrick Value *CallExprAST::Codegen() {
698*09467b48Spatrick   // Look up the name in the global module table.
699*09467b48Spatrick   Function *CalleeF = TheModule->getFunction(Callee);
700*09467b48Spatrick   if (CalleeF == 0) {
701*09467b48Spatrick     char error_str[64];
702*09467b48Spatrick     sprintf(error_str, "Unknown function referenced %s", Callee.c_str());
703*09467b48Spatrick     return ErrorV(error_str);
704*09467b48Spatrick   }
705*09467b48Spatrick 
706*09467b48Spatrick   // If argument mismatch error.
707*09467b48Spatrick   if (CalleeF->arg_size() != Args.size())
708*09467b48Spatrick     return ErrorV("Incorrect # arguments passed");
709*09467b48Spatrick 
710*09467b48Spatrick   std::vector<Value*> ArgsV;
711*09467b48Spatrick   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
712*09467b48Spatrick     ArgsV.push_back(Args[i]->Codegen());
713*09467b48Spatrick     if (ArgsV.back() == 0) return 0;
714*09467b48Spatrick   }
715*09467b48Spatrick 
716*09467b48Spatrick   return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
717*09467b48Spatrick }
718*09467b48Spatrick 
719*09467b48Spatrick Value *IfExprAST::Codegen() {
720*09467b48Spatrick   Value *CondV = Cond->Codegen();
721*09467b48Spatrick   if (CondV == 0) return 0;
722*09467b48Spatrick 
723*09467b48Spatrick   // Convert condition to a bool by comparing equal to 0.0.
724*09467b48Spatrick   CondV = Builder.CreateFCmpONE(
725*09467b48Spatrick       CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
726*09467b48Spatrick 
727*09467b48Spatrick   Function *TheFunction = Builder.GetInsertBlock()->getParent();
728*09467b48Spatrick 
729*09467b48Spatrick   // Create blocks for the then and else cases.  Insert the 'then' block at the
730*09467b48Spatrick   // end of the function.
731*09467b48Spatrick   BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
732*09467b48Spatrick   BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
733*09467b48Spatrick   BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
734*09467b48Spatrick 
735*09467b48Spatrick   Builder.CreateCondBr(CondV, ThenBB, ElseBB);
736*09467b48Spatrick 
737*09467b48Spatrick   // Emit then value.
738*09467b48Spatrick   Builder.SetInsertPoint(ThenBB);
739*09467b48Spatrick 
740*09467b48Spatrick   Value *ThenV = Then->Codegen();
741*09467b48Spatrick   if (ThenV == 0) return 0;
742*09467b48Spatrick 
743*09467b48Spatrick   Builder.CreateBr(MergeBB);
744*09467b48Spatrick   // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
745*09467b48Spatrick   ThenBB = Builder.GetInsertBlock();
746*09467b48Spatrick 
747*09467b48Spatrick   // Emit else block.
748*09467b48Spatrick   TheFunction->getBasicBlockList().push_back(ElseBB);
749*09467b48Spatrick   Builder.SetInsertPoint(ElseBB);
750*09467b48Spatrick 
751*09467b48Spatrick   Value *ElseV = Else->Codegen();
752*09467b48Spatrick   if (ElseV == 0) return 0;
753*09467b48Spatrick 
754*09467b48Spatrick   Builder.CreateBr(MergeBB);
755*09467b48Spatrick   // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
756*09467b48Spatrick   ElseBB = Builder.GetInsertBlock();
757*09467b48Spatrick 
758*09467b48Spatrick   // Emit merge block.
759*09467b48Spatrick   TheFunction->getBasicBlockList().push_back(MergeBB);
760*09467b48Spatrick   Builder.SetInsertPoint(MergeBB);
761*09467b48Spatrick   PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
762*09467b48Spatrick 
763*09467b48Spatrick   PN->addIncoming(ThenV, ThenBB);
764*09467b48Spatrick   PN->addIncoming(ElseV, ElseBB);
765*09467b48Spatrick   return PN;
766*09467b48Spatrick }
767*09467b48Spatrick 
768*09467b48Spatrick Value *ForExprAST::Codegen() {
769*09467b48Spatrick   // Output this as:
770*09467b48Spatrick   //   var = alloca double
771*09467b48Spatrick   //   ...
772*09467b48Spatrick   //   start = startexpr
773*09467b48Spatrick   //   store start -> var
774*09467b48Spatrick   //   goto loop
775*09467b48Spatrick   // loop:
776*09467b48Spatrick   //   ...
777*09467b48Spatrick   //   bodyexpr
778*09467b48Spatrick   //   ...
779*09467b48Spatrick   // loopend:
780*09467b48Spatrick   //   step = stepexpr
781*09467b48Spatrick   //   endcond = endexpr
782*09467b48Spatrick   //
783*09467b48Spatrick   //   curvar = load var
784*09467b48Spatrick   //   nextvar = curvar + step
785*09467b48Spatrick   //   store nextvar -> var
786*09467b48Spatrick   //   br endcond, loop, endloop
787*09467b48Spatrick   // outloop:
788*09467b48Spatrick 
789*09467b48Spatrick   Function *TheFunction = Builder.GetInsertBlock()->getParent();
790*09467b48Spatrick 
791*09467b48Spatrick   // Create an alloca for the variable in the entry block.
792*09467b48Spatrick   AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
793*09467b48Spatrick 
794*09467b48Spatrick   // Emit the start code first, without 'variable' in scope.
795*09467b48Spatrick   Value *StartVal = Start->Codegen();
796*09467b48Spatrick   if (StartVal == 0) return 0;
797*09467b48Spatrick 
798*09467b48Spatrick   // Store the value into the alloca.
799*09467b48Spatrick   Builder.CreateStore(StartVal, Alloca);
800*09467b48Spatrick 
801*09467b48Spatrick   // Make the new basic block for the loop header, inserting after current
802*09467b48Spatrick   // block.
803*09467b48Spatrick   BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
804*09467b48Spatrick 
805*09467b48Spatrick   // Insert an explicit fall through from the current block to the LoopBB.
806*09467b48Spatrick   Builder.CreateBr(LoopBB);
807*09467b48Spatrick 
808*09467b48Spatrick   // Start insertion in LoopBB.
809*09467b48Spatrick   Builder.SetInsertPoint(LoopBB);
810*09467b48Spatrick 
811*09467b48Spatrick   // Within the loop, the variable is defined equal to the PHI node.  If it
812*09467b48Spatrick   // shadows an existing variable, we have to restore it, so save it now.
813*09467b48Spatrick   AllocaInst *OldVal = NamedValues[VarName];
814*09467b48Spatrick   NamedValues[VarName] = Alloca;
815*09467b48Spatrick 
816*09467b48Spatrick   // Emit the body of the loop.  This, like any other expr, can change the
817*09467b48Spatrick   // current BB.  Note that we ignore the value computed by the body, but don't
818*09467b48Spatrick   // allow an error.
819*09467b48Spatrick   if (Body->Codegen() == 0)
820*09467b48Spatrick     return 0;
821*09467b48Spatrick 
822*09467b48Spatrick   // Emit the step value.
823*09467b48Spatrick   Value *StepVal;
824*09467b48Spatrick   if (Step) {
825*09467b48Spatrick     StepVal = Step->Codegen();
826*09467b48Spatrick     if (StepVal == 0) return 0;
827*09467b48Spatrick   } else {
828*09467b48Spatrick     // If not specified, use 1.0.
829*09467b48Spatrick     StepVal = ConstantFP::get(TheContext, APFloat(1.0));
830*09467b48Spatrick   }
831*09467b48Spatrick 
832*09467b48Spatrick   // Compute the end condition.
833*09467b48Spatrick   Value *EndCond = End->Codegen();
834*09467b48Spatrick   if (EndCond == 0) return EndCond;
835*09467b48Spatrick 
836*09467b48Spatrick   // Reload, increment, and restore the alloca.  This handles the case where
837*09467b48Spatrick   // the body of the loop mutates the variable.
838*09467b48Spatrick   Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
839*09467b48Spatrick   Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
840*09467b48Spatrick   Builder.CreateStore(NextVar, Alloca);
841*09467b48Spatrick 
842*09467b48Spatrick   // Convert condition to a bool by comparing equal to 0.0.
843*09467b48Spatrick   EndCond = Builder.CreateFCmpONE(
844*09467b48Spatrick       EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
845*09467b48Spatrick 
846*09467b48Spatrick   // Create the "after loop" block and insert it.
847*09467b48Spatrick   BasicBlock *AfterBB =
848*09467b48Spatrick       BasicBlock::Create(TheContext, "afterloop", TheFunction);
849*09467b48Spatrick 
850*09467b48Spatrick   // Insert the conditional branch into the end of LoopEndBB.
851*09467b48Spatrick   Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
852*09467b48Spatrick 
853*09467b48Spatrick   // Any new code will be inserted in AfterBB.
854*09467b48Spatrick   Builder.SetInsertPoint(AfterBB);
855*09467b48Spatrick 
856*09467b48Spatrick   // Restore the unshadowed variable.
857*09467b48Spatrick   if (OldVal)
858*09467b48Spatrick     NamedValues[VarName] = OldVal;
859*09467b48Spatrick   else
860*09467b48Spatrick     NamedValues.erase(VarName);
861*09467b48Spatrick 
862*09467b48Spatrick 
863*09467b48Spatrick   // for expr always returns 0.0.
864*09467b48Spatrick   return Constant::getNullValue(Type::getDoubleTy(TheContext));
865*09467b48Spatrick }
866*09467b48Spatrick 
867*09467b48Spatrick Value *VarExprAST::Codegen() {
868*09467b48Spatrick   std::vector<AllocaInst *> OldBindings;
869*09467b48Spatrick 
870*09467b48Spatrick   Function *TheFunction = Builder.GetInsertBlock()->getParent();
871*09467b48Spatrick 
872*09467b48Spatrick   // Register all variables and emit their initializer.
873*09467b48Spatrick   for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
874*09467b48Spatrick     const std::string &VarName = VarNames[i].first;
875*09467b48Spatrick     ExprAST *Init = VarNames[i].second;
876*09467b48Spatrick 
877*09467b48Spatrick     // Emit the initializer before adding the variable to scope, this prevents
878*09467b48Spatrick     // the initializer from referencing the variable itself, and permits stuff
879*09467b48Spatrick     // like this:
880*09467b48Spatrick     //  var a = 1 in
881*09467b48Spatrick     //    var a = a in ...   # refers to outer 'a'.
882*09467b48Spatrick     Value *InitVal;
883*09467b48Spatrick     if (Init) {
884*09467b48Spatrick       InitVal = Init->Codegen();
885*09467b48Spatrick       if (InitVal == 0) return 0;
886*09467b48Spatrick     } else { // If not specified, use 0.0.
887*09467b48Spatrick       InitVal = ConstantFP::get(TheContext, APFloat(0.0));
888*09467b48Spatrick     }
889*09467b48Spatrick 
890*09467b48Spatrick     AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
891*09467b48Spatrick     Builder.CreateStore(InitVal, Alloca);
892*09467b48Spatrick 
893*09467b48Spatrick     // Remember the old variable binding so that we can restore the binding when
894*09467b48Spatrick     // we unrecurse.
895*09467b48Spatrick     OldBindings.push_back(NamedValues[VarName]);
896*09467b48Spatrick 
897*09467b48Spatrick     // Remember this binding.
898*09467b48Spatrick     NamedValues[VarName] = Alloca;
899*09467b48Spatrick   }
900*09467b48Spatrick 
901*09467b48Spatrick   // Codegen the body, now that all vars are in scope.
902*09467b48Spatrick   Value *BodyVal = Body->Codegen();
903*09467b48Spatrick   if (BodyVal == 0) return 0;
904*09467b48Spatrick 
905*09467b48Spatrick   // Pop all our variables from scope.
906*09467b48Spatrick   for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
907*09467b48Spatrick     NamedValues[VarNames[i].first] = OldBindings[i];
908*09467b48Spatrick 
909*09467b48Spatrick   // Return the body computation.
910*09467b48Spatrick   return BodyVal;
911*09467b48Spatrick }
912*09467b48Spatrick 
913*09467b48Spatrick Function *PrototypeAST::Codegen() {
914*09467b48Spatrick   // Make the function type:  double(double,double) etc.
915*09467b48Spatrick   std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext));
916*09467b48Spatrick   FunctionType *FT =
917*09467b48Spatrick       FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
918*09467b48Spatrick 
919*09467b48Spatrick   Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
920*09467b48Spatrick   // If F conflicted, there was already something named 'Name'.  If it has a
921*09467b48Spatrick   // body, don't allow redefinition or reextern.
922*09467b48Spatrick   if (F->getName() != Name) {
923*09467b48Spatrick     // Delete the one we just made and get the existing one.
924*09467b48Spatrick     F->eraseFromParent();
925*09467b48Spatrick     F = TheModule->getFunction(Name);
926*09467b48Spatrick     // If F already has a body, reject this.
927*09467b48Spatrick     if (!F->empty()) {
928*09467b48Spatrick       ErrorF("redefinition of function");
929*09467b48Spatrick       return 0;
930*09467b48Spatrick     }
931*09467b48Spatrick     // If F took a different number of args, reject.
932*09467b48Spatrick     if (F->arg_size() != Args.size()) {
933*09467b48Spatrick       ErrorF("redefinition of function with different # args");
934*09467b48Spatrick       return 0;
935*09467b48Spatrick     }
936*09467b48Spatrick   }
937*09467b48Spatrick 
938*09467b48Spatrick   // Set names for all arguments.
939*09467b48Spatrick   unsigned Idx = 0;
940*09467b48Spatrick   for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
941*09467b48Spatrick        ++AI, ++Idx)
942*09467b48Spatrick     AI->setName(Args[Idx]);
943*09467b48Spatrick 
944*09467b48Spatrick   return F;
945*09467b48Spatrick }
946*09467b48Spatrick 
947*09467b48Spatrick /// CreateArgumentAllocas - Create an alloca for each argument and register the
948*09467b48Spatrick /// argument in the symbol table so that references to it will succeed.
949*09467b48Spatrick void PrototypeAST::CreateArgumentAllocas(Function *F) {
950*09467b48Spatrick   Function::arg_iterator AI = F->arg_begin();
951*09467b48Spatrick   for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
952*09467b48Spatrick     // Create an alloca for this variable.
953*09467b48Spatrick     AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
954*09467b48Spatrick 
955*09467b48Spatrick     // Store the initial value into the alloca.
956*09467b48Spatrick     Builder.CreateStore(AI, Alloca);
957*09467b48Spatrick 
958*09467b48Spatrick     // Add arguments to variable symbol table.
959*09467b48Spatrick     NamedValues[Args[Idx]] = Alloca;
960*09467b48Spatrick   }
961*09467b48Spatrick }
962*09467b48Spatrick 
963*09467b48Spatrick Function *FunctionAST::Codegen() {
964*09467b48Spatrick   NamedValues.clear();
965*09467b48Spatrick 
966*09467b48Spatrick   Function *TheFunction = Proto->Codegen();
967*09467b48Spatrick   if (TheFunction == 0)
968*09467b48Spatrick     return 0;
969*09467b48Spatrick 
970*09467b48Spatrick   // If this is an operator, install it.
971*09467b48Spatrick   if (Proto->isBinaryOp())
972*09467b48Spatrick     BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
973*09467b48Spatrick 
974*09467b48Spatrick   // Create a new basic block to start insertion into.
975*09467b48Spatrick   BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
976*09467b48Spatrick   Builder.SetInsertPoint(BB);
977*09467b48Spatrick 
978*09467b48Spatrick   // Add all arguments to the symbol table and create their allocas.
979*09467b48Spatrick   Proto->CreateArgumentAllocas(TheFunction);
980*09467b48Spatrick 
981*09467b48Spatrick   if (Value *RetVal = Body->Codegen()) {
982*09467b48Spatrick     // Finish off the function.
983*09467b48Spatrick     Builder.CreateRet(RetVal);
984*09467b48Spatrick 
985*09467b48Spatrick     // Validate the generated code, checking for consistency.
986*09467b48Spatrick     verifyFunction(*TheFunction);
987*09467b48Spatrick 
988*09467b48Spatrick     // Optimize the function.
989*09467b48Spatrick     TheFPM->run(*TheFunction);
990*09467b48Spatrick 
991*09467b48Spatrick     return TheFunction;
992*09467b48Spatrick   }
993*09467b48Spatrick 
994*09467b48Spatrick   // Error reading body, remove function.
995*09467b48Spatrick   TheFunction->eraseFromParent();
996*09467b48Spatrick 
997*09467b48Spatrick   if (Proto->isBinaryOp())
998*09467b48Spatrick     BinopPrecedence.erase(Proto->getOperatorName());
999*09467b48Spatrick   return 0;
1000*09467b48Spatrick }
1001*09467b48Spatrick 
1002*09467b48Spatrick //===----------------------------------------------------------------------===//
1003*09467b48Spatrick // Top-Level parsing and JIT Driver
1004*09467b48Spatrick //===----------------------------------------------------------------------===//
1005*09467b48Spatrick 
1006*09467b48Spatrick static ExecutionEngine *TheExecutionEngine;
1007*09467b48Spatrick 
1008*09467b48Spatrick static void HandleDefinition() {
1009*09467b48Spatrick   if (FunctionAST *F = ParseDefinition()) {
1010*09467b48Spatrick     if (Function *LF = F->Codegen()) {
1011*09467b48Spatrick #ifndef MINIMAL_STDERR_OUTPUT
1012*09467b48Spatrick       fprintf(stderr, "Read function definition:");
1013*09467b48Spatrick       LF->print(errs());
1014*09467b48Spatrick       fprintf(stderr, "\n");
1015*09467b48Spatrick #endif
1016*09467b48Spatrick     }
1017*09467b48Spatrick   } else {
1018*09467b48Spatrick     // Skip token for error recovery.
1019*09467b48Spatrick     getNextToken();
1020*09467b48Spatrick   }
1021*09467b48Spatrick }
1022*09467b48Spatrick 
1023*09467b48Spatrick static void HandleExtern() {
1024*09467b48Spatrick   if (PrototypeAST *P = ParseExtern()) {
1025*09467b48Spatrick     if (Function *F = P->Codegen()) {
1026*09467b48Spatrick #ifndef MINIMAL_STDERR_OUTPUT
1027*09467b48Spatrick       fprintf(stderr, "Read extern: ");
1028*09467b48Spatrick       F->print(errs());
1029*09467b48Spatrick       fprintf(stderr, "\n");
1030*09467b48Spatrick #endif
1031*09467b48Spatrick     }
1032*09467b48Spatrick   } else {
1033*09467b48Spatrick     // Skip token for error recovery.
1034*09467b48Spatrick     getNextToken();
1035*09467b48Spatrick   }
1036*09467b48Spatrick }
1037*09467b48Spatrick 
1038*09467b48Spatrick static void HandleTopLevelExpression() {
1039*09467b48Spatrick   // Evaluate a top-level expression into an anonymous function.
1040*09467b48Spatrick   if (FunctionAST *F = ParseTopLevelExpr()) {
1041*09467b48Spatrick     if (Function *LF = F->Codegen()) {
1042*09467b48Spatrick       // JIT the function, returning a function pointer.
1043*09467b48Spatrick       void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
1044*09467b48Spatrick       // Cast it to the right type (takes no arguments, returns a double) so we
1045*09467b48Spatrick       // can call it as a native function.
1046*09467b48Spatrick       double (*FP)() = (double (*)())(intptr_t)FPtr;
1047*09467b48Spatrick #ifdef MINIMAL_STDERR_OUTPUT
1048*09467b48Spatrick       FP();
1049*09467b48Spatrick #else
1050*09467b48Spatrick       fprintf(stderr, "Evaluated to %f\n", FP());
1051*09467b48Spatrick #endif
1052*09467b48Spatrick     }
1053*09467b48Spatrick   } else {
1054*09467b48Spatrick     // Skip token for error recovery.
1055*09467b48Spatrick     getNextToken();
1056*09467b48Spatrick   }
1057*09467b48Spatrick }
1058*09467b48Spatrick 
1059*09467b48Spatrick /// top ::= definition | external | expression | ';'
1060*09467b48Spatrick static void MainLoop() {
1061*09467b48Spatrick   while (1) {
1062*09467b48Spatrick #ifndef MINIMAL_STDERR_OUTPUT
1063*09467b48Spatrick     fprintf(stderr, "ready> ");
1064*09467b48Spatrick #endif
1065*09467b48Spatrick     switch (CurTok) {
1066*09467b48Spatrick     case tok_eof:    return;
1067*09467b48Spatrick     case ';':        getNextToken(); break;  // ignore top-level semicolons.
1068*09467b48Spatrick     case tok_def:    HandleDefinition(); break;
1069*09467b48Spatrick     case tok_extern: HandleExtern(); break;
1070*09467b48Spatrick     default:         HandleTopLevelExpression(); break;
1071*09467b48Spatrick     }
1072*09467b48Spatrick   }
1073*09467b48Spatrick }
1074*09467b48Spatrick 
1075*09467b48Spatrick //===----------------------------------------------------------------------===//
1076*09467b48Spatrick // "Library" functions that can be "extern'd" from user code.
1077*09467b48Spatrick //===----------------------------------------------------------------------===//
1078*09467b48Spatrick 
1079*09467b48Spatrick /// putchard - putchar that takes a double and returns 0.
1080*09467b48Spatrick extern "C"
1081*09467b48Spatrick double putchard(double X) {
1082*09467b48Spatrick   putchar((char)X);
1083*09467b48Spatrick   return 0;
1084*09467b48Spatrick }
1085*09467b48Spatrick 
1086*09467b48Spatrick /// printd - printf that takes a double prints it as "%f\n", returning 0.
1087*09467b48Spatrick extern "C"
1088*09467b48Spatrick double printd(double X) {
1089*09467b48Spatrick   printf("%f", X);
1090*09467b48Spatrick   return 0;
1091*09467b48Spatrick }
1092*09467b48Spatrick 
1093*09467b48Spatrick extern "C"
1094*09467b48Spatrick double printlf() {
1095*09467b48Spatrick   printf("\n");
1096*09467b48Spatrick   return 0;
1097*09467b48Spatrick }
1098*09467b48Spatrick 
1099*09467b48Spatrick //===----------------------------------------------------------------------===//
1100*09467b48Spatrick // Main driver code.
1101*09467b48Spatrick //===----------------------------------------------------------------------===//
1102*09467b48Spatrick 
1103*09467b48Spatrick int main(int argc, char **argv) {
1104*09467b48Spatrick   InitializeNativeTarget();
1105*09467b48Spatrick   LLVMContext &Context = TheContext;
1106*09467b48Spatrick 
1107*09467b48Spatrick   // Install standard binary operators.
1108*09467b48Spatrick   // 1 is lowest precedence.
1109*09467b48Spatrick   BinopPrecedence['='] = 2;
1110*09467b48Spatrick   BinopPrecedence['<'] = 10;
1111*09467b48Spatrick   BinopPrecedence['+'] = 20;
1112*09467b48Spatrick   BinopPrecedence['-'] = 20;
1113*09467b48Spatrick   BinopPrecedence['/'] = 40;
1114*09467b48Spatrick   BinopPrecedence['*'] = 40;  // highest.
1115*09467b48Spatrick 
1116*09467b48Spatrick   // Make the module, which holds all the code.
1117*09467b48Spatrick   TheModule = new Module("my cool jit", Context);
1118*09467b48Spatrick 
1119*09467b48Spatrick   // Create the JIT.  This takes ownership of the module.
1120*09467b48Spatrick   std::string ErrStr;
1121*09467b48Spatrick   TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
1122*09467b48Spatrick   if (!TheExecutionEngine) {
1123*09467b48Spatrick     fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
1124*09467b48Spatrick     exit(1);
1125*09467b48Spatrick   }
1126*09467b48Spatrick 
1127*09467b48Spatrick   FunctionPassManager OurFPM(TheModule);
1128*09467b48Spatrick 
1129*09467b48Spatrick   // Set up the optimizer pipeline.  Start with registering info about how the
1130*09467b48Spatrick   // target lays out data structures.
1131*09467b48Spatrick   OurFPM.add(new DataLayout(*TheExecutionEngine->getDataLayout()));
1132*09467b48Spatrick   // Provide basic AliasAnalysis support for GVN.
1133*09467b48Spatrick   OurFPM.add(createBasicAliasAnalysisPass());
1134*09467b48Spatrick   // Promote allocas to registers.
1135*09467b48Spatrick   OurFPM.add(createPromoteMemoryToRegisterPass());
1136*09467b48Spatrick   // Do simple "peephole" optimizations and bit-twiddling optzns.
1137*09467b48Spatrick   OurFPM.add(createInstructionCombiningPass());
1138*09467b48Spatrick   // Reassociate expressions.
1139*09467b48Spatrick   OurFPM.add(createReassociatePass());
1140*09467b48Spatrick   // Eliminate Common SubExpressions.
1141*09467b48Spatrick   OurFPM.add(createGVNPass());
1142*09467b48Spatrick   // Simplify the control flow graph (deleting unreachable blocks, etc).
1143*09467b48Spatrick   OurFPM.add(createCFGSimplificationPass());
1144*09467b48Spatrick 
1145*09467b48Spatrick   OurFPM.doInitialization();
1146*09467b48Spatrick 
1147*09467b48Spatrick   // Set the global so the code gen can use this.
1148*09467b48Spatrick   TheFPM = &OurFPM;
1149*09467b48Spatrick 
1150*09467b48Spatrick   // Prime the first token.
1151*09467b48Spatrick #ifndef MINIMAL_STDERR_OUTPUT
1152*09467b48Spatrick   fprintf(stderr, "ready> ");
1153*09467b48Spatrick #endif
1154*09467b48Spatrick   getNextToken();
1155*09467b48Spatrick 
1156*09467b48Spatrick   // Run the main "interpreter loop" now.
1157*09467b48Spatrick   MainLoop();
1158*09467b48Spatrick 
1159*09467b48Spatrick   // Print out all of the generated code.
1160*09467b48Spatrick   TheFPM = 0;
1161*09467b48Spatrick #ifndef MINIMAL_STDERR_OUTPUT
1162*09467b48Spatrick   TheModule->print(errs(), nullptr);
1163*09467b48Spatrick #endif
1164*09467b48Spatrick   return 0;
1165*09467b48Spatrick }
1166