|
@@ -1,18 +1,22 @@
|
|
|
#ifndef _AST_H
|
|
|
#define _AST_H
|
|
|
|
|
|
-//LLVM includes
|
|
|
+// LLVM includes
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
#include "llvm/IR/Module.h"
|
|
|
+#include "llvm/IR/DIBuilder.h"
|
|
|
+
|
|
|
+// Local includes
|
|
|
+#include "Lexer.h"
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
// Abstract Syntax Tree (aka Parse Tree)
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
-namespace ast{
|
|
|
+namespace ast {
|
|
|
// struct instantiated by the 'main' to access data structures if llvm IR.
|
|
|
struct AstObjects {
|
|
|
public:
|
|
@@ -21,12 +25,23 @@ public:
|
|
|
static std::map<std::string, AllocaInst*> NamedValues;
|
|
|
};
|
|
|
|
|
|
-/// ExprAST - Base class for all expression nodes.
|
|
|
+raw_ostream &indent(raw_ostream &O, int size) {
|
|
|
+ return O << std::string(size, ' ');
|
|
|
+}
|
|
|
|
|
|
+/// ExprAST - Base class for all expression nodes.
|
|
|
class ExprAST {
|
|
|
+ lexer::SourceLocation Loc;
|
|
|
+
|
|
|
public:
|
|
|
+ ExprAST(lexer::SourceLocation Loc = lexer::CurLoc) : Loc(Loc) {}
|
|
|
virtual ~ExprAST() {}
|
|
|
- virtual llvm::Value *codegen() = 0;
|
|
|
+ virtual Value *codegen() = 0;
|
|
|
+ int getLine() const { return Loc.Line; }
|
|
|
+ int getCol() const { return Loc.Col; }
|
|
|
+ virtual raw_ostream &dump(raw_ostream &out, int ind) {
|
|
|
+ return out << ':' << getLine() << ':' << getCol() << '\n';
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
/// NumberExprAST - Expression class for numeric literals like "1.0".
|
|
@@ -35,6 +50,9 @@ class NumberExprAST : public ExprAST {
|
|
|
|
|
|
public:
|
|
|
NumberExprAST(double Val) : Val(Val) {}
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) override {
|
|
|
+ return ExprAST::dump(out << Val, ind);
|
|
|
+ }
|
|
|
Value *codegen() override;
|
|
|
};
|
|
|
|
|
@@ -43,12 +61,15 @@ class VariableExprAST : public ExprAST {
|
|
|
std::string Name;
|
|
|
|
|
|
public:
|
|
|
- VariableExprAST(const std::string &Name) : Name(Name) {}
|
|
|
+ VariableExprAST(lexer::SourceLocation Loc, const std::string &Name)
|
|
|
+ : ExprAST(Loc), Name(Name) {}
|
|
|
const std::string &getName() const { return Name; }
|
|
|
Value *codegen() override;
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) override {
|
|
|
+ return ExprAST::dump(out << Name, ind);
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
-
|
|
|
/// UnaryExprAST - Expression class for a unary operator.
|
|
|
class UnaryExprAST : public ExprAST {
|
|
|
char Opcode;
|
|
@@ -58,6 +79,11 @@ public:
|
|
|
UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)
|
|
|
: Opcode(Opcode), Operand(std::move(Operand)) {}
|
|
|
Value *codegen() override;
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) override {
|
|
|
+ ExprAST::dump(out << "unary" << Opcode, ind);
|
|
|
+ Operand->dump(out, ind + 1);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
/// BinaryExprAST - Expression class for a binary operator.
|
|
@@ -66,33 +92,53 @@ class BinaryExprAST : public ExprAST {
|
|
|
std::unique_ptr<ExprAST> LHS, RHS;
|
|
|
|
|
|
public:
|
|
|
- BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
|
|
|
+ BinaryExprAST(lexer::SourceLocation Loc, char Op, std::unique_ptr<ExprAST> LHS,
|
|
|
std::unique_ptr<ExprAST> RHS)
|
|
|
- : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
|
|
|
+ : ExprAST(Loc), Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
|
|
|
Value *codegen() override;
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) override {
|
|
|
+ ExprAST::dump(out << "binary" << Op, ind);
|
|
|
+ LHS->dump(indent(out, ind) << "LHS:", ind + 1);
|
|
|
+ RHS->dump(indent(out, ind) << "RHS:", ind + 1);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
-/// ExprAST - Base class for all expression nodes.
|
|
|
/// CallExprAST - Expression class for function calls.
|
|
|
class CallExprAST : public ExprAST {
|
|
|
std::string Callee;
|
|
|
std::vector<std::unique_ptr<ExprAST>> Args;
|
|
|
|
|
|
public:
|
|
|
- CallExprAST(const std::string &Callee,
|
|
|
- std::vector<std::unique_ptr<ExprAST>> Args) : Callee(Callee), Args(std::move(Args)) {}
|
|
|
- Value *codegen() override;
|
|
|
+ CallExprAST(lexer::SourceLocation Loc, const std::string &Callee,
|
|
|
+ std::vector<std::unique_ptr<ExprAST>> Args)
|
|
|
+ : ExprAST(Loc), Callee(Callee), Args(std::move(Args)) {}
|
|
|
+ Value *codegen() override;
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) override {
|
|
|
+ ExprAST::dump(out << "call " << Callee, ind);
|
|
|
+ for (const auto &Arg : Args)
|
|
|
+ Arg->dump(indent(out, ind + 1), ind + 1);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
- /// IfExprAST - Expression class for if/then/else.
|
|
|
+/// IfExprAST - Expression class for if/then/else.
|
|
|
class IfExprAST : public ExprAST {
|
|
|
std::unique_ptr<ExprAST> Cond, Then, Else;
|
|
|
|
|
|
public:
|
|
|
- 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)) {}
|
|
|
- virtual Value *codegen();
|
|
|
+ IfExprAST(lexer::SourceLocation Loc, std::unique_ptr<ExprAST> Cond,
|
|
|
+ std::unique_ptr<ExprAST> Then, std::unique_ptr<ExprAST> Else)
|
|
|
+ : ExprAST(Loc), Cond(std::move(Cond)), Then(std::move(Then)),
|
|
|
+ Else(std::move(Else)) {}
|
|
|
+ Value *codegen() override;
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) override {
|
|
|
+ ExprAST::dump(out << "if", ind);
|
|
|
+ Cond->dump(indent(out, ind) << "Cond:", ind + 1);
|
|
|
+ Then->dump(indent(out, ind) << "Then:", ind + 1);
|
|
|
+ Else->dump(indent(out, ind) << "Else:", ind + 1);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
/// ForExprAST - Expression class for for/in.
|
|
@@ -104,9 +150,17 @@ public:
|
|
|
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)) {}
|
|
|
- virtual Value *codegen();
|
|
|
+ : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
|
|
|
+ Step(std::move(Step)), Body(std::move(Body)) {}
|
|
|
+ Value *codegen() override;
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) override {
|
|
|
+ ExprAST::dump(out << "for", ind);
|
|
|
+ Start->dump(indent(out, ind) << "Cond:", ind + 1);
|
|
|
+ End->dump(indent(out, ind) << "End:", ind + 1);
|
|
|
+ Step->dump(indent(out, ind) << "Step:", ind + 1);
|
|
|
+ Body->dump(indent(out, ind) << "Body:", ind + 1);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
/// VarExprAST - Expression class for var/in
|
|
@@ -120,6 +174,13 @@ public:
|
|
|
std::unique_ptr<ExprAST> Body)
|
|
|
: VarNames(std::move(VarNames)), Body(std::move(Body)) {}
|
|
|
Value *codegen() override;
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) override {
|
|
|
+ ExprAST::dump(out << "var", ind);
|
|
|
+ for (const auto &NamedVar : VarNames)
|
|
|
+ NamedVar.second->dump(indent(out, ind) << NamedVar.first << ':', ind + 1);
|
|
|
+ Body->dump(indent(out, ind) << "Body:", ind + 1);
|
|
|
+ return out;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
/// PrototypeAST - This class represents the "prototype" for a function,
|
|
@@ -130,12 +191,14 @@ class PrototypeAST {
|
|
|
std::vector<std::string> Args;
|
|
|
bool IsOperator;
|
|
|
unsigned Precedence; // Precedence if a binary op.
|
|
|
+ int Line;
|
|
|
|
|
|
public:
|
|
|
- PrototypeAST(const std::string &Name, std::vector<std::string> Args,
|
|
|
- bool IsOperator = false, unsigned Prec = 0)
|
|
|
+ PrototypeAST(lexer::SourceLocation Loc, const std::string &Name,
|
|
|
+ std::vector<std::string> Args, bool IsOperator = false,
|
|
|
+ unsigned Prec = 0)
|
|
|
: Name(Name), Args(std::move(Args)), IsOperator(IsOperator),
|
|
|
- Precedence(Prec) {}
|
|
|
+ Precedence(Prec), Line(Loc.Line) {}
|
|
|
Function *codegen();
|
|
|
const std::string &getName() const { return Name; }
|
|
|
|
|
@@ -148,7 +211,7 @@ public:
|
|
|
}
|
|
|
|
|
|
unsigned getBinaryPrecedence() const { return Precedence; }
|
|
|
- void CreateArgumentAllocas(Function *F);
|
|
|
+ int getLine() const { return Line; }
|
|
|
};
|
|
|
|
|
|
/// FunctionAST - This class represents a function definition itself.
|
|
@@ -161,15 +224,12 @@ public:
|
|
|
std::unique_ptr<ExprAST> Body)
|
|
|
: Proto(std::move(Proto)), Body(std::move(Body)) {}
|
|
|
Function *codegen();
|
|
|
+ raw_ostream &dump(raw_ostream &out, int ind) {
|
|
|
+ indent(out, ind) << "FunctionAST\n";
|
|
|
+ ++ind;
|
|
|
+ indent(out, ind) << "Body:";
|
|
|
+ return Body ? Body->dump(out, ind) : out << "null\n";
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
-llvm::Value *ErrorV(const char *Str);
|
|
|
-
|
|
|
-static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
|
|
|
- const std::string &VarName);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
#endif
|