blob: ad091e4496b73775aa6412ef961b122e448cfd72 [file] [log] [blame]
Chandler Carruth605e30e2012-12-04 10:16:57 +00001#include "llvm/Analysis/Passes.h"
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +00002#include "llvm/ExecutionEngine/ExecutionEngine.h"
Eric Christopher1b74b652014-12-08 18:00:38 +00003#include "llvm/ExecutionEngine/MCJIT.h"
4#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +00005#include "llvm/IR/DataLayout.h"
6#include "llvm/IR/DerivedTypes.h"
7#include "llvm/IR/IRBuilder.h"
8#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +00009#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000010#include "llvm/IR/Module.h"
Chandler Carruth20d4e6b2014-01-13 09:58:03 +000011#include "llvm/IR/Verifier.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000012#include "llvm/Support/TargetSelect.h"
Chandler Carruth605e30e2012-12-04 10:16:57 +000013#include "llvm/Transforms/Scalar.h"
Will Dietz981af002013-10-12 00:55:57 +000014#include <cctype>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000015#include <cstdio>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000016#include <map>
Chandler Carruth605e30e2012-12-04 10:16:57 +000017#include <string>
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000018#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
Eric Christopherc0239362014-12-08 18:12:28 +000031 tok_def = -2,
32 tok_extern = -3,
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000033
34 // primary
Eric Christopherc0239362014-12-08 18:12:28 +000035 tok_identifier = -4,
36 tok_number = -5
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000037};
38
Eric Christopherc0239362014-12-08 18:12:28 +000039static std::string IdentifierStr; // Filled in if tok_identifier
40static double NumVal; // Filled in if tok_number
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000041
42/// gettok - Return the next token from standard input.
43static int gettok() {
44 static int LastChar = ' ';
45
46 // Skip any whitespace.
47 while (isspace(LastChar))
48 LastChar = getchar();
49
50 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
51 IdentifierStr = LastChar;
52 while (isalnum((LastChar = getchar())))
53 IdentifierStr += LastChar;
54
Eric Christopherc0239362014-12-08 18:12:28 +000055 if (IdentifierStr == "def")
56 return tok_def;
57 if (IdentifierStr == "extern")
58 return tok_extern;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000059 return tok_identifier;
60 }
61
Eric Christopherc0239362014-12-08 18:12:28 +000062 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000063 std::string NumStr;
64 do {
65 NumStr += LastChar;
66 LastChar = getchar();
67 } while (isdigit(LastChar) || LastChar == '.');
68
69 NumVal = strtod(NumStr.c_str(), 0);
70 return tok_number;
71 }
72
73 if (LastChar == '#') {
74 // Comment until end of line.
Eric Christopherc0239362014-12-08 18:12:28 +000075 do
76 LastChar = getchar();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000077 while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
Eric Christopherc0239362014-12-08 18:12:28 +000078
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000079 if (LastChar != EOF)
80 return gettok();
81 }
Eric Christopherc0239362014-12-08 18:12:28 +000082
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000083 // Check for end of file. Don't eat the EOF.
84 if (LastChar == EOF)
85 return tok_eof;
86
87 // Otherwise, just return the character as its ascii value.
88 int ThisChar = LastChar;
89 LastChar = getchar();
90 return ThisChar;
91}
92
93//===----------------------------------------------------------------------===//
94// Abstract Syntax Tree (aka Parse Tree)
95//===----------------------------------------------------------------------===//
Juergen Ributzka05c5a932013-11-19 03:08:35 +000096namespace {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +000097/// ExprAST - Base class for all expression nodes.
98class ExprAST {
99public:
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000100 virtual ~ExprAST() {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000101 virtual Value *Codegen() = 0;
102};
103
104/// NumberExprAST - Expression class for numeric literals like "1.0".
105class NumberExprAST : public ExprAST {
106 double Val;
Eric Christopherc0239362014-12-08 18:12:28 +0000107
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000108public:
109 NumberExprAST(double val) : Val(val) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000110 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000111};
112
113/// VariableExprAST - Expression class for referencing a variable, like "a".
114class VariableExprAST : public ExprAST {
115 std::string Name;
Eric Christopherc0239362014-12-08 18:12:28 +0000116
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000117public:
118 VariableExprAST(const std::string &name) : Name(name) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000119 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000120};
121
122/// BinaryExprAST - Expression class for a binary operator.
123class BinaryExprAST : public ExprAST {
124 char Op;
125 ExprAST *LHS, *RHS;
Eric Christopherc0239362014-12-08 18:12:28 +0000126
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000127public:
Eric Christopherc0239362014-12-08 18:12:28 +0000128 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
129 : Op(op), LHS(lhs), RHS(rhs) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000130 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000131};
132
133/// CallExprAST - Expression class for function calls.
134class CallExprAST : public ExprAST {
135 std::string Callee;
Eric Christopherc0239362014-12-08 18:12:28 +0000136 std::vector<ExprAST *> Args;
137
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000138public:
Eric Christopherc0239362014-12-08 18:12:28 +0000139 CallExprAST(const std::string &callee, std::vector<ExprAST *> &args)
140 : Callee(callee), Args(args) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000141 Value *Codegen() override;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000142};
143
144/// PrototypeAST - This class represents the "prototype" for a function,
145/// which captures its name, and its argument names (thus implicitly the number
146/// of arguments the function takes).
147class PrototypeAST {
148 std::string Name;
149 std::vector<std::string> Args;
Eric Christopherc0239362014-12-08 18:12:28 +0000150
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000151public:
152 PrototypeAST(const std::string &name, const std::vector<std::string> &args)
Eric Christopherc0239362014-12-08 18:12:28 +0000153 : Name(name), Args(args) {}
154
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000155 Function *Codegen();
156};
157
158/// FunctionAST - This class represents a function definition itself.
159class FunctionAST {
160 PrototypeAST *Proto;
161 ExprAST *Body;
Eric Christopherc0239362014-12-08 18:12:28 +0000162
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000163public:
Eric Christopherc0239362014-12-08 18:12:28 +0000164 FunctionAST(PrototypeAST *proto, ExprAST *body) : Proto(proto), Body(body) {}
165
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000166 Function *Codegen();
167};
Juergen Ributzka05c5a932013-11-19 03:08:35 +0000168} // end anonymous namespace
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000169
170//===----------------------------------------------------------------------===//
171// Parser
172//===----------------------------------------------------------------------===//
173
174/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
175/// token the parser is looking at. getNextToken reads another token from the
176/// lexer and updates CurTok with its results.
177static int CurTok;
Eric Christopherc0239362014-12-08 18:12:28 +0000178static int getNextToken() { return CurTok = gettok(); }
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000179
180/// BinopPrecedence - This holds the precedence for each binary operator that is
181/// defined.
182static std::map<char, int> BinopPrecedence;
183
184/// GetTokPrecedence - Get the precedence of the pending binary operator token.
185static int GetTokPrecedence() {
186 if (!isascii(CurTok))
187 return -1;
Eric Christopherc0239362014-12-08 18:12:28 +0000188
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000189 // Make sure it's a declared binop.
190 int TokPrec = BinopPrecedence[CurTok];
Eric Christopherc0239362014-12-08 18:12:28 +0000191 if (TokPrec <= 0)
192 return -1;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000193 return TokPrec;
194}
195
196/// Error* - These are little helper functions for error handling.
Eric Christopherc0239362014-12-08 18:12:28 +0000197ExprAST *Error(const char *Str) {
198 fprintf(stderr, "Error: %s\n", Str);
199 return 0;
200}
201PrototypeAST *ErrorP(const char *Str) {
202 Error(Str);
203 return 0;
204}
205FunctionAST *ErrorF(const char *Str) {
206 Error(Str);
207 return 0;
208}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000209
210static ExprAST *ParseExpression();
211
212/// identifierexpr
213/// ::= identifier
214/// ::= identifier '(' expression* ')'
215static ExprAST *ParseIdentifierExpr() {
216 std::string IdName = IdentifierStr;
Eric Christopherc0239362014-12-08 18:12:28 +0000217
218 getNextToken(); // eat identifier.
219
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000220 if (CurTok != '(') // Simple variable ref.
221 return new VariableExprAST(IdName);
Eric Christopherc0239362014-12-08 18:12:28 +0000222
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000223 // Call.
Eric Christopherc0239362014-12-08 18:12:28 +0000224 getNextToken(); // eat (
225 std::vector<ExprAST *> Args;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000226 if (CurTok != ')') {
227 while (1) {
228 ExprAST *Arg = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000229 if (!Arg)
230 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000231 Args.push_back(Arg);
232
Eric Christopherc0239362014-12-08 18:12:28 +0000233 if (CurTok == ')')
234 break;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000235
236 if (CurTok != ',')
237 return Error("Expected ')' or ',' in argument list");
238 getNextToken();
239 }
240 }
241
242 // Eat the ')'.
243 getNextToken();
Eric Christopherc0239362014-12-08 18:12:28 +0000244
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000245 return new CallExprAST(IdName, Args);
246}
247
248/// numberexpr ::= number
249static ExprAST *ParseNumberExpr() {
250 ExprAST *Result = new NumberExprAST(NumVal);
251 getNextToken(); // consume the number
252 return Result;
253}
254
255/// parenexpr ::= '(' expression ')'
256static ExprAST *ParseParenExpr() {
Eric Christopherc0239362014-12-08 18:12:28 +0000257 getNextToken(); // eat (.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000258 ExprAST *V = ParseExpression();
Eric Christopherc0239362014-12-08 18:12:28 +0000259 if (!V)
260 return 0;
261
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000262 if (CurTok != ')')
263 return Error("expected ')'");
Eric Christopherc0239362014-12-08 18:12:28 +0000264 getNextToken(); // eat ).
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000265 return V;
266}
267
268/// primary
269/// ::= identifierexpr
270/// ::= numberexpr
271/// ::= parenexpr
272static ExprAST *ParsePrimary() {
273 switch (CurTok) {
Eric Christopherc0239362014-12-08 18:12:28 +0000274 default:
275 return Error("unknown token when expecting an expression");
276 case tok_identifier:
277 return ParseIdentifierExpr();
278 case tok_number:
279 return ParseNumberExpr();
280 case '(':
281 return ParseParenExpr();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000282 }
283}
284
285/// binoprhs
286/// ::= ('+' primary)*
287static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
288 // If this is a binop, find its precedence.
289 while (1) {
290 int TokPrec = GetTokPrecedence();
Eric Christopherc0239362014-12-08 18:12:28 +0000291
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000292 // If this is a binop that binds at least as tightly as the current binop,
293 // consume it, otherwise we are done.
294 if (TokPrec < ExprPrec)
295 return LHS;
Eric Christopherc0239362014-12-08 18:12:28 +0000296
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000297 // Okay, we know this is a binop.
298 int BinOp = CurTok;
Eric Christopherc0239362014-12-08 18:12:28 +0000299 getNextToken(); // eat binop
300
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000301 // Parse the primary expression after the binary operator.
302 ExprAST *RHS = ParsePrimary();
Eric Christopherc0239362014-12-08 18:12:28 +0000303 if (!RHS)
304 return 0;
305
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000306 // If BinOp binds less tightly with RHS than the operator after RHS, let
307 // the pending operator take RHS as its LHS.
308 int NextPrec = GetTokPrecedence();
309 if (TokPrec < NextPrec) {
Eric Christopherc0239362014-12-08 18:12:28 +0000310 RHS = ParseBinOpRHS(TokPrec + 1, RHS);
311 if (RHS == 0)
312 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000313 }
Eric Christopherc0239362014-12-08 18:12:28 +0000314
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000315 // Merge LHS/RHS.
316 LHS = new BinaryExprAST(BinOp, LHS, RHS);
317 }
318}
319
320/// expression
321/// ::= primary binoprhs
322///
323static ExprAST *ParseExpression() {
324 ExprAST *LHS = ParsePrimary();
Eric Christopherc0239362014-12-08 18:12:28 +0000325 if (!LHS)
326 return 0;
327
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000328 return ParseBinOpRHS(0, LHS);
329}
330
331/// prototype
332/// ::= id '(' id* ')'
333static PrototypeAST *ParsePrototype() {
334 if (CurTok != tok_identifier)
335 return ErrorP("Expected function name in prototype");
336
337 std::string FnName = IdentifierStr;
338 getNextToken();
Eric Christopherc0239362014-12-08 18:12:28 +0000339
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000340 if (CurTok != '(')
341 return ErrorP("Expected '(' in prototype");
Eric Christopherc0239362014-12-08 18:12:28 +0000342
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000343 std::vector<std::string> ArgNames;
344 while (getNextToken() == tok_identifier)
345 ArgNames.push_back(IdentifierStr);
346 if (CurTok != ')')
347 return ErrorP("Expected ')' in prototype");
Eric Christopherc0239362014-12-08 18:12:28 +0000348
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000349 // success.
Eric Christopherc0239362014-12-08 18:12:28 +0000350 getNextToken(); // eat ')'.
351
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000352 return new PrototypeAST(FnName, ArgNames);
353}
354
355/// definition ::= 'def' prototype expression
356static FunctionAST *ParseDefinition() {
Eric Christopherc0239362014-12-08 18:12:28 +0000357 getNextToken(); // eat def.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000358 PrototypeAST *Proto = ParsePrototype();
Eric Christopherc0239362014-12-08 18:12:28 +0000359 if (Proto == 0)
360 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000361
362 if (ExprAST *E = ParseExpression())
363 return new FunctionAST(Proto, E);
364 return 0;
365}
366
367/// toplevelexpr ::= expression
368static FunctionAST *ParseTopLevelExpr() {
369 if (ExprAST *E = ParseExpression()) {
370 // Make an anonymous proto.
371 PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
372 return new FunctionAST(Proto, E);
373 }
374 return 0;
375}
376
377/// external ::= 'extern' prototype
378static PrototypeAST *ParseExtern() {
Eric Christopherc0239362014-12-08 18:12:28 +0000379 getNextToken(); // eat extern.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000380 return ParsePrototype();
381}
382
383//===----------------------------------------------------------------------===//
Lang Hames1cb54812015-01-16 19:44:46 +0000384// Quick and dirty hack
385//===----------------------------------------------------------------------===//
386
387// FIXME: Obviously we can do better than this
Lang Hames37679192015-01-16 21:42:07 +0000388std::string GenerateUniqueName(const char *root) {
Lang Hames1cb54812015-01-16 19:44:46 +0000389 static int i = 0;
390 char s[16];
391 sprintf(s, "%s%d", root, i++);
392 std::string S = s;
393 return S;
394}
395
Lang Hames37679192015-01-16 21:42:07 +0000396std::string MakeLegalFunctionName(std::string Name) {
Lang Hames1cb54812015-01-16 19:44:46 +0000397 std::string NewName;
398 if (!Name.length())
Lang Hames37679192015-01-16 21:42:07 +0000399 return GenerateUniqueName("anon_func_");
Lang Hames1cb54812015-01-16 19:44:46 +0000400
401 // Start with what we have
402 NewName = Name;
403
404 // Look for a numberic first character
405 if (NewName.find_first_of("0123456789") == 0) {
406 NewName.insert(0, 1, 'n');
407 }
408
409 // Replace illegal characters with their ASCII equivalent
Lang Hames37679192015-01-16 21:42:07 +0000410 std::string legal_elements =
411 "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Lang Hames1cb54812015-01-16 19:44:46 +0000412 size_t pos;
Lang Hames37679192015-01-16 21:42:07 +0000413 while ((pos = NewName.find_first_not_of(legal_elements)) !=
414 std::string::npos) {
Lang Hames1cb54812015-01-16 19:44:46 +0000415 char old_c = NewName.at(pos);
416 char new_str[16];
417 sprintf(new_str, "%d", (int)old_c);
418 NewName = NewName.replace(pos, 1, new_str);
419 }
420
421 return NewName;
422}
423
424//===----------------------------------------------------------------------===//
425// MCJIT helper class
426//===----------------------------------------------------------------------===//
427
Lang Hames37679192015-01-16 21:42:07 +0000428class MCJITHelper {
Lang Hames1cb54812015-01-16 19:44:46 +0000429public:
Lang Hames37679192015-01-16 21:42:07 +0000430 MCJITHelper(LLVMContext &C) : Context(C), OpenModule(NULL) {}
Lang Hames1cb54812015-01-16 19:44:46 +0000431 ~MCJITHelper();
432
433 Function *getFunction(const std::string FnName);
434 Module *getModuleForNewFunction();
Lang Hames37679192015-01-16 21:42:07 +0000435 void *getPointerToFunction(Function *F);
Lang Hames1cb54812015-01-16 19:44:46 +0000436 void *getSymbolAddress(const std::string &Name);
437 void dump();
438
439private:
Lang Hames37679192015-01-16 21:42:07 +0000440 typedef std::vector<Module *> ModuleVector;
441 typedef std::vector<ExecutionEngine *> EngineVector;
Lang Hames1cb54812015-01-16 19:44:46 +0000442
Lang Hames37679192015-01-16 21:42:07 +0000443 LLVMContext &Context;
444 Module *OpenModule;
445 ModuleVector Modules;
446 EngineVector Engines;
Lang Hames1cb54812015-01-16 19:44:46 +0000447};
448
Lang Hames37679192015-01-16 21:42:07 +0000449class HelpingMemoryManager : public SectionMemoryManager {
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000450 HelpingMemoryManager(const HelpingMemoryManager &) = delete;
451 void operator=(const HelpingMemoryManager &) = delete;
Lang Hames1cb54812015-01-16 19:44:46 +0000452
453public:
454 HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000455 ~HelpingMemoryManager() override {}
Lang Hames1cb54812015-01-16 19:44:46 +0000456
457 /// This method returns the address of the specified symbol.
458 /// Our implementation will attempt to find symbols in other
459 /// modules associated with the MCJITHelper to cross link symbols
460 /// from one generated module to another.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000461 uint64_t getSymbolAddress(const std::string &Name) override;
Lang Hames37679192015-01-16 21:42:07 +0000462
Lang Hames1cb54812015-01-16 19:44:46 +0000463private:
464 MCJITHelper *MasterHelper;
465};
466
Lang Hames37679192015-01-16 21:42:07 +0000467uint64_t HelpingMemoryManager::getSymbolAddress(const std::string &Name) {
Lang Hames1cb54812015-01-16 19:44:46 +0000468 uint64_t FnAddr = SectionMemoryManager::getSymbolAddress(Name);
469 if (FnAddr)
470 return FnAddr;
471
Lang Hames37679192015-01-16 21:42:07 +0000472 uint64_t HelperFun = (uint64_t)MasterHelper->getSymbolAddress(Name);
Lang Hames1cb54812015-01-16 19:44:46 +0000473 if (!HelperFun)
474 report_fatal_error("Program used extern function '" + Name +
475 "' which could not be resolved!");
476
477 return HelperFun;
478}
479
Lang Hames37679192015-01-16 21:42:07 +0000480MCJITHelper::~MCJITHelper() {
Lang Hames1cb54812015-01-16 19:44:46 +0000481 if (OpenModule)
482 delete OpenModule;
483 EngineVector::iterator begin = Engines.begin();
484 EngineVector::iterator end = Engines.end();
485 EngineVector::iterator it;
486 for (it = begin; it != end; ++it)
487 delete *it;
488}
489
490Function *MCJITHelper::getFunction(const std::string FnName) {
491 ModuleVector::iterator begin = Modules.begin();
492 ModuleVector::iterator end = Modules.end();
493 ModuleVector::iterator it;
494 for (it = begin; it != end; ++it) {
495 Function *F = (*it)->getFunction(FnName);
496 if (F) {
497 if (*it == OpenModule)
Lang Hames37679192015-01-16 21:42:07 +0000498 return F;
Lang Hames1cb54812015-01-16 19:44:46 +0000499
500 assert(OpenModule != NULL);
501
502 // This function is in a module that has already been JITed.
503 // We need to generate a new prototype for external linkage.
504 Function *PF = OpenModule->getFunction(FnName);
505 if (PF && !PF->empty()) {
506 ErrorF("redefinition of function across modules");
507 return 0;
508 }
509
510 // If we don't have a prototype yet, create one.
511 if (!PF)
Lang Hames37679192015-01-16 21:42:07 +0000512 PF = Function::Create(F->getFunctionType(), Function::ExternalLinkage,
513 FnName, OpenModule);
Lang Hames1cb54812015-01-16 19:44:46 +0000514 return PF;
515 }
516 }
517 return NULL;
518}
519
520Module *MCJITHelper::getModuleForNewFunction() {
521 // If we have a Module that hasn't been JITed, use that.
522 if (OpenModule)
523 return OpenModule;
524
525 // Otherwise create a new Module.
526 std::string ModName = GenerateUniqueName("mcjit_module_");
527 Module *M = new Module(ModName, Context);
528 Modules.push_back(M);
529 OpenModule = M;
530 return M;
531}
532
Lang Hames37679192015-01-16 21:42:07 +0000533void *MCJITHelper::getPointerToFunction(Function *F) {
Lang Hames1cb54812015-01-16 19:44:46 +0000534 // See if an existing instance of MCJIT has this function.
535 EngineVector::iterator begin = Engines.begin();
536 EngineVector::iterator end = Engines.end();
537 EngineVector::iterator it;
538 for (it = begin; it != end; ++it) {
539 void *P = (*it)->getPointerToFunction(F);
540 if (P)
541 return P;
542 }
543
544 // If we didn't find the function, see if we can generate it.
545 if (OpenModule) {
546 std::string ErrStr;
Lang Hames37679192015-01-16 21:42:07 +0000547 ExecutionEngine *NewEngine =
548 EngineBuilder(std::unique_ptr<Module>(OpenModule))
549 .setErrorStr(&ErrStr)
550 .setMCJITMemoryManager(std::unique_ptr<HelpingMemoryManager>(
551 new HelpingMemoryManager(this)))
552 .create();
Lang Hames1cb54812015-01-16 19:44:46 +0000553 if (!NewEngine) {
554 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
555 exit(1);
556 }
557
558 // Create a function pass manager for this engine
Chandler Carruth7ecd9912015-02-13 10:21:05 +0000559 auto *FPM = new legacy::FunctionPassManager(OpenModule);
Lang Hames1cb54812015-01-16 19:44:46 +0000560
561 // Set up the optimizer pipeline. Start with registering info about how the
562 // target lays out data structures.
Rafael Espindola265ffbe2015-03-04 19:15:29 +0000563 OpenModule->setDataLayout(*NewEngine->getDataLayout());
Lang Hames1cb54812015-01-16 19:44:46 +0000564 // Provide basic AliasAnalysis support for GVN.
565 FPM->add(createBasicAliasAnalysisPass());
566 // Promote allocas to registers.
567 FPM->add(createPromoteMemoryToRegisterPass());
568 // Do simple "peephole" optimizations and bit-twiddling optzns.
569 FPM->add(createInstructionCombiningPass());
570 // Reassociate expressions.
571 FPM->add(createReassociatePass());
572 // Eliminate Common SubExpressions.
573 FPM->add(createGVNPass());
574 // Simplify the control flow graph (deleting unreachable blocks, etc).
575 FPM->add(createCFGSimplificationPass());
576 FPM->doInitialization();
577
578 // For each function in the module
579 Module::iterator it;
580 Module::iterator end = OpenModule->end();
581 for (it = OpenModule->begin(); it != end; ++it) {
582 // Run the FPM on this function
583 FPM->run(*it);
584 }
585
586 // We don't need this anymore
587 delete FPM;
588
589 OpenModule = NULL;
590 Engines.push_back(NewEngine);
591 NewEngine->finalizeObject();
592 return NewEngine->getPointerToFunction(F);
593 }
594 return NULL;
595}
596
Lang Hames37679192015-01-16 21:42:07 +0000597void *MCJITHelper::getSymbolAddress(const std::string &Name) {
Lang Hames1cb54812015-01-16 19:44:46 +0000598 // Look for the symbol in each of our execution engines.
599 EngineVector::iterator begin = Engines.begin();
600 EngineVector::iterator end = Engines.end();
601 EngineVector::iterator it;
602 for (it = begin; it != end; ++it) {
603 uint64_t FAddr = (*it)->getFunctionAddress(Name);
604 if (FAddr) {
Lang Hames37679192015-01-16 21:42:07 +0000605 return (void *)FAddr;
Lang Hames1cb54812015-01-16 19:44:46 +0000606 }
607 }
608 return NULL;
609}
610
Lang Hames37679192015-01-16 21:42:07 +0000611void MCJITHelper::dump() {
Lang Hames1cb54812015-01-16 19:44:46 +0000612 ModuleVector::iterator begin = Modules.begin();
613 ModuleVector::iterator end = Modules.end();
614 ModuleVector::iterator it;
615 for (it = begin; it != end; ++it)
616 (*it)->dump();
617}
618//===----------------------------------------------------------------------===//
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000619// Code Generation
620//===----------------------------------------------------------------------===//
621
Lang Hames1cb54812015-01-16 19:44:46 +0000622static MCJITHelper *JITHelper;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000623static IRBuilder<> Builder(getGlobalContext());
Eric Christopherc0239362014-12-08 18:12:28 +0000624static std::map<std::string, Value *> NamedValues;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000625
Eric Christopherc0239362014-12-08 18:12:28 +0000626Value *ErrorV(const char *Str) {
627 Error(Str);
628 return 0;
629}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000630
631Value *NumberExprAST::Codegen() {
632 return ConstantFP::get(getGlobalContext(), APFloat(Val));
633}
634
635Value *VariableExprAST::Codegen() {
636 // Look this variable up in the function.
637 Value *V = NamedValues[Name];
638 return V ? V : ErrorV("Unknown variable name");
639}
640
641Value *BinaryExprAST::Codegen() {
642 Value *L = LHS->Codegen();
643 Value *R = RHS->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000644 if (L == 0 || R == 0)
645 return 0;
646
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000647 switch (Op) {
Eric Christopherc0239362014-12-08 18:12:28 +0000648 case '+':
649 return Builder.CreateFAdd(L, R, "addtmp");
650 case '-':
651 return Builder.CreateFSub(L, R, "subtmp");
652 case '*':
653 return Builder.CreateFMul(L, R, "multmp");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000654 case '<':
655 L = Builder.CreateFCmpULT(L, R, "cmptmp");
656 // Convert bool 0/1 to double 0.0 or 1.0
657 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
658 "booltmp");
Eric Christopherc0239362014-12-08 18:12:28 +0000659 default:
660 return ErrorV("invalid binary operator");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000661 }
662}
663
664Value *CallExprAST::Codegen() {
665 // Look up the name in the global module table.
Lang Hames1cb54812015-01-16 19:44:46 +0000666 Function *CalleeF = JITHelper->getFunction(Callee);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000667 if (CalleeF == 0)
668 return ErrorV("Unknown function referenced");
Eric Christopherc0239362014-12-08 18:12:28 +0000669
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000670 // If argument mismatch error.
671 if (CalleeF->arg_size() != Args.size())
672 return ErrorV("Incorrect # arguments passed");
673
Eric Christopherc0239362014-12-08 18:12:28 +0000674 std::vector<Value *> ArgsV;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000675 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
676 ArgsV.push_back(Args[i]->Codegen());
Eric Christopherc0239362014-12-08 18:12:28 +0000677 if (ArgsV.back() == 0)
678 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000679 }
Eric Christopherc0239362014-12-08 18:12:28 +0000680
Francois Pichetc5d10502011-07-15 10:59:52 +0000681 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000682}
683
684Function *PrototypeAST::Codegen() {
685 // Make the function type: double(double,double) etc.
Eric Christopherc0239362014-12-08 18:12:28 +0000686 std::vector<Type *> Doubles(Args.size(),
687 Type::getDoubleTy(getGlobalContext()));
688 FunctionType *FT =
689 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
690
Lang Hames1cb54812015-01-16 19:44:46 +0000691 std::string FnName = MakeLegalFunctionName(Name);
692
693 Module *M = JITHelper->getModuleForNewFunction();
694
Lang Hames37679192015-01-16 21:42:07 +0000695 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
Eric Christopherc0239362014-12-08 18:12:28 +0000696
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000697 // If F conflicted, there was already something named 'Name'. If it has a
698 // body, don't allow redefinition or reextern.
Lang Hames1cb54812015-01-16 19:44:46 +0000699 if (F->getName() != FnName) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000700 // Delete the one we just made and get the existing one.
701 F->eraseFromParent();
Lang Hames37679192015-01-16 21:42:07 +0000702 F = JITHelper->getFunction(Name);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000703 // If F already has a body, reject this.
704 if (!F->empty()) {
705 ErrorF("redefinition of function");
706 return 0;
707 }
Eric Christopherc0239362014-12-08 18:12:28 +0000708
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000709 // If F took a different number of args, reject.
710 if (F->arg_size() != Args.size()) {
711 ErrorF("redefinition of function with different # args");
712 return 0;
713 }
714 }
Eric Christopherc0239362014-12-08 18:12:28 +0000715
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000716 // Set names for all arguments.
717 unsigned Idx = 0;
718 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
719 ++AI, ++Idx) {
720 AI->setName(Args[Idx]);
Eric Christopherc0239362014-12-08 18:12:28 +0000721
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000722 // Add arguments to variable symbol table.
723 NamedValues[Args[Idx]] = AI;
724 }
Eric Christopherc0239362014-12-08 18:12:28 +0000725
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000726 return F;
727}
728
729Function *FunctionAST::Codegen() {
730 NamedValues.clear();
Eric Christopherc0239362014-12-08 18:12:28 +0000731
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000732 Function *TheFunction = Proto->Codegen();
733 if (TheFunction == 0)
734 return 0;
Eric Christopherc0239362014-12-08 18:12:28 +0000735
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000736 // Create a new basic block to start insertion into.
737 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
738 Builder.SetInsertPoint(BB);
Eric Christopherc0239362014-12-08 18:12:28 +0000739
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000740 if (Value *RetVal = Body->Codegen()) {
741 // Finish off the function.
742 Builder.CreateRet(RetVal);
743
744 // Validate the generated code, checking for consistency.
745 verifyFunction(*TheFunction);
746
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000747 return TheFunction;
748 }
Eric Christopherc0239362014-12-08 18:12:28 +0000749
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000750 // Error reading body, remove function.
751 TheFunction->eraseFromParent();
752 return 0;
753}
754
755//===----------------------------------------------------------------------===//
756// Top-Level parsing and JIT Driver
757//===----------------------------------------------------------------------===//
758
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000759static void HandleDefinition() {
760 if (FunctionAST *F = ParseDefinition()) {
761 if (Function *LF = F->Codegen()) {
762 fprintf(stderr, "Read function definition:");
763 LF->dump();
764 }
765 } else {
766 // Skip token for error recovery.
767 getNextToken();
768 }
769}
770
771static void HandleExtern() {
772 if (PrototypeAST *P = ParseExtern()) {
773 if (Function *F = P->Codegen()) {
774 fprintf(stderr, "Read extern: ");
775 F->dump();
776 }
777 } else {
778 // Skip token for error recovery.
779 getNextToken();
780 }
781}
782
783static void HandleTopLevelExpression() {
784 // Evaluate a top-level expression into an anonymous function.
785 if (FunctionAST *F = ParseTopLevelExpr()) {
786 if (Function *LF = F->Codegen()) {
787 // JIT the function, returning a function pointer.
Lang Hames1cb54812015-01-16 19:44:46 +0000788 void *FPtr = JITHelper->getPointerToFunction(LF);
Eric Christopherc0239362014-12-08 18:12:28 +0000789
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000790 // Cast it to the right type (takes no arguments, returns a double) so we
791 // can call it as a native function.
792 double (*FP)() = (double (*)())(intptr_t)FPtr;
793 fprintf(stderr, "Evaluated to %f\n", FP());
794 }
795 } else {
796 // Skip token for error recovery.
797 getNextToken();
798 }
799}
800
801/// top ::= definition | external | expression | ';'
802static void MainLoop() {
803 while (1) {
804 fprintf(stderr, "ready> ");
805 switch (CurTok) {
Eric Christopherc0239362014-12-08 18:12:28 +0000806 case tok_eof:
807 return;
808 case ';':
809 getNextToken();
810 break; // ignore top-level semicolons.
811 case tok_def:
812 HandleDefinition();
813 break;
814 case tok_extern:
815 HandleExtern();
816 break;
817 default:
818 HandleTopLevelExpression();
819 break;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000820 }
821 }
822}
823
824//===----------------------------------------------------------------------===//
825// "Library" functions that can be "extern'd" from user code.
826//===----------------------------------------------------------------------===//
827
828/// putchard - putchar that takes a double and returns 0.
Eric Christopherc0239362014-12-08 18:12:28 +0000829extern "C" double putchard(double X) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000830 putchar((char)X);
831 return 0;
832}
833
834//===----------------------------------------------------------------------===//
835// Main driver code.
836//===----------------------------------------------------------------------===//
837
838int main() {
839 InitializeNativeTarget();
Eric Christopher1b74b652014-12-08 18:00:38 +0000840 InitializeNativeTargetAsmPrinter();
841 InitializeNativeTargetAsmParser();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000842 LLVMContext &Context = getGlobalContext();
Lang Hames1cb54812015-01-16 19:44:46 +0000843 JITHelper = new MCJITHelper(Context);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000844
845 // Install standard binary operators.
846 // 1 is lowest precedence.
847 BinopPrecedence['<'] = 10;
848 BinopPrecedence['+'] = 20;
849 BinopPrecedence['-'] = 20;
Eric Christopherc0239362014-12-08 18:12:28 +0000850 BinopPrecedence['*'] = 40; // highest.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000851
852 // Prime the first token.
853 fprintf(stderr, "ready> ");
854 getNextToken();
855
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000856 // Run the main "interpreter loop" now.
857 MainLoop();
858
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000859 // Print out all of the generated code.
Lang Hames1cb54812015-01-16 19:44:46 +0000860 JITHelper->dump();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000861
862 return 0;
863}