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