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