#include "llvm/Analysis/Passes.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/MCJIT.h"
#include "llvm/ExecutionEngine/ObjectCache.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar.h"
#include <cctype>
#include <cstdio>
#include <map>
#include <string>
#include <vector>
using namespace llvm;
namespace {
cl::opt<std::string>
InputIR("input-IR",
cl::desc("Specify the name of an IR file to load for function definitions"),
cl::value_desc("input IR file name"));
cl::opt<bool>
VerboseOutput("verbose",
cl::desc("Enable verbose output (results, IR, etc.) to stderr"),
cl::init(false));
cl::opt<bool>
SuppressPrompts("suppress-prompts",
cl::desc("Disable printing the 'ready' prompt"),
cl::init(false));
cl::opt<bool>
DumpModulesOnExit("dump-modules",
cl::desc("Dump IR from modules to stderr on shutdown"),
cl::init(false));
cl::opt<bool> EnableLazyCompilation(
"enable-lazy-compilation", cl::desc("Enable lazy compilation when using the MCJIT engine"),
cl::init(true));
cl::opt<bool> UseObjectCache(
"use-object-cache", cl::desc("Enable use of the MCJIT object caching"),
cl::init(false));
}
enum Token {
tok_eof = -1,
tok_def = -2, tok_extern = -3,
tok_identifier = -4, tok_number = -5,
tok_if = -6, tok_then = -7, tok_else = -8,
tok_for = -9, tok_in = -10,
tok_binary = -11, tok_unary = -12,
tok_var = -13
};
static std::string IdentifierStr; static double NumVal;
static int gettok() {
static int LastChar = ' ';
while (isspace(LastChar))
LastChar = getchar();
if (isalpha(LastChar)) { IdentifierStr = LastChar;
while (isalnum((LastChar = getchar())))
IdentifierStr += LastChar;
if (IdentifierStr == "def") return tok_def;
if (IdentifierStr == "extern") return tok_extern;
if (IdentifierStr == "if") return tok_if;
if (IdentifierStr == "then") return tok_then;
if (IdentifierStr == "else") return tok_else;
if (IdentifierStr == "for") return tok_for;
if (IdentifierStr == "in") return tok_in;
if (IdentifierStr == "binary") return tok_binary;
if (IdentifierStr == "unary") return tok_unary;
if (IdentifierStr == "var") return tok_var;
return tok_identifier;
}
if (isdigit(LastChar) || LastChar == '.') { std::string NumStr;
do {
NumStr += LastChar;
LastChar = getchar();
} while (isdigit(LastChar) || LastChar == '.');
NumVal = strtod(NumStr.c_str(), 0);
return tok_number;
}
if (LastChar == '#') {
do LastChar = getchar();
while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
if (LastChar != EOF)
return gettok();
}
if (LastChar == EOF)
return tok_eof;
int ThisChar = LastChar;
LastChar = getchar();
return ThisChar;
}
class ExprAST {
public:
virtual ~ExprAST() {}
virtual Value *Codegen() = 0;
};
class NumberExprAST : public ExprAST {
double Val;
public:
NumberExprAST(double val) : Val(val) {}
virtual Value *Codegen();
};
class VariableExprAST : public ExprAST {
std::string Name;
public:
VariableExprAST(const std::string &name) : Name(name) {}
const std::string &getName() const { return Name; }
virtual Value *Codegen();
};
class UnaryExprAST : public ExprAST {
char Opcode;
ExprAST *Operand;
public:
UnaryExprAST(char opcode, ExprAST *operand)
: Opcode(opcode), Operand(operand) {}
virtual Value *Codegen();
};
class BinaryExprAST : public ExprAST {
char Op;
ExprAST *LHS, *RHS;
public:
BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
: Op(op), LHS(lhs), RHS(rhs) {}
virtual Value *Codegen();
};
class CallExprAST : public ExprAST {
std::string Callee;
std::vector<ExprAST*> Args;
public:
CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
: Callee(callee), Args(args) {}
virtual Value *Codegen();
};
class IfExprAST : public ExprAST {
ExprAST *Cond, *Then, *Else;
public:
IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
: Cond(cond), Then(then), Else(_else) {}
virtual Value *Codegen();
};
class ForExprAST : public ExprAST {
std::string VarName;
ExprAST *Start, *End, *Step, *Body;
public:
ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
ExprAST *step, ExprAST *body)
: VarName(varname), Start(start), End(end), Step(step), Body(body) {}
virtual Value *Codegen();
};
class VarExprAST : public ExprAST {
std::vector<std::pair<std::string, ExprAST*> > VarNames;
ExprAST *Body;
public:
VarExprAST(const std::vector<std::pair<std::string, ExprAST*> > &varnames,
ExprAST *body)
: VarNames(varnames), Body(body) {}
virtual Value *Codegen();
};
class PrototypeAST {
std::string Name;
std::vector<std::string> Args;
bool isOperator;
unsigned Precedence; public:
PrototypeAST(const std::string &name, const std::vector<std::string> &args,
bool isoperator = false, unsigned prec = 0)
: Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
bool isUnaryOp() const { return isOperator && Args.size() == 1; }
bool isBinaryOp() const { return isOperator && Args.size() == 2; }
char getOperatorName() const {
assert(isUnaryOp() || isBinaryOp());
return Name[Name.size()-1];
}
unsigned getBinaryPrecedence() const { return Precedence; }
Function *Codegen();
void CreateArgumentAllocas(Function *F);
};
class FunctionAST {
PrototypeAST *Proto;
ExprAST *Body;
public:
FunctionAST(PrototypeAST *proto, ExprAST *body)
: Proto(proto), Body(body) {}
Function *Codegen();
};
static int CurTok;
static int getNextToken() {
return CurTok = gettok();
}
static std::map<char, int> BinopPrecedence;
static int GetTokPrecedence() {
if (!isascii(CurTok))
return -1;
int TokPrec = BinopPrecedence[CurTok];
if (TokPrec <= 0) return -1;
return TokPrec;
}
ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
static ExprAST *ParseExpression();
static ExprAST *ParseIdentifierExpr() {
std::string IdName = IdentifierStr;
getNextToken();
if (CurTok != '(') return new VariableExprAST(IdName);
getNextToken(); std::vector<ExprAST*> Args;
if (CurTok != ')') {
while (1) {
ExprAST *Arg = ParseExpression();
if (!Arg) return 0;
Args.push_back(Arg);
if (CurTok == ')') break;
if (CurTok != ',')
return Error("Expected ')' or ',' in argument list");
getNextToken();
}
}
getNextToken();
return new CallExprAST(IdName, Args);
}
static ExprAST *ParseNumberExpr() {
ExprAST *Result = new NumberExprAST(NumVal);
getNextToken(); return Result;
}
static ExprAST *ParseParenExpr() {
getNextToken(); ExprAST *V = ParseExpression();
if (!V) return 0;
if (CurTok != ')')
return Error("expected ')'");
getNextToken(); return V;
}
static ExprAST *ParseIfExpr() {
getNextToken();
ExprAST *Cond = ParseExpression();
if (!Cond) return 0;
if (CurTok != tok_then)
return Error("expected then");
getNextToken();
ExprAST *Then = ParseExpression();
if (Then == 0) return 0;
if (CurTok != tok_else)
return Error("expected else");
getNextToken();
ExprAST *Else = ParseExpression();
if (!Else) return 0;
return new IfExprAST(Cond, Then, Else);
}
static ExprAST *ParseForExpr() {
getNextToken();
if (CurTok != tok_identifier)
return Error("expected identifier after for");
std::string IdName = IdentifierStr;
getNextToken();
if (CurTok != '=')
return Error("expected '=' after for");
getNextToken();
ExprAST *Start = ParseExpression();
if (Start == 0) return 0;
if (CurTok != ',')
return Error("expected ',' after for start value");
getNextToken();
ExprAST *End = ParseExpression();
if (End == 0) return 0;
ExprAST *Step = 0;
if (CurTok == ',') {
getNextToken();
Step = ParseExpression();
if (Step == 0) return 0;
}
if (CurTok != tok_in)
return Error("expected 'in' after for");
getNextToken();
ExprAST *Body = ParseExpression();
if (Body == 0) return 0;
return new ForExprAST(IdName, Start, End, Step, Body);
}
static ExprAST *ParseVarExpr() {
getNextToken();
std::vector<std::pair<std::string, ExprAST*> > VarNames;
if (CurTok != tok_identifier)
return Error("expected identifier after var");
while (1) {
std::string Name = IdentifierStr;
getNextToken();
ExprAST *Init = 0;
if (CurTok == '=') {
getNextToken();
Init = ParseExpression();
if (Init == 0) return 0;
}
VarNames.push_back(std::make_pair(Name, Init));
if (CurTok != ',') break;
getNextToken();
if (CurTok != tok_identifier)
return Error("expected identifier list after var");
}
if (CurTok != tok_in)
return Error("expected 'in' keyword after 'var'");
getNextToken();
ExprAST *Body = ParseExpression();
if (Body == 0) return 0;
return new VarExprAST(VarNames, Body);
}
static ExprAST *ParsePrimary() {
switch (CurTok) {
default: return Error("unknown token when expecting an expression");
case tok_identifier: return ParseIdentifierExpr();
case tok_number: return ParseNumberExpr();
case '(': return ParseParenExpr();
case tok_if: return ParseIfExpr();
case tok_for: return ParseForExpr();
case tok_var: return ParseVarExpr();
}
}
static ExprAST *ParseUnary() {
if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
return ParsePrimary();
int Opc = CurTok;
getNextToken();
if (ExprAST *Operand = ParseUnary())
return new UnaryExprAST(Opc, Operand);
return 0;
}
static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
while (1) {
int TokPrec = GetTokPrecedence();
if (TokPrec < ExprPrec)
return LHS;
int BinOp = CurTok;
getNextToken();
ExprAST *RHS = ParseUnary();
if (!RHS) return 0;
int NextPrec = GetTokPrecedence();
if (TokPrec < NextPrec) {
RHS = ParseBinOpRHS(TokPrec+1, RHS);
if (RHS == 0) return 0;
}
LHS = new BinaryExprAST(BinOp, LHS, RHS);
}
}
static ExprAST *ParseExpression() {
ExprAST *LHS = ParseUnary();
if (!LHS) return 0;
return ParseBinOpRHS(0, LHS);
}
static PrototypeAST *ParsePrototype() {
std::string FnName;
unsigned Kind = 0; unsigned BinaryPrecedence = 30;
switch (CurTok) {
default:
return ErrorP("Expected function name in prototype");
case tok_identifier:
FnName = IdentifierStr;
Kind = 0;
getNextToken();
break;
case tok_unary:
getNextToken();
if (!isascii(CurTok))
return ErrorP("Expected unary operator");
FnName = "unary";
FnName += (char)CurTok;
Kind = 1;
getNextToken();
break;
case tok_binary:
getNextToken();
if (!isascii(CurTok))
return ErrorP("Expected binary operator");
FnName = "binary";
FnName += (char)CurTok;
Kind = 2;
getNextToken();
if (CurTok == tok_number) {
if (NumVal < 1 || NumVal > 100)
return ErrorP("Invalid precedecnce: must be 1..100");
BinaryPrecedence = (unsigned)NumVal;
getNextToken();
}
break;
}
if (CurTok != '(')
return ErrorP("Expected '(' in prototype");
std::vector<std::string> ArgNames;
while (getNextToken() == tok_identifier)
ArgNames.push_back(IdentifierStr);
if (CurTok != ')')
return ErrorP("Expected ')' in prototype");
getNextToken();
if (Kind && ArgNames.size() != Kind)
return ErrorP("Invalid number of operands for operator");
return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
}
static FunctionAST *ParseDefinition() {
getNextToken(); PrototypeAST *Proto = ParsePrototype();
if (Proto == 0) return 0;
if (ExprAST *E = ParseExpression())
return new FunctionAST(Proto, E);
return 0;
}
static FunctionAST *ParseTopLevelExpr() {
if (ExprAST *E = ParseExpression()) {
PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
return new FunctionAST(Proto, E);
}
return 0;
}
static PrototypeAST *ParseExtern() {
getNextToken(); return ParsePrototype();
}
std::string GenerateUniqueName(const char *root)
{
static int i = 0;
char s[16];
sprintf(s, "%s%d", root, i++);
std::string S = s;
return S;
}
std::string MakeLegalFunctionName(std::string Name)
{
std::string NewName;
if (!Name.length())
return GenerateUniqueName("anon_func_");
NewName = Name;
if (NewName.find_first_of("0123456789") == 0) {
NewName.insert(0, 1, 'n');
}
std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
size_t pos;
while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) {
char old_c = NewName.at(pos);
char new_str[16];
sprintf(new_str, "%d", (int)old_c);
NewName = NewName.replace(pos, 1, new_str);
}
return NewName;
}
class MCJITObjectCache : public ObjectCache {
public:
MCJITObjectCache() {
sys::fs::current_path(CacheDir);
sys::path::append(CacheDir, "toy_object_cache");
}
virtual ~MCJITObjectCache() {
}
virtual void notifyObjectCompiled(const Module *M, const MemoryBuffer *Obj) {
const std::string ModuleID = M->getModuleIdentifier();
if (0 == ModuleID.compare(0, 3, "IR:")) {
std::string IRFileName = ModuleID.substr(3);
SmallString<128>IRCacheFile = CacheDir;
sys::path::append(IRCacheFile, IRFileName);
if (!sys::fs::exists(CacheDir.str()) && sys::fs::create_directory(CacheDir.str())) {
fprintf(stderr, "Unable to create cache directory\n");
return;
}
std::string ErrStr;
raw_fd_ostream IRObjectFile(IRCacheFile.c_str(), ErrStr, raw_fd_ostream::F_Binary);
IRObjectFile << Obj->getBuffer();
}
}
virtual MemoryBuffer* getObject(const Module* M) {
const std::string ModuleID = M->getModuleIdentifier();
if (0 == ModuleID.compare(0, 3, "IR:")) {
std::string IRFileName = ModuleID.substr(3);
SmallString<128> IRCacheFile = CacheDir;
sys::path::append(IRCacheFile, IRFileName);
if (!sys::fs::exists(IRCacheFile.str())) {
return NULL;
}
std::unique_ptr<MemoryBuffer> IRObjectBuffer;
MemoryBuffer::getFile(IRCacheFile.c_str(), IRObjectBuffer, -1, false);
return MemoryBuffer::getMemBufferCopy(IRObjectBuffer->getBuffer());
}
return NULL;
}
private:
SmallString<128> CacheDir;
};
Module* parseInputIR(std::string InputFile, LLVMContext &Context) {
SMDiagnostic Err;
Module *M = ParseIRFile(InputFile, Err, Context);
if (!M) {
Err.print("IR parsing failed: ", errs());
return NULL;
}
char ModID[256];
sprintf(ModID, "IR:%s", InputFile.c_str());
M->setModuleIdentifier(ModID);
return M;
}
class BaseHelper
{
public:
BaseHelper() {}
virtual ~BaseHelper() {}
virtual Function *getFunction(const std::string FnName) = 0;
virtual Module *getModuleForNewFunction() = 0;
virtual void *getPointerToFunction(Function* F) = 0;
virtual void *getPointerToNamedFunction(const std::string &Name) = 0;
virtual void closeCurrentModule() = 0;
virtual void runFPM(Function &F) = 0;
virtual void dump();
};
class MCJITHelper : public BaseHelper
{
public:
MCJITHelper(LLVMContext& C) : Context(C), CurrentModule(NULL) {
if (!InputIR.empty()) {
Module *M = parseInputIR(InputIR, Context);
Modules.push_back(M);
if (!EnableLazyCompilation)
compileModule(M);
}
}
~MCJITHelper();
Function *getFunction(const std::string FnName);
Module *getModuleForNewFunction();
void *getPointerToFunction(Function* F);
void *getPointerToNamedFunction(const std::string &Name);
void closeCurrentModule();
virtual void runFPM(Function &F) {} void dump();
protected:
ExecutionEngine *compileModule(Module *M);
private:
typedef std::vector<Module*> ModuleVector;
MCJITObjectCache OurObjectCache;
LLVMContext &Context;
ModuleVector Modules;
std::map<Module *, ExecutionEngine *> EngineMap;
Module *CurrentModule;
};
class HelpingMemoryManager : public SectionMemoryManager
{
HelpingMemoryManager(const HelpingMemoryManager&) = delete;
void operator=(const HelpingMemoryManager&) = delete;
public:
HelpingMemoryManager(MCJITHelper *Helper) : MasterHelper(Helper) {}
virtual ~HelpingMemoryManager() {}
virtual void *getPointerToNamedFunction(const std::string &Name,
bool AbortOnFailure = true);
private:
MCJITHelper *MasterHelper;
};
void *HelpingMemoryManager::getPointerToNamedFunction(const std::string &Name,
bool AbortOnFailure)
{
void *pfn = RTDyldMemoryManager::getPointerToNamedFunction(Name, false);
if (pfn)
return pfn;
pfn = MasterHelper->getPointerToNamedFunction(Name);
if (!pfn && AbortOnFailure)
report_fatal_error("Program used external function '" + Name +
"' which could not be resolved!");
return pfn;
}
MCJITHelper::~MCJITHelper()
{
ModuleVector::iterator it, end;
for (it = Modules.begin(), end = Modules.end();
it != end; ++it) {
std::map<Module*, ExecutionEngine*>::iterator mapIt = EngineMap.find(*it);
if (mapIt != EngineMap.end()) {
delete mapIt->second;
} else {
delete *it;
}
}
}
Function *MCJITHelper::getFunction(const std::string FnName) {
ModuleVector::iterator begin = Modules.begin();
ModuleVector::iterator end = Modules.end();
ModuleVector::iterator it;
for (it = begin; it != end; ++it) {
Function *F = (*it)->getFunction(FnName);
if (F) {
if (*it == CurrentModule)
return F;
assert(CurrentModule != NULL);
Function *PF = CurrentModule->getFunction(FnName);
if (PF && !PF->empty()) {
ErrorF("redefinition of function across modules");
return 0;
}
if (!PF)
PF = Function::Create(F->getFunctionType(),
Function::ExternalLinkage,
FnName,
CurrentModule);
return PF;
}
}
return NULL;
}
Module *MCJITHelper::getModuleForNewFunction() {
if (CurrentModule)
return CurrentModule;
std::string ModName = GenerateUniqueName("mcjit_module_");
Module *M = new Module(ModName, Context);
Modules.push_back(M);
CurrentModule = M;
return M;
}
ExecutionEngine *MCJITHelper::compileModule(Module *M) {
assert(EngineMap.find(M) == EngineMap.end());
if (M == CurrentModule)
closeCurrentModule();
std::string ErrStr;
ExecutionEngine *EE = EngineBuilder(M)
.setErrorStr(&ErrStr)
.setMCJITMemoryManager(new HelpingMemoryManager(this))
.create();
if (!EE) {
fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
exit(1);
}
if (UseObjectCache)
EE->setObjectCache(&OurObjectCache);
const std::string ModuleID = M->getModuleIdentifier();
if (0 != ModuleID.compare(0, 3, "IR:")) {
FunctionPassManager *FPM = 0;
FPM = new FunctionPassManager(M);
FPM->add(new DataLayout(*EE->getDataLayout()));
FPM->add(createBasicAliasAnalysisPass());
FPM->add(createPromoteMemoryToRegisterPass());
FPM->add(createInstructionCombiningPass());
FPM->add(createReassociatePass());
FPM->add(createGVNPass());
FPM->add(createCFGSimplificationPass());
FPM->doInitialization();
Module::iterator it;
Module::iterator end = M->end();
for (it = M->begin(); it != end; ++it) {
FPM->run(*it);
}
delete FPM;
}
EE->finalizeObject();
EngineMap[M] = EE;
return EE;
}
void *MCJITHelper::getPointerToFunction(Function* F) {
ModuleVector::iterator begin = Modules.begin();
ModuleVector::iterator end = Modules.end();
ModuleVector::iterator it;
std::string FnName = F->getName();
for (it = begin; it != end; ++it) {
Function *MF = (*it)->getFunction(FnName);
if (MF == F) {
std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it);
if (eeIt != EngineMap.end()) {
void *P = eeIt->second->getPointerToFunction(F);
if (P)
return P;
} else {
ExecutionEngine *EE = compileModule(*it);
void *P = EE->getPointerToFunction(F);
if (P)
return P;
}
}
}
return NULL;
}
void MCJITHelper::closeCurrentModule() {
if (CurrentModule) {
CurrentModule = NULL;
}
}
void *MCJITHelper::getPointerToNamedFunction(const std::string &Name)
{
ModuleVector::iterator begin = Modules.begin();
ModuleVector::iterator end = Modules.end();
ModuleVector::iterator it;
for (it = begin; it != end; ++it) {
Function *F = (*it)->getFunction(Name);
if (F && !F->empty()) {
std::map<Module*, ExecutionEngine*>::iterator eeIt = EngineMap.find(*it);
if (eeIt != EngineMap.end()) {
void *P = eeIt->second->getPointerToFunction(F);
if (P)
return P;
} else {
ExecutionEngine *EE = compileModule(*it);
void *P = EE->getPointerToFunction(F);
if (P)
return P;
}
}
}
return NULL;
}
void MCJITHelper::dump()
{
ModuleVector::iterator begin = Modules.begin();
ModuleVector::iterator end = Modules.end();
ModuleVector::iterator it;
for (it = begin; it != end; ++it)
(*it)->dump();
}
static BaseHelper *TheHelper;
static LLVMContext TheContext;
static IRBuilder<> Builder(TheContext);
static std::map<std::string, AllocaInst*> NamedValues;
Value *ErrorV(const char *Str) { Error(Str); return 0; }
static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
const std::string &VarName) {
IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
TheFunction->getEntryBlock().begin());
return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), 0, VarName.c_str());
}
Value *NumberExprAST::Codegen() {
return ConstantFP::get(TheContext, APFloat(Val));
}
Value *VariableExprAST::Codegen() {
Value *V = NamedValues[Name];
if (V == 0) return ErrorV("Unknown variable name");
return Builder.CreateLoad(V, Name.c_str());
}
Value *UnaryExprAST::Codegen() {
Value *OperandV = Operand->Codegen();
if (OperandV == 0) return 0;
Function *F;
F = TheHelper->getFunction(
MakeLegalFunctionName(std::string("unary") + Opcode));
if (F == 0)
return ErrorV("Unknown unary operator");
return Builder.CreateCall(F, OperandV, "unop");
}
Value *BinaryExprAST::Codegen() {
if (Op == '=') {
VariableExprAST *LHSE = static_cast<VariableExprAST*>(LHS);
if (!LHSE)
return ErrorV("destination of '=' must be a variable");
Value *Val = RHS->Codegen();
if (Val == 0) return 0;
Value *Variable = NamedValues[LHSE->getName()];
if (Variable == 0) return ErrorV("Unknown variable name");
Builder.CreateStore(Val, Variable);
return Val;
}
Value *L = LHS->Codegen();
Value *R = RHS->Codegen();
if (L == 0 || R == 0) return 0;
switch (Op) {
case '+': return Builder.CreateFAdd(L, R, "addtmp");
case '-': return Builder.CreateFSub(L, R, "subtmp");
case '*': return Builder.CreateFMul(L, R, "multmp");
case '/': return Builder.CreateFDiv(L, R, "divtmp");
case '<':
L = Builder.CreateFCmpULT(L, R, "cmptmp");
return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
default: break;
}
Function *F;
F = TheHelper->getFunction(MakeLegalFunctionName(std::string("binary")+Op));
assert(F && "binary operator not found!");
Value *Ops[] = { L, R };
return Builder.CreateCall(F, Ops, "binop");
}
Value *CallExprAST::Codegen() {
Function *CalleeF = TheHelper->getFunction(Callee);
if (CalleeF == 0) {
char error_str[64];
sprintf(error_str, "Unknown function referenced %s", Callee.c_str());
return ErrorV(error_str);
}
if (CalleeF->arg_size() != Args.size())
return ErrorV("Incorrect # arguments passed");
std::vector<Value*> ArgsV;
for (unsigned i = 0, e = Args.size(); i != e; ++i) {
ArgsV.push_back(Args[i]->Codegen());
if (ArgsV.back() == 0) return 0;
}
return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
}
Value *IfExprAST::Codegen() {
Value *CondV = Cond->Codegen();
if (CondV == 0) return 0;
CondV = Builder.CreateFCmpONE(
CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
Function *TheFunction = Builder.GetInsertBlock()->getParent();
BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
Builder.CreateCondBr(CondV, ThenBB, ElseBB);
Builder.SetInsertPoint(ThenBB);
Value *ThenV = Then->Codegen();
if (ThenV == 0) return 0;
Builder.CreateBr(MergeBB);
ThenBB = Builder.GetInsertBlock();
TheFunction->getBasicBlockList().push_back(ElseBB);
Builder.SetInsertPoint(ElseBB);
Value *ElseV = Else->Codegen();
if (ElseV == 0) return 0;
Builder.CreateBr(MergeBB);
ElseBB = Builder.GetInsertBlock();
TheFunction->getBasicBlockList().push_back(MergeBB);
Builder.SetInsertPoint(MergeBB);
PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
PN->addIncoming(ThenV, ThenBB);
PN->addIncoming(ElseV, ElseBB);
return PN;
}
Value *ForExprAST::Codegen() {
Function *TheFunction = Builder.GetInsertBlock()->getParent();
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
Value *StartVal = Start->Codegen();
if (StartVal == 0) return 0;
Builder.CreateStore(StartVal, Alloca);
BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
Builder.CreateBr(LoopBB);
Builder.SetInsertPoint(LoopBB);
AllocaInst *OldVal = NamedValues[VarName];
NamedValues[VarName] = Alloca;
if (Body->Codegen() == 0)
return 0;
Value *StepVal;
if (Step) {
StepVal = Step->Codegen();
if (StepVal == 0) return 0;
} else {
StepVal = ConstantFP::get(TheContext, APFloat(1.0));
}
Value *EndCond = End->Codegen();
if (EndCond == 0) return EndCond;
Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
Builder.CreateStore(NextVar, Alloca);
EndCond = Builder.CreateFCmpONE(
EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
BasicBlock *AfterBB =
BasicBlock::Create(TheContext, "afterloop", TheFunction);
Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
Builder.SetInsertPoint(AfterBB);
if (OldVal)
NamedValues[VarName] = OldVal;
else
NamedValues.erase(VarName);
return Constant::getNullValue(Type::getDoubleTy(TheContext));
}
Value *VarExprAST::Codegen() {
std::vector<AllocaInst *> OldBindings;
Function *TheFunction = Builder.GetInsertBlock()->getParent();
for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
const std::string &VarName = VarNames[i].first;
ExprAST *Init = VarNames[i].second;
Value *InitVal;
if (Init) {
InitVal = Init->Codegen();
if (InitVal == 0) return 0;
} else { InitVal = ConstantFP::get(TheContext, APFloat(0.0));
}
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
Builder.CreateStore(InitVal, Alloca);
OldBindings.push_back(NamedValues[VarName]);
NamedValues[VarName] = Alloca;
}
Value *BodyVal = Body->Codegen();
if (BodyVal == 0) return 0;
for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
NamedValues[VarNames[i].first] = OldBindings[i];
return BodyVal;
}
Function *PrototypeAST::Codegen() {
std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext));
FunctionType *FT =
FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
std::string FnName;
FnName = MakeLegalFunctionName(Name);
Module* M = TheHelper->getModuleForNewFunction();
Function *F = Function::Create(FT, Function::ExternalLinkage, FnName, M);
if (F->getName() != FnName) {
F->eraseFromParent();
F = M->getFunction(FnName);
if (!F->empty()) {
ErrorF("redefinition of function");
return 0;
}
if (F->arg_size() != Args.size()) {
ErrorF("redefinition of function with different # args");
return 0;
}
}
unsigned Idx = 0;
for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
++AI, ++Idx)
AI->setName(Args[Idx]);
return F;
}
void PrototypeAST::CreateArgumentAllocas(Function *F) {
Function::arg_iterator AI = F->arg_begin();
for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
Builder.CreateStore(AI, Alloca);
NamedValues[Args[Idx]] = Alloca;
}
}
Function *FunctionAST::Codegen() {
NamedValues.clear();
Function *TheFunction = Proto->Codegen();
if (TheFunction == 0)
return 0;
if (Proto->isBinaryOp())
BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
Builder.SetInsertPoint(BB);
Proto->CreateArgumentAllocas(TheFunction);
if (Value *RetVal = Body->Codegen()) {
Builder.CreateRet(RetVal);
verifyFunction(*TheFunction);
return TheFunction;
}
TheFunction->eraseFromParent();
if (Proto->isBinaryOp())
BinopPrecedence.erase(Proto->getOperatorName());
return 0;
}
static void HandleDefinition() {
if (FunctionAST *F = ParseDefinition()) {
if (EnableLazyCompilation)
TheHelper->closeCurrentModule();
Function *LF = F->Codegen();
if (LF && VerboseOutput) {
fprintf(stderr, "Read function definition:");
LF->print(errs());
fprintf(stderr, "\n");
}
} else {
getNextToken();
}
}
static void HandleExtern() {
if (PrototypeAST *P = ParseExtern()) {
Function *F = P->Codegen();
if (F && VerboseOutput) {
fprintf(stderr, "Read extern: ");
F->print(errs());
fprintf(stderr, "\n");
}
} else {
getNextToken();
}
}
static void HandleTopLevelExpression() {
if (FunctionAST *F = ParseTopLevelExpr()) {
if (Function *LF = F->Codegen()) {
void *FPtr = TheHelper->getPointerToFunction(LF);
double (*FP)() = (double (*)())(intptr_t)FPtr;
double Result = FP();
if (VerboseOutput)
fprintf(stderr, "Evaluated to %f\n", Result);
}
} else {
getNextToken();
}
}
static void MainLoop() {
while (1) {
if (!SuppressPrompts)
fprintf(stderr, "ready> ");
switch (CurTok) {
case tok_eof: return;
case ';': getNextToken(); break; case tok_def: HandleDefinition(); break;
case tok_extern: HandleExtern(); break;
default: HandleTopLevelExpression(); break;
}
}
}
extern "C"
double putchard(double X) {
putchar((char)X);
return 0;
}
extern "C"
double printd(double X) {
printf("%f", X);
return 0;
}
extern "C"
double printlf() {
printf("\n");
return 0;
}
int main(int argc, char **argv) {
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
LLVMContext &Context = TheContext;
cl::ParseCommandLineOptions(argc, argv,
"Kaleidoscope example program\n");
BinopPrecedence['='] = 2;
BinopPrecedence['<'] = 10;
BinopPrecedence['+'] = 20;
BinopPrecedence['-'] = 20;
BinopPrecedence['/'] = 40;
BinopPrecedence['*'] = 40;
TheHelper = new MCJITHelper(Context);
if (!SuppressPrompts)
fprintf(stderr, "ready> ");
getNextToken();
MainLoop();
if (DumpModulesOnExit)
TheHelper->dump();
return 0;
}