llvm.org GIT mirror llvm / 7a21775
[Kaleidoscope] Remove the old Kaleidoscope/ORC tutorial series. This code has been superseded by the new Building A JIT series. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271059 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 4 years ago
13 changed file(s) with 0 addition(s) and 5684 deletion(s). Raw diff Collapse all Expand all
+0
-4
examples/Kaleidoscope/Orc/CMakeLists.txt less more
None add_subdirectory(initial)
1 add_subdirectory(lazy_codegen)
2 add_subdirectory(lazy_irgen)
3 add_subdirectory(fully_lazy)
+0
-13
examples/Kaleidoscope/Orc/fully_lazy/CMakeLists.txt less more
None set(LLVM_LINK_COMPONENTS
1 Core
2 ExecutionEngine
3 Object
4 OrcJIT
5 RuntimeDyld
6 Support
7 native
8 )
9
10 add_kaleidoscope_chapter(Kaleidoscope-Orc-fully_lazy
11 toy.cpp
12 )
+0
-21
examples/Kaleidoscope/Orc/fully_lazy/README.txt less more
None //===----------------------------------------------------------------------===/
1 // Kaleidoscope with Orc - Lazy IRGen Version
2 //===----------------------------------------------------------------------===//
3
4 This version of Kaleidoscope with Orc demonstrates fully lazy IR-generation.
5 Building on the lazy-irgen version of the tutorial, this version injects JIT
6 callbacks to defer the bulk of IR-generation and code-generation of functions until
7 they are first called.
8
9 When a function definition is entered, a JIT callback is created and a stub
10 function is built that will call the body of the function indirectly. The body of
11 the function is *not* IRGen'd at this point. Instead, the function pointer for
12 the indirect call is initialized to point at the JIT callback, and the compile
13 action for the callback is initialized with a lambda that IRGens the body of the
14 function and adds it to the JIT. The function pointer is updated by the JIT
15 callback's update action to point at the newly emitted function body, so future
16 calls to the stub will go straight to the body, not through the JIT.
17
18 This directory contains a Makefile that allows the code to be built in a
19 standalone manner, independent of the larger LLVM build infrastructure. To build
20 the program you will need to have 'clang++' and 'llvm-config' in your path.
+0
-1461
examples/Kaleidoscope/Orc/fully_lazy/toy.cpp less more
None #include "llvm/ADT/APFloat.h"
1 #include "llvm/ADT/STLExtras.h"
2 #include "llvm/ExecutionEngine/ExecutionEngine.h"
3 #include "llvm/ExecutionEngine/RuntimeDyld.h"
4 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
5 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
6 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
7 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
8 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
9 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
10 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
11 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
12 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
13 #include "llvm/IR/BasicBlock.h"
14 #include "llvm/IR/Constant.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/GlobalVariable.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/IR/Verifier.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/TargetSelect.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include
30 #include
31 #include
32 #include
33 #include
34 #include
35 #include
36 #include
37 #include
38 #include
39 #include
40 #include
41 #include
42
43 using namespace llvm;
44 using namespace llvm::orc;
45
46 //===----------------------------------------------------------------------===//
47 // Lexer
48 //===----------------------------------------------------------------------===//
49
50 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
51 // of these for known things.
52 enum Token {
53 tok_eof = -1,
54
55 // commands
56 tok_def = -2, tok_extern = -3,
57
58 // primary
59 tok_identifier = -4, tok_number = -5,
60
61 // control
62 tok_if = -6, tok_then = -7, tok_else = -8,
63 tok_for = -9, tok_in = -10,
64
65 // operators
66 tok_binary = -11, tok_unary = -12,
67
68 // var definition
69 tok_var = -13
70 };
71
72 static std::string IdentifierStr; // Filled in if tok_identifier
73 static double NumVal; // Filled in if tok_number
74
75 /// gettok - Return the next token from standard input.
76 static int gettok() {
77 static int LastChar = ' ';
78
79 // Skip any whitespace.
80 while (isspace(LastChar))
81 LastChar = getchar();
82
83 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
84 IdentifierStr = LastChar;
85 while (isalnum((LastChar = getchar())))
86 IdentifierStr += LastChar;
87
88 if (IdentifierStr == "def") return tok_def;
89 if (IdentifierStr == "extern") return tok_extern;
90 if (IdentifierStr == "if") return tok_if;
91 if (IdentifierStr == "then") return tok_then;
92 if (IdentifierStr == "else") return tok_else;
93 if (IdentifierStr == "for") return tok_for;
94 if (IdentifierStr == "in") return tok_in;
95 if (IdentifierStr == "binary") return tok_binary;
96 if (IdentifierStr == "unary") return tok_unary;
97 if (IdentifierStr == "var") 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 LastChar = getchar();
115 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
116
117 if (LastChar != EOF)
118 return gettok();
119 }
120
121 // Check for end of file. Don't eat the EOF.
122 if (LastChar == EOF)
123 return tok_eof;
124
125 // Otherwise, just return the character as its ascii value.
126 int ThisChar = LastChar;
127 LastChar = getchar();
128 return ThisChar;
129 }
130
131 //===----------------------------------------------------------------------===//
132 // Abstract Syntax Tree (aka Parse Tree)
133 //===----------------------------------------------------------------------===//
134
135 class IRGenContext;
136
137 /// ExprAST - Base class for all expression nodes.
138 struct ExprAST {
139 virtual ~ExprAST() {}
140 virtual Value *IRGen(IRGenContext &C) const = 0;
141 };
142
143 /// NumberExprAST - Expression class for numeric literals like "1.0".
144 struct NumberExprAST : public ExprAST {
145 NumberExprAST(double Val) : Val(Val) {}
146 Value *IRGen(IRGenContext &C) const override;
147
148 double Val;
149 };
150
151 /// VariableExprAST - Expression class for referencing a variable, like "a".
152 struct VariableExprAST : public ExprAST {
153 VariableExprAST(std::string Name) : Name(std::move(Name)) {}
154 Value *IRGen(IRGenContext &C) const override;
155
156 std::string Name;
157 };
158
159 /// UnaryExprAST - Expression class for a unary operator.
160 struct UnaryExprAST : public ExprAST {
161 UnaryExprAST(char Opcode, std::unique_ptr Operand)
162 : Opcode(std::move(Opcode)), Operand(std::move(Operand)) {}
163
164 Value *IRGen(IRGenContext &C) const override;
165
166 char Opcode;
167 std::unique_ptr Operand;
168 };
169
170 /// BinaryExprAST - Expression class for a binary operator.
171 struct BinaryExprAST : public ExprAST {
172 BinaryExprAST(char Op, std::unique_ptr LHS,
173 std::unique_ptr RHS)
174 : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
175
176 Value *IRGen(IRGenContext &C) const override;
177
178 char Op;
179 std::unique_ptr LHS, RHS;
180 };
181
182 /// CallExprAST - Expression class for function calls.
183 struct CallExprAST : public ExprAST {
184 CallExprAST(std::string CalleeName,
185 std::vector> Args)
186 : CalleeName(std::move(CalleeName)), Args(std::move(Args)) {}
187
188 Value *IRGen(IRGenContext &C) const override;
189
190 std::string CalleeName;
191 std::vector> Args;
192 };
193
194 /// IfExprAST - Expression class for if/then/else.
195 struct IfExprAST : public ExprAST {
196 IfExprAST(std::unique_ptr Cond, std::unique_ptr Then,
197 std::unique_ptr Else)
198 : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
199 Value *IRGen(IRGenContext &C) const override;
200
201 std::unique_ptr Cond, Then, Else;
202 };
203
204 /// ForExprAST - Expression class for for/in.
205 struct ForExprAST : public ExprAST {
206 ForExprAST(std::string VarName, std::unique_ptr Start,
207 std::unique_ptr End, std::unique_ptr Step,
208 std::unique_ptr Body)
209 : VarName(std::move(VarName)), Start(std::move(Start)), End(std::move(End)),
210 Step(std::move(Step)), Body(std::move(Body)) {}
211
212 Value *IRGen(IRGenContext &C) const override;
213
214 std::string VarName;
215 std::unique_ptr Start, End, Step, Body;
216 };
217
218 /// VarExprAST - Expression class for var/in
219 struct VarExprAST : public ExprAST {
220 typedef std::pair> Binding;
221 typedef std::vector BindingList;
222
223 VarExprAST(BindingList VarBindings, std::unique_ptr Body)
224 : VarBindings(std::move(VarBindings)), Body(std::move(Body)) {}
225
226 Value *IRGen(IRGenContext &C) const override;
227
228 BindingList VarBindings;
229 std::unique_ptr Body;
230 };
231
232 /// PrototypeAST - This class represents the "prototype" for a function,
233 /// which captures its argument names as well as if it is an operator.
234 struct PrototypeAST {
235 PrototypeAST(std::string Name, std::vector Args,
236 bool IsOperator = false, unsigned Precedence = 0)
237 : Name(std::move(Name)), Args(std::move(Args)), IsOperator(IsOperator),
238 Precedence(Precedence) {}
239
240 Function *IRGen(IRGenContext &C) const;
241 void CreateArgumentAllocas(Function *F, IRGenContext &C);
242
243 bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
244 bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
245
246 char getOperatorName() const {
247 assert(isUnaryOp() || isBinaryOp());
248 return Name[Name.size()-1];
249 }
250
251 std::string Name;
252 std::vector Args;
253 bool IsOperator;
254 unsigned Precedence; // Precedence if a binary op.
255 };
256
257 /// FunctionAST - This class represents a function definition itself.
258 struct FunctionAST {
259 FunctionAST(std::unique_ptr Proto,
260 std::unique_ptr Body)
261 : Proto(std::move(Proto)), Body(std::move(Body)) {}
262
263 Function *IRGen(IRGenContext &C) const;
264
265 std::unique_ptr Proto;
266 std::unique_ptr Body;
267 };
268
269 //===----------------------------------------------------------------------===//
270 // Parser
271 //===----------------------------------------------------------------------===//
272
273 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
274 /// token the parser is looking at. getNextToken reads another token from the
275 /// lexer and updates CurTok with its results.
276 static int CurTok;
277 static int getNextToken() {
278 return CurTok = gettok();
279 }
280
281 /// BinopPrecedence - This holds the precedence for each binary operator that is
282 /// defined.
283 static std::map BinopPrecedence;
284
285 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
286 static int GetTokPrecedence() {
287 if (!isascii(CurTok))
288 return -1;
289
290 // Make sure it's a declared binop.
291 int TokPrec = BinopPrecedence[CurTok];
292 if (TokPrec <= 0) return -1;
293 return TokPrec;
294 }
295
296 template
297 std::unique_ptr ErrorU(const std::string &Str) {
298 std::cerr << "Error: " << Str << "\n";
299 return nullptr;
300 }
301
302 template
303 T* ErrorP(const std::string &Str) {
304 std::cerr << "Error: " << Str << "\n";
305 return nullptr;
306 }
307
308 static std::unique_ptr ParseExpression();
309
310 /// identifierexpr
311 /// ::= identifier
312 /// ::= identifier '(' expression* ')'
313 static std::unique_ptr ParseIdentifierExpr() {
314 std::string IdName = IdentifierStr;
315
316 getNextToken(); // eat identifier.
317
318 if (CurTok != '(') // Simple variable ref.
319 return llvm::make_unique(IdName);
320
321 // Call.
322 getNextToken(); // eat (
323 std::vector> Args;
324 if (CurTok != ')') {
325 while (true) {
326 auto Arg = ParseExpression();
327 if (!Arg) return nullptr;
328 Args.push_back(std::move(Arg));
329
330 if (CurTok == ')') break;
331
332 if (CurTok != ',')
333 return ErrorU("Expected ')' or ',' in argument list");
334 getNextToken();
335 }
336 }
337
338 // Eat the ')'.
339 getNextToken();
340
341 return llvm::make_unique(IdName, std::move(Args));
342 }
343
344 /// numberexpr ::= number
345 static std::unique_ptr ParseNumberExpr() {
346 auto Result = llvm::make_unique(NumVal);
347 getNextToken(); // consume the number
348 return Result;
349 }
350
351 /// parenexpr ::= '(' expression ')'
352 static std::unique_ptr ParseParenExpr() {
353 getNextToken(); // eat (.
354 auto V = ParseExpression();
355 if (!V)
356 return nullptr;
357
358 if (CurTok != ')')
359 return ErrorU("expected ')'");
360 getNextToken(); // eat ).
361 return V;
362 }
363
364 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
365 static std::unique_ptr ParseIfExpr() {
366 getNextToken(); // eat the if.
367
368 // condition.
369 auto Cond = ParseExpression();
370 if (!Cond)
371 return nullptr;
372
373 if (CurTok != tok_then)
374 return ErrorU("expected then");
375 getNextToken(); // eat the then
376
377 auto Then = ParseExpression();
378 if (!Then)
379 return nullptr;
380
381 if (CurTok != tok_else)
382 return ErrorU("expected else");
383
384 getNextToken();
385
386 auto Else = ParseExpression();
387 if (!Else)
388 return nullptr;
389
390 return llvm::make_unique(std::move(Cond), std::move(Then),
391 std::move(Else));
392 }
393
394 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
395 static std::unique_ptr ParseForExpr() {
396 getNextToken(); // eat the for.
397
398 if (CurTok != tok_identifier)
399 return ErrorU("expected identifier after for");
400
401 std::string IdName = IdentifierStr;
402 getNextToken(); // eat identifier.
403
404 if (CurTok != '=')
405 return ErrorU("expected '=' after for");
406 getNextToken(); // eat '='.
407
408 auto Start = ParseExpression();
409 if (!Start)
410 return nullptr;
411 if (CurTok != ',')
412 return ErrorU("expected ',' after for start value");
413 getNextToken();
414
415 auto End = ParseExpression();
416 if (!End)
417 return nullptr;
418
419 // The step value is optional.
420 std::unique_ptr Step;
421 if (CurTok == ',') {
422 getNextToken();
423 Step = ParseExpression();
424 if (!Step)
425 return nullptr;
426 }
427
428 if (CurTok != tok_in)
429 return ErrorU("expected 'in' after for");
430 getNextToken(); // eat 'in'.
431
432 auto Body = ParseExpression();
433 if (Body)
434 return nullptr;
435
436 return llvm::make_unique(IdName, std::move(Start), std::move(End),
437 std::move(Step), std::move(Body));
438 }
439
440 /// varexpr ::= 'var' identifier ('=' expression)?
441 // (',' identifier ('=' expression)?)* 'in' expression
442 static std::unique_ptr ParseVarExpr() {
443 getNextToken(); // eat the var.
444
445 VarExprAST::BindingList VarBindings;
446
447 // At least one variable name is required.
448 if (CurTok != tok_identifier)
449 return ErrorU("expected identifier after var");
450
451 while (true) {
452 std::string Name = IdentifierStr;
453 getNextToken(); // eat identifier.
454
455 // Read the optional initializer.
456 std::unique_ptr Init;
457 if (CurTok == '=') {
458 getNextToken(); // eat the '='.
459
460 Init = ParseExpression();
461 if (!Init)
462 return nullptr;
463 }
464
465 VarBindings.push_back(VarExprAST::Binding(Name, std::move(Init)));
466
467 // End of var list, exit loop.
468 if (CurTok != ',') break;
469 getNextToken(); // eat the ','.
470
471 if (CurTok != tok_identifier)
472 return ErrorU("expected identifier list after var");
473 }
474
475 // At this point, we have to have 'in'.
476 if (CurTok != tok_in)
477 return ErrorU("expected 'in' keyword after 'var'");
478 getNextToken(); // eat 'in'.
479
480 auto Body = ParseExpression();
481 if (!Body)
482 return nullptr;
483
484 return llvm::make_unique(std::move(VarBindings), std::move(Body));
485 }
486
487 /// primary
488 /// ::= identifierexpr
489 /// ::= numberexpr
490 /// ::= parenexpr
491 /// ::= ifexpr
492 /// ::= forexpr
493 /// ::= varexpr
494 static std::unique_ptr ParsePrimary() {
495 switch (CurTok) {
496 default: return ErrorU("unknown token when expecting an expression");
497 case tok_identifier: return ParseIdentifierExpr();
498 case tok_number: return ParseNumberExpr();
499 case '(': return ParseParenExpr();
500 case tok_if: return ParseIfExpr();
501 case tok_for: return ParseForExpr();
502 case tok_var: return ParseVarExpr();
503 }
504 }
505
506 /// unary
507 /// ::= primary
508 /// ::= '!' unary
509 static std::unique_ptr ParseUnary() {
510 // If the current token is not an operator, it must be a primary expr.
511 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
512 return ParsePrimary();
513
514 // If this is a unary operator, read it.
515 int Opc = CurTok;
516 getNextToken();
517 if (auto Operand = ParseUnary())
518 return llvm::make_unique(Opc, std::move(Operand));
519 return nullptr;
520 }
521
522 /// binoprhs
523 /// ::= ('+' unary)*
524 static std::unique_ptr ParseBinOpRHS(int ExprPrec,
525 std::unique_ptr LHS) {
526 // If this is a binop, find its precedence.
527 while (true) {
528 int TokPrec = GetTokPrecedence();
529
530 // If this is a binop that binds at least as tightly as the current binop,
531 // consume it, otherwise we are done.
532 if (TokPrec < ExprPrec)
533 return LHS;
534
535 // Okay, we know this is a binop.
536 int BinOp = CurTok;
537 getNextToken(); // eat binop
538
539 // Parse the unary expression after the binary operator.
540 auto RHS = ParseUnary();
541 if (!RHS)
542 return nullptr;
543
544 // If BinOp binds less tightly with RHS than the operator after RHS, let
545 // the pending operator take RHS as its LHS.
546 int NextPrec = GetTokPrecedence();
547 if (TokPrec < NextPrec) {
548 RHS = ParseBinOpRHS(TokPrec+1, std::move(RHS));
549 if (!RHS)
550 return nullptr;
551 }
552
553 // Merge LHS/RHS.
554 LHS = llvm::make_unique(BinOp, std::move(LHS), std::move(RHS));
555 }
556 }
557
558 /// expression
559 /// ::= unary binoprhs
560 ///
561 static std::unique_ptr ParseExpression() {
562 auto LHS = ParseUnary();
563 if (!LHS)
564 return nullptr;
565
566 return ParseBinOpRHS(0, std::move(LHS));
567 }
568
569 /// prototype
570 /// ::= id '(' id* ')'
571 /// ::= binary LETTER number? (id, id)
572 /// ::= unary LETTER (id)
573 static std::unique_ptr ParsePrototype() {
574 std::string FnName;
575
576 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
577 unsigned BinaryPrecedence = 30;
578
579 switch (CurTok) {
580 default:
581 return ErrorU("Expected function name in prototype");
582 case tok_identifier:
583 FnName = IdentifierStr;
584 Kind = 0;
585 getNextToken();
586 break;
587 case tok_unary:
588 getNextToken();
589 if (!isascii(CurTok))
590 return ErrorU("Expected unary operator");
591 FnName = "unary";
592 FnName += (char)CurTok;
593 Kind = 1;
594 getNextToken();
595 break;
596 case tok_binary:
597 getNextToken();
598 if (!isascii(CurTok))
599 return ErrorU("Expected binary operator");
600 FnName = "binary";
601 FnName += (char)CurTok;
602 Kind = 2;
603 getNextToken();
604
605 // Read the precedence if present.
606 if (CurTok == tok_number) {
607 if (NumVal < 1 || NumVal > 100)
608 return ErrorU("Invalid precedecnce: must be 1..100");
609 BinaryPrecedence = (unsigned)NumVal;
610 getNextToken();
611 }
612 break;
613 }
614
615 if (CurTok != '(')
616 return ErrorU("Expected '(' in prototype");
617
618 std::vector ArgNames;
619 while (getNextToken() == tok_identifier)
620 ArgNames.push_back(IdentifierStr);
621 if (CurTok != ')')
622 return ErrorU("Expected ')' in prototype");
623
624 // success.
625 getNextToken(); // eat ')'.
626
627 // Verify right number of names for operator.
628 if (Kind && ArgNames.size() != Kind)
629 return ErrorU("Invalid number of operands for operator");
630
631 return llvm::make_unique(FnName, std::move(ArgNames), Kind != 0,
632 BinaryPrecedence);
633 }
634
635 /// definition ::= 'def' prototype expression
636 static std::unique_ptr ParseDefinition() {
637 getNextToken(); // eat def.
638 auto Proto = ParsePrototype();
639 if (!Proto)
640 return nullptr;
641
642 if (auto Body = ParseExpression())
643 return llvm::make_unique(std::move(Proto), std::move(Body));
644 return nullptr;
645 }
646
647 /// toplevelexpr ::= expression
648 static std::unique_ptr ParseTopLevelExpr() {
649 if (auto E = ParseExpression()) {
650 // Make an anonymous proto.
651 auto Proto =
652 llvm::make_unique("__anon_expr", std::vector());
653 return llvm::make_unique(std::move(Proto), std::move(E));
654 }
655 return nullptr;
656 }
657
658 /// external ::= 'extern' prototype
659 static std::unique_ptr ParseExtern() {
660 getNextToken(); // eat extern.
661 return ParsePrototype();
662 }
663
664 //===----------------------------------------------------------------------===//
665 // Code Generation
666 //===----------------------------------------------------------------------===//
667
668 // FIXME: Obviously we can do better than this
669 std::string GenerateUniqueName(const std::string &Root) {
670 static int i = 0;
671 std::ostringstream NameStream;
672 NameStream << Root << ++i;
673 return NameStream.str();
674 }
675
676 std::string MakeLegalFunctionName(std::string Name)
677 {
678 std::string NewName;
679 assert(!Name.empty() && "Base name must not be empty");
680
681 // Start with what we have
682 NewName = Name;
683
684 // Look for a numberic first character
685 if (NewName.find_first_of("0123456789") == 0) {
686 NewName.insert(0, 1, 'n');
687 }
688
689 // Replace illegal characters with their ASCII equivalent
690 std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
691 size_t pos;
692 while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
693 std::ostringstream NumStream;
694 NumStream << (int)NewName.at(pos);
695 NewName = NewName.replace(pos, 1, NumStream.str());
696 }
697
698 return NewName;
699 }
700
701 class SessionContext {
702 public:
703 SessionContext(LLVMContext &C)
704 : Context(C), TM(EngineBuilder().selectTarget()) {}
705 LLVMContext& getLLVMContext() const { return Context; }
706 TargetMachine& getTarget() { return *TM; }
707 void addPrototypeAST(std::unique_ptr P);
708 PrototypeAST* getPrototypeAST(const std::string &Name);
709
710 private:
711 typedef std::map> PrototypeMap;
712
713 LLVMContext &Context;
714 std::unique_ptr TM;
715
716 PrototypeMap Prototypes;
717 };
718
719 void SessionContext::addPrototypeAST(std::unique_ptr P) {
720 Prototypes[P->Name] = std::move(P);
721 }
722
723 PrototypeAST* SessionContext::getPrototypeAST(const std::string &Name) {
724 PrototypeMap::iterator I = Prototypes.find(Name);
725 if (I != Prototypes.end())
726 return I->second.get();
727 return nullptr;
728 }
729
730 class IRGenContext {
731 public:
732 IRGenContext(SessionContext &S)
733 : Session(S),
734 M(new Module(GenerateUniqueName("jit_module_"),
735 Session.getLLVMContext())),
736 Builder(Session.getLLVMContext()) {
737 M->setDataLayout(Session.getTarget().createDataLayout());
738 }
739
740 SessionContext& getSession() { return Session; }
741 Module& getM() const { return *M; }
742 std::unique_ptr takeM() { return std::move(M); }
743 IRBuilder<>& getBuilder() { return Builder; }
744 LLVMContext& getLLVMContext() { return Session.getLLVMContext(); }
745 Function* getPrototype(const std::string &Name);
746
747 std::map NamedValues;
748
749 private:
750 SessionContext &Session;
751 std::unique_ptr M;
752 IRBuilder<> Builder;
753 };
754
755 Function* IRGenContext::getPrototype(const std::string &Name) {
756 if (Function *ExistingProto = M->getFunction(Name))
757 return ExistingProto;
758 if (PrototypeAST *ProtoAST = Session.getPrototypeAST(Name))
759 return ProtoAST->IRGen(*this);
760 return nullptr;
761 }
762
763 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
764 /// the function. This is used for mutable variables etc.
765 static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
766 const std::string &VarName) {
767 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
768 TheFunction->getEntryBlock().begin());
769 return TmpB.CreateAlloca(Type::getDoubleTy(TheFunction->getContext()),
770 nullptr, VarName);
771 }
772
773 Value *NumberExprAST::IRGen(IRGenContext &C) const {
774 return ConstantFP::get(C.getLLVMContext(), APFloat(Val));
775 }
776
777 Value *VariableExprAST::IRGen(IRGenContext &C) const {
778 // Look this variable up in the function.
779 Value *V = C.NamedValues[Name];
780
781 if (!V)
782 return ErrorP("Unknown variable name '" + Name + "'");
783
784 // Load the value.
785 return C.getBuilder().CreateLoad(V, Name.c_str());
786 }
787
788 Value *UnaryExprAST::IRGen(IRGenContext &C) const {
789 if (Value *OperandV = Operand->IRGen(C)) {
790 std::string FnName = MakeLegalFunctionName(std::string("unary")+Opcode);
791 if (Function *F = C.getPrototype(FnName))
792 return C.getBuilder().CreateCall(F, OperandV, "unop");
793 return ErrorP("Unknown unary operator");
794 }
795
796 // Could not codegen operand - return null.
797 return nullptr;
798 }
799
800 Value *BinaryExprAST::IRGen(IRGenContext &C) const {
801 // Special case '=' because we don't want to emit the LHS as an expression.
802 if (Op == '=') {
803 // Assignment requires the LHS to be an identifier.
804 auto &LHSVar = static_cast(*LHS);
805 // Codegen the RHS.
806 Value *Val = RHS->IRGen(C);
807 if (!Val) return nullptr;
808
809 // Look up the name.
810 if (auto Variable = C.NamedValues[LHSVar.Name]) {
811 C.getBuilder().CreateStore(Val, Variable);
812 return Val;
813 }
814 return ErrorP("Unknown variable name");
815 }
816
817 Value *L = LHS->IRGen(C);
818 Value *R = RHS->IRGen(C);
819 if (!L || !R) return nullptr;
820
821 switch (Op) {
822 case '+': return C.getBuilder().CreateFAdd(L, R, "addtmp");
823 case '-': return C.getBuilder().CreateFSub(L, R, "subtmp");
824 case '*': return C.getBuilder().CreateFMul(L, R, "multmp");
825 case '/': return C.getBuilder().CreateFDiv(L, R, "divtmp");
826 case '<':
827 L = C.getBuilder().CreateFCmpULT(L, R, "cmptmp");
828 // Convert bool 0/1 to double 0.0 or 1.0
829 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(C.getLLVMContext()),
830 "booltmp");
831 default: break;
832 }
833
834 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
835 // a call to it.
836 std::string FnName = MakeLegalFunctionName(std::string("binary")+Op);
837 if (Function *F = C.getPrototype(FnName)) {
838 Value *Ops[] = { L, R };
839 return C.getBuilder().CreateCall(F, Ops, "binop");
840 }
841
842 return ErrorP("Unknown binary operator");
843 }
844
845 Value *CallExprAST::IRGen(IRGenContext &C) const {
846 // Look up the name in the global module table.
847 if (auto CalleeF = C.getPrototype(CalleeName)) {
848 // If argument mismatch error.
849 if (CalleeF->arg_size() != Args.size())
850 return ErrorP("Incorrect # arguments passed");
851
852 std::vector ArgsV;
853 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
854 ArgsV.push_back(Args[i]->IRGen(C));
855 if (!ArgsV.back()) return nullptr;
856 }
857
858 return C.getBuilder().CreateCall(CalleeF, ArgsV, "calltmp");
859 }
860
861 return ErrorP("Unknown function referenced");
862 }
863
864 Value *IfExprAST::IRGen(IRGenContext &C) const {
865 Value *CondV = Cond->IRGen(C);
866 if (!CondV) return nullptr;
867
868 // Convert condition to a bool by comparing equal to 0.0.
869 ConstantFP *FPZero =
870 ConstantFP::get(C.getLLVMContext(), APFloat(0.0));
871 CondV = C.getBuilder().CreateFCmpONE(CondV, FPZero, "ifcond");
872
873 Function *TheFunction = C.getBuilder().GetInsertBlock()->getParent();
874
875 // Create blocks for the then and else cases. Insert the 'then' block at the
876 // end of the function.
877 BasicBlock *ThenBB = BasicBlock::Create(C.getLLVMContext(), "then", TheFunction);
878 BasicBlock *ElseBB = BasicBlock::Create(C.getLLVMContext(), "else");
879 BasicBlock *MergeBB = BasicBlock::Create(C.getLLVMContext(), "ifcont");
880
881 C.getBuilder().CreateCondBr(CondV, ThenBB, ElseBB);
882
883 // Emit then value.
884 C.getBuilder().SetInsertPoint(ThenBB);
885
886 Value *ThenV = Then->IRGen(C);
887 if (!ThenV) return nullptr;
888
889 C.getBuilder().CreateBr(MergeBB);
890 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
891 ThenBB = C.getBuilder().GetInsertBlock();
892
893 // Emit else block.
894 TheFunction->getBasicBlockList().push_back(ElseBB);
895 C.getBuilder().SetInsertPoint(ElseBB);
896
897 Value *ElseV = Else->IRGen(C);
898 if (!ElseV) return nullptr;
899
900 C.getBuilder().CreateBr(MergeBB);
901 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
902 ElseBB = C.getBuilder().GetInsertBlock();
903
904 // Emit merge block.
905 TheFunction->getBasicBlockList().push_back(MergeBB);
906 C.getBuilder().SetInsertPoint(MergeBB);
907 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(C.getLLVMContext()),
908 2, "iftmp");
909
910 PN->addIncoming(ThenV, ThenBB);
911 PN->addIncoming(ElseV, ElseBB);
912 return PN;
913 }
914
915 Value *ForExprAST::IRGen(IRGenContext &C) const {
916 // Output this as:
917 // var = alloca double
918 // ...
919 // start = startexpr
920 // store start -> var
921 // goto loop
922 // loop:
923 // ...
924 // bodyexpr
925 // ...
926 // loopend:
927 // step = stepexpr
928 // endcond = endexpr
929 //
930 // curvar = load var
931 // nextvar = curvar + step
932 // store nextvar -> var
933 // br endcond, loop, endloop
934 // outloop:
935
936 Function *TheFunction = C.getBuilder().GetInsertBlock()->getParent();
937
938 // Create an alloca for the variable in the entry block.
939 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
940
941 // Emit the start code first, without 'variable' in scope.
942 Value *StartVal = Start->IRGen(C);
943 if (!StartVal) return nullptr;
944
945 // Store the value into the alloca.
946 C.getBuilder().CreateStore(StartVal, Alloca);
947
948 // Make the new basic block for the loop header, inserting after current
949 // block.
950 BasicBlock *LoopBB =
951 BasicBlock::Create(C.getLLVMContext(), "loop", TheFunction);
952
953 // Insert an explicit fall through from the current block to the LoopBB.
954 C.getBuilder().CreateBr(LoopBB);
955
956 // Start insertion in LoopBB.
957 C.getBuilder().SetInsertPoint(LoopBB);
958
959 // Within the loop, the variable is defined equal to the PHI node. If it
960 // shadows an existing variable, we have to restore it, so save it now.
961 AllocaInst *OldVal = C.NamedValues[VarName];
962 C.NamedValues[VarName] = Alloca;
963
964 // Emit the body of the loop. This, like any other expr, can change the
965 // current BB. Note that we ignore the value computed by the body, but don't
966 // allow an error.
967 if (!Body->IRGen(C))
968 return nullptr;
969
970 // Emit the step value.
971 Value *StepVal;
972 if (Step) {
973 StepVal = Step->IRGen(C);
974 if (!StepVal) return nullptr;
975 } else {
976 // If not specified, use 1.0.
977 StepVal = ConstantFP::get(C.getLLVMContext(), APFloat(1.0));
978 }
979
980 // Compute the end condition.
981 Value *EndCond = End->IRGen(C);
982 if (!EndCond) return nullptr;
983
984 // Reload, increment, and restore the alloca. This handles the case where
985 // the body of the loop mutates the variable.
986 Value *CurVar = C.getBuilder().CreateLoad(Alloca, VarName.c_str());
987 Value *NextVar = C.getBuilder().CreateFAdd(CurVar, StepVal, "nextvar");
988 C.getBuilder().CreateStore(NextVar, Alloca);
989
990 // Convert condition to a bool by comparing equal to 0.0.
991 EndCond = C.getBuilder().CreateFCmpONE(
992 EndCond, ConstantFP::get(C.getLLVMContext(), APFloat(0.0)), "loopcond");
993
994 // Create the "after loop" block and insert it.
995 BasicBlock *AfterBB =
996 BasicBlock::Create(C.getLLVMContext(), "afterloop", TheFunction);
997
998 // Insert the conditional branch into the end of LoopEndBB.
999 C.getBuilder().CreateCondBr(EndCond, LoopBB, AfterBB);
1000
1001 // Any new code will be inserted in AfterBB.
1002 C.getBuilder().SetInsertPoint(AfterBB);
1003
1004 // Restore the unshadowed variable.
1005 if (OldVal)
1006 C.NamedValues[VarName] = OldVal;
1007 else
1008 C.NamedValues.erase(VarName);
1009
1010 // for expr always returns 0.0.
1011 return Constant::getNullValue(Type::getDoubleTy(C.getLLVMContext()));
1012 }
1013
1014 Value *VarExprAST::IRGen(IRGenContext &C) const {
1015 std::vector OldBindings;
1016
1017 Function *TheFunction = C.getBuilder().GetInsertBlock()->getParent();
1018
1019 // Register all variables and emit their initializer.
1020 for (unsigned i = 0, e = VarBindings.size(); i != e; ++i) {
1021 auto &VarName = VarBindings[i].first;
1022 auto &Init = VarBindings[i].second;
1023
1024 // Emit the initializer before adding the variable to scope, this prevents
1025 // the initializer from referencing the variable itself, and permits stuff
1026 // like this:
1027 // var a = 1 in
1028 // var a = a in ... # refers to outer 'a'.
1029 Value *InitVal;
1030 if (Init) {
1031 InitVal = Init->IRGen(C);
1032 if (!InitVal) return nullptr;
1033 } else // If not specified, use 0.0.
1034 InitVal = ConstantFP::get(C.getLLVMContext(), APFloat(0.0));
1035
1036 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1037 C.getBuilder().CreateStore(InitVal, Alloca);
1038
1039 // Remember the old variable binding so that we can restore the binding when
1040 // we unrecurse.
1041 OldBindings.push_back(C.NamedValues[VarName]);
1042
1043 // Remember this binding.
1044 C.NamedValues[VarName] = Alloca;
1045 }
1046
1047 // Codegen the body, now that all vars are in scope.
1048 Value *BodyVal = Body->IRGen(C);
1049 if (!BodyVal) return nullptr;
1050
1051 // Pop all our variables from scope.
1052 for (unsigned i = 0, e = VarBindings.size(); i != e; ++i)
1053 C.NamedValues[VarBindings[i].first] = OldBindings[i];
1054
1055 // Return the body computation.
1056 return BodyVal;
1057 }
1058
1059 Function *PrototypeAST::IRGen(IRGenContext &C) const {
1060 std::string FnName = MakeLegalFunctionName(Name);
1061
1062 // Make the function type: double(double,double) etc.
1063 std::vector Doubles(Args.size(),
1064 Type::getDoubleTy(C.getLLVMContext()));
1065 FunctionType *FT =
1066 FunctionType::get(Type::getDoubleTy(C.getLLVMContext()), Doubles, false);
1067 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName,
1068 &C.getM());
1069
1070 // If F conflicted, there was already something named 'FnName'. If it has a
1071 // body, don't allow redefinition or reextern.
1072 if (F->getName() != FnName) {
1073 // Delete the one we just made and get the existing one.
1074 F->eraseFromParent();
1075 F = C.getM().getFunction(Name);
1076
1077 // If F already has a body, reject this.
1078 if (!F->empty()) {
1079 ErrorP("redefinition of function");
1080 return nullptr;
1081 }
1082
1083 // If F took a different number of args, reject.
1084 if (F->arg_size() != Args.size()) {
1085 ErrorP("redefinition of function with different # args");
1086 return nullptr;
1087 }
1088 }
1089
1090 // Set names for all arguments.
1091 unsigned Idx = 0;
1092 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1093 ++AI, ++Idx)
1094 AI->setName(Args[Idx]);
1095
1096 return F;
1097 }
1098
1099 /// CreateArgumentAllocas - Create an alloca for each argument and register the
1100 /// argument in the symbol table so that references to it will succeed.
1101 void PrototypeAST::CreateArgumentAllocas(Function *F, IRGenContext &C) {
1102 Function::arg_iterator AI = F->arg_begin();
1103 for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
1104 // Create an alloca for this variable.
1105 AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
1106
1107 // Store the initial value into the alloca.
1108 C.getBuilder().CreateStore(&*AI, Alloca);
1109
1110 // Add arguments to variable symbol table.
1111 C.NamedValues[Args[Idx]] = Alloca;
1112 }
1113 }
1114
1115 Function *FunctionAST::IRGen(IRGenContext &C) const {
1116 C.NamedValues.clear();
1117
1118 Function *TheFunction = Proto->IRGen(C);
1119 if (!TheFunction)
1120 return nullptr;
1121
1122 // If this is an operator, install it.
1123 if (Proto->isBinaryOp())
1124 BinopPrecedence[Proto->getOperatorName()] = Proto->Precedence;
1125
1126 // Create a new basic block to start insertion into.
1127 BasicBlock *BB = BasicBlock::Create(C.getLLVMContext(), "entry", TheFunction);
1128 C.getBuilder().SetInsertPoint(BB);
1129
1130 // Add all arguments to the symbol table and create their allocas.
1131 Proto->CreateArgumentAllocas(TheFunction, C);
1132
1133 if (Value *RetVal = Body->IRGen(C)) {
1134 // Finish off the function.
1135 C.getBuilder().CreateRet(RetVal);
1136
1137 // Validate the generated code, checking for consistency.
1138 verifyFunction(*TheFunction);
1139
1140 return TheFunction;
1141 }
1142
1143 // Error reading body, remove function.
1144 TheFunction->eraseFromParent();
1145
1146 if (Proto->isBinaryOp())
1147 BinopPrecedence.erase(Proto->getOperatorName());
1148 return nullptr;
1149 }
1150
1151 //===----------------------------------------------------------------------===//
1152 // Top-Level parsing and JIT Driver
1153 //===----------------------------------------------------------------------===//
1154
1155 static std::unique_ptr IRGen(SessionContext &S,
1156 const FunctionAST &F) {
1157 IRGenContext C(S);
1158 auto LF = F.IRGen(C);
1159 if (!LF)
1160 return nullptr;
1161 #ifndef MINIMAL_STDERR_OUTPUT
1162 fprintf(stderr, "Read function definition:");
1163 LF->dump();
1164 #endif
1165 return C.takeM();
1166 }
1167
1168 template
1169 static std::vector singletonSet(T t) {
1170 std::vector Vec;
1171 Vec.push_back(std::move(t));
1172 return Vec;
1173 }
1174
1175 static void EarthShatteringKaboom() {
1176 fprintf(stderr, "Earth shattering kaboom.");
1177 exit(1);
1178 }
1179
1180 class KaleidoscopeJIT {
1181 public:
1182 typedef ObjectLinkingLayer<> ObjLayerT;
1183 typedef IRCompileLayer CompileLayerT;
1184 typedef LazyEmittingLayer LazyEmitLayerT;
1185 typedef LazyEmitLayerT::ModuleSetHandleT ModuleHandleT;
1186
1187 KaleidoscopeJIT(SessionContext &Session)
1188 : Session(Session),
1189 CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())),
1190 LazyEmitLayer(CompileLayer),
1191 CompileCallbacks(reinterpret_cast(EarthShatteringKaboom)) {}
1192
1193 std::string mangle(const std::string &Name) {
1194 std::string MangledName;
1195 {
1196 raw_string_ostream MangledNameStream(MangledName);
1197 Mangler::getNameWithPrefix(MangledNameStream, Name,
1198 Session.getTarget().createDataLayout());
1199 }
1200 return MangledName;
1201 }
1202
1203 void addFunctionAST(std::unique_ptr FnAST) {
1204 std::cerr << "Adding AST: " << FnAST->Proto->Name << "\n";
1205 FunctionDefs[mangle(FnAST->Proto->Name)] = std::move(FnAST);
1206 }
1207
1208 ModuleHandleT addModule(std::unique_ptr M) {
1209 // We need a memory manager to allocate memory and resolve symbols for this
1210 // new module. Create one that resolves symbols by looking back into the
1211 // JIT.
1212 auto Resolver = createLambdaResolver(
1213 [&](const std::string &Name) {
1214 // First try to find 'Name' within the JIT.
1215 if (auto Symbol = findSymbol(Name))
1216 return RuntimeDyld::SymbolInfo(Symbol.getAddress(),
1217 Symbol.getFlags());
1218
1219 // If we don't already have a definition of 'Name' then search
1220 // the ASTs.
1221 return searchFunctionASTs(Name);
1222 },
1223 [](const std::string &S) { return nullptr; } );
1224
1225 return LazyEmitLayer.addModuleSet(singletonSet(std::move(M)),
1226 make_unique(),
1227 std::move(Resolver));
1228 }
1229
1230 void removeModule(ModuleHandleT H) { LazyEmitLayer.removeModuleSet(H); }
1231
1232 JITSymbol findSymbol(const std::string &Name) {
1233 return LazyEmitLayer.findSymbol(Name, false);
1234 }
1235
1236 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name) {
1237 return LazyEmitLayer.findSymbolIn(H, Name, false);
1238 }
1239
1240 JITSymbol findUnmangledSymbol(const std::string &Name) {
1241 return findSymbol(mangle(Name));
1242 }
1243
1244 JITSymbol findUnmangledSymbolIn(ModuleHandleT H, const std::string &Name) {
1245 return findSymbolIn(H, mangle(Name));
1246 }
1247
1248 private:
1249 // This method searches the FunctionDefs map for a definition of 'Name'. If it
1250 // finds one it generates a stub for it and returns the address of the stub.
1251 RuntimeDyld::SymbolInfo searchFunctionASTs(const std::string &Name) {
1252 auto DefI = FunctionDefs.find(Name);
1253 if (DefI == FunctionDefs.end())
1254 return nullptr;
1255
1256 // Return the address of the stub.
1257 // Take the FunctionAST out of the map.
1258 auto FnAST = std::move(DefI->second);
1259 FunctionDefs.erase(DefI);
1260
1261 // IRGen the AST, add it to the JIT, and return the address for it.
1262 auto H = irGenStub(std::move(FnAST));
1263 auto Sym = findSymbolIn(H, Name);
1264 return RuntimeDyld::SymbolInfo(Sym.getAddress(), Sym.getFlags());
1265 }
1266
1267 // This method will take the AST for a function definition and IR-gen a stub
1268 // for that function that will, on first call, IR-gen the actual body of the
1269 // function.
1270 ModuleHandleT irGenStub(std::unique_ptr FnAST) {
1271 // Step 1) IRGen a prototype for the stub. This will have the same type as
1272 // the function.
1273 IRGenContext C(Session);
1274 Function *F = FnAST->Proto->IRGen(C);
1275
1276 // Step 2) Get a compile callback that can be used to compile the body of
1277 // the function. The resulting CallbackInfo type will let us set the
1278 // compile and update actions for the callback, and get a pointer to
1279 // the jit trampoline that we need to call to trigger those actions.
1280 auto CallbackInfo = CompileCallbacks.getCompileCallback();
1281
1282 // Step 3) Create a stub that will indirectly call the body of this
1283 // function once it is compiled. Initially, set the function
1284 // pointer for the indirection to point at the trampoline.
1285 std::string BodyPtrName = (F->getName() + "$address").str();
1286 GlobalVariable *FunctionBodyPointer =
1287 createImplPointer(*F->getType(), *F->getParent(), BodyPtrName,
1288 createIRTypedAddress(*F->getFunctionType(),
1289 CallbackInfo.getAddress()));
1290 makeStub(*F, *FunctionBodyPointer);
1291
1292 // Step 4) Add the module containing the stub to the JIT.
1293 auto StubH = addModule(C.takeM());
1294
1295 // Step 5) Set the compile and update actions.
1296 //
1297 // The compile action will IRGen the function and add it to the JIT, then
1298 // request its address, which will trigger codegen. Since we don't need the
1299 // AST after this, we pass ownership of the AST into the compile action:
1300 // compile actions (and update actions) are deleted after they're run, so
1301 // this will free the AST for us.
1302 //
1303 // The update action will update FunctionBodyPointer to point at the newly
1304 // compiled function.
1305 std::shared_ptr Fn = std::move(FnAST);
1306 CallbackInfo.setCompileAction([this, Fn, BodyPtrName, StubH]() {
1307 auto H = addModule(IRGen(Session, *Fn));
1308 auto BodySym = findUnmangledSymbolIn(H, Fn->Proto->Name);
1309 auto BodyPtrSym = findUnmangledSymbolIn(StubH, BodyPtrName);
1310 assert(BodySym && "Missing function body.");
1311 assert(BodyPtrSym && "Missing function pointer.");
1312 auto BodyAddr = BodySym.getAddress();
1313 auto BodyPtr = reinterpret_cast(
1314 static_cast(BodyPtrSym.getAddress()));
1315 memcpy(BodyPtr, &BodyAddr, sizeof(uintptr_t));
1316 return BodyAddr;
1317 });
1318
1319 return StubH;
1320 }
1321
1322 SessionContext &Session;
1323 SectionMemoryManager CCMgrMemMgr;
1324 ObjLayerT ObjectLayer;
1325 CompileLayerT CompileLayer;
1326 LazyEmitLayerT LazyEmitLayer;
1327
1328 std::map> FunctionDefs;
1329
1330 LocalJITCompileCallbackManager CompileCallbacks;
1331 };
1332
1333 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
1334 if (auto F = ParseDefinition()) {
1335 S.addPrototypeAST(llvm::make_unique(*F->Proto));
1336 J.addFunctionAST(std::move(F));
1337 } else {
1338 // Skip token for error recovery.
1339 getNextToken();
1340 }
1341 }
1342
1343 static void HandleExtern(SessionContext &S) {
1344 if (auto P = ParseExtern())
1345 S.addPrototypeAST(std::move(P));
1346 else {
1347 // Skip token for error recovery.
1348 getNextToken();
1349 }
1350 }
1351
1352 static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
1353 // Evaluate a top-level expression into an anonymous function.
1354 if (auto F = ParseTopLevelExpr()) {
1355 IRGenContext C(S);
1356 if (auto ExprFunc = F->IRGen(C)) {
1357 #ifndef MINIMAL_STDERR_OUTPUT
1358 std::cerr << "Expression function:\n";
1359 ExprFunc->dump();
1360 #endif
1361 // Add the CodeGen'd module to the JIT. Keep a handle to it: We can remove
1362 // this module as soon as we've executed Function ExprFunc.
1363 auto H = J.addModule(C.takeM());
1364
1365 // Get the address of the JIT'd function in memory.
1366 auto ExprSymbol = J.findUnmangledSymbol("__anon_expr");
1367
1368 // Cast it to the right type (takes no arguments, returns a double) so we
1369 // can call it as a native function.
1370 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
1371 #ifdef MINIMAL_STDERR_OUTPUT
1372 FP();
1373 #else
1374 std::cerr << "Evaluated to " << FP() << "\n";
1375 #endif
1376
1377 // Remove the function.
1378 J.removeModule(H);
1379 }
1380 } else {
1381 // Skip token for error recovery.
1382 getNextToken();
1383 }
1384 }
1385
1386 /// top ::= definition | external | expression | ';'
1387 static void MainLoop() {
1388 LLVMContext TheContext;
1389 SessionContext S(TheContext);
1390 KaleidoscopeJIT J(S);
1391
1392 while (true) {
1393 switch (CurTok) {
1394 case tok_eof: return;
1395 case ';': getNextToken(); continue; // ignore top-level semicolons.
1396 case tok_def: HandleDefinition(S, J); break;
1397 case tok_extern: HandleExtern(S); break;
1398 default: HandleTopLevelExpression(S, J); break;
1399 }
1400 #ifndef MINIMAL_STDERR_OUTPUT
1401 std::cerr << "ready> ";
1402 #endif
1403 }
1404 }
1405
1406 //===----------------------------------------------------------------------===//
1407 // "Library" functions that can be "extern'd" from user code.
1408 //===----------------------------------------------------------------------===//
1409
1410 /// putchard - putchar that takes a double and returns 0.
1411 extern "C"
1412 double putchard(double X) {
1413 putchar((char)X);
1414 return 0;
1415 }
1416
1417 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1418 extern "C"
1419 double printd(double X) {
1420 printf("%f", X);
1421 return 0;
1422 }
1423
1424 extern "C"
1425 double printlf() {
1426 printf("\n");
1427 return 0;
1428 }
1429
1430 //===----------------------------------------------------------------------===//
1431 // Main driver code.
1432 //===----------------------------------------------------------------------===//
1433
1434 int main() {
1435 InitializeNativeTarget();
1436 InitializeNativeTargetAsmPrinter();
1437 InitializeNativeTargetAsmParser();
1438
1439 // Install standard binary operators.
1440 // 1 is lowest precedence.
1441 BinopPrecedence['='] = 2;
1442 BinopPrecedence['<'] = 10;
1443 BinopPrecedence['+'] = 20;
1444 BinopPrecedence['-'] = 20;
1445 BinopPrecedence['/'] = 40;
1446 BinopPrecedence['*'] = 40; // highest.
1447
1448 // Prime the first token.
1449 #ifndef MINIMAL_STDERR_OUTPUT
1450 std::cerr << "ready> ";
1451 #endif
1452 getNextToken();
1453
1454 std::cerr << std::fixed;
1455
1456 // Run the main "interpreter loop" now.
1457 MainLoop();
1458
1459 return 0;
1460 }
+0
-12
examples/Kaleidoscope/Orc/initial/CMakeLists.txt less more
None set(LLVM_LINK_COMPONENTS
1 Core
2 ExecutionEngine
3 Object
4 RuntimeDyld
5 Support
6 native
7 )
8
9 add_kaleidoscope_chapter(Kaleidoscope-Orc-initial
10 toy.cpp
11 )
+0
-13
examples/Kaleidoscope/Orc/initial/README.txt less more
None //===----------------------------------------------------------------------===/
1 // Kaleidoscope with Orc - Initial Version
2 //===----------------------------------------------------------------------===//
3
4 This version of Kaleidoscope with Orc demonstrates fully eager compilation. When
5 a function definition or top-level expression is entered it is immediately
6 translated (IRGen'd) to LLVM IR and added to the JIT, where it is code-gen'd to
7 native code and either stored (for function definitions) or executed (for
8 top-level expressions).
9
10 This directory contain a Makefile that allow the code to be built in a
11 standalone manner, independent of the larger LLVM build infrastructure. To build
12 the program you will need to have 'clang++' and 'llvm-config' in your path.
+0
-1356
examples/Kaleidoscope/Orc/initial/toy.cpp less more
None #include "llvm/ADT/APFloat.h"
1 #include "llvm/ADT/STLExtras.h"
2 #include "llvm/ExecutionEngine/ExecutionEngine.h"
3 #include "llvm/ExecutionEngine/RuntimeDyld.h"
4 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
5 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
6 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
7 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
8 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
9 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
10 #include "llvm/IR/BasicBlock.h"
11 #include "llvm/IR/Constant.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/DerivedTypes.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/IR/Verifier.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/TargetSelect.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include
27 #include
28 #include
29 #include
30 #include
31 #include
32 #include
33 #include
34 #include
35 #include
36 #include
37 #include
38 #include
39
40 using namespace llvm;
41 using namespace llvm::orc;
42
43 //===----------------------------------------------------------------------===//
44 // Lexer
45 //===----------------------------------------------------------------------===//
46
47 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
48 // of these for known things.
49 enum Token {
50 tok_eof = -1,
51
52 // commands
53 tok_def = -2, tok_extern = -3,
54
55 // primary
56 tok_identifier = -4, tok_number = -5,
57
58 // control
59 tok_if = -6, tok_then = -7, tok_else = -8,
60 tok_for = -9, tok_in = -10,
61
62 // operators
63 tok_binary = -11, tok_unary = -12,
64
65 // var definition
66 tok_var = -13
67 };
68
69 static std::string IdentifierStr; // Filled in if tok_identifier
70 static double NumVal; // Filled in if tok_number
71
72 /// gettok - Return the next token from standard input.
73 static int gettok() {
74 static int LastChar = ' ';
75
76 // Skip any whitespace.
77 while (isspace(LastChar))
78 LastChar = getchar();
79
80 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
81 IdentifierStr = LastChar;
82 while (isalnum((LastChar = getchar())))
83 IdentifierStr += LastChar;
84
85 if (IdentifierStr == "def") return tok_def;
86 if (IdentifierStr == "extern") return tok_extern;
87 if (IdentifierStr == "if") return tok_if;
88 if (IdentifierStr == "then") return tok_then;
89 if (IdentifierStr == "else") return tok_else;
90 if (IdentifierStr == "for") return tok_for;
91 if (IdentifierStr == "in") return tok_in;
92 if (IdentifierStr == "binary") return tok_binary;
93 if (IdentifierStr == "unary") return tok_unary;
94 if (IdentifierStr == "var") return tok_var;
95 return tok_identifier;
96 }
97
98 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
99 std::string NumStr;
100 do {
101 NumStr += LastChar;
102 LastChar = getchar();
103 } while (isdigit(LastChar) || LastChar == '.');
104
105 NumVal = strtod(NumStr.c_str(), nullptr);
106 return tok_number;
107 }
108
109 if (LastChar == '#') {
110 // Comment until end of line.
111 do LastChar = getchar();
112 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
113
114 if (LastChar != EOF)
115 return gettok();
116 }
117
118 // Check for end of file. Don't eat the EOF.
119 if (LastChar == EOF)
120 return tok_eof;
121
122 // Otherwise, just return the character as its ascii value.
123 int ThisChar = LastChar;
124 LastChar = getchar();
125 return ThisChar;
126 }
127
128 //===----------------------------------------------------------------------===//
129 // Abstract Syntax Tree (aka Parse Tree)
130 //===----------------------------------------------------------------------===//
131
132 class IRGenContext;
133
134 /// ExprAST - Base class for all expression nodes.
135 struct ExprAST {
136 virtual ~ExprAST() {}
137 virtual Value *IRGen(IRGenContext &C) const = 0;
138 };
139
140 /// NumberExprAST - Expression class for numeric literals like "1.0".
141 struct NumberExprAST : public ExprAST {
142 NumberExprAST(double Val) : Val(Val) {}
143 Value *IRGen(IRGenContext &C) const override;
144
145 double Val;
146 };
147
148 /// VariableExprAST - Expression class for referencing a variable, like "a".
149 struct VariableExprAST : public ExprAST {
150 VariableExprAST(std::string Name) : Name(std::move(Name)) {}
151 Value *IRGen(IRGenContext &C) const override;
152
153 std::string Name;
154 };
155
156 /// UnaryExprAST - Expression class for a unary operator.
157 struct UnaryExprAST : public ExprAST {
158 UnaryExprAST(char Opcode, std::unique_ptr Operand)
159 : Opcode(std::move(Opcode)), Operand(std::move(Operand)) {}
160
161 Value *IRGen(IRGenContext &C) const override;
162
163 char Opcode;
164 std::unique_ptr Operand;
165 };
166
167 /// BinaryExprAST - Expression class for a binary operator.
168 struct BinaryExprAST : public ExprAST {
169 BinaryExprAST(char Op, std::unique_ptr LHS,
170 std::unique_ptr RHS)
171 : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
172
173 Value *IRGen(IRGenContext &C) const override;
174
175 char Op;
176 std::unique_ptr LHS, RHS;
177 };
178
179 /// CallExprAST - Expression class for function calls.
180 struct CallExprAST : public ExprAST {
181 CallExprAST(std::string CalleeName,
182 std::vector> Args)
183 : CalleeName(std::move(CalleeName)), Args(std::move(Args)) {}
184
185 Value *IRGen(IRGenContext &C) const override;
186
187 std::string CalleeName;
188 std::vector> Args;
189 };
190
191 /// IfExprAST - Expression class for if/then/else.
192 struct IfExprAST : public ExprAST {
193 IfExprAST(std::unique_ptr Cond, std::unique_ptr Then,
194 std::unique_ptr Else)
195 : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
196
197 Value *IRGen(IRGenContext &C) const override;
198
199 std::unique_ptr Cond, Then, Else;
200 };
201
202 /// ForExprAST - Expression class for for/in.
203 struct ForExprAST : public ExprAST {
204 ForExprAST(std::string VarName, std::unique_ptr Start,
205 std::unique_ptr End, std::unique_ptr Step,
206 std::unique_ptr Body)
207 : VarName(std::move(VarName)), Start(std::move(Start)), End(std::move(End)),
208 Step(std::move(Step)), Body(std::move(Body)) {}
209
210 Value *IRGen(IRGenContext &C) const override;
211
212 std::string VarName;
213 std::unique_ptr Start, End, Step, Body;
214 };
215
216 /// VarExprAST - Expression class for var/in
217 struct VarExprAST : public ExprAST {
218 typedef std::pair> Binding;
219 typedef std::vector BindingList;
220
221 VarExprAST(BindingList VarBindings, std::unique_ptr Body)
222 : VarBindings(std::move(VarBindings)), Body(std::move(Body)) {}
223
224 Value *IRGen(IRGenContext &C) const override;
225
226 BindingList VarBindings;
227 std::unique_ptr Body;
228 };
229
230 /// PrototypeAST - This class represents the "prototype" for a function,
231 /// which captures its argument names as well as if it is an operator.
232 struct PrototypeAST {
233 PrototypeAST(std::string Name, std::vector Args,
234 bool IsOperator = false, unsigned Precedence = 0)
235 : Name(std::move(Name)), Args(std::move(Args)), IsOperator(IsOperator),
236 Precedence(Precedence) {}
237
238 Function *IRGen(IRGenContext &C) const;
239 void CreateArgumentAllocas(Function *F, IRGenContext &C);
240
241 bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
242 bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
243
244 char getOperatorName() const {
245 assert(isUnaryOp() || isBinaryOp());
246 return Name[Name.size()-1];
247 }
248
249 std::string Name;
250 std::vector Args;
251 bool IsOperator;
252 unsigned Precedence; // Precedence if a binary op.
253 };
254
255 /// FunctionAST - This class represents a function definition itself.
256 struct FunctionAST {
257 FunctionAST(std::unique_ptr Proto,
258 std::unique_ptr Body)
259 : Proto(std::move(Proto)), Body(std::move(Body)) {}
260
261 Function *IRGen(IRGenContext &C) const;
262
263 std::unique_ptr Proto;
264 std::unique_ptr Body;
265 };
266
267 //===----------------------------------------------------------------------===//
268 // Parser
269 //===----------------------------------------------------------------------===//
270
271 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
272 /// token the parser is looking at. getNextToken reads another token from the
273 /// lexer and updates CurTok with its results.
274 static int CurTok;
275 static int getNextToken() {
276 return CurTok = gettok();
277 }
278
279 /// BinopPrecedence - This holds the precedence for each binary operator that is
280 /// defined.
281 static std::map BinopPrecedence;
282
283 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
284 static int GetTokPrecedence() {
285 if (!isascii(CurTok))
286 return -1;
287
288 // Make sure it's a declared binop.
289 int TokPrec = BinopPrecedence[CurTok];
290 if (TokPrec <= 0) return -1;
291 return TokPrec;
292 }
293
294 template
295 std::unique_ptr ErrorU(const std::string &Str) {
296 std::cerr << "Error: " << Str << "\n";
297 return nullptr;
298 }
299
300 template
301 T* ErrorP(const std::string &Str) {
302 std::cerr << "Error: " << Str << "\n";
303 return nullptr;
304 }
305
306 static std::unique_ptr ParseExpression();
307
308 /// identifierexpr
309 /// ::= identifier
310 /// ::= identifier '(' expression* ')'
311 static std::unique_ptr ParseIdentifierExpr() {
312 std::string IdName = IdentifierStr;
313
314 getNextToken(); // eat identifier.
315
316 if (CurTok != '(') // Simple variable ref.
317 return llvm::make_unique(IdName);
318
319 // Call.
320 getNextToken(); // eat (
321 std::vector> Args;
322 if (CurTok != ')') {
323 while (true) {
324 auto Arg = ParseExpression();
325 if (!Arg) return nullptr;
326 Args.push_back(std::move(Arg));
327
328 if (CurTok == ')') break;
329
330 if (CurTok != ',')
331 return ErrorU("Expected ')' or ',' in argument list");
332 getNextToken();
333 }
334 }
335
336 // Eat the ')'.
337 getNextToken();
338
339 return llvm::make_unique(IdName, std::move(Args));
340 }
341
342 /// numberexpr ::= number
343 static std::unique_ptr ParseNumberExpr() {
344 auto Result = llvm::make_unique(NumVal);
345 getNextToken(); // consume the number
346 return Result;
347 }
348
349 /// parenexpr ::= '(' expression ')'
350 static std::unique_ptr ParseParenExpr() {
351 getNextToken(); // eat (.
352 auto V = ParseExpression();
353 if (!V)
354 return nullptr;
355
356 if (CurTok != ')')
357 return ErrorU("expected ')'");
358 getNextToken(); // eat ).
359 return V;
360 }
361
362 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
363 static std::unique_ptr ParseIfExpr() {
364 getNextToken(); // eat the if.
365
366 // condition.
367 auto Cond = ParseExpression();
368 if (!Cond)
369 return nullptr;
370
371 if (CurTok != tok_then)
372 return ErrorU("expected then");
373 getNextToken(); // eat the then
374
375 auto Then = ParseExpression();
376 if (!Then)
377 return nullptr;
378
379 if (CurTok != tok_else)
380 return ErrorU("expected else");
381
382 getNextToken();
383
384 auto Else = ParseExpression();
385 if (!Else)
386 return nullptr;
387
388 return llvm::make_unique(std::move(Cond), std::move(Then),
389 std::move(Else));
390 }
391
392 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
393 static std::unique_ptr ParseForExpr() {
394 getNextToken(); // eat the for.
395
396 if (CurTok != tok_identifier)
397 return ErrorU("expected identifier after for");
398
399 std::string IdName = IdentifierStr;
400 getNextToken(); // eat identifier.
401
402 if (CurTok != '=')
403 return ErrorU("expected '=' after for");
404 getNextToken(); // eat '='.
405
406 auto Start = ParseExpression();
407 if (!Start)
408 return nullptr;
409 if (CurTok != ',')
410 return ErrorU("expected ',' after for start value");
411 getNextToken();
412
413 auto End = ParseExpression();
414 if (!End)
415 return nullptr;
416
417 // The step value is optional.
418 std::unique_ptr Step;
419 if (CurTok == ',') {
420 getNextToken();
421 Step = ParseExpression();
422 if (!Step)
423 return nullptr;
424 }
425
426 if (CurTok != tok_in)
427 return ErrorU("expected 'in' after for");
428 getNextToken(); // eat 'in'.
429
430 auto Body = ParseExpression();
431 if (Body)
432 return nullptr;
433
434 return llvm::make_unique(IdName, std::move(Start), std::move(End),
435 std::move(Step), std::move(Body));
436 }
437
438 /// varexpr ::= 'var' identifier ('=' expression)?
439 // (',' identifier ('=' expression)?)* 'in' expression
440 static std::unique_ptr ParseVarExpr() {
441 getNextToken(); // eat the var.
442
443 VarExprAST::BindingList VarBindings;
444
445 // At least one variable name is required.
446 if (CurTok != tok_identifier)
447 return ErrorU("expected identifier after var");
448
449 while (true) {
450 std::string Name = IdentifierStr;
451 getNextToken(); // eat identifier.
452
453 // Read the optional initializer.
454 std::unique_ptr Init;
455 if (CurTok == '=') {
456 getNextToken(); // eat the '='.
457
458 Init = ParseExpression();
459 if (!Init)
460 return nullptr;
461 }
462
463 VarBindings.push_back(VarExprAST::Binding(Name, std::move(Init)));
464
465 // End of var list, exit loop.
466 if (CurTok != ',') break;
467 getNextToken(); // eat the ','.
468
469 if (CurTok != tok_identifier)
470 return ErrorU("expected identifier list after var");
471 }
472
473 // At this point, we have to have 'in'.
474 if (CurTok != tok_in)
475 return ErrorU("expected 'in' keyword after 'var'");
476 getNextToken(); // eat 'in'.
477
478 auto Body = ParseExpression();
479 if (!Body)
480 return nullptr;
481
482 return llvm::make_unique(std::move(VarBindings), std::move(Body));
483 }
484
485 /// primary
486 /// ::= identifierexpr
487 /// ::= numberexpr
488 /// ::= parenexpr
489 /// ::= ifexpr
490 /// ::= forexpr
491 /// ::= varexpr
492 static std::unique_ptr ParsePrimary() {
493 switch (CurTok) {
494 default: return ErrorU("unknown token when expecting an expression");
495 case tok_identifier: return ParseIdentifierExpr();
496 case tok_number: return ParseNumberExpr();
497 case '(': return ParseParenExpr();
498 case tok_if: return ParseIfExpr();
499 case tok_for: return ParseForExpr();
500 case tok_var: return ParseVarExpr();
501 }
502 }
503
504 /// unary
505 /// ::= primary
506 /// ::= '!' unary
507 static std::unique_ptr ParseUnary() {
508 // If the current token is not an operator, it must be a primary expr.
509 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
510 return ParsePrimary();
511
512 // If this is a unary operator, read it.
513 int Opc = CurTok;
514 getNextToken();
515 if (auto Operand = ParseUnary())
516 return llvm::make_unique(Opc, std::move(Operand));
517 return nullptr;
518 }
519
520 /// binoprhs
521 /// ::= ('+' unary)*
522 static std::unique_ptr ParseBinOpRHS(int ExprPrec,
523 std::unique_ptr LHS) {
524 // If this is a binop, find its precedence.
525 while (true) {
526 int TokPrec = GetTokPrecedence();
527
528 // If this is a binop that binds at least as tightly as the current binop,
529 // consume it, otherwise we are done.
530 if (TokPrec < ExprPrec)
531 return LHS;
532
533 // Okay, we know this is a binop.
534 int BinOp = CurTok;
535 getNextToken(); // eat binop
536
537 // Parse the unary expression after the binary operator.
538 auto RHS = ParseUnary();
539 if (!RHS)
540 return nullptr;
541
542 // If BinOp binds less tightly with RHS than the operator after RHS, let
543 // the pending operator take RHS as its LHS.
544 int NextPrec = GetTokPrecedence();
545 if (TokPrec < NextPrec) {
546 RHS = ParseBinOpRHS(TokPrec+1, std::move(RHS));
547 if (!RHS)
548 return nullptr;
549 }
550
551 // Merge LHS/RHS.
552 LHS = llvm::make_unique(BinOp, std::move(LHS), std::move(RHS));
553 }
554 }
555
556 /// expression
557 /// ::= unary binoprhs
558 ///
559 static std::unique_ptr ParseExpression() {
560 auto LHS = ParseUnary();
561 if (!LHS)
562 return nullptr;
563
564 return ParseBinOpRHS(0, std::move(LHS));
565 }
566
567 /// prototype
568 /// ::= id '(' id* ')'
569 /// ::= binary LETTER number? (id, id)
570 /// ::= unary LETTER (id)
571 static std::unique_ptr ParsePrototype() {
572 std::string FnName;
573
574 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
575 unsigned BinaryPrecedence = 30;
576
577 switch (CurTok) {
578 default:
579 return ErrorU("Expected function name in prototype");
580 case tok_identifier:
581 FnName = IdentifierStr;
582 Kind = 0;
583 getNextToken();
584 break;
585 case tok_unary:
586 getNextToken();
587 if (!isascii(CurTok))
588 return ErrorU("Expected unary operator");
589 FnName = "unary";
590 FnName += (char)CurTok;
591 Kind = 1;
592 getNextToken();
593 break;
594 case tok_binary:
595 getNextToken();
596 if (!isascii(CurTok))
597 return ErrorU("Expected binary operator");
598 FnName = "binary";
599 FnName += (char)CurTok;
600 Kind = 2;
601 getNextToken();
602
603 // Read the precedence if present.
604 if (CurTok == tok_number) {
605 if (NumVal < 1 || NumVal > 100)
606 return ErrorU("Invalid precedecnce: must be 1..100");
607 BinaryPrecedence = (unsigned)NumVal;
608 getNextToken();
609 }
610 break;
611 }
612
613 if (CurTok != '(')
614 return ErrorU("Expected '(' in prototype");
615
616 std::vector ArgNames;
617 while (getNextToken() == tok_identifier)
618 ArgNames.push_back(IdentifierStr);
619 if (CurTok != ')')
620 return ErrorU("Expected ')' in prototype");
621
622 // success.
623 getNextToken(); // eat ')'.
624
625 // Verify right number of names for operator.
626 if (Kind && ArgNames.size() != Kind)
627 return ErrorU("Invalid number of operands for operator");
628
629 return llvm::make_unique(FnName, std::move(ArgNames), Kind != 0,
630 BinaryPrecedence);
631 }
632
633 /// definition ::= 'def' prototype expression
634 static std::unique_ptr ParseDefinition() {
635 getNextToken(); // eat def.
636 auto Proto = ParsePrototype();
637 if (!Proto)
638 return nullptr;
639
640 if (auto Body = ParseExpression())
641 return llvm::make_unique(std::move(Proto), std::move(Body));
642 return nullptr;
643 }
644
645 /// toplevelexpr ::= expression
646 static std::unique_ptr ParseTopLevelExpr() {
647 if (auto E = ParseExpression()) {
648 // Make an anonymous proto.
649 auto Proto =
650 llvm::make_unique("__anon_expr", std::vector());
651 return llvm::make_unique(std::move(Proto), std::move(E));
652 }
653 return nullptr;
654 }
655
656 /// external ::= 'extern' prototype
657 static std::unique_ptr ParseExtern() {
658 getNextToken(); // eat extern.
659 return ParsePrototype();
660 }
661
662 //===----------------------------------------------------------------------===//
663 // Code Generation
664 //===----------------------------------------------------------------------===//
665
666 // FIXME: Obviously we can do better than this
667 std::string GenerateUniqueName(const std::string &Root) {
668 static int i = 0;
669 std::ostringstream NameStream;
670 NameStream << Root << ++i;
671 return NameStream.str();
672 }
673
674 std::string MakeLegalFunctionName(std::string Name)
675 {
676 std::string NewName;
677 assert(!Name.empty() && "Base name must not be empty");
678
679 // Start with what we have
680 NewName = Name;
681
682 // Look for a numberic first character
683 if (NewName.find_first_of("0123456789") == 0) {
684 NewName.insert(0, 1, 'n');
685 }
686
687 // Replace illegal characters with their ASCII equivalent
688 std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
689 size_t pos;
690 while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
691 std::ostringstream NumStream;
692 NumStream << (int)NewName.at(pos);
693 NewName = NewName.replace(pos, 1, NumStream.str());
694 }
695
696 return NewName;
697 }
698
699 class SessionContext {
700 public:
701 SessionContext(LLVMContext &C)
702 : Context(C), TM(EngineBuilder().selectTarget()) {}
703 LLVMContext& getLLVMContext() const { return Context; }
704 TargetMachine& getTarget() { return *TM; }
705 void addPrototypeAST(std::unique_ptr P);
706 PrototypeAST* getPrototypeAST(const std::string &Name);
707
708 private:
709 typedef std::map> PrototypeMap;
710
711 LLVMContext &Context;
712 std::unique_ptr TM;
713
714 PrototypeMap Prototypes;
715 };
716
717 void SessionContext::addPrototypeAST(std::unique_ptr P) {
718 Prototypes[P->Name] = std::move(P);
719 }
720
721 PrototypeAST* SessionContext::getPrototypeAST(const std::string &Name) {
722 PrototypeMap::iterator I = Prototypes.find(Name);
723 if (I != Prototypes.end())
724 return I->second.get();
725 return nullptr;
726 }
727
728 class IRGenContext {
729 public:
730 IRGenContext(SessionContext &S)
731 : Session(S),
732 M(new Module(GenerateUniqueName("jit_module_"),
733 Session.getLLVMContext())),
734 Builder(Session.getLLVMContext()) {
735 M->setDataLayout(Session.getTarget().createDataLayout());
736 }
737
738 SessionContext& getSession() { return Session; }
739 Module& getM() const { return *M; }
740 std::unique_ptr takeM() { return std::move(M); }
741 IRBuilder<>& getBuilder() { return Builder; }
742 LLVMContext& getLLVMContext() { return Session.getLLVMContext(); }
743 Function* getPrototype(const std::string &Name);
744
745 std::map NamedValues;
746
747 private:
748 SessionContext &Session;
749 std::unique_ptr M;
750 IRBuilder<> Builder;
751 };
752
753 Function* IRGenContext::getPrototype(const std::string &Name) {
754 if (Function *ExistingProto = M->getFunction(Name))
755 return ExistingProto;
756 if (PrototypeAST *ProtoAST = Session.getPrototypeAST(Name))
757 return ProtoAST->IRGen(*this);
758 return nullptr;
759 }
760
761 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
762 /// the function. This is used for mutable variables etc.
763 static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
764 const std::string &VarName) {
765 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
766 TheFunction->getEntryBlock().begin());
767 return TmpB.CreateAlloca(Type::getDoubleTy(TheFunction->getContext()),
768 nullptr, VarName);
769 }
770
771 Value *NumberExprAST::IRGen(IRGenContext &C) const {
772 return ConstantFP::get(C.getLLVMContext(), APFloat(Val));
773 }
774
775 Value *VariableExprAST::IRGen(IRGenContext &C) const {
776 // Look this variable up in the function.
777 Value *V = C.NamedValues[Name];
778
779 if (!V)
780 return ErrorP("Unknown variable name '" + Name + "'");
781
782 // Load the value.
783 return C.getBuilder().CreateLoad(V, Name.c_str());
784 }
785
786 Value *UnaryExprAST::IRGen(IRGenContext &C) const {
787 if (Value *OperandV = Operand->IRGen(C)) {
788 std::string FnName = MakeLegalFunctionName(std::string("unary")+Opcode);
789 if (Function *F = C.getPrototype(FnName))
790 return C.getBuilder().CreateCall(F, OperandV, "unop");
791 return ErrorP("Unknown unary operator");
792 }
793
794 // Could not codegen operand - return null.
795 return nullptr;
796 }
797
798 Value *BinaryExprAST::IRGen(IRGenContext &C) const {
799 // Special case '=' because we don't want to emit the LHS as an expression.
800 if (Op == '=') {
801 // Assignment requires the LHS to be an identifier.
802 auto &LHSVar = static_cast(*LHS);
803 // Codegen the RHS.
804 Value *Val = RHS->IRGen(C);
805 if (!Val) return nullptr;
806
807 // Look up the name.
808 if (auto Variable = C.NamedValues[LHSVar.Name]) {
809 C.getBuilder().CreateStore(Val, Variable);
810 return Val;
811 }
812 return ErrorP("Unknown variable name");
813 }
814
815 Value *L = LHS->IRGen(C);
816 Value *R = RHS->IRGen(C);
817 if (!L || !R) return nullptr;
818
819 switch (Op) {
820 case '+': return C.getBuilder().CreateFAdd(L, R, "addtmp");
821 case '-': return C.getBuilder().CreateFSub(L, R, "subtmp");
822 case '*': return C.getBuilder().CreateFMul(L, R, "multmp");
823 case '/': return C.getBuilder().CreateFDiv(L, R, "divtmp");
824 case '<':
825 L = C.getBuilder().CreateFCmpULT(L, R, "cmptmp");
826 // Convert bool 0/1 to double 0.0 or 1.0
827 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(C.getLLVMContext()),
828 "booltmp");
829 default: break;
830 }
831
832 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
833 // a call to it.
834 std::string FnName = MakeLegalFunctionName(std::string("binary")+Op);
835 if (Function *F = C.getPrototype(FnName)) {
836 Value *Ops[] = { L, R };
837 return C.getBuilder().CreateCall(F, Ops, "binop");
838 }
839
840 return ErrorP("Unknown binary operator");
841 }
842
843 Value *CallExprAST::IRGen(IRGenContext &C) const {
844 // Look up the name in the global module table.
845 if (auto CalleeF = C.getPrototype(CalleeName)) {
846 // If argument mismatch error.
847 if (CalleeF->arg_size() != Args.size())
848 return ErrorP("Incorrect # arguments passed");
849
850 std::vector ArgsV;
851 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
852 ArgsV.push_back(Args[i]->IRGen(C));
853 if (!ArgsV.back()) return nullptr;
854 }
855
856 return C.getBuilder().CreateCall(CalleeF, ArgsV, "calltmp");
857 }
858
859 return ErrorP("Unknown function referenced");
860 }
861
862 Value *IfExprAST::IRGen(IRGenContext &C) const {
863 Value *CondV = Cond->IRGen(C);
864 if (!CondV) return nullptr;
865
866 // Convert condition to a bool by comparing equal to 0.0.
867 ConstantFP *FPZero =
868 ConstantFP::get(C.getLLVMContext(), APFloat(0.0));
869 CondV = C.getBuilder().CreateFCmpONE(CondV, FPZero, "ifcond");
870
871 Function *TheFunction = C.getBuilder().GetInsertBlock()->getParent();
872
873 // Create blocks for the then and else cases. Insert the 'then' block at the
874 // end of the function.
875 BasicBlock *ThenBB = BasicBlock::Create(C.getLLVMContext(), "then", TheFunction);
876 BasicBlock *ElseBB = BasicBlock::Create(C.getLLVMContext(), "else");
877 BasicBlock *MergeBB = BasicBlock::Create(C.getLLVMContext(), "ifcont");
878
879 C.getBuilder().CreateCondBr(CondV, ThenBB, ElseBB);
880
881 // Emit then value.
882 C.getBuilder().SetInsertPoint(ThenBB);
883
884 Value *ThenV = Then->IRGen(C);
885 if (!ThenV) return nullptr;
886
887 C.getBuilder().CreateBr(MergeBB);
888 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
889 ThenBB = C.getBuilder().GetInsertBlock();
890
891 // Emit else block.
892 TheFunction->getBasicBlockList().push_back(ElseBB);
893 C.getBuilder().SetInsertPoint(ElseBB);
894
895 Value *ElseV = Else->IRGen(C);
896 if (!ElseV) return nullptr;
897
898 C.getBuilder().CreateBr(MergeBB);
899 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
900 ElseBB = C.getBuilder().GetInsertBlock();
901
902 // Emit merge block.
903 TheFunction->getBasicBlockList().push_back(MergeBB);
904 C.getBuilder().SetInsertPoint(MergeBB);
905 PHINode *PN = C.getBuilder().CreatePHI(Type::getDoubleTy(C.getLLVMContext()),
906 2, "iftmp");
907
908 PN->addIncoming(ThenV, ThenBB);
909 PN->addIncoming(ElseV, ElseBB);
910 return PN;
911 }
912
913 Value *ForExprAST::IRGen(IRGenContext &C) const {
914 // Output this as:
915 // var = alloca double
916 // ...
917 // start = startexpr
918 // store start -> var
919 // goto loop
920 // loop:
921 // ...
922 // bodyexpr
923 // ...
924 // loopend:
925 // step = stepexpr
926 // endcond = endexpr
927 //
928 // curvar = load var
929 // nextvar = curvar + step
930 // store nextvar -> var
931 // br endcond, loop, endloop
932 // outloop:
933
934 Function *TheFunction = C.getBuilder().GetInsertBlock()->getParent();
935
936 // Create an alloca for the variable in the entry block.
937 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
938
939 // Emit the start code first, without 'variable' in scope.
940 Value *StartVal = Start->IRGen(C);
941 if (!StartVal) return nullptr;
942
943 // Store the value into the alloca.
944 C.getBuilder().CreateStore(StartVal, Alloca);
945
946 // Make the new basic block for the loop header, inserting after current
947 // block.
948 BasicBlock *LoopBB =
949 BasicBlock::Create(C.getLLVMContext(), "loop", TheFunction);
950
951 // Insert an explicit fall through from the current block to the LoopBB.
952 C.getBuilder().CreateBr(LoopBB);
953
954 // Start insertion in LoopBB.
955 C.getBuilder().SetInsertPoint(LoopBB);
956
957 // Within the loop, the variable is defined equal to the PHI node. If it
958 // shadows an existing variable, we have to restore it, so save it now.
959 AllocaInst *OldVal = C.NamedValues[VarName];
960 C.NamedValues[VarName] = Alloca;
961
962 // Emit the body of the loop. This, like any other expr, can change the
963 // current BB. Note that we ignore the value computed by the body, but don't
964 // allow an error.
965 if (!Body->IRGen(C))
966 return nullptr;
967
968 // Emit the step value.
969 Value *StepVal;
970 if (Step) {
971 StepVal = Step->IRGen(C);
972 if (!StepVal) return nullptr;
973 } else {
974 // If not specified, use 1.0.
975 StepVal = ConstantFP::get(C.getLLVMContext(), APFloat(1.0));
976 }
977
978 // Compute the end condition.
979 Value *EndCond = End->IRGen(C);
980 if (!EndCond) return nullptr;
981
982 // Reload, increment, and restore the alloca. This handles the case where
983 // the body of the loop mutates the variable.
984 Value *CurVar = C.getBuilder().CreateLoad(Alloca, VarName.c_str());
985 Value *NextVar = C.getBuilder().CreateFAdd(CurVar, StepVal, "nextvar");
986 C.getBuilder().CreateStore(NextVar, Alloca);
987
988 // Convert condition to a bool by comparing equal to 0.0.
989 EndCond = C.getBuilder().CreateFCmpONE(
990 EndCond, ConstantFP::get(C.getLLVMContext(), APFloat(0.0)), "loopcond");
991
992 // Create the "after loop" block and insert it.
993 BasicBlock *AfterBB =
994 BasicBlock::Create(C.getLLVMContext(), "afterloop", TheFunction);
995
996 // Insert the conditional branch into the end of LoopEndBB.
997 C.getBuilder().CreateCondBr(EndCond, LoopBB, AfterBB);
998
999 // Any new code will be inserted in AfterBB.
1000 C.getBuilder().SetInsertPoint(AfterBB);
1001
1002 // Restore the unshadowed variable.
1003 if (OldVal)
1004 C.NamedValues[VarName] = OldVal;
1005 else
1006 C.NamedValues.erase(VarName);
1007
1008 // for expr always returns 0.0.
1009 return Constant::getNullValue(Type::getDoubleTy(C.getLLVMContext()));
1010 }
1011
1012 Value *VarExprAST::IRGen(IRGenContext &C) const {
1013 std::vector OldBindings;
1014
1015 Function *TheFunction = C.getBuilder().GetInsertBlock()->getParent();
1016
1017 // Register all variables and emit their initializer.
1018 for (unsigned i = 0, e = VarBindings.size(); i != e; ++i) {
1019 auto &VarName = VarBindings[i].first;
1020 auto &Init = VarBindings[i].second;
1021
1022 // Emit the initializer before adding the variable to scope, this prevents
1023 // the initializer from referencing the variable itself, and permits stuff
1024 // like this:
1025 // var a = 1 in
1026 // var a = a in ... # refers to outer 'a'.
1027 Value *InitVal;
1028 if (Init) {
1029 InitVal = Init->IRGen(C);
1030 if (!InitVal) return nullptr;
1031 } else // If not specified, use 0.0.
1032 InitVal = ConstantFP::get(C.getLLVMContext(), APFloat(0.0));
1033
1034 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1035 C.getBuilder().CreateStore(InitVal, Alloca);
1036
1037 // Remember the old variable binding so that we can restore the binding when
1038 // we unrecurse.
1039 OldBindings.push_back(C.NamedValues[VarName]);
1040
1041 // Remember this binding.
1042 C.NamedValues[VarName] = Alloca;
1043 }
1044
1045 // Codegen the body, now that all vars are in scope.
1046 Value *BodyVal = Body->IRGen(C);
1047 if (!BodyVal) return nullptr;
1048
1049 // Pop all our variables from scope.
1050 for (unsigned i = 0, e = VarBindings.size(); i != e; ++i)
1051 C.NamedValues[VarBindings[i].first] = OldBindings[i];
1052
1053 // Return the body computation.
1054 return BodyVal;
1055 }
1056
1057 Function *PrototypeAST::IRGen(IRGenContext &C) const {
1058 std::string FnName = MakeLegalFunctionName(Name);
1059
1060 // Make the function type: double(double,double) etc.
1061 std::vector Doubles(Args.size(),
1062 Type::getDoubleTy(C.getLLVMContext()));
1063 FunctionType *FT =
1064 FunctionType::get(Type::getDoubleTy(C.getLLVMContext()), Doubles, false);
1065 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName,
1066 &C.getM());
1067
1068 // If F conflicted, there was already something named 'FnName'. If it has a
1069 // body, don't allow redefinition or reextern.
1070 if (F->getName() != FnName) {
1071 // Delete the one we just made and get the existing one.
1072 F->eraseFromParent();
1073 F = C.getM().getFunction(Name);
1074
1075 // If F already has a body, reject this.
1076 if (!F->empty()) {
1077 ErrorP("redefinition of function");
1078 return nullptr;
1079 }
1080
1081 // If F took a different number of args, reject.
1082 if (F->arg_size() != Args.size()) {
1083 ErrorP("redefinition of function with different # args");
1084 return nullptr;
1085 }
1086 }
1087
1088 // Set names for all arguments.
1089 unsigned Idx = 0;
1090 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
1091 ++AI, ++Idx)
1092 AI->setName(Args[Idx]);
1093
1094 return F;
1095 }
1096
1097 /// CreateArgumentAllocas - Create an alloca for each argument and register the
1098 /// argument in the symbol table so that references to it will succeed.
1099 void PrototypeAST::CreateArgumentAllocas(Function *F, IRGenContext &C) {
1100 Function::arg_iterator AI = F->arg_begin();
1101 for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
1102 // Create an alloca for this variable.
1103 AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
1104
1105 // Store the initial value into the alloca.
1106 C.getBuilder().CreateStore(&*AI, Alloca);
1107
1108 // Add arguments to variable symbol table.
1109 C.NamedValues[Args[Idx]] = Alloca;
1110 }
1111 }
1112
1113 Function *FunctionAST::IRGen(IRGenContext &C) const {
1114 C.NamedValues.clear();
1115
1116 Function *TheFunction = Proto->IRGen(C);
1117 if (!TheFunction)
1118 return nullptr;
1119
1120 // If this is an operator, install it.
1121 if (Proto->isBinaryOp())
1122 BinopPrecedence[Proto->getOperatorName()] = Proto->Precedence;
1123
1124 // Create a new basic block to start insertion into.
1125 BasicBlock *BB = BasicBlock::Create(C.getLLVMContext(), "entry", TheFunction);
1126 C.getBuilder().SetInsertPoint(BB);
1127
1128 // Add all arguments to the symbol table and create their allocas.
1129 Proto->CreateArgumentAllocas(TheFunction, C);
1130
1131 if (Value *RetVal = Body->IRGen(C)) {
1132 // Finish off the function.
1133 C.getBuilder().CreateRet(RetVal);
1134
1135 // Validate the generated code, checking for consistency.
1136 verifyFunction(*TheFunction);
1137
1138 return TheFunction;
1139 }
1140
1141 // Error reading body, remove function.
1142 TheFunction->eraseFromParent();
1143
1144 if (Proto->isBinaryOp())
1145 BinopPrecedence.erase(Proto->getOperatorName());
1146 return nullptr;
1147 }
1148
1149 //===----------------------------------------------------------------------===//
1150 // Top-Level parsing and JIT Driver
1151 //===----------------------------------------------------------------------===//
1152
1153 static std::unique_ptr IRGen(SessionContext &S,
1154 const FunctionAST &F) {
1155 IRGenContext C(S);
1156 auto LF = F.IRGen(C);
1157 if (!LF)
1158 return nullptr;
1159 #ifndef MINIMAL_STDERR_OUTPUT
1160 fprintf(stderr, "Read function definition:");
1161 LF->dump();
1162 #endif
1163 return C.takeM();
1164 }
1165
1166 template
1167 static std::vector singletonSet(T t) {
1168 std::vector Vec;
1169 Vec.push_back(std::move(t));
1170 return Vec;
1171 }
1172
1173 class KaleidoscopeJIT {
1174 public:
1175 typedef ObjectLinkingLayer<> ObjLayerT;
1176 typedef IRCompileLayer CompileLayerT;
1177 typedef CompileLayerT::ModuleSetHandleT ModuleHandleT;
1178
1179 KaleidoscopeJIT(SessionContext &Session)
1180 : DL(Session.getTarget().createDataLayout()),
1181 CompileLayer(ObjectLayer, SimpleCompiler(Session.getTarget())) {}
1182
1183 std::string mangle(const std::string &Name) {
1184 std::string MangledName;
1185 {
1186 raw_string_ostream MangledNameStream(MangledName);
1187 Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
1188 }
1189 return MangledName;
1190 }
1191
1192 ModuleHandleT addModule(std::unique_ptr M) {
1193 // We need a memory manager to allocate memory and resolve symbols for this
1194 // new module. Create one that resolves symbols by looking back into the
1195 // JIT.
1196 auto Resolver = createLambdaResolver(
1197 [&](const std::string &Name) {
1198 if (auto Sym = findSymbol(Name))
1199 return RuntimeDyld::SymbolInfo(Sym.getAddress(),
1200 Sym.getFlags());
1201 return RuntimeDyld::SymbolInfo(nullptr);
1202 },
1203 [](const std::string &S) { return nullptr; }
1204 );
1205 return CompileLayer.addModuleSet(singletonSet(std::move(M)),
1206 make_unique(),
1207 std::move(Resolver));
1208 }
1209
1210 void removeModule(ModuleHandleT H) { CompileLayer.removeModuleSet(H); }
1211
1212 JITSymbol findSymbol(const std::string &Name) {
1213 return CompileLayer.findSymbol(Name, true);
1214 }
1215
1216 JITSymbol findUnmangledSymbol(const std::string Name) {
1217 return findSymbol(mangle(Name));
1218 }
1219
1220 private:
1221 const DataLayout DL;
1222 ObjLayerT ObjectLayer;
1223 CompileLayerT CompileLayer;
1224 };
1225
1226 static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) {
1227 if (auto F = ParseDefinition()) {
1228 if (auto M = IRGen(S, *F)) {
1229 S.addPrototypeAST(llvm::make_unique(*F->Proto));
1230 J.addModule(std::move(M));
1231 }
1232 } else {
1233 // Skip token for error recovery.
1234 getNextToken();
1235 }
1236 }
1237
1238 static void HandleExtern(SessionContext &S) {
1239 if (auto P = ParseExtern())
1240 S.addPrototypeAST(std::move(P));
1241 else {
1242 // Skip token for error recovery.
1243 getNextToken();
1244 }
1245 }
1246
1247 static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) {
1248 // Evaluate a top-level expression into an anonymous function.
1249 if (auto F = ParseTopLevelExpr()) {
1250 IRGenContext C(S);
1251 if (auto ExprFunc = F->IRGen(C)) {
1252 #ifndef MINIMAL_STDERR_OUTPUT
1253 std::cerr << "Expression function:\n";
1254 ExprFunc->dump();
1255 #endif
1256 // Add the CodeGen'd module to the JIT. Keep a handle to it: We can remove
1257 // this module as soon as we've executed Function ExprFunc.
1258 auto H = J.addModule(C.takeM());
1259
1260 // Get the address of the JIT'd function in memory.
1261 auto ExprSymbol = J.findUnmangledSymbol("__anon_expr");
1262
1263 // Cast it to the right type (takes no arguments, returns a double) so we
1264 // can call it as a native function.
1265 double (*FP)() = (double (*)())(intptr_t)ExprSymbol.getAddress();
1266 #ifdef MINIMAL_STDERR_OUTPUT
1267 FP();
1268 #else
1269 std::cerr << "Evaluated to " << FP() << "\n";
1270 #endif
1271
1272 // Remove the function.
1273 J.removeModule(H);
1274 }
1275 } else {
1276 // Skip token for error recovery.
1277 getNextToken();
1278 }
1279 }
1280
1281 /// top ::= definition | external | expression | ';'
1282 static void MainLoop() {
1283 LLVMContext TheContext;
1284 SessionContext S(TheContext);
1285 KaleidoscopeJIT J(S);
1286
1287 while (true) {
1288 switch (CurTok) {
1289 case tok_eof: return;
1290 case ';': getNextToken(); continue; // ignore top-level semicolons.
1291 case tok_def: HandleDefinition(S, J); break;
1292 case tok_extern: HandleExtern(S); break;
1293 default: HandleTopLevelExpression(S, J); break;
1294 }
1295 #ifndef MINIMAL_STDERR_OUTPUT
1296 std::cerr << "ready> ";
1297 #endif
1298 }
1299 }
1300
1301 //===----------------------------------------------------------------------===//
1302 // "Library" functions that can be "extern'd" from user code.
1303 //===----------------------------------------------------------------------===//
1304
1305 /// putchard - putchar that takes a double and returns 0.
1306 extern "C"
1307 double putchard(double X) {
1308 putchar((char)X);
1309 return 0;
1310 }
1311
1312 /// printd - printf that takes a double prints it as "%f\n", returning 0.
1313 extern "C"
1314 double printd(double X) {
1315 printf("%f", X);
1316 return 0;
1317 }
1318
1319 extern "C"
1320 double printlf() {
1321 printf("\n");
1322 return 0;
1323 }
1324
1325 //===----------------------------------------------------------------------===//
1326 // Main driver code.
1327 //===----------------------------------------------------------------------===//
1328
1329 int main() {
1330 InitializeNativeTarget();
1331 InitializeNativeTargetAsmPrinter();
1332 InitializeNativeTargetAsmParser();
1333
1334 // Install standard binary operators.
1335 // 1 is lowest precedence.
1336 BinopPrecedence['='] = 2;
1337 BinopPrecedence['<'] = 10;
1338 BinopPrecedence['+'] = 20;
1339 BinopPrecedence['-'] = 20;
1340 BinopPrecedence['/'] = 40;
1341 BinopPrecedence['*'] = 40; // highest.
1342
1343 // Prime the first token.
1344 #ifndef MINIMAL_STDERR_OUTPUT
1345 std::cerr << "ready> ";
1346 #endif
1347 getNextToken();
1348
1349 std::cerr << std::fixed;
1350
1351 // Run the main "interpreter loop" now.
1352 MainLoop();
1353
1354 return 0;
1355 }
+0
-12
examples/Kaleidoscope/Orc/lazy_codegen/CMakeLists.txt less more
None set(LLVM_LINK_COMPONENTS
1 Core
2 ExecutionEngine
3 Object
4 RuntimeDyld
5 Support
6 native
7 )
8
9 add_kaleidoscope_chapter(Kaleidoscope-Orc-lazy_codegen
10 toy.cpp
11 )
+0
-13
examples/Kaleidoscope/Orc/lazy_codegen/README.txt less more
None //===----------------------------------------------------------------------===/
1 // Kaleidoscope with Orc - Initial Version
2 //===----------------------------------------------------------------------===//
3
4 This version of Kaleidoscope with Orc demonstrates lazy code-generation.
5 Unlike the first Kaleidoscope-Orc tutorial, where code-gen was performed as soon
6 as modules were added to the JIT, this tutorial adds a LazyEmittingLayer to defer
7 code-generation until modules are actually referenced. All IR-generation is still
8 performed up-front.
9
10 This directory contain a Makefile that allow the code to be built in a
11 standalone manner, independent of the larger LLVM build infrastructure. To build
12 the program you will need to have 'clang++' and 'llvm-config' in your path.
+0
-1361
examples/Kaleidoscope/Orc/lazy_codegen/toy.cpp less more
None #include "llvm/ADT/APFloat.h"
1 #include "llvm/ADT/STLExtras.h"
2 #include "llvm/ExecutionEngine/ExecutionEngine.h"
3 #include "llvm/ExecutionEngine/RuntimeDyld.h"
4 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
5 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
6 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
7 #include "llvm/ExecutionEngine/Orc/JITSymbol.h"
8 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
9 #include "llvm/ExecutionEngine/Orc/LazyEmittingLayer.h"
10 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/Constant.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/IR/DerivedTypes.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/TargetSelect.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include
28 #include
29 #include
30 #include
31 #include
32 #include
33 #include
34 #include
35 #include
36 #include
37 #include
38 #include
39 #include
40
41 using namespace llvm;
42 using namespace llvm::orc;
43
44 //===----------------------------------------------------------------------===//
45 // Lexer
46 //===----------------------------------------------------------------------===//
47
48 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
49 // of these for known things.
50 enum Token {
51 tok_eof = -1,
52
53 // commands
54 tok_def = -2, tok_extern = -3,
55
56 // primary
57 tok_identifier = -4, tok_number = -5,
58
59 // control
60 tok_if = -6, tok_then = -7, tok_else = -8,
61 tok_for = -9, tok_in = -10,
62
63 // operators
64 tok_binary = -11, tok_unary = -12,
65
66 // var definition
67 tok_var = -13
68 };
69
70 static std::string IdentifierStr; // Filled in if tok_identifier
71 static double NumVal; // Filled in if tok_number
72
73 /// gettok - Return the next token from standard input.
74 static int gettok() {
75 static int LastChar = ' ';
76
77 // Skip any whitespace.
78 while (isspace(LastChar))
79 LastChar = getchar();
80
81 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
82 IdentifierStr = LastChar;
83 while (isalnum((LastChar = getchar())))
84 IdentifierStr += LastChar;
85
86 if (IdentifierStr == "def") return tok_def;
87 if (IdentifierStr == "extern") return tok_extern;
88 if (IdentifierStr == "if") return tok_if;
89 if (IdentifierStr == "then") return tok_then;
90 if (IdentifierStr == "else") return tok_else;
91 if (IdentifierStr == "for") return tok_for;
92 if (IdentifierStr == "in") return tok_in;
93 if (IdentifierStr == "binary") return tok_binary;
94 if (IdentifierStr == "unary") return tok_unary;
95 if (IdentifierStr == "var") return tok_var;
96 return tok_identifier;
97 }
98
99 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
100 std::string NumStr;
101 do {
102 NumStr += LastChar;
103 LastChar = getchar();
104 } while (isdigit(LastChar) || LastChar == '.');
105
106 NumVal = strtod(NumStr.c_str(), nullptr);
107 return tok_number;
108 }
109
110 if (LastChar == '#') {
111 // Comment until end of line.
112 do LastChar = getchar();
113 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
114
115 if (LastChar != EOF)
116 return gettok();
117 }
118
119 // Check for end of file. Don't eat the EOF.
120 if (LastChar == EOF)
121 return tok_eof;
122
123 // Otherwise, just return the character as its ascii value.
124 int ThisChar = LastChar;
125 LastChar = getchar();
126 return ThisChar;
127 }
128
129 //===----------------------------------------------------------------------===//
130 // Abstract Syntax Tree (aka Parse Tree)
131 //===----------------------------------------------------------------------===//
132
133 class IRGenContext;
134
135 /// ExprAST - Base class for all expression nodes.
136 struct ExprAST {
137 virtual ~ExprAST() {}
138 virtual Value *IRGen(IRGenContext &C) const = 0;
139 };
140
141 /// NumberExprAST - Expression class for numeric literals like "1.0".
142 struct NumberExprAST : public ExprAST {
143 NumberExprAST(double Val) : Val(Val) {}
144 Value *IRGen(IRGenContext &C) const override;
145
146 double Val;
147 };
148
149 /// VariableExprAST - Expression class for referencing a variable, like "a".
150 struct VariableExprAST : public ExprAST {
151 VariableExprAST(std::string Name) : Name(std::move(Name)) {}
152 Value *IRGen(IRGenContext &C) const override;
153
154 std::string Name;
155 };
156
157 /// UnaryExprAST - Expression class for a unary operator.
158 struct UnaryExprAST : public ExprAST {
159 UnaryExprAST(char Opcode, std::unique_ptr Operand)
160 : Opcode(std::move(Opcode)), Operand(std::move(Operand)) {}
161
162 Value *IRGen(IRGenContext &C) const override;
163
164 char Opcode;
165 std::unique_ptr Operand;
166 };
167
168 /// BinaryExprAST - Expression class for a binary operator.
169 struct BinaryExprAST : public ExprAST {
170 BinaryExprAST(char Op, std::unique_ptr LHS,
171 std::unique_ptr RHS)
172 : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
173
174 Value *IRGen(IRGenContext &C) const override;
175
176 char Op;
177 std::unique_ptr LHS, RHS;
178 };
179
180 /// CallExprAST - Expression class for function calls.
181 struct CallExprAST : public ExprAST {
182 CallExprAST(std::string CalleeName,
183 std::vector> Args)
184 : CalleeName(std::move(CalleeName)), Args(std::move(Args)) {}
185
186 Value *IRGen(IRGenContext &C) const override;
187
188 std::string CalleeName;
189 std::vector> Args;
190 };
191
192 /// IfExprAST - Expression class for if/then/else.
193 struct IfExprAST : public ExprAST {
194 IfExprAST(std::unique_ptr Cond, std::unique_ptr Then,
195 std::unique_ptr Else)
196 : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
197
198 Value *IRGen(IRGenContext &C) const override;
199
200 std::unique_ptr Cond, Then, Else;
201 };
202
203 /// ForExprAST - Expression class for for/in.
204 struct ForExprAST : public ExprAST {
205 ForExprAST(std::string VarName, std::unique_ptr Start,
206 std::unique_ptr End, std::unique_ptr Step,
207 std::unique_ptr Body)
208 : VarName(std::move(VarName)), Start(std::move(Start)), End(std::move(End)),
209 Step(std::move(Step)), Body(std::move(Body)) {}
210
211 Value *IRGen(IRGenContext &C) const override;
212
213 std::string VarName;
214 std::unique_ptr Start, End, Step, Body;
215 };
216
217 /// VarExprAST - Expression class for var/in
218 struct VarExprAST : public ExprAST {
219 typedef std::pair> Binding;
220 typedef std::vector BindingList;
221
222 VarExprAST(BindingList VarBindings, std::unique_ptr Body)
223 : VarBindings(std::move(VarBindings)), Body(std::move(Body)) {}
224
225 Value *IRGen(IRGenContext &C) const override;
226
227 BindingList VarBindings;
228 std::unique_ptr Body;
229 };
230
231 /// PrototypeAST - This class represents the "prototype" for a function,
232 /// which captures its argument names as well as if it is an operator.
233 struct PrototypeAST {
234 PrototypeAST(std::string Name, std::vector Args,
235 bool IsOperator = false, unsigned Precedence = 0)
236 : Name(std::move(Name)), Args(std::move(Args)), IsOperator(IsOperator),
237 Precedence(Precedence) {}
238
239 Function *IRGen(IRGenContext &C) const;
240 void CreateArgumentAllocas(Function *F, IRGenContext &C);
241
242 bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
243 bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
244
245 char getOperatorName() const {
246 assert(isUnaryOp() || isBinaryOp());
247 return Name[Name.size()-1];
248 }
249
250 std::string Name;
251 std::vector Args;
252 bool IsOperator;
253 unsigned Precedence; // Precedence if a binary op.
254 };
255
256 /// FunctionAST - This class represents a function definition itself.
257 struct FunctionAST {
258 FunctionAST(std::unique_ptr Proto,
259 std::unique_ptr Body)
260 : Proto(std::move(Proto)), Body(std::move(Body)) {}
261
262 Function *IRGen(IRGenContext &C) const;
263
264 std::unique_ptr Proto;
265 std::unique_ptr Body;
266 };
267
268 //===----------------------------------------------------------------------===//
269 // Parser
270 //===----------------------------------------------------------------------===//
271
272 /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
273 /// token the parser is looking at. getNextToken reads another token from the
274 /// lexer and updates CurTok with its results.
275 static int CurTok;
276 static int getNextToken() {
277 return CurTok = gettok();
278 }
279
280 /// BinopPrecedence - This holds the precedence for each binary operator that is
281 /// defined.
282 static std::map BinopPrecedence;
283
284 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
285 static int GetTokPrecedence() {
286 if (!isascii(CurTok))
287 return -1;
288
289 // Make sure it's a declared binop.
290 int TokPrec = BinopPrecedence[CurTok];
291 if (TokPrec <= 0) return -1;
292 return TokPrec;
293 }
294
295 template
296 std::unique_ptr ErrorU(const std::string &Str) {
297 std::cerr << "Error: " << Str << "\n";
298 return nullptr;
299 }
300
301 template
302 T* ErrorP(const std::string &Str) {
303 std::cerr << "Error: " << Str << "\n";
304 return nullptr;
305 }
306
307 static std::unique_ptr ParseExpression();
308
309 /// identifierexpr
310 /// ::= identifier
311 /// ::= identifier '(' expression* ')'
312 static std::unique_ptr ParseIdentifierExpr() {
313 std::string IdName = IdentifierStr;
314
315 getNextToken(); // eat identifier.
316
317 if (CurTok != '(') // Simple variable ref.
318 return llvm::make_unique(IdName);
319
320 // Call.
321 getNextToken(); // eat (
322 std::vector> Args;
323 if (CurTok != ')') {
324 while (true) {
325 auto Arg = ParseExpression();
326 if (!Arg) return nullptr;
327 Args.push_back(std::move(Arg));
328
329 if (CurTok == ')') break;
330
331 if (CurTok != ',')
332 return ErrorU("Expected ')' or ',' in argument list");
333 getNextToken();
334 }
335 }
336
337 // Eat the ')'.
338 getNextToken();
339
340 return llvm::make_unique(IdName, std::move(Args));
341 }
342
343 /// numberexpr ::= number
344 static std::unique_ptr ParseNumberExpr() {
345 auto Result = llvm::make_unique(NumVal);
346 getNextToken(); // consume the number
347 return Result;
348 }
349
350 /// parenexpr ::= '(' expression ')'
351 static std::unique_ptr ParseParenExpr() {
352 getNextToken(); // eat (.
353 auto V = ParseExpression();
354 if (!V)
355 return nullptr;
356
357 if (CurTok != ')')
358 return ErrorU("expected ')'");
359 getNextToken(); // eat ).
360 return V;
361 }
362
363 /// ifexpr ::= 'if' expression 'then' expression 'else' expression
364 static std::unique_ptr ParseIfExpr() {
365 getNextToken(); // eat the if.
366
367 // condition.
368 auto Cond = ParseExpression();
369 if (!Cond)
370 return nullptr;
371
372 if (CurTok != tok_then)
373 return ErrorU("expected then");
374 getNextToken(); // eat the then
375
376 auto Then = ParseExpression();
377 if (!Then)
378 return nullptr;
379
380 if (CurTok != tok_else)
381 return ErrorU("expected else");
382
383 getNextToken();
384
385 auto Else = ParseExpression();
386 if (!Else)
387 return nullptr;
388
389 return llvm::make_unique(std::move(Cond), std::move(Then),
390 std::move(Else));
391 }
392
393 /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
394 static std::unique_ptr ParseForExpr() {
395 getNextToken(); // eat the for.
396
397 if (CurTok != tok_identifier)
398 return ErrorU("expected identifier after for");
399
400 std::string IdName = IdentifierStr;
401 getNextToken(); // eat identifier.
402
403 if (CurTok != '=')
404 return ErrorU("expected '=' after for");
405 getNextToken(); // eat '='.
406
407 auto Start = ParseExpression();
408 if (!Start)
409 return nullptr;
410 if (CurTok != ',')
411 return ErrorU("expected ',' after for start value");
412 getNextToken();
413
414 auto End = ParseExpression();
415 if (!End)
416 return nullptr;
417
418 // The step value is optional.
419 std::unique_ptr Step;
420 if (CurTok == ',') {
421 getNextToken();
422 Step = ParseExpression();
423 if (!Step)
424 return nullptr;
425 }
426
427 if (CurTok != tok_in)
428 return ErrorU("expected 'in' after for");
429 getNextToken(); // eat 'in'.
430
431 auto Body = ParseExpression();
432 if (Body)
433 return nullptr;
434
435 return llvm::make_unique(IdName, std::move(Start), std::move(End),
436 std::move(Step), std::move(Body));
437 }
438
439 /// varexpr ::= 'var' identifier ('=' expression)?
440 // (',' identifier ('=' expression)?)* 'in' expression
441 static std::unique_ptr ParseVarExpr() {
442 getNextToken(); // eat the var.
443
444 VarExprAST::BindingList VarBindings;
445
446 // At least one variable name is required.
447 if (CurTok != tok_identifier)
448 return ErrorU("expected identifier after var");
449
450 while (true) {
451 std::string Name = IdentifierStr;
452 getNextToken(); // eat identifier.
453
454 // Read the optional initializer.
455 std::unique_ptr Init;
456 if (CurTok == '=') {
457 getNextToken(); // eat the '='.
458
459 Init = ParseExpression();
460 if (!Init)
461 return nullptr;
462 }
463
464 VarBindings.push_back(VarExprAST::Binding(Name, std::move(Init)));
465
466 // End of var list, exit loop.
467 if (CurTok != ',') break;
468 getNextToken(); // eat the ','.
469
470 if (CurTok != tok_identifier)
471 return ErrorU("expected identifier list after var");
472 }
473
474 // At this point, we have to have 'in'.
475 if (CurTok != tok_in)
476 return ErrorU("expected 'in' keyword after 'var'");
477 getNextToken(); // eat 'in'.
478
479 auto Body = ParseExpression();
480 if (!Body)
481 return nullptr;
482
483 return llvm::make_unique(std::move(VarBindings), std::move(Body));
484 }
485
486 /// primary
487 /// ::= identifierexpr
488 /// ::= numberexpr
489 /// ::= parenexpr
490 /// ::= ifexpr
491 /// ::= forexpr
492 /// ::= varexpr
493 static std::unique_ptr ParsePrimary() {
494 switch (CurTok) {
495 default: return ErrorU("unknown token when expecting an expression");
496 case tok_identifier: return ParseIdentifierExpr();
497 case tok_number: return ParseNumberExpr();
498 case '(': return ParseParenExpr();
499 case tok_if: return ParseIfExpr();
500 case tok_for: return ParseForExpr();
501 case tok_var: return ParseVarExpr();
502 }
503 }
504
505 /// unary
506 /// ::= primary
507 /// ::= '!' unary
508 static std::unique_ptr ParseUnary() {
509 // If the current token is not an operator, it must be a primary expr.
510 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
511 return ParsePrimary();
512
513 // If this is a unary operator, read it.
514 int Opc = CurTok;
515 getNextToken();
516 if (auto Operand = ParseUnary())
517 return llvm::make_unique(Opc, std::move(Operand));
518 return nullptr;
519 }
520
521 /// binoprhs
522 /// ::= ('+' unary)*
523 static std::unique_ptr ParseBinOpRHS(int ExprPrec,
524 std::unique_ptr LHS) {
525 // If this is a binop, find its precedence.
526 while (true) {
527 int TokPrec = GetTokPrecedence();
528
529 // If this is a binop that binds at least as tightly as the current binop,
530 // consume it, otherwise we are done.
531 if (TokPrec < ExprPrec)
532 return LHS;
533
534 // Okay, we know this is a binop.
535 int BinOp = CurTok;
536 getNextToken(); // eat binop
537
538 // Parse the unary expression after the binary operator.
539 auto RHS = ParseUnary();
540 if (!RHS)
541 return nullptr;
542
543 // If BinOp binds less tightly with RHS than the operator after RHS, let
544 // the pending operator take RHS as its LHS.
545 int NextPrec = GetTokPrecedence();
546 if (TokPrec < NextPrec) {
547 RHS = ParseBinOpRHS(TokPrec+1, std::move(RHS));
548 if (!RHS)
549 return nullptr;
550 }
551
552 // Merge LHS/RHS.
553 LHS = llvm::make_unique(BinOp, std::move(LHS), std::move(RHS));
554 }
555 }
556
557 /// expression
558 /// ::= unary binoprhs
559 ///
560 static std::unique_ptr ParseExpression() {
561 auto LHS = ParseUnary();
562 if (!LHS)
563 return nullptr;
564
565 return ParseBinOpRHS(0, std::move(LHS));
566 }
567
568 /// prototype
569 /// ::= id '(' id* ')'
570 /// ::= binary LETTER number? (id, id)
571 /// ::= unary LETTER (id)
572 static std::unique_ptr ParsePrototype() {
573 std::string FnName;
574
575 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
576 unsigned BinaryPrecedence = 30;
577
578 switch (CurTok) {
579 default:
580 return ErrorU("Expected function name in prototype");
581 case tok_identifier:
582 FnName = IdentifierStr;
583 Kind = 0;
584 getNextToken();
585 break;
586 case tok_unary:
587 getNextToken();
588 if (!isascii(CurTok))
589 return ErrorU("Expected unary operator");
590 FnName = "unary";
591 FnName += (char)CurTok;
592 Kind = 1;
593 getNextToken();
594 break;
595 case tok_binary:
596 getNextToken();
597 if (!isascii(CurTok))
598 return ErrorU("Expected binary operator");
599 FnName = "binary";
600 FnName += (char)CurTok;
601 Kind = 2;
602 getNextToken();
603
604 // Read the precedence if present.
605 if (CurTok == tok_number) {
606 if (NumVal < 1 || NumVal > 100)
607 return ErrorU("Invalid precedecnce: must be 1..100");
608 BinaryPrecedence = (unsigned)NumVal;
609 getNextToken();
610 }
611 break;
612 }
613
614 if (CurTok != '(')
615 return ErrorU("Expected '(' in prototype");
616
617 std::vector ArgNames;
618 while (getNextToken() == tok_identifier)
619 ArgNames.push_back(IdentifierStr);
620 if (CurTok != ')')
621 return ErrorU("Expected ')' in prototype");
622
623 // success.
624 getNextToken(); // eat ')'.
625
626 // Verify right number of names for operator.
627 if (Kind && ArgNames.size() != Kind)
628 return ErrorU("Invalid number of operands for operator");
629
630 return llvm::make_unique(FnName, std::move(ArgNames), Kind != 0,
631 BinaryPrecedence);
632 }
633
634 /// definition ::= 'def' prototype expression
635 static std::unique_ptr ParseDefinition() {
636 getNextToken(); // eat def.
637 auto Proto = ParsePrototype();
638 if (!Proto)
639 return nullptr;
640
641 if (auto Body = ParseExpression())
642 return llvm::make_unique(std::move(Proto), std::move(Body));
643 return nullptr;
644 }
645
646 /// toplevelexpr ::= expression
647 static std::unique_ptr ParseTopLevelExpr() {
648 if (auto E = ParseExpression()) {
649 // Make an anonymous proto.
650 auto Proto =
651 llvm::make_unique("__anon_expr", std::vector());
652 return llvm::make_unique(std::move(Proto), std::move(E));
653 }
654 return nullptr;
655 }
656
657 /// external ::= 'extern' prototype
658 static std::unique_ptr ParseExtern() {
659 getNextToken(); // eat extern.
660 return ParsePrototype();
661 }
662
663 //===----------------------------------------------------------------------===//
664 // Code Generation
665 //===----------------------------------------------------------------------===//
666
667 // FIXME: Obviously we can do better than this
668 std::string GenerateUniqueName(const std::string &Root) {
669 static int i = 0;
670 std::ostringstream NameStream;
671 NameStream << Root << ++i;
672 return NameStream.str();
673 }
674
675 std::string MakeLegalFunctionName(std::string Name)
676 {
677 std::string NewName;
678 assert(!Name.empty() && "Base name must not be empty");
679
680 // Start with what we have
681 NewName = Name;
682
683 // Look for a numberic first character
684 if (NewName.find_first_of("0123456789") == 0) {
685 NewName.insert(0, 1, 'n');
686 }
687
688 // Replace illegal characters with their ASCII equivalent
689 std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
690 size_t pos;
691 while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
692 std::ostringstream NumStream;
693 NumStream << (int)NewName.at(pos);
694 NewName = NewName.replace(pos, 1, NumStream.str());
695 }
696
697 return NewName;
698 }
699
700 class SessionContext {
701 public:
702 SessionContext(LLVMContext &C)
703 : Context(C), TM(EngineBuilder().selectTarget()) {}
704 LLVMContext& getLLVMContext() const { return Context; }
705 TargetMachine& getTarget() { return *TM; }
706 void addPrototypeAST(std::unique_ptr P);
707 PrototypeAST* getPrototypeAST(const std::string &Name);
708
709 private:
710 typedef std::map> PrototypeMap;
711
712 LLVMContext &Context;
713 std::unique_ptr TM;
714
715 PrototypeMap Prototypes;
716 };
717
718 void SessionContext::addPrototypeAST(std::unique_ptr P) {
719 Prototypes[P->Name] = std::move(P);
720 }
721
722 PrototypeAST* SessionContext::getPrototypeAST(const std::string &Name) {
723 PrototypeMap::iterator I = Prototypes.find(Name);
724 if (I != Prototypes.end())
725 return I->second.get();
726 return nullptr;
727 }
728
729 class IRGenContext {
730 public:
731 IRGenContext(SessionContext &S)
732 : Session(S),
733 M(new Module(GenerateUniqueName("jit_module_"),
734 Session.getLLVMContext())),
735 Builder(Session.getLLVMContext()) {
736 M->setDataLayout(Session.getTarget().createDataLayout());
737 }
738
739 SessionContext& getSession() { return Session; }
740 Module& getM() const { return *M; }
741 std::unique_ptr takeM() { return std::move(M); }
742 IRBuilder<>& getBuilder() { return Builder; }
743 LLVMContext& getLLVMContext() { return Session.getLLVMContext(); }
744 Function* getPrototype(const std::string &Name);
745
746 std::map NamedValues;
747
748 private:
749 SessionContext &Session;
750 std::unique_ptr M;
751 IRBuilder<> Builder;
752 };
753
754 Function* IRGenContext::getPrototype(const std::string &Name) {
755 if (Function *ExistingProto = M->getFunction(Name))
756 return ExistingProto;
757 if (PrototypeAST *ProtoAST = Session.getPrototypeAST(Name))
758 return ProtoAST->IRGen(*this);
759 return nullptr;
760 }
761
762 /// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
763 /// the function. This is used for mutable variables etc.
764 static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
765 const std::string &VarName) {
766 IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
767 TheFunction->getEntryBlock().begin());
768 return TmpB.CreateAlloca(Type::getDoubleTy(TheFunction->getContext()),
769 nullptr, VarName);
770 }
771
772 Value *NumberExprAST::IRGen(IRGenContext &C) const {
773 return ConstantFP::get(C.getLLVMContext(), APFloat(Val));
774 }
775
776 Value *VariableExprAST::IRGen(IRGenContext &C) const {
777 // Look this variable up in the function.
778 Value *V = C.NamedValues[Name];
779
780 if (!V)
781 return ErrorP("Unknown variable name '" + Name + "'");
782
783 // Load the value.
784 return C.getBuilder().CreateLoad(V, Name.c_str());
785 }
786
787 Value *UnaryExprAST::IRGen(IRGenContext &C) const {
788 if (Value *OperandV = Operand->IRGen(C)) {
789 std::string FnName = MakeLegalFunctionName(std::string("unary")+Opcode);
790 if (Function *F = C.getPrototype(FnName))
791 return C.getBuilder().CreateCall(F, OperandV, "unop");
792 return ErrorP("Unknown unary operator");
793 }
794
795 // Could not codegen operand - return null.
796 return nullptr;
797 }
798
799 Value *BinaryExprAST::IRGen(IRGenContext &C) const {
800 // Special case '=' because we don't want to emit the LHS as an expression.
801 if (Op == '=') {
802 // Assignment requires the LHS to be an identifier.
803 auto &LHSVar = static_cast(*LHS);
804 // Codegen the RHS.
805 Value *Val = RHS->IRGen(C);
806 if (!Val) return nullptr;
807
808 // Look up the name.
809 if (auto Variable = C.NamedValues[LHSVar.Name]) {
810 C.getBuilder().CreateStore(Val, Variable);
811 return Val;
812 }
813 return ErrorP("Unknown variable name");
814 }
815
816 Value *L = LHS->IRGen(C);
817 Value *R = RHS->IRGen(C);
818 if (!L || !R) return nullptr;
819
820 switch (Op) {
821 case '+': return C.getBuilder().CreateFAdd(L, R, "addtmp");
822 case '-': return C.getBuilder().CreateFSub(L, R, "subtmp");
823 case '*': return C.getBuilder().CreateFMul(L, R, "multmp");
824 case '/': return C.getBuilder().CreateFDiv(L, R, "divtmp");
825 case '<':
826 L = C.getBuilder().CreateFCmpULT(L, R, "cmptmp");
827 // Convert bool 0/1 to double 0.0 or 1.0
828 return C.getBuilder().CreateUIToFP(L, Type::getDoubleTy(C.getLLVMContext()),
829 "booltmp");
830 default: break;
831 }
832
833 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
834 // a call to it.
835 std::string FnName = MakeLegalFunctionName(std::string("binary")+Op);
836 if (Function *F = C.getPrototype(FnName)) {
837 Value *Ops[] = { L, R };
838 return C.getBuilder().CreateCall(F, Ops, "binop");
839 }
840
841 return ErrorP("Unknown binary operator");
842 }
843
844 Value *CallExprAST::IRGen(IRGenContext &C) const {
845 // Look up the name in the global module table.
846 if (auto CalleeF = C.getPrototype(CalleeName)) {
847 // If argument mismatch error.
848 if (CalleeF->arg_size() != Args.size())
849 return ErrorP("Incorrect # arguments passed");
850
851 std::vector ArgsV;
852 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
853 ArgsV.push_back(Args[i]->IRGen(C));
854 if (!ArgsV.back()) return nullptr;
855 }
856
857 return C.getBuilder().CreateCall(CalleeF, ArgsV, "calltmp");
858 }
859