2016-05-19 09:08:04 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2016-11-19 05:57:58 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2015-03-02 09:04:34 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-11-19 05:57:58 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2014-12-09 02:00:47 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-07-03 01:01:59 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-12-09 02:00:47 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2016-05-19 09:08:04 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
2014-12-09 02:00:47 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2016-07-03 01:01:59 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-11-19 05:57:58 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-07-03 01:01:59 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2014-12-09 02:00:47 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2016-05-19 09:08:04 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2016-07-03 01:01:59 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2016-11-19 05:57:58 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2014-12-09 02:00:47 +08:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstdio>
|
2016-05-19 09:08:04 +08:00
|
|
|
#include <cstdlib>
|
2014-12-09 02:00:47 +08:00
|
|
|
#include <map>
|
2016-05-19 09:08:04 +08:00
|
|
|
#include <memory>
|
2014-12-09 02:00:47 +08:00
|
|
|
#include <string>
|
2016-11-19 05:57:58 +08:00
|
|
|
#include <system_error>
|
2016-05-19 09:08:04 +08:00
|
|
|
#include <utility>
|
2014-12-09 02:00:47 +08:00
|
|
|
#include <vector>
|
2015-08-26 11:07:41 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
using namespace llvm;
|
2016-07-03 01:01:59 +08:00
|
|
|
using namespace llvm::sys;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Lexer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
|
|
|
|
// of these for known things.
|
|
|
|
enum Token {
|
|
|
|
tok_eof = -1,
|
|
|
|
|
|
|
|
// commands
|
|
|
|
tok_def = -2,
|
|
|
|
tok_extern = -3,
|
|
|
|
|
|
|
|
// primary
|
|
|
|
tok_identifier = -4,
|
|
|
|
tok_number = -5,
|
|
|
|
|
|
|
|
// control
|
|
|
|
tok_if = -6,
|
|
|
|
tok_then = -7,
|
|
|
|
tok_else = -8,
|
|
|
|
tok_for = -9,
|
|
|
|
tok_in = -10,
|
|
|
|
|
|
|
|
// operators
|
|
|
|
tok_binary = -11,
|
|
|
|
tok_unary = -12,
|
|
|
|
|
|
|
|
// var definition
|
|
|
|
tok_var = -13
|
|
|
|
};
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
static std::string IdentifierStr; // Filled in if tok_identifier
|
|
|
|
static double NumVal; // Filled in if tok_number
|
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
/// gettok - Return the next token from standard input.
|
|
|
|
static int gettok() {
|
|
|
|
static int LastChar = ' ';
|
|
|
|
|
|
|
|
// Skip any whitespace.
|
|
|
|
while (isspace(LastChar))
|
2016-07-03 01:01:59 +08:00
|
|
|
LastChar = getchar();
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
|
|
|
|
IdentifierStr = LastChar;
|
2016-07-03 01:01:59 +08:00
|
|
|
while (isalnum((LastChar = getchar())))
|
2014-12-09 02:00:47 +08:00
|
|
|
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 == '.') { // Number: [0-9.]+
|
|
|
|
std::string NumStr;
|
|
|
|
do {
|
|
|
|
NumStr += LastChar;
|
2016-07-03 01:01:59 +08:00
|
|
|
LastChar = getchar();
|
2014-12-09 02:00:47 +08:00
|
|
|
} while (isdigit(LastChar) || LastChar == '.');
|
|
|
|
|
2015-09-30 02:02:48 +08:00
|
|
|
NumVal = strtod(NumStr.c_str(), nullptr);
|
2014-12-09 02:00:47 +08:00
|
|
|
return tok_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LastChar == '#') {
|
|
|
|
// Comment until end of line.
|
|
|
|
do
|
2016-07-03 01:01:59 +08:00
|
|
|
LastChar = getchar();
|
2014-12-09 02:00:47 +08:00
|
|
|
while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
|
|
|
|
|
|
|
|
if (LastChar != EOF)
|
|
|
|
return gettok();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for end of file. Don't eat the EOF.
|
|
|
|
if (LastChar == EOF)
|
|
|
|
return tok_eof;
|
|
|
|
|
|
|
|
// Otherwise, just return the character as its ascii value.
|
|
|
|
int ThisChar = LastChar;
|
2016-07-03 01:01:59 +08:00
|
|
|
LastChar = getchar();
|
2014-12-09 02:00:47 +08:00
|
|
|
return ThisChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Abstract Syntax Tree (aka Parse Tree)
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
namespace {
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
/// ExprAST - Base class for all expression nodes.
|
|
|
|
class ExprAST {
|
|
|
|
public:
|
2016-11-19 05:57:58 +08:00
|
|
|
virtual ~ExprAST() = default;
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
virtual Value *codegen() = 0;
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// NumberExprAST - Expression class for numeric literals like "1.0".
|
|
|
|
class NumberExprAST : public ExprAST {
|
|
|
|
double Val;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2015-08-19 02:11:06 +08:00
|
|
|
NumberExprAST(double Val) : Val(Val) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2016-05-19 09:08:04 +08:00
|
|
|
Value *codegen() override;
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// VariableExprAST - Expression class for referencing a variable, like "a".
|
|
|
|
class VariableExprAST : public ExprAST {
|
|
|
|
std::string Name;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2016-07-03 01:01:59 +08:00
|
|
|
VariableExprAST(const std::string &Name) : Name(Name) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *codegen() override;
|
2016-11-19 05:57:58 +08:00
|
|
|
const std::string &getName() const { return Name; }
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// UnaryExprAST - Expression class for a unary operator.
|
|
|
|
class UnaryExprAST : public ExprAST {
|
|
|
|
char Opcode;
|
2015-08-19 02:11:06 +08:00
|
|
|
std::unique_ptr<ExprAST> Operand;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2015-08-19 02:11:06 +08:00
|
|
|
UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)
|
|
|
|
: Opcode(Opcode), Operand(std::move(Operand)) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *codegen() override;
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// BinaryExprAST - Expression class for a binary operator.
|
|
|
|
class BinaryExprAST : public ExprAST {
|
|
|
|
char Op;
|
2015-08-19 02:11:06 +08:00
|
|
|
std::unique_ptr<ExprAST> LHS, RHS;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2016-07-03 01:01:59 +08:00
|
|
|
BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
|
2015-08-19 02:11:06 +08:00
|
|
|
std::unique_ptr<ExprAST> RHS)
|
2016-07-03 01:01:59 +08:00
|
|
|
: Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *codegen() override;
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// CallExprAST - Expression class for function calls.
|
|
|
|
class CallExprAST : public ExprAST {
|
|
|
|
std::string Callee;
|
2015-08-19 02:11:06 +08:00
|
|
|
std::vector<std::unique_ptr<ExprAST>> Args;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2016-07-03 01:01:59 +08:00
|
|
|
CallExprAST(const std::string &Callee,
|
2015-08-19 02:11:06 +08:00
|
|
|
std::vector<std::unique_ptr<ExprAST>> Args)
|
2016-07-03 01:01:59 +08:00
|
|
|
: Callee(Callee), Args(std::move(Args)) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *codegen() override;
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// IfExprAST - Expression class for if/then/else.
|
|
|
|
class IfExprAST : public ExprAST {
|
2015-08-19 02:11:06 +08:00
|
|
|
std::unique_ptr<ExprAST> Cond, Then, Else;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2016-07-03 01:01:59 +08:00
|
|
|
IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,
|
|
|
|
std::unique_ptr<ExprAST> Else)
|
|
|
|
: Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *codegen() override;
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// ForExprAST - Expression class for for/in.
|
|
|
|
class ForExprAST : public ExprAST {
|
|
|
|
std::string VarName;
|
2015-08-19 02:11:06 +08:00
|
|
|
std::unique_ptr<ExprAST> Start, End, Step, Body;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2015-08-19 02:11:06 +08:00
|
|
|
ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
|
|
|
|
std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
|
|
|
|
std::unique_ptr<ExprAST> Body)
|
|
|
|
: VarName(VarName), Start(std::move(Start)), End(std::move(End)),
|
|
|
|
Step(std::move(Step)), Body(std::move(Body)) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *codegen() override;
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// VarExprAST - Expression class for var/in
|
|
|
|
class VarExprAST : public ExprAST {
|
2015-08-19 02:11:06 +08:00
|
|
|
std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;
|
|
|
|
std::unique_ptr<ExprAST> Body;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2015-08-20 02:15:58 +08:00
|
|
|
VarExprAST(
|
|
|
|
std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames,
|
|
|
|
std::unique_ptr<ExprAST> Body)
|
2015-08-19 02:11:06 +08:00
|
|
|
: VarNames(std::move(VarNames)), Body(std::move(Body)) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *codegen() override;
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// PrototypeAST - This class represents the "prototype" for a function,
|
2015-08-20 02:15:58 +08:00
|
|
|
/// which captures its name, and its argument names (thus implicitly the number
|
|
|
|
/// of arguments the function takes), as well as if it is an operator.
|
2014-12-09 02:00:47 +08:00
|
|
|
class PrototypeAST {
|
|
|
|
std::string Name;
|
|
|
|
std::vector<std::string> Args;
|
2015-08-19 02:11:06 +08:00
|
|
|
bool IsOperator;
|
2014-12-09 02:00:47 +08:00
|
|
|
unsigned Precedence; // Precedence if a binary op.
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2016-07-03 01:01:59 +08:00
|
|
|
PrototypeAST(const std::string &Name, std::vector<std::string> Args,
|
|
|
|
bool IsOperator = false, unsigned Prec = 0)
|
2015-08-20 02:15:58 +08:00
|
|
|
: Name(Name), Args(std::move(Args)), IsOperator(IsOperator),
|
2016-07-03 01:01:59 +08:00
|
|
|
Precedence(Prec) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Function *codegen();
|
|
|
|
const std::string &getName() const { return Name; }
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
|
|
|
|
bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
char getOperatorName() const {
|
|
|
|
assert(isUnaryOp() || isBinaryOp());
|
|
|
|
return Name[Name.size() - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getBinaryPrecedence() const { return Precedence; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// FunctionAST - This class represents a function definition itself.
|
|
|
|
class FunctionAST {
|
2015-08-19 02:11:06 +08:00
|
|
|
std::unique_ptr<PrototypeAST> Proto;
|
|
|
|
std::unique_ptr<ExprAST> Body;
|
2015-08-20 02:15:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
public:
|
2015-08-19 02:11:06 +08:00
|
|
|
FunctionAST(std::unique_ptr<PrototypeAST> Proto,
|
|
|
|
std::unique_ptr<ExprAST> Body)
|
|
|
|
: Proto(std::move(Proto)), Body(std::move(Body)) {}
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Function *codegen();
|
2014-12-09 02:00:47 +08:00
|
|
|
};
|
2016-11-19 05:57:58 +08:00
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Parser
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
|
|
|
|
/// token the parser is looking at. getNextToken reads another token from the
|
|
|
|
/// lexer and updates CurTok with its results.
|
|
|
|
static int CurTok;
|
|
|
|
static int getNextToken() { return CurTok = gettok(); }
|
|
|
|
|
|
|
|
/// BinopPrecedence - This holds the precedence for each binary operator that is
|
|
|
|
/// defined.
|
|
|
|
static std::map<char, int> BinopPrecedence;
|
|
|
|
|
|
|
|
/// GetTokPrecedence - Get the precedence of the pending binary operator token.
|
|
|
|
static int GetTokPrecedence() {
|
|
|
|
if (!isascii(CurTok))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Make sure it's a declared binop.
|
|
|
|
int TokPrec = BinopPrecedence[CurTok];
|
|
|
|
if (TokPrec <= 0)
|
|
|
|
return -1;
|
|
|
|
return TokPrec;
|
|
|
|
}
|
|
|
|
|
2016-03-26 01:33:32 +08:00
|
|
|
/// LogError* - These are little helper functions for error handling.
|
|
|
|
std::unique_ptr<ExprAST> LogError(const char *Str) {
|
2014-12-09 02:00:47 +08:00
|
|
|
fprintf(stderr, "Error: %s\n", Str);
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
2015-09-30 02:02:48 +08:00
|
|
|
|
2016-03-26 01:33:32 +08:00
|
|
|
std::unique_ptr<PrototypeAST> LogErrorP(const char *Str) {
|
|
|
|
LogError(Str);
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParseExpression();
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2015-08-20 02:15:58 +08:00
|
|
|
/// numberexpr ::= number
|
|
|
|
static std::unique_ptr<ExprAST> ParseNumberExpr() {
|
|
|
|
auto Result = llvm::make_unique<NumberExprAST>(NumVal);
|
|
|
|
getNextToken(); // consume the number
|
|
|
|
return std::move(Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// parenexpr ::= '(' expression ')'
|
|
|
|
static std::unique_ptr<ExprAST> ParseParenExpr() {
|
|
|
|
getNextToken(); // eat (.
|
|
|
|
auto V = ParseExpression();
|
|
|
|
if (!V)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (CurTok != ')')
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected ')'");
|
2015-08-20 02:15:58 +08:00
|
|
|
getNextToken(); // eat ).
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
/// identifierexpr
|
|
|
|
/// ::= identifier
|
|
|
|
/// ::= identifier '(' expression* ')'
|
2015-08-20 02:15:58 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
|
2014-12-09 02:00:47 +08:00
|
|
|
std::string IdName = IdentifierStr;
|
|
|
|
|
|
|
|
getNextToken(); // eat identifier.
|
|
|
|
|
|
|
|
if (CurTok != '(') // Simple variable ref.
|
2016-07-03 01:01:59 +08:00
|
|
|
return llvm::make_unique<VariableExprAST>(IdName);
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Call.
|
|
|
|
getNextToken(); // eat (
|
2015-08-19 02:11:06 +08:00
|
|
|
std::vector<std::unique_ptr<ExprAST>> Args;
|
2014-12-09 02:00:47 +08:00
|
|
|
if (CurTok != ')') {
|
2016-05-19 09:08:04 +08:00
|
|
|
while (true) {
|
2015-08-20 02:15:58 +08:00
|
|
|
if (auto Arg = ParseExpression())
|
|
|
|
Args.push_back(std::move(Arg));
|
|
|
|
else
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
if (CurTok == ')')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (CurTok != ',')
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("Expected ')' or ',' in argument list");
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eat the ')'.
|
|
|
|
getNextToken();
|
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
return llvm::make_unique<CallExprAST>(IdName, std::move(Args));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ifexpr ::= 'if' expression 'then' expression 'else' expression
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParseIfExpr() {
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat the if.
|
|
|
|
|
|
|
|
// condition.
|
2015-08-19 02:11:06 +08:00
|
|
|
auto Cond = ParseExpression();
|
2014-12-09 02:00:47 +08:00
|
|
|
if (!Cond)
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
if (CurTok != tok_then)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected then");
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat the then
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
auto Then = ParseExpression();
|
|
|
|
if (!Then)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
if (CurTok != tok_else)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected else");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
getNextToken();
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
auto Else = ParseExpression();
|
2014-12-09 02:00:47 +08:00
|
|
|
if (!Else)
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then),
|
2015-08-19 02:11:06 +08:00
|
|
|
std::move(Else));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParseForExpr() {
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat the for.
|
|
|
|
|
|
|
|
if (CurTok != tok_identifier)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected identifier after for");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
std::string IdName = IdentifierStr;
|
|
|
|
getNextToken(); // eat identifier.
|
|
|
|
|
|
|
|
if (CurTok != '=')
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected '=' after for");
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat '='.
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
auto Start = ParseExpression();
|
|
|
|
if (!Start)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
if (CurTok != ',')
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected ',' after for start value");
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken();
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
auto End = ParseExpression();
|
|
|
|
if (!End)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// The step value is optional.
|
2015-08-19 02:11:06 +08:00
|
|
|
std::unique_ptr<ExprAST> Step;
|
2014-12-09 02:00:47 +08:00
|
|
|
if (CurTok == ',') {
|
|
|
|
getNextToken();
|
|
|
|
Step = ParseExpression();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!Step)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CurTok != tok_in)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected 'in' after for");
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat 'in'.
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
auto Body = ParseExpression();
|
|
|
|
if (!Body)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
return llvm::make_unique<ForExprAST>(IdName, std::move(Start), std::move(End),
|
|
|
|
std::move(Step), std::move(Body));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// varexpr ::= 'var' identifier ('=' expression)?
|
|
|
|
// (',' identifier ('=' expression)?)* 'in' expression
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParseVarExpr() {
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat the var.
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// At least one variable name is required.
|
|
|
|
if (CurTok != tok_identifier)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected identifier after var");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-05-19 09:08:04 +08:00
|
|
|
while (true) {
|
2014-12-09 02:00:47 +08:00
|
|
|
std::string Name = IdentifierStr;
|
|
|
|
getNextToken(); // eat identifier.
|
|
|
|
|
|
|
|
// Read the optional initializer.
|
2015-08-19 02:11:06 +08:00
|
|
|
std::unique_ptr<ExprAST> Init = nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
if (CurTok == '=') {
|
|
|
|
getNextToken(); // eat the '='.
|
|
|
|
|
|
|
|
Init = ParseExpression();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!Init)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
VarNames.push_back(std::make_pair(Name, std::move(Init)));
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// End of var list, exit loop.
|
|
|
|
if (CurTok != ',')
|
|
|
|
break;
|
|
|
|
getNextToken(); // eat the ','.
|
|
|
|
|
|
|
|
if (CurTok != tok_identifier)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected identifier list after var");
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we have to have 'in'.
|
|
|
|
if (CurTok != tok_in)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("expected 'in' keyword after 'var'");
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat 'in'.
|
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
auto Body = ParseExpression();
|
|
|
|
if (!Body)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
return llvm::make_unique<VarExprAST>(std::move(VarNames), std::move(Body));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// primary
|
|
|
|
/// ::= identifierexpr
|
|
|
|
/// ::= numberexpr
|
|
|
|
/// ::= parenexpr
|
|
|
|
/// ::= ifexpr
|
|
|
|
/// ::= forexpr
|
|
|
|
/// ::= varexpr
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParsePrimary() {
|
2014-12-09 02:00:47 +08:00
|
|
|
switch (CurTok) {
|
|
|
|
default:
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogError("unknown token when expecting an expression");
|
2014-12-09 02:00:47 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// unary
|
|
|
|
/// ::= primary
|
|
|
|
/// ::= '!' unary
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParseUnary() {
|
2014-12-09 02:00:47 +08:00
|
|
|
// If the current token is not an operator, it must be a primary expr.
|
|
|
|
if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
|
|
|
|
return ParsePrimary();
|
|
|
|
|
|
|
|
// If this is a unary operator, read it.
|
|
|
|
int Opc = CurTok;
|
|
|
|
getNextToken();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (auto Operand = ParseUnary())
|
|
|
|
return llvm::make_unique<UnaryExprAST>(Opc, std::move(Operand));
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// binoprhs
|
|
|
|
/// ::= ('+' unary)*
|
2015-08-20 02:15:58 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec,
|
|
|
|
std::unique_ptr<ExprAST> LHS) {
|
2014-12-09 02:00:47 +08:00
|
|
|
// If this is a binop, find its precedence.
|
2016-05-19 09:08:04 +08:00
|
|
|
while (true) {
|
2014-12-09 02:00:47 +08:00
|
|
|
int TokPrec = GetTokPrecedence();
|
|
|
|
|
|
|
|
// If this is a binop that binds at least as tightly as the current binop,
|
|
|
|
// consume it, otherwise we are done.
|
|
|
|
if (TokPrec < ExprPrec)
|
|
|
|
return LHS;
|
|
|
|
|
|
|
|
// Okay, we know this is a binop.
|
|
|
|
int BinOp = CurTok;
|
|
|
|
getNextToken(); // eat binop
|
|
|
|
|
|
|
|
// Parse the unary expression after the binary operator.
|
2015-08-19 02:11:06 +08:00
|
|
|
auto RHS = ParseUnary();
|
2014-12-09 02:00:47 +08:00
|
|
|
if (!RHS)
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// If BinOp binds less tightly with RHS than the operator after RHS, let
|
|
|
|
// the pending operator take RHS as its LHS.
|
|
|
|
int NextPrec = GetTokPrecedence();
|
|
|
|
if (TokPrec < NextPrec) {
|
2015-08-19 02:11:06 +08:00
|
|
|
RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
|
|
|
|
if (!RHS)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge LHS/RHS.
|
2016-07-03 01:01:59 +08:00
|
|
|
LHS =
|
|
|
|
llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// expression
|
|
|
|
/// ::= unary binoprhs
|
|
|
|
///
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<ExprAST> ParseExpression() {
|
|
|
|
auto LHS = ParseUnary();
|
2014-12-09 02:00:47 +08:00
|
|
|
if (!LHS)
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
return ParseBinOpRHS(0, std::move(LHS));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// prototype
|
|
|
|
/// ::= id '(' id* ')'
|
|
|
|
/// ::= binary LETTER number? (id, id)
|
|
|
|
/// ::= unary LETTER (id)
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<PrototypeAST> ParsePrototype() {
|
2014-12-09 02:00:47 +08:00
|
|
|
std::string FnName;
|
|
|
|
|
|
|
|
unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
|
|
|
|
unsigned BinaryPrecedence = 30;
|
|
|
|
|
|
|
|
switch (CurTok) {
|
|
|
|
default:
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorP("Expected function name in prototype");
|
2014-12-09 02:00:47 +08:00
|
|
|
case tok_identifier:
|
|
|
|
FnName = IdentifierStr;
|
|
|
|
Kind = 0;
|
|
|
|
getNextToken();
|
|
|
|
break;
|
|
|
|
case tok_unary:
|
|
|
|
getNextToken();
|
|
|
|
if (!isascii(CurTok))
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorP("Expected unary operator");
|
2014-12-09 02:00:47 +08:00
|
|
|
FnName = "unary";
|
|
|
|
FnName += (char)CurTok;
|
|
|
|
Kind = 1;
|
|
|
|
getNextToken();
|
|
|
|
break;
|
|
|
|
case tok_binary:
|
|
|
|
getNextToken();
|
|
|
|
if (!isascii(CurTok))
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorP("Expected binary operator");
|
2014-12-09 02:00:47 +08:00
|
|
|
FnName = "binary";
|
|
|
|
FnName += (char)CurTok;
|
|
|
|
Kind = 2;
|
|
|
|
getNextToken();
|
|
|
|
|
|
|
|
// Read the precedence if present.
|
|
|
|
if (CurTok == tok_number) {
|
|
|
|
if (NumVal < 1 || NumVal > 100)
|
2017-02-12 05:26:52 +08:00
|
|
|
return LogErrorP("Invalid precedence: must be 1..100");
|
2014-12-09 02:00:47 +08:00
|
|
|
BinaryPrecedence = (unsigned)NumVal;
|
|
|
|
getNextToken();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurTok != '(')
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorP("Expected '(' in prototype");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
std::vector<std::string> ArgNames;
|
|
|
|
while (getNextToken() == tok_identifier)
|
|
|
|
ArgNames.push_back(IdentifierStr);
|
|
|
|
if (CurTok != ')')
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorP("Expected ')' in prototype");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// success.
|
|
|
|
getNextToken(); // eat ')'.
|
|
|
|
|
|
|
|
// Verify right number of names for operator.
|
|
|
|
if (Kind && ArgNames.size() != Kind)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorP("Invalid number of operands for operator");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
return llvm::make_unique<PrototypeAST>(FnName, ArgNames, Kind != 0,
|
2015-08-19 02:11:06 +08:00
|
|
|
BinaryPrecedence);
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// definition ::= 'def' prototype expression
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<FunctionAST> ParseDefinition() {
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat def.
|
2015-08-19 02:11:06 +08:00
|
|
|
auto Proto = ParsePrototype();
|
|
|
|
if (!Proto)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2015-08-19 02:11:06 +08:00
|
|
|
if (auto E = ParseExpression())
|
|
|
|
return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// toplevelexpr ::= expression
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
|
|
|
|
if (auto E = ParseExpression()) {
|
2014-12-09 02:00:47 +08:00
|
|
|
// Make an anonymous proto.
|
2016-07-03 01:01:59 +08:00
|
|
|
auto Proto = llvm::make_unique<PrototypeAST>("__anon_expr",
|
2015-08-20 02:15:58 +08:00
|
|
|
std::vector<std::string>());
|
2015-08-19 02:11:06 +08:00
|
|
|
return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// external ::= 'extern' prototype
|
2015-08-19 02:11:06 +08:00
|
|
|
static std::unique_ptr<PrototypeAST> ParseExtern() {
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken(); // eat extern.
|
|
|
|
return ParsePrototype();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Code Generation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
static LLVMContext TheContext;
|
|
|
|
static IRBuilder<> Builder(TheContext);
|
2015-08-26 11:07:41 +08:00
|
|
|
static std::unique_ptr<Module> TheModule;
|
2014-12-09 02:00:47 +08:00
|
|
|
static std::map<std::string, AllocaInst *> NamedValues;
|
2015-08-26 11:07:41 +08:00
|
|
|
static std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-03-26 01:33:32 +08:00
|
|
|
Value *LogErrorV(const char *Str) {
|
|
|
|
LogError(Str);
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Function *getFunction(std::string Name) {
|
|
|
|
// First, see if the function has already been added to the current module.
|
|
|
|
if (auto *F = TheModule->getFunction(Name))
|
|
|
|
return F;
|
|
|
|
|
|
|
|
// If not, check whether we can codegen the declaration from some existing
|
|
|
|
// prototype.
|
|
|
|
auto FI = FunctionProtos.find(Name);
|
|
|
|
if (FI != FunctionProtos.end())
|
|
|
|
return FI->second->codegen();
|
|
|
|
|
|
|
|
// If no existing prototype exists, return null.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
/// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
|
|
|
|
/// the function. This is used for mutable variables etc.
|
|
|
|
static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
|
|
|
|
const std::string &VarName) {
|
|
|
|
IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
|
|
|
|
TheFunction->getEntryBlock().begin());
|
2016-05-19 09:08:04 +08:00
|
|
|
return TmpB.CreateAlloca(Type::getDoubleTy(TheContext), nullptr, VarName);
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *NumberExprAST::codegen() {
|
2016-04-15 05:59:01 +08:00
|
|
|
return ConstantFP::get(TheContext, APFloat(Val));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *VariableExprAST::codegen() {
|
2014-12-09 02:00:47 +08:00
|
|
|
// Look this variable up in the function.
|
|
|
|
Value *V = NamedValues[Name];
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!V)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorV("Unknown variable name");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Load the value.
|
|
|
|
return Builder.CreateLoad(V, Name.c_str());
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *UnaryExprAST::codegen() {
|
|
|
|
Value *OperandV = Operand->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!OperandV)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Function *F = getFunction(std::string("unary") + Opcode);
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!F)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorV("Unknown unary operator");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
return Builder.CreateCall(F, OperandV, "unop");
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *BinaryExprAST::codegen() {
|
2014-12-09 02:00:47 +08:00
|
|
|
// Special case '=' because we don't want to emit the LHS as an expression.
|
|
|
|
if (Op == '=') {
|
|
|
|
// Assignment requires the LHS to be an identifier.
|
2015-04-23 04:41:34 +08:00
|
|
|
// This assume we're building without RTTI because LLVM builds that way by
|
|
|
|
// default. If you build LLVM with RTTI this can be changed to a
|
|
|
|
// dynamic_cast for automatic error checking.
|
2015-08-20 02:15:58 +08:00
|
|
|
VariableExprAST *LHSE = static_cast<VariableExprAST *>(LHS.get());
|
2014-12-09 02:00:47 +08:00
|
|
|
if (!LHSE)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorV("destination of '=' must be a variable");
|
2014-12-09 02:00:47 +08:00
|
|
|
// Codegen the RHS.
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *Val = RHS->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!Val)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Look up the name.
|
|
|
|
Value *Variable = NamedValues[LHSE->getName()];
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!Variable)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorV("Unknown variable name");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
Builder.CreateStore(Val, Variable);
|
|
|
|
return Val;
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *L = LHS->codegen();
|
|
|
|
Value *R = RHS->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!L || !R)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
switch (Op) {
|
|
|
|
case '+':
|
|
|
|
return Builder.CreateFAdd(L, R, "addtmp");
|
|
|
|
case '-':
|
|
|
|
return Builder.CreateFSub(L, R, "subtmp");
|
|
|
|
case '*':
|
|
|
|
return Builder.CreateFMul(L, R, "multmp");
|
|
|
|
case '<':
|
|
|
|
L = Builder.CreateFCmpULT(L, R, "cmptmp");
|
|
|
|
// Convert bool 0/1 to double 0.0 or 1.0
|
2016-04-15 05:59:01 +08:00
|
|
|
return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
|
2014-12-09 02:00:47 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it wasn't a builtin binary operator, it must be a user defined one. Emit
|
|
|
|
// a call to it.
|
2015-08-26 11:07:41 +08:00
|
|
|
Function *F = getFunction(std::string("binary") + Op);
|
2014-12-09 02:00:47 +08:00
|
|
|
assert(F && "binary operator not found!");
|
|
|
|
|
2015-08-20 02:15:58 +08:00
|
|
|
Value *Ops[] = {L, R};
|
2014-12-09 02:00:47 +08:00
|
|
|
return Builder.CreateCall(F, Ops, "binop");
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *CallExprAST::codegen() {
|
2014-12-09 02:00:47 +08:00
|
|
|
// Look up the name in the global module table.
|
2015-08-26 11:07:41 +08:00
|
|
|
Function *CalleeF = getFunction(Callee);
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!CalleeF)
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorV("Unknown function referenced");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// If argument mismatch error.
|
|
|
|
if (CalleeF->arg_size() != Args.size())
|
2016-03-26 01:33:32 +08:00
|
|
|
return LogErrorV("Incorrect # arguments passed");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
std::vector<Value *> ArgsV;
|
|
|
|
for (unsigned i = 0, e = Args.size(); i != e; ++i) {
|
2015-08-26 11:07:41 +08:00
|
|
|
ArgsV.push_back(Args[i]->codegen());
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!ArgsV.back())
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *IfExprAST::codegen() {
|
|
|
|
Value *CondV = Cond->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!CondV)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2017-02-12 05:26:52 +08:00
|
|
|
// Convert condition to a bool by comparing non-equal to 0.0.
|
2014-12-09 02:00:47 +08:00
|
|
|
CondV = Builder.CreateFCmpONE(
|
2016-04-15 05:59:01 +08:00
|
|
|
CondV, ConstantFP::get(TheContext, APFloat(0.0)), "ifcond");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
Function *TheFunction = Builder.GetInsertBlock()->getParent();
|
|
|
|
|
|
|
|
// Create blocks for the then and else cases. Insert the 'then' block at the
|
|
|
|
// end of the function.
|
2016-04-15 05:59:01 +08:00
|
|
|
BasicBlock *ThenBB = BasicBlock::Create(TheContext, "then", TheFunction);
|
|
|
|
BasicBlock *ElseBB = BasicBlock::Create(TheContext, "else");
|
|
|
|
BasicBlock *MergeBB = BasicBlock::Create(TheContext, "ifcont");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
Builder.CreateCondBr(CondV, ThenBB, ElseBB);
|
|
|
|
|
|
|
|
// Emit then value.
|
|
|
|
Builder.SetInsertPoint(ThenBB);
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *ThenV = Then->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!ThenV)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
Builder.CreateBr(MergeBB);
|
|
|
|
// Codegen of 'Then' can change the current block, update ThenBB for the PHI.
|
|
|
|
ThenBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
// Emit else block.
|
|
|
|
TheFunction->getBasicBlockList().push_back(ElseBB);
|
|
|
|
Builder.SetInsertPoint(ElseBB);
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *ElseV = Else->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!ElseV)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
Builder.CreateBr(MergeBB);
|
|
|
|
// Codegen of 'Else' can change the current block, update ElseBB for the PHI.
|
|
|
|
ElseBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
// Emit merge block.
|
|
|
|
TheFunction->getBasicBlockList().push_back(MergeBB);
|
|
|
|
Builder.SetInsertPoint(MergeBB);
|
2016-04-15 05:59:01 +08:00
|
|
|
PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(TheContext), 2, "iftmp");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
PN->addIncoming(ThenV, ThenBB);
|
|
|
|
PN->addIncoming(ElseV, ElseBB);
|
|
|
|
return PN;
|
|
|
|
}
|
|
|
|
|
2015-08-20 02:15:58 +08:00
|
|
|
// Output for-loop as:
|
|
|
|
// var = alloca double
|
|
|
|
// ...
|
|
|
|
// start = startexpr
|
|
|
|
// store start -> var
|
|
|
|
// goto loop
|
|
|
|
// loop:
|
|
|
|
// ...
|
|
|
|
// bodyexpr
|
|
|
|
// ...
|
|
|
|
// loopend:
|
|
|
|
// step = stepexpr
|
|
|
|
// endcond = endexpr
|
|
|
|
//
|
|
|
|
// curvar = load var
|
|
|
|
// nextvar = curvar + step
|
|
|
|
// store nextvar -> var
|
|
|
|
// br endcond, loop, endloop
|
|
|
|
// outloop:
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *ForExprAST::codegen() {
|
2014-12-09 02:00:47 +08:00
|
|
|
Function *TheFunction = Builder.GetInsertBlock()->getParent();
|
|
|
|
|
|
|
|
// Create an alloca for the variable in the entry block.
|
|
|
|
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
|
|
|
|
|
|
|
|
// Emit the start code first, without 'variable' in scope.
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *StartVal = Start->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!StartVal)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Store the value into the alloca.
|
|
|
|
Builder.CreateStore(StartVal, Alloca);
|
|
|
|
|
|
|
|
// Make the new basic block for the loop header, inserting after current
|
|
|
|
// block.
|
2016-04-15 05:59:01 +08:00
|
|
|
BasicBlock *LoopBB = BasicBlock::Create(TheContext, "loop", TheFunction);
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Insert an explicit fall through from the current block to the LoopBB.
|
|
|
|
Builder.CreateBr(LoopBB);
|
|
|
|
|
|
|
|
// Start insertion in LoopBB.
|
|
|
|
Builder.SetInsertPoint(LoopBB);
|
|
|
|
|
|
|
|
// Within the loop, the variable is defined equal to the PHI node. If it
|
|
|
|
// shadows an existing variable, we have to restore it, so save it now.
|
|
|
|
AllocaInst *OldVal = NamedValues[VarName];
|
|
|
|
NamedValues[VarName] = Alloca;
|
|
|
|
|
|
|
|
// Emit the body of the loop. This, like any other expr, can change the
|
|
|
|
// current BB. Note that we ignore the value computed by the body, but don't
|
|
|
|
// allow an error.
|
2015-08-26 11:07:41 +08:00
|
|
|
if (!Body->codegen())
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Emit the step value.
|
2015-08-20 02:15:58 +08:00
|
|
|
Value *StepVal = nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
if (Step) {
|
2015-08-26 11:07:41 +08:00
|
|
|
StepVal = Step->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!StepVal)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
} else {
|
|
|
|
// If not specified, use 1.0.
|
2016-04-15 05:59:01 +08:00
|
|
|
StepVal = ConstantFP::get(TheContext, APFloat(1.0));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the end condition.
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *EndCond = End->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!EndCond)
|
2015-08-20 02:15:58 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Reload, increment, and restore the alloca. This handles the case where
|
|
|
|
// the body of the loop mutates the variable.
|
|
|
|
Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
|
|
|
|
Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar");
|
|
|
|
Builder.CreateStore(NextVar, Alloca);
|
|
|
|
|
2017-02-12 05:26:52 +08:00
|
|
|
// Convert condition to a bool by comparing non-equal to 0.0.
|
2014-12-09 02:00:47 +08:00
|
|
|
EndCond = Builder.CreateFCmpONE(
|
2016-04-15 05:59:01 +08:00
|
|
|
EndCond, ConstantFP::get(TheContext, APFloat(0.0)), "loopcond");
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Create the "after loop" block and insert it.
|
|
|
|
BasicBlock *AfterBB =
|
2016-04-15 05:59:01 +08:00
|
|
|
BasicBlock::Create(TheContext, "afterloop", TheFunction);
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Insert the conditional branch into the end of LoopEndBB.
|
|
|
|
Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
|
|
|
|
|
|
|
|
// Any new code will be inserted in AfterBB.
|
|
|
|
Builder.SetInsertPoint(AfterBB);
|
|
|
|
|
|
|
|
// Restore the unshadowed variable.
|
|
|
|
if (OldVal)
|
|
|
|
NamedValues[VarName] = OldVal;
|
|
|
|
else
|
|
|
|
NamedValues.erase(VarName);
|
|
|
|
|
|
|
|
// for expr always returns 0.0.
|
2016-04-15 05:59:01 +08:00
|
|
|
return Constant::getNullValue(Type::getDoubleTy(TheContext));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *VarExprAST::codegen() {
|
2014-12-09 02:00:47 +08:00
|
|
|
std::vector<AllocaInst *> OldBindings;
|
|
|
|
|
|
|
|
Function *TheFunction = Builder.GetInsertBlock()->getParent();
|
|
|
|
|
|
|
|
// Register all variables and emit their initializer.
|
|
|
|
for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
|
|
|
|
const std::string &VarName = VarNames[i].first;
|
2015-08-19 02:11:06 +08:00
|
|
|
ExprAST *Init = VarNames[i].second.get();
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Emit the initializer before adding the variable to scope, this prevents
|
|
|
|
// the initializer from referencing the variable itself, and permits stuff
|
|
|
|
// like this:
|
|
|
|
// var a = 1 in
|
|
|
|
// var a = a in ... # refers to outer 'a'.
|
|
|
|
Value *InitVal;
|
|
|
|
if (Init) {
|
2015-08-26 11:07:41 +08:00
|
|
|
InitVal = Init->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!InitVal)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
} else { // If not specified, use 0.0.
|
2016-04-15 05:59:01 +08:00
|
|
|
InitVal = ConstantFP::get(TheContext, APFloat(0.0));
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
|
|
|
|
Builder.CreateStore(InitVal, Alloca);
|
|
|
|
|
|
|
|
// Remember the old variable binding so that we can restore the binding when
|
|
|
|
// we unrecurse.
|
|
|
|
OldBindings.push_back(NamedValues[VarName]);
|
|
|
|
|
|
|
|
// Remember this binding.
|
|
|
|
NamedValues[VarName] = Alloca;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Codegen the body, now that all vars are in scope.
|
2015-08-26 11:07:41 +08:00
|
|
|
Value *BodyVal = Body->codegen();
|
2015-08-19 02:11:06 +08:00
|
|
|
if (!BodyVal)
|
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Pop all our variables from scope.
|
|
|
|
for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
|
|
|
|
NamedValues[VarNames[i].first] = OldBindings[i];
|
|
|
|
|
|
|
|
// Return the body computation.
|
|
|
|
return BodyVal;
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
Function *PrototypeAST::codegen() {
|
2014-12-09 02:00:47 +08:00
|
|
|
// Make the function type: double(double,double) etc.
|
2016-04-15 05:59:01 +08:00
|
|
|
std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext));
|
2014-12-09 02:00:47 +08:00
|
|
|
FunctionType *FT =
|
2016-04-15 05:59:01 +08:00
|
|
|
FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
Function *F =
|
2015-08-26 11:07:41 +08:00
|
|
|
Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Set names for all arguments.
|
|
|
|
unsigned Idx = 0;
|
2015-08-26 11:07:41 +08:00
|
|
|
for (auto &Arg : F->args())
|
|
|
|
Arg.setName(Args[Idx++]);
|
|
|
|
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
Function *FunctionAST::codegen() {
|
|
|
|
// Transfer ownership of the prototype to the FunctionProtos map, but keep a
|
|
|
|
// reference to it for use below.
|
|
|
|
auto &P = *Proto;
|
|
|
|
FunctionProtos[Proto->getName()] = std::move(Proto);
|
|
|
|
Function *TheFunction = getFunction(P.getName());
|
|
|
|
if (!TheFunction)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If this is an operator, install it.
|
|
|
|
if (P.isBinaryOp())
|
|
|
|
BinopPrecedence[P.getOperatorName()] = P.getBinaryPrecedence();
|
|
|
|
|
|
|
|
// Create a new basic block to start insertion into.
|
2016-04-15 05:59:01 +08:00
|
|
|
BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
|
2015-08-26 11:07:41 +08:00
|
|
|
Builder.SetInsertPoint(BB);
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
// Record the function arguments in the NamedValues map.
|
|
|
|
NamedValues.clear();
|
|
|
|
for (auto &Arg : TheFunction->args()) {
|
2014-12-09 02:00:47 +08:00
|
|
|
// Create an alloca for this variable.
|
2015-08-26 11:07:41 +08:00
|
|
|
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName());
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Store the initial value into the alloca.
|
2015-08-26 11:07:41 +08:00
|
|
|
Builder.CreateStore(&Arg, Alloca);
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
// Add arguments to variable symbol table.
|
2015-08-26 11:07:41 +08:00
|
|
|
NamedValues[Arg.getName()] = Alloca;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
if (Value *RetVal = Body->codegen()) {
|
2014-12-09 02:00:47 +08:00
|
|
|
// Finish off the function.
|
|
|
|
Builder.CreateRet(RetVal);
|
|
|
|
|
|
|
|
// Validate the generated code, checking for consistency.
|
|
|
|
verifyFunction(*TheFunction);
|
|
|
|
|
|
|
|
return TheFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error reading body, remove function.
|
|
|
|
TheFunction->eraseFromParent();
|
|
|
|
|
2015-08-26 11:07:41 +08:00
|
|
|
if (P.isBinaryOp())
|
2017-05-07 19:00:01 +08:00
|
|
|
BinopPrecedence.erase(P.getOperatorName());
|
2015-08-19 02:11:06 +08:00
|
|
|
return nullptr;
|
2014-12-09 02:00:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Top-Level parsing and JIT Driver
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
static void InitializeModuleAndPassManager() {
|
2015-08-26 11:07:41 +08:00
|
|
|
// Open a new module.
|
2016-04-15 05:59:01 +08:00
|
|
|
TheModule = llvm::make_unique<Module>("my cool jit", TheContext);
|
2015-08-26 11:07:41 +08:00
|
|
|
}
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
static void HandleDefinition() {
|
2015-08-19 02:11:06 +08:00
|
|
|
if (auto FnAST = ParseDefinition()) {
|
2016-07-03 01:01:59 +08:00
|
|
|
if (auto *FnIR = FnAST->codegen()) {
|
|
|
|
fprintf(stderr, "Read function definition:");
|
2017-01-28 10:47:46 +08:00
|
|
|
FnIR->print(errs());
|
|
|
|
fprintf(stderr, "\n");
|
2016-07-03 01:01:59 +08:00
|
|
|
}
|
2014-12-09 02:00:47 +08:00
|
|
|
} else {
|
|
|
|
// Skip token for error recovery.
|
|
|
|
getNextToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleExtern() {
|
2015-08-19 02:11:06 +08:00
|
|
|
if (auto ProtoAST = ParseExtern()) {
|
2016-07-03 01:01:59 +08:00
|
|
|
if (auto *FnIR = ProtoAST->codegen()) {
|
|
|
|
fprintf(stderr, "Read extern: ");
|
2017-01-28 10:47:46 +08:00
|
|
|
FnIR->print(errs());
|
|
|
|
fprintf(stderr, "\n");
|
2015-08-26 11:07:41 +08:00
|
|
|
FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST);
|
2016-07-03 01:01:59 +08:00
|
|
|
}
|
2014-12-09 02:00:47 +08:00
|
|
|
} else {
|
|
|
|
// Skip token for error recovery.
|
|
|
|
getNextToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleTopLevelExpression() {
|
|
|
|
// Evaluate a top-level expression into an anonymous function.
|
2015-08-19 02:11:06 +08:00
|
|
|
if (auto FnAST = ParseTopLevelExpr()) {
|
2016-07-03 01:01:59 +08:00
|
|
|
FnAST->codegen();
|
2014-12-09 02:00:47 +08:00
|
|
|
} else {
|
|
|
|
// Skip token for error recovery.
|
|
|
|
getNextToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// top ::= definition | external | expression | ';'
|
|
|
|
static void MainLoop() {
|
2016-05-19 09:08:04 +08:00
|
|
|
while (true) {
|
2014-12-09 02:00:47 +08:00
|
|
|
switch (CurTok) {
|
|
|
|
case tok_eof:
|
|
|
|
return;
|
2015-08-20 02:15:58 +08:00
|
|
|
case ';': // ignore top-level semicolons.
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken();
|
2015-08-20 02:15:58 +08:00
|
|
|
break;
|
2014-12-09 02:00:47 +08:00
|
|
|
case tok_def:
|
|
|
|
HandleDefinition();
|
|
|
|
break;
|
|
|
|
case tok_extern:
|
|
|
|
HandleExtern();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
HandleTopLevelExpression();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// "Library" functions that can be "extern'd" from user code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-02-12 05:26:52 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
#define DLLEXPORT __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
#define DLLEXPORT
|
|
|
|
#endif
|
|
|
|
|
2014-12-09 02:00:47 +08:00
|
|
|
/// putchard - putchar that takes a double and returns 0.
|
2017-02-12 05:26:52 +08:00
|
|
|
extern "C" DLLEXPORT double putchard(double X) {
|
2015-08-28 04:31:44 +08:00
|
|
|
fputc((char)X, stderr);
|
2014-12-09 02:00:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// printd - printf that takes a double prints it as "%f\n", returning 0.
|
2017-02-12 05:26:52 +08:00
|
|
|
extern "C" DLLEXPORT double printd(double X) {
|
2015-08-28 04:31:44 +08:00
|
|
|
fprintf(stderr, "%f\n", X);
|
2014-12-09 02:00:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Main driver code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
// Install standard binary operators.
|
|
|
|
// 1 is lowest precedence.
|
|
|
|
BinopPrecedence['<'] = 10;
|
|
|
|
BinopPrecedence['+'] = 20;
|
|
|
|
BinopPrecedence['-'] = 20;
|
|
|
|
BinopPrecedence['*'] = 40; // highest.
|
|
|
|
|
|
|
|
// Prime the first token.
|
2016-07-03 01:01:59 +08:00
|
|
|
fprintf(stderr, "ready> ");
|
2014-12-09 02:00:47 +08:00
|
|
|
getNextToken();
|
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
InitializeModuleAndPassManager();
|
2015-08-26 11:07:41 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
// Run the main "interpreter loop" now.
|
|
|
|
MainLoop();
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
// Initialize the target registry etc.
|
|
|
|
InitializeAllTargetInfos();
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
|
|
|
|
auto TargetTriple = sys::getDefaultTargetTriple();
|
|
|
|
TheModule->setTargetTriple(TargetTriple);
|
|
|
|
|
|
|
|
std::string Error;
|
|
|
|
auto Target = TargetRegistry::lookupTarget(TargetTriple, Error);
|
|
|
|
|
|
|
|
// Print an error and exit if we couldn't find the requested target.
|
|
|
|
// This generally occurs if we've forgotten to initialise the
|
|
|
|
// TargetRegistry or we have a bogus target triple.
|
|
|
|
if (!Target) {
|
|
|
|
errs() << Error;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
auto CPU = "generic";
|
|
|
|
auto Features = "";
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
TargetOptions opt;
|
|
|
|
auto RM = Optional<Reloc::Model>();
|
|
|
|
auto TheTargetMachine =
|
|
|
|
Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
TheModule->setDataLayout(TheTargetMachine->createDataLayout());
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
auto Filename = "output.o";
|
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
|
|
|
|
|
|
|
|
if (EC) {
|
|
|
|
errs() << "Could not open file: " << EC.message();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
legacy::PassManager pass;
|
|
|
|
auto FileType = TargetMachine::CGFT_ObjectFile;
|
|
|
|
|
|
|
|
if (TheTargetMachine->addPassesToEmitFile(pass, dest, FileType)) {
|
|
|
|
errs() << "TheTargetMachine can't emit a file of this type";
|
|
|
|
return 1;
|
|
|
|
}
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
pass.run(*TheModule);
|
|
|
|
dest.flush();
|
2014-12-09 02:00:47 +08:00
|
|
|
|
2016-07-03 01:01:59 +08:00
|
|
|
outs() << "Wrote " << Filename << "\n";
|
2014-12-09 02:00:47 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|