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