blob: 289209b3df49e46f7eea887b5093b9262853e54b [file] [log] [blame]
NAKAMURA Takumi85c9bac2015-03-02 01:04:34 +00001#include "llvm/ADT/STLExtras.h"
Lang Hames2d789c32015-08-26 03:07:41 +00002#include "llvm/Analysis/BasicAliasAnalysis.h"
Eric Christopher05917fa2014-12-08 18:00:47 +00003#include "llvm/Analysis/Passes.h"
Chandler Carruth1f832f72015-02-13 09:14:30 +00004#include "llvm/IR/DIBuilder.h"
Eric Christopher05917fa2014-12-08 18:00:47 +00005#include "llvm/IR/IRBuilder.h"
6#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +00007#include "llvm/IR/LegacyPassManager.h"
Eric Christopher05917fa2014-12-08 18:00:47 +00008#include "llvm/IR/Module.h"
9#include "llvm/IR/Verifier.h"
Eric Christopher05917fa2014-12-08 18:00:47 +000010#include "llvm/Support/TargetSelect.h"
11#include "llvm/Transforms/Scalar.h"
12#include <cctype>
13#include <cstdio>
14#include <map>
15#include <string>
16#include <vector>
Lang Hames2d789c32015-08-26 03:07:41 +000017#include "../include/KaleidoscopeJIT.h"
18
Eric Christopher05917fa2014-12-08 18:00:47 +000019using namespace llvm;
Lang Hames2d789c32015-08-26 03:07:41 +000020using namespace llvm::orc;
Eric Christopher05917fa2014-12-08 18:00:47 +000021
22//===----------------------------------------------------------------------===//
23// Lexer
24//===----------------------------------------------------------------------===//
25
26// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
27// of these for known things.
28enum Token {
29 tok_eof = -1,
30
31 // commands
32 tok_def = -2,
33 tok_extern = -3,
34
35 // primary
36 tok_identifier = -4,
37 tok_number = -5,
38
39 // control
40 tok_if = -6,
41 tok_then = -7,
42 tok_else = -8,
43 tok_for = -9,
44 tok_in = -10,
45
46 // operators
47 tok_binary = -11,
48 tok_unary = -12,
49
50 // var definition
51 tok_var = -13
52};
53
54std::string getTokName(int Tok) {
55 switch (Tok) {
56 case tok_eof:
57 return "eof";
58 case tok_def:
59 return "def";
60 case tok_extern:
61 return "extern";
62 case tok_identifier:
63 return "identifier";
64 case tok_number:
65 return "number";
66 case tok_if:
67 return "if";
68 case tok_then:
69 return "then";
70 case tok_else:
71 return "else";
72 case tok_for:
73 return "for";
74 case tok_in:
75 return "in";
76 case tok_binary:
77 return "binary";
78 case tok_unary:
79 return "unary";
80 case tok_var:
81 return "var";
82 }
83 return std::string(1, (char)Tok);
84}
85
86namespace {
87class PrototypeAST;
88class ExprAST;
89}
90static IRBuilder<> Builder(getGlobalContext());
91struct DebugInfo {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000092 DICompileUnit *TheCU;
93 DIType *DblTy;
94 std::vector<DIScope *> LexicalBlocks;
Eric Christopher05917fa2014-12-08 18:00:47 +000095
96 void emitLocation(ExprAST *AST);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000097 DIType *getDoubleTy();
Eric Christopher05917fa2014-12-08 18:00:47 +000098} KSDbgInfo;
99
Eric Christopher05917fa2014-12-08 18:00:47 +0000100struct SourceLocation {
101 int Line;
102 int Col;
103};
104static SourceLocation CurLoc;
Lang Hames59b0da82015-08-19 18:15:58 +0000105static SourceLocation LexLoc = {1, 0};
Eric Christopher05917fa2014-12-08 18:00:47 +0000106
107static int advance() {
108 int LastChar = getchar();
109
110 if (LastChar == '\n' || LastChar == '\r') {
111 LexLoc.Line++;
112 LexLoc.Col = 0;
113 } else
114 LexLoc.Col++;
115 return LastChar;
116}
117
Lang Hames2d789c32015-08-26 03:07:41 +0000118static std::string IdentifierStr; // Filled in if tok_identifier
119static double NumVal; // Filled in if tok_number
120
Eric Christopher05917fa2014-12-08 18:00:47 +0000121/// gettok - Return the next token from standard input.
122static int gettok() {
123 static int LastChar = ' ';
124
125 // Skip any whitespace.
126 while (isspace(LastChar))
127 LastChar = advance();
128
129 CurLoc = LexLoc;
130
131 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
132 IdentifierStr = LastChar;
133 while (isalnum((LastChar = advance())))
134 IdentifierStr += LastChar;
135
136 if (IdentifierStr == "def")
137 return tok_def;
138 if (IdentifierStr == "extern")
139 return tok_extern;
140 if (IdentifierStr == "if")
141 return tok_if;
142 if (IdentifierStr == "then")
143 return tok_then;
144 if (IdentifierStr == "else")
145 return tok_else;
146 if (IdentifierStr == "for")
147 return tok_for;
148 if (IdentifierStr == "in")
149 return tok_in;
150 if (IdentifierStr == "binary")
151 return tok_binary;
152 if (IdentifierStr == "unary")
153 return tok_unary;
154 if (IdentifierStr == "var")
155 return tok_var;
156 return tok_identifier;
157 }
158
159 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
160 std::string NumStr;
161 do {
162 NumStr += LastChar;
163 LastChar = advance();
164 } while (isdigit(LastChar) || LastChar == '.');
165
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000166 NumVal = strtod(NumStr.c_str(), nullptr);
Eric Christopher05917fa2014-12-08 18:00:47 +0000167 return tok_number;
168 }
169
170 if (LastChar == '#') {
171 // Comment until end of line.
172 do
173 LastChar = advance();
174 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
175
176 if (LastChar != EOF)
177 return gettok();
178 }
179
180 // Check for end of file. Don't eat the EOF.
181 if (LastChar == EOF)
182 return tok_eof;
183
184 // Otherwise, just return the character as its ascii value.
185 int ThisChar = LastChar;
186 LastChar = advance();
187 return ThisChar;
188}
189
190//===----------------------------------------------------------------------===//
191// Abstract Syntax Tree (aka Parse Tree)
192//===----------------------------------------------------------------------===//
193namespace {
194
Lang Hames59b0da82015-08-19 18:15:58 +0000195raw_ostream &indent(raw_ostream &O, int size) {
Eric Christopher05917fa2014-12-08 18:00:47 +0000196 return O << std::string(size, ' ');
197}
198
199/// ExprAST - Base class for all expression nodes.
200class ExprAST {
201 SourceLocation Loc;
Lang Hames59b0da82015-08-19 18:15:58 +0000202
Eric Christopher05917fa2014-12-08 18:00:47 +0000203public:
Eric Christopher05917fa2014-12-08 18:00:47 +0000204 ExprAST(SourceLocation Loc = CurLoc) : Loc(Loc) {}
Eric Christopher05917fa2014-12-08 18:00:47 +0000205 virtual ~ExprAST() {}
Lang Hames2d789c32015-08-26 03:07:41 +0000206 virtual Value *codegen() = 0;
Lang Hames59b0da82015-08-19 18:15:58 +0000207 int getLine() const { return Loc.Line; }
208 int getCol() const { return Loc.Col; }
209 virtual raw_ostream &dump(raw_ostream &out, int ind) {
210 return out << ':' << getLine() << ':' << getCol() << '\n';
211 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000212};
213
214/// NumberExprAST - Expression class for numeric literals like "1.0".
215class NumberExprAST : public ExprAST {
216 double Val;
Lang Hames59b0da82015-08-19 18:15:58 +0000217
Eric Christopher05917fa2014-12-08 18:00:47 +0000218public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000219 NumberExprAST(double Val) : Val(Val) {}
Lang Hames59b0da82015-08-19 18:15:58 +0000220 raw_ostream &dump(raw_ostream &out, int ind) override {
Eric Christopher05917fa2014-12-08 18:00:47 +0000221 return ExprAST::dump(out << Val, ind);
222 }
Lang Hames2d789c32015-08-26 03:07:41 +0000223 Value *codegen() override;
Eric Christopher05917fa2014-12-08 18:00:47 +0000224};
225
226/// VariableExprAST - Expression class for referencing a variable, like "a".
227class VariableExprAST : public ExprAST {
228 std::string Name;
Lang Hames59b0da82015-08-19 18:15:58 +0000229
Eric Christopher05917fa2014-12-08 18:00:47 +0000230public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000231 VariableExprAST(SourceLocation Loc, const std::string &Name)
232 : ExprAST(Loc), Name(Name) {}
Eric Christopher05917fa2014-12-08 18:00:47 +0000233 const std::string &getName() const { return Name; }
Lang Hames2d789c32015-08-26 03:07:41 +0000234 Value *codegen() override;
Lang Hames59b0da82015-08-19 18:15:58 +0000235 raw_ostream &dump(raw_ostream &out, int ind) override {
Eric Christopher05917fa2014-12-08 18:00:47 +0000236 return ExprAST::dump(out << Name, ind);
237 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000238};
239
240/// UnaryExprAST - Expression class for a unary operator.
241class UnaryExprAST : public ExprAST {
242 char Opcode;
Lang Hames09bf4c12015-08-18 18:11:06 +0000243 std::unique_ptr<ExprAST> Operand;
Lang Hames59b0da82015-08-19 18:15:58 +0000244
Eric Christopher05917fa2014-12-08 18:00:47 +0000245public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000246 UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)
247 : Opcode(Opcode), Operand(std::move(Operand)) {}
Lang Hames2d789c32015-08-26 03:07:41 +0000248 Value *codegen() override;
Lang Hames59b0da82015-08-19 18:15:58 +0000249 raw_ostream &dump(raw_ostream &out, int ind) override {
Eric Christopher05917fa2014-12-08 18:00:47 +0000250 ExprAST::dump(out << "unary" << Opcode, ind);
251 Operand->dump(out, ind + 1);
252 return out;
253 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000254};
255
256/// BinaryExprAST - Expression class for a binary operator.
257class BinaryExprAST : public ExprAST {
258 char Op;
Lang Hames09bf4c12015-08-18 18:11:06 +0000259 std::unique_ptr<ExprAST> LHS, RHS;
Lang Hames59b0da82015-08-19 18:15:58 +0000260
Eric Christopher05917fa2014-12-08 18:00:47 +0000261public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000262 BinaryExprAST(SourceLocation Loc, char Op, std::unique_ptr<ExprAST> LHS,
263 std::unique_ptr<ExprAST> RHS)
264 : ExprAST(Loc), Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
Lang Hames2d789c32015-08-26 03:07:41 +0000265 Value *codegen() override;
Lang Hames59b0da82015-08-19 18:15:58 +0000266 raw_ostream &dump(raw_ostream &out, int ind) override {
Eric Christopher05917fa2014-12-08 18:00:47 +0000267 ExprAST::dump(out << "binary" << Op, ind);
268 LHS->dump(indent(out, ind) << "LHS:", ind + 1);
269 RHS->dump(indent(out, ind) << "RHS:", ind + 1);
270 return out;
271 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000272};
273
274/// CallExprAST - Expression class for function calls.
275class CallExprAST : public ExprAST {
276 std::string Callee;
Lang Hames09bf4c12015-08-18 18:11:06 +0000277 std::vector<std::unique_ptr<ExprAST>> Args;
Lang Hames59b0da82015-08-19 18:15:58 +0000278
Eric Christopher05917fa2014-12-08 18:00:47 +0000279public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000280 CallExprAST(SourceLocation Loc, const std::string &Callee,
281 std::vector<std::unique_ptr<ExprAST>> Args)
282 : ExprAST(Loc), Callee(Callee), Args(std::move(Args)) {}
Lang Hames2d789c32015-08-26 03:07:41 +0000283 Value *codegen() override;
Lang Hames59b0da82015-08-19 18:15:58 +0000284 raw_ostream &dump(raw_ostream &out, int ind) override {
Eric Christopher05917fa2014-12-08 18:00:47 +0000285 ExprAST::dump(out << "call " << Callee, ind);
Lang Hames09bf4c12015-08-18 18:11:06 +0000286 for (const auto &Arg : Args)
Eric Christopher05917fa2014-12-08 18:00:47 +0000287 Arg->dump(indent(out, ind + 1), ind + 1);
288 return out;
289 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000290};
291
292/// IfExprAST - Expression class for if/then/else.
293class IfExprAST : public ExprAST {
Lang Hames09bf4c12015-08-18 18:11:06 +0000294 std::unique_ptr<ExprAST> Cond, Then, Else;
Lang Hames59b0da82015-08-19 18:15:58 +0000295
Eric Christopher05917fa2014-12-08 18:00:47 +0000296public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000297 IfExprAST(SourceLocation Loc, std::unique_ptr<ExprAST> Cond,
298 std::unique_ptr<ExprAST> Then, std::unique_ptr<ExprAST> Else)
Lang Hames59b0da82015-08-19 18:15:58 +0000299 : ExprAST(Loc), Cond(std::move(Cond)), Then(std::move(Then)),
300 Else(std::move(Else)) {}
Lang Hames2d789c32015-08-26 03:07:41 +0000301 Value *codegen() override;
Lang Hames59b0da82015-08-19 18:15:58 +0000302 raw_ostream &dump(raw_ostream &out, int ind) override {
Eric Christopher05917fa2014-12-08 18:00:47 +0000303 ExprAST::dump(out << "if", ind);
304 Cond->dump(indent(out, ind) << "Cond:", ind + 1);
305 Then->dump(indent(out, ind) << "Then:", ind + 1);
306 Else->dump(indent(out, ind) << "Else:", ind + 1);
307 return out;
308 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000309};
310
311/// ForExprAST - Expression class for for/in.
312class ForExprAST : public ExprAST {
313 std::string VarName;
Lang Hames09bf4c12015-08-18 18:11:06 +0000314 std::unique_ptr<ExprAST> Start, End, Step, Body;
Lang Hames59b0da82015-08-19 18:15:58 +0000315
Eric Christopher05917fa2014-12-08 18:00:47 +0000316public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000317 ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
318 std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
319 std::unique_ptr<ExprAST> Body)
320 : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
321 Step(std::move(Step)), Body(std::move(Body)) {}
Lang Hames2d789c32015-08-26 03:07:41 +0000322 Value *codegen() override;
Lang Hames59b0da82015-08-19 18:15:58 +0000323 raw_ostream &dump(raw_ostream &out, int ind) override {
Eric Christopher05917fa2014-12-08 18:00:47 +0000324 ExprAST::dump(out << "for", ind);
325 Start->dump(indent(out, ind) << "Cond:", ind + 1);
326 End->dump(indent(out, ind) << "End:", ind + 1);
327 Step->dump(indent(out, ind) << "Step:", ind + 1);
328 Body->dump(indent(out, ind) << "Body:", ind + 1);
329 return out;
330 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000331};
332
333/// VarExprAST - Expression class for var/in
334class VarExprAST : public ExprAST {
Lang Hames09bf4c12015-08-18 18:11:06 +0000335 std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;
336 std::unique_ptr<ExprAST> Body;
Lang Hames59b0da82015-08-19 18:15:58 +0000337
Eric Christopher05917fa2014-12-08 18:00:47 +0000338public:
Lang Hames59b0da82015-08-19 18:15:58 +0000339 VarExprAST(
340 std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames,
341 std::unique_ptr<ExprAST> Body)
Lang Hames09bf4c12015-08-18 18:11:06 +0000342 : VarNames(std::move(VarNames)), Body(std::move(Body)) {}
Lang Hames2d789c32015-08-26 03:07:41 +0000343 Value *codegen() override;
Lang Hames59b0da82015-08-19 18:15:58 +0000344 raw_ostream &dump(raw_ostream &out, int ind) override {
Eric Christopher05917fa2014-12-08 18:00:47 +0000345 ExprAST::dump(out << "var", ind);
346 for (const auto &NamedVar : VarNames)
347 NamedVar.second->dump(indent(out, ind) << NamedVar.first << ':', ind + 1);
348 Body->dump(indent(out, ind) << "Body:", ind + 1);
349 return out;
350 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000351};
352
353/// PrototypeAST - This class represents the "prototype" for a function,
Lang Hames59b0da82015-08-19 18:15:58 +0000354/// which captures its name, and its argument names (thus implicitly the number
355/// of arguments the function takes), as well as if it is an operator.
Eric Christopher05917fa2014-12-08 18:00:47 +0000356class PrototypeAST {
357 std::string Name;
358 std::vector<std::string> Args;
Lang Hames09bf4c12015-08-18 18:11:06 +0000359 bool IsOperator;
Eric Christopher05917fa2014-12-08 18:00:47 +0000360 unsigned Precedence; // Precedence if a binary op.
361 int Line;
Lang Hames59b0da82015-08-19 18:15:58 +0000362
Eric Christopher05917fa2014-12-08 18:00:47 +0000363public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000364 PrototypeAST(SourceLocation Loc, const std::string &Name,
365 std::vector<std::string> Args, bool IsOperator = false,
366 unsigned Prec = 0)
Lang Hames59b0da82015-08-19 18:15:58 +0000367 : Name(Name), Args(std::move(Args)), IsOperator(IsOperator),
368 Precedence(Prec), Line(Loc.Line) {}
Lang Hames2d789c32015-08-26 03:07:41 +0000369 Function *codegen();
370 const std::string &getName() const { return Name; }
Eric Christopher05917fa2014-12-08 18:00:47 +0000371
Lang Hames09bf4c12015-08-18 18:11:06 +0000372 bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
373 bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
Eric Christopher05917fa2014-12-08 18:00:47 +0000374
375 char getOperatorName() const {
376 assert(isUnaryOp() || isBinaryOp());
377 return Name[Name.size() - 1];
378 }
379
380 unsigned getBinaryPrecedence() const { return Precedence; }
Lang Hames2d789c32015-08-26 03:07:41 +0000381 int getLine() const { return Line; }
Eric Christopher05917fa2014-12-08 18:00:47 +0000382};
383
384/// FunctionAST - This class represents a function definition itself.
385class FunctionAST {
Lang Hames09bf4c12015-08-18 18:11:06 +0000386 std::unique_ptr<PrototypeAST> Proto;
387 std::unique_ptr<ExprAST> Body;
Lang Hames59b0da82015-08-19 18:15:58 +0000388
Eric Christopher05917fa2014-12-08 18:00:47 +0000389public:
Lang Hames09bf4c12015-08-18 18:11:06 +0000390 FunctionAST(std::unique_ptr<PrototypeAST> Proto,
391 std::unique_ptr<ExprAST> Body)
392 : Proto(std::move(Proto)), Body(std::move(Body)) {}
Lang Hames2d789c32015-08-26 03:07:41 +0000393 Function *codegen();
Lang Hames59b0da82015-08-19 18:15:58 +0000394 raw_ostream &dump(raw_ostream &out, int ind) {
Eric Christopher05917fa2014-12-08 18:00:47 +0000395 indent(out, ind) << "FunctionAST\n";
396 ++ind;
397 indent(out, ind) << "Body:";
398 return Body ? Body->dump(out, ind) : out << "null\n";
399 }
Eric Christopher05917fa2014-12-08 18:00:47 +0000400};
401} // end anonymous namespace
402
403//===----------------------------------------------------------------------===//
404// Parser
405//===----------------------------------------------------------------------===//
406
407/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
408/// token the parser is looking at. getNextToken reads another token from the
409/// lexer and updates CurTok with its results.
410static int CurTok;
411static int getNextToken() { return CurTok = gettok(); }
412
413/// BinopPrecedence - This holds the precedence for each binary operator that is
414/// defined.
415static std::map<char, int> BinopPrecedence;
416
417/// GetTokPrecedence - Get the precedence of the pending binary operator token.
418static int GetTokPrecedence() {
419 if (!isascii(CurTok))
420 return -1;
421
422 // Make sure it's a declared binop.
423 int TokPrec = BinopPrecedence[CurTok];
424 if (TokPrec <= 0)
425 return -1;
426 return TokPrec;
427}
428
429/// Error* - These are little helper functions for error handling.
Lang Hames09bf4c12015-08-18 18:11:06 +0000430std::unique_ptr<ExprAST> Error(const char *Str) {
Eric Christopher05917fa2014-12-08 18:00:47 +0000431 fprintf(stderr, "Error: %s\n", Str);
Lang Hames09bf4c12015-08-18 18:11:06 +0000432 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000433}
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000434
Lang Hames09bf4c12015-08-18 18:11:06 +0000435std::unique_ptr<PrototypeAST> ErrorP(const char *Str) {
Eric Christopher05917fa2014-12-08 18:00:47 +0000436 Error(Str);
Lang Hames09bf4c12015-08-18 18:11:06 +0000437 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000438}
Eric Christopher05917fa2014-12-08 18:00:47 +0000439
Lang Hames09bf4c12015-08-18 18:11:06 +0000440static std::unique_ptr<ExprAST> ParseExpression();
Eric Christopher05917fa2014-12-08 18:00:47 +0000441
Eric Christopher05917fa2014-12-08 18:00:47 +0000442/// numberexpr ::= number
Lang Hames09bf4c12015-08-18 18:11:06 +0000443static std::unique_ptr<ExprAST> ParseNumberExpr() {
444 auto Result = llvm::make_unique<NumberExprAST>(NumVal);
Eric Christopher05917fa2014-12-08 18:00:47 +0000445 getNextToken(); // consume the number
Lang Hames09bf4c12015-08-18 18:11:06 +0000446 return std::move(Result);
Eric Christopher05917fa2014-12-08 18:00:47 +0000447}
448
449/// parenexpr ::= '(' expression ')'
Lang Hames09bf4c12015-08-18 18:11:06 +0000450static std::unique_ptr<ExprAST> ParseParenExpr() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000451 getNextToken(); // eat (.
Lang Hames09bf4c12015-08-18 18:11:06 +0000452 auto V = ParseExpression();
Eric Christopher05917fa2014-12-08 18:00:47 +0000453 if (!V)
Lang Hames09bf4c12015-08-18 18:11:06 +0000454 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000455
456 if (CurTok != ')')
457 return Error("expected ')'");
458 getNextToken(); // eat ).
459 return V;
460}
461
Lang Hames59b0da82015-08-19 18:15:58 +0000462/// identifierexpr
463/// ::= identifier
464/// ::= identifier '(' expression* ')'
465static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
466 std::string IdName = IdentifierStr;
467
468 SourceLocation LitLoc = CurLoc;
469
470 getNextToken(); // eat identifier.
471
472 if (CurTok != '(') // Simple variable ref.
473 return llvm::make_unique<VariableExprAST>(LitLoc, IdName);
474
475 // Call.
476 getNextToken(); // eat (
477 std::vector<std::unique_ptr<ExprAST>> Args;
478 if (CurTok != ')') {
479 while (1) {
480 if (auto Arg = ParseExpression())
481 Args.push_back(std::move(Arg));
482 else
483 return nullptr;
484
485 if (CurTok == ')')
486 break;
487
488 if (CurTok != ',')
489 return Error("Expected ')' or ',' in argument list");
490 getNextToken();
491 }
492 }
493
494 // Eat the ')'.
495 getNextToken();
496
497 return llvm::make_unique<CallExprAST>(LitLoc, IdName, std::move(Args));
498}
499
Eric Christopher05917fa2014-12-08 18:00:47 +0000500/// ifexpr ::= 'if' expression 'then' expression 'else' expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000501static std::unique_ptr<ExprAST> ParseIfExpr() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000502 SourceLocation IfLoc = CurLoc;
503
504 getNextToken(); // eat the if.
505
506 // condition.
Lang Hames09bf4c12015-08-18 18:11:06 +0000507 auto Cond = ParseExpression();
Eric Christopher05917fa2014-12-08 18:00:47 +0000508 if (!Cond)
Lang Hames09bf4c12015-08-18 18:11:06 +0000509 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000510
511 if (CurTok != tok_then)
512 return Error("expected then");
513 getNextToken(); // eat the then
514
Lang Hames09bf4c12015-08-18 18:11:06 +0000515 auto Then = ParseExpression();
516 if (!Then)
517 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000518
519 if (CurTok != tok_else)
520 return Error("expected else");
521
522 getNextToken();
523
Lang Hames09bf4c12015-08-18 18:11:06 +0000524 auto Else = ParseExpression();
Eric Christopher05917fa2014-12-08 18:00:47 +0000525 if (!Else)
Lang Hames09bf4c12015-08-18 18:11:06 +0000526 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000527
Lang Hames09bf4c12015-08-18 18:11:06 +0000528 return llvm::make_unique<IfExprAST>(IfLoc, std::move(Cond), std::move(Then),
529 std::move(Else));
Eric Christopher05917fa2014-12-08 18:00:47 +0000530}
531
532/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000533static std::unique_ptr<ExprAST> ParseForExpr() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000534 getNextToken(); // eat the for.
535
536 if (CurTok != tok_identifier)
537 return Error("expected identifier after for");
538
539 std::string IdName = IdentifierStr;
540 getNextToken(); // eat identifier.
541
542 if (CurTok != '=')
543 return Error("expected '=' after for");
544 getNextToken(); // eat '='.
545
Lang Hames09bf4c12015-08-18 18:11:06 +0000546 auto Start = ParseExpression();
547 if (!Start)
548 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000549 if (CurTok != ',')
550 return Error("expected ',' after for start value");
551 getNextToken();
552
Lang Hames09bf4c12015-08-18 18:11:06 +0000553 auto End = ParseExpression();
554 if (!End)
555 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000556
557 // The step value is optional.
Lang Hames09bf4c12015-08-18 18:11:06 +0000558 std::unique_ptr<ExprAST> Step;
Eric Christopher05917fa2014-12-08 18:00:47 +0000559 if (CurTok == ',') {
560 getNextToken();
561 Step = ParseExpression();
Lang Hames09bf4c12015-08-18 18:11:06 +0000562 if (!Step)
563 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000564 }
565
566 if (CurTok != tok_in)
567 return Error("expected 'in' after for");
568 getNextToken(); // eat 'in'.
569
Lang Hames09bf4c12015-08-18 18:11:06 +0000570 auto Body = ParseExpression();
571 if (!Body)
572 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000573
Lang Hames09bf4c12015-08-18 18:11:06 +0000574 return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
575 std::move(Step), std::move(Body));
Eric Christopher05917fa2014-12-08 18:00:47 +0000576}
577
578/// varexpr ::= 'var' identifier ('=' expression)?
579// (',' identifier ('=' expression)?)* 'in' expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000580static std::unique_ptr<ExprAST> ParseVarExpr() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000581 getNextToken(); // eat the var.
582
Lang Hames09bf4c12015-08-18 18:11:06 +0000583 std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;
Eric Christopher05917fa2014-12-08 18:00:47 +0000584
585 // At least one variable name is required.
586 if (CurTok != tok_identifier)
587 return Error("expected identifier after var");
588
589 while (1) {
590 std::string Name = IdentifierStr;
591 getNextToken(); // eat identifier.
592
593 // Read the optional initializer.
Lang Hames09bf4c12015-08-18 18:11:06 +0000594 std::unique_ptr<ExprAST> Init = nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000595 if (CurTok == '=') {
596 getNextToken(); // eat the '='.
597
598 Init = ParseExpression();
Lang Hames09bf4c12015-08-18 18:11:06 +0000599 if (!Init)
600 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000601 }
602
Lang Hames09bf4c12015-08-18 18:11:06 +0000603 VarNames.push_back(std::make_pair(Name, std::move(Init)));
Eric Christopher05917fa2014-12-08 18:00:47 +0000604
605 // End of var list, exit loop.
606 if (CurTok != ',')
607 break;
608 getNextToken(); // eat the ','.
609
610 if (CurTok != tok_identifier)
611 return Error("expected identifier list after var");
612 }
613
614 // At this point, we have to have 'in'.
615 if (CurTok != tok_in)
616 return Error("expected 'in' keyword after 'var'");
617 getNextToken(); // eat 'in'.
618
Lang Hames09bf4c12015-08-18 18:11:06 +0000619 auto Body = ParseExpression();
620 if (!Body)
621 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000622
Lang Hames09bf4c12015-08-18 18:11:06 +0000623 return llvm::make_unique<VarExprAST>(std::move(VarNames), std::move(Body));
Eric Christopher05917fa2014-12-08 18:00:47 +0000624}
625
626/// primary
627/// ::= identifierexpr
628/// ::= numberexpr
629/// ::= parenexpr
630/// ::= ifexpr
631/// ::= forexpr
632/// ::= varexpr
Lang Hames09bf4c12015-08-18 18:11:06 +0000633static std::unique_ptr<ExprAST> ParsePrimary() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000634 switch (CurTok) {
635 default:
636 return Error("unknown token when expecting an expression");
637 case tok_identifier:
638 return ParseIdentifierExpr();
639 case tok_number:
640 return ParseNumberExpr();
641 case '(':
642 return ParseParenExpr();
643 case tok_if:
644 return ParseIfExpr();
645 case tok_for:
646 return ParseForExpr();
647 case tok_var:
648 return ParseVarExpr();
649 }
650}
651
652/// unary
653/// ::= primary
654/// ::= '!' unary
Lang Hames09bf4c12015-08-18 18:11:06 +0000655static std::unique_ptr<ExprAST> ParseUnary() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000656 // If the current token is not an operator, it must be a primary expr.
657 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
658 return ParsePrimary();
659
660 // If this is a unary operator, read it.
661 int Opc = CurTok;
662 getNextToken();
Lang Hames09bf4c12015-08-18 18:11:06 +0000663 if (auto Operand = ParseUnary())
664 return llvm::make_unique<UnaryExprAST>(Opc, std::move(Operand));
665 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000666}
667
668/// binoprhs
669/// ::= ('+' unary)*
Lang Hames59b0da82015-08-19 18:15:58 +0000670static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
671 std::unique_ptr<ExprAST> LHS) {
Eric Christopher05917fa2014-12-08 18:00:47 +0000672 // If this is a binop, find its precedence.
673 while (1) {
674 int TokPrec = GetTokPrecedence();
675
676 // If this is a binop that binds at least as tightly as the current binop,
677 // consume it, otherwise we are done.
678 if (TokPrec < ExprPrec)
679 return LHS;
680
681 // Okay, we know this is a binop.
682 int BinOp = CurTok;
683 SourceLocation BinLoc = CurLoc;
684 getNextToken(); // eat binop
685
686 // Parse the unary expression after the binary operator.
Lang Hames09bf4c12015-08-18 18:11:06 +0000687 auto RHS = ParseUnary();
Eric Christopher05917fa2014-12-08 18:00:47 +0000688 if (!RHS)
Lang Hames09bf4c12015-08-18 18:11:06 +0000689 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000690
691 // If BinOp binds less tightly with RHS than the operator after RHS, let
692 // the pending operator take RHS as its LHS.
693 int NextPrec = GetTokPrecedence();
694 if (TokPrec < NextPrec) {
Lang Hames09bf4c12015-08-18 18:11:06 +0000695 RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
696 if (!RHS)
697 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000698 }
699
700 // Merge LHS/RHS.
Lang Hames09bf4c12015-08-18 18:11:06 +0000701 LHS = llvm::make_unique<BinaryExprAST>(BinLoc, BinOp, std::move(LHS),
702 std::move(RHS));
Eric Christopher05917fa2014-12-08 18:00:47 +0000703 }
704}
705
706/// expression
707/// ::= unary binoprhs
708///
Lang Hames09bf4c12015-08-18 18:11:06 +0000709static std::unique_ptr<ExprAST> ParseExpression() {
710 auto LHS = ParseUnary();
Eric Christopher05917fa2014-12-08 18:00:47 +0000711 if (!LHS)
Lang Hames09bf4c12015-08-18 18:11:06 +0000712 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000713
Lang Hames09bf4c12015-08-18 18:11:06 +0000714 return ParseBinOpRHS(0, std::move(LHS));
Eric Christopher05917fa2014-12-08 18:00:47 +0000715}
716
717/// prototype
718/// ::= id '(' id* ')'
719/// ::= binary LETTER number? (id, id)
720/// ::= unary LETTER (id)
Lang Hames09bf4c12015-08-18 18:11:06 +0000721static std::unique_ptr<PrototypeAST> ParsePrototype() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000722 std::string FnName;
723
724 SourceLocation FnLoc = CurLoc;
725
726 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
727 unsigned BinaryPrecedence = 30;
728
729 switch (CurTok) {
730 default:
731 return ErrorP("Expected function name in prototype");
732 case tok_identifier:
733 FnName = IdentifierStr;
734 Kind = 0;
735 getNextToken();
736 break;
737 case tok_unary:
738 getNextToken();
739 if (!isascii(CurTok))
740 return ErrorP("Expected unary operator");
741 FnName = "unary";
742 FnName += (char)CurTok;
743 Kind = 1;
744 getNextToken();
745 break;
746 case tok_binary:
747 getNextToken();
748 if (!isascii(CurTok))
749 return ErrorP("Expected binary operator");
750 FnName = "binary";
751 FnName += (char)CurTok;
752 Kind = 2;
753 getNextToken();
754
755 // Read the precedence if present.
756 if (CurTok == tok_number) {
757 if (NumVal < 1 || NumVal > 100)
758 return ErrorP("Invalid precedecnce: must be 1..100");
759 BinaryPrecedence = (unsigned)NumVal;
760 getNextToken();
761 }
762 break;
763 }
764
765 if (CurTok != '(')
766 return ErrorP("Expected '(' in prototype");
767
768 std::vector<std::string> ArgNames;
769 while (getNextToken() == tok_identifier)
770 ArgNames.push_back(IdentifierStr);
771 if (CurTok != ')')
772 return ErrorP("Expected ')' in prototype");
773
774 // success.
775 getNextToken(); // eat ')'.
776
777 // Verify right number of names for operator.
778 if (Kind && ArgNames.size() != Kind)
779 return ErrorP("Invalid number of operands for operator");
780
Lang Hames09bf4c12015-08-18 18:11:06 +0000781 return llvm::make_unique<PrototypeAST>(FnLoc, FnName, ArgNames, Kind != 0,
782 BinaryPrecedence);
Eric Christopher05917fa2014-12-08 18:00:47 +0000783}
784
785/// definition ::= 'def' prototype expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000786static std::unique_ptr<FunctionAST> ParseDefinition() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000787 getNextToken(); // eat def.
Lang Hames09bf4c12015-08-18 18:11:06 +0000788 auto Proto = ParsePrototype();
789 if (!Proto)
790 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000791
Lang Hames09bf4c12015-08-18 18:11:06 +0000792 if (auto E = ParseExpression())
793 return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
794 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000795}
796
797/// toplevelexpr ::= expression
Lang Hames09bf4c12015-08-18 18:11:06 +0000798static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000799 SourceLocation FnLoc = CurLoc;
Lang Hames09bf4c12015-08-18 18:11:06 +0000800 if (auto E = ParseExpression()) {
Eric Christopher05917fa2014-12-08 18:00:47 +0000801 // Make an anonymous proto.
Lang Hames2d789c32015-08-26 03:07:41 +0000802 auto Proto = llvm::make_unique<PrototypeAST>(FnLoc, "__anon_expr",
Lang Hames59b0da82015-08-19 18:15:58 +0000803 std::vector<std::string>());
Lang Hames09bf4c12015-08-18 18:11:06 +0000804 return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
Eric Christopher05917fa2014-12-08 18:00:47 +0000805 }
Lang Hames09bf4c12015-08-18 18:11:06 +0000806 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000807}
808
809/// external ::= 'extern' prototype
Lang Hames09bf4c12015-08-18 18:11:06 +0000810static std::unique_ptr<PrototypeAST> ParseExtern() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000811 getNextToken(); // eat extern.
812 return ParsePrototype();
813}
814
815//===----------------------------------------------------------------------===//
816// Debug Info Support
817//===----------------------------------------------------------------------===//
818
Lang Hames2d789c32015-08-26 03:07:41 +0000819static std::unique_ptr<DIBuilder> DBuilder;
Eric Christopher05917fa2014-12-08 18:00:47 +0000820
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000821DIType *DebugInfo::getDoubleTy() {
Duncan P. N. Exon Smith1d1a8e02015-04-15 23:49:09 +0000822 if (DblTy)
Eric Christopher05917fa2014-12-08 18:00:47 +0000823 return DblTy;
824
825 DblTy = DBuilder->createBasicType("double", 64, 64, dwarf::DW_ATE_float);
826 return DblTy;
827}
828
829void DebugInfo::emitLocation(ExprAST *AST) {
830 if (!AST)
831 return Builder.SetCurrentDebugLocation(DebugLoc());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000832 DIScope *Scope;
Eric Christopher05917fa2014-12-08 18:00:47 +0000833 if (LexicalBlocks.empty())
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000834 Scope = TheCU;
Eric Christopher05917fa2014-12-08 18:00:47 +0000835 else
Duncan P. N. Exon Smith3b6f1d52015-04-20 21:29:44 +0000836 Scope = LexicalBlocks.back();
Eric Christopher05917fa2014-12-08 18:00:47 +0000837 Builder.SetCurrentDebugLocation(
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000838 DebugLoc::get(AST->getLine(), AST->getCol(), Scope));
Eric Christopher05917fa2014-12-08 18:00:47 +0000839}
840
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000841static DISubroutineType *CreateFunctionType(unsigned NumArgs, DIFile *Unit) {
Duncan P. N. Exon Smith4e752fb2015-01-06 23:48:22 +0000842 SmallVector<Metadata *, 8> EltTys;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000843 DIType *DblTy = KSDbgInfo.getDoubleTy();
Eric Christopher05917fa2014-12-08 18:00:47 +0000844
845 // Add the result type.
846 EltTys.push_back(DblTy);
847
848 for (unsigned i = 0, e = NumArgs; i != e; ++i)
849 EltTys.push_back(DblTy);
850
Eric Christopher73f00412015-10-15 07:01:16 +0000851 return DBuilder->createSubroutineType(DBuilder->getOrCreateTypeArray(EltTys));
Eric Christopher05917fa2014-12-08 18:00:47 +0000852}
853
854//===----------------------------------------------------------------------===//
855// Code Generation
856//===----------------------------------------------------------------------===//
857
Lang Hames2d789c32015-08-26 03:07:41 +0000858static std::unique_ptr<Module> TheModule;
Eric Christopher05917fa2014-12-08 18:00:47 +0000859static std::map<std::string, AllocaInst *> NamedValues;
Lang Hames2d789c32015-08-26 03:07:41 +0000860static std::unique_ptr<KaleidoscopeJIT> TheJIT;
861static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
Eric Christopher05917fa2014-12-08 18:00:47 +0000862
863Value *ErrorV(const char *Str) {
864 Error(Str);
Lang Hames09bf4c12015-08-18 18:11:06 +0000865 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000866}
867
Lang Hames2d789c32015-08-26 03:07:41 +0000868Function *getFunction(std::string Name) {
869 // First, see if the function has already been added to the current module.
870 if (auto *F = TheModule->getFunction(Name))
871 return F;
872
873 // If not, check whether we can codegen the declaration from some existing
874 // prototype.
875 auto FI = FunctionProtos.find(Name);
876 if (FI != FunctionProtos.end())
877 return FI->second->codegen();
878
879 // If no existing prototype exists, return null.
880 return nullptr;
881}
882
Eric Christopher05917fa2014-12-08 18:00:47 +0000883/// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
884/// the function. This is used for mutable variables etc.
885static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
886 const std::string &VarName) {
887 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
888 TheFunction->getEntryBlock().begin());
Hans Wennborgcc9deb42015-09-29 18:02:48 +0000889 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), nullptr,
Eric Christopher05917fa2014-12-08 18:00:47 +0000890 VarName.c_str());
891}
892
Lang Hames2d789c32015-08-26 03:07:41 +0000893Value *NumberExprAST::codegen() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000894 KSDbgInfo.emitLocation(this);
895 return ConstantFP::get(getGlobalContext(), APFloat(Val));
896}
897
Lang Hames2d789c32015-08-26 03:07:41 +0000898Value *VariableExprAST::codegen() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000899 // Look this variable up in the function.
900 Value *V = NamedValues[Name];
Lang Hames09bf4c12015-08-18 18:11:06 +0000901 if (!V)
Eric Christopher05917fa2014-12-08 18:00:47 +0000902 return ErrorV("Unknown variable name");
903
904 KSDbgInfo.emitLocation(this);
905 // Load the value.
906 return Builder.CreateLoad(V, Name.c_str());
907}
908
Lang Hames2d789c32015-08-26 03:07:41 +0000909Value *UnaryExprAST::codegen() {
910 Value *OperandV = Operand->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +0000911 if (!OperandV)
912 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000913
Lang Hames2d789c32015-08-26 03:07:41 +0000914 Function *F = getFunction(std::string("unary") + Opcode);
Lang Hames09bf4c12015-08-18 18:11:06 +0000915 if (!F)
Eric Christopher05917fa2014-12-08 18:00:47 +0000916 return ErrorV("Unknown unary operator");
917
918 KSDbgInfo.emitLocation(this);
919 return Builder.CreateCall(F, OperandV, "unop");
920}
921
Lang Hames2d789c32015-08-26 03:07:41 +0000922Value *BinaryExprAST::codegen() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000923 KSDbgInfo.emitLocation(this);
924
925 // Special case '=' because we don't want to emit the LHS as an expression.
926 if (Op == '=') {
927 // Assignment requires the LHS to be an identifier.
Lang Hamese7c28bc2015-04-22 20:41:34 +0000928 // This assume we're building without RTTI because LLVM builds that way by
929 // default. If you build LLVM with RTTI this can be changed to a
930 // dynamic_cast for automatic error checking.
Lang Hames59b0da82015-08-19 18:15:58 +0000931 VariableExprAST *LHSE = static_cast<VariableExprAST *>(LHS.get());
Eric Christopher05917fa2014-12-08 18:00:47 +0000932 if (!LHSE)
933 return ErrorV("destination of '=' must be a variable");
934 // Codegen the RHS.
Lang Hames2d789c32015-08-26 03:07:41 +0000935 Value *Val = RHS->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +0000936 if (!Val)
937 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000938
939 // Look up the name.
940 Value *Variable = NamedValues[LHSE->getName()];
Lang Hames09bf4c12015-08-18 18:11:06 +0000941 if (!Variable)
Eric Christopher05917fa2014-12-08 18:00:47 +0000942 return ErrorV("Unknown variable name");
943
944 Builder.CreateStore(Val, Variable);
945 return Val;
946 }
947
Lang Hames2d789c32015-08-26 03:07:41 +0000948 Value *L = LHS->codegen();
949 Value *R = RHS->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +0000950 if (!L || !R)
951 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000952
953 switch (Op) {
954 case '+':
955 return Builder.CreateFAdd(L, R, "addtmp");
956 case '-':
957 return Builder.CreateFSub(L, R, "subtmp");
958 case '*':
959 return Builder.CreateFMul(L, R, "multmp");
960 case '<':
961 L = Builder.CreateFCmpULT(L, R, "cmptmp");
962 // Convert bool 0/1 to double 0.0 or 1.0
963 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
964 "booltmp");
965 default:
966 break;
967 }
968
969 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
970 // a call to it.
Lang Hames2d789c32015-08-26 03:07:41 +0000971 Function *F = getFunction(std::string("binary") + Op);
Eric Christopher05917fa2014-12-08 18:00:47 +0000972 assert(F && "binary operator not found!");
973
Lang Hames59b0da82015-08-19 18:15:58 +0000974 Value *Ops[] = {L, R};
Eric Christopher05917fa2014-12-08 18:00:47 +0000975 return Builder.CreateCall(F, Ops, "binop");
976}
977
Lang Hames2d789c32015-08-26 03:07:41 +0000978Value *CallExprAST::codegen() {
Eric Christopher05917fa2014-12-08 18:00:47 +0000979 KSDbgInfo.emitLocation(this);
980
981 // Look up the name in the global module table.
Lang Hames2d789c32015-08-26 03:07:41 +0000982 Function *CalleeF = getFunction(Callee);
Lang Hames09bf4c12015-08-18 18:11:06 +0000983 if (!CalleeF)
Eric Christopher05917fa2014-12-08 18:00:47 +0000984 return ErrorV("Unknown function referenced");
985
986 // If argument mismatch error.
987 if (CalleeF->arg_size() != Args.size())
988 return ErrorV("Incorrect # arguments passed");
989
990 std::vector<Value *> ArgsV;
991 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Lang Hames2d789c32015-08-26 03:07:41 +0000992 ArgsV.push_back(Args[i]->codegen());
Lang Hames09bf4c12015-08-18 18:11:06 +0000993 if (!ArgsV.back())
994 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +0000995 }
996
997 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
998}
999
Lang Hames2d789c32015-08-26 03:07:41 +00001000Value *IfExprAST::codegen() {
Eric Christopher05917fa2014-12-08 18:00:47 +00001001 KSDbgInfo.emitLocation(this);
1002
Lang Hames2d789c32015-08-26 03:07:41 +00001003 Value *CondV = Cond->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +00001004 if (!CondV)
1005 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001006
1007 // Convert condition to a bool by comparing equal to 0.0.
1008 CondV = Builder.CreateFCmpONE(
1009 CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
1010
1011 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1012
1013 // Create blocks for the then and else cases. Insert the 'then' block at the
1014 // end of the function.
1015 BasicBlock *ThenBB =
1016 BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1017 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1018 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
1019
1020 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1021
1022 // Emit then value.
1023 Builder.SetInsertPoint(ThenBB);
1024
Lang Hames2d789c32015-08-26 03:07:41 +00001025 Value *ThenV = Then->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +00001026 if (!ThenV)
1027 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001028
1029 Builder.CreateBr(MergeBB);
1030 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1031 ThenBB = Builder.GetInsertBlock();
1032
1033 // Emit else block.
1034 TheFunction->getBasicBlockList().push_back(ElseBB);
1035 Builder.SetInsertPoint(ElseBB);
1036
Lang Hames2d789c32015-08-26 03:07:41 +00001037 Value *ElseV = Else->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +00001038 if (!ElseV)
1039 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001040
1041 Builder.CreateBr(MergeBB);
1042 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1043 ElseBB = Builder.GetInsertBlock();
1044
1045 // Emit merge block.
1046 TheFunction->getBasicBlockList().push_back(MergeBB);
1047 Builder.SetInsertPoint(MergeBB);
1048 PHINode *PN =
1049 Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
1050
1051 PN->addIncoming(ThenV, ThenBB);
1052 PN->addIncoming(ElseV, ElseBB);
1053 return PN;
1054}
1055
Lang Hames59b0da82015-08-19 18:15:58 +00001056// Output for-loop as:
1057// var = alloca double
1058// ...
1059// start = startexpr
1060// store start -> var
1061// goto loop
1062// loop:
1063// ...
1064// bodyexpr
1065// ...
1066// loopend:
1067// step = stepexpr
1068// endcond = endexpr
1069//
1070// curvar = load var
1071// nextvar = curvar + step
1072// store nextvar -> var
1073// br endcond, loop, endloop
1074// outloop:
Lang Hames2d789c32015-08-26 03:07:41 +00001075Value *ForExprAST::codegen() {
Eric Christopher05917fa2014-12-08 18:00:47 +00001076 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1077
1078 // Create an alloca for the variable in the entry block.
1079 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1080
1081 KSDbgInfo.emitLocation(this);
1082
1083 // Emit the start code first, without 'variable' in scope.
Lang Hames2d789c32015-08-26 03:07:41 +00001084 Value *StartVal = Start->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +00001085 if (!StartVal)
1086 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001087
1088 // Store the value into the alloca.
1089 Builder.CreateStore(StartVal, Alloca);
1090
1091 // Make the new basic block for the loop header, inserting after current
1092 // block.
1093 BasicBlock *LoopBB =
1094 BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
1095
1096 // Insert an explicit fall through from the current block to the LoopBB.
1097 Builder.CreateBr(LoopBB);
1098
1099 // Start insertion in LoopBB.
1100 Builder.SetInsertPoint(LoopBB);
1101
1102 // Within the loop, the variable is defined equal to the PHI node. If it
1103 // shadows an existing variable, we have to restore it, so save it now.
1104 AllocaInst *OldVal = NamedValues[VarName];
1105 NamedValues[VarName] = Alloca;
1106
1107 // Emit the body of the loop. This, like any other expr, can change the
1108 // current BB. Note that we ignore the value computed by the body, but don't
1109 // allow an error.
Lang Hames2d789c32015-08-26 03:07:41 +00001110 if (!Body->codegen())
Lang Hames09bf4c12015-08-18 18:11:06 +00001111 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001112
1113 // Emit the step value.
Lang Hames59b0da82015-08-19 18:15:58 +00001114 Value *StepVal = nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001115 if (Step) {
Lang Hames2d789c32015-08-26 03:07:41 +00001116 StepVal = Step->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +00001117 if (!StepVal)
1118 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001119 } else {
1120 // If not specified, use 1.0.
1121 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
1122 }
1123
1124 // Compute the end condition.
Lang Hames2d789c32015-08-26 03:07:41 +00001125 Value *EndCond = End->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +00001126 if (!EndCond)
Lang Hames59b0da82015-08-19 18:15:58 +00001127 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001128
1129 // Reload, increment, and restore the alloca. This handles the case where
1130 // the body of the loop mutates the variable.
1131 Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
1132 Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
1133 Builder.CreateStore(NextVar, Alloca);
1134
1135 // Convert condition to a bool by comparing equal to 0.0.
1136 EndCond = Builder.CreateFCmpONE(
1137 EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
1138
1139 // Create the "after loop" block and insert it.
1140 BasicBlock *AfterBB =
1141 BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
1142
1143 // Insert the conditional branch into the end of LoopEndBB.
1144 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1145
1146 // Any new code will be inserted in AfterBB.
1147 Builder.SetInsertPoint(AfterBB);
1148
1149 // Restore the unshadowed variable.
1150 if (OldVal)
1151 NamedValues[VarName] = OldVal;
1152 else
1153 NamedValues.erase(VarName);
1154
1155 // for expr always returns 0.0.
1156 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
1157}
1158
Lang Hames2d789c32015-08-26 03:07:41 +00001159Value *VarExprAST::codegen() {
Eric Christopher05917fa2014-12-08 18:00:47 +00001160 std::vector<AllocaInst *> OldBindings;
1161
1162 Function *TheFunction = Builder.GetInsertBlock()->getParent();
1163
1164 // Register all variables and emit their initializer.
1165 for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
1166 const std::string &VarName = VarNames[i].first;
Lang Hames09bf4c12015-08-18 18:11:06 +00001167 ExprAST *Init = VarNames[i].second.get();
Eric Christopher05917fa2014-12-08 18:00:47 +00001168
1169 // Emit the initializer before adding the variable to scope, this prevents
1170 // the initializer from referencing the variable itself, and permits stuff
1171 // like this:
1172 // var a = 1 in
1173 // var a = a in ... # refers to outer 'a'.
1174 Value *InitVal;
1175 if (Init) {
Lang Hames2d789c32015-08-26 03:07:41 +00001176 InitVal = Init->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +00001177 if (!InitVal)
1178 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001179 } else { // If not specified, use 0.0.
1180 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
1181 }
1182
1183 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1184 Builder.CreateStore(InitVal, Alloca);
1185
1186 // Remember the old variable binding so that we can restore the binding when
1187 // we unrecurse.
1188 OldBindings.push_back(NamedValues[VarName]);
1189
1190 // Remember this binding.
1191 NamedValues[VarName] = Alloca;
1192 }
1193
1194 KSDbgInfo.emitLocation(this);
1195
1196 // Codegen the body, now that all vars are in scope.
Lang Hames2d789c32015-08-26 03:07:41 +00001197 Value *BodyVal = Body->codegen();
Lang Hames09bf4c12015-08-18 18:11:06 +00001198 if (!BodyVal)
1199 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001200
1201 // Pop all our variables from scope.
1202 for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
1203 NamedValues[VarNames[i].first] = OldBindings[i];
1204
1205 // Return the body computation.
1206 return BodyVal;
1207}
1208
Lang Hames2d789c32015-08-26 03:07:41 +00001209Function *PrototypeAST::codegen() {
Eric Christopher05917fa2014-12-08 18:00:47 +00001210 // Make the function type: double(double,double) etc.
1211 std::vector<Type *> Doubles(Args.size(),
1212 Type::getDoubleTy(getGlobalContext()));
1213 FunctionType *FT =
1214 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
1215
1216 Function *F =
Lang Hames2d789c32015-08-26 03:07:41 +00001217 Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
Eric Christopher05917fa2014-12-08 18:00:47 +00001218
1219 // Set names for all arguments.
1220 unsigned Idx = 0;
Lang Hames2d789c32015-08-26 03:07:41 +00001221 for (auto &Arg : F->args())
1222 Arg.setName(Args[Idx++]);
1223
1224 return F;
1225}
1226
1227Function *FunctionAST::codegen() {
1228 // Transfer ownership of the prototype to the FunctionProtos map, but keep a
1229 // reference to it for use below.
1230 auto &P = *Proto;
1231 FunctionProtos[Proto->getName()] = std::move(Proto);
1232 Function *TheFunction = getFunction(P.getName());
1233 if (!TheFunction)
1234 return nullptr;
1235
1236 // If this is an operator, install it.
1237 if (P.isBinaryOp())
1238 BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
1239
1240 // Create a new basic block to start insertion into.
1241 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
1242 Builder.SetInsertPoint(BB);
Eric Christopher05917fa2014-12-08 18:00:47 +00001243
1244 // Create a subprogram DIE for this function.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001245 DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU->getFilename(),
Duncan P. N. Exon Smithbe9e4fe2015-04-20 18:32:29 +00001246 KSDbgInfo.TheCU->getDirectory());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001247 DIScope *FContext = Unit;
Lang Hames2d789c32015-08-26 03:07:41 +00001248 unsigned LineNo = P.getLine();
1249 unsigned ScopeLine = LineNo;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001250 DISubprogram *SP = DBuilder->createFunction(
Lang Hames2d789c32015-08-26 03:07:41 +00001251 FContext, P.getName(), StringRef(), Unit, LineNo,
1252 CreateFunctionType(TheFunction->arg_size(), Unit),
1253 false /* internal linkage */, true /* definition */, ScopeLine,
Peter Collingbournea252ea02015-11-05 22:18:31 +00001254 DINode::FlagPrototyped, false);
1255 TheFunction->setSubprogram(SP);
Eric Christopher05917fa2014-12-08 18:00:47 +00001256
1257 // Push the current scope.
Lang Hames2d789c32015-08-26 03:07:41 +00001258 KSDbgInfo.LexicalBlocks.push_back(SP);
Eric Christopher05917fa2014-12-08 18:00:47 +00001259
1260 // Unset the location for the prologue emission (leading instructions with no
1261 // location in a function are considered part of the prologue and the debugger
1262 // will run past them when breaking on a function)
1263 KSDbgInfo.emitLocation(nullptr);
1264
Lang Hames2d789c32015-08-26 03:07:41 +00001265 // Record the function arguments in the NamedValues map.
1266 NamedValues.clear();
1267 unsigned ArgIdx = 0;
1268 for (auto &Arg : TheFunction->args()) {
1269 // Create an alloca for this variable.
1270 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName());
Eric Christopher05917fa2014-12-08 18:00:47 +00001271
Lang Hames2d789c32015-08-26 03:07:41 +00001272 // Create a debug descriptor for the variable.
1273 DILocalVariable *D = DBuilder->createParameterVariable(
1274 SP, Arg.getName(), ++ArgIdx, Unit, LineNo, KSDbgInfo.getDoubleTy(),
1275 true);
Eric Christopher05917fa2014-12-08 18:00:47 +00001276
Lang Hames2d789c32015-08-26 03:07:41 +00001277 DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(),
1278 DebugLoc::get(LineNo, 0, SP),
1279 Builder.GetInsertBlock());
1280
1281 // Store the initial value into the alloca.
1282 Builder.CreateStore(&Arg, Alloca);
1283
1284 // Add arguments to variable symbol table.
1285 NamedValues[Arg.getName()] = Alloca;
1286 }
Eric Christopher05917fa2014-12-08 18:00:47 +00001287
Lang Hames09bf4c12015-08-18 18:11:06 +00001288 KSDbgInfo.emitLocation(Body.get());
Eric Christopher05917fa2014-12-08 18:00:47 +00001289
Lang Hames2d789c32015-08-26 03:07:41 +00001290 if (Value *RetVal = Body->codegen()) {
Eric Christopher05917fa2014-12-08 18:00:47 +00001291 // Finish off the function.
1292 Builder.CreateRet(RetVal);
1293
1294 // Pop off the lexical block for the function.
1295 KSDbgInfo.LexicalBlocks.pop_back();
1296
1297 // Validate the generated code, checking for consistency.
1298 verifyFunction(*TheFunction);
1299
Eric Christopher05917fa2014-12-08 18:00:47 +00001300 return TheFunction;
1301 }
1302
1303 // Error reading body, remove function.
1304 TheFunction->eraseFromParent();
1305
Lang Hames2d789c32015-08-26 03:07:41 +00001306 if (P.isBinaryOp())
Eric Christopher05917fa2014-12-08 18:00:47 +00001307 BinopPrecedence.erase(Proto->getOperatorName());
1308
1309 // Pop off the lexical block for the function since we added it
1310 // unconditionally.
1311 KSDbgInfo.LexicalBlocks.pop_back();
1312
Lang Hames09bf4c12015-08-18 18:11:06 +00001313 return nullptr;
Eric Christopher05917fa2014-12-08 18:00:47 +00001314}
1315
1316//===----------------------------------------------------------------------===//
1317// Top-Level parsing and JIT Driver
1318//===----------------------------------------------------------------------===//
1319
Lang Hames2d789c32015-08-26 03:07:41 +00001320static void InitializeModule() {
1321 // Open a new module.
1322 TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext());
1323 TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());
1324}
Eric Christopher05917fa2014-12-08 18:00:47 +00001325
1326static void HandleDefinition() {
Lang Hames09bf4c12015-08-18 18:11:06 +00001327 if (auto FnAST = ParseDefinition()) {
Lang Hames2d789c32015-08-26 03:07:41 +00001328 if (!FnAST->codegen())
Eric Christopher05917fa2014-12-08 18:00:47 +00001329 fprintf(stderr, "Error reading function definition:");
Eric Christopher05917fa2014-12-08 18:00:47 +00001330 } else {
1331 // Skip token for error recovery.
1332 getNextToken();
1333 }
1334}
1335
1336static void HandleExtern() {
Lang Hames09bf4c12015-08-18 18:11:06 +00001337 if (auto ProtoAST = ParseExtern()) {
Lang Hames2d789c32015-08-26 03:07:41 +00001338 if (!ProtoAST->codegen())
Eric Christopher05917fa2014-12-08 18:00:47 +00001339 fprintf(stderr, "Error reading extern");
Lang Hames2d789c32015-08-26 03:07:41 +00001340 else
1341 FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST);
Eric Christopher05917fa2014-12-08 18:00:47 +00001342 } else {
1343 // Skip token for error recovery.
1344 getNextToken();
1345 }
1346}
1347
1348static void HandleTopLevelExpression() {
1349 // Evaluate a top-level expression into an anonymous function.
Lang Hames09bf4c12015-08-18 18:11:06 +00001350 if (auto FnAST = ParseTopLevelExpr()) {
Lang Hames2d789c32015-08-26 03:07:41 +00001351 if (!FnAST->codegen()) {
Eric Christopher05917fa2014-12-08 18:00:47 +00001352 fprintf(stderr, "Error generating code for top level expr");
1353 }
1354 } else {
1355 // Skip token for error recovery.
1356 getNextToken();
1357 }
1358}
1359
1360/// top ::= definition | external | expression | ';'
1361static void MainLoop() {
1362 while (1) {
1363 switch (CurTok) {
1364 case tok_eof:
1365 return;
Lang Hames59b0da82015-08-19 18:15:58 +00001366 case ';': // ignore top-level semicolons.
Eric Christopher05917fa2014-12-08 18:00:47 +00001367 getNextToken();
Lang Hames59b0da82015-08-19 18:15:58 +00001368 break;
Eric Christopher05917fa2014-12-08 18:00:47 +00001369 case tok_def:
1370 HandleDefinition();
1371 break;
1372 case tok_extern:
1373 HandleExtern();
1374 break;
1375 default:
1376 HandleTopLevelExpression();
1377 break;
1378 }
1379 }
1380}
1381
1382//===----------------------------------------------------------------------===//
1383// "Library" functions that can be "extern'd" from user code.
1384//===----------------------------------------------------------------------===//
1385
1386/// putchard - putchar that takes a double and returns 0.
NAKAMURA Takumiac9d3732015-08-28 03:34:33 +00001387extern "C" double putchard(double X) {
Lang Hamesd76e0672015-08-27 20:31:44 +00001388 fputc((char)X, stderr);
Eric Christopher05917fa2014-12-08 18:00:47 +00001389 return 0;
1390}
1391
1392/// printd - printf that takes a double prints it as "%f\n", returning 0.
NAKAMURA Takumiac9d3732015-08-28 03:34:33 +00001393extern "C" double printd(double X) {
Lang Hamesd76e0672015-08-27 20:31:44 +00001394 fprintf(stderr, "%f\n", X);
Eric Christopher05917fa2014-12-08 18:00:47 +00001395 return 0;
1396}
1397
1398//===----------------------------------------------------------------------===//
1399// Main driver code.
1400//===----------------------------------------------------------------------===//
1401
1402int main() {
1403 InitializeNativeTarget();
1404 InitializeNativeTargetAsmPrinter();
1405 InitializeNativeTargetAsmParser();
Eric Christopher05917fa2014-12-08 18:00:47 +00001406
1407 // Install standard binary operators.
1408 // 1 is lowest precedence.
1409 BinopPrecedence['='] = 2;
1410 BinopPrecedence['<'] = 10;
1411 BinopPrecedence['+'] = 20;
1412 BinopPrecedence['-'] = 20;
1413 BinopPrecedence['*'] = 40; // highest.
1414
1415 // Prime the first token.
1416 getNextToken();
1417
Lang Hames2d789c32015-08-26 03:07:41 +00001418 TheJIT = llvm::make_unique<KaleidoscopeJIT>();
1419
1420 InitializeModule();
Eric Christopher05917fa2014-12-08 18:00:47 +00001421
1422 // Add the current debug info version into the module.
1423 TheModule->addModuleFlag(Module::Warning, "Debug Info Version",
1424 DEBUG_METADATA_VERSION);
1425
1426 // Darwin only supports dwarf2.
1427 if (Triple(sys::getProcessTriple()).isOSDarwin())
1428 TheModule->addModuleFlag(llvm::Module::Warning, "Dwarf Version", 2);
1429
1430 // Construct the DIBuilder, we do this here because we need the module.
Lang Hames2d789c32015-08-26 03:07:41 +00001431 DBuilder = llvm::make_unique<DIBuilder>(*TheModule);
Eric Christopher05917fa2014-12-08 18:00:47 +00001432
1433 // Create the compile unit for the module.
1434 // Currently down as "fib.ks" as a filename since we're redirecting stdin
1435 // but we'd like actual source locations.
1436 KSDbgInfo.TheCU = DBuilder->createCompileUnit(
1437 dwarf::DW_LANG_C, "fib.ks", ".", "Kaleidoscope Compiler", 0, "", 0);
1438
Eric Christopher05917fa2014-12-08 18:00:47 +00001439 // Run the main "interpreter loop" now.
1440 MainLoop();
1441
Eric Christopher05917fa2014-12-08 18:00:47 +00001442 // Finalize the debug info.
1443 DBuilder->finalize();
1444
1445 // Print out all of the generated code.
1446 TheModule->dump();
1447
1448 return 0;
1449}