blob: ae52db82db3f848e1cb2c74fc4d7ddccb3bb7a12 [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) {}
110 virtual Value *Codegen();
111};
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) {}
119 virtual Value *Codegen();
120};
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) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000130 virtual Value *Codegen();
131};
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) {}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000141 virtual Value *Codegen();
142};
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 {
450 HelpingMemoryManager(const HelpingMemoryManager &) LLVM_DELETED_FUNCTION;
451 void operator=(const HelpingMemoryManager &) LLVM_DELETED_FUNCTION;
Lang Hames1cb54812015-01-16 19:44:46 +0000452
453public:
454 HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
455 virtual ~HelpingMemoryManager() {}
456
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.
461 virtual 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
559 FunctionPassManager *FPM = new FunctionPassManager(OpenModule);
560
561 // Set up the optimizer pipeline. Start with registering info about how the
562 // target lays out data structures.
563 OpenModule->setDataLayout(NewEngine->getDataLayout());
564 FPM->add(new DataLayoutPass());
565 // Provide basic AliasAnalysis support for GVN.
566 FPM->add(createBasicAliasAnalysisPass());
567 // Promote allocas to registers.
568 FPM->add(createPromoteMemoryToRegisterPass());
569 // Do simple "peephole" optimizations and bit-twiddling optzns.
570 FPM->add(createInstructionCombiningPass());
571 // Reassociate expressions.
572 FPM->add(createReassociatePass());
573 // Eliminate Common SubExpressions.
574 FPM->add(createGVNPass());
575 // Simplify the control flow graph (deleting unreachable blocks, etc).
576 FPM->add(createCFGSimplificationPass());
577 FPM->doInitialization();
578
579 // For each function in the module
580 Module::iterator it;
581 Module::iterator end = OpenModule->end();
582 for (it = OpenModule->begin(); it != end; ++it) {
583 // Run the FPM on this function
584 FPM->run(*it);
585 }
586
587 // We don't need this anymore
588 delete FPM;
589
590 OpenModule = NULL;
591 Engines.push_back(NewEngine);
592 NewEngine->finalizeObject();
593 return NewEngine->getPointerToFunction(F);
594 }
595 return NULL;
596}
597
Lang Hames37679192015-01-16 21:42:07 +0000598void *MCJITHelper::getSymbolAddress(const std::string &Name) {
Lang Hames1cb54812015-01-16 19:44:46 +0000599 // Look for the symbol in each of our execution engines.
600 EngineVector::iterator begin = Engines.begin();
601 EngineVector::iterator end = Engines.end();
602 EngineVector::iterator it;
603 for (it = begin; it != end; ++it) {
604 uint64_t FAddr = (*it)->getFunctionAddress(Name);
605 if (FAddr) {
Lang Hames37679192015-01-16 21:42:07 +0000606 return (void *)FAddr;
Lang Hames1cb54812015-01-16 19:44:46 +0000607 }
608 }
609 return NULL;
610}
611
Lang Hames37679192015-01-16 21:42:07 +0000612void MCJITHelper::dump() {
Lang Hames1cb54812015-01-16 19:44:46 +0000613 ModuleVector::iterator begin = Modules.begin();
614 ModuleVector::iterator end = Modules.end();
615 ModuleVector::iterator it;
616 for (it = begin; it != end; ++it)
617 (*it)->dump();
618}
619//===----------------------------------------------------------------------===//
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000620// Code Generation
621//===----------------------------------------------------------------------===//
622
Lang Hames1cb54812015-01-16 19:44:46 +0000623static MCJITHelper *JITHelper;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000624static IRBuilder<> Builder(getGlobalContext());
Eric Christopherc0239362014-12-08 18:12:28 +0000625static std::map<std::string, Value *> NamedValues;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000626
Eric Christopherc0239362014-12-08 18:12:28 +0000627Value *ErrorV(const char *Str) {
628 Error(Str);
629 return 0;
630}
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000631
632Value *NumberExprAST::Codegen() {
633 return ConstantFP::get(getGlobalContext(), APFloat(Val));
634}
635
636Value *VariableExprAST::Codegen() {
637 // Look this variable up in the function.
638 Value *V = NamedValues[Name];
639 return V ? V : ErrorV("Unknown variable name");
640}
641
642Value *BinaryExprAST::Codegen() {
643 Value *L = LHS->Codegen();
644 Value *R = RHS->Codegen();
Eric Christopherc0239362014-12-08 18:12:28 +0000645 if (L == 0 || R == 0)
646 return 0;
647
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000648 switch (Op) {
Eric Christopherc0239362014-12-08 18:12:28 +0000649 case '+':
650 return Builder.CreateFAdd(L, R, "addtmp");
651 case '-':
652 return Builder.CreateFSub(L, R, "subtmp");
653 case '*':
654 return Builder.CreateFMul(L, R, "multmp");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000655 case '<':
656 L = Builder.CreateFCmpULT(L, R, "cmptmp");
657 // Convert bool 0/1 to double 0.0 or 1.0
658 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
659 "booltmp");
Eric Christopherc0239362014-12-08 18:12:28 +0000660 default:
661 return ErrorV("invalid binary operator");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000662 }
663}
664
665Value *CallExprAST::Codegen() {
666 // Look up the name in the global module table.
Lang Hames1cb54812015-01-16 19:44:46 +0000667 Function *CalleeF = JITHelper->getFunction(Callee);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000668 if (CalleeF == 0)
669 return ErrorV("Unknown function referenced");
Eric Christopherc0239362014-12-08 18:12:28 +0000670
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000671 // If argument mismatch error.
672 if (CalleeF->arg_size() != Args.size())
673 return ErrorV("Incorrect # arguments passed");
674
Eric Christopherc0239362014-12-08 18:12:28 +0000675 std::vector<Value *> ArgsV;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000676 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
677 ArgsV.push_back(Args[i]->Codegen());
Eric Christopherc0239362014-12-08 18:12:28 +0000678 if (ArgsV.back() == 0)
679 return 0;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000680 }
Eric Christopherc0239362014-12-08 18:12:28 +0000681
Francois Pichetc5d10502011-07-15 10:59:52 +0000682 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000683}
684
685Function *PrototypeAST::Codegen() {
686 // Make the function type: double(double,double) etc.
Eric Christopherc0239362014-12-08 18:12:28 +0000687 std::vector<Type *> Doubles(Args.size(),
688 Type::getDoubleTy(getGlobalContext()));
689 FunctionType *FT =
690 FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
691
Lang Hames1cb54812015-01-16 19:44:46 +0000692 std::string FnName = MakeLegalFunctionName(Name);
693
694 Module *M = JITHelper->getModuleForNewFunction();
695
Lang Hames37679192015-01-16 21:42:07 +0000696 Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
Eric Christopherc0239362014-12-08 18:12:28 +0000697
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000698 // If F conflicted, there was already something named 'Name'. If it has a
699 // body, don't allow redefinition or reextern.
Lang Hames1cb54812015-01-16 19:44:46 +0000700 if (F->getName() != FnName) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000701 // Delete the one we just made and get the existing one.
702 F->eraseFromParent();
Lang Hames37679192015-01-16 21:42:07 +0000703 F = JITHelper->getFunction(Name);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000704 // If F already has a body, reject this.
705 if (!F->empty()) {
706 ErrorF("redefinition of function");
707 return 0;
708 }
Eric Christopherc0239362014-12-08 18:12:28 +0000709
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000710 // If F took a different number of args, reject.
711 if (F->arg_size() != Args.size()) {
712 ErrorF("redefinition of function with different # args");
713 return 0;
714 }
715 }
Eric Christopherc0239362014-12-08 18:12:28 +0000716
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000717 // Set names for all arguments.
718 unsigned Idx = 0;
719 for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
720 ++AI, ++Idx) {
721 AI->setName(Args[Idx]);
Eric Christopherc0239362014-12-08 18:12:28 +0000722
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000723 // Add arguments to variable symbol table.
724 NamedValues[Args[Idx]] = AI;
725 }
Eric Christopherc0239362014-12-08 18:12:28 +0000726
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000727 return F;
728}
729
730Function *FunctionAST::Codegen() {
731 NamedValues.clear();
Eric Christopherc0239362014-12-08 18:12:28 +0000732
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000733 Function *TheFunction = Proto->Codegen();
734 if (TheFunction == 0)
735 return 0;
Eric Christopherc0239362014-12-08 18:12:28 +0000736
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000737 // Create a new basic block to start insertion into.
738 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
739 Builder.SetInsertPoint(BB);
Eric Christopherc0239362014-12-08 18:12:28 +0000740
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000741 if (Value *RetVal = Body->Codegen()) {
742 // Finish off the function.
743 Builder.CreateRet(RetVal);
744
745 // Validate the generated code, checking for consistency.
746 verifyFunction(*TheFunction);
747
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000748 return TheFunction;
749 }
Eric Christopherc0239362014-12-08 18:12:28 +0000750
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000751 // Error reading body, remove function.
752 TheFunction->eraseFromParent();
753 return 0;
754}
755
756//===----------------------------------------------------------------------===//
757// Top-Level parsing and JIT Driver
758//===----------------------------------------------------------------------===//
759
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000760static void HandleDefinition() {
761 if (FunctionAST *F = ParseDefinition()) {
762 if (Function *LF = F->Codegen()) {
763 fprintf(stderr, "Read function definition:");
764 LF->dump();
765 }
766 } else {
767 // Skip token for error recovery.
768 getNextToken();
769 }
770}
771
772static void HandleExtern() {
773 if (PrototypeAST *P = ParseExtern()) {
774 if (Function *F = P->Codegen()) {
775 fprintf(stderr, "Read extern: ");
776 F->dump();
777 }
778 } else {
779 // Skip token for error recovery.
780 getNextToken();
781 }
782}
783
784static void HandleTopLevelExpression() {
785 // Evaluate a top-level expression into an anonymous function.
786 if (FunctionAST *F = ParseTopLevelExpr()) {
787 if (Function *LF = F->Codegen()) {
788 // JIT the function, returning a function pointer.
Lang Hames1cb54812015-01-16 19:44:46 +0000789 void *FPtr = JITHelper->getPointerToFunction(LF);
Eric Christopherc0239362014-12-08 18:12:28 +0000790
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000791 // Cast it to the right type (takes no arguments, returns a double) so we
792 // can call it as a native function.
793 double (*FP)() = (double (*)())(intptr_t)FPtr;
794 fprintf(stderr, "Evaluated to %f\n", FP());
795 }
796 } else {
797 // Skip token for error recovery.
798 getNextToken();
799 }
800}
801
802/// top ::= definition | external | expression | ';'
803static void MainLoop() {
804 while (1) {
805 fprintf(stderr, "ready> ");
806 switch (CurTok) {
Eric Christopherc0239362014-12-08 18:12:28 +0000807 case tok_eof:
808 return;
809 case ';':
810 getNextToken();
811 break; // ignore top-level semicolons.
812 case tok_def:
813 HandleDefinition();
814 break;
815 case tok_extern:
816 HandleExtern();
817 break;
818 default:
819 HandleTopLevelExpression();
820 break;
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000821 }
822 }
823}
824
825//===----------------------------------------------------------------------===//
826// "Library" functions that can be "extern'd" from user code.
827//===----------------------------------------------------------------------===//
828
829/// putchard - putchar that takes a double and returns 0.
Eric Christopherc0239362014-12-08 18:12:28 +0000830extern "C" double putchard(double X) {
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000831 putchar((char)X);
832 return 0;
833}
834
835//===----------------------------------------------------------------------===//
836// Main driver code.
837//===----------------------------------------------------------------------===//
838
839int main() {
840 InitializeNativeTarget();
Eric Christopher1b74b652014-12-08 18:00:38 +0000841 InitializeNativeTargetAsmPrinter();
842 InitializeNativeTargetAsmParser();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000843 LLVMContext &Context = getGlobalContext();
Lang Hames1cb54812015-01-16 19:44:46 +0000844 JITHelper = new MCJITHelper(Context);
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000845
846 // Install standard binary operators.
847 // 1 is lowest precedence.
848 BinopPrecedence['<'] = 10;
849 BinopPrecedence['+'] = 20;
850 BinopPrecedence['-'] = 20;
Eric Christopherc0239362014-12-08 18:12:28 +0000851 BinopPrecedence['*'] = 40; // highest.
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000852
853 // Prime the first token.
854 fprintf(stderr, "ready> ");
855 getNextToken();
856
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000857 // Run the main "interpreter loop" now.
858 MainLoop();
859
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000860 // Print out all of the generated code.
Lang Hames1cb54812015-01-16 19:44:46 +0000861 JITHelper->dump();
Erick Tryzelaar21e83ea2009-09-22 21:15:19 +0000862
863 return 0;
864}